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

杀毒

开发平台:

Visual C++

  1. #Boa:Dialog:wxDialogScheduledScan
  2. #-----------------------------------------------------------------------------
  3. # Name:        wxDialogScheduledScan.py
  4. # Product:     ClamWin Free Antivirus
  5. #
  6. # Author:      alch [alch at users dot sourceforge dot net]
  7. #
  8. # Created:     2004/18/04
  9. # Copyright:   Copyright alch (c) 2004
  10. # Licence:     
  11. #   This program is free software; you can redistribute it and/or modify
  12. #   it under the terms of the GNU General Public License as published by
  13. #   the Free Software Foundation; either version 2 of the License, or
  14. #   (at your option) any later version.
  15. #   This program is distributed in the hope that it will be useful,
  16. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. #   GNU General Public License for more details.
  19. #   You should have received a copy of the GNU General Public License
  20. #   along with this program; if not, write to the Free Software
  21. #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. #-----------------------------------------------------------------------------
  23. from wxPython.wx import *
  24. from wxPython.lib.timectrl import *
  25. import os, sys, time, locale
  26. import Utils
  27. import shelve
  28. # scheduled scan information holder
  29. # it is used for persistent storage
  30. class ScheduledScanInfo(list):
  31.     def __init__(self, frequency='Daily', time='18:30:00', weekDay=3, path='', description='', active=True):
  32.         list.__init__(self, [frequency, time, weekDay, path, description, active])            
  33.         
  34.     def __getFrequency(self): return self[0]
  35.     def __setFrequency(self, value): self[0] = value    
  36.     Frequency = property(__getFrequency, __setFrequency)
  37.     
  38.     def __getTime(self): return self[1]
  39.     def __setTime(self, value): self[1] = value    
  40.     Time = property(__getTime, __setTime)
  41.     
  42.     def __getWeekDay(self): return self[2]
  43.     def __setWeekDay(self, value): self[2] = value    
  44.     WeekDay = property(__getWeekDay, __setWeekDay)
  45.         
  46.     def __getPath(self): return self[3]
  47.     def __setPath(self, value): self[3] = value    
  48.     Path = property(__getPath, __setPath)
  49.     
  50.     def __getDescription(self): return self[4]
  51.     def __setDescription(self, value): self[4] = value    
  52.     Description = property(__getDescription, __setDescription)
  53.     
  54.     def __getActive(self): return self[5]
  55.     def __setActive(self, value): self[5] = value    
  56.     Active = property(__getActive, __setActive)
  57.     
  58. def LoadPersistentScheduledScans(filename):
  59.     try:              
  60.         _shelve = shelve.open(filename)
  61.         # set version of the persistent storage data
  62.         # we may need it in future when upgrading to newer data set
  63.         try:
  64.             version = _shelve['version']
  65.         except KeyError:
  66.             version = 1
  67.         # read our scheduled scans info
  68.         # or create a new empty list    
  69.         try:
  70.             scheduledScans = _shelve['ScheduledScans']                                 
  71.         except KeyError:
  72.             scheduledScans = []        
  73.         if version < 2:
  74.             for i in range(len(scheduledScans)):
  75.                 scheduledScans[i] = ScheduledScanInfo(scheduledScans[i][0],
  76.                                     scheduledScans[i][1], scheduledScans[i][2],
  77.                                     scheduledScans[i][3], 
  78.                                     'Scan ' + scheduledScans[i][3], True)          
  79.             _shelve['version'] = 2        
  80.             _shelve['ScheduledScans'] = scheduledScans
  81.     except Exception, e:       
  82.         scheduledScans = [] 
  83.         print 'Could not open persistent storage for scheduled scans. Error: %s' % str(e)            
  84.     
  85.     return scheduledScans
  86. def SavePersistentScheduledScans(filename, scheduledScans):
  87.     try:
  88.         _shelve = shelve.open(filename)        
  89.         _shelve['ScheduledScans'] = scheduledScans
  90.         _shelve['version'] = 2
  91.     except Exception, e:               
  92.         print 'Could not save scheduled scans to persistent storage. Error: %s' % str(e)                
  93.     
  94. def create(parent, scanInfo):
  95.     return wxDialogScheduledScan(parent, scanInfo)
  96. [wxID_WXDIALOGSCHEDULEDSCAN, wxID_WXDIALOGSCHEDULEDSCANBUTTONBROWSEFOLDER, 
  97.  wxID_WXDIALOGSCHEDULEDSCANBUTTONCANCEL, wxID_WXDIALOGSCHEDULEDSCANBUTTONOK, 
  98.  wxID_WXDIALOGSCHEDULEDSCANCHECKBOXENABLED, 
  99.  wxID_WXDIALOGSCHEDULEDSCANCHOICEDAY, 
  100.  wxID_WXDIALOGSCHEDULEDSCANCHOICEFREQUENCY, 
  101.  wxID_WXDIALOGSCHEDULEDSCANSPINBUTTONTIME, 
  102.  wxID_WXDIALOGSCHEDULEDSCANSTATICBOX1, 
  103.  wxID_WXDIALOGSCHEDULEDSCANSTATICLINETIMECTRL, 
  104.  wxID_WXDIALOGSCHEDULEDSCANSTATICTEXTDAY, 
  105.  wxID_WXDIALOGSCHEDULEDSCANSTATICTEXTDESCRIPTION, 
  106.  wxID_WXDIALOGSCHEDULEDSCANSTATICTEXTFOLDER, 
  107.  wxID_WXDIALOGSCHEDULEDSCANSTATICTEXTFREQUENCY, 
  108.  wxID_WXDIALOGSCHEDULEDSCANSTATICTEXTTIME, 
  109.  wxID_WXDIALOGSCHEDULEDSCANTEXTCTRLDESCRIPTION, 
  110.  wxID_WXDIALOGSCHEDULEDSCANTEXTCTRLFOLDER, 
  111. ] = map(lambda _init_ctrls: wxNewId(), range(17))
  112. class wxDialogScheduledScan(wxDialog):
  113.     def _init_ctrls(self, prnt):
  114.         # generated method, don't edit
  115.         wxDialog.__init__(self, id=wxID_WXDIALOGSCHEDULEDSCAN,
  116.               name='wxDialogScheduledScan', parent=prnt, pos=wxPoint(427, 201),
  117.               size=wxSize(311, 301), style=wxDEFAULT_DIALOG_STYLE,
  118.               title='Scheduled Scan')
  119.         self.SetClientSize(wxSize(303, 274))
  120.         self.SetToolTipString('')
  121.         self.Center(wxBOTH)
  122.         EVT_CHAR_HOOK(self, self.OnCharHook)
  123.         self.staticBox1 = wxStaticBox(id=wxID_WXDIALOGSCHEDULEDSCANSTATICBOX1,
  124.               label='Schedule', name='staticBox1', parent=self, pos=wxPoint(11,
  125.               8), size=wxSize(282, 104), style=0)
  126.         self.staticTextFrequency = wxStaticText(id=wxID_WXDIALOGSCHEDULEDSCANSTATICTEXTFREQUENCY,
  127.               label='Scanning &Frequency:', name='staticTextFrequency',
  128.               parent=self, pos=wxPoint(25, 30), size=wxSize(131, 13), style=0)
  129.         self.staticTextFrequency.SetToolTipString('')
  130.         self.choiceFrequency = wxChoice(choices=['Hourly', 'Daily', 'Workdays',
  131.               'Weekly'], id=wxID_WXDIALOGSCHEDULEDSCANCHOICEFREQUENCY,
  132.               name='choiceFrequency', parent=self, pos=wxPoint(171, 27),
  133.               size=wxSize(107, 21), style=0)
  134.         self.choiceFrequency.SetColumns(2)
  135.         self.choiceFrequency.SetToolTipString('How often the schedule is executed')
  136.         self.choiceFrequency.SetStringSelection('Daily')
  137.         EVT_CHOICE(self.choiceFrequency,
  138.               wxID_WXDIALOGSCHEDULEDSCANCHOICEFREQUENCY,
  139.               self.OnChoiceFrequency)
  140.         self.staticTextTime = wxStaticText(id=wxID_WXDIALOGSCHEDULEDSCANSTATICTEXTTIME,
  141.               label='&Time:', name='staticTextTime', parent=self,
  142.               pos=wxPoint(25, 56), size=wxSize(121, 18), style=0)
  143.         self.staticLineTimeCtrl = wxStaticLine(id=wxID_WXDIALOGSCHEDULEDSCANSTATICLINETIMECTRL,
  144.               name='staticLineTimeCtrl', parent=self, pos=wxPoint(171, 54),
  145.               size=wxSize(90, 22), style=0)
  146.         self.staticLineTimeCtrl.Show(False)
  147.         self.staticLineTimeCtrl.SetToolTipString('When the schedule should be started')
  148.         self.spinButtonTime = wxSpinButton(id=wxID_WXDIALOGSCHEDULEDSCANSPINBUTTONTIME,
  149.               name='spinButtonTime', parent=self, pos=wxPoint(261, 53),
  150.               size=wxSize(16, 22), style=wxSP_ARROW_KEYS | wxSP_VERTICAL)
  151.         self.spinButtonTime.SetToolTipString('')
  152.         self.staticTextDay = wxStaticText(id=wxID_WXDIALOGSCHEDULEDSCANSTATICTEXTDAY,
  153.               label='&Day Of The Week:', name='staticTextDay', parent=self,
  154.               pos=wxPoint(25, 85), size=wxSize(123, 18), style=0)
  155.         self.staticTextDay.SetToolTipString('')
  156.         self.choiceDay = wxChoice(choices=['Monday', 'Tuesday', 'Wednesday',
  157.               'Thursday', 'Friday', 'Saturday', 'Sunday'],
  158.               id=wxID_WXDIALOGSCHEDULEDSCANCHOICEDAY, name='choiceDay',
  159.               parent=self, pos=wxPoint(171, 82), size=wxSize(107, 21), style=0)
  160.         self.choiceDay.SetColumns(2)
  161.         self.choiceDay.SetToolTipString('When schedule frequency is weekly select day of the week')
  162.         self.choiceDay.SetStringSelection('Tuesday')
  163.         self.staticTextFolder = wxStaticText(id=wxID_WXDIALOGSCHEDULEDSCANSTATICTEXTFOLDER,
  164.               label='&Scan Folder:', name='staticTextFolder', parent=self,
  165.               pos=wxPoint(11, 121), size=wxSize(78, 15), style=0)
  166.         self.staticTextFolder.SetToolTipString('')
  167.         self.textCtrlFolder = wxTextCtrl(id=wxID_WXDIALOGSCHEDULEDSCANTEXTCTRLFOLDER,
  168.               name='textCtrlFolder', parent=self, pos=wxPoint(11, 139),
  169.               size=wxSize(260, 20), style=0, value='')
  170.         self.textCtrlFolder.SetToolTipString('Specify a folder to be scanned')
  171.         self.buttonBrowseFolder = wxButton(id=wxID_WXDIALOGSCHEDULEDSCANBUTTONBROWSEFOLDER,
  172.               label='...', name='buttonBrowseFolder', parent=self,
  173.               pos=wxPoint(273, 139), size=wxSize(20, 20), style=0)
  174.         self.buttonBrowseFolder.SetToolTipString('Click to browse for a folder')
  175.         EVT_BUTTON(self.buttonBrowseFolder,
  176.               wxID_WXDIALOGSCHEDULEDSCANBUTTONBROWSEFOLDER,
  177.               self.OnButtonBrowseFolder)
  178.         self.staticTextDescription = wxStaticText(id=wxID_WXDIALOGSCHEDULEDSCANSTATICTEXTDESCRIPTION,
  179.               label='D&escription:', name='staticTextDescription', parent=self,
  180.               pos=wxPoint(11, 164), size=wxSize(68, 16), style=0)
  181.         self.staticTextDescription.SetToolTipString('')
  182.         self.textCtrlDescription = wxTextCtrl(id=wxID_WXDIALOGSCHEDULEDSCANTEXTCTRLDESCRIPTION,
  183.               name='textCtrlDescription', parent=self, pos=wxPoint(11, 182),
  184.               size=wxSize(282, 20), style=0, value='')
  185.         self.textCtrlDescription.SetToolTipString('Specify a friendly description for the scheduled scan')
  186.         self.checkBoxEnabled = wxCheckBox(id=wxID_WXDIALOGSCHEDULEDSCANCHECKBOXENABLED,
  187.               label='&Activate This Schedule', name='checkBoxEnabled',
  188.               parent=self, pos=wxPoint(11, 213), size=wxSize(278, 15), style=0)
  189.         self.checkBoxEnabled.SetValue(False)
  190.         self.checkBoxEnabled.SetToolTipString('Select if you wish to enable this schedule')
  191.         self.buttonOK = wxButton(id=wxID_WXDIALOGSCHEDULEDSCANBUTTONOK,
  192.               label='OK', name='buttonOK', parent=self, pos=wxPoint(73, 242),
  193.               size=wxSize(75, 23), style=0)
  194.         self.buttonOK.SetDefault()
  195.         self.buttonOK.SetToolTipString('Closes the dialog and applies the settings')
  196.         EVT_BUTTON(self.buttonOK, wxID_WXDIALOGSCHEDULEDSCANBUTTONOK, self.OnOK)
  197.         self.buttonCancel = wxButton(id=wxID_WXDIALOGSCHEDULEDSCANBUTTONCANCEL,
  198.               label='Cancel', name='buttonCancel', parent=self, pos=wxPoint(160,
  199.               242), size=wxSize(75, 23), style=0)
  200.         self.buttonCancel.SetToolTipString('Closes the dialog and discards the changes')
  201.         EVT_BUTTON(self.buttonCancel, wxID_WXDIALOGSCHEDULEDSCANBUTTONCANCEL,
  202.               self.OnCancel)
  203.     def __init__(self, parent, scanInfo):   
  204.         self._scanInfo = None     
  205.         self._scanInfo = scanInfo        
  206.         self._init_ctrls(parent)
  207.         locale.setlocale(locale.LC_ALL, 'C')                            
  208.         self.timeCtrl = wxTimeCtrl(parent=self, 
  209.                         pos=self.staticLineTimeCtrl.GetPosition(), 
  210.                         size=self.staticLineTimeCtrl.GetSize(), 
  211.                         fmt24hr=Utils.IsTime24(), 
  212.                         spinButton=self.spinButtonTime,
  213.                         useFixedWidthFont=False, display_seconds=True)
  214.         self.timeCtrl.SetToolTipString(self.staticLineTimeCtrl.GetToolTip().GetTip())
  215.         
  216.         self.choiceFrequency.SetValidator(MyValidator(self._scanInfo, 'Frequency'))        
  217.         self.timeCtrl.SetValidator(MyValidator(self._scanInfo, 'Time'))
  218.         self.choiceDay.SetValidator(MyValidator(self._scanInfo, 'WeekDay'))        
  219.         self.textCtrlDescription.SetValidator(MyValidator(self._scanInfo, 'Description', False))                
  220.         self.textCtrlFolder.SetValidator(MyValidator(self._scanInfo, 'Path', False))                
  221.         self.checkBoxEnabled.SetValidator(MyValidator(self._scanInfo, 'Active'))                
  222.         self.TransferDataToWindow()   
  223.         
  224.         self.choiceDay.Enable(self.choiceFrequency.GetStringSelection() == 'Weekly')        
  225.         
  226.     def _Apply(self):                       
  227.         if not self.Validate():
  228.             return False
  229.         self.TransferDataFromWindow()              
  230.         return True        
  231.     
  232.     def OnChoiceFrequency(self, event):        
  233.         self.choiceDay.Enable(self.choiceFrequency.GetStringSelection() == 'Weekly')
  234.         event.Skip()
  235.   
  236.     def OnOK(self, event):
  237.         if self._Apply():
  238.             self.EndModal(wxID_OK)
  239.     def OnCancel(self, event):
  240.         self.EndModal(wxID_CANCEL)
  241.     def OnCharHook(self, event):        
  242.         if event.GetKeyCode() == WXK_ESCAPE:
  243.             self.EndModal(wxID_CANCEL)
  244.         else:
  245.             event.Skip()  
  246.     def OnButtonBrowseFolder(self, event):
  247.         dlg = wxDirDialog(self)
  248.         try:
  249.             if dlg.ShowModal() == wxID_OK:
  250.                 dir = dlg.GetPath()                            
  251.                 self.textCtrlFolder.Clear()
  252.                 self.textCtrlFolder.WriteText(dir)   
  253.         finally:
  254.             dlg.Destroy()
  255. class MyValidator(wxPyValidator):
  256.     def __init__(self, scanInfo, propName, canEmpty=True):         
  257.         wxPyValidator.__init__(self)         
  258.         self._scanInfo = scanInfo
  259.         self._propName = propName
  260.         self._canEmpty = canEmpty
  261.                   
  262.     def Clone(self):         
  263.         return MyValidator(self._scanInfo, self._propName, self._canEmpty)
  264.     def Validate(self, win):         
  265.         ctrl = self.GetWindow()
  266.         if isinstance(ctrl, (wxChoice, wxCheckBox)) or self._canEmpty:
  267.             return True   
  268.         if isinstance(ctrl, wxSpinCtrl):     
  269.             text = str(ctrl.GetValue())
  270.         else:
  271.             text = ctrl.GetValue()
  272.         invalid = False
  273.         if len(text) == 0:            
  274.             wxMessageBox("Value cannot be empty", "ClamWin", style=wxICON_EXCLAMATION|wxOK)
  275.             invalid = True            
  276.         elif ctrl.GetName() == 'textCtrlFolder' and not os.path.exists(text):
  277.             wxMessageBox("Specified path is invalid. Plese verify your selection.", "ClamWin", style=wxICON_EXCLAMATION|wxOK)
  278.             invalid = True            
  279.         else:
  280.             ctrl.SetBackgroundColour(wxSystemSettings_GetColour(wxSYS_COLOUR_WINDOW))
  281.             ctrl.Refresh()            
  282.         if invalid:       
  283.             ctrl.SetBackgroundColour("yellow")
  284.             ctrl.SetFocus()
  285.             ctrl.Refresh()     
  286.         return not invalid
  287.     def TransferToWindow(self):
  288.         value = getattr(self._scanInfo, self._propName)         
  289.         ctrl = self.GetWindow()             
  290.         if(isinstance(ctrl, wxChoice)):
  291.             if ctrl.GetName() == 'choiceDay':
  292.                 ctrl.SetSelection(value)
  293.             else:
  294.                 ctrl.SetStringSelection(value)
  295.         else:            
  296.             ctrl.SetValue(value)         
  297.         return True
  298.     def TransferFromWindow(self):
  299.         ctrl = self.GetWindow()
  300.         if(isinstance(ctrl, wxChoice)):
  301.             if ctrl.GetName() == 'choiceDay':
  302.                 value = ctrl.GetSelection()
  303.             else:
  304.                 value = ctrl.GetStringSelection()            
  305.         elif isinstance(ctrl, wxCheckBox):
  306.             value = ctrl.GetValue()
  307.         elif isinstance(ctrl, wxTimeCtrl):
  308.             # set C locale, otherwise python and wxpython complain
  309.             locale.setlocale(locale.LC_ALL, 'C')            
  310.             value = ctrl.GetWxDateTime().Format('%H:%M:%S')
  311.         else:
  312.             value = ctrl.GetValue()                 
  313.         setattr(self._scanInfo, self._propName, value)
  314.         return True