3 Handles listing of feed items in a view (i.e. GTK+ TreeView)
5 __copyright__
= "Copyright (c) 2002-2005 Free Software Foundation, Inc."
6 __license__
= """GNU General Public License
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public
19 License along with this program; if not, write to the
20 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
24 from straw
import helpers
25 from xml
.sax
import saxutils
32 import os
.path
, operator
39 Constants for the item list treeview columns
41 TITLE
, STICKY
, ITEM
, WEIGHT
, STICKY_FLAG
, FOREGROUND
= range(6)
44 ''' A model of summary items for tree views '''
47 self
.store
= gtk
.ListStore(str, gobject
.TYPE_OBJECT
, gobject
.TYPE_PYOBJECT
, int, bool, str)
48 config
= Config
.get_instance()
49 self
.item_order
= config
.item_order
51 def populate(self
, items
):
52 #print "POPULATE ", len(summaryItems)
53 ## XXX should we sort here or in feeds.Feed?
54 #items = sorted(summaryItems, key=operator.attrgetter('title'),
55 # reverse=self.item_order)
62 weight
= (pango
.WEIGHT_BOLD
, pango
.WEIGHT_NORMAL
)[item
.is_read
]
63 #weight = pango.WEIGHT_NORMAL
64 #colour = ("#0000FF", "#000000")[item.is_read]
66 treeiter
= self
.store
.append()
67 self
.store
.set(treeiter
,
68 Column
.TITLE
, item
.title
,
70 Column
.WEIGHT
, weight
,
71 Column
.STICKY_FLAG
, False,
72 Column
.FOREGROUND
, colour
)
74 item
.connect("notify", self
.item_changed_cb
)
76 def item_changed_cb(self
, item
, property):
77 row
= [row
for row
in self
.store
if row
[Column
.ITEM
] is item
]
82 row
[0][Column
.WEIGHT
] = (pango
.WEIGHT_BOLD
, pango
.WEIGHT_NORMAL
)[item
.is_read
]
88 class ItemListView(MVP
.WidgetView
):
90 def _initialize(self
):
91 self
._widget
.set_rules_hint(False)
92 self
._widget
.connect("button_press_event",self
._on
_button
_press
_event
)
93 self
._widget
.connect("popup-menu", self
._on
_popup
_menu
)
94 self
._widget
.connect("row-activated", self
._on
_row
_activated
)
95 uifactory
= helpers
.UIFactory('ItemListActions')
96 action
= uifactory
.get_action('/itemlist_popup/mark_as_unread')
97 action
.connect('activate', self
.on_menu_mark_as_unread_activate
)
98 self
.popup
= uifactory
.get_popup('/itemlist_popup')
100 renderer
= gtk
.CellRendererToggle()
101 column
= gtk
.TreeViewColumn(_('Keep'), renderer
,
102 active
=Column
.STICKY_FLAG
)
103 column
.set_resizable(True)
104 column
.set_reorderable(True)
105 self
._widget
.append_column(column
)
106 renderer
.connect('toggled', self
._sticky
_toggled
)
108 renderer
= gtk
.CellRendererText()
109 column
= gtk
.TreeViewColumn(_('_Title'), renderer
,
111 foreground
=Column
.FOREGROUND
,
112 weight
=Column
.WEIGHT
)
113 column
.set_resizable(True)
114 column
.set_reorderable(True)
115 self
._widget
.append_column(column
)
117 selection
= self
._widget
.get_selection()
118 selection
.set_mode(gtk
.SELECTION_SINGLE
)
119 # selection.connect('changed', lambda x: self.item_mark_as_read(True))
121 def add_selection_changed_listener(self
, listener
):
122 selection
= self
._widget
.get_selection()
123 selection
.connect('changed',
124 listener
.itemlist_selection_changed
,
127 def _model_set(self
):
128 self
._widget
.set_model(self
._model
.model
)
130 def _sticky_toggled(self
, cell
, path
):
131 model
= self
._widget
.get_model()
132 treeiter
= model
.get_iter((int(path
),))
133 item
= model
.get_value(treeiter
, Column
.ITEM
)
134 item
.sticky
= not item
.sticky
135 model
.set(treeiter
, Column
.STICKY_FLAG
, item
.sticky
)
137 def _on_popup_menu(self
, treeview
, *args
):
138 self
.popup
.popup(None,None,None,0,0)
140 def _on_button_press_event(self
, treeview
, event
):
142 if event
.button
== 3:
145 time
= gtk
.get_current_event_time()
146 path
= treeview
.get_path_at_pos(x
, y
)
149 path
, col
, cellx
, celly
= path
150 treeview
.grab_focus()
151 treeview
.set_cursor( path
, col
, 0)
152 self
.popup
.popup(None, None, None, event
.button
, time
)
156 def on_menu_mark_as_unread_activate(self
, *args
):
157 self
.item_mark_as_read(False)
159 def item_mark_as_read(self
, isRead
):
160 selection
= self
._widget
.get_selection()
161 (model
, treeiter
) = selection
.get_selected()
162 if not treeiter
: return
163 item
= model
.get_value(treeiter
, Column
.ITEM
)
165 item
.props
.is_read
= isRead
168 weight
= pango
.WEIGHT_NORMAL
170 weight
= pango
.WEIGHT_BOLD
171 #colour = ("#0000FF", "#000000")[item.is_read]
173 model
.set(treeiter
, Column
.FOREGROUND
, colour
,
174 Column
.WEIGHT
, weight
,
178 def _on_row_activated(self
, treeview
, path
, column
):
179 ''' double-clicking an item opens that item in the web browser '''
180 model
= self
._widget
.get_model()
182 treeiter
= model
.get_iter(path
)
185 link
= model
.get_value(treeiter
, Column
.ITEM
).link
187 helpers
.url_show(link
)
190 def select_first_item(self
):
191 selection
= self
._widget
.get_selection()
192 (model
, treeiter
) = selection
.get_selected()
193 path
= model
.get_path(model
.get_iter_first())
194 selection
.select_path(path
)
197 def select_previous_item(self
):
199 Selects the item before the current selection. If there
200 is no current selection, selects the last item. If there is no
201 previous row, returns False.
203 selection
= self
._widget
.get_selection()
204 (model
, treeiter
) = selection
.get_selected()
205 if not treeiter
: return False
206 path
= model
.get_path(treeiter
)
210 selection
.select_path(prev
)
213 def select_next_item(self
):
215 Selects the item after the current selection. If there is no current
216 selection, selects the first item. If there is no next row, returns False.
218 selection
= self
._widget
.get_selection()
219 (model
, treeiter
) = selection
.get_selected()
221 treeiter
= model
.get_iter_first()
222 next_iter
= model
.iter_next(treeiter
)
223 if not next_iter
or not model
.iter_is_valid(treeiter
):
225 next_path
= model
.get_path(next_iter
)
226 selection
.select_path(next_path
)
229 def select_last_item(self
):
231 Selects the last item in this list.
233 selection
= self
._widget
.get_selection()
234 selection
.select_path(len(self
.model
.model
) - 1)
237 def select_next_unread_item(self
):
239 Selects the first unread item after the current selection. If there is
240 no current selection, or if there are no unread items, returns False..
242 By returning False, the caller can either go to the next feed, or go
243 to the next feed with an unread item.
245 selection
= self
._widget
.get_selection()
246 (model
, treeiter
) = selection
.get_selected()
247 # check if we have a selection. if none,
248 # start searching from the first item
250 treeiter
= model
.get_iter_first()
251 if not treeiter
: return False
252 nextiter
= model
.iter_next(treeiter
)
253 if not nextiter
: return False # no more rows to iterate
254 treerow
= model
[nextiter
]
257 item
= treerow
[Column
.ITEM
]
260 selection
.select_path(treerow
.path
)
262 treerow
= treerow
.next
265 class ItemListPresenter(MVP
.BasicPresenter
):
267 ## XXX listen for RefreshFeedDisplaySignal, FeedOrder changes,
269 def _initialize(self
):
270 self
.model
= ItemListModel()
272 def feedlist_selection_changed(self
, feedlist_selection
, column
):
273 (model
, pathlist
) = feedlist_selection
.get_selected_rows()
274 iters
= [model
.get_iter(path
) for path
in pathlist
]
279 nodes
= [model
.get_value(treeiter
, column
) for treeiter
in iters
]
283 if node
.node
.type == "F":
284 items
= ItemManager
.get_feed_items(node
.node
.id)
285 elif node
.node
.type == "C":
286 items
= ItemManager
.get_category_items(node
.node
.id)
288 self
.model
.populate(items
)
289 if not len(self
.model
.model
): return
290 if not self
.view
.select_next_unread_item():
291 self
.view
.select_first_item()
293 def flatten(self
, sequence
):
294 ''' python cookbook recipe 4.6 '''
295 for item
in sequence
:
296 if isinstance(item
, (list, tuple)):
297 for subitem
in self
.flatten(item
):
302 def select_previous_item(self
):
303 return self
.view
.select_previous_item()
305 def select_next_item(self
):
306 return self
.view
.select_next_item()
308 def select_next_unread_item(self
):
309 return self
.view
.select_next_unread_item()
311 def select_last_item(self
):
312 return self
.view
.select_last_item()