-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui-client.py
executable file
·200 lines (147 loc) · 5.31 KB
/
gui-client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
sys.dont_write_bytecode = True
import pkgutil
import os
import threading
import time
from misc import logger
from misc import plugin
from misc import communicator
from config import CFG
from config import CONST
from config import METHOD
if os.geteuid() == 0:
print "Do not run as a root!"
quit(1)
if len( os.getenv( 'DISPLAY', '' ) ) == 0:
print "No DISPLAY set! Trying default DISPLAY = ':0.0'"
os.putenv( 'DISPLAY', ':0.0' )
import gtk
gtk.gdk.threads_init()
class GUI(plugin.Plugin):
"""
gtk - gui menu
"""
def init(self):
"""
init - reset items
"""
self.items = {}
def run(self):
"""
create gtk systry menu and ask for items
"""
self.askForItems()
try:
self.systray = gtk.StatusIcon()
self.systray.set_from_stock(gtk.STOCK_ABOUT)
self.systray.connect('popup-menu', self.onRightClick)
gtk.main()
except Exception as err:
logger.logDebug("error: %s", err )
def onRightClick(self, icon, eventbutton, eventtime):
"""
show popup menu on click
"""
self.showPopupMenu(eventbutton, eventtime)
def onAskForItems(self, widget):
"""
on ask for items click - call ask for items
"""
self.items = {}
self.askForItems()
def showPopupMenu(self, eventbutton, eventtime):
"""
create popup menu
"""
if not self.items:
self.askForItems()
reqreload = gtk.MenuItem(u'\u21BB', False)
reqreload.show()
reqreload.connect('activate', self.onAskForItems)
about = gtk.MenuItem(u'\u00A9', False)
about.show()
about.connect('activate', self.showAbout)
#quit = gtk.MenuItem("Quit", False)
#quit.show()
#quit.connect('activate', gtk.main_quit)
menu = gtk.Menu()
for plugin_name in self.items:
menu = self.createMenu(self.items[plugin_name], plugin_name, menu)
menu.append(reqreload)
menu.append(about)
#menu.append(quit)
menu.popup(None, None, gtk.status_icon_position_menu, eventbutton, eventtime, self.systray)
def showAbout(self, widget):
"""
show about dialog
"""
about = gtk.AboutDialog()
about.set_destroy_with_parent(True)
about.set_icon_name ("ikona")
about.set_name(CONST.APP_NAME)
about.set_version(CONST.VERSION)
about.set_copyright(CONST.COPYRIGHT)
about.set_comments((CONST.DESCRIPTION))
about.set_authors(CONST.AUTHORS)
about.run()
about.destroy()
def createMenu(self, items, cmdpath, menu = None):
"""
create submenu with items
"""
if not menu:
menu = gtk.Menu()
addlater = []
for item in items.keys():
later = False
for mi in menu:
#if not isinstance(items[item], dict):
# continue
if mi.get_label() == item:
addlater.append(item)
later = True
submi=mi.get_submenu()
self.createMenu(items[item], cmdpath + CONST.DELIMITER + item, submi)
break
if later:
continue
menuitem = gtk.MenuItem(item, False)
menuitem.show()
if isinstance(items[item], dict):
menuitem.set_submenu(self.createMenu(items[item], cmdpath + CONST.DELIMITER + item ))
else:
menuitem.connect('activate', self.menuHandler, cmdpath + CONST.DELIMITER + item)
menu.append(menuitem)
return menu
def menuHandler(self, widget, data = None):
"""
send command on item click
"""
target = data.split(CONST.DELIMITER)[0]
cmdstring = CONST.DELIMITER.join(data.split(CONST.DELIMITER)[1:])
logger.logDebug("Sending cmd for %s: '%s'" % (target, cmdstring) )
self.sendCommands([cmdstring], target)
def receiveData(self, data_dict, addr=None):
"""
data recieved, check if items available
"""
if "result" in data_dict.keys():
if data_dict["result"]["type"] == METHOD.ITEMS:
target = data_dict["result"]["plugin"]
if not target in self.items:
self.items[target] = {}
self.items[target].update(data_dict["result"]["items"])
if __name__ == '__main__':
clientsocket = communicator.SocketClient(CFG.SOCKET_UNIX_FILE, communicator.SOCKET_TCP)
clientsocket.keepAlive(True)
clientsocket.useReplying(False)
clientsocket.initClient()
clientsocket.startReceiving()
gui = GUI(CONST.APP_SHORT_NAME)
gui.setSender(clientsocket.send)
clientsocket.setDataHandler(gui.receiveData)
while True:
time.sleep(10)