Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/unrefactored/configure.py
blob: d56db690f9c2a20aebe5d6f13853df32d05d4e22 (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
# 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

from ConfigParser import SafeConfigParser as ConfigParser, NoOptionError
import os

from sugar.profile import get_nick_name

import accounts

class ConfigureCanvas(gtk.Button):
    def __init__(self, mailactivity):
        gtk.Button.__init__(self, 'CONFIGURE')

class ConfigureToolbar(gtk.Toolbar):

    def __init__(self, mailactivity):
        gtk.Toolbar.__init__(self)        
        self.mailactivity = mailactivity
        self.canvas = ConfigureCanvas
        
class Configuration:
    
    def __init__(self, cfg_file):        
        self._cfg_file = cfg_file
        
        if not os.path.exists(self._cfg_file): # create it
            open(self._cfg_file, 'w')
        
        self._cp = ConfigParser()
        if not self._cp.read([self._cfg_file]):
            logging.error('ConfigParser: error parsing %s', self._cfg_file)
        
        ### public options ###
        
        # defaults
        self.name = get_nick_name()
        self.sync_every = 10 # minutes
        self.del_on_retr = True
        self.store_account = accounts.DummyStoreAccount()
        self.transport_account = accounts.DummyTransportAccount()
        
        # now parse
        self._parse_profile()
        self._parse_store()
        self._parse_transport()

    def __del__(self):
        fp = open(self._cfg_file, 'w')
        self._cp.write(fp)
        fp.close()

    def _account_dict(self, sec):
        kwds = {}
        fields = ('host', 'port', 'auth_type', 'username', 'password')
        for field in fields:
            try:
                kwds[field] = self._cp.get(sec, field)
            except NoOptionError:
                kwds[field] = None
        kwds['port'] = int(kwds['port'])
        return kwds

    # name, address, sync_every
    def _parse_profile(self):
        sec = 'sending'
        try:
            self.name = self._cp.get(sec, 'name')
            self.address = self._cp.get(sec, 'address')
        except NoOptionError:
            pass

    # transport_account
    def _parse_store(self):
        kwds = _account_dict('store')
        try:
            kwds['del_on_retr'] = self._cp.getboolean('store', 'delete_on_retrieval')
        except NoOptionError:
                kwds[field] = True
        self._store_account = POPStoreAccount(**kwds)

    # store_account
    def _parse_transport(self):
        kwds = _account_dict('transport')
        try:
            kwds['del_on_retr'] = self._cp.getboolean('store', 'delete_on_retrieval')
        except NoOptionError:
            kwds[field] = True
        self._store_account = TransportAccount(**kwds)
    
    @property
    def from_header(self):
        from email.utils import formataddr
        return formataddr((self.name, self.address))