~jhonnyc/cgmail/jonathanc-branch

« back to all changes in this revision

Viewing changes to src/accpropdialog.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 gobject
5
 
 
6
 
from common import *
7
 
 
8
 
GLADE_FILE = os.path.join(GLADE_BASE_PATH, "accounts_properties_dialog.glade")
9
 
#GLADE_FILE = "../glade/accounts_properties_dialog.glade"
10
 
 
11
 
 
12
 
(
13
 
 ICON_COL,
14
 
 NAME_COL,
15
 
 ID_COL
16
 
) = range(3)
17
 
 
18
 
(
19
 
 TYPE_GMAIL,
20
 
 TYPE_POP3,
21
 
 TYPE_IMAP
22
 
) = range(3)
23
 
 
24
 
class AccountPropertiesDialog:
25
 
        def __init__(self):
26
 
                self.widgets = gtk.glade.XML(GLADE_FILE, domain = "cgmail")
27
 
 
28
 
                dict = {
29
 
                        "on_imap_other_rb_toggled": self.on_imap_other_toggled
30
 
                }
31
 
 
32
 
                self.widgets.signal_autoconnect(dict)
33
 
                self.dialog = self.widgets.get_widget("dialog")
34
 
 
35
 
                self.gmail_icon = gtk.gdk.pixbuf_new_from_file(GMAIL_ICON)
36
 
                self.pop3_icon = gtk.gdk.pixbuf_new_from_file(POP3_ICON)
37
 
                self.imap_icon = gtk.gdk.pixbuf_new_from_file(IMAP_ICON)
38
 
                #self.gmail_icon = gtk.gdk.pixbuf_new_from_file("../data/gmail.png")
39
 
                #self.pop3_icon = gtk.gdk.pixbuf_new_from_file("../data/nomail.png")
40
 
 
41
 
                self.notebook = self.widgets.get_widget("notebook")
42
 
                
43
 
                self.typebox_liststore = gtk.ListStore( gtk.gdk.Pixbuf,
44
 
                                gobject.TYPE_STRING, gobject.TYPE_INT)
45
 
                self.typebox_liststore.append([self.gmail_icon, _("gmail"),
46
 
                                                                        TYPE_GMAIL])
47
 
                self.typebox_liststore.append([self.pop3_icon, _("pop3"),
48
 
                                                                        TYPE_POP3])
49
 
                self.typebox_liststore.append([self.imap_icon, _("imap"),
50
 
                                                                        TYPE_IMAP])
51
 
 
52
 
 
53
 
 
54
 
                self.typebox = self.widgets.get_widget("type_box")
55
 
                self.typebox.set_model(self.typebox_liststore)
56
 
 
57
 
                self.type_pixcell = gtk.CellRendererPixbuf()
58
 
                self.type_textcell = gtk.CellRendererText()
59
 
                self.typebox.pack_start(self.type_pixcell, True)
60
 
                self.typebox.pack_end(self.type_textcell, True)
61
 
                self.type_pixcell.set_property( 'xalign', 0.0 )
62
 
 
63
 
                self.typebox.add_attribute(self.type_pixcell, 'pixbuf', 0)
64
 
                self.typebox.add_attribute(self.type_textcell, 'text', 1)
65
 
 
66
 
                self.typebox.set_active(TYPE_GMAIL)
67
 
                self.typebox.connect("changed", self.on_typebox_changed)
68
 
 
69
 
                self.conn_settings_frame = self.widgets.get_widget("conn_settings_frame")
70
 
                self.conn_type_frame = self.widgets.get_widget("conn_type_frame")
71
 
                self.imap_mbox_frame = self.widgets.get_widget("imap_mbox_frame")
72
 
 
73
 
                self.conn_settings_frame.hide()
74
 
                self.conn_type_frame.hide()
75
 
                self.imap_mbox_frame.hide()
76
 
                self.dialog.resize(400, 300)
77
 
 
78
 
                self.account_enabled_cb = self.widgets.get_widget("account_enabled_cb")
79
 
                        
80
 
                self.user_entry = self.widgets.get_widget("user_entry")
81
 
                self.passw_entry = self.widgets.get_widget("password_entry")
