~ubuntu-branches/ubuntu/karmic/sugar-web-activity/karmic

« back to all changes in this revision

Viewing changes to Web.activity/sessionstore.py

  • Committer: Bazaar Package Importer
  • Author(s): Jani Monoses
  • Date: 2008-01-31 15:17:08 UTC
  • Revision ID: james.westby@ubuntu.com-20080131151708-4tegqfae3vdhiylk
Tags: upstream-85
ImportĀ upstreamĀ versionĀ 85

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007, One Laptop Per Child
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
16
 
 
17
# Based on
 
18
# http://lxr.mozilla.org/seamonkey/source/browser/components/sessionstore
 
19
 
 
20
import logging
 
21
 
 
22
from xpcom import components
 
23
from xpcom.components import interfaces
 
24
 
 
25
def get_session(browser):
 
26
    session_history = browser.web_navigation.sessionHistory
 
27
        
 
28
    if session_history.count == 0:
 
29
        return ''        
 
30
    return _get_history(session_history)            
 
31
    
 
32
def set_session(browser, data):
 
33
    _set_history(browser.web_navigation.sessionHistory, data)
 
34
        
 
35
    if data:
 
36
        browser.web_navigation.gotoIndex(len(data) - 1)
 
37
    else:
 
38
        browser.load_uri('about:blank')
 
39
 
 
40
def _get_history(history):
 
41
    logging.debug('%r' % history.count)
 
42
    entries_dest = []
 
43
    for i in range(0, history.count):
 
44
        entry_orig = history.getEntryAtIndex(i, False)
 
45
        entry_dest = {'url':    entry_orig.URI.spec,
 
46
                      'title':  entry_orig.title}
 
47
 
 
48
        entries_dest.append(entry_dest)
 
49
 
 
50
    return entries_dest
 
51
 
 
52
def _set_history(history, history_data):
 
53
    history_internal = history.queryInterface(interfaces.nsISHistoryInternal);
 
54
    
 
55
    if history_internal.count > 0:
 
56
        history_internal.purgeHistory(history_internal.count);
 
57
 
 
58
    for entry_dict in history_data:
 
59
        logging.debug('entry_dict: %r' % entry_dict)
 
60
        entry_class = components.classes["@mozilla.org/browser/session-history-entry;1"]
 
61
        entry = entry_class.createInstance(interfaces.nsISHEntry)
 
62
                  
 
63
        io_service_class = components.classes["@mozilla.org/network/io-service;1"]
 
64
        io_service = io_service_class.getService(interfaces.nsIIOService)
 
65
        entry.setURI(io_service.newURI(entry_dict['url'], None, None));
 
66
        entry.setTitle(entry_dict['title']);
 
67
 
 
68
        history_internal.addEntry(entry, True)
 
69