From e23ae0747e4648c3f15a11b49c4aaf196dd3423a Mon Sep 17 00:00:00 2001 From: Manuel QuiƱones Date: Wed, 04 Jan 2012 02:45:36 +0000 Subject: Improve session store and restore This is the review from Simon Schampijer of commit 14102bcd65f015dcbf12d1406df8ef7ffb64d13d . * the sessionstore code is moved into Browser, we do have set_history_index/get_history_index already there, the TabbedView does use that path for their requests about the history * Rename a few bits s/session/history and the return value from self.get_back_forward_list() Use back_forward_list as variable * Fold _get_history and _set_history into their appropriate 'mother' methods * Make set_history_index a bit cleaner, the API provided by webkitgtk does not seem to cleanly allow our usage like: get_current_item_index and go_to_back_forward_item(index) also the API does seem to miss a back_forward_list.get_length() (see the code we have to do in _items_history_as_list) there only exist a back_forward_list.get_back_length() and back_forward_list.get_forward_length(). Signed-off-by: Manuel QuiƱones --- diff --git a/browser.py b/browser.py index 5d5dc98..f6efe26 100644 --- a/browser.py +++ b/browser.py @@ -31,8 +31,6 @@ from sugar3.activity import activity from sugar3.graphics import style from sugar3.graphics.icon import Icon -import sessionstore - # FIXME # from palettes import ContentInvoker # from sessionhistory import HistoryListener @@ -289,27 +287,27 @@ class TabbedView(BrowserNotebook): current_browser = GObject.property(type=object, getter=_get_current_browser) - def get_session(self): - tab_sessions = [] + def get_history(self): + tab_histories = [] for index in xrange(0, self.get_n_pages()): scrolled_window = self.get_nth_page(index) browser = scrolled_window.get_child() - tab_sessions.append(sessionstore.get_session(browser)) - return tab_sessions + tab_histories.append(browser.get_history()) + return tab_histories - def set_session(self, tab_sessions): - if tab_sessions and isinstance(tab_sessions[0], dict): - # Old format, no tabs - tab_sessions = [tab_sessions] + def set_history(self, tab_histories): + if tab_histories and isinstance(tab_histories[0], dict): + # Old format, no tabs + tab_histories = [tab_histories] while self.get_n_pages(): self.remove_page(self.get_n_pages() - 1) - for tab_session in tab_sessions: + for tab_history in tab_histories: browser = Browser() browser.connect('new-tab', self.__new_tab_cb) self._append_tab(browser) - sessionstore.set_session(browser, tab_session) + browser.set_history(tab_history) Gtk.rc_parse_string(''' @@ -421,11 +419,61 @@ class Browser(WebKit.WebView): texttosuburi = cls.getService(interfaces.nsITextToSubURI) return texttosuburi.unEscapeURIForUI(uri.originCharset, uri.spec) - def get_session(self): - return sessionstore.get_session(self) + def get_history(self): + """Return the browsing history of this browser.""" + back_forward_list = self.get_back_forward_list() + if back_forward_list.get_back_length() == 0: + return '' + + items_list = self._items_history_as_list(back_forward_list) + history = [] + for item in items_list: + history.append({'url': item.get_uri(), + 'title': item.get_title()}) + + return history + + def set_history(self, history): + """Restore the browsing history for this browser.""" + back_forward_list = self.get_back_forward_list() + back_forward_list.clear() + for entry in history: + uri, title = entry['url'], entry['title'] + history_item = WebKit.WebHistoryItem.new_with_data(uri, title) + back_forward_list.add_item(history_item) + + def get_history_index(self): + """Return the index of the current item in the history.""" + back_forward_list = self.get_back_forward_list() + history_list = self._items_history_as_list(back_forward_list) + current_item = back_forward_list.get_current_item() + return history_list.index(current_item) - def set_session(self, data): - return sessionstore.set_session(self, data) + def set_history_index(self, index): + """Go to the item in the history specified by the index.""" + back_forward_list = self.get_back_forward_list() + if back_forward_list.get_back_length() != 0: + current_item = index - back_forward_list.get_back_length() + item = back_forward_list.get_nth_item(current_item) + if item is not None: + self.go_to_back_forward_item(item) + + def _items_history_as_list(self, history): + """Return a list with the items of a WebKit.WebBackForwardList.""" + back_items = [] + for n in reversed(range(1, history.get_back_length() + 1)): + item = history.get_nth_item(n * -1) + back_items.append(item) + + current_item = [history.get_current_item()] + + forward_items = [] + for n in range(1, history.get_forward_length() + 1): + item = history.get_nth_item(n) + forward_items.append(item) + + all_items = back_items + current_item + forward_items + return all_items def get_source(self, async_cb, async_err_cb): cls = components.classes[ \ @@ -448,12 +496,6 @@ class Browser(WebKit.WebView): uri = self.web_navigation.currentURI persist.saveURI(uri, self.doc_shell, None, None, None, local_file) - def get_history_index(self): - return sessionstore.get_history_index(self) - - def set_history_index(self, index): - return sessionstore.set_history_index(self, index) - def open_new_tab(self, url): self.emit('new-tab', url) diff --git a/sessionstore.py b/sessionstore.py deleted file mode 100644 index 589b44d..0000000 --- a/sessionstore.py +++ /dev/null @@ -1,89 +0,0 @@ -# Copyright (C) 2007, One Laptop Per Child -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - -import logging - -from gi.repository import WebKit - - -def get_session(browser): - session_history = browser.get_back_forward_list() - if session_history.get_back_length() == 0: - return '' - return _get_history(session_history) - - -def set_session(browser, data): - session_history = browser.get_back_forward_list() - _set_history(session_history, data) - - -def _get_history(history): - items_list = _items_history_as_list(history) - logging.debug('history count: %r', len(items_list)) - entries_dest = [] - for item in items_list: - entry_dest = {'url': item.get_uri(), - 'title': item.get_title()} - entries_dest.append(entry_dest) - - return entries_dest - - -def _set_history(history, history_data): - history.clear() - for entry in history_data: - uri, title = entry['url'], entry['title'] - history_item = WebKit.WebHistoryItem.new_with_data(uri, title) - history.add_item(history_item) - - -def get_history_index(browser): - """Return the index of the current item in the history.""" - history = browser.get_back_forward_list() - history_list = _items_history_as_list(history) - current_item = history.get_current_item() - return history_list.index(current_item) - - -def set_history_index(browser, index): - """Go to the item in the history specified by the index.""" - history = browser.get_back_forward_list() - history_list = _items_history_as_list(history) - last_index = len(history_list) - 1 - for i in range(last_index - index): - browser.go_back() - if index == last_index: - browser.go_back() - browser.go_forward() - - -def _items_history_as_list(history): - """Return a list with the items of a WebKit.WebBackForwardList.""" - back_items = [] - for n in reversed(range(1, history.get_back_length() + 1)): - item = history.get_nth_item(n * -1) - back_items.append(item) - - current_item = [history.get_current_item()] - - forward_items = [] - for n in range(1, history.get_forward_length() + 1): - item = history.get_nth_item(n) - forward_items.append(item) - - all_items = back_items + current_item + forward_items - return all_items diff --git a/webactivity.py b/webactivity.py index 9ccbe4c..48eb780 100644 --- a/webactivity.py +++ b/webactivity.py @@ -399,7 +399,7 @@ class WebActivity(activity.Activity): link['color'], link['title'], link['owner'], -1, link['hash']) logging.debug('########## reading %s', data) - self._tabbed_view.set_session(self.model.data['history']) + self._tabbed_view.set_history(self.model.data['history']) for number, tab in enumerate(self.model.data['currents']): scrolled_window = self._tabbed_view.get_nth_page(number) browser = scrolled_window.get_child() @@ -431,7 +431,7 @@ class WebActivity(activity.Activity): else: self.metadata['title'] = browser.props.title - self.model.data['history'] = self._tabbed_view.get_session() + self.model.data['history'] = self._tabbed_view.get_history() current_tab = self._tabbed_view.get_current_page() self.model.data['current_tab'] = current_tab -- cgit v0.9.1