1
from main import Application
2
from ..utils import show_desktop
3
import ldtp, ldtputils, ooldtp
7
from shutil import move, rmtree, copytree
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/mail'
104
os.makedirs(os.path.expanduser(conf_folder))
106
formatter = Formatter()
107
for fn in os.listdir(profile_template):
108
if os.path.isdir(os.path.join(profile_template, fn)):
109
copytree(os.path.join(profile_template, fn),
110
os.path.join(os.path.expanduser(conf_folder), fn))
112
buf = open(os.path.join(profile_template, fn)).read()
113
f = open(os.path.join(os.path.expanduser(conf_folder), fn), 'w')
115
buf = formatter.format(buf, **template_args)
121
def compose_new_message(self, to, subject='', body='', account=''):
123
evolution = ooldtp.context(self.WINDOW)
124
mnuComposeNew = evolution.getchild(self.MNU_COMPOSENEW)
125
mnuComposeNew.click()
126
ldtp.waittillguiexist(self.FRM_COMPOSE)
127
compose_window = ooldtp.context(self.FRM_COMPOSE)
128
txtTo = compose_window.getchild(self.TXT_TO)
129
txtTo.settextvalue(to)
130
txtSubject = compose_window.getchild(self.TXT_SUBJECT)
131
txtSubject.settextvalue(subject)
133
# Remap is needed because the frame changes its name
136
compose_window = ooldtp.context(subject)
138
txtBody = compose_window.getchild(self.TXT_BODY)
139
txtBody.settextvalue(body)
141
def send_email(self, subject):
143
compose_window = ooldtp.context(subject)
144
btnSend = compose_window.getchild(self.BTN_SEND)
147
def send_and_receive(self, password='', minimize=False, dont_wait=False):
148
evolution = ooldtp.context(self.WINDOW)
149
btnsr = evolution.getchild(self.BTN_SENDRECEIVE)
151
ldtp.waittillguiexist(self.DLG_SENDRECEIVE)
156
if ldtp.waittillguiexist(self.DLG_ENTER_PASSWORD, guiTimeOut=2) == 1:
157
enter_pass_dlg = ooldtp.context(self.DLG_ENTER_PASSWORD)
158
txt_pass = enter_pass_dlg.getchild(role='password_text')[0]
159
txt_pass.settextvalue(password)
160
btn_ok = enter_pass_dlg.getchild(self.BTN_OK)
168
ldtp.waittillguinotexist(self.DLG_SENDRECEIVE)
171
def get_list_messages(self, only_unread=False, folder="Inbox"):
175
self.select_folder(folder)
177
evolution = ooldtp.context(self.WINDOW)
178
messages = evolution.getchild(self.MESSAGES)
180
for i in range(messages.getrowcount()):
181
status = messages.getcellvalue(i, self.STATUS_INDEX)
182
sender = messages.getcellvalue(i, self.FROM_INDEX)
183
subject = messages.getcellvalue(i, self.SUBJECT_INDEX)
185
if not only_unread or status == '0':
186
messages_list.append({'subject':subject,'sender':sender,'status':status})
190
def select_folder(self, folder):
191
evolution = ooldtp.context(self.WINDOW)
192
table_msg = evolution.getchild(self.TBL_MSG_FOLDERS)
193
table_msg.selectrowpartialmatch(folder)
195
def backup_config(self):
197
It saves the configuration of Evolution in a file
198
called ~/.evo.bak.tar.gz{.n}
200
It saves the evolution config & messages
202
p = os.path.expanduser('~/.evo.bak.tar.gz')
205
while os.path.exists(backup_path):
206
backup_path = '%s.%d' % (p, i)
210
tar = tarfile.open(backup_path, mode='w:gz')
211
evolution_conf = os.path.expanduser('~/.evolution')
212
evolution_gconf = os.path.expanduser('~/.gconf/apps/evolution')
214
if os.path.exists(evolution_conf):
215
tar.add(evolution_conf)
216
rmtree(evolution_conf)
217
if os.path.exists(evolution_gconf):
218
tar.add(evolution_gconf)
219
rmtree(evolution_gconf)
224
self.backup_path = backup_path
226
def restore_config(self):
228
It deletes the configuration folder and restore then
229
one backed up (at backup_path)
232
rmtree(os.path.expanduser('~/.evolution'))
233
rmtree(os.path.expanduser('~/.gconf/apps/evolution'))
238
tar = tarfile.open(self.backup_path, mode='r:gz')
239
tar.extractall(os.path.expanduser('/.'))
241
traceback.print_exc()
245
if __name__ == "__main__":
246
from time import sleep
249
#subject = "Greetings"
250
#test.compose_new_message("tester.ubuntu@gmail.com", subject, "hey hey")
251
#test.send_email(subject)
252
#test.send_and_receive('testingubuntu')
254
test.restore_config()