144 lines
4.2 KiB
JavaScript
144 lines
4.2 KiB
JavaScript
// Example#8
|
|
|
|
imports.gi.versions.Gtk = "4.0";
|
|
const { Gtk } = imports.gi;
|
|
|
|
const Main = imports.ui.main;
|
|
const St = imports.gi.St;
|
|
const ByteArray = imports.byteArray;
|
|
const GLib = imports.gi.GLib;
|
|
const GObject = imports.gi.GObject;
|
|
const Gio = imports.gi.Gio;
|
|
const Soup = imports.gi.Soup;
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
const Me = imports.misc.extensionUtils.getCurrentExtension();
|
|
|
|
const version = Gtk.get_major_version();
|
|
|
|
log(`GTK version is ${version}`);
|
|
|
|
let myPopup;
|
|
let started = false;
|
|
let key = get_config('key');
|
|
|
|
const MyPopup = GObject.registerClass(
|
|
class MyPopup extends PanelMenu.Button {
|
|
|
|
_init () {
|
|
|
|
super._init(0);
|
|
|
|
let icon = new St.Icon({
|
|
//icon_name : 'security-low-symbolic',
|
|
gicon : Gio.icon_new_for_string( Me.dir.get_path() + '/logo.png' ),
|
|
style_class : 'system-status-icon',
|
|
});
|
|
|
|
this.add_child(icon);
|
|
|
|
let today = new Date()
|
|
let time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
|
|
|
|
let timeItem = new PopupMenu.PopupMenuItem('');
|
|
let timeLabel = new St.Label({text:'Capital actuel : +0:00'});
|
|
timeItem.add(timeLabel);
|
|
|
|
this.menu.addMenuItem(timeItem);
|
|
|
|
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
|
|
|
let startItem = new PopupMenu.PopupMenuItem('');
|
|
let label = new St.Label({
|
|
text:'DÉBUT POINTAGE',
|
|
style_class:'timer-btn'
|
|
});
|
|
startItem.add(label);
|
|
|
|
this.menu.addMenuItem(startItem);
|
|
|
|
this.menu.connect('open-state-changed', (menu, open) => {
|
|
if (open) {
|
|
log('opened');
|
|
let result = send_request('https://badge.cadoles.com/ninebadge/rest/showtimers', 'POST', key)
|
|
timeLabel.set_text('Capital actuel : ' + result.capitaltime)
|
|
let status = send_request('https://badge.cadoles.com/ninebadge/rest/status', 'POST', key)
|
|
if(status.status == "started") {
|
|
label.set_text('FIN POINTAGE');
|
|
started = true
|
|
} else {
|
|
started = false
|
|
label.set_text('DÉBUT POINTAGE');
|
|
}
|
|
}
|
|
});
|
|
|
|
|
|
startItem.connect('activate', () => {
|
|
try {
|
|
let result = send_request('https://badge.cadoles.com/ninebadge/rest/showtimers', 'POST', key)
|
|
timeLabel.set_text('CAPITAL : ' + result.capitaltime)
|
|
} catch(error) {
|
|
log(error);
|
|
}
|
|
if(started){
|
|
send_request('https://badge.cadoles.com/ninebadge/rest/clockout', 'POST', key)
|
|
started = false
|
|
label.set_text('DÉBUT POINTAGE');
|
|
} else {
|
|
send_request('https://badge.cadoles.com/ninebadge/rest/clockin', 'POST', key)
|
|
label.set_text('FIN POINTAGE');
|
|
started = true
|
|
}
|
|
log(started);
|
|
});
|
|
|
|
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
|
|
|
let validateItem = new PopupMenu.PopupMenuItem('');
|
|
let validateLabel = new St.Label({
|
|
text:'VALIDER MA JOURNÉE',
|
|
style_class:'validate-btn'
|
|
});
|
|
validateItem.add(validateLabel);
|
|
this.menu.addMenuItem(validateItem);
|
|
|
|
validateItem.connect('activate', () => {
|
|
send_request('https://badge.cadoles.com/ninebadge/rest/clockout', 'POST', key)
|
|
started = false
|
|
label.set_text('DÉBUT POINTAGE');
|
|
send_request('https://badge.cadoles.com/ninebadge/rest/validate', 'POST', key)
|
|
});
|
|
|
|
// you can close, open and toggle the menu with
|
|
// this.menu.close();
|
|
// this.menu.open();
|
|
// this.menu.toggle();
|
|
}
|
|
});
|
|
|
|
function send_request(url, type='POST', key) {
|
|
let [ok, out, err, exit] = GLib.spawn_command_line_sync(`/usr/bin/curl --insecure -X ${type} -d '{"key": "${key}"}' -H "Content-Type: application/json" ${url} -o /tmp/test.json`);
|
|
let text = GLib.file_get_contents('/tmp/test.json')[1];
|
|
let json_result = JSON.parse(text);
|
|
|
|
return json_result
|
|
}
|
|
|
|
function get_config(property) {
|
|
let text = GLib.file_get_contents(Me.dir.get_path() + '/config.json')[1];
|
|
let json_result = JSON.parse(text);
|
|
return json_result[property];
|
|
}
|
|
|
|
function init() {
|
|
}
|
|
|
|
function enable() {
|
|
myPopup = new MyPopup();
|
|
Main.panel.addToStatusArea('myPopup', myPopup, 1);
|
|
}
|
|
|
|
function disable() {
|
|
myPopup.destroy();
|
|
} |