82
 
                self.server_entry = self.widgets.get_widget("server_entry")
83
 
                self.port_spin = self.widgets.get_widget("port_spin")
84
 
                self.standard_connection = self.widgets.get_widget("standard_connection")
85
 
                self.ssl_connection = self.widgets.get_widget("ssl_connection")
86
 
                self.imap_default_rb = self.widgets.get_widget("imap_default_rb")
87
 
                self.imap_other_rb = self.widgets.get_widget("imap_other_rb")
88
 
                self.imap_mbox_entry = self.widgets.get_widget("imap_mbox_entry")
89
 
 
90
 
                self.imap_mbox_entry.set_sensitive(False)
91
 
 
92
 
        
93
 
        def fill(self, amanager, id = None):
94
 
                self.amanager = amanager
95
 
                self.account_id = id
96
 
                
97
 
                if self.account_id is not None:
98
 
                        info = {}
99
 
                        try:
100
 
                                dic = self.amanager.get_accounts_dicts()
101
 
                        except:
102
 
                                print "accpropdialog.py: Error retriving configuration"
103
 
                                self.dialog.destroy()
104
 
                                return
105
 
                        for account in dic:
106
 
                                if account["id"] == self.account_id:
107
 
                                        info = account
108
 
                                        break
109
 
                        if info["type"] == "gmail":
110
 
                                self.typebox.set_active(TYPE_GMAIL)
111
 
                        elif info["type"] == "pop3":
112
 
                                self.typebox.set_active(TYPE_POP3)
113
 
                        elif info["type"] == "imap":
114
 
                                self.typebox.set_active(TYPE_IMAP)
115
 
 
116
 
                        enabled = info["enabled"]
117
 
                        if enabled == "1":
118
 
                                self.account_enabled_cb.set_active(True)
119
 
                        else:
120
 
                                self.account_enabled_cb.set_active(False)
121
 
        
122
 
                        self.user_entry.set_text(info["username"])
123
 
                        self.passw_entry.set_text(info["password"])
124
 
                        if info["type"] != "gmail":
125
 
                                self.server_entry.set_text(info["server"])
126
 
                                self.port_spin.set_value(int(info["port"]))
127
 
                                if info["ssl"] == "0":
128
 
                                        self.standard_connection.set_active(True)
129
 
                                else:
130
 
                                        self.ssl_connection.set_active(True)
131
 
                                if info["type"] == "imap":
132
 
                                        if info["use_default_mbox"] == "1":
133
 
                                                self.imap_default_rb.set_active(True)
134
 
                                                self.imap_mbox_entry.set_sensitive(False)
135
 
                                        else:
136
 
                                                self.imap_other_rb.set_active(True)
137
 
                                                self.imap_mbox_entry.set_sensitive(True)
138
 
                                                self.imap_mbox.set_text(info["mbox"])
139
 
 
140
 
        
141
 
        def enable_disable_widg(self, type):
142
 
                if type == "pop3":
143
 
                        self.conn_settings_frame.show()
144
 
                        self.conn_type_frame.show()
145
 
                        self.imap_mbox_frame.hide()
146
 
                        self.port_spin.set_value(110) # pop3 default port
147
 
                        self.dialog.resize(400, 500)
148
 
                elif type == "gmail":
149
 
                        self.conn_settings_frame.hide()
150
 
                        self.conn_type_frame.hide()
151
 
                        self.imap_mbox_frame.hide()
152
 
                        self.dialog.resize(400, 300)
153
 
                elif type == "imap":
154
 
                        self.conn_settings_frame.show()
155
 
                        self.conn_type_frame.show()
156
 
                        self.port_spin.set_value(143) # imap default port
157
 
                        self.imap_mbox_frame.show()
158
 
                        self.dialog.resize(400, 600)
159
 
 
160
 
 
161
 
        def run(self):
162
 
        #       print "OOOOOOO"
163
 
                result = self.dialog.run()
164
 
                
165
 
                # retrieve data from dialog
166
 
                uname = self.user_entry.get_text().strip()
167
 
                passw = self.passw_entry.get_text().strip()
