Created
October 14, 2020 14:14
-
-
Save consoleaf/f764882ed425a54a57f6cb9f52fae22a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* extension.js | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 2 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
* | |
* SPDX-License-Identifier: GPL-2.0-or-later | |
*/ | |
/* exported init */ | |
const Main = imports.ui.main; | |
const St = imports.gi.St; | |
const GObject = imports.gi.GObject; | |
const Gio = imports.gi.Gio; | |
const GLib = imports.gi.GLib; | |
const PanelMenu = imports.ui.panelMenu; | |
const PopupMenu = imports.ui.popupMenu; | |
const Me = imports.misc.extensionUtils.getCurrentExtension(); | |
const CURL_PATH = GLib.find_program_in_path('curl') | |
let myPopup; | |
function execCommand(argv, input = null, cancellable = null) { | |
let flags = Gio.SubprocessFlags.STDOUT_PIPE; | |
if (input !== null) | |
flags |= Gio.SubprocessFlags.STDIN_PIPE; | |
let proc = new Gio.Subprocess({ | |
argv: argv, | |
flags: flags | |
}); | |
proc.init(cancellable); | |
return new Promise((resolve, reject) => { | |
proc.communicate_utf8_async(input, cancellable, (proc, res) => { | |
try { | |
resolve(proc.communicate_utf8_finish(res)[1]); | |
} catch (e) { | |
reject(e); | |
} | |
}); | |
}); | |
} | |
async function getRelativeSchedule(relative) { | |
const date = (await execCommand(['/usr/bin/date', "+%Y.%m.%d", '-d', relative])).trim() // '2020.10.14' | |
const url = `https://ruz.hse.ru/api/schedule/student/216333?start=${date}&finish=${date}`; | |
const result = JSON.parse(await execCommand([CURL_PATH, url, '-q'])); | |
return result; | |
} | |
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('calendar-go-today'), | |
style_class: 'system-status-icon', | |
}); | |
this.add_child(icon); | |
let pmItem = new PopupMenu.PopupMenuItem('Schedule. Click here to update'); | |
// this.menu.addMenuItem(pmItem); | |
pmItem.connect('activate', (e) => { | |
// TODO: Grab new schedule data | |
}); | |
// sub menu | |
let todayItem = new PopupMenu.PopupSubMenuMenuItem('Сегодня'); | |
this.menu.addMenuItem(todayItem); | |
let tomorrowItem = new PopupMenu.PopupSubMenuMenuItem('Завтра'); | |
this.menu.addMenuItem(tomorrowItem); | |
this.menu.connect('open-state-changed', (popup, open) => { | |
if (!open) return; | |
todayItem.menu.removeAll() | |
todayItem.menu.addMenuItem(new PopupMenu.PopupMenuItem('Загрузка...')) | |
tomorrowItem.menu.removeAll() | |
todayItem.menu.open() | |
getRelativeSchedule('').then(res => { | |
todayItem.menu.removeAll(); | |
for (let item of res) | |
todayItem.menu.addMenuItem(new PopupMenu.PopupMenuItem(`${item.beginLesson}: ${item.discipline}\n${item.auditorium} - ${item.kindOfWork}`)); | |
todayItem.menu.open() | |
}) | |
getRelativeSchedule('tomorrow').then(res => { | |
tomorrowItem.menu.removeAll(); | |
for (let item of res) | |
tomorrowItem.menu.addMenuItem(new PopupMenu.PopupMenuItem(`${item.beginLesson}: ${item.discipline}\n${item.auditorium} - ${item.kindOfWork}`)); | |
if (res.length === 0) | |
tomorrowItem.menu.addMenuItem(new PopupMenu.PopupMenuItem('Занятий не найдено!', 0)) | |
}) | |
}) | |
} | |
}); | |
class Extension { | |
constructor() {} | |
enable() { | |
myPopup = new MyPopup(); | |
Main.panel.addToStatusArea('myPopup', myPopup, 1) | |
} | |
disable() { | |
myPopup.destroy() | |
} | |
} | |
function init() { | |
return new Extension(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment