Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sessionstore.py
blob: 8956a04719e38b9123c2484fbc232a8da41d6a73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

# Based on
# http://lxr.mozilla.org/seamonkey/source/browser/components/sessionstore

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)

    if data:
        # FIXME: this is adding the latest page twice:
        browser.load_uri(session_history.get_current_item().get_uri())
    else:
        browser.load_uri('about:blank')


def _get_history(history):
    entries_dest = []
    for i in reversed(range(history.get_back_length())):
        entry_orig = history.get_nth_item(i * -1)
        entry_dest = {'url': entry_orig.get_uri(),
                      'title': entry_orig.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)