~jhonnyc/cgmail/jonathanc-branch

« back to all changes in this revision

Viewing changes to src/manager/preferencesdialog.py

  • Committer: Marco Ferragina
  • Date: 2007-05-06 15:51:12 UTC
  • Revision ID: marco.ferragina@gmail.com-20070506155112-874uk2m8blrknyuf
Restructured package source dir. Now is much more organized. Implemented right click menu on account window treeview

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import gtk
 
2
import gtk.glade
 
3
import os
 
4
import shutil
 
5
 
 
6
from lib.common import *
 
7
from lib.gconfhelper import GconfHelper
 
8
from lib.accountmanager import *
 
9
 
 
10
GLADE_FILE = os.path.join(GLADE_BASE_PATH, "preferences_dialog.glade")
 
11
 
 
12
 
 
13
class PreferencesDialog:
 
14
        def __init__(self):
 
15
                self.ignore_signals = True
 
16
                self.account_manager = AccountManager()
 
17
 
 
18
                self.widgets = gtk.glade.XML(GLADE_FILE, domain = "cgmail")
 
19
 
 
20
                dict = {
 
21
                        "on_display_notif_cb_toggled": self.on_display_notif_toggled,
 
22
                        "on_play_snd_cb_toggled" : self.on_play_snd_toggled,
 
23
                        "on_autostart_cb_toggled" : self.on_autostart_cb_toggled,
 
24
                        "on_gnome_keyring_rb_toggled" : self.on_use_gnome_keyring,
 
25
                        "on_always_status_icon_cb_toggled" : self.on_always_status_icon,
 
26
                        "on_command_check_toggled" : self.on_command_check,
 
27
                        "on_errors_notify_cb_toggled" : self.on_notify_errors,
 
28
                        "on_plain_file_rb_toggled" : self.on_use_plain_file,
 
29
                        "on_check_interval_sp_value_changed" : self.on_check_interval_changed
 
30
                }
 
31
 
 
32
                self.command_entry = self.widgets.get_widget("command_entry")
 
33
 
 
34
                self.widgets.signal_autoconnect(dict)
 
35
                self.dialog = self.widgets.get_widget("dialog")
 
36
                self.gconf_helper = GconfHelper()
 
37
 
 
38
                display_notif_cb = self.widgets.get_widget("display_notif_cb")
 
39
                value = self.gconf_helper.get_key("display_notifications")
 
40
                display_notif_cb.set_active(value)
 
41
                
 
42
                play_snd_cb = self.widgets.get_widget("play_snd_cb")
 
43
                value = self.gconf_helper.get_key("play_sounds_on_new_mails")
 
44
                play_snd_cb.set_active(value)
 
45
                
 
46
                errors_notify_cb = self.widgets.get_widget("errors_notify_cb")
 
47
                value = self.gconf_helper.get_key("notify_errors")
 
48
                errors_notify_cb.set_active(value)
 
49
 
 
50
                command_check = self.widgets.get_widget("command_check")
 
51
                value = self.gconf_helper.get_key("exec_command")
 
52
                command_check.set_active(value)
 
53
                self.command_entry.set_sensitive(value)
 
54
 
 
55
                value = self.gconf_helper.get_key("new_mail_command")
 
56
                self.command_entry.set_text(value)
 
57
                
 
58
                self.gnome_keyring_rb = self.widgets.get_widget("gnome_keyring_rb")
 
59
                self.plain_file_rb = self.widgets.get_widget("plain_file_rb")
 
60
                value = self.gconf_helper.get_key("use_gnome_keyring")
 
61
                if value:
 
62
                        self.gnome_keyring_rb.set_active(True)
 
63
                else:
 
64
                        self.plain_file_rb.set_active(True)
 
65
                
 
66
                check_interval_sp = self.widgets.get_widget("check_interval_sp")
 
67
                value = self.gconf_helper.get_key("check_interval")
 
68
                check_interval_sp.set_value(value)
 
69
 
 
70
                self.autostart_file = os.path.expanduser("~/.config/autostart/cgmailservice.desktop")
 
71
                autostart_cb = self.widgets.get_widget("autostart_cb")
 
72
                value =  os.path.exists(self.autostart_file)
 
73
                autostart_cb.set_active(value)
 
