2007-08-09 Inaki Larranaga Murgoitio
[straw.git] / straw / dialogs.py
blob5072a466c8b55b98f50f6f15b669fd05adae2823
1 """ dialogs.py
3 Module for displaying dialogs related to errors, warnings, notifications,
4 etc...
5 """
6 __copyright__ = "Copyright (c) 2002-2005 Free Software Foundation, Inc."
7 __license__ = """
8 Straw is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
13 Straw is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA. """
21 import os, os.path
22 from gettext import gettext as _
23 import pygtk
24 pygtk.require('2.0')
25 import gobject
26 import gtk
27 import gnome
29 from straw import constants
30 import utils
31 from straw import OPMLExport
32 from straw import OPMLImport
33 from straw import error
35 class UIFactory(object):
36 _shared_state = {}
37 manager = None
39 actions = [
40 ("refresh", gtk.STOCK_REFRESH, _("_Refresh"), None, _("Update this feed"), None),
41 ("mark_as_read", gtk.STOCK_CLEAR, _("_Mark As Read"),
42 None, _("Mark all items in this feed as read"), None),
43 ("stop_refresh", None, _("_Stop Refresh"), None, _("Stop updating this feed"), None),
44 ("remove", None, _("Remo_ve Feed"), None, _("Remove this feed from my subscription"), None),
45 ("properties", gtk.STOCK_INFO, _("_Information"), None, _("Feed-specific properties"), None),
46 ("text_copy", gtk.STOCK_COPY, "_Copy", None, None, None),
47 ("link_copy", None, "_Copy Link Location", None, None, None),
48 ("link_open", None, "_Open Link", None, None, None),
49 ("subscribe", None, "_Subscribe", None, None, None),
50 ("zoom_in", gtk.STOCK_ZOOM_IN, "Zoom _In", None, None, None),
51 ("zoom_out", gtk.STOCK_ZOOM_OUT, "Zoom _Out", None, None, None),
52 ("zoom_100", gtk.STOCK_ZOOM_100, "_Normal Size", None, None, None),
53 ("mark_as_unread", gtk.STOCK_CONVERT, "Mark as _Unread", None, "Mark this item as unread", None)
56 def __new__(cls, *a, **k):
57 obj = object.__new__(cls, *a, **k)
58 obj.__dict__ = cls._shared_state
59 return obj
61 def __init__(self, name):
62 if not self.manager:
63 print 'initializing ui manager'
64 self.action_groups = {}
65 self.manager = gtk.UIManager()
66 action_group = gtk.ActionGroup(name)
67 action_group.add_actions(UIFactory.actions)
68 num_action_groups = len(self.manager.get_action_groups())
69 self.action_groups[name] = num_action_groups
70 self.manager.insert_action_group(action_group, num_action_groups)
71 ui = os.path.join(utils.find_data_dir(), 'ui.xml')
72 self.manager.add_ui_from_file(ui)
74 def get_popup(self, path):
75 return self.manager.get_widget(path)
77 def get_action(self, path):
78 return self.manager.get_action(path)
80 def ensure_update(self):
81 self.manager.ensure_update()
83 def _setup_filechooser_dialog(title, action, extra_widget_title):
84 """
85 Setup the file chooser dialog. This includes an extra widget (a combobox)
86 to include the categories to import or export
87 """
88 dialog = gtk.FileChooserDialog(title, action=action,
89 buttons=(gtk.STOCK_CANCEL,
90 gtk.RESPONSE_CANCEL,
91 gtk.STOCK_OK, gtk.RESPONSE_OK))
92 category_list = feeds.category_list
93 model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
94 combobox = gtk.ComboBox(model)
95 celltitle = gtk.CellRendererText()
96 combobox.pack_start(celltitle,False)
97 combobox.add_attribute(celltitle, 'text', 0)
99 # add 'All' Category
100 it = model.append()
101 model.set(it, 0, category_list.all_category.title,
102 1, category_list.all_category)
104 # add user categories
105 for category in category_list.user_categories:
106 it = model.append()
107 model.set(it, 0, category.title, 1, category)
109 # ..
110 combobox.set_active(0)
111 label = gtk.Label(extra_widget_title)
112 label.set_alignment(1.0,0.5)
113 hbox = gtk.HBox(spacing=6)
114 hbox.pack_start(label,True,True,0)
115 hbox.pack_end(combobox,False,False,0)
116 hbox.show_all()
118 dialog.set_extra_widget(hbox)
119 return (dialog, combobox)
122 def export_subscriptions(parent):
123 (dialog,combobox) = _setup_filechooser_dialog(_("Export Subscriptions"),
124 gtk.FILE_CHOOSER_ACTION_SAVE,
125 _("Select category to export:"))
126 def selection_changed(widget, dialog):
127 model = widget.get_model()
128 category = model[widget.get_active()][1]
129 dialog.set_current_name("Straw-%s.xml" % category.title)
131 combobox.connect('changed',
132 selection_changed,
133 dialog)
134 selection_changed(combobox, dialog)
135 dialog.set_transient_for(parent)
136 response = dialog.run()
137 if response == gtk.RESPONSE_OK:
138 filename = dialog.get_filename()
139 model = combobox.get_model()
140 cat = model[combobox.get_active()][1]
141 OPMLExport.export(cat.title, cat.feeds, filename)
142 dialog.destroy()
144 def import_subscriptions(parent):
145 (dialog,combobox) = _setup_filechooser_dialog(_("Import Subscriptions"),
146 gtk.FILE_CHOOSER_ACTION_OPEN,
147 _("Add new subscriptions in:"))
148 ffilter = gtk.FileFilter()
149 ffilter.set_name(_("OPML Files Only"))
150 ffilter.add_pattern("*.xml")
151 ffilter.add_pattern("*.opml")
152 dialog.add_filter(ffilter)
153 dialog.set_transient_for(parent)
154 response = dialog.run()
155 if response == gtk.RESPONSE_OK:
156 filename = dialog.get_filename()
157 model = combobox.get_model()
158 cat = model[combobox.get_active()][1]
159 dialog.hide()
160 OPMLImport.import_opml(filename, cat)
161 dialog.destroy()
163 def report_error(primary, secondary, parent=None):
164 dialog = gtk.MessageDialog(parent,
165 type=gtk.MESSAGE_ERROR,
166 buttons=gtk.BUTTONS_OK,
167 message_format=primary)
168 dialog.format_secondary_text(secondary)
169 response = dialog.run()
170 dialog.destroy()
171 return response
173 def report_offline_status(parent=None):
174 dialog = gtk.MessageDialog(parent,
175 type=gtk.MESSAGE_WARNING,
176 buttons=gtk.BUTTONS_OK_CANCEL,
177 message_format="You Are Offline!")
178 dialog.format_secondary_markup(_("You are currently reading offline. Would you like to go online now?"))
179 response = dialog.run()
180 dialog.destroy()
181 return response
183 def confirm_delete(primary, secondary,parent=None):
184 dialog = gtk.MessageDialog(parent,
185 type=gtk.MESSAGE_QUESTION,
186 buttons=gtk.BUTTONS_OK_CANCEL,
187 message_format=primary)
188 dialog.format_secondary_text(secondary)
189 response = dialog.run()
190 dialog.hide()
191 return response
193 def credits():
194 people = u'''Author:
195 Juri Pakaste <juri@iki.fi>
197 Contributors:
198 Jan Alonzo <jmalonzo@unpluggable.com>
199 Ryan P. Skadberg <skadz@stigmata.org>
200 Leandro Lameiro <lameiro@gmail.com>
201 Tuukka Hastrup <tuukka@iki.fi>
203 Past Contributors:
204 Iain McCoy <iain@mccoy.id.au>
205 Lucas Nussbaum <lucas@lucas-nussbaum.net>
206 Olivier Crete <tester@tester.ca>
207 Scott Douglas-Watson <sdouglaswatson@yahoo.co.uk>
208 Terje R\xf8sten (distutils)
210 Special Thanks:
211 Mark Pilgrim (feedparser and feedfinder)
212 Joe Gregorio (httplib2)
214 iconfile = os.path.join(utils.find_image_dir(),"straw.png")
215 logo = gtk.gdk.pixbuf_new_from_file(iconfile)
216 description = _("A Desktop Feed Reader")
217 straw_copyright = u"""
218 Copyright \xa9 2005-2007 Straw Contributors
219 Copyright \xa9 2002-2004 Juri Pakaste"""
220 artists = [
221 u'Jakub \'jimmac\' Steiner',
222 u'Juri Pakaste'
225 translators = [
226 u"GNOME i18n Project and Translators<http://developer.gnome.org/projects/gtp/>",
227 u"Juri Pakaste <juri@iki.fi>",
228 u"Martin Steldinger <tribble@hanfplantage.de>",
229 u"David Rousseau <boiteaflood@wanadoo.fr>",
230 u"Sergei Vavinov <svv@cmc.msu.ru>",
231 u"Terje R\xf8sten <terjeros@phys.ntnu.no>",
232 u"Francisco J. Fernandez <franciscojavier.fernandez.serrador@hispalinux.es>",
233 u"Elros Cyriatan (Dutch Translation)"
236 translator_credits = 'translator_credits'
237 about = gtk.AboutDialog()
238 about.set_name(constants.APPNAME)
239 about.set_version(constants.VERSION)
240 about.set_copyright(straw_copyright)
241 about.set_comments(description)
242 about.set_authors(people.splitlines())
243 about.set_artists(artists)
244 about.set_logo(logo)
245 about.set_translator_credits(translator_credits)
246 about.set_license(__license__)
247 gtk.about_dialog_set_url_hook(lambda about, url: utils.url_show(url))
248 about.set_website(constants.STRAW_URL)
249 about.set_website_label(constants.STRAW_URL)
250 about.connect('response', lambda widget, response: widget.destroy())
251 return about