From 6b5701f10aff5a4013c1896bdf54aef2fe5bb393 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 6 Apr 2010 07:34:11 -0300 Subject: [PATCH] Add grid and iterator in the selected message, so it's easier to track those messages back; delete action uses the grid and iterator to remove the message from the grid when the operation completes successfully; the network layer will take care of the favorite status, which will be returned after the call (in case of error, an exception should be raised.) --- mitterlib/network/__init__.py | 17 ++++++++----- mitterlib/ui/helpers/gtk_messagegrid.py | 30 ++++++++++++++++++++++ mitterlib/ui/ui_pygtk.py | 33 +++++++++---------------- 3 files changed, 53 insertions(+), 27 deletions(-) diff --git a/mitterlib/network/__init__.py b/mitterlib/network/__init__.py index 6e6766a..2dc4d96 100644 --- a/mitterlib/network/__init__.py +++ b/mitterlib/network/__init__.py @@ -264,18 +264,23 @@ class Networks(object): return def favouite(self, message): - """Change the favorite status of the message.""" + """Change the favorite status of the message. Returns the new + favorite status.""" assert(isinstance(message, NetworkData)) self._proxy() self.networks[message.network].favorite(message) self._operations += 1 self._save() - return + + # since the operation completed without a problem, we can toggle the + # message as favorited already. + message.favorite = not message.favorite + return message.favorite def delete_message(self, message, network=None): - """Delete an update. Message can be a NetworkData object, in which - case network is not necessary; otherwise a network must be - provided.""" + """Delete an update. Message can be a :class:`NetworkData` object, + in which case network is not necessary; otherwise a network must be + provided. Always return the same message object""" if isinstance(message, NetworkData): network = message.network @@ -283,7 +288,7 @@ class Networks(object): self.networks[network].delete_message(message) self._operations += 1 self._save() - return + return message def message(self, message_id, network): """Return a single NetworkData object for a specified message.""" diff --git a/mitterlib/ui/helpers/gtk_messagegrid.py b/mitterlib/ui/helpers/gtk_messagegrid.py index aac86ce..61c4ddc 100644 --- a/mitterlib/ui/helpers/gtk_messagegrid.py +++ b/mitterlib/ui/helpers/gtk_messagegrid.py @@ -95,6 +95,11 @@ class MessageGrid(gtk.ScrolledWindow, gobject.GObject): return None data = model.get_value(iter, 0) + + # just so we can track this message again + data.grid = self + data.iter = iter + return data def __init__(self, avatar_cache): @@ -340,6 +345,20 @@ class MessageGrid(gtk.ScrolledWindow, gobject.GObject): iter = model.iter_next(iter) return + #----------------------------------------------------------------------- + # Class methods + #----------------------------------------------------------------------- + @staticmethod + def delete(self, message): + """Removes a message from the grid.""" + if not hasattr(message, 'grid'): + _log.debug('No grid information in the message') + return + + grid = message.grid + grid.remove(message) + return + #----------------------------------------------------------------------- # Public functions #----------------------------------------------------------------------- @@ -384,3 +403,14 @@ class MessageGrid(gtk.ScrolledWindow, gobject.GObject): """Make the grid to be redraw when possible.""" # we need this 'cause _grid is not exposed. self._grid.queue_draw() + return + + def remove(self, message): + """Remove a message from the list""" + if not hasattr(message, 'iter'): + _log.debug('No iterator information in the message') + return + + iter = message.iter + self._grid.get_model().remove(iter) + return \ No newline at end of file diff --git a/mitterlib/ui/ui_pygtk.py b/mitterlib/ui/ui_pygtk.py index ba89d52..def3250 100644 --- a/mitterlib/ui/ui_pygtk.py +++ b/mitterlib/ui/ui_pygtk.py @@ -494,7 +494,6 @@ class Interface(object): def _clear_text(self, widget): """Clear the text field.""" - self._delete_info = None self._clear_reply() self._update_field.hide() @@ -537,9 +536,10 @@ class Interface(object): def _delete_message(self, widget, user_data=None): """Delete a message.""" - (grid, counter) = self._grids[self._main_tabs.get_current_page()] - (model, iter) = grid.get_selection().get_selected() - message = model.get_value(iter, 0) + page = self._main_tabs.get_current_page() + message = self._main_tabs.get_nth_page(page).selected + if not message: + return confirm = gtk.MessageDialog(parent=self._main_window, type=gtk.MESSAGE_QUESTION, @@ -554,7 +554,6 @@ class Interface(object): return False self._update_statusbar(_('Deleting message...')) - self._delete_info = (grid, iter) _log.debug('Deleting messing %d', message.id) self._threads.add_work(self._post_delete_message, self._exception_delete_message, @@ -857,13 +856,11 @@ class Interface(object): return ### Results for the delete message call - def _post_delete_message(self, widget, data): + def _post_delete_message(self, widget, message): """Called when the message is deleted successfully.""" _log.debug('Message deleted.') - if self._delete_info: - (grid, iter) = self._delete_info - grid.get_model().remove(iter) - self._delete_info = None + # any grid can take care of deleting the message + MessageGrid.delete(message) self._update_statusbar(_('Message deleted.')) return @@ -895,18 +892,14 @@ class Interface(object): return ### Results from the favorite call - def _post_favorite_message(self, widget, data): + def _post_favorite_message(self, widget, status): """Called when the message was favorited successfully.""" _log.debug('Favorite status changed.') - (grid, iter) = self._favorite_info - message = grid.get_model().get_value(iter, 0) - if message.favorite: - display = _('Message unfavorited.') - else: + if status: display = _('Message favorited.') + else: + display = _('Message unfavorited.') self._update_statusbar(display) - message.favorite = not message.favorite - self._favorite_info = None return def _exception_favorite_message(self, widget, exception): @@ -957,9 +950,7 @@ class Interface(object): # This is the ugly bit for speeding up things and making # interthread communication. - self._delete_info = None self._reply_message_id = None - self._favorite_info = None return @@ -1103,4 +1094,4 @@ class Interface(object): 'protected/private.', metavar='CHAR', default=_('(protected)'), - is_cmd_option=False) + is_cmd_option=False) \ No newline at end of file