74
 
 
75
                self.ignore_signals = False
 
76
 
 
77
                self.run()
 
78
 
 
79
        
 
80
        def run(self):
 
81
                result = self.dialog.run()
 
82
                cmd = self.command_entry.get_text()
 
83
                self.gconf_helper.set_key("new_mail_command", cmd)
 
84
                self.dialog.destroy()
 
85
        
 
86
        def on_autostart_cb_toggled(self, w):
 
87
                if self.ignore_signals: return
 
88
 
 
89
                if w.get_active():
 
90
                        if not os.path.exists(self.autostart_file):
 
91
                                try:
 
92
                                        shutil.copyfile(CGMAIL_AUTOSTART_FILE, 
 
93
                                                        self.autostart_file)
 
94
                                        os.chmod(self.autostart_file, 0700)
 
95
                                except IOError:
 
96
                                        print "Warning: cannot write to path", self.autostart_file
 
97
                else:
 
98
                        if os.path.exists(self.autostart_file):
 
99
                                try:
 
100
                                        os.unlink(self.autostart_file)
 
101
                                except:
 
102
                                        print "Warning: cannot delete", self.autostart_file
 
103
 
 
104
        def on_command_check(self, w):
 
105
                if self.ignore_signals: return
 
106
                self.command_entry.set_sensitive(w.get_active())
 
107
                if w.get_active():
 
108
                        self.gconf_helper.set_key("exec_command", True)
 
109
                        cmd = self.command_entry.get_text()
 
110
                        self.gconf_helper.set_key("new_mail_command", cmd)
 
111
                else:
 
112
                        self.gconf_helper.set_key("exec_command", False)
 
113
                        self.gconf_helper.set_key("new_mail_command", "")
 
114
        
 
115
        def on_display_notif_toggled(self, w):
 
116
                if self.ignore_signals: return
 
117
 
 
118
                if w.get_active():
 
119
                        self.gconf_helper.set_key("display_notifications", True)
 
120
                else:
 
121
                        self.gconf_helper.set_key("display_notifications", False)
 
122
        
 
123
        def on_play_snd_toggled(self, w):
 
124
                if self.ignore_signals: return
 
125
 
 
126
                if w.get_active():
 
127
                        self.gconf_helper.set_key("play_sounds_on_new_mails", True)
 
128
                else:
 
129
                        self.gconf_helper.set_key("play_sounds_on_new_mails", False)
 
130
        
 
131
        def on_notify_errors(self, w):
 
132
                if self.ignore_signals: return
 
133
 
 
134
                if w.get_active():
 
135
                        self.gconf_helper.set_key("notify_errors", True)
 
136
                else:
 
137
                        self.gconf_helper.set_key("notify_errors", False)
 
138
 
 
139
 
 
140
        def on_use_gnome_keyring(self, w):
 
141
                if self.ignore_signals: return
 
142
 
 
143
                if w.get_active():
 
144
                        try:
 
145
                                self.account_manager.set_keyring_default()
 
146
                                self.gconf_helper.set_key("use_gnome_keyring", True)
 
147
                        except CannotSetDefault:
 
148
                                # We can pass here becouse if set_keyring_default
 
149
                                # will raise the exception, the set_key method will
 
150
                                # not be called
 
151
                                self.ignore_signals = True
 
152
                                self.plain_file_rb.set_active(True)
 
153
                                self.ignore_signals = False
 
154
        
 
155
        def on_use_plain_file(self, w):
 
156
                if self.ignore_signals: return
 
157
 
 
158
                if w.get_active():
 
159
                        try:
 
160
                                self.account_manager.set_xml_default()
 
161
                                self.gconf_helper.set_key("use_gnome_keyring", False)
 
162
                        except CannotSetDefault:
 
163
                                # same as on_use_gnome_keyring
 
164
                                self.ignore_signals = True
 
165
                                self.gnome_keyring_rb.set_active(True)
 
166
                                self.ignore_signals = False
 
167
        
 
168
        def on_always_status_icon(self, w):
 
169
                if self.ignore_signals: return
 
170
        
 
171
        def on_check_interval_changed(self, w):
 
172
                if self.ignore_signals: return
 
173
 
 
174
                value = w.get_value_as_int()
 
175
                self.gconf_helper.set_key("check_interval", value)