Ejemplo n.º 1
0
    def __init__(self, ID, params):
        """
        Initialize the main frame
        """
        utility.frame = self
        self.doneflag = Event()

        title = product_name + " " + version_short
        size, position = self._getWindowSettings()
        wx.Frame.__init__(self, None, ID, title, position, size, name = "MainFrame")
        SafeInvocation.__init__(self)

        #if wx.Platform == "__WXMSW__":
        #    if wx.GetApp().GetComCtl32Version() >= 600 and wx.DisplayDepth() >= 32:
        #        wx.SystemOptions.SetOptionInt("msw.remap", 2)
        #if wx.Platform == "__WXMAC__":
            #wx.SystemOptions.SetOptionInt("mac.listctrl.always_use_generic", 1)
            #self.SetExtraStyle(wx.FRAME_EX_METAL)

        # Don't update GUI as often when iconized
        self.GUIupdate = True

        # Set Application icon
        try:
            self.SetIcon(utility.icon)
        except:
            pass
        
        #######################################################
        # Main GUI stuff
        #######################################################

        # Scheduler
        ##############
        utility.queue  = Scheduler()
        
        # StatusBar
        ##############
        self.SetStatusBar(StatusBar(self))

        # MenuBar
        ##############
        self.SetMenuBar(MenuBar(self))

        # TaskBar icon
        ##############
        try:
            self.tbicon = TaskBarIcon(self)
        except:
            self.tbicon = None

        # Main Panel 
        ##############
        self.window = MainPanel(self)

        # Pref. Frame
        ##############
        self.preferences = None

        # ToolBar
        ##############
        self.tb = MainToolBar(self)
        self.SetToolBar(self.tb)
        
        # Command Scheduler
        ##############
        self.commandScheduler = CommandScheduler()

        # Win Settings  
        ##############
        self._loadWindowSettings()

        # Update Menu
        ##############
        self.updateMenuChecks()

        wx.LogMessage('GUI loaded')
        
        # Hot Key
        ############## 
        if wx.Platform == "__WXMSW__":
            self.openwin = []
            self.modaldlg = {}
            utility.hotkey = {"ID": wx.NewId(),
                              "modifiers": utility.config.Read('hotkeymod', "int"),
                              "keys": utility.config.Read('hotkeykeycode', "int"),
                              "wxkeycode": utility.config.Read('hotkeywxkeycode', "int"),
                              "current": {"modifiers": None,
                                          "keys": None,
                                          "wxkeycode": None},
                              "active": False,
                              "hiding": False}
            
            if utility.config.Read('hotkey', "boolean"):
                utility.hotkey["current"]["modifiers"] = utility.hotkey["modifiers"]
                utility.hotkey["current"]["keys"] = utility.hotkey["keys"]
                utility.hotkey["current"]["wxkeycode"] = utility.hotkey["wxkeycode"]     
                self.RegisterHotKey(utility.hotkey["ID"], utility.hotkey["modifiers"], utility.hotkey["keys"])
                utility.hotkey["active"] = True

        #######################################################
        # Events Table
        #######################################################
        self.Bind(wx.EVT_QUERY_END_SESSION, self.OnCloseWindow)
        self.Bind(wx.EVT_END_SESSION, self.OnCloseWindow)
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Bind(wx.EVT_ICONIZE, self.onIconify)
        self.Bind(wx.EVT_SIZE, self.onSize)
        self.Bind(wx.EVT_SHOW, self.onShow)
        if wx.Platform == "__WXMSW__":
            self.Bind(wx.EVT_HOTKEY, self.HotKeyDown, id = utility.hotkey["ID"])
            self.Bind(wx.EVT_SYS_COLOUR_CHANGED, self.OnSysColourChange)
        if wx.Platform == "__WXMAC__":
            self.Bind(wx.EVT_ACTIVATE, self.OnActivate)
        
        # Accelerator Table
        fsID = wx.NewId()
        self.Bind(wx.EVT_MENU, self.onFullScreen, id = fsID)
        accelTable = wx.AcceleratorTable([(wx.ACCEL_NORMAL, wx.WXK_F11, fsID)])
        self.SetAcceleratorTable(accelTable)

        #######################################################
        # Set Up Filter Manager
        #######################################################
        utility.ipfilter = FilterManager()
        if utility.ipfilter:
            wx.LogMessage('Filter Manager: %d IP ranges loaded' % len(utility.ipfilter))
        #######################################################
        # Start the controller
        #######################################################
        utility.controller = LaunchMany()
        utility.controller.start()
        utility.queue.postInitTasks()
        wx.LogMessage('Controller loaded and started')

        #######################################################
        # Start DHT
        #######################################################
        if utility.dht:
            utility.dht.start()
            wx.LogMessage('DHT loaded')
            
        #######################################################
        # Set Up Label Manager
        #######################################################
        utility.labelmanager = LabelManager()
        
        #######################################################
        # Web Service
        #######################################################
        if WEBSERVICE:
            WebService()
        else:
            wx.LogMessage(_("Web Service disabled: Please download wsgiref or update to Python 2.5")) 

        #######################################################
        # Startup params
        #######################################################
        for param in params:
            wx.GetApp().MacOpenFile(param)  # Yes, even if the platform isn't Mac

        #######################################################
        # Check For Update
        #######################################################
        if utility.config.Read('checkforupdates', "boolean"):
            utility.actions[ACTION_UPDATECHECK].action()
            
        self.Show(True)
        
        # Check to see if ABC is associated with torrents
        #######################################################
        if (sys.platform[:3] == 'win') and \
           utility.config.Read('associate', "boolean") and \
           not utility.regchecker.testRegistry():
            dialog = RegCheckDialog(self)
            dialog.ShowModal()
            dialog.Destroy()
