Created
February 4, 2019 11:08
-
-
Save bunyk/615d69abf89c8d9f88c95dd071c869e2 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
"""Widget to check office time without opening page. | |
https://askubuntu.com/questions/751608/how-can-i-write-a-dynamically-updated-panel-app-indicator | |
http://candidtim.github.io/appindicator/2014/09/13/ubuntu-appindicator-step-by-step.html | |
https://python-gtk-3-tutorial.readthedocs.io/en/latest/introduction.html | |
""" | |
from datetime import timedelta | |
import os | |
from time import sleep | |
from threading import Thread | |
import gi | |
gi.require_version('Gtk', '3.0') | |
gi.require_version('AppIndicator3', '0.1') | |
# pylint: disable=wrong-import-position | |
from gi.repository import GLib, Gtk, AppIndicator3 | |
# from gl_client import get_duration, show_details | |
USERNAME = 'taras.bunyk' | |
USER_ID = 2466 | |
PASSWORD = '' | |
def main(): | |
"""Run our widget""" | |
# OfficeTimeIndicator() | |
win = SettingsWindow(USERNAME, PASSWORD) | |
win.show_all() | |
Gtk.main() | |
class SettingsWindow(Gtk.Window): | |
"""Window to set credentials""" | |
def __init__(self, username, password): | |
self.username = username | |
self.password = password | |
self.user_id = USER_ID | |
super().__init__(title="OfficeTime Settings") | |
self.set_border_width(10) | |
self.username_entry = Gtk.Entry(text=self.username) | |
self.password_entry = Gtk.Entry(text=self.password, visibility=False) | |
cancel_button = Gtk.Button(label="Cancel") | |
cancel_button.connect("clicked", self.cancel) | |
ok_button = Gtk.Button(label="OK") | |
ok_button.connect("clicked", self.submit) | |
self.add(vbox( | |
hbox( | |
Gtk.Label(label="Username:"), | |
self.username_entry, | |
), | |
hbox( | |
Gtk.Label(label="Password:"), | |
self.password_entry, | |
), | |
hbox( | |
Gtk.Label(label="User ID:"), | |
Gtk.Entry(text=self.user_id), | |
), | |
hbox( | |
ok_button, | |
cancel_button, | |
) | |
)) | |
self.connect("destroy", self.cancel) | |
def cancel(self, _): | |
self.username_entry.set_text(self.username) | |
self.password_entry.set_text(self.password) | |
self.hide() | |
def submit(self, _): | |
self.username = self.username_entry.get_text() | |
self.password = self.password_entry.get_text() | |
self.hide() | |
def create_box(orientation, *subcomponents): | |
"""Creates layout box""" | |
box = Gtk.Box(orientation=orientation, spacing=6) | |
for component in subcomponents: | |
box.pack_start(component, True, True, 0) | |
return box | |
def hbox(*subcomponents): | |
"""Horizontal box""" | |
return create_box(Gtk.Orientation.HORIZONTAL, *subcomponents) | |
def vbox(*subcomponents): | |
"""Vertical box""" | |
return create_box(Gtk.Orientation.VERTICAL, *subcomponents) | |
class OfficeTimeIndicator(): | |
"""Class to hold indicator actions and state""" | |
APPINDICATOR_ID = 'officetimeindicator' | |
def __init__(self): | |
self.indicator = AppIndicator3.Indicator.new( | |
self.APPINDICATOR_ID, | |
os.path.abspath('./icon.ico'), | |
AppIndicator3.IndicatorCategory.SYSTEM_SERVICES | |
) | |
self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE) | |
self.indicator.set_menu(build_menu()) | |
update = Thread(target=self.update_loop) | |
update.setDaemon(True) # daemonize the thread to make the indicator stoppable | |
update.start() | |
def update_loop(self): | |
"""Update label sometimes""" | |
duration = timedelta() | |
while True: | |
duration = get_duration('TODO', 'TODO', 'TODO') | |
for _ in range(10): # update time without requests to server for 10 minutes | |
GLib.idle_add( | |
self.indicator.set_label, | |
':'.join(str(duration).split(':')[:-1]), | |
self.APPINDICATOR_ID, | |
priority=GLib.PRIORITY_DEFAULT | |
) | |
sleep(60) | |
duration += timedelta(seconds=60) | |
def stop(_): | |
"""Exit program""" | |
Gtk.main_quit() | |
def configure(_): | |
"""Show settings window""" | |
print("TODO") | |
def build_menu(): | |
"""Our widget menu""" | |
menu = Gtk.Menu() | |
item_time = Gtk.MenuItem(label='Details') | |
item_time.connect('activate', show_details) | |
menu.append(item_time) | |
item_config = Gtk.MenuItem(label='Configure') | |
item_config.connect('activate', configure) | |
menu.append(item_config) | |
item_quit = Gtk.MenuItem(label='Quit') | |
item_quit.connect('activate', stop) | |
menu.append(item_quit) | |
menu.show_all() | |
return menu | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment