~saurabhanandiit/gtg/exportFixed

« back to all changes in this revision

Viewing changes to GTG/gtk/backends_dialog/parameters_ui/pathui.py

Merge of my work on liblarch newbase and all the backends ported to liblarch
(which mainly means porting the datastore).
One failing test, will check it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
5
#
 
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
 
9
# version.
 
10
#
 
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
 
14
# details.
 
15
#
 
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
# -----------------------------------------------------------------------------
 
19
 
 
20
import gtk
 
21
import os.path
 
22
 
 
23
from GTG import _
 
24
 
 
25
 
 
26
 
 
27
 
 
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)
 
31
    '''
 
32
    
 
33
 
 
34
    def __init__(self, req, backend, width):
 
35
        '''
 
36
        Creates the textbox, the button and loads the current path.
 
37
 
 
38
        @param req: a Requester
 
39
        @param backend: a backend object
 
40
        @param width: the width of the gtk.Label object
 
41
        '''
 
42
        super(PathUI, self).__init__()
 
43
        self.backend = backend
 
44
        self.req = req
 
45
        self._populate_gtk(width)
 
46
 
 
47
    def _populate_gtk(self, width):
 
48
        '''Creates the gtk.Label, the textbox and the button
 
49
        
 
50
        @param width: the width of the gtk.Label object
 
51
        '''
 
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)
 
67
 
 
68
    def commit_changes(self):
 
69
        '''Saves the changes to the backend parameter'''
 
70
        self.backend.set_parameter('path', self.textbox.get_text())
 
71
 
 
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
 
75
        (s)he made.
 
76
 
 
77
        @param sender: not used, only here for signal compatibility
 
78
        '''
 
79
        if self.backend.is_enabled() and not self.backend.is_default():
 
80
            self.req.set_backend_enabled(self.backend.get_id(), False)
 
81
    
 
82
    def on_button_clicked(self, sender):
 
83
        '''Shows the filesystem explorer to choose a new file
 
84
 
 
85
        @param sender: not used, only here for signal compatibility
 
86
        '''
 
87
        self.chooser = gtk.FileChooserDialog( \
 
88
                    title=None,
 
89
                    action=gtk.FILE_CHOOSER_ACTION_SAVE,
 
90
                    buttons=(gtk.STOCK_CANCEL,
 
91
                             gtk.RESPONSE_CANCEL, \
 
92
                             gtk.STOCK_OK, \
 
93
                             gtk.RESPONSE_OK))
 
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()))
 
98
        
 
99
        #filter files
 
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()