Added debug logging around document view creation.
[straw.git] / straw / categoryproperties.py
blob3f920c8c50b27ec1d136cee943fdb45aa5ad052d
1 """
2 Provides a module for handling the properties of a Category.
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. """
19 from error import log, logparam, logtb
20 from gtk.glade import XML
21 from straw import helpers
22 from xml.sax import saxutils
23 import Config
24 import MVP
25 import SummaryParser
26 import gettext
27 import gobject
28 import gtk
29 import os, os.path
30 import straw.defs
31 import time
33 class FeedPropertyView(MVP.GladeView):
35 COLUMN_NAME = 0
36 COLUMN_MEMBER = 1
37 COLUMN_OBJECT = 2
39 def _initialize(self):
40 self._category = None
41 self._defaults = {'title':'','url':'','username':'','password':''}
42 self._window = self._widget.get_widget('feed_properties')
44 self._title_entry = self._widget.get_widget('title_entry')
45 self._location_entry = self._widget.get_widget('location_entry')
46 self._website_entry = self._widget.get_widget('website_entry')
47 self._description_label = self._widget.get_widget('description_label')
48 self._copyright_label = self._widget.get_widget('copyright_label')
50 self._username = self._widget.get_widget('properties_username_entry')
51 self._password = self._widget.get_widget('properties_password_entry')
53 self._next_refresh_label = self._widget.get_widget('properties_next_refresh_label')
54 self._previous_refresh_label = self._widget.get_widget('properties_previous_refresh_label')
56 self._restore_button = self._widget.get_widget('properties_reset_button')
57 self._refresh_spin = self._widget.get_widget('properties_refresh_spin')
58 self._articles_spin = self._widget.get_widget('properties_articles_spin')
59 self._refresh_default_check = self._widget.get_widget('properties_keep_refresh_default')
60 self._articles_default_check = self._widget.get_widget('properties_keep_articles_default')
62 def set_feed(self, feed):
63 self._category = feed
65 def show(self):
66 self._initializing_window = True
67 self.display_properties()
68 self._window.present()
69 self._initializing_window = False
71 def hide(self, *args):
72 self._window.hide()
73 self._window.destroy()
75 def _on_feed_properties_delete_event(self, *args):
76 self.hide()
77 return True
79 def _on_properties_close_button_clicked(self, *args):
80 self.hide()
81 return
83 def _on_location_button_clicked(self, *args):
84 helpers.url_show(self._location_entry.get_text())
86 def _on_website_button_clicked(self, *args):
87 helpers.url_show(self._website_entry.get_text())
89 def display_properties(self):
90 self._window.set_title(_("%s Properties") % self._category.name)
92 self._title_entry.set_text(self._category.name)
94 def _load_uri(self, widget, event):
95 helpers.url_show(widget.get_uri())
97 def restore_defaults(self):
98 # FIXME: add frequency and number of items and the default flags
99 self._category.name = self._defaults['title']
100 self._category.access_info = (
101 self._defaults['url'], self._defaults['username'], self._defaults['password'])
102 self._title.set_text(self._category.name)
103 self._location.set_text(self._category.location)
104 self._username.set_text(self._defaults.get('username',''))
105 self._password.set_text(self._defaults.get('password',''))
106 self._refresh_default_check.set_active(self._refresh_keep)
107 if not self._refresh_keep:
108 self._refresh_spin.set_value(float(self._refresh_default))
109 self._articles_default_check.set_active(self._articles_keep)
110 if not self._articles_keep:
111 self._articles_spin.set_value(float(self._articles_default))
112 self._restore_button.set_sensitive(False)
114 def _on_close_button_clicked(self, *args):
115 self.hide()
117 def _on_properties_title_entry_insert_text(self, *args):
118 if self._initializing_window:
119 return
120 self._restore_button.set_sensitive(True)
122 def _on_properties_title_entry_delete_text(self, *args):
123 if self._initializing_window:
124 return
125 self._restore_button.set_sensitive(True)
127 def _on_properties_title_entry_focus_out_event(self, widget, *args):
128 self._category.name = widget.get_text().strip()
130 def _on_properties_location_entry_insert_text(self, *args):
131 if self._initializing_window:
132 return
133 self._restore_button.set_sensitive(True)
135 def _on_properties_location_entry_delete_text(self, *args):
136 if self._initializing_window:
137 return
138 self._restore_button.set_sensitive(True)
140 def _on_properties_location_entry_focus_out_event(self, widget, *args):
141 loc, username, pw = self._category.access_info
142 self._category.access_info = (widget.get_text().strip(), username, pw)
144 def _on_properties_username_entry_insert_text(self, *args):
145 if self._initializing_window:
146 return
147 self._restore_button.set_sensitive(True)
149 def _on_properties_username_entry_delete_text(self, *args):
150 if self._initializing_window:
151 return
152 self._restore_button.set_sensitive(False)
154 def _on_properties_username_entry_focus_out_event(self, widget, *args):
155 self._presenter.set_username(widget.get_text().strip())
157 def _on_properties_password_entry_insert_text(self, *args):
158 if self._initializing_window:
159 return
160 self._restore_button.set_sensitive(True)
162 def _on_properties_password_entry_delete_text(self, *args):
163 if self._initializing_window:
164 return
165 self._restore_button.set_sensitive(False)
167 def _on_properties_password_entry_focus_out_event(self, widget, *args):
168 self._presenter.set_password(widget.get_text().strip())
170 def _on_properties_reset_button_clicked(self, *args):
171 self.restore_defaults()
173 def _on_properties_refresh_spin_focus_out_event(self, widget, *args):
174 widget.update()
175 value = widget.get_value_as_int()
176 self._presenter.set_poll_frequency(value)
178 def _on_properties_articles_spin_focus_out_event(self, widget, *args):
179 widget.update()
180 value = widget.get_value_as_int()
181 self._presenter.set_items_stored(value)
183 def _on_properties_articles_spin_value_changed(self, widget, *args):
184 if self._initializing_window:
185 return
186 self._restore_button.set_sensitive(True)
188 def _on_properties_refresh_spin_value_changed(self, widget, *args):
189 if self._initializing_window:
190 return
191 self._restore_button.set_sensitive(True)
193 def _on_properties_keep_refresh_default_toggled(self, widget, *args):
194 if self._initializing_window:
195 return
196 isactive = widget.get_active()
197 if isactive:
198 self._presenter.set_poll_frequency(feeds.Feed.DEFAULT)
199 self._refresh_spin.set_value(float(Config.get_instance().poll_frequency / 60))
200 else:
201 self._presenter.set_poll_frequency(self._refresh_spin.get_value_as_int() * 60)
203 self._refresh_spin.set_sensitive(not isactive)
204 self._restore_button.set_sensitive(True)
206 def _on_properties_keep_articles_default_toggled(self, widget, *args):
207 if self._initializing_window:
208 return
209 isactive = widget.get_active()
210 if isactive:
211 self._presenter.set_items_stored(feeds.Feed.DEFAULT)
212 self._articles_spin.set_value(float(Config.get_instance().number_of_items_stored))
213 else:
214 self._presenter.set_items_stored(self._articles_spin.get_value_as_int())
215 self._articles_spin.set_sensitive(not isactive)
216 self._restore_button.set_sensitive(True)
218 class FeedPropertyPresenter(MVP.BasicPresenter):
220 TIME_INTERVAL = 60
222 def _initialize(self):
223 self._category = None
225 def set_feed(self, feed):
226 self._category = feed
227 self._view.set_feed(self._category)
229 def set_username(self, username):
230 loc, uname, pw = self._category.access_info
231 self._category.access_info = (loc, username, pw)
233 def set_password(self, password):
234 loc, uname, pw = self._category.access_info
235 self._category.access_info = (loc, uname, password)
237 def set_poll_frequency(self, pf):
238 if pf != self._category.poll_frequency:
239 self._category.poll_frequency = pf * FeedPropertyPresenter.TIME_INTERVAL
241 def set_items_stored(self, nitems):
242 if nitems != self._category.number_of_items_stored:
243 self._category.number_of_items_stored = nitems
245 def show(feed):
246 gladefile = XML(os.path.join(straw.defs.STRAW_DATA_DIR, "node-properties.glade"))
247 dialog = FeedPropertyPresenter(view=FeedPropertyView(gladefile))
248 dialog.set_feed(feed)
250 dialog.view.show()
252 def read_text(fragment, chars):
253 """Read chars cdata characters from html fragment fragment"""
254 parser = SummaryParser.TitleImgParser()
255 parser.feed(fragment)
256 text = parser.get_text(chars)
257 parser.close()
258 return text