source: epiphany-session-manager/session-manager.py @ 46

Last change on this file since 46 was 46, checked in by simon, 17 years ago
  • First hack at something to save/restore browser sessions.
  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-python
File size: 3.4 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Session save/restore facility for Epiphany
5#
6# Copyright © 2007 Simon E. Ward <simon@bleah.co.uk>
7#
8# This program is free software; you can redistribute it and/or
9# modify it under the terms of version 2 of the GNU General Public
10# License as published by the Free Software Foundation.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15# General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20# 02110-1301, USA
21
22import epiphany
23import gtk
24import os.path
25
26_ui_str = """
27<ui>
28        <menubar name="menubar">
29                <menu name="ToolsMenu" action="Tools">
30                        <separator/>
31                        <menuitem name="SaveSession" action="SaveSession"/>
32                        <menuitem name="LoadSession" action="LoadSession"/>
33                        <separator/>
34                </menu>
35        </menubar>
36</ui>
37"""
38
39def _warn_on_overwrite(parent, filename):
40        dirname, basename = os.path.split(filename)
41        msg = 'A file named "%s" already exists in the directory "%s".'\
42              'Do you want to overwrite it?' % (basename, dirname)
43        dialog = gtk.MessageDialog(
44                        parent=parent,
45                        flags=gtk.DIALOG_MODAL
46                             |gtk.DIALOG_DESTROY_WITH_PARENT,
47                        type=gtk.MESSAGE_WARNING,
48                        buttons=gtk.BUTTONS_YES_NO,
49                        message_format=msg)
50        overwrite = dialog.run()
51        dialog.destroy()
52        return overwrite
53
54def _show_dialog(title, action):
55        if action == gtk.FILE_CHOOSER_ACTION_SAVE:
56                ok_button = gtk.STOCK_SAVE
57        else:
58                ok_button = gtk.STOCK_OPEN
59
60        dialog = gtk.FileChooserDialog(
61                        title=title,
62                        action=action,
63                        buttons=(
64                                gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
65                                ok_button, gtk.RESPONSE_OK
66                                )
67                        )
68        dialog.set_default_response(gtk.RESPONSE_OK)
69
70        response = dialog.run()
71        if response == gtk.RESPONSE_OK:
72                filename = dialog.get_filename()
73
74                if action == gtk.FILE_CHOOSER_ACTION_SAVE:
75                        while os.path.exists(filename):
76                                if _warn_on_overwrite(dialog, filename) \
77                                        == gtk.RESPONSE_YES:
78                                        break
79                                response = dialog.run()
80                                if response != gtk.RESPONSE_OK:
81                                        filename = None
82                                        break
83                                filename = dialog.get_filename()
84        else:
85                filename = None
86
87        dialog.destroy()
88        return filename
89
90def _get_session():
91        shell = epiphany.ephy_embed_shell_get_default()
92        return shell.get_session()
93
94def _save_session(action, session):
95        filename = _show_dialog('Save Session',
96                                gtk.FILE_CHOOSER_ACTION_SAVE)
97        if filename:
98                session.save(filename)
99
100def _load_session(action, session):
101        filename = _show_dialog('Load Session',
102                                gtk.FILE_CHOOSER_ACTION_OPEN)
103        if filename:
104                session.load(filename, 0)
105
106_actions = [
107        ('SaveSession', None, 'Save Session…', None, None, _save_session),
108        ('LoadSession', None, 'Load Session…', None, None, _load_session)
109        ]
110
111def attach_window(window):
112        session = _get_session()
113        ui_manager = window.get_ui_manager()
114        group = gtk.ActionGroup('SessionManager')
115        group.add_actions(_actions, user_data=session)
116        ui_manager.insert_action_group(group, -1)
117        ui_id = ui_manager.add_ui_from_string(_ui_str)
118
119        window._session_manager_data = (group, ui_id)
120
121def detach_window(window):
122        group, ui_id = window._session_manager_data
123        del window._session_manager_data
124
125        ui_manager = window.get_ui_manager()
126        ui_manager.remove_ui(ui_id)
127        ui_manager.remove_action_group(group)
128        ui_manager.ensure_update()
Note: See TracBrowser for help on using the repository browser.