home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / deskbar / DeskbarHistory.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  8.2 KB  |  271 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import cPickle
  5. import os
  6. import gtk
  7. import gobject
  8. from deskbar import MAX_HISTORY, HISTORY_FILE
  9. from deskbar.Utils import load_icon
  10. from gettext import gettext as _
  11.  
  12. class DeskbarHistoryIter:
  13.     '''An iter type to iterate over a DeskbarHistory.
  14. \tThis object is (typically) not used directly.
  15. \tFor documentation on iters see: http://docs.python.org/lib/typeiter.html
  16. \t'''
  17.     
  18.     def __init__(self, owner):
  19.         self.owner = owner
  20.         self.owner_iter = owner.get_iter_first()
  21.  
  22.     
  23.     def __iter__(self):
  24.         return self
  25.  
  26.     
  27.     def next(self):
  28.         
  29.         try:
  30.             item = self.owner[self.owner_iter][0]
  31.             self.owner_iter = self.owner.iter_next(self.owner_iter)
  32.         except TypeError:
  33.             raise StopIteration
  34.  
  35.         return item
  36.  
  37.  
  38. empty_history_icon = load_icon(gtk.STOCK_STOP)
  39.  
  40. class EmptyHistoryMatch:
  41.     
  42.     def get_icon(self):
  43.         return empty_history_icon
  44.  
  45.     
  46.     def action(self, text = None):
  47.         pass
  48.  
  49.     
  50.     def get_name(self, text = None):
  51.         return {
  52.             'msg': _('No History') }
  53.  
  54.     
  55.     def get_verb(self):
  56.         return '%(msg)s'
  57.  
  58.  
  59.  
  60. class DeskbarHistory(gtk.ListStore):
  61.     '''
  62. \tIterating over a DeskbarHistory with a for loop returns (text,match) pairs.
  63. \tKeeps an internal pointer to a history index which you can move with up(), down(),
  64. \tand reset(). You retrieve the item in question by get_history().
  65. \t
  66. \tText-Match pairs are stored in column 0, while a timestamp (a simple counter really)
  67. \tis stored in column 1. The timestamp is used for sorting purposes.
  68. \t
  69. \tSignals:
  70. \t\t"changed" : emitted when the internal pointer has changed
  71. \t'''
  72.     __gsignals__ = {
  73.         'changed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []) }
  74.     
  75.     def __init__(self):
  76.         gtk.ListStore.__init__(self, gobject.TYPE_PYOBJECT, gobject.TYPE_INT)
  77.         self.set_sort_column_id(1, gtk.SORT_ASCENDING)
  78.         self.set_sort_func(1, self.sort_matches)
  79.         self.timestamp = 0
  80.         self._index = -1
  81.  
  82.     
  83.     def __iter__(self):
  84.         return DeskbarHistoryIter(self)
  85.  
  86.     
  87.     def set_sort_order(self, order):
  88.         '''order should be one of gtk.SORT_{ASCENDING,DESCENDING}'''
  89.         self.set_sort_column_id(1, order)
  90.  
  91.     
  92.     def sort_matches(self, model, iter1, iter2):
  93.         if self[iter1][1] > self[iter2][1]:
  94.             return 1
  95.         else:
  96.             return -1
  97.  
  98.     
  99.     def clear(self):
  100.         gtk.ListStore.clear(self)
  101.         self.timestamp = 0
  102.         self.append(('', EmptyHistoryMatch()))
  103.         self._index = -1
  104.         self.emit('changed')
  105.  
  106.     
  107.     def clear_stub(self):
  108.         if len(self) == 1 and self[self.get_iter_first()][0][1].__class__ == EmptyHistoryMatch:
  109.             gtk.ListStore.clear(self)
  110.         
  111.  
  112.     
  113.     def load(self, module_list):
  114.         new_history = []
  115.         
  116.         try:
  117.             saved_history = cPickle.load(file(HISTORY_FILE))
  118.             
  119.             def strip_class(name):
  120.                 i = name.rfind('.')
  121.                 if i == -1:
  122.                     return None
  123.                 
  124.                 return name[i + 1:]
  125.  
  126.             for text, handler_class_name, match_class_name, serialized in saved_history:
  127.                 for modctx in module_list:
  128.                     if strip_class(handler_class_name) != modctx.handler:
  129.                         continue
  130.                     
  131.                     match_class = strip_class(match_class_name)
  132.                     if match_class == None:
  133.                         continue
  134.                     
  135.                     match = modctx.module.deserialize(match_class, serialized)
  136.                     if match != None:
  137.                         new_history.append((text, match))
  138.                         continue
  139.                 
  140.         except IOError:
  141.             pass
  142.         except Exception:
  143.             msg = None
  144.             return None
  145.  
  146.         self.clear()
  147.         if len(new_history) > 0:
  148.             self.clear_stub()
  149.             for hist in new_history:
  150.                 self.append(hist)
  151.             
  152.         
  153.  
  154.     
  155.     def save(self):
  156.         (column_id, old_order) = self.get_sort_column_id()
  157.         self.set_sort_column_id(1, gtk.SORT_ASCENDING)
  158.         save = []
  159.         for text, match in self:
  160.             if match.__class__ == EmptyHistoryMatch:
  161.                 return None
  162.             
  163.             hsh = match.get_hash(text)
  164.             serialized = match.serialize()
  165.             if serialized != None:
  166.                 save.append((text, str(match.get_handler().__class__), str(match.__class__), serialized))
  167.                 continue
  168.         
  169.         
  170.         try:
  171.             cPickle.dump(save, file(HISTORY_FILE, 'w'), cPickle.HIGHEST_PROTOCOL)
  172.         except Exception:
  173.             msg = None
  174.             print 'Error:History.save:%s', msg
  175.  
  176.         self.set_sort_column_id(1, old_order)
  177.  
  178.     
  179.     def append(self, match_obj):
  180.         gtk.ListStore.append(self, (match_obj, self.timestamp))
  181.         self.timestamp = self.timestamp + 1
  182.  
  183.     
  184.     def prepend(self, match_obj):
  185.         print 'ERROR: DeskbarHistory does not support prepending of matches, use append() instead.'
  186.         raise Exception
  187.  
  188.     
  189.     def add(self, text, match):
  190.         if match.__class__ == EmptyHistoryMatch:
  191.             return None
  192.         
  193.         if match.skip_history():
  194.             self.reset()
  195.             return None
  196.         
  197.         self.clear_stub()
  198.         copy_match = True
  199.         for idx, val in enumerate(self):
  200.             (htext, hmatch) = val
  201.             if (match.get_hash(text), match.__class__) == (hmatch.get_hash(htext), hmatch.__class__):
  202.                 match = self[self.get_iter_from_string(str(idx))][0][1]
  203.                 self.remove(self.get_iter_from_string(str(idx)))
  204.                 copy_match = False
  205.                 break
  206.                 continue
  207.         
  208.         if copy_match:
  209.             copy = match.copy()
  210.             if copy != None:
  211.                 match = copy
  212.             
  213.         
  214.         self.append((text, match))
  215.         if len(self) > MAX_HISTORY:
  216.             last = self.get_iter_from_string(str(len(self) - 1))
  217.             self.remove(last)
  218.         
  219.         self.reset()
  220.         self.save()
  221.  
  222.     
  223.     def up(self):
  224.         if self._index < len(self) - 1:
  225.             self._index = self._index + 1
  226.             self.emit('changed')
  227.         
  228.  
  229.     
  230.     def down(self):
  231.         if self._index > -1:
  232.             self._index = self._index - 1
  233.             self.emit('changed')
  234.         
  235.  
  236.     
  237.     def reset(self):
  238.         if self._index != -1:
  239.             self._index = -1
  240.             self.emit('changed')
  241.         
  242.  
  243.     
  244.     def last(self):
  245.         if len(self) == 0:
  246.             return None
  247.         
  248.         last = self.get_iter_from_string(str(len(self) - 1))
  249.         return self[last][0]
  250.  
  251.     
  252.     def get_all_history(self):
  253.         return self
  254.  
  255.     
  256.     def get_history(self):
  257.         if self._index == -1:
  258.             return None
  259.         
  260.         return self[self.get_iter_from_string(str(self._index))][0]
  261.  
  262.  
  263. if gtk.pygtk_version < (2, 8, 0):
  264.     gobject.type_register(DeskbarHistory)
  265.  
  266. shared_history = DeskbarHistory()
  267.  
  268. def get_deskbar_history():
  269.     return shared_history
  270.  
  271.