[Spambayes-checkins] spambayes/Outlook2000 addin.py,1.57,1.58

Mark Hammond mhammond at users.sourceforge.net
Tue Jun 3 20:45:18 EDT 2003


Update of /cvsroot/spambayes/spambayes/Outlook2000
In directory sc8-pr-cvs1:/tmp/cvs-serv2893

Modified Files:
	addin.py 
Log Message:
Change the way we use toolbars - create our own, and add our items to that.
Seems to work fine for me.  You may need to reset your standard toolbar if
the old items are there.

Attempts were made to save the database when classifying - although the
save didn't happen (not dirty) is was an error.

Log refers to "SpamBayes" rather than "SpamAddin"

Wait cursor when incremental training (as it now includes the save, and
with many message selected, takes a long time anyway)


Index: addin.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/Outlook2000/addin.py,v
retrieving revision 1.57
retrieving revision 1.58
diff -C2 -d -r1.57 -r1.58
*** addin.py	16 May 2003 03:50:56 -0000	1.57
--- addin.py	4 Jun 2003 02:45:14 -0000	1.58
***************
*** 22,28 ****
--- 22,31 ----
  import pythoncom
  from win32com.client import constants, getevents
+ import win32ui
  
  import win32gui, win32con, win32clipboard # for button images!
  
+ toolbar_name = "SpamBayes"
+ 
  # If we are not running in a console, redirect all print statements to the
  # win32traceutil collector.
***************
*** 237,242 ****
                      print "already was trained as spam"
                  assert train.been_trained_as_spam(msgstore_message, self.manager)
!             # And if the DB can save itself incrementally, do it now
!             self.manager.SaveBayesPostIncrementalTrain()
  
  # Event function fired from the "Show Clues" UI items.
--- 240,245 ----
                      print "already was trained as spam"
                  assert train.been_trained_as_spam(msgstore_message, self.manager)
!                 # And if the DB can save itself incrementally, do it now
!                 self.manager.SaveBayesPostIncrementalTrain()
  
  # Event function fired from the "Show Clues" UI items.
***************
*** 348,351 ****
--- 351,355 ----
          if not msgstore_messages:
              return
+         win32ui.DoWaitCursor(1)
          # Delete this item as spam.
          spam_folder_id = self.manager.config.filter.spam_folder_id
***************
*** 359,363 ****
              # Must train before moving, else we lose the message!
              subject = msgstore_message.GetSubject()
!             print "Deleting and spam training message '%s' - " % (subject,),
              if train.train_message(msgstore_message, True, self.manager, rescore = True):
                  print "trained as spam"
--- 363,367 ----
              # Must train before moving, else we lose the message!
              subject = msgstore_message.GetSubject()
!             print "Moving and spam training message '%s' - " % (subject,),
              if train.train_message(msgstore_message, True, self.manager, rescore = True):
                  print "trained as spam"
***************
*** 370,373 ****
--- 374,378 ----
          # And if the DB can save itself incrementally, do it now
          self.manager.SaveBayesPostIncrementalTrain()
+         win32ui.DoWaitCursor(0)
  
  class ButtonRecoverFromSpamEvent(ButtonDeleteAsEventBase):
***************
*** 388,391 ****
--- 393,397 ----
          if not msgstore_messages:
              return
+         win32ui.DoWaitCursor(1)
          # Get the inbox as the default place to restore to
          # (incase we dont know (early code) or folder removed etc
***************
*** 417,420 ****
--- 423,427 ----
          # And if the DB can save itself incrementally, do it now
          self.manager.SaveBayesPostIncrementalTrain()
+         win32ui.DoWaitCursor(0)
  
  # Helpers to work with images on buttons/toolbars.
***************
*** 445,448 ****
--- 452,456 ----
          self.have_setup_ui = False
          self.explorer_list = explorer_list
+         self.toolbar = None
          self.buttons = []
  
***************
*** 451,474 ****
          self.buttons = []
          activeExplorer = self
