Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import os
- import signal
- import gi
- import os.path
- import subprocess
- gi.require_version('Gtk', '3.0')
- gi.require_version('AppIndicator3', '0.1')
- from gi.repository import Gtk as gtk
- from gi.repository import AppIndicator3 as appindicator
- APPINDICATOR_ID = 'onedrive-icon'
- def main():
- global indicator
- icon = 'onedrive.svg'
- if isOnedriveRunning() == False:
- icon = 'onedrive-offline.svg'
- currentDirectory = os.path.dirname(os.path.realpath(__file__))
- print(currentDirectory)
- indicator = appindicator.Indicator.new(APPINDICATOR_ID, currentDirectory + "/" + icon, appindicator.IndicatorCategory.SYSTEM_SERVICES)
- indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
- indicator.set_menu(build_menu())
- gtk.main()
- def isOnedriveRunning():
- processname = 'onedrive'
- tmp = os.popen("ps -Af").read()
- proccount = tmp.count(processname)
- return proccount > 0;
- def set_icon(ico):
- currentDirectory = os.path.dirname(os.path.realpath(__file__))
- indicator.set_icon_full(icon_name=currentDirectory + "/" +ico, icon_desc="")
- def get_od_pid():
- processname = 'onedrive'
- return int(subprocess.check_output(["pidof", processname]))
- def build_menu():
- menu = gtk.Menu()
- if isOnedriveRunning() == False:
- add_turnOnItem(menu)
- else:
- add_pauseItem(menu)
- item_quit = gtk.MenuItem(label='Quit', use_underline=True)
- item_quit.connect('activate', quit)
- menu.append(item_quit)
- menu.show_all()
- return menu
- def add_turnOnItem(menu):
- item_turnOn = gtk.MenuItem(label='Start synchronization', use_underline=True)
- item_turnOn.connect('activate', start_sync)
- menu.append(item_turnOn)
- def add_pauseItem(menu):
- item_pause = gtk.MenuItem(label='Pause synchronization', use_underline=True)
- item_pause.connect('activate', pause_sync)
- menu.append(item_pause)
- def quit(source):
- os.kill(get_od_pid(), signal.SIGTERM)
- gtk.main_quit()
- def start_sync(source):
- subprocess.Popen(['/usr/local/bin/onedrive --monitor'], shell=True)
- set_icon("onedrive.svg")
- indicator.set_menu(build_menu())
- def pause_sync(source):
- if isOnedriveRunning() == True:
- print(get_od_pid())
- os.kill(get_od_pid(), signal.SIGTERM)
- set_icon("onedrive-offline.svg")
- indicator.set_menu(build_menu())
- if __name__ == "__main__":
- signal.signal(signal.SIGINT, signal.SIG_DFL)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement