PK [1EӘ0606SharedBrowser.pyUX 6A6Aimport os import sys import logging import sys import getopt from AccessGrid import Platform from wxPython.wx import * from wxPython.mozilla import * from AccessGrid.SharedAppClient import SharedAppClient from AccessGrid.Platform.Config import UserConfig from AccessGrid.ClientProfile import ClientProfile from AccessGrid import icons from AccessGrid.Toolkit import WXGUIApplication class UpdateTimer(wxTimer): def __init__(self, cb): wxTimer.__init__(self) self.cb = cb def Notify(self): self.cb() class WebBrowser(wxPanel): """ WebBrowser is a basic ie-based web browser class """ def __init__(self, parent, id, log, frame = None): wxPanel.__init__(self, parent, id) self.log = log self.current = None self.populate() self.navigation_callbacks = [] self.frame = frame if frame is not None: self.title_base = frame.GetTitle() self.just_received_navigate = 0 # The document url currently being loaded. self.docLoading = "" # Pages whose completion we need to ignore. This is because # the events don't tell us which events are for the main page. self.ignoreComplete = [] self.timer = UpdateTimer(self.Change) def add_navigation_callback(self, listener): self.log.debug("add_navigation_callback") self.navigation_callbacks.append(listener) def remove_navigation_callback(self, listener): self.log.debug("remove_navigation_callback") self.navigation_callbacks.remove(listener) def add_button(self, name, func, sizer): b = wxButton(self, -1, name) EVT_BUTTON(self, b.GetId(), func) sizer.Add(b, 0, wxEXPAND) return b def populate(self): sizer = wxBoxSizer(wxVERTICAL) # # Create the button bar # bsizer = wxBoxSizer(wxHORIZONTAL) self.back_button = self.add_button("Back", self.OnBack, bsizer) self.forward_button = self.add_button("Forward", self.OnForward, bsizer) if sys.platform == Platform.WIN: self.home_button = self.add_button("Home", self.OnHome, bsizer) self.stop_button = self.add_button("Stop", self.OnStop, bsizer) self.refresh_button = self.add_button("Refresh", self.OnRefresh, bsizer) t = wxStaticText(self, -1, "Location: ") bsizer.Add(t, 0, wxEXPAND) self.location = wxComboBox(self, wxNewId(), "", style=wxCB_DROPDOWN|wxPROCESS_ENTER) EVT_COMBOBOX(self, self.location.GetId(), self.OnLocationSelect) EVT_KEY_UP(self.location, self.OnLocationKey) bsizer.Add(self.location, 1, wxEXPAND) sizer.Add(bsizer, 0, wxEXPAND) # # Now we can set up the browser widget # self.ie = wxMozillaBrowser(self, -1, style = wxNO_FULL_REPAINT_ON_RESIZE) sizer.Add(self.ie, 1, wxEXPAND) # Hook up the event handlers for the Mozilla window # EVT_MOZILLA_BEFORE_LOAD(self, -1, self.OnBeforeLoad) EVT_MOZILLA_URL_CHANGED(self, -1, self.UpdateURL) EVT_MOZILLA_LOAD_COMPLETE(self, -1, self.OnLoadComplete) EVT_MOZILLA_STATUS_CHANGED(self, -1, self.UpdateStatus) EVT_MOZILLA_STATE_CHANGED(self, -1, self.UpdateState) self.SetSizer(sizer) self.SetAutoLayout(1) self.Layout() # Mozilla event handler # def OnBeforeLoad(self, event): # self.log.debug("OnBeforeLoad just_received_navigate=" + self.just_received_navigate) # if not self.just_received_navigate: # # Go to a new url and also send it to the other Shared # # Browser clients. The Send is done in IBrowsedCallback. # url = event.GetURL() # message = "Before load "+url # self.log.debug("Before load "+url) # self.just_received_navigate = 1 # self.docLoading = url # # Mozilla event handler def UpdateURL(self, event): url = event.GetNewURL() self.log.debug("UpdateURL url=" + url) if self.current != url: map(lambda a: a(url), self.navigation_callbacks) self.location.SetValue(url) self.back_button.Enable(event.CanGoBack()) self.forward_button.Enable(event.CanGoForward()) # Mozilla callback def OnLoadComplete(self, event): self.log.debug("OnLoadComplete: " + self.ie.GetURL()) self.current = self.ie.GetURL() if self.location.FindString(self.current) == wxNOT_FOUND: self.location.Append(self.current) if self.frame: self.frame.SetStatusText("") if self.ie.GetURL() == "about:blank" and self.docLoading != "about:blank": # This case happens at startup. self.log.debug("Ignoring DocComplete for first about:blank") else: # Finished loading, allow user to click links again now. # Needed since there is not enough information in the # events to tell if they refer to a popup (and other sub- # pages) or a user clicking on a url. self.log.debug("Finished loading.") self.just_received_navigate = 0 self.location.SetValue(self.current) if self.frame: self.frame.SetTitle(self.title_base + ' -- ' + self.ie.GetTitle()) def UpdateStatus(self, event): if self.frame: self.frame.SetStatusText(event.GetStatusText()) def UpdateState(self, event): if self.frame: if (event.GetState() & wxMOZILLA_STATE_START) or (event.GetState() & wxMOZILLA_STATE_TRANSFERRING): self.frame.SetStatusText("Loading " + event.GetURL() + "...") elif event.GetState() & wxMOZILLA_STATE_NEGOTIATING: self.frame.SetStatusText("Contacting server...") elif event.GetState() & wxMOZILLA_STATE_REDIRECTING: self.frame.SetStatusText("Redirecting from " + event.GetURL()) def OnBack(self, event): self.ie.GoBack() def OnForward(self, event): self.ie.GoForward() def OnStop(self, event): self.ie.Stop() def OnRefresh(self, event): self.ie.Reload() def ChangeURL(self, url): self.timer.Stop if url != self.current: self.ie.LoadURL(url) def OnLocationSelect(self, event): url = self.location.GetStringSelection() self.log.debug("OnLocationSelect url=" + url) self.ChangeURL(url) def OnLocationKey(self, event): if event.KeyCode() == WXK_RETURN: url = self.location.GetValue() self.log.debug("OnLocationKey url=" + url) self.ChangeURL(url) else: event.Skip() def Change(self): self.ChangeURL(self.rtarget) def RChangeURL(self, url): self.log.debug("RChangeURL url=" + url) self.rtarget = url # XXX this dies if we call LoadURL from the AG callback #self.ie.LoadURL(url) # XXX get a local timer to do the work for us self.timer.Start(1, wxTIMER_ONE_SHOT) class SharedBrowser( wxApp ): """ SharedBrowser combines a SharedApplication and a WebBrowser to provide shared web browsing to venue users """ def OnInit(self): return 1 def OnExit(self): ''' Shut down shared browser. ''' self.sharedAppClient.Shutdown() os._exit(1) def __init__( self, appUrl, name): ''' Creates the shared application client, used for application service interaction, and opens a web browser for UI display. ''' wxApp.__init__(self, False) # Create shared application client self.sharedAppClient = SharedAppClient(name) self.log = self.sharedAppClient.InitLogging() # Get client profile try: clientProfileFile = os.path.join(UserConfig.instance().GetConfigDir(), "profile") clientProfile = ClientProfile(clientProfileFile) except: self.log.info("SharedAppClient.Connect: Could not load client profile, set clientProfile = None") clientProfile = None # Join the application session. self.sharedAppClient.Join(appUrl, clientProfile) # Register browse event callback self.sharedAppClient.RegisterEventCallback("browse", self.BrowseCallback) # Create Browser Window self.frame = wxFrame(None, -1, "Browser") if sys.platform != Platform.WIN: self.frame.CreateStatusBar() self.browser = WebBrowser(self.frame, -1, self.log, self.frame) # Add callback for local browsing self.browser.add_navigation_callback(self.LocalChange) # Browse to the current url, if exists currentUrl = self.sharedAppClient.GetData("url") if len(currentUrl) > 0: self.browser.RChangeURL(currentUrl) try: self.sharedAppClient.SetParticipantStatus(currentUrl) except: self.log.exception("SharedBrowser:__init__: Failed to set participant status") self.frame.SetIcon(icons.getAGIconIcon()) self.frame.Show(1) self.SetTopWindow(self.frame) def LocalChange(self,data): ''' Callback invoked when local browse events occur. ''' # Send out the event, including our public ID in the message. publicId = self.sharedAppClient.GetPublicId() self.sharedAppClient.SendEvent("browse", ( publicId, data ) ) # Store the URL in the application service in the venue self.sharedAppClient.SetData("url", data) def BrowseCallback(self, event): """ Callback invoked when incoming browse events arrive. Events can include this component's browse events, so these need to be filtered out. """ # Determine if the sender of the event is this component or not. (senderId, url) = event.data self.log.debug("BrowseCallback url=" + url) if senderId == self.sharedAppClient.GetPublicId(): self.log.debug("BrowseCallback Ignoring url from myself ") else: self.log.debug("BrowseCallback loading") self.browser.RChangeURL(url) #self.browser.navigate(url) #self.browser.location.SetValue(url) #self.browser.location.Append(url) #self.browser.ie.LoadURL(url) try: self.sharedAppClient.SetParticipantStatus(url) except: self.log.exception("SharedBrowser:__init__: Failed to set participant status") class ArgumentManager: def __init__(self): self.arguments = {} self.arguments['applicationUrl'] = None self.arguments['debug'] = 0 def GetArguments(self): return self.arguments def Usage(self): """ How to use the program. """ print "%s:" % sys.argv[0] print " -a|--applicationURL : " print " -d|--debug : " print " -h|--help : " def ProcessArgs(self): """ Handle any arguments we're interested in. """ try: opts, args = getopt.getopt(sys.argv[1:], "a:d:h", ["applicationURL=", "debug", "help"]) except getopt.GetoptError: self.Usage() sys.exit(2) for o, a in opts: if o in ("-a", "--applicationURL"): self.arguments["applicationUrl"] = a elif o in ("-d", "--debug"): self.arguments["debug"] = 1 elif o in ("-h", "--help"): self.Usage() sys.exit(0) if __name__ == "__main__": app = WXGUIApplication() name = "SharedBrowser" # Parse command line options am = ArgumentManager() am.ProcessArgs() aDict = am.GetArguments() appUrl = aDict['applicationUrl'] debugMode = aDict['debug'] init_args = [] if "--debug" in sys.argv or "-d" in sys.argv: init_args.append("--debug") app.Initialize(name, args=init_args) if not appUrl: am.Usage() else: wxInitAllImageHandlers() sb = SharedBrowser( appUrl, name) sb.MainLoop() # # Stress test. Start a client and send events. # #import threading #import time #browsers = [] #threadList = [] #urls = ["www.oea.se","www.aftonbladet.se", "www.passagen.se"] #def StartBrowser(): # sb = SharedBrowser(appUrl, debugMode, logging) # browsers.append(sb) # sb.MainLoop() #def SendEvents(sharedAppClient): # time.sleep(3) # while 1: # for url in urls: # publicId = sharedAppClient.GetPublicId() # sharedAppClient.SendEvent("browse", (publicId, url)) # sharedAppClient.SetParticipantStatus(url) # # Store the URL in the application service in the venue # sharedAppClient.SetData("url", url) # time.sleep(0.07) #s = SharedAppClient("SharedAppClientTest") #s.InitLogging() #clientProfileFile = os.path.join(GetUserConfigDir(), "profile") #clientProfile = ClientProfile(clientProfileFile) #s.Join(appUrl, clientProfile) #thread = threading.Thread(target = SendEvents, args = [s]) #thread.start() #StartBrowser() PK 71]οSharedBrowser.appUX 7A#A[application] name = Shared Browser mimetype = application/x-ag-shared-browser extension = sharedbrowser files = SharedBrowser.py [commands] Open = %(python)s SharedBrowser.py -a %(appUrl)s PK [1EӘ0606 @SharedBrowser.pyUX6A6APK 71]ο @n6SharedBrowser.appUX7A#APKl7