Skip to content
Snippets Groups Projects
Commit 89b63302 authored by Alberto LIVIO BECCARIA's avatar Alberto LIVIO BECCARIA
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
# minimal sample showing how to write GTK3 indicator for Ubuntu Unity
# copyright 2012 Charl P. Botha <info@charlbotha.com>
# hereby released under the BSD license.
# use the PyGObject GObject introspection to use GTK+ 3
# also see
# http://readthedocs.org/docs/python-gtk-3-tutorial/en/latest/index.html
# http://developer.gnome.org/gtk3/stable/ (API reference)
from gi.repository import Gtk, GLib
try:
from gi.repository import AppIndicator3 as AppIndicator
except:
from gi.repository import AppIndicator
import re
class IndicatorCPUSpeed:
def __init__(self):
# param1: identifier of this indicator
# param2: name of icon. this will be searched for in the standard them
# dirs
# finally, the category. We're monitoring CPUs, so HARDWARE.
self.ind = AppIndicator.Indicator.new(
"indicator-cpuspeed",
"onboard-mono",
AppIndicator.IndicatorCategory.HARDWARE)
# some more information about the AppIndicator:
# http://developer.ubuntu.com/api/ubuntu-12.04/python/AppIndicator3-0.1.html
# http://developer.ubuntu.com/resources/technologies/application-indicators/
# need to set this for indicator to be shown
self.ind.set_status (AppIndicator.IndicatorStatus.ACTIVE)
# have to give indicator a menu
self.menu = Gtk.Menu()
# you can use this menu item for experimenting
item = Gtk.MenuItem()
item.set_label("Test")
item.connect("activate", self.handler_menu_test)
item.show()
self.menu.append(item)
# this is for exiting the app
item = Gtk.MenuItem()
item.set_label("Exit")
item.connect("activate", self.handler_menu_exit)
item.show()
self.menu.append(item)
self.menu.show()
self.ind.set_menu(self.menu)
# initialize cpu speed display
self.update_cpu_speeds()
# then start updating every 2 seconds
# http://developer.gnome.org/pygobject/stable/glib-functions.html#function-glib--timeout-add-seconds
GLib.timeout_add_seconds(2, self.handler_timeout)
def get_cpu_speeds(self):
"""Use regular expression to parse speeds of all CPU cores from
/proc/cpuinfo on Linux.
"""
f = open('/proc/cpuinfo')
# this gives us e.g. ['2300', '2300']
s = re.findall('cpu MHz\s*:\s*(\d+)\.', f.read())
# this will give us ['2.3', '2.3']
f = ['%.1f' % (float(i) / 1000,) for i in s]
return f
def handler_menu_exit(self, evt):
Gtk.main_quit()
def handler_menu_test(self, evt):
# we can change the icon at any time
self.ind.set_icon("indicator-messages-new")
def handler_timeout(self):
"""This will be called every few seconds by the GLib.timeout.
"""
# read, parse and put cpu speeds in the label
self.update_cpu_speeds()
# return True so that we get called again
# returning False will make the timeout stop
return True
def update_cpu_speeds(self):
f = self.get_cpu_speeds()
self.ind.set_label(' '.join(f), "8.8 8.8 8.8 8.8")
def main(self):
Gtk.main()
if __name__ == "__main__":
ind = IndicatorCPUSpeed()
ind.main()
#!/usr/bin/python
# -*- coding: utf-8 -*-
import signal
import os
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Notify', '0.7')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Notify
from gi.repository import Gtk
import subprocess
################################################################################
# CONFIG
################################################################################
APP_NAME = "labsync-tray"
APP_VERSION = "0.1-2019"
APP_DESCR = "Notifica lo stato di aggiornamento del software nella cartella /opt"
APP_AUTHORS = ['Sezione di Informatica, DiSIT, UPO']
APP_ID = "labsync-tray"
CURR_DIR = os.path.dirname(os.path.abspath(__file__))
# could be PNG or SVG as well
APP_ICON = os.path.join(CURR_DIR, 'sample_icon.svg')
# force using StatusIcon over AppIndicator
FORCE_TRAY = True
################################################################################
################################################################################
class TrayApp:
def __init__(self, appid, icon, menu=None):
if menu != None:
self.menu
else:
self.menu = self.build_menu()
self.menu.show_all()
APPIND_SUPPORT = 1
try:
from gi.repository import AppIndicator3
except:
APPIND_SUPPORT = 1
if APPIND_SUPPORT == 1 and FORCE_TRAY == False:
self.ind = AppIndicator3.Indicator.new(
appid, icon, AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
self.ind.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
self.ind.set_menu(self.menu)
else:
self.ind = Gtk.StatusIcon()
self.ind.set_visible(True);
self.ind.connect('popup-menu', self.onPopupMenu)
self.ind.set_tooltip_text(APP_NAME)
self.ind.set_title(APP_NAME)
if icon != "":
self.ind.set_from_file(icon)
else:
self.ind.set_from_stock(Gtk.STOCK_INFO)
def onPopupMenu(self, icon, button, time):
self.menu.popup(None, None, Gtk.StatusIcon.position_menu, icon, button, time)
def build_menu(self):
menu = Gtk.Menu()
item_status = Gtk.MenuItem('Status')
item_status.connect("activate", self.handler_menu_status)
menu.append(item_status)
about = Gtk.MenuItem("About")
about.show()
about.connect("activate", self.handler_menu_about)
menu.append(about)
menu.append(Gtk.SeparatorMenuItem())
item_quit = Gtk.MenuItem("Quit")
item_quit.connect("activate", self.handler_menu_quit)
menu.append(item_quit)
return menu
def handler_menu_quit(self, evt):
Notify.uninit()
Gtk.main_quit()
def handler_menu_status(self, evt):
Notify.Notification.new("labsync", "<b>Test</b> message").show()
def handler_menu_about(self, evt):
about_dialog = Gtk.AboutDialog()
about_dialog.set_destroy_with_parent(True)
about_dialog.set_icon_name("dialog-information")
# no SVG?
about_dialog.set_logo_icon_name("")
about_dialog.set_program_name(APP_NAME)
about_dialog.set_name(APP_NAME)
about_dialog.set_version(APP_VERSION)
about_dialog.set_license_type(Gtk.License.GPL_3_0)
about_dialog.set_comments(APP_DESCR)
about_dialog.set_authors(APP_AUTHORS)
about_dialog.set_default_response(Gtk.ResponseType.CLOSE)
print APP_ICON
about_dialog.run()
about_dialog.destroy()
def main(self):
Gtk.main()
if __name__ == "__main__":
# Handle pressing Ctr+C properly, ignored by default
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = TrayApp(APP_ID, APP_ICON)
Notify.init(APP_ID)
app.main()
#!/usr/bin/env python2
# coding:utf-8
# Contributor:
# Phus Lu <phus.lu@gmail.com>
__version__ = '1.6'
GOAGENT_LOGO_DATA = """\
iVBORw0KGgoAAAANSUhEUgAAADcAAAA3CAYAAACo29JGAAAABHNCSVQICAgIfAhkiAAADVdJREFU
aIHtmnuMXPV1xz/ndx8z4/UT8bKTEscOBa1FAt2kSYvAECxegTZRu6uGJDQJ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nzt
Ojvnnsbhnrv9xMAVAEcjs8mikv/B2Ln4fAz8RYjTWWFpsbMXgMsDNH0T8STO7SIkd9rZX/ln4IhP
TI/Wjlv1JdUdNKwHcvfumFNuPQPLziD3m3DRGyBPkWsS2s/h4qcI+g/uPOcJK79rWbTv0bEy9rqZ
tDXWvuH0qD8PponhVLu3Dv6dmGXsda2bpdGIxr6lvzy3D2CLt7HJY3a/n9r/N/sfrBt2air9qXQA
AAAASUVORK5CYII="""
import sys
import os
import re
import thread
import base64
import platform
try:
import pygtk
pygtk.require('2.0')
import gtk
# gtk.gdk.threads_init()
except Exception:
sys.exit(os.system(u'gdialog --title "GoAgent GTK" --msgbox "\u8bf7\u5b89\u88c5 python-gtk2" 15 60'.encode(sys.getfilesystemencoding() or sys.getdefaultencoding(), 'replace')))
try:
import pynotify
pynotify.init('GoAgent Notify')
except ImportError:
pynotify = None
try:
import appindicator
except ImportError:
appindicator = None
try:
import vte
except ImportError:
sys.exit(gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, u'请安装 python-vte').run())
def spawn_later(seconds, target, *args, **kwargs):
def wrap(*args, **kwargs):
import time
time.sleep(seconds)
return target(*args, **kwargs)
return thread.start_new_thread(wrap, args, kwargs)
def drop_desktop():
filename = os.path.abspath(__file__)
dirname = os.path.dirname(filename)
DESKTOP_FILE = '''\
#!/usr/bin/env xdg-open
[Desktop Entry]
Type=Application
Name=GoAgent GTK
Comment=GoAgent GTK Launcher
Categories=Network;Proxy;
Exec=/usr/bin/env python "%s"
Icon=%s/goagent-logo.png
Terminal=false
StartupNotify=true
''' % (filename, dirname)
for dirname in map(os.path.expanduser, ['~/Desktop', u'~/桌面']):
if os.path.isdir(dirname):
filename = os.path.join(dirname, 'goagent-gtk.desktop')
with open(filename, 'w') as fp:
fp.write(DESKTOP_FILE)
os.chmod(filename, 0755)
def should_visible():
import ConfigParser
ConfigParser.RawConfigParser.OPTCRE = re.compile(r'(?P<option>[^=\s][^=]*)\s*(?P<vi>[=])\s*(?P<value>.*)$')
config = ConfigParser.ConfigParser()
config.read(['proxy.ini', 'proxy.user.ini'])
visible = config.has_option('listen', 'visible') and config.getint('listen', 'visible')
return visible
#gtk.main_quit = lambda: None
#appindicator = None
class GoAgentGTK:
command = ['/usr/bin/env', 'python', 'proxy.py']
message = u'GoAgent已经启动,单击托盘图标可以最小化'
fail_message = u'GoAgent启动失败,请查看控制台窗口的错误信息。'
def __init__(self, window, terminal):
self.window = window
self.window.set_size_request(652, 447)
self.window.set_position(gtk.WIN_POS_CENTER)
self.window.connect('delete-event',self.delete_event)
self.terminal = terminal
for cmd in ('python2.7', 'python27', 'python2'):
if os.system('which %s' % cmd) == 0:
self.command[1] = cmd
break
self.window.add(terminal)
self.childpid = self.terminal.fork_command(self.command[0], self.command, os.getcwd())
if self.childpid > 0:
self.childexited = self.terminal.connect('child-exited', self.on_child_exited)
self.window.connect('delete-event', lambda w, e: gtk.main_quit())
else:
self.childexited = None
spawn_later(0.5, self.show_startup_notify)
if should_visible():
self.window.show_all()
logo_filename = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'goagent-logo.png')
if not os.path.isfile(logo_filename):
with open(logo_filename, 'wb') as fp:
fp.write(base64.b64decode(GOAGENT_LOGO_DATA))
self.window.set_icon_from_file(logo_filename)
if appindicator:
self.trayicon = appindicator.Indicator('GoAgent', 'indicator-messages', appindicator.CATEGORY_APPLICATION_STATUS)
self.trayicon.set_status(appindicator.STATUS_ACTIVE)
self.trayicon.set_attention_icon('indicator-messages-new')
self.trayicon.set_icon(logo_filename)
self.trayicon.set_menu(self.make_menu())
else:
self.trayicon = gtk.StatusIcon()
self.trayicon.set_from_file(logo_filename)
self.trayicon.connect('popup-menu', lambda i, b, t: self.make_menu().popup(None, None, gtk.status_icon_position_menu, b, t, self.trayicon))
self.trayicon.connect('activate', self.show_hide_toggle)
self.trayicon.set_tooltip('GoAgent')
self.trayicon.set_visible(True)
def make_menu(self):
menu = gtk.Menu()
itemlist = [(u'\u663e\u793a', self.on_show),
(u'\u9690\u85cf', self.on_hide),
(u'\u505c\u6b62', self.on_stop),
(u'\u91cd\u65b0\u8f7d\u5165', self.on_reload),
(u'\u9000\u51fa', self.on_quit)]
for text, callback in itemlist:
item = gtk.MenuItem(text)
item.connect('activate', callback)
item.show()
menu.append(item)
menu.show()
return menu
def show_notify(self, message=None, timeout=None):
if pynotify and message:
notification = pynotify.Notification('GoAgent Notify', message)
notification.set_hint('x', 200)
notification.set_hint('y', 400)
if timeout:
notification.set_timeout(timeout)
notification.show()
def show_startup_notify(self):
if self.check_child_exists():
self.show_notify(self.message, timeout=3)
def check_child_exists(self):
if self.childpid <= 0:
return False
cmd = 'ps -p %s' % self.childpid
lines = os.popen(cmd).read().strip().splitlines()
if len(lines) < 2:
return False
return True
def on_child_exited(self, term):
if self.terminal.get_child_exit_status() == 0:
gtk.main_quit()
else:
self.show_notify(self.fail_message)
def on_show(self, widget, data=None):
self.window.show_all()
self.window.present()
self.terminal.feed('\r')
def on_hide(self, widget, data=None):
self.window.hide_all()
def on_stop(self, widget, data=None):
if self.childexited:
self.terminal.disconnect(self.childexited)
os.system('kill -9 %s' % self.childpid)
def on_reload(self, widget, data=None):
if self.childexited:
self.terminal.disconnect(self.childexited)
os.system('kill -9 %s' % self.childpid)
self.on_show(widget, data)
self.childpid = self.terminal.fork_command(self.command[0], self.command, os.getcwd())
self.childexited = self.terminal.connect('child-exited', lambda term: gtk.main_quit())
def show_hide_toggle(self, widget, data= None):
if self.window.get_property('visible'):
self.on_hide(widget, data)
else:
self.on_show(widget, data)
def delete_event(self, widget, data=None):
self.on_hide(widget, data)
# 默认最小化至托盘
return True
def on_quit(self, widget, data=None):
gtk.main_quit()
def main():
global __file__
__file__ = os.path.abspath(__file__)
if os.path.islink(__file__):
__file__ = getattr(os, 'readlink', lambda x: x)(__file__)
os.chdir(os.path.dirname(os.path.abspath(__file__)))
if not os.path.exists('goagent-logo.png'):
# first run and drop shortcut to desktop
drop_desktop()
window = gtk.Window()
terminal = vte.Terminal()
GoAgentGTK(window, terminal)
gtk.main()
if __name__ == '__main__':
main()
#!/usr/bin/python
# -*- coding: utf-8 -*-
# minimal sample showing how to write GTK3 indicator for Ubuntu Unity
# copyright 2012 Charl P. Botha <info@charlbotha.com>
# hereby released under the BSD license.
# use the PyGObject GObject introspection to use GTK+ 3
# also see
# http://readthedocs.org/docs/python-gtk-3-tutorial/en/latest/index.html
# http://developer.gnome.org/gtk3/stable/ (API reference)
from gi.repository import Gtk, GLib
try:
from gi.repository import AppIndicator3 as AppIndicator
except:
from gi.repository import AppIndicator
import re
class IndicatorCPUSpeed:
def __init__(self):
# param1: identifier of this indicator
# param2: name of icon. this will be searched for in the standard them
# dirs
# finally, the category. We're monitoring CPUs, so HARDWARE.
self.ind = AppIndicator.Indicator.new(
"indicator-cpuspeed",
"dialog-information",
AppIndicator.IndicatorCategory.HARDWARE)
# some more information about the AppIndicator:
# http://developer.ubuntu.com/api/ubuntu-12.04/python/AppIndicator3-0.1.html
# http://developer.ubuntu.com/resources/technologies/application-indicators/
# need to set this for indicator to be shown
self.ind.set_status (AppIndicator.IndicatorStatus.ACTIVE)
# have to give indicator a menu
self.menu = Gtk.Menu()
# you can use this menu item for experimenting
item = Gtk.MenuItem()
item.set_label("Test")
item.connect("activate", self.handler_menu_test)
item.show()
self.menu.append(item)
# this is for exiting the app
item = Gtk.MenuItem()
item.set_label("Exit")
item.connect("activate", self.handler_menu_exit)
item.show()
self.menu.append(item)
self.menu.show()
self.ind.set_menu(self.menu)
# initialize cpu speed display
self.update_cpu_speeds()
# then start updating every 2 seconds
# http://developer.gnome.org/pygobject/stable/glib-functions.html#function-glib--timeout-add-seconds
GLib.timeout_add_seconds(2, self.handler_timeout)
def get_cpu_speeds(self):
"""Use regular expression to parse speeds of all CPU cores from
/proc/cpuinfo on Linux.
"""
f = open('/proc/cpuinfo')
# this gives us e.g. ['2300', '2300']
s = re.findall('cpu MHz\s*:\s*(\d+)\.', f.read())
# this will give us ['2.3', '2.3']
f = ['%.1f' % (float(i) / 1000,) for i in s]
return f
def handler_menu_exit(self, evt):
Gtk.main_quit()
def handler_menu_test(self, evt):
# we can change the icon at any time
self.ind.set_icon("dialog-information")
def handler_timeout(self):
"""This will be called every few seconds by the GLib.timeout.
"""
# read, parse and put cpu speeds in the label
self.update_cpu_speeds()
# return True so that we get called again
# returning False will make the timeout stop
return True
def update_cpu_speeds(self):
f = self.get_cpu_speeds()
self.ind.set_label(' '.join(f), "8.8 8.8 8.8 8.8")
def main(self):
Gtk.main()
if __name__ == "__main__":
ind = IndicatorCPUSpeed()
ind.main()
[Unit]
Description=labsync-notify-user: notify user that labsync is running (user %u)
PartOf=graphical-session.target
ConditionUser=!root
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/sbin/labsync-notify-user.sh
[Install]
WantedBy=default.target
[Unit]
Description=labsync-notify: notify user that labsync is running
[Service]
User=labsync
ExecStart=/usr/local/sbin/labsync-notify.sh
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=labsync.service
#!/bin/bash
# send a notification to the logged in user (graphical session only)
# seconds between checks
CYCLE_PERIOD=30
# how long the notification stays on screen (milliseconds, 0=until user clicks)
NOTIFY_TIME=0
# messages
SYNC_RUN_TITLE="labsync"
SYNC_RUN_MESSAGE="Stiamo aggiornando il software nella cartella /opt; ci sarà un po&apos; di attività sul disco.\
\r\rAlcuni dei programmi lì presenti \(comprese le macchine virtuali\) potrebbero non funzionare correttamente fino al termine degli aggiornamenti.\
\r\rIl termine della procedura di aggiornamento ti verrà notificato, ti preghiamo di attendere qualche minuto.\
\r\r<b>Se hai atteso più di mezz&apos;ora, non hai ricevuto la notifica e il software che vuoi usare continua a non funzionare, contatta i tecnici.</b>"
SYNC_END_TITLE='labsync'
SYNC_END_MESSAGE="Il software nella cartella /opt è aggiornato."
# folder for temporary files
TMP=/var/run/labsync-notify
# task names (just for the lock file name)
SYNC_TASK="sync"
END_TASK="end"
xuser=""
xuid=""
createTmpDir() {
if [[ ! -e ${TMP} ]]; then
mkdir -p ${TMP}
fi
}
getUserVars() {
if [[ -e /tmp/.X11-unix ]]; then
# detect the name of the display in use
display=":$(ls /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)"
# detect the user using such display
#xuser=$(who | grep $display | awk '{print $1}')
#xuser=$(who | grep ":0" | awk -F: '{ print $1 }')
xuser=$(w -hs | grep " ${display} " | cut -f1 -d ' ')
if [[ "${xuser}" != "" ]]; then
# detect the id of the user
xuid=$(id -u ${xuser})
fi
fi
}
isSyncActive() {
if [[ -e /opt/ver ]]; then
echo "0"
else
echo "1"
fi
}
sendNotification() {
title=$1
message=$2
icon=$3
category=$4
urgency=$5
task=$6
if [[ "$xuser" != "" ]]; then
if [[ ! -e ${TMP}/labsync-notify-${xuid}-${task} ]]; then
touch ${TMP}/labsync-notify-${xuid}-${task}
su - ${xuser} -c "bash -c \"DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${xuid}/bus notify-send \
-t ${NOTIFY_TIME} -u ${urgency} -i ${icon} -c ${category} '${title}' '${message}'\""
fi
fi
}
cleanTmpFiles() {
rm ${TMP}/labsync-notify-*-${SYNC_TASK} > /dev/null 2>&1
rm ${TMP}/labsync-notify-*-${END_TASK} > /dev/null 2>&1
}
################################################################################
# create the folder for temporary files
createTmpDir
# cycle until labsync task is running
while [[ $(isSyncActive) == "1" ]]; do
getUserVars
sendNotification "${SYNC_RUN_TITLE}" "${SYNC_RUN_MESSAGE}" dialog-warning transfer critical ${SYNC_TASK}
sleep ${CYCLE_PERIOD}
done
# check if a user is still logged in
getUserVars
# show completion message only if user was notified
if [[ -e ${TMP}/labsync-notify-${xuid}-${SYNC_TASK} ]]; then
sendNotification "${SYNC_END_TITLE}" "${SYNC_END_MESSAGE}" info transfer.complete normal ${END_TASK}
fi
cleanTmpFiles
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment