Improve session viewer. There were some off-by-one errors.
[fpdb-dooglus.git] / pyfpdb / GuiImapFetcher.py
blobb738b139b854c24695ddb0e29bf154fb82cac42a
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 #Copyright 2010-2011 Steffen Schaumburg
5 #This program is free software: you can redistribute it and/or modify
6 #it under the terms of the GNU Affero General Public License as published by
7 #the Free Software Foundation, version 3 of the License.
9 #This program is distributed in the hope that it will be useful,
10 #but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 #GNU General Public License for more details.
14 #You should have received a copy of the GNU Affero General Public License
15 #along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #In the "official" distribution you can find the license in agpl-3.0.txt.
18 import L10n
19 _ = L10n.get_translation()
21 import threading
22 import pygtk
23 pygtk.require('2.0')
24 import gtk
25 from imaplib import IMAP4
26 from socket import gaierror
28 import ImapFetcher
30 class GuiImapFetcher (threading.Thread):
31 def __init__(self, config, db, sql, mainwin, debug=True):
32 self.config = config
33 self.db = db
34 self.mainVBox = gtk.VBox()
37 self.buttonsHBox = gtk.HBox()
38 self.mainVBox.pack_end(self.buttonsHBox, expand=False)
40 label=gtk.Label(_("To cancel just close this tab."))
41 self.buttonsHBox.add(label)
43 self.saveButton = gtk.Button(_("_Save"))
44 self.saveButton.connect('clicked', self.saveClicked)
45 self.buttonsHBox.add(self.saveButton)
47 self.importAllButton = gtk.Button(_("_Import All"))
48 self.importAllButton.connect('clicked', self.importAllClicked)
49 self.buttonsHBox.add(self.importAllButton)
51 self.statusLabel=gtk.Label(_("If you change the config you must save before importing"))
52 self.mainVBox.pack_end(self.statusLabel, expand=False, padding=4)
54 self.passwords={}
55 self.displayConfig()
57 self.mainVBox.show_all()
58 #end def __init__
60 def saveClicked(self, widget, data=None):
61 row = self.rowVBox.get_children()
62 columns=row[0].get_children() #TODO: make save capable of handling multiple email entries - not relevant yet as only one entry is useful atm. The rest of this tab works fine for multiple entries though
64 siteName=columns[0].get_text()
65 fetchType=columns[1].get_text()
67 toSave=self.config.supported_sites[siteName].emails[fetchType]
69 toSave.host=columns[2].get_text()
70 toSave.username=columns[3].get_text()
72 if columns[4].get_text()=="***":
73 toSave.password=self.passwords[siteName+fetchType]
74 else:
75 toSave.password=columns[4].get_text()
77 toSave.folder=columns[5].get_text()
79 if columns[6].get_active() == 0:
80 toSave.useSsl="True"
81 else:
82 toSave.useSsl="False"
84 self.config.editEmail(siteName, fetchType, toSave)
85 self.config.save()
86 #def saveClicked
88 def importAllClicked(self, widget, data=None):
89 self.statusLabel.set_label(_("Starting import. Please wait.")) #FIXME: why doesnt this one show?
90 for siteName in self.config.supported_sites:
91 for fetchType in self.config.supported_sites[siteName].emails:
92 try:
93 result=ImapFetcher.run(self.config.supported_sites[siteName].emails[fetchType], self.db)
94 self.statusLabel.set_label(_("Finished import without error."))
95 except IMAP4.error as error:
96 if str(error)=="[AUTHENTICATIONFAILED] Authentication failed.":
97 self.statusLabel.set_label(_("Login to mailserver failed: please check mailserver, username and password"))
98 except gaierror as error:
99 if str(error)=="[Errno -2] Name or service not known":
100 self.statusLabel.set_label(_("Could not connect to mailserver: check mailserver and use SSL settings and internet connectivity"))
101 #def importAllClicked
103 def get_vbox(self):
104 """returns the vbox of this thread"""
105 return self.mainVBox
106 #end def get_vbox
108 def displayConfig(self):
109 box=gtk.HBox(homogeneous=True)
110 for text in (_("Site"), _("Fetch Type"), _("Mail Server"), _("Username"), _("Password"), _("Mail Folder"), _("Use SSL")):
111 label=gtk.Label(text)
112 box.add(label)
113 self.mainVBox.pack_start(box, expand=False)
115 self.rowVBox = gtk.VBox()
116 self.mainVBox.add(self.rowVBox)
118 for siteName in self.config.supported_sites:
119 for fetchType in self.config.supported_sites[siteName].emails:
120 config=self.config.supported_sites[siteName].emails[fetchType]
121 box=gtk.HBox(homogeneous=True)
123 for field in (siteName, config.fetchType):
124 label=gtk.Label(field)
125 box.add(label)
127 for field in (config.host, config.username):
128 entry=gtk.Entry()
129 entry.set_text(field)
130 box.add(entry)
132 entry=gtk.Entry()
133 self.passwords[siteName+fetchType]=config.password
134 entry.set_text("***")
135 box.add(entry)
137 entry=gtk.Entry()
138 entry.set_text(config.folder)
139 box.add(entry)
141 sslBox = gtk.combo_box_new_text()
142 sslBox.append_text(_("Yes"))
143 sslBox.append_text(_("No"))
144 if config.useSsl:
145 sslBox.set_active(0)
146 else:
147 sslBox.set_active(1)
148 box.add(sslBox)
150 #TODO: "run just this one" button
152 self.rowVBox.pack_start(box, expand=False)
153 #print
155 self.mainVBox.show_all()
156 #end def displayConfig
157 #end class GuiImapFetcher