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

2012-01-03 Thread Simon Schampijer

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ñonesma...@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):
 -

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

2012-01-03 Thread Manuel Quiñones
2012/1/3 Simon Schampijer si...@schampijer.de:
 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...

This was a bit confusing for me, I thought webkit would have a better
way to iterate over the history of visited pages, but it doesn't.
Happy to hear Epiphany does the same.

 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 agree, there is no reason for a separate py file with that code.

 * 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

Looks much better now.  I was respecting the old code names without a reason.

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

I agree.

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

This is a much nicer way to set the history index!

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

Great improvement over my patch, Simon.  I only have to remove the
import of sessionstore in browse.py to test yours.

I will rebase your patch for current Browse code and resend.

Cheers,

-- 
.. manuq ..
___
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel


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

2011-12-20 Thread Manuel Quiñones
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 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 =