wxFrameMain.py
上传用户:lswyart
上传日期:2008-06-12
资源大小:3441k
文件大小:20k
源码类别:

杀毒

开发平台:

Visual C++

  1. #-----------------------------------------------------------------------------
  2. #Boa:Frame:wxMainFrame
  3. #-----------------------------------------------------------------------------
  4. # Name:        wxFrameMain.py
  5. # Product:     ClamWin Free Antivirus
  6. #
  7. # Author:      alch [alch at users dot sourceforge dot net]
  8. #
  9. # Created:     2004/19/03
  10. # Copyright:   Copyright alch (c) 2004
  11. # Licence:     
  12. #   This program is free software; you can redistribute it and/or modify
  13. #   it under the terms of the GNU General Public License as published by
  14. #   the Free Software Foundation; either version 2 of the License, or
  15. #   (at your option) any later version.
  16. #   This program is distributed in the hope that it will be useful,
  17. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. #   GNU General Public License for more details.
  20. #   You should have received a copy of the GNU General Public License
  21. #   along with this program; if not, write to the Free Software
  22. #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. import sys, os
  24. from wxPython.wx import *
  25. import MsgBox, Utils, wxDialogUtils, version
  26. def create(parent, config):
  27.     return wxMainFrame(parent, config)
  28. [wxID_WXMAINFRAME, wxID_WXMAINFRAMEBUTTONCLOSE, wxID_WXMAINFRAMEBUTTONSCAN, 
  29.  wxID_WXMAINFRAMEDIRCTRLSCAN, wxID_WXMAINFRAMEPANELFRAME, 
  30.  wxID_WXMAINFRAMESTATIC1, wxID_WXMAINFRAMESTATUSBAR, wxID_WXMAINFRAMETOOLBAR, 
  31. ] = map(lambda _init_ctrls: wxNewId(), range(8))
  32. [wxID_WXMAINFRAMETOOLBARTOOLS_INETUPDATE, wxID_WXMAINFRAMETOOLBARTOOLS_PREFS, 
  33.  wxID_WXMAINFRAMETOOLBARTOOLS_SCAN, 
  34. ] = map(lambda _init_coll_toolBar_Tools: wxNewId(), range(3))
  35. [wxID_WXMAINFRAMEREPORTSDATABASE, wxID_WXMAINFRAMEREPORTSSCAN, 
  36. ] = map(lambda _init_coll_Reports_Items: wxNewId(), range(2))
  37. [wxID_WXMAINFRAMETOOLSDBUPDATE, wxID_WXMAINFRAMETOOLSPREFERENCES, 
  38.  wxID_WXMAINFRAMETOOLSREPORTS, 
  39. ] = map(lambda _init_coll_Tools_Items: wxNewId(), range(3))
  40. [wxID_WXMAINFRAMEHELPABOUT, wxID_WXMAINFRAMEHELPFAQ, wxID_WXMAINFRAMEHELPUPDATE, wxID_WXMAINFRAMEHELPWEBSITE, wxID_WXMAINFRAMEHELPHELP, 
  41. ] = map(lambda _init_coll_Help_Items: wxNewId(), range(5))
  42. [wxID_WXMAINFRAMEFILEITEMS0, wxID_WXMAINFRAMEFILESCAN, 
  43. ] = map(lambda _init_coll_File_Items: wxNewId(), range(2))
  44. class wxMainFrame(wxFrame):    
  45.     def _init_coll_flexGridSizerPanel_Items(self, parent):
  46.         # generated method, don't edit
  47.         parent.AddSpacer(8, 8, border=0, flag=0)
  48.         parent.AddWindow(self.static1, 0, border=5,
  49.               flag=wxLEFT | wxRIGHT | wxBOTTOM | wxALIGN_LEFT)
  50.         parent.AddWindow(self.dirCtrlScan, 0, border=10,
  51.               flag=wxLEFT | wxRIGHT | wxGROW)
  52.     def _init_coll_flexGridSizerPanel_Growables(self, parent):
  53.         # generated method, don't edit
  54.         parent.AddGrowableRow(2)
  55.         parent.AddGrowableCol(0)
  56.     def _init_coll_gridSizerFrame_Items(self, parent):
  57.         # generated method, don't edit
  58.         parent.AddWindow(self.panelFrame, 0, border=0, flag=wxGROW)
  59.     def _init_coll_gridSizerButtons_Items(self, parent):
  60.         # generated method, don't edit
  61.         parent.AddWindow(self.buttonScan, 0, border=10,
  62.               flag=wxALIGN_RIGHT | wxALL)
  63.         parent.AddWindow(self.buttonClose, 0, border=10,
  64.               flag=wxALIGN_LEFT | wxALL)
  65.     def _init_coll_Tools_Items(self, parent):
  66.         # generated method, don't edit
  67.         parent.Append(helpString='Displays the configuration window',
  68.               id=wxID_WXMAINFRAMETOOLSPREFERENCES, item='&Preferences',
  69.               kind=wxITEM_NORMAL)
  70.         parent.Append(helpString='Downloads latest virus database from the Internet',
  71.               id=wxID_WXMAINFRAMETOOLSDBUPDATE,
  72.               item='Download &Virus Database Update', kind=wxITEM_NORMAL)
  73.         parent.AppendMenu(helpString='Displays ClamWin Log Files',
  74.               id=wxID_WXMAINFRAMETOOLSREPORTS, item='&Display Reports',
  75.               subMenu=self.Reports)
  76.         EVT_MENU(self, wxID_WXMAINFRAMETOOLSPREFERENCES,
  77.               self.OnToolsPreferences)
  78.         EVT_MENU(self, wxID_WXMAINFRAMETOOLSDBUPDATE, self.OnToolsUpdate)
  79.     def _init_coll_menuBar_Menus(self, parent):
  80.         # generated method, don't edit
  81.         parent.Append(menu=self.File, title='&File')
  82.         parent.Append(menu=self.Tools, title='&Tools')
  83.         parent.Append(menu=self.Help, title='&Help')
  84.     def _init_coll_Help_Items(self, parent):
  85.         # generated method, don't edit
  86.         parent.Append(helpString='Displays ClamWin Free Antivirus Manual',
  87.               id=wxID_WXMAINFRAMEHELPHELP, item='&Help', kind=wxITEM_NORMAL)
  88.         parent.Append(helpString='Checks for the Latest Version',
  89.               id=wxID_WXMAINFRAMEHELPUPDATE, item='&Check Latest Version', kind=wxITEM_NORMAL)
  90.         parent.Append(helpString='Opens ClamWin Free Antivirus Website',
  91.               id=wxID_WXMAINFRAMEHELPWEBSITE, item='ClamWin &Website', kind=wxITEM_NORMAL)
  92.         parent.Append(helpString='Opens Frequently Asked Questions Page in the Web Browser',
  93.               id=wxID_WXMAINFRAMEHELPFAQ, item='&FAQ', kind=wxITEM_NORMAL)
  94.         parent.AppendSeparator()
  95.         parent.Append(helpString='Displays the About Box',
  96.               id=wxID_WXMAINFRAMEHELPABOUT, item='&About', kind=wxITEM_NORMAL)
  97.         EVT_MENU(self, wxID_WXMAINFRAMEHELPABOUT, self.OnHelpAbout)
  98.         EVT_MENU(self, wxID_WXMAINFRAMEHELPHELP, self.OnHelpHelp)
  99.         EVT_MENU(self, wxID_WXMAINFRAMEHELPUPDATE, self.OnHelpUpdate)
  100.         EVT_MENU(self, wxID_WXMAINFRAMEHELPWEBSITE, self.OnHelpWebsite)
  101.         EVT_MENU(self, wxID_WXMAINFRAMEHELPFAQ, self.OnHelpFAQ)
  102.     def _init_coll_Reports_Items(self, parent):
  103.         # generated method, don't edit
  104.         parent.Append(helpString='Displays Virus Database Update Log FIle',
  105.               id=wxID_WXMAINFRAMEREPORTSDATABASE,
  106.               item='&Virus Database Update Report', kind=wxITEM_NORMAL)
  107.         parent.Append(helpString='Displays Virus Scan Log File',
  108.               id=wxID_WXMAINFRAMEREPORTSSCAN, item='&Scan Report',
  109.               kind=wxITEM_NORMAL)
  110.         EVT_MENU(self, wxID_WXMAINFRAMEREPORTSDATABASE, self.OnViewUpdateLog)
  111.         EVT_MENU(self, wxID_WXMAINFRAMEREPORTSSCAN, self.OnViewScanLog)
  112.     def _init_coll_File_Items(self, parent):
  113.         # generated method, don't edit
  114.         
  115.         parent.Append(helpString='Scans Files or Folders for Computer Viruses',
  116.               id=wxID_WXMAINFRAMEFILESCAN, item='&Scan', kind=wxITEM_NORMAL)
  117.         parent.AppendSeparator()
  118.         parent.Append(helpString='Exits the application',
  119.               id=wxID_WXMAINFRAMEFILEITEMS0, item='E&xit', kind=wxITEM_NORMAL)
  120.         EVT_MENU(self, wxID_WXMAINFRAMEFILESCAN, self.OnScanButton)
  121.         EVT_MENU(self, wxID_WXMAINFRAMEFILEITEMS0, self.OnFileExit)        
  122.     def _init_coll_toolBar_Tools(self, parent):
  123.         # generated method, don't edit
  124.         parent.DoAddTool(bitmap=wxBitmap('img/Control.png', wxBITMAP_TYPE_PNG),
  125.               bmpDisabled=wxNullBitmap, id=wxID_WXMAINFRAMETOOLBARTOOLS_PREFS,
  126.               kind=wxITEM_NORMAL, label='Preferences',
  127.               longHelp='Displays Preferences Window',
  128.               shortHelp='Displays Preferences Window')
  129.         parent.DoAddTool(bitmap=wxBitmap('img/World.png', wxBITMAP_TYPE_PNG),
  130.               bmpDisabled=wxNullBitmap,
  131.               id=wxID_WXMAINFRAMETOOLBARTOOLS_INETUPDATE, kind=wxITEM_NORMAL,
  132.               label='Update',
  133.               longHelp='Updates virus databases over the Internet',
  134.               shortHelp='Starts Internet Update')
  135.         parent.AddSeparator()
  136.         parent.DoAddTool(bitmap=wxBitmap('img/Scan.png', wxBITMAP_TYPE_PNG),
  137.               bmpDisabled=wxNullBitmap, id=wxID_WXMAINFRAMETOOLBARTOOLS_SCAN,
  138.               kind=wxITEM_NORMAL, label='Scan',
  139.               longHelp='Scans Selected File or Folder for Computer Viruses',
  140.               shortHelp='Scans For Viruses')
  141.         EVT_TOOL(self, wxID_WXMAINFRAMETOOLBARTOOLS_INETUPDATE,
  142.               self.OnToolsUpdate)
  143.         EVT_TOOL(self, wxID_WXMAINFRAMETOOLBARTOOLS_PREFS,
  144.               self.OnToolsPreferences)
  145.         EVT_TOOL(self, wxID_WXMAINFRAMETOOLBARTOOLS_SCAN, self.OnScanButton)
  146.         parent.Realize()
  147.     def _init_sizers(self):
  148.         # generated method, don't edit
  149.         self.gridSizerFrame = wxGridSizer(cols=1, hgap=0, rows=1, vgap=0)
  150.         self.flexGridSizerPanel = wxFlexGridSizer(cols=1, hgap=0, rows=4,
  151.               vgap=0)
  152.         self.gridSizerButtons = wxGridSizer(cols=2, hgap=0, rows=1, vgap=0)
  153.         self._init_coll_gridSizerFrame_Items(self.gridSizerFrame)
  154.         self._init_coll_flexGridSizerPanel_Items(self.flexGridSizerPanel)
  155.         self._init_coll_flexGridSizerPanel_Growables(self.flexGridSizerPanel)
  156.         self._init_coll_gridSizerButtons_Items(self.gridSizerButtons)
  157.         self.SetSizer(self.gridSizerFrame)
  158.         self.panelFrame.SetSizer(self.flexGridSizerPanel)
  159.     def _init_utils(self):
  160.         # generated method, don't edit
  161.         self.menuBar = wxMenuBar()
  162.         self.File = wxMenu(title='')
  163.         self.Tools = wxMenu(title='')
  164.         self.Help = wxMenu(title='')
  165.         self.Reports = wxMenu(title='')
  166.         self._init_coll_menuBar_Menus(self.menuBar)
  167.         self._init_coll_File_Items(self.File)
  168.         self._init_coll_Tools_Items(self.Tools)
  169.         self._init_coll_Help_Items(self.Help)
  170.         self._init_coll_Reports_Items(self.Reports)
  171.     def _init_ctrls(self, prnt):
  172.         # generated method, don't edit
  173.         wxFrame.__init__(self, id=wxID_WXMAINFRAME, name='wxMainFrame',
  174.               parent=prnt, pos=wxPoint(250, 143), size=wxSize(568, 430),
  175.               style=wxDEFAULT_FRAME_STYLE, title='ClamWin Free Antivirus')
  176.         self._init_utils()
  177.         self.SetClientSize(wxSize(560, 403))
  178.         self.SetMenuBar(self.menuBar)
  179.         self.SetHelpText('ClamWin Free Antivirus')
  180.         self.Center(wxBOTH)
  181.         self.toolBar = wxToolBar(id=wxID_WXMAINFRAMETOOLBAR, name='toolBar',
  182.               parent=self, pos=wxPoint(0, 0), size=wxSize(560, 41),
  183.               style=wxTB_FLAT | wxTB_HORIZONTAL | wxNO_BORDER)
  184.         self.toolBar.SetToolTipString('')
  185.         self.toolBar.SetToolBitmapSize(wxSize(32, 32))
  186.         self.SetToolBar(self.toolBar)
  187.         self.statusBar = wxStatusBar(id=wxID_WXMAINFRAMESTATUSBAR,
  188.               name='statusBar', parent=self, style=0)
  189.         self.statusBar.SetSize(wxSize(537, 20))
  190.         self.statusBar.SetPosition(wxPoint(0, 218))
  191.         self.statusBar.SetToolTipString('Status Bar')
  192.         self.SetStatusBar(self.statusBar)
  193.         self.panelFrame = wxPanel(id=wxID_WXMAINFRAMEPANELFRAME,
  194.               name='panelFrame', parent=self, pos=wxPoint(0, 0),
  195.               size=wxSize(560, 403), style=wxTAB_TRAVERSAL)
  196.         self.static1 = wxStaticText(id=wxID_WXMAINFRAMESTATIC1,
  197.               label='Select a folder or a file to scann(Hold Shift key to select multiple files or folders)', name='static1',
  198.               parent=self.panelFrame, pos=wxPoint(5, 8), size=wxSize(435, 32),
  199.               style=0)
  200.         
  201.         self.dirCtrlScan = wxGenericDirCtrlEx(defaultFilter=0, dir='.', filter='',
  202.               id=wxID_WXMAINFRAMEDIRCTRLSCAN, name='dirCtrlScan',
  203.              parent=self.panelFrame, pos=wxPoint(10, 27), size=wxSize(540,
  204.              376),
  205.              style=wxDIRCTRL_SELECT_FIRST | wxSUNKEN_BORDER | wxDIRCTRL_3D_INTERNAL)
  206.         self.buttonScan = wxButton(id=wxID_WXMAINFRAMEBUTTONSCAN, label='&Scan',
  207.               name='buttonScan', parent=self.panelFrame, pos=wxPoint(-85, 10),
  208.               size=wxSize(75, 23), style=0)
  209.         self.buttonScan.SetDefault()
  210.         EVT_BUTTON(self.buttonScan, wxID_WXMAINFRAMEBUTTONSCAN,
  211.               self.OnScanButton)
  212.         self.buttonClose = wxButton(id=wxID_WXMAINFRAMEBUTTONCLOSE,
  213.               label='&Close', name='buttonClose', parent=self.panelFrame,
  214.               pos=wxPoint(10, 10), size=wxSize(75, 23), style=0)
  215.         EVT_BUTTON(self.buttonClose, wxID_WXMAINFRAMEBUTTONCLOSE,
  216.               self.OnButtonClose)
  217.         self._init_coll_toolBar_Tools(self.toolBar)
  218.         ##self._init_sizers()
  219.     def __init__(self, parent, config):
  220.         self._config = None
  221.         self._config = config
  222.         self._init_ctrls(parent)                     
  223.         
  224.         # select second in the directory tree (usually c:)
  225.         try:
  226.             # navigate to the second item
  227.             treeCtrl = self.dirCtrlScan.GetTreeCtrl()            
  228.             cookie = 0
  229.             itemid, cookie = treeCtrl.GetFirstChild(treeCtrl.GetRootItem(), cookie)            
  230.             itemid, cookie = treeCtrl.GetNextChild(treeCtrl.GetRootItem(), cookie)
  231.             # select it
  232.             treeCtrl.SetFocus()                         
  233.             treeCtrl.UnselectAll()                        
  234.             treeCtrl.SelectItem(itemid)                                    
  235.         except:                        
  236.             pass
  237.         
  238.         # we need to set controls heights to 0 and reinit sizers
  239.         # to overcome boa sizers bug
  240.         self.dirCtrlScan.SetSize((-1, 0))
  241.         self.panelFrame.SetSize((-1, 0))        
  242.         self._init_sizers()
  243.         self.flexGridSizerPanel.AddSizer(self.gridSizerButtons, flag = wxGROW)
  244.         # set window icons
  245.         icons = wxIconBundle()
  246.         icons.AddIconFromFile('img/FrameIcon.ico', wxBITMAP_TYPE_ICO)
  247.         self.SetIcons(icons)
  248.         
  249.         if not sys.platform.startswith('win'):
  250.             self.Help.Remove(wxID_WXMAINFRAMEHELPHELP)
  251.         self._UpdateState()
  252.         
  253.     def OnFileExit(self, event):
  254.         self.Close()
  255.     def OnToolsPreferences(self, event):
  256.         wxDialogUtils.wxConfigure(self, self._config)        
  257.         self._UpdateState()
  258.     def OnHelpAbout(self, event):
  259.         wxDialogUtils.wxAbout(self, self._config)    
  260.     def _IsConfigured(self):        
  261.         if self._config.Get('ClamAV', 'ClamScan') == '' or 
  262.             self._config.Get('ClamAV', 'FreshClam') == '' or 
  263.             self._config.Get('ClamAV', 'Database') == '' :
  264.             return False
  265.         else:
  266.             return True
  267.     def _UpdateState(self):
  268.         try:
  269.             # disable Run Command button if the configuration is invalid
  270.             # or no item is selected in the tree
  271.             configured = self._IsConfigured()       
  272.             if not configured:
  273.                 if wxID_YES == MsgBox.MessageBox(None, 'ClamWin Free Antivirus', 'ClamWin Free Antivirus is not configured. Would you like to configure it now?', wxYES_NO | wxICON_QUESTION):
  274.                     wxDialogUtils.wxConfigure(None, self._config)
  275.                     configured = self._IsConfigured()
  276.             hasdb = Utils.CheckDatabase(self._config)      
  277.             if configured and not hasdb:
  278.                 if wxID_YES == MsgBox.MessageBox(None, 'ClamWin Free Antivirus', 'You have not yet downloaded Virus Definitions Database. Would you like to download it now?', wxYES_NO | wxICON_QUESTION):
  279.                     wxDialogUtils.wxUpdateVirDB(self, self._config)
  280.                     hasdb = Utils.CheckDatabase(self._config)
  281.             self.buttonScan.Enable(configured and hasdb)
  282.             self.toolBar.EnableTool(wxID_WXMAINFRAMETOOLBARTOOLS_INETUPDATE, configured)    
  283.             self.toolBar.EnableTool(wxID_WXMAINFRAMETOOLBARTOOLS_SCAN, configured and hasdb)    
  284.             
  285.             
  286.             
  287.         except Exception, e:
  288.             print 'An Error occured while updating UI selection. %s' % str(e)
  289.     def OnScanButton(self, event):
  290.         scanPath = ''
  291.         for path in self.dirCtrlScan.GetMultiplePath():
  292.             #if sys.platform.startswith("win"):                           
  293.             #    path = path.replace('\', '/')                                    
  294.             scanPath += ""%s" " % path                             
  295.         wxDialogUtils.wxScan(self, self._config, scanPath)        
  296.     def OnToolsUpdate(self, event):
  297.         wxDialogUtils.wxUpdateVirDB(self, self._config)
  298.     def OnButtonClose(self, event):
  299.         self.Close()        
  300.     def OnViewUpdateLog(self, event):                
  301.         wxDialogUtils.wxShowLog(self, self._config.Get('Updates', 'DBUpdateLogFile'))
  302.     def OnViewScanLog(self, event):
  303.         wxDialogUtils.wxShowLog(self, self._config.Get('ClamAV', 'LogFile'))
  304.                                 
  305.     def OnHelpHelp(self, event):        
  306.         if sys.platform.startswith('win'):            
  307.             import win32api, win32con
  308.             curDir = Utils.GetCurrentDir(True)
  309.             helpfile = os.path.join(curDir, 'manual.chm')
  310.             if not os.path.isfile(helpfile):
  311.                 MsgBox.ErrorBox(self, 'Could not open help file - %s not found.' % helpfile)
  312.             else:    
  313.                 try:                
  314.                     win32api.ShellExecute(self.GetHandle(), 'open', 
  315.                         helpfile,
  316.                         None, curDir, win32con.SW_SHOWNORMAL)
  317.                 except Exception, e:
  318.                     MsgBox.ErrorBox(self, 'Could not open help file. Please ensure that you have Adobe Acrobat Reader installed.')
  319.     def OnHelpFAQ(self, event):
  320.         wxDialogUtils.wxGoToInternetUrl('http://sourceforge.net/docman/display_doc.php?docid=22588&group_id=105508')
  321.         
  322.         
  323.     def OnHelpUpdate(self, event):
  324.         wxDialogUtils.wxGoToInternetUrl('http://www.clamwin.com/index.php?option=content&task=view&id=40&Itemid=60&version='+version.clamwin_version)
  325.         
  326.                 
  327.     def OnHelpWebsite(self, event):
  328.         wxDialogUtils.wxGoToInternetUrl('http://www.clamwin.com')
  329.                 
  330. class wxGenericDirCtrlEx(wxGenericDirCtrl):    
  331.     def __init__(self,*_args,**_kwargs):
  332.         try:
  333.             if _kwargs['multiselect'] == True:
  334.                 multiselect = True
  335.                 del _kwargs['multiselect']
  336.             else:
  337.                 multiselect = False        
  338.         except KeyError:
  339.             multiselect = True
  340.             
  341.         try:    
  342.             if _kwargs['showhidden'] == True:
  343.                 showhidden = True
  344.                 del _kwargs['showhidden']
  345.             else:
  346.                 showhidden = False
  347.         except KeyError:
  348.             showhidden = True
  349.             
  350.         wxGenericDirCtrl.__init__(self, *_args, **_kwargs)        
  351.         self.ShowHidden(showhidden)
  352.         if multiselect:  
  353.             tree = self.GetTreeCtrl()                      
  354.             tree.SetWindowStyleFlag(tree.GetWindowStyleFlag() | wxTR_MULTIPLE)
  355.         
  356.     def GetMultiplePath(self):
  357.         multiPath = []
  358.         tree = self.GetTreeCtrl()
  359.         sels = tree.GetSelections()
  360.         for sel in sels:
  361.             item = sel
  362.             path = ''
  363.             itemtext = tree.GetItemText(item)
  364.             while True:
  365.                 try:                                              
  366.                     if not sys.platform.startswith("win"):                           
  367.                         # unix - terminate when path=='/'
  368.                         if itemtext == '/':
  369.                             path = itemtext + path                            
  370.                             break
  371.                         else:
  372.                             path = itemtext + '/' + path                            
  373.                     else:
  374.                         # windows, root drive is enclosed in ()
  375.                         if itemtext.startswith('(') and itemtext.endswith(':)'):
  376.                             # remove '(' and ')'
  377.                             itemtext = itemtext.strip('()')
  378.                             path = itemtext + '\' + path
  379.                             break
  380.                         else:                            
  381.                             path = itemtext + '\' + path                                                
  382.                     item = tree.GetItemParent(item)
  383.                     itemtext = tree.GetItemText(item)                                        
  384.                 except:
  385.                     break               
  386.             if len(path) > 1:
  387.                 path = path.rstrip('\/')                             
  388.             multiPath.append(path)
  389.         return multiPath
  390.         
  391.     
  392.