Guest User

Untitled

a guest
Jan 11th, 2019
140
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import os
  3. import signal
  4. import gi
  5. import os.path
  6. import subprocess
  7.  
  8. gi.require_version('Gtk', '3.0')
  9. gi.require_version('AppIndicator3', '0.1')
  10.  
  11. from gi.repository import Gtk as gtk
  12. from gi.repository import AppIndicator3 as appindicator
  13.  
  14. APPINDICATOR_ID = 'onedrive-icon'
  15.  
  16. def main():
  17.     global indicator
  18.  
  19.     icon = 'onedrive.svg'
  20.  
  21.     if isOnedriveRunning() == False:
  22.         icon = 'onedrive-offline.svg'
  23.  
  24.     currentDirectory =  os.path.dirname(os.path.realpath(__file__))
  25.     print(currentDirectory)
  26.     indicator = appindicator.Indicator.new(APPINDICATOR_ID, currentDirectory + "/" + icon, appindicator.IndicatorCategory.SYSTEM_SERVICES)
  27.     indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
  28.     indicator.set_menu(build_menu())
  29.     gtk.main()
  30.  
  31. def isOnedriveRunning():
  32.     processname = 'onedrive'
  33.     tmp = os.popen("ps -Af").read()
  34.     proccount = tmp.count(processname)
  35.  
  36.     return proccount > 0;
  37.  
  38. def set_icon(ico):
  39.     currentDirectory =  os.path.dirname(os.path.realpath(__file__))
  40.     indicator.set_icon_full(icon_name=currentDirectory + "/" +ico, icon_desc="")
  41.  
  42. def get_od_pid():
  43.     processname = 'onedrive'
  44.     return int(subprocess.check_output(["pidof", processname]))
  45.  
  46. def build_menu():
  47.     menu = gtk.Menu()
  48.  
  49.     if isOnedriveRunning() == False:
  50.         add_turnOnItem(menu)
  51.     else:
  52.         add_pauseItem(menu)
  53.  
  54.     item_quit = gtk.MenuItem(label='Quit', use_underline=True)
  55.     item_quit.connect('activate', quit)
  56.     menu.append(item_quit)
  57.  
  58.     menu.show_all()
  59.     return menu
  60.  
  61. def add_turnOnItem(menu):
  62.     item_turnOn = gtk.MenuItem(label='Start synchronization', use_underline=True)
  63.     item_turnOn.connect('activate', start_sync)
  64.     menu.append(item_turnOn)
  65.  
  66. def add_pauseItem(menu):
  67.     item_pause = gtk.MenuItem(label='Pause synchronization', use_underline=True)
  68.     item_pause.connect('activate', pause_sync)
  69.     menu.append(item_pause)
  70.  
  71. def quit(source):
  72.     os.kill(get_od_pid(), signal.SIGTERM)
  73.     gtk.main_quit()
  74.  
  75. def start_sync(source):
  76.     subprocess.Popen(['/usr/local/bin/onedrive --monitor'], shell=True)
  77.     set_icon("onedrive.svg")
  78.     indicator.set_menu(build_menu())
  79.  
  80. def pause_sync(source):
  81.     if isOnedriveRunning() == True:
  82.         print(get_od_pid())
  83.         os.kill(get_od_pid(), signal.SIGTERM)
  84.  
  85.     set_icon("onedrive-offline.svg")
  86.     indicator.set_menu(build_menu())
  87.  
  88. if __name__ == "__main__":
  89.     signal.signal(signal.SIGINT, signal.SIG_DFL)
  90.     main()
RAW Paste Data