~ubuntu-branches/ubuntu/natty/mago/natty

« back to all changes in this revision

Viewing changes to mago/application/evolution.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-02-08 13:32:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110208133213-m1og7ey0m990chg6
Tags: 0.3+bzr20-0ubuntu1
* debian/rules:
  - updated to debhelper 7
  - use dh_python2 instead of python-central
* debian/pycompat:
  - removed, no longer needed
* debian/control:
  - dropped cdbs and python-central dependencies
* bzr snapshot of the current trunk

Show diffs side-by-side

added added

removed removed

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