1
from main import Application
2
from ..utils import show_desktop
3
import ldtp, ldtputils, ooldtp
7
from shutil import move, rmtree, copytree, copy
8
from ConfigParser import ConfigParser
9
from string import Formatter
11
class AccountInfo(object):
13
def __init__(self, name, credentials):
14
if not isinstance(credentials, ConfigParser):
15
creds_fn = credentials
16
credentials = ConfigParser()
17
credentials.read(creds_fn)
18
self.details = dict(credentials.items(name))
21
def __getattr__(self, name):
23
return self.details[name]
28
def template_args(self):
30
for arg in ('popaccount', 'popserver', 'smtpaccount', 'smtpserver',
31
'emailaddress', 'accountname', 'realname', 'protocol', 'password'):
32
args[arg] = getattr(self, arg)
35
class Evolution(Application):
37
Evolution application helper class.
39
WINDOW = "*-Evolution"
40
LAUNCHER = "evolution"
43
# Main window elements
44
BTN_SENDRECEIVE = "btnSend/Receive"
45
DLG_SENDRECEIVE = "dlgSend&ReceiveMail"
46
DLG_ENTER_PASSWORD = "dlgEnterPasswordfor*"
47
TBL_MSG_FOLDERS = 'ttblMailFolderTree'
49
# Compose message constants
50
MNU_COMPOSENEW = "mnuComposeNewMessage"
51
FRM_COMPOSE = "frmComposeMessage"
53
TXT_SUBJECT = "txtSubject"
58
MESSAGES = "ttblMessages"
61
ATTACH_LBL = "Attachment"
63
FLAGGED_LBL = "Flagged"
67
SUBJECT_LBL = "Subject"
73
Application.__init__(self)
75
def open(self, clean_profile=True, credentials=''):
77
It saves the old profile (if needed) and
78
set up a new one. After this initial process,
79
it opens the application
81
@type clean_profile: boolean
82
@param clean_profile: True, to back up the old profile and create a
84
@type credentials: string
85
@param credentials: Path to the config file with accounts information
87
clean_profile = clean_profile not in ('False', '0', False)
88
self.creds_fn = credentials
89
self.credentials = ConfigParser()
90
self.credentials.read(self.creds_fn)
95
Application.open(self)
97
def generate_profile(self, profile_template, template_args=None):
99
It uses the profile_template and the
100
credentials to build a new profile folder
103
conf_folder = '~/.gconf/apps/evolution'
104
template_file = 'mail/%gconf.xml'
106
formatter = Formatter()
107
copytree(profile_template, os.path.expanduser(conf_folder))
109
buf = open(os.path.join(os.path.join(profile_template, template_file))).read()
110
f = open(os.path.join(os.path.expanduser(conf_folder), template_file), 'w')
112
buf = formatter.format(buf, **template_args)
118
def compose_new_message(self, to, subject='', body='', account=''):
120
evolution = ooldtp.context(self.WINDOW)
121
mnuComposeNew = evolution.getchild(self.MNU_COMPOSENEW)
122
mnuComposeNew.click()
123
ldtp.waittillguiexist(self.FRM_COMPOSE)
124
compose_window = ooldtp.context(self.FRM_COMPOSE)
125
txtTo = compose_window.getchild(self.TXT_TO)
126
txtTo.settextvalue(to)
127
txtSubject = compose_window.getchild(self.TXT_SUBJECT)
128
txtSubject.settextvalue(subject)
130
# Remap is needed because the frame changes its name
133
compose_window = ooldtp.context(subject)
135
txtBody = compose_window.getchild(self.TXT_BODY)
136
txtBody.settextvalue(body)
138
def send_email(self, subject):
140
compose_window = ooldtp.context(subject)
141
btnSend = compose_window.getchild(self.BTN_SEND)
144
def send_and_receive(self, password='', minimize=False, dont_wait=False):
145
evolution = ooldtp.context(self.WINDOW)
146
btnsr = evolution.getchild(self.BTN_SENDRECEIVE)
148
ldtp.waittillguiexist(self.DLG_SENDRECEIVE)
153
if ldtp.waittillguiexist(self.DLG_ENTER_PASSWORD, guiTimeOut=2) == 1:
154
enter_pass_dlg = ooldtp.context(self.DLG_ENTER_PASSWORD)
155
txt_pass = enter_pass_dlg.getchild(role='password_text')[0]
156
txt_pass.settextvalue(password)
157
btn_ok = enter_pass_dlg.getchild(self.BTN_OK)
165
ldtp.waittillguinotexist(self.DLG_SENDRECEIVE)
168
def get_list_messages(self, only_unread=False, folder="Inbox"):
172
self.select_folder(folder)
174
evolution = ooldtp.context(self.WINDOW)
175
messages = evolution.getchild(self.MESSAGES)
177
for i in range(messages.getrowcount()):
178
status = messages.getcellvalue(i, self.STATUS_INDEX)
179
sender = messages.getcellvalue(i, self.FROM_INDEX)
180
subject = messages.getcellvalue(i, self.SUBJECT_INDEX)
182
if not only_unread or status == '0':
183
messages_list.append({'subject':subject,'sender':sender,'status':status})
187
def select_folder(self, folder):
188
evolution = ooldtp.context(self.WINDOW)
189
table_msg = evolution.getchild(self.TBL_MSG_FOLDERS)
190
table_msg.selectrowpartialmatch(folder)
192
def backup_config(self):
194
It saves the configuration of Evolution in a file
195
called ~/.evo.bak.tar.gz{.n}
197
It saves the evolution config & messages
199
p = os.path.expanduser('~/.evo.bak.tar.gz')
202
while os.path.exists(backup_path):
203
backup_path = '%s.%d' % (p, i)
207
tar = tarfile.open(backup_path, mode='w:gz')
208
evolution_conf = os.path.expanduser('~/.evolution')
209
evolution_gconf = os.path.expanduser('~/.gconf/apps/evolution')
211
if os.path.exists(evolution_conf):
212
tar.add(evolution_conf)
213
rmtree(evolution_conf)
214
if os.path.exists(evolution_gconf):
215
tar.add(evolution_gconf)
216
rmtree(evolution_gconf)
221
self.backup_path = backup_path
223
# Shutdown processes related to the evolution configuration
224
os.popen("evolution --force-shutdown")
225
os.popen("gconftool-2 --shutdown")
227
def restore_config(self):
229
It deletes the configuration folder and restore then
230
one backed up (at backup_path)
233
rmtree(os.path.expanduser('~/.evolution'))
234
rmtree(os.path.expanduser('~/.gconf/apps/evolution'))
239
tar = tarfile.open(self.backup_path, mode='r:gz')
240
tar.extractall(os.path.expanduser('/.'))
242
traceback.print_exc()
246
if __name__ == "__main__":
247
from time import sleep
250
#subject = "Greetings"
251
#test.compose_new_message("tester.ubuntu@gmail.com", subject, "hey hey")
252
#test.send_email(subject)
253
#test.send_and_receive('testingubuntu')
255
test.restore_config()