Ejemplo n.º 2
0
class MainFrame(wx.Frame, SafeInvocation):
    """
    Main Frame class that contains MainPanel, menubar, statusbar, toolbar
    """
    def __init__(self, ID, params):
        """
        Initialize the main frame
        """
        utility.frame = self
        self.doneflag = Event()

        title = product_name + " " + version_short
        size, position = self._getWindowSettings()
        wx.Frame.__init__(self, None, ID, title, position, size, name = "MainFrame")
        SafeInvocation.__init__(self)

        #if wx.Platform == "__WXMSW__":
        #    if wx.GetApp().GetComCtl32Version() >= 600 and wx.DisplayDepth() >= 32:
        #        wx.SystemOptions.SetOptionInt("msw.remap", 2)
        #if wx.Platform == "__WXMAC__":
            #wx.SystemOptions.SetOptionInt("mac.listctrl.always_use_generic", 1)
            #self.SetExtraStyle(wx.FRAME_EX_METAL)

        # Don't update GUI as often when iconized
        self.GUIupdate = True

        # Set Application icon
        try:
            self.SetIcon(utility.icon)
        except:
            pass
        
        #######################################################
        # Main GUI stuff
        #######################################################

        # Scheduler
        ##############
        utility.queue  = Scheduler()
        
        # StatusBar
        ##############
        self.SetStatusBar(StatusBar(self))

        # MenuBar
        ##############
        self.SetMenuBar(MenuBar(self))

        # TaskBar icon
        ##############
        try:
            self.tbicon = TaskBarIcon(self)
        except:
            self.tbicon = None

        # Main Panel 
        ##############
        self.window = MainPanel(self)

        # Pref. Frame
        ##############
        self.preferences = None

        # ToolBar
        ##############
        self.tb = MainToolBar(self)
        self.SetToolBar(self.tb)
        
        # Command Scheduler
        ##############
        self.commandScheduler = CommandScheduler()

        # Win Settings  
        ##############
        self._loadWindowSettings()

        # Update Menu
        ##############
        self.updateMenuChecks()

        wx.LogMessage('GUI loaded')
        
        # Hot Key
        ############## 
        if wx.Platform == "__WXMSW__":
            self.openwin = []
            self.modaldlg = {}
            utility.hotkey = {"ID": wx.NewId(),
                              "modifiers": utility.config.Read('hotkeymod', "int"),
                              "keys": utility.config.Read('hotkeykeycode', "int"),
                              "wxkeycode": utility.config.Read('hotkeywxkeycode', "int"),
                              "current": {"modifiers": None,
                                          "keys": None,
                                          "wxkeycode": None},
                              "active": False,
                              "hiding": False}
            
            if utility.config.Read('hotkey', "boolean"):
                utility.hotkey["current"]["modifiers"] = utility.hotkey["modifiers"]
                utility.hotkey["current"]["keys"] = utility.hotkey["keys"]
                utility.hotkey["current"]["wxkeycode"] = utility.hotkey["wxkeycode"]     
                self.RegisterHotKey(utility.hotkey["ID"], utility.hotkey["modifiers"], utility.hotkey["keys"])
                utility.hotkey["active"] = True

        #######################################################
        # Events Table
        #######################################################
        self.Bind(wx.EVT_QUERY_END_SESSION, self.OnCloseWindow)
        self.Bind(wx.EVT_END_SESSION, self.OnCloseWindow)
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Bind(wx.EVT_ICONIZE, self.onIconify)
        self.Bind(wx.EVT_SIZE, self.onSize)
        self.Bind(wx.EVT_SHOW, self.onShow)
        if wx.Platform == "__WXMSW__":
            self.Bind(wx.EVT_HOTKEY, self.HotKeyDown, id = utility.hotkey["ID"])
            self.Bind(wx.EVT_SYS_COLOUR_CHANGED, self.OnSysColourChange)
        if wx.Platform == "__WXMAC__":
            self.Bind(wx.EVT_ACTIVATE, self.OnActivate)
        
        # Accelerator Table
        fsID = wx.NewId()
        self.Bind(wx.EVT_MENU, self.onFullScreen, id = fsID)
        accelTable = wx.AcceleratorTable([(wx.ACCEL_NORMAL, wx.WXK_F11, fsID)])
        self.SetAcceleratorTable(accelTable)

        #######################################################
        # Set Up Filter Manager
        #######################################################
        utility.ipfilter = FilterManager()
        if utility.ipfilter:
            wx.LogMessage('Filter Manager: %d IP ranges loaded' % len(utility.ipfilter))
        #######################################################
        # Start the controller
        #######################################################
        utility.controller = LaunchMany()
        utility.controller.start()
        utility.queue.postInitTasks()
        wx.LogMessage('Controller loaded and started')

        #######################################################
        # Start DHT
        #######################################################
        if utility.dht:
            utility.dht.start()
            wx.LogMessage('DHT loaded')
            
        #######################################################
        # Set Up Label Manager
        #######################################################
        utility.labelmanager = LabelManager()
        
        #######################################################
        # Web Service
        #######################################################
        if WEBSERVICE:
            WebService()
        else:
            wx.LogMessage(_("Web Service disabled: Please download wsgiref or update to Python 2.5")) 

        #######################################################
        # Startup params
        #######################################################
        for param in params:
            wx.GetApp().MacOpenFile(param)  # Yes, even if the platform isn't Mac

        #######################################################
        # Check For Update
        #######################################################
        if utility.config.Read('checkforupdates', "boolean"):
            utility.actions[ACTION_UPDATECHECK].action()
            
        self.Show(True)
        
        # Check to see if ABC is associated with torrents
        #######################################################
        if (sys.platform[:3] == 'win') and \
           utility.config.Read('associate', "boolean") and \
           not utility.regchecker.testRegistry():
            dialog = RegCheckDialog(self)
            dialog.ShowModal()
            dialog.Destroy()
        
    #######################################
    # minimize to tray bar control
    #######################################
    def OnActivate(self, event):
        if event.GetActive():
            self.StatusBar.clean()
            self.setGUIupdate(True)
        event.Skip()
        
    def taskbarCallback(self):
        self.invokeLater(self.onTaskBarActivate, [])

    def onTaskBarActivate(self, event = None, close = False):
        # In case frame is already shown, hide it.
        if self.IsShown() and event is not None:
            self.Iconize(True)
            return
        
        self.Iconize(False)
        self.Show(True)
        self.Raise()

        if close and self.IsMaximized():
            self.Iconize(False)
            
        if self.tbicon is not None:
            self.tbicon.updateIcon(close = close)

        # Resume updating GUI
        self.setGUIupdate(True)

    def onIconify(self, event = None):
        """
        This event handler is called both when being
        minimalized and when being restored.
        """
        if event is None:
            self.Iconize(True)
            if self.tbicon is not None:
                self.tbicon.updateIcon(True)
                self.Show(False)
            self.setGUIupdate(False)
        elif event.Iconized():
            if self.tbicon is not None \
               and 0 < utility.config.Read('mintray', "int") != 2:
                self.tbicon.updateIcon(True)
                self.Show(False)                    
            self.setGUIupdate(False)
        else:
            self.setGUIupdate(True)
        if event is not None:
            event.Skip()

    #######################################
    # Setting GUI Update
    #######################################
    def onSize(self, event = None):
        self.setGUIupdate(True)
        if event is not None:
            event.Skip()
            
    def onShow(self, event):
        self.setGUIupdate(event.GetShow())
        event.Skip()
        
    def setGUIupdate(self, update):
        if update == self.GUIupdate:
            return
        
        self.GUIupdate = update
        if self.GUIupdate:
            # Force an update of all torrents
            for torrent in utility.torrents["all"]:
                torrent.updateColumns()
            utility.window.details.updateAll()
        #else:
        #    print "NOT?"
        #    # Clear selection [no details]
        #    #utility.window.list.unSelectAll()        

    #######################################
    # Window Settings
    #######################################
    def onFullScreen(self, event = None):
        """
        Toggles full screen mode 
        """
        if self.IsFullScreen():
            self.ShowFullScreen(False)
        elif event:
            # keep toolbar and menubar... for now
            #wx.FULLSCREEN_NOMENUBAR | wx.FULLSCREEN_NOTOOLBAR 
            style = wx.FULLSCREEN_NOSTATUSBAR | wx.FULLSCREEN_NOCAPTION |wx.FULLSCREEN_NOBORDER
            self.ShowFullScreen(True, style)

        if event:
            event.Skip()

    def updateMenuChecks(self):
        for action in utility.actions.values():
            if hasattr(action, "updateCheck"):
                action.updateCheck()

        # page selector
        utility.actions[ACTION_PAGE_SELECTION].updateButton()
        # Directory Scanner
        utility.actions[ACTION_DIRSCANNER].updateButton()
        
    def _getWindowSettings(self):
        width = utility.config.Read("window_width")
        height = utility.config.Read("window_height")
        try:
            size = wx.Size(int(width), int(height))
        except:
            size = wx.Size(710, 400)
        
        x = utility.config.Read("window_x")
        y = utility.config.Read("window_y")
        if (x == "" or y == ""):
            position = wx.DefaultPosition
        else:
            position = wx.Point(int(x), int(y))

        return size, position
        
    def _saveWindowSettings(self):
        width, height = self.GetSizeTuple()
        x, y = self.GetPositionTuple()
        utility.config.Write("window_x", x)
        utility.config.Write("window_y", y)
        utility.config.Write("window_width", width)
        utility.config.Write("window_height", height)

        utility.config.Write("show_statusbar", self.StatusBar.IsShown(), "boolean")
        utility.config.Write("show_toolbar", self.tb.IsShown(), "boolean")
        utility.config.Write("show_inspector", utility.window.details.IsShown() and not utility.actions[ACTION_TORRENTDETAILS].isFramed(), "boolean")
        utility.config.Write("framed_inspector", utility.actions[ACTION_TORRENTDETAILS].isFramed(), "boolean")

        if utility.window.splitter.IsSplit():
            utility.config.Write("splitter_pos", utility.window.splitter.GetSashPosition(), "int")

        utility.config.Flush()

    def OnSysColourChange(self, event):
        try:
            utility.actions[ACTION_THEME].update()
        except:
            pass
        event.Skip()
        
    def _loadWindowSettings(self):
        # Hide statusbar if needed
        if not utility.config.Read("show_statusbar", "boolean"):
            utility.actions[ACTION_VIEWSTATUSBAR].action()
        # Hide toolbar if needed
        if not utility.config.Read("show_toolbar", "boolean"):
            utility.actions[ACTION_VIEWTOOLBAR].action()
        # Make sure the current position is valid
        self.RepositionInScreen()
        # Maximize if needed
        if utility.config.Read('maximized', "boolean"):
            self.Maximize()
        # Mac fix
        if wx.Platform == "__WXMAC__":
            self.SetSize(self._getWindowSettings()[0])
        # Apply theme color
        if wx.Platform == "__WXMSW__":
            utility.actions[ACTION_THEME].update()
	
    def RepositionInScreen(self):
        displayRect = wx.GetClientDisplayRect()
        currentRect = self.GetRect()
        if currentRect.IsEmpty() or displayRect.IsEmpty():
            return False
        if currentRect.top < displayRect.top:
            currentRect.y += displayRect.top - currentRect.top
        if currentRect.bottom > displayRect.bottom:
            currentRect.y += displayRect.bottom - currentRect.bottom
        if currentRect.left < displayRect.left:
            currentRect.x += displayRect.left - currentRect.left
        if currentRect.right > displayRect.right:
            currentRect.x += displayRect.right - currentRect.right
        self.SetRect(currentRect)
        return True    

    def _Refresh(self):
        self.Layout()
        self.SendSizeEvent()
        self.Refresh()

    ##################################
    # Hot Key Control
    ##################################      
    def HotKeyDown(self, event=None):
        """
        Hides/Shows the entire application
        """
        if utility.hotkey["hiding"]:
            for item in self.openwin:
                item.Show()
            for item in self.modaldlg:
                item.SetPosition(self.modaldlg[item])
                item.SetFocus()
                
            if self.IsIconized():
                self.tbicon.updateIcon(True)
            else:
                self.tbicon.updateIcon()
                # Resume updating GUI
                self.setGUIupdate(True)
        else:
            self.openwin = []
            self.modaldlg = {}
            for item in wx.GetTopLevelWindows():
                if hasattr(item, "GetFrame"):
                    item = item.GetFrame()
                if item.IsShown():
                    if (not hasattr(item, "IsModal")) or not item.IsModal():
                        item.Hide()
                        self.openwin.append(item)
                    else:
                        self.modaldlg[item] = item.GetPosition()
                        item.SetPosition(wx.Point(-0xFFFF, -0xFFFF))
            if self.tbicon.IsIconInstalled():
                self.tbicon.RemoveIcon()
            # stop updating GUI
            self.setGUIupdate(False)

        utility.hotkey["hiding"] = not utility.hotkey["hiding"]

    ##################################
    # Close Program
    ##################################
    def OnClose(self, event):
        # FIXME: Mac Hack for now...
        if wx.Platform == "__WXMAC__":
            if hasattr(wx.GetApp(), "MacHideApp"):
                wx.GetApp().MacHideApp()
            else:
                self.OnCloseWindow(event)
            return
        
        # Mimimize on close
        if utility.config.Read("mintray", "int") == 2 and event.CanVeto():
            self.onIconify()
            event.Veto()
        # Destroy on close
        else:
            self.OnCloseWindow(event)
                                    
    def OnCloseWindow(self, event = None, silent = False, shutdown = False):
        if utility.quitting:
            return
        
        if not silent and utility.config.Read('confirmonclose', "boolean") \
           and (event is None or event.CanVeto()):
            dialog = wx.MessageDialog(None,
                                      _('Are you sure you want to exit?'),
                                      _('Exit Confirmation'),
                                      wx.OK|wx.CANCEL)
            result = dialog.ShowModal()
            dialog.Destroy()
            if result != wx.ID_OK:
                if event: event.Veto()
                return

        utility.quitting = True

        # tell scheduler to close all active thread
        utility.queue.clearScheduler()

        # Cancel logger
        try:
            utility.window['log'].install(False)
        except:
            pass
        
        # Make sure not hidden
        if wx.Platform == "__WXMSW__" and utility.hotkey["hiding"]: 
            self.HotKeyDown()

        # Close Torrent Maker
        utility.actions[ACTION_MAKER].closeWin()

        # Stop Web Service
        try:
            utility.webserver.stop()
        except:
            pass
        
        # Stop Web Service
        try:
            self.commandScheduler.stop()
        except:
            pass

        # Save Window Size/Perspective
        self.onFullScreen()
        utility.config.Write('maximized', self.IsMaximized(), "boolean")
        self.onTaskBarActivate(close = True)
        self._saveWindowSettings()

        # Shutdown computer
        if shutdown:
            self.ShutDown()

        # Destroy Icon
        if self.tbicon is not None:
            self.tbicon.Destroy()
            self.tbicon = None
            
        # Destroy
        self.Destroy()
        
    def ShutDown(self):
        # Disable hotkey function
        if wx.Platform == "__WXMSW__" and utility.hotkey["active"]:
            if not self.UnregisterHotKey(utility.hotkey["ID"]):
                self.Bind(wx.EVT_HOTKEY, None)

        # Wait 3 minutes before shutting down
        self.Hide()
        delacdlg = wx.ProgressDialog(_('Shutting Down Computer'), _('A computer shutdown will occur soon'),
                                        maximum = 720, style = wx.PD_CAN_ABORT | wx.PD_AUTO_HIDE)
        keepgoing = True
        count = 0
        while keepgoing and count < 720:
            count += 1
            wx.MilliSleep(250)
            keepgoing = delacdlg.Update(count)
            if type(keepgoing) is tuple:
                keepgoing = keepgoing[0]
        delacdlg.Destroy()
        if keepgoing:
            utility.shutdownComputer()