Implemented "mark all as read".
[straw.git] / straw / MainloopManager.py
blobf66f32af310d17980b6a969423ac6ce5719ad536
1 """ MainloopManager.py
3 """
4 __copyright__ = "Copyright (c) 2002-2005 Free Software Foundation, Inc."
5 __license__ = """
6 Straw is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 2 of the License, or (at your option) any later
9 version.
11 Straw is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 Place - Suite 330, Boston, MA 02111-1307, USA. """
20 import pygtk
21 pygtk.require("2.0")
22 import gtk
23 import gobject
24 import error
26 def schedule(function, *args, **kw):
27 """
28 Schedules a function call in the main loop thread.
29 The given arguments and keywords are passed along if any.
30 Threads may not access Straw and GTK elsewhere.
31 XXX occasional "Trying re-entry!" warnings
32 """
34 assert function.__call__
36 def callback():
37 function(*args, **kw)
38 return False # call only once
40 gobject.timeout_add(0, callback) # call as soon as possible
43 class MainloopManager:
44 instance = None
46 def __init__(self):
47 self._idle_handlers = []
48 self._timers = {}
49 self._call_depth = 0
51 def add_idle_handler(self, handler):
52 oid = self._add_idle(handler)
53 self._idle_handlers.append((oid, handler))
55 def _add_idle(self, handler):
56 oid = gtk.idle_add(handler)
57 return oid
59 def remove_idle_handler(self, handler):
60 for oid, h in self._idle_handlers:
61 if h == handler:
62 gtk.idle_remove(oid)
63 break
64 else:
65 error.log("no handler ", handler, " found!")
67 def remove_idle_handler_by_id(self, oid):
68 gtk.idle_remove(oid)
70 def call_pending(self):
71 if self._call_depth > 0:
72 error.log("Trying re-entry! Not allowed.")
73 return
74 self._call_depth += 1
75 try:
76 for oid, handler in self._idle_handlers:
77 self.remove_idle_handler_by_id(oid)
78 while gtk.events_pending():
79 gtk.main_iteration(False)
81 for index in xrange(len(self._idle_handlers)):
82 oid, handler = self._idle_handlers[index]
83 new_id = self._add_idle(handler)
84 self._idle_handlers[index] = (new_id, handler)
85 finally:
86 self._call_depth -= 1
88 def set_repeating_timer(self, timeout, function, data = None):
89 def f(*args):
90 if not self._timers.has_key(function):
91 return
92 try:
93 if len(args) > 0:
94 function(*args)
95 else:
96 function()
97 finally:
98 if data is not None:
99 gobject.timeout_add(timeout, f, data)
100 else:
101 gobject.timeout_add(timeout, f)
102 if data is not None:
103 gobject.timeout_add(timeout, f, data)
104 else:
105 gobject.timeout_add(timeout, f)
106 self._timers[function] = True
108 def end_repeating_timer(self, function):
109 if self._timers.has_key(function):
110 del self._timers[function]
112 def get_instance(klass):
113 if klass.instance is None:
114 klass.instance = klass()
115 return klass.instance
116 get_instance = classmethod(get_instance)