168
 
                server = self.server_entry.get_text().strip()
169
 
                port = str(self.port_spin.get_value_as_int())
170
 
                ssl = "0"
171
 
                if self.ssl_connection.get_active():
172
 
                        ssl = "1"
173
 
 
174
 
                account_enabled = "0"
175
 
                if self.account_enabled_cb.get_active():
176
 
                        account_enabled = "1"
177
 
 
178
 
                imap_default = "1"
179
 
                imap_mbox = ""
180
 
                if self.imap_other_rb.get_active():
181
 
                        imap_default = "0"
182
 
                        imap_mbox = self.imap_mbox_entry.get_text()
183
 
                
184
 
                type = None
185
 
                try:
186
 
                        model = self.typebox.get_model()
187
 
                        iter = self.typebox.get_active_iter()
188
 
                        type = model.get_value(iter, 1)
189
 
                except:
190
 
                        pass
191
 
                
192
 
                dic = {}
193
 
                if result == gtk.RESPONSE_OK or \
194
 
                        result == gtk.RESPONSE_APPLY:
195
 
 
196
 
                        canstore = True
197
 
 
198
 
                        if type == "gmail":
199
 
                                if uname != "" and passw != "":
200
 
                                        dic = {
201
 
                                                "type" : "gmail",
202
 
                                                "username" : uname,
203
 
                                                "enabled" : account_enabled,
204
 
                                                "password" : passw
205
 
                                        }
206
 
                                else:
207
 
                                        canstore = False
208
 
                        elif type == "pop3" or type == "imap":
209
 
                                if uname != "" and passw != "" \
210
 
                                                and server != "":
211
 
                                        dic = {
212
 
                                                "type" : type,
213
 
                                                "username" : uname,
214
 
                                                "password" : passw,
215
 
                                                "enabled" : account_enabled,
216
 
                                                "server": server,
217
 
                                                "ssl": ssl,
218
 
                                                "port" : port
219
 
                                        }
220
 
                                        if type == "imap":
221
 
                                                dic["use_default_mbox"] = imap_default
222
 
                                                dic["mbox"] = imap_mbox
223
 
                                else:
224
 
                                        canstore = False
225
 
                        else:
226
 
                                canstore = False
227
 
                                #print uname, passw, server, port, type, ssl
228
 
 
229
 
                        # check for required entries
230
 
                
231
 
                        if self.account_id is None and canstore:
232
 
                                # this is a new account
233
 
                                #print "adding account: ", dic
234
 
                                self.account_id = self.amanager.add_account(dic)
235
 
 
236
 
                                # must update checker
237
 
 
238
 
                        elif canstore:
239
 
                                # update an old account
240
 
                                #self.amanager.remove_account(self.account_id)
241
 
                                #self.account_id = self.amanager.add_account(dic)
242
 
                                self.amanager.update_account(self.account_id, dic)
243
 
                                #print "updating account: ", dic
244
 
 
245
 
                                # must update checker
246
 
 
247
 
                if result == gtk.RESPONSE_OK or result == gtk.RESPONSE_CANCEL:
248
 
                        self.dialog.destroy()
249
 
 
250
 
                elif result == gtk.RESPONSE_APPLY:
251
 
                        self.run()
252
 
        
253
 
        def on_imap_other_toggled(self, w):
254
 
                if w.get_active():
255
 
                        self.imap_mbox_entry.set_sensitive(True)
256
 
                else:
257
 
                        self.imap_mbox_entry.set_sensitive(False)
258
 
 
259
 
        def on_typebox_changed(self, arg):
260
 
                type = None
261
 
                try:
262
 
                        model = self.typebox.get_model()
263
 
                        iter = self.typebox.get_active_iter()
264
 
                        type = model.get_value(iter, 1)
265
 
                        self.enable_disable_widg(type)
266
 
                except:
267
 
                        print "notype error"
268
 
                        pass
269
 
                        
270
 
        
271
 
 
272
 
if __name__ == "__main__":
273
 
        from accountmanager import AccountManager
274
 
        a = AccountManager().get_manager()
275
 
        AccountPropertiesDialog(a).run()
276
 
        gtk.main()
277