!         bars = activeExplorer.CommandBars
!         toolbar = bars.Item("Standard")
          # Add our "Delete as ..." and "Recover as" buttons
          self.but_delete_as = self._AddControl(
!                         toolbar,
                          constants.msoControlButton,
                          ButtonDeleteAsSpamEvent, (self.manager, self),
!                         BeginGroup = True,
!                         Tag = "SpamBayes.DeleteAsSpam")
          # And again for "Recover as"
          self.but_recover_as = self._AddControl(
!                         toolbar,
                          constants.msoControlButton,
                          ButtonRecoverFromSpamEvent, (self.manager, self),
!                         Tag = "SpamBayes.RecoverFromSpam")
  
          # The main tool-bar dropdown with all our entries.
          # Add a pop-up menu to the toolbar
          popup = self._AddControl(
!                         toolbar,
                          constants.msoControlPopup,
                          None, None,
--- 459,481 ----
          self.buttons = []
          activeExplorer = self
!         assert self.toolbar is None, "Should not yet have a toolbar"
          # Add our "Delete as ..." and "Recover as" buttons
          self.but_delete_as = self._AddControl(
!                         None,
                          constants.msoControlButton,
                          ButtonDeleteAsSpamEvent, (self.manager, self),
!                         BeginGroup = False,
!                         Tag = "SpamBayesCommand.DeleteAsSpam")
          # And again for "Recover as"
          self.but_recover_as = self._AddControl(
!                         None,
                          constants.msoControlButton,
                          ButtonRecoverFromSpamEvent, (self.manager, self),
!                         Tag = "SpamBayesCommand.RecoverFromSpam")
  
          # The main tool-bar dropdown with all our entries.
          # Add a pop-up menu to the toolbar
          popup = self._AddControl(
!                         None,
                          constants.msoControlPopup,
                          None, None,
***************
*** 476,480 ****
                          TooltipText = "Anti-Spam filters and functions",
                          Enabled = True,
!                         Tag = "SpamBayes.Popup")
          # Convert from "CommandBarItem" to derived
          # "CommandBarPopup" Not sure if we should be able to work
--- 483,487 ----
                          TooltipText = "Anti-Spam filters and functions",
                          Enabled = True,
!                         Tag = "SpamBayesCommand.Popup")
          # Convert from "CommandBarItem" to derived
          # "CommandBarPopup" Not sure if we should be able to work
***************
*** 490,494 ****
                         TooltipText = "Show the Anti-Spam manager dialog.",
                         Enabled = True,
!                        Tag = "SpamBayes.Manager")
          self._AddControl(popup,
                         constants.msoControlButton,
--- 497,502 ----
                         TooltipText = "Show the Anti-Spam manager dialog.",
                         Enabled = True,
!                        Visible=True,
!                        Tag = "SpamBayesCommand.Manager")
          self._AddControl(popup,
                         constants.msoControlButton,
***************
*** 496,500 ****
                         Caption="Show spam clues for current message",
                         Enabled=True,
!                        Tag = "SpamBayes.Clues")
          # If we are running from Python sources, enable a few extra items
          if not hasattr(sys, "frozen"):
--- 504,509 ----
                         Caption="Show spam clues for current message",
                         Enabled=True,
!                        Visible=True,
!                        Tag = "SpamBayesCommand.Clues")
          # If we are running from Python sources, enable a few extra items
          if not hasattr(sys, "frozen"):
***************
*** 504,508 ****
                             Caption="Execute test suite",
                             Enabled=True,
!                            Tag = "SpamBayes.TestSuite")
          self.have_setup_ui = True
  
--- 513,518 ----
                             Caption="Execute test suite",
                             Enabled=True,
!                            Visible=True,
!                            Tag = "SpamBayesCommand.TestSuite")
          self.have_setup_ui = True
  
***************
*** 512,537 ****
                      events_class, events_init_args, # class/Init() args
                      **item_attrs): # extra control attributes.
!         # Sigh - sometimes our toolbar etc items will become
!         # permanent, even though we make them temporary.
!         # I found
          # http://groups.google.com/groups?threadm=eKKmbvQvAHA.1808%40tkmsftngp02
!         # Maybe we should consider making them permanent - this would then
!         # allow the user to drag them around the toolbars and have them
!         # stick.  The downside is that should the user uninstall this addin
!         # there is no clean way to remove the buttons.  Do we even care?
          assert item_attrs.has_key('Tag'), "Need a 'Tag' attribute!"
-         # Note we search *all* command bars here for the tag, only
-         # adding to the specified bar if not found.
          item = self.CommandBars.FindControl(
                          Type = control_type,
                          Tag = item_attrs['Tag'])
          if item is None:
