1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | |
---|
4 | # PyKotIcon - Client side helper for PyKota |
---|
5 | # |
---|
6 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
7 | # This program is free software; you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation; either version 2 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # This program is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with this program; if not, write to the Free Software |
---|
19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | # |
---|
21 | # $Id$ |
---|
22 | # |
---|
23 | # |
---|
24 | |
---|
25 | import sys |
---|
26 | import os |
---|
27 | import urllib |
---|
28 | import urllib2 |
---|
29 | import locale |
---|
30 | import gettext |
---|
31 | import socket |
---|
32 | import threading |
---|
33 | import xmlrpclib |
---|
34 | import SimpleXMLRPCServer |
---|
35 | |
---|
36 | import time |
---|
37 | |
---|
38 | if sys.platform == "win32" : |
---|
39 | isWindows = 1 |
---|
40 | try : |
---|
41 | import win32api |
---|
42 | except ImportError : |
---|
43 | raise ImportError, "Mark Hammond's Win32 Extensions are missing. Please install them." |
---|
44 | else : |
---|
45 | iconsdir = os.path.split(sys.argv[0])[0] |
---|
46 | else : |
---|
47 | isWindows = 0 |
---|
48 | iconsdir = "/usr/share/pykoticon" # TODO : change this |
---|
49 | import pwd |
---|
50 | |
---|
51 | try : |
---|
52 | import wxPython.wx |
---|
53 | import wx |
---|
54 | hasWxPython = 1 |
---|
55 | except ImportError : |
---|
56 | hasWxPython = 0 |
---|
57 | raise ImportError, "wxPython is missing. Please install it." |
---|
58 | |
---|
59 | def getCurrentUserName() : |
---|
60 | """Retrieves the current user's name.""" |
---|
61 | if isWindows : |
---|
62 | return win32api.GetUserName() |
---|
63 | else : |
---|
64 | try : |
---|
65 | return pwd.getpwuid(os.geteuid())[0] |
---|
66 | except : |
---|
67 | return "** Unknown **" |
---|
68 | |
---|
69 | class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer) : |
---|
70 | """My own server class.""" |
---|
71 | allow_reuse_address = True |
---|
72 | def __init__(self, frame, printserver, localport, debug=False) : |
---|
73 | SimpleXMLRPCServer.SimpleXMLRPCServer.__init__(self, ('0.0.0.0', localport)) |
---|
74 | self.printServer = printserver |
---|
75 | self.frame = frame |
---|
76 | self.debug = debug |
---|
77 | loop = threading.Thread(target=self.mainloop) |
---|
78 | loop.start() |
---|
79 | |
---|
80 | def logDebug(self, message) : |
---|
81 | """Logs a debug message if debug mode is active.""" |
---|
82 | if self.debug : |
---|
83 | sys.stderr.write("%s\n" % message) |
---|
84 | |
---|
85 | def export_quitApplication(self) : |
---|
86 | """Makes the application quit.""" |
---|
87 | self.logDebug("Remote host asked to close the application.") |
---|
88 | self.frame.quitEvent.set() |
---|
89 | wx.CallAfter(self.frame.OnClose, None) |
---|
90 | return True |
---|
91 | |
---|
92 | def export_showDialog(self, message, yesno) : |
---|
93 | """Opens a notification or confirmation dialog.""" |
---|
94 | wx.CallAfter(self.frame.showDialog, message, yesno) |
---|
95 | # ugly, isn't it ? |
---|
96 | while self.frame.dialogAnswer is None : |
---|
97 | time.sleep(0.1) |
---|
98 | retcode = self.frame.dialogAnswer |
---|
99 | self.frame.dialogAnswer = None # prepare for next call, just in case |
---|
100 | return retcode |
---|
101 | |
---|
102 | def export_nop(self) : |
---|
103 | """Does nothing, but allows a clean shutdown from the frame itself.""" |
---|
104 | self.logDebug("No operation !") |
---|
105 | return True |
---|
106 | |
---|
107 | def verify_request(self, request, client_address) : |
---|
108 | """Ensures that requests which don't come from the print server are rejected.""" |
---|
109 | (client, port) = client_address |
---|
110 | if socket.gethostbyname(self.printServer) == client : |
---|
111 | self.logDebug("%s accepted." % client) |
---|
112 | return True |
---|
113 | else : |
---|
114 | # Unauthorized access ! |
---|
115 | self.logDebug("%s rejected." % client) |
---|
116 | return False |
---|
117 | |
---|
118 | def _dispatch(self, method, params) : |
---|
119 | """Ensure that only export_* methods are available.""" |
---|
120 | return getattr(self, "export_%s" % method)(*params) |
---|
121 | |
---|
122 | def mainloop(self) : |
---|
123 | """XML-RPC Server's main loop.""" |
---|
124 | self.register_function(self.export_showDialog) |
---|
125 | self.register_function(self.export_quitApplication) |
---|
126 | self.register_function(self.export_nop) |
---|
127 | while not self.frame.quitEvent.isSet() : |
---|
128 | self.handle_request() |
---|
129 | self.server_close() |
---|
130 | sys.exit(0) |
---|
131 | |
---|
132 | class PyKotIcon(wx.Frame): |
---|
133 | """Main class.""" |
---|
134 | def __init__(self, parent, id): |
---|
135 | self.dialogAnswer = None |
---|
136 | wx.Frame.__init__(self, parent, wx.ID_ANY, \ |
---|
137 | _("PyKota info for %s") % getCurrentUserName(), \ |
---|
138 | size = (-1, -1), \ |
---|
139 | style = wxPython.wx.wxDEFAULT_FRAME_STYLE \ |
---|
140 | | wxPython.wx.wxSIZE_AUTO_HEIGHT \ |
---|
141 | | wxPython.wx.wxSIZE_AUTO_WIDTH \ |
---|
142 | | wxPython.wx.wxNO_FULL_REPAINT_ON_RESIZE) |
---|
143 | # try : |
---|
144 | # self.tbicon = wxPython.wx.wxTaskBarIcon() |
---|
145 | # except AttributeError : |
---|
146 | # self.tbicon = None # No taskbar icon facility, old wxWidgets maybe |
---|
147 | # |
---|
148 | # self.greenicon = wxPython.wx.wxIcon(os.path.join(iconsdir, "pykoticon-green.ico"), \ |
---|
149 | # wxPython.wx.wxBITMAP_TYPE_ICO) |
---|
150 | # self.redicon = wxPython.wx.wxIcon(os.path.join(iconsdir, "pykoticon-red.ico"), \ |
---|
151 | # wxPython.wx.wxBITMAP_TYPE_ICO) |
---|
152 | # |
---|
153 | # self.SetIcon(self.greenicon) |
---|
154 | # if self.tbicon is not None : |
---|
155 | # self.tbicon.SetIcon(self.greenicon, "PyKotIcon") |
---|
156 | # wxPython.wx.EVT_TASKBAR_LEFT_DCLICK(self.tbicon, self.OnTaskBarActivate) |
---|
157 | # wxPython.wx.EVT_TASKBAR_RIGHT_UP(self.tbicon, self.OnTaskBarMenu) |
---|
158 | # |
---|
159 | # self.TBMENU_RESTORE = wx.NewId() |
---|
160 | # self.TBMENU_CLOSE = wx.NewId() |
---|
161 | # wxPython.wx.EVT_MENU(self.tbicon, self.TBMENU_RESTORE, \ |
---|
162 | # self.OnTaskBarActivate) |
---|
163 | # wxPython.wx.EVT_MENU(self.tbicon, self.TBMENU_CLOSE, \ |
---|
164 | # self.OnTaskBarClose) |
---|
165 | # self.menu = wxPython.wx.wxMenu() |
---|
166 | # self.menu.Append(self.TBMENU_RESTORE, _("Show Print Quota")) |
---|
167 | # self.menu.Append(self.TBMENU_CLOSE, _("Quit")) |
---|
168 | # |
---|
169 | wxPython.wx.EVT_ICONIZE(self, self.OnIconify) |
---|
170 | wxPython.wx.EVT_CLOSE(self, self.OnClose) |
---|
171 | self.Show(True) |
---|
172 | |
---|
173 | def closeServer(self) : |
---|
174 | """Tells the xml-rpc server to exit.""" |
---|
175 | if not self.quitEvent.isSet() : |
---|
176 | self.quitEvent.set() |
---|
177 | server = xmlrpclib.ServerProxy("http://localhost:%s" % self.port) |
---|
178 | try : |
---|
179 | # wake the server with an empty request |
---|
180 | # for it to see the event object |
---|
181 | # which has just been set |
---|
182 | server.nop() |
---|
183 | except : |
---|
184 | # Probably already stopped |
---|
185 | pass |
---|
186 | |
---|
187 | def postInit(self, printserver, localport) : |
---|
188 | """Starts the XML-RPC server.""" |
---|
189 | self.quitEvent = threading.Event() |
---|
190 | self.port = localport |
---|
191 | self.server = MyXMLRPCServer(self, printserver, localport, debug=True) |
---|
192 | |
---|
193 | def OnIconify(self, event) : |
---|
194 | self.Hide() |
---|
195 | |
---|
196 | def OnTaskBarActivate(self, event) : |
---|
197 | #if self.IsIconized() : |
---|
198 | # self.Iconize(False) |
---|
199 | if not self.IsShown() : |
---|
200 | self.Show(True) |
---|
201 | self.Raise() |
---|
202 | |
---|
203 | def OnClose(self, event) : |
---|
204 | sys.stderr.write("Close event !\n") |
---|
205 | self.closeServer() |
---|
206 | if hasattr(self, "menu") : |
---|
207 | self.menu.Destroy() |
---|
208 | del self.menu |
---|
209 | if hasattr(self, "tbicon") and self.tbicon : |
---|
210 | self.tbicon.Destroy() |
---|
211 | del self.tbicon |
---|
212 | self.Destroy() |
---|
213 | |
---|
214 | def OnTaskBarMenu(self, event) : |
---|
215 | #if self.tbicon : |
---|
216 | # self.tbicon.PopupMenu(self.menu) |
---|
217 | pass |
---|
218 | |
---|
219 | def OnTaskBarClose(self, event) : |
---|
220 | self.Close() |
---|
221 | |
---|
222 | def showDialog(self, message, yesno) : |
---|
223 | """Opens a notification dialog.""" |
---|
224 | self.dialogAnswer = None |
---|
225 | if yesno : |
---|
226 | caption = _("Confirmation") |
---|
227 | style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION |
---|
228 | else : |
---|
229 | caption = _("Information") |
---|
230 | style = wx.OK | wx.ICON_INFORMATION |
---|
231 | style |= wx.STAY_ON_TOP |
---|
232 | dialog = wx.MessageDialog(self, message, caption, style) |
---|
233 | self.dialogAnswer = ((dialog.ShowModal() == wx.ID_NO) and "CANCEL") or "OK" |
---|
234 | dialog.Destroy() |
---|
235 | |
---|
236 | |
---|
237 | class PyKotIconApp(wx.PySimpleApp): |
---|
238 | def OnInit(self) : |
---|
239 | self.frame = PyKotIcon(None, -1) |
---|
240 | return True |
---|
241 | |
---|
242 | def postInit(self, printserver, localport) : |
---|
243 | """Continues processing.""" |
---|
244 | self.frame.postInit(printserver, localport) |
---|
245 | self.frame.Show(True) |
---|
246 | |
---|
247 | def main(printserver, localport): |
---|
248 | """Program's entry point.""" |
---|
249 | try : |
---|
250 | locale.setlocale(locale.LC_ALL, "") |
---|
251 | except (locale.Error, IOError) : |
---|
252 | sys.stderr.write("Problem while setting locale.\n") |
---|
253 | try : |
---|
254 | gettext.install("pykoticon") |
---|
255 | except : |
---|
256 | gettext.NullTranslations().install() |
---|
257 | app = PyKotIconApp() |
---|
258 | try : |
---|
259 | localport = int(localport) |
---|
260 | except (TypeError, ValueError) : |
---|
261 | raise ValueError, "Invalid TCP port parameter %s\n" % localport |
---|
262 | app.postInit(printserver, localport) |
---|
263 | app.MainLoop() |
---|
264 | |
---|
265 | def crashed() : |
---|
266 | """Minimal crash method.""" |
---|
267 | import traceback |
---|
268 | lines = [] |
---|
269 | for line in traceback.format_exception(*sys.exc_info()) : |
---|
270 | lines.extend([l for l in line.split("\n") if l]) |
---|
271 | msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKotIcon"] + lines)]) |
---|
272 | sys.stderr.write(msg) |
---|
273 | sys.stderr.flush() |
---|
274 | |
---|
275 | if __name__ == '__main__': |
---|
276 | if len(sys.argv) >= 2 : |
---|
277 | arg = sys.argv[1] |
---|
278 | if arg in ("-v", "--version") : |
---|
279 | print "0.3" |
---|
280 | elif arg in ("-h", "--help") : |
---|
281 | sys.stderr.write("usage : pykoticon pykota_server_hostname_or_ip_address localTCPPort\n") |
---|
282 | else : |
---|
283 | main(*sys.argv[1:3]) |
---|
284 | else : |
---|
285 | sys.stderr.write("usage : pykoticon pykota_server_hostname_or_ip_address localTCPPort\n") |
---|