1
# -*- coding: utf-8 -*-
2
# -----------------------------------------------------------------------------
3
# Getting Things Gnome! - a personal organizer for the GNOME desktop
4
# Copyright (c) 2008-2009 - Lionel Dricot & Bertrand Rousseau
6
# This program is free software: you can redistribute it and/or modify it under
7
# the terms of the GNU General Public License as published by the Free Software
8
# Foundation, either version 3 of the License, or (at your option) any later
11
# This program is distributed in the hope that it will be useful, but WITHOUT
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
# You should have received a copy of the GNU General Public License along with
17
# this program. If not, see <http://www.gnu.org/licenses/>.
18
# -----------------------------------------------------------------------------
28
class PathUI(gtk.HBox):
29
'''Gtk widgets to show a path in a textbox, and a button to bring up a
30
filesystem explorer to modify that path (also, a label to describe those)
34
def __init__(self, req, backend, width):
36
Creates the textbox, the button and loads the current path.
38
@param req: a Requester
39
@param backend: a backend object
40
@param width: the width of the gtk.Label object
42
super(PathUI, self).__init__()
43
self.backend = backend
45
self._populate_gtk(width)
47
def _populate_gtk(self, width):
48
'''Creates the gtk.Label, the textbox and the button
50
@param width: the width of the gtk.Label object
52
label = gtk.Label(_("Filename:"))
53
label.set_line_wrap(True)
54
label.set_alignment(xalign = 0, yalign = 0.5)
55
label.set_size_request(width = width, height = -1)
56
self.pack_start(label, False)
57
align = gtk.Alignment(xalign = 0, yalign = 0.5, xscale = 1)
58
align.set_padding(0, 0, 10, 0)
59
self.pack_start(align, True)
60
self.textbox = gtk.Entry()
61
self.textbox.set_text(self.backend.get_parameters()['path'])
62
self.textbox.connect('changed', self.on_path_modified)
63
align.add(self.textbox)
64
self.button = gtk.Button(stock = gtk.STOCK_EDIT)
65
self.button.connect('clicked', self.on_button_clicked)
66
self.pack_start(self.button, False)
68
def commit_changes(self):
69
'''Saves the changes to the backend parameter'''
70
self.backend.set_parameter('path', self.textbox.get_text())
72
def on_path_modified(self, sender):
73
''' Signal callback, executed when the user edits the path.
74
Disables the backend. The user will re-enable it to confirm the changes
77
@param sender: not used, only here for signal compatibility
79
if self.backend.is_enabled() and not self.backend.is_default():
80
self.req.set_backend_enabled(self.backend.get_id(), False)
82
def on_button_clicked(self, sender):
83
'''Shows the filesystem explorer to choose a new file
85
@param sender: not used, only here for signal compatibility
87
self.chooser = gtk.FileChooserDialog( \
89
action=gtk.FILE_CHOOSER_ACTION_SAVE,
90
buttons=(gtk.STOCK_CANCEL,
91
gtk.RESPONSE_CANCEL, \
94
self.chooser.set_default_response(gtk.RESPONSE_OK)
95
#set default file as the current self.path
96
self.chooser.set_current_name(os.path.basename(self.textbox.get_text()))
97
self.chooser.set_current_folder(os.path.dirname(self.textbox.get_text()))
100
afilter = gtk.FileFilter()
101
afilter.set_name("All files")
102
afilter.add_pattern("*")
103
self.chooser.add_filter(afilter)
104
afilter = gtk.FileFilter()
105
afilter.set_name("XML files")
106
afilter.add_mime_type("text/plain")
107
afilter.add_pattern("*.xml")
108
self.chooser.add_filter(afilter)
109
response = self.chooser.run()
110
if response == gtk.RESPONSE_OK:
111
self.textbox.set_text(self.chooser.get_filename())
112
self.chooser.destroy()