📄 pop3proxy_tray.py
字号:
if GetTickCount() - dwStartTickCount > ssStatus[6]: break self.started = ssStatus[1] == SERVICE_RUNNING CloseServiceHandle(schService) self.started = True def StopService(self): schSCManager = OpenSCManager(None, None, SC_MANAGER_CONNECT) schService = OpenService(schSCManager, serviceName, SERVICE_STOP | SERVICE_QUERY_STATUS) # we assume IsServiceAvailable() was called before ssStatus = QueryServiceStatus(schService) if ssStatus[1] in stoppedStatus: self.started = False CloseServiceHandle(schService) return ControlService(schService, SERVICE_CONTROL_STOP) ssStatus = QueryServiceStatus(schService) dwStartTickCount = GetTickCount() dwOldCheckPoint = ssStatus[5] while ssStatus[1] == SERVICE_STOP_PENDING: dwWaitTime = ssStatus[6] / 10; if dwWaitTime < 1000: dwWaitTime = 1000 elif dwWaitTime > 10000: dwWaitTime = 10000 Sleep(dwWaitTime); ssStatus = QueryServiceStatus(schService) if ssStatus[5] > dwOldCheckPoint: dwStartTickCount = GetTickCount() dwOldCheckPoint = ssStatus[5] else: if GetTickCount() - dwStartTickCount > ssStatus[6]: break CloseServiceHandle(schService) self.started = False def OnDestroy(self, hwnd, msg, wparam, lparam): nid = (self.hwnd, 0) Shell_NotifyIcon(NIM_DELETE, nid) PostQuitMessage(0) def OnTaskbarNotify(self, hwnd, msg, wparam, lparam): if lparam==win32con.WM_MOUSEMOVE: if self.tip != self.BuildToolTip(): self.UpdateIcon() else: self.CheckCurrentState() if lparam==win32con.WM_LBUTTONUP: # We ignore left clicks pass elif lparam==win32con.WM_LBUTTONDBLCLK: # Default behaviour is to open up the web interface # XXX This should be set as the default (which then means bold # XXX text) through the win32 calls, but win32all doesn't # XXX include SetDefault(), which it needs to... self.OpenReview() elif lparam==win32con.WM_RBUTTONUP: # check our state before creating the menu, so it reflects the # true "running state", not just what we thought it was last. self.CheckCurrentState() menu = CreatePopupMenu() ids = self.control_functions.keys() ids.sort() for id in ids: (wording, function) = self.control_functions[id] if function: AppendMenu( menu, win32con.MF_STRING, id, wording) else: AppendMenu( menu, win32con.MF_SEPARATOR, id, wording) pos = GetCursorPos() SetForegroundWindow(self.hwnd) # Set the default menu item ("Review messages", currently). # Make sure that the item here matches the behaviour in # DBCLICK above! # This is only available in recent versions of win32all, # so for those that don't have it, they just get a dull # menu. try: SetMenuDefaultItem(menu, 2, 1) except NameError: pass TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None) PostMessage(self.hwnd, win32con.WM_NULL, 0, 0) return 1 def OnCommand(self, hwnd, msg, wparam, lparam): id = LOWORD(wparam) try: unused, function = self.control_functions[id] except KeyError: print "Unknown command -", id return function() def OnTaskbarRestart(self, hwnd, msg, wparam, lparam): # Called as the taskbar is created (either as Windows starts, or # as Windows recovers from a crashed explorer.exe) self._AddTaskbarIcon() def OnExit(self): if self.started and not self.use_service: try: sb_server.stop() except: print "Error stopping proxy at shutdown" traceback.print_exc() print "Shutting down anyway..." self.started = False DestroyWindow(self.hwnd) PostQuitMessage(0) def _ProxyThread(self): self.started = True try: sb_server.start() finally: self.started = False self.have_prepared_state = False def StartProxyThread(self): thread.start_new_thread(self._ProxyThread, ()) def Start(self): self.CheckCurrentState() if self.started: print "Ignoring start request - server already running" return if self.use_service: if verbose: print "Doing 'Start' via service" if self.GetServiceStatus() in stoppedStatus: self.StartService() else: print "Service was already running - ignoring!" else: # Running it internally. if verbose: print "Doing 'Start' internally" if not self.have_prepared_state: try: sb_server.prepare() self.have_prepared_state = True except sb_server.AlreadyRunningException: msg = "The proxy is already running on this " \ "machine - please\r\n stop the existing " \ "proxy, and try again." self.ShowMessage(msg) return self.StartProxyThread() self.started = True self.UpdateUIState() def Stop(self): self.CheckCurrentState() if not self.started: print "Ignoring stop request - server doesn't appear to be running" return try: use_service = self.use_service if use_service: # XXX - watch out - if service status is "stopping", trying # to start is likely to fail until it actually gets to # "stopped" if verbose: print "Doing 'Stop' via service" if self.GetServiceStatus() not in stoppedStatus: self.StopService() else: print "Service was already stopped - weird - falling " \ "back to a socket based quit" use_service = False if not use_service: if verbose: print "Stopping local server" sb_server.stop() except: print "There was an error stopping the server" traceback.print_exc() # but either way, assume it stopped for the sake of our UI self.started = False self.UpdateUIState() def CheckCurrentState(self): self.started = IsServerRunningAnywhere() self.UpdateUIState() def UpdateUIState(self): if self.started != self.last_started_state: self.UpdateIcon() if self.started: self.control_functions[START_STOP_ID] = ("Stop SpamBayes", self.Stop) else: self.control_functions[START_STOP_ID] = ("Start SpamBayes", self.Start) self.last_started_state = self.started def OpenInterface(self): if self.started: webbrowser.open_new("http://localhost:%d/" % \ (options["html_ui", "port"],)) else: self.ShowMessage("SpamBayes is not running.") def OpenConfig(self): if self.started: webbrowser.open_new("http://localhost:%d/config" % \ (options["html_ui", "port"],)) else: self.ShowMessage("SpamBayes is not running.") def OpenReview(self): if self.started: webbrowser.open_new("http://localhost:%d/review" % \ (options["html_ui", "port"],)) else: self.ShowMessage("SpamBayes is not running.") def GetHelp(self): # We don't need to be running for this. self.ShowHTML("troubleshooting.html") def ShowHTML(self, url): """Displays the main SpamBayes documentation in your Web browser""" # Stolen from Outlook's Manager.py import sys, os, urllib if urllib.splittype(url)[0] is None: # just a file spec if hasattr(sys, "frozen"): # New binary is in ../docs/sb_server relative to executable. fname = os.path.join(os.path.dirname(sys.argv[0]), "..", "docs", "sb_server", url) if not os.path.isfile(fname): # Still support same directory as to the executable. fname = os.path.join(os.path.dirname(sys.argv[0]), url) else: # ../windows/docs dir fname = os.path.join(os.path.dirname(__file__), "docs", url) fname = os.path.abspath(fname) if not os.path.isfile(fname): self.ShowMessage("Can't find "+url) return url = fname # else assume it is valid! SetWaitCursor(1) os.startfile(url) SetWaitCursor(0) def CheckVersion(self): # Stolen, with few modifications, from addin.py from spambayes.Version import get_current_version, get_version, \ get_download_page, fetch_latest_dict app_name = "POP3 Proxy" app_display_name = "SpamBayes POP3 Proxy" ver_current = get_current_version() cur_ver_string = ver_current.get_long_version(app_display_name) try: SetWaitCursor(1) latest = fetch_latest_dict() SetWaitCursor(0) ver_latest = get_version(app_name, version_dict=latest) latest_ver_string = ver_latest.get_long_version(app_display_name) except: self.ShowMessage("Error checking the latest version") traceback.print_exc() return ver_message = "Current version is %s.\r\n" \ "Latest version is %s.\r\n\r\n" % \ (cur_ver_string, latest_ver_string) if ver_latest == ver_current: ver_message += "Your are running the latest downloadable version." elif ver_current > ver_latest: ver_message += "Your current version is newer than the latest " \ "downloadable version." else: ver_message += "There is a newer version available. You may " \ "download the updated version from:\r\n" url = get_download_page(app_name, version_dict=latest) ver_message += url self.ShowMessage(ver_message) # It would be nice to offer to open up the url if there is a # newer version, but we don't do that yet.## os.startfile(url) def ShowMessage(self, msg): MessageBox(self.hwnd, msg, "SpamBayes", win32con.MB_OK)def main(): w = MainWindow() PumpMessages()if __name__=='__main__': main()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -