[Sugar-devel] [PATCH] Store and restore session for each tab

Manuel Quiñones manuq at laptop.org
Tue Dec 20 08:54:26 EST 2011


For going to a specific item in the history, it uses go_back() method
of WebKit.WebView.  I tried also with WebKit.WebHistoryItem go_back(),
seemed the proper solution, but the page wouldn't update.

Signed-off-by: Manuel Quiñones <manuq at laptop.org>
---
 browser.py      |   12 ++++----
 sessionstore.py |   91 ++++++++++++++++++++++++++++++++-----------------------
 webactivity.py  |   17 ++++------
 3 files changed, 66 insertions(+), 54 deletions(-)

diff --git a/browser.py b/browser.py
index 2c3b4f8..69713ca 100644
--- a/browser.py
+++ b/browser.py
@@ -31,8 +31,9 @@ from sugar3.activity import activity
 from sugar3.graphics import style
 from sugar3.graphics.icon import Icon
 
+import sessionstore
+
 # FIXME
-# import sessionstore
 # from palettes import ContentInvoker
 # from sessionhistory import HistoryListener
 # from progresslistener import ProgressListener
@@ -291,7 +292,8 @@ class TabbedView(BrowserNotebook):
     def get_session(self):
         tab_sessions = []
         for index in xrange(0, self.get_n_pages()):
-            browser = self.get_nth_page(index)
+            scrolled_window = self.get_nth_page(index)
+            browser = scrolled_window.get_child()
             tab_sessions.append(sessionstore.get_session(browser))
         return tab_sessions
 
@@ -463,12 +465,10 @@ class Browser(WebKit.WebView):
             markupDocumentViewer.fullZoom -= _ZOOM_AMOUNT
 
     def get_history_index(self):
-        return self.web_navigation.sessionHistory.index
+        return sessionstore.get_history_index(self)
 
     def set_history_index(self, index):
-        if index == -1:
-            return
-        self.web_navigation.gotoIndex(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
index 73edb24..589b44d 100644
--- a/sessionstore.py
+++ b/sessionstore.py
@@ -14,61 +14,76 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
-# Based on
-# http://lxr.mozilla.org/seamonkey/source/browser/components/sessionstore
-
 import logging
 
-from xpcom import components
-from xpcom.components import interfaces
+from gi.repository import WebKit
 
 
 def get_session(browser):
-    session_history = browser.web_navigation.sessionHistory
-
-    if session_history.count == 0:
+    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):
-    _set_history(browser.web_navigation.sessionHistory, data)
-
-    if data:
-        browser.web_navigation.gotoIndex(len(data) - 1)
-    else:
-        browser.load_uri('about:blank')
+    session_history = browser.get_back_forward_list()
+    _set_history(session_history, data)
 
 
 def _get_history(history):
-    logging.debug('%r', history.count)
+    items_list = _items_history_as_list(history)
+    logging.debug('history count: %r', len(items_list))
     entries_dest = []
-    for i in range(0, history.count):
-        entry_orig = history.getEntryAtIndex(i, False)
-        entry_dest = {'url':    entry_orig.URI.spec,
-                      'title':  entry_orig.title}
-
+    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_internal = history.queryInterface(interfaces.nsISHistoryInternal)
-
-    if history_internal.count > 0:
-        history_internal.purgeHistory(history_internal.count)
-
-    for entry_dict in history_data:
-        logging.debug('entry_dict: %r', entry_dict)
-        entry_class = components.classes[ \
-                "@mozilla.org/browser/session-history-entry;1"]
-        entry = entry_class.createInstance(interfaces.nsISHEntry)
-
-        io_service_class = components.classes[ \
-                "@mozilla.org/network/io-service;1"]
-        io_service = io_service_class.getService(interfaces.nsIIOService)
-        entry.setURI(io_service.newURI(entry_dict['url'], None, None))
-        entry.setTitle(entry_dict['title'])
-
-        history_internal.addEntry(entry, True)
+    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 0e03fb2..9ccbe4c 100644
--- a/webactivity.py
+++ b/webactivity.py
@@ -401,7 +401,8 @@ class WebActivity(activity.Activity):
             logging.debug('########## reading %s', data)
             self._tabbed_view.set_session(self.model.data['history'])
             for number, tab in enumerate(self.model.data['currents']):
-                browser = self._tabbed_view.get_nth_page(number)
+                scrolled_window = self._tabbed_view.get_nth_page(number)
+                browser = scrolled_window.get_child()
                 browser.set_history_index(tab['history_index'])
 
             self._tabbed_view.set_current_page(self.model.data['current_tab'])
@@ -430,22 +431,18 @@ class WebActivity(activity.Activity):
                 else:
                     self.metadata['title'] = browser.props.title
 
-            # FIXME
-            # self.model.data['history'] = self._tabbed_view.get_session()
-            self.model.data['history'] = []
+            self.model.data['history'] = self._tabbed_view.get_session()
             current_tab = self._tabbed_view.get_current_page()
             self.model.data['current_tab'] = current_tab
 
             self.model.data['currents'] = []
             for n in range(0, self._tabbed_view.get_n_pages()):
-                # FIXME
-                continue
-                n_browser = self._tabbed_view.get_nth_page(n)
+                scrolled_window = self._tabbed_view.get_nth_page(n)
+                n_browser = scrolled_window.get_child()
                 if n_browser != None:
-                    nsiuri = n_browser.progress.location
-                    ui_uri = n_browser.get_url_from_nsiuri(nsiuri)
+                    uri = n_browser.get_uri()
                     history_index = n_browser.get_history_index()
-                    info = {'title': n_browser.props.title, 'url': ui_uri,
+                    info = {'title': n_browser.props.title, 'url': uri,
                             'history_index': history_index}
 
                     self.model.data['currents'].append(info)
-- 
1.7.7.3



More information about the Sugar-devel mailing list