On 20/12/11 14:54, Manuel Quiñones wrote:
> 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.

Using webkit_web_view_go_back [1] is the right thing to do here. Epiphany is doing the same. Tbh, I am not sure I understand what WebKit.WebHistoryItem go_back() is doing, I was trying to dig into the Webkit code but stopped after a while of poking around...

I have a counter proposal for your patch (applies on top of your patch) which does the following things:

* 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

* I did rename a few bits s/session/history and the return value from self.get_back_forward_list() I use back_forward_list as variable

* I folded _get_history and _set_history into their appropriate 'mother' methods

* I tried to 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().

That should be pretty much it, let me know what you think,
   Simon


> Signed-off-by: Manuel Quiñones<ma...@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] http://webkitgtk.org/reference/webkitgtk-webkitwebview.html#webkit-web-view-go-back

diff --git a/browser.py b/browser.py
index b738591..1031944 100644
--- a/browser.py
+++ b/browser.py
@@ -287,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):
+    def set_history(self, tab_histories):
+        if tab_histories and isinstance(tab_histories[0], dict):
             # Old format, no tabs
-            tab_sessions = [tab_sessions]
+            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('''
@@ -419,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_session(self, data):
-        return sessionstore.set_session(self, data)
+    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_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[ \
@@ -462,12 +512,6 @@ class Browser(WebKit.WebView):
                     interfaces.nsIMarkupDocumentViewer)
             markupDocumentViewer.fullZoom -= _ZOOM_AMOUNT
 
-    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
 
_______________________________________________
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel

Reply via email to