~ara/ubuntu/lucid/mago/fix_tests_lucid

« back to all changes in this revision

Viewing changes to mago/application/evolution.py

  • Committer: Bazaar Package Importer
  • Author(s): Ara Pulido
  • Date: 2009-08-04 09:21:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090804092140-7ggrh3nlujflk0v8
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from main import Application
 
2
from ..utils import show_desktop 
 
3
import ldtp, ldtputils, ooldtp
 
4
import os
 
5
import gtk, glib
 
6
import tarfile
 
7
from shutil import move, rmtree, copytree, copy
 
8
from ConfigParser import ConfigParser
 
9
from string import Formatter
 
10
 
 
11
class AccountInfo(object):
 
12
 
 
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))
 
19
        self.name = name
 
20
 
 
21
    def __getattr__(self, name):
 
22
        try:
 
23
            return self.details[name]
 
24
        except KeyError:
 
25
            raise AttributeError
 
26
 
 
27
    @property
 
28
    def template_args(self):
 
29
        args = {}
 
30
        for arg in ('popaccount', 'popserver', 'smtpaccount', 'smtpserver', 
 
31
                    'emailaddress', 'accountname', 'realname', 'protocol', 'password'):
 
32
            args[arg] = getattr(self, arg)
 
33
        return args
 
34
 
 
35
class Evolution(Application):
 
36
    """
 
37
    Evolution application helper class.
 
38
    """
 
39
    WINDOW         = "*-Evolution"
 
40
    LAUNCHER       = "evolution"
 
41
    BTN_OK         = "OK"
 
42
 
 
43
    # Main window elements
 
44
    BTN_SENDRECEIVE = "btnSend/Receive"
 
45
    DLG_SENDRECEIVE = "dlgSend&ReceiveMail"
 
46
    DLG_ENTER_PASSWORD = "dlgEnterPasswordfor*" 
 
47
    TBL_MSG_FOLDERS = 'ttblMailFolderTree'
 
48
 
 
49
    # Compose message constants
 
50
    MNU_COMPOSENEW = "mnuComposeNewMessage"
 
51
    FRM_COMPOSE    = "frmComposeMessage"
 
52
    TXT_TO         = "txtTo"
 
53
    TXT_SUBJECT    = "txtSubject"
 
54
    TXT_BODY       = "txt6"
 
55
    BTN_SEND       = "btnSend"
 
56
 
 
57
    # Messages treetable
 
58
    MESSAGES       = "ttblMessages"
 
59
    STATUS_LBL     = "Status"
 
60
    STATUS_INDEX   = 0
 
61
    ATTACH_LBL     = "Attachment"
 
62
    ATTACH_INDEX   = 1
 
63
    FLAGGED_LBL    = "Flagged"
 
64
    FLAGGED_INDEX  = 2
 
65
    FROM_LBL       = "From"
 
66
    FROM_INDEX     = 3
 
67
    SUBJECT_LBL    = "Subject"
 
68
    SUBJECT_INDEX  = 4
 
69
    DATE_LBL       = "Date"
 
70
    DATE_INDEX     = 5
 
71
 
 
72
    def __init__(self):
 
73
        Application.__init__(self)
 
74
 
 
75
    def open(self, clean_profile=True, credentials=''):
 
76
        """
 
77
        It saves the old profile (if needed) and
 
78
        set up a new one. After this initial process,
 
79
        it opens the application
 
80
 
 
81
        @type clean_profile: boolean
 
82
        @param clean_profile: True, to back up the old profile and create a 
 
83
            new one (default)
 
84
        @type credentials: string
 
85
        @param credentials: Path to the config file with accounts information
 
86
        """
 
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)
 
91
 
 
92
        if clean_profile:
 
93
            self.backup_config()
 
94
 
 
95
        Application.open(self)
 
96
 
 
97
    def generate_profile(self, profile_template, template_args=None):
 
98
        """
 
99
        It uses the profile_template and the
 
100
        credentials to build a new profile folder
 
101
        """
 
102
        
 
103
        conf_folder = '~/.gconf/apps/evolution'
 
