Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/unrefactored/tracker.py
blob: 7d0828b0f5ecc7124e87ba90b2f31fffab719b0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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 General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import email
import email.utils
import gtk

from gettext import gettext as _
from sugar.graphics.icon import Icon
from sugar.graphics import alert

from tags import FLAGS, HARDCODED

def ugly_hack(self, papa, pspec, value):
    if pspec.name=='msg':
        if self._msg != value:
            self._msg = value
            self._msg_label.set_markup(self._msg)
    else:
        papa.do_set_property(self, pspec, value)

class ProgressAlert(alert.Alert):
    
    def __init__(self, *args, **kwds):
        alert.Alert.__init__(self, *args, **kwds)
        icon = Icon(icon_name='emblem-busy')
        self.props.icon = icon
        icon.show()
    
    do_set_property = lambda self, pspec, value: ugly_hack(self, alert.Alert, pspec, value)

class ErrorAlert(alert.NotifyAlert):
    
    def __init__(self, *args, **kwds):
        alert.NotifyAlert.__init__(self, *args, **kwds)
        icon = Icon(icon_name='emblem-notification')
        self.props.icon = icon
        icon.show()
    
    do_set_property = lambda self, pspec, value: ugly_hack(self, alert.NotifyAlert, pspec, value)

class ProgressTracker(object):

    def __init__(self, activity, title):
        self._activity = activity
        self._title = title
        self._alert = ProgressAlert()

        self._alert.props.title = title
        self._activity.add_alert(self._alert)
        
    def _remove_alert(self, *args):
        gtk.gdk.threads_enter()
        self._activity.remove_alert(self._alert)
        gtk.gdk.threads_leave()

    def done(self):
        self._remove_alert()

    def update(self, msg):
        gtk.gdk.threads_enter()
        self._alert.props.msg = msg
        gtk.gdk.threads_leave()

    def error(self, msg, remove_old=True):
        if remove_old: self._remove_alert()
        gtk.gdk.threads_enter()
        notify(self._activity, self._title, msg)
        gtk.gdk.threads_leave()

class InboundTracker(ProgressTracker):
    
    def __init__(self, activity):
        ProgressTracker.__init__(self, activity, _('Checking email'))
    
    def dump_msg(self, msg_str):
        # TODO setting of FLAGS{'has_attachment'], filtering(!)
        msg = email.message_from_string(msg_str)
        ms = self._activity.ms
        key = ms.add_msg(msg)
        # gmail sent emails hack
        if email.utils.parseaddr(msg['From'])[1]==self._activity.config.transport_account._from_addr:
            ms.flag(key, FLAGS['sent'])
        else:
            ms.associate(HARDCODED['inbox'], key)

class OutboundTracker(ProgressTracker):
    
    def __init__(self, activity):
        ProgressTracker.__init__(self, activity, _('Sending email'))

    def _add_and_flag(self, msg, flag):
        ms = self._activity.ms
        key = ms.add_msg(msg)
        ms.flag(key, flag)

    def try_later(self, msgs):
        for msg in msgs:
            self._add_and_flag(msg, FLAGS['outbound'])
    
    def error_delivering(self, msg):
        self._add_and_flag(msg, FLAGS['draft'])
        ProgressTracker.error(self, _('Error delivering <i>%s</i>, message saved as draft.' % msg['Subject']), remove_old=False)
        
    def some_rcpts_failed(self, msg, who):
        msg['To'] = '; '.join(who)
        self._add_and_flag(msg, FLAGS['draft'])
        ProgressTracker.error(self, _('Error delivering <i></i> to %s; saved as draft.' % ', '.join(who)), remove_old=False)
    
    def sent(self, msg):
        self._add_and_flag(msg, FLAGS['sent'])

def notify(activity, title, msg, timeout=5):
    alert = ErrorAlert(timeout)
    alert.props.title = title
    alert.props.msg = msg
    activity.add_alert(alert)
    alert.connect('response', lambda x,y: activity.remove_alert(alert))