2010-08-10 03:28:57 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
#Copyright 2010 Steffen Schaumburg
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as published by
#the Free Software Foundation, version 3 of the License.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU Affero General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
#In the "official" distribution you can find the license in agpl-3.0.txt.
import threading
import pygtk
pygtk . require ( ' 2.0 ' )
import gtk
2010-08-10 05:10:39 +02:00
from imaplib import IMAP4
2010-08-10 03:28:57 +02:00
import ImapFetcher
class GuiImapFetcher ( threading . Thread ) :
def __init__ ( self , config , db , sql , mainwin , debug = True ) :
self . config = config
self . db = db
self . mainVBox = gtk . VBox ( )
self . buttonsHBox = gtk . HBox ( )
self . mainVBox . pack_end ( self . buttonsHBox , expand = False )
2010-08-10 04:44:35 +02:00
label = gtk . Label ( " To cancel just close this tab. " )
2010-08-10 03:28:57 +02:00
self . buttonsHBox . add ( label )
2010-08-10 04:44:35 +02:00
self . saveButton = gtk . Button ( " _Save " )
2010-08-10 03:28:57 +02:00
self . saveButton . connect ( ' clicked ' , self . saveClicked )
self . buttonsHBox . add ( self . saveButton )
2010-08-10 04:44:35 +02:00
self . importAllButton = gtk . Button ( " _Import All " )
self . importAllButton . connect ( ' clicked ' , self . importAllClicked )
self . buttonsHBox . add ( self . importAllButton )
2010-08-10 03:28:57 +02:00
2010-08-10 04:44:35 +02:00
self . statusLabel = gtk . Label ( " If you change the config you must save before importing " )
2010-08-10 03:28:57 +02:00
self . mainVBox . pack_end ( self . statusLabel , expand = False , padding = 4 )
self . displayConfig ( )
self . mainVBox . show_all ( )
#end def __init__
def saveClicked ( self , widget , data = None ) :
2010-08-10 04:44:35 +02:00
row = self . rowVBox . get_children ( )
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
siteName = columns [ 0 ] . get_text ( )
fetchType = columns [ 1 ] . get_text ( )
code = siteName + " _ " + fetchType
for email in self . config . emails :
toSave = self . config . emails [ email ]
break
toSave . siteName = siteName
toSave . fetchType = fetchType
toSave . host = columns [ 2 ] . get_text ( )
toSave . username = columns [ 3 ] . get_text ( )
toSave . password = columns [ 4 ] . get_text ( )
toSave . folder = columns [ 5 ] . get_text ( )
if columns [ 6 ] . get_active ( ) == 0 :
toSave . useSsl = " True "
else :
toSave . useSsl = " False "
self . config . editEmail ( siteName , fetchType , toSave )
self . config . save ( )
2010-08-10 03:28:57 +02:00
#def saveClicked
2010-08-10 04:44:35 +02:00
def importAllClicked ( self , widget , data = None ) :
2010-08-10 03:28:57 +02:00
self . statusLabel . set_label ( " Starting import. Please wait. " ) #FIXME: why doesnt this one show?
for email in self . config . emails :
2010-08-10 05:10:39 +02:00
try :
print " trying to run "
result = ImapFetcher . run ( self . config . emails [ email ] , self . db )
self . statusLabel . set_label ( " Finished import without error. " )
print " finished to run "
except IMAP4 . error as strerror :
if str ( strerror ) == " [AUTHENTICATIONFAILED] Authentication failed. " :
self . statusLabel . set_label ( " Login to mailserver failed: please check mailserver, username and password " )
2010-08-10 04:44:35 +02:00
#def importAllClicked
2010-08-10 03:28:57 +02:00
def get_vbox ( self ) :
""" returns the vbox of this thread """
return self . mainVBox
#end def get_vbox
def displayConfig ( self ) :
2010-08-10 03:50:26 +02:00
box = gtk . HBox ( homogeneous = True )
for text in ( " Site " , " Fetch Type " , " Mailserver " , " Username " , " Password " , " Mail Folder " , " Use SSL " ) :
label = gtk . Label ( text )
box . add ( label )
2010-08-10 04:44:35 +02:00
self . mainVBox . pack_start ( box , expand = False )
self . rowVBox = gtk . VBox ( )
self . mainVBox . add ( self . rowVBox )
2010-08-10 03:50:26 +02:00
2010-08-10 03:28:57 +02:00
for email in self . config . emails :
2010-08-10 03:50:26 +02:00
config = self . config . emails [ email ]
box = gtk . HBox ( homogeneous = True )
2010-08-10 04:50:27 +02:00
for field in ( config . siteName , config . fetchType ) :
label = gtk . Label ( field )
box . add ( label )
for field in ( config . host , config . username , config . password , config . folder ) :
2010-08-10 03:50:26 +02:00
entry = gtk . Entry ( )
entry . set_text ( field )
box . add ( entry )
sslBox = gtk . combo_box_new_text ( )
sslBox . append_text ( " Yes " )
sslBox . append_text ( " No " )
sslBox . set_active ( 0 )
box . add ( sslBox )
2010-08-10 04:44:35 +02:00
#TODO: "run just this one" button
2010-08-10 03:50:26 +02:00
self . rowVBox . pack_start ( box , expand = False )
#print
2010-08-10 03:28:57 +02:00
2010-08-10 04:44:35 +02:00
self . mainVBox . show_all ( )
2010-08-10 03:28:57 +02:00
#end def displayConfig
#end class GuiImapFetcher