104
        template_file = 'mail/%gconf.xml'
 
105
 
 
106
        formatter = Formatter()
 
107
        copytree(profile_template, os.path.expanduser(conf_folder))
 
108
 
 
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')
 
111
        try:
 
112
            buf = formatter.format(buf, **template_args)
 
113
        except KeyError, e:
 
114
            raise Exception, e
 
115
        f.write(buf)
 
116
        f.close()
 
117
 
 
118
    def compose_new_message(self, to, subject='', body='', account=''):
 
119
       
 
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)
 
129
       
 
130
        # Remap is needed because the frame changes its name
 
131
        self.remap() 
 
132
        
 
133
        compose_window = ooldtp.context(subject)
 
134
 
 
135
        txtBody = compose_window.getchild(self.TXT_BODY)
 
136
        txtBody.settextvalue(body)
 
137
   
 
138
    def send_email(self, subject):
 
139
 
 
140
        compose_window = ooldtp.context(subject)
 
141
        btnSend = compose_window.getchild(self.BTN_SEND)
 
142
        btnSend.click()
 
143
 
 
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)
 
147
        btnsr.click()
 
148
        ldtp.waittillguiexist(self.DLG_SENDRECEIVE)
 
149
 
 
150
        if minimize:
 
151
            show_desktop(True)
 
152
 
 
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)
 
158
            btn_ok.click()
 
159
            if minimize:
 
160
                show_desktop(True)
 
161
 
 
162
        if dont_wait:
 
163
            return
 
164
 
 
165
        ldtp.waittillguinotexist(self.DLG_SENDRECEIVE)
 
166
 
 
167
     
 
168
    def get_list_messages(self, only_unread=False, folder="Inbox"):
 
169
 
 
170
        messages_list = []
 
171
        
 
172
        self.select_folder(folder)
 
173
 
 
174
        evolution = ooldtp.context(self.WINDOW)
 
175
        messages = evolution.getchild(self.MESSAGES)
 
176
 
 
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)
 
181
 
 
182
            if not only_unread or status == '0':
 
183
                messages_list.append({'subject':subject,'sender':sender,'status':status})
 
184
 
 
185
        return messages_list
 
186
 
 
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)
 
191
 
 
192
    def backup_config(self):
 
193
        """
 
194
        It saves the configuration of Evolution in a file
 
195
        called ~/.evo.bak.tar.gz{.n}
 
196
 
 
197
        It saves the evolution config & messages
 
198
        """
 
199
        p = os.path.expanduser('~/.evo.bak.tar.gz')
 
200
        backup_path = p
 
201
        i = 2
 
202
        while os.path.exists(backup_path):
 
203
            backup_path = '%s.%d' % (p, i)
 
204
            i += 1
 
205
            
 
206
        try:
 
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')
 
210
 
 
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)
 
217
            tar.close()
 
218
        except IOError:
 
219
            pass
 
220
        else:
 
221
            self.backup_path = backup_path
 
222
 
 
223
        # Shutdown processes related to the evolution configuration
 
224
        os.popen("evolution --force-shutdown")
 
225
        os.popen("gconftool-2 --shutdown")
 
226
        
 
227
    def restore_config(self):
 
228
        """
 
229
        It deletes the configuration folder and restore then
 
230
        one backed up (at backup_path)
 
231
        """
 
232
        try:
 
233
            rmtree(os.path.expanduser('~/.evolution'))
 
234
            rmtree(os.path.expanduser('~/.gconf/apps/evolution'))
 
235
        except OSError:
 
236
            pass
 
237
 
 
238
        try:
 
239
            tar = tarfile.open(self.backup_path, mode='r:gz')
 
240
            tar.extractall(os.path.expanduser('/.'))
 
241
        except IOError:
 
242
            traceback.print_exc()
 
243
 
 
244
 
 
245
    
 
246
if __name__ == "__main__":
 
247
    from time import sleep
 
248
    test = Evolution()
 
249
    #test.open()
 
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')
 
254
    test.backup_config()
 
255
    test.restore_config()
 
256
   # test.close()