!             item = parent.Controls.Add(Type=control_type, Temporary=True)
          # Hook events for the item
          if events_class is not None:
              item = DispatchWithEvents(item, events_class)
              item.Init(*events_init_args)
          for attr, val in item_attrs.items():
              setattr(item, attr, val)
          self.buttons.append(item)
          return item
--- 522,576 ----
                      events_class, events_init_args, # class/Init() args
                      **item_attrs): # extra control attributes.
!         # Outlook Toolbars suck :)
!         # We have tried a number of options: temp/perm in the standard toolbar,
!         # Always creating our own toolbar, etc.
!         # This seems to be fairly common:
          # http://groups.google.com/groups?threadm=eKKmbvQvAHA.1808%40tkmsftngp02
!         # Now the strategy is just to use our own, permanent toolbar, with
!         # permanent items, and ignore uninstall issues.
!         # We search all commandbars for a control with our Tag.  If found, we
!         # use it (the user may have customized the bar and moved our buttons
!         # elsewhere).  If we can not find the child control, we then try and
!         # locate our toolbar, creating if necessary.  Our items get added to
!         # that.
          assert item_attrs.has_key('Tag'), "Need a 'Tag' attribute!"
          item = self.CommandBars.FindControl(
                          Type = control_type,
                          Tag = item_attrs['Tag'])
          if item is None:
!             if parent is None:
!                 # No parent specified - that means top-level - locate the
!                 # toolbar to use as the parent.
!                 if self.toolbar is None:
!                     # See if we can find our "SpamBayes" toolbar
!                     # Indexing via the name appears unreliable, so just loop
!                     # Pity we have no "Tag" on a toolbar - then we could even
!                     # handle being renamed by the user.
!                     bars = self.CommandBars
!                     for i in range(bars.Count):
!                         toolbar = bars.Item(i+1)
!                         if toolbar.Name == "SpamBayes":
!                             self.toolbar = toolbar
!                             print "Found SB toolbar - visible state is", toolbar.Visible
!                             break
!                     else:
!                         # for not broken - can't find toolbar.  Create a new one.
!                         # Create it as a permanent one (which is default)
!                         print "Creating new SpamBayes toolbar to host our buttons"
!                         self.toolbar = bars.Add(toolbar_name, constants.msoBarTop, Temporary=False)
!                     self.toolbar.Visible = True
!                 parent = self.toolbar
!             # Now add the item itself to the parent.
!             item = parent.Controls.Add(Type=control_type, Temporary=False)
          # Hook events for the item
          if events_class is not None:
              item = DispatchWithEvents(item, events_class)
              item.Init(*events_init_args)
+         # Set the extra attributes passed in.
          for attr, val in item_attrs.items():
              setattr(item, attr, val)
+         # remember and return the item.
+         # (not remembering causes events to get disconnected as our objects
+         # destruct)
          self.buttons.append(item)
          return item
***************
*** 579,582 ****
--- 618,622 ----
              if closer is not None:
                  closer()
+         self.toolbar = None
          self.buttons = []
          self.close() # disconnect events.
***************
*** 665,669 ****
          # automatically disabled by Outlook.
          # Our error reporter is in the "manager" module, so we get that first
!         print "SpamAddin - Connecting to Outlook"
          import manager
          try:
--- 705,709 ----
          # automatically disabled by Outlook.
          # Our error reporter is in the "manager" module, so we get that first
!         print "SpamBayes - Connecting to Outlook"
          import manager
          try:
***************
*** 767,775 ****
  
      def OnDisconnection(self, mode, custom):
!         print "SpamAddin - Disconnecting from Outlook"
          self.folder_hooks = None
          self.application = None
          self.explorers_events = None
          if self.manager is not None:
              self.manager.Save()
              stats = self.manager.stats
--- 807,819 ----
  
      def OnDisconnection(self, mode, custom):
!         print "SpamBayes - Disconnecting from Outlook"
          self.folder_hooks = None
          self.application = None
          self.explorers_events = None
          if self.manager is not None:
+             # Save database - bsddb databases will generally do nothing here
+             # as it will not be dirty, but pickles will.
+             # config never needs saving as it is always done by whoever changes
+             # it (ie, the dialog)
              self.manager.Save()
              stats = self.manager.stats





More information about the Spambayes-checkins mailing list