Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/extensions/cpsection/modemconfiguration/model.py
blob: 73b9c636e8930998ceed15b0befe93bd88fe46a5 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Copyright (C) 2009 Paraguay Educa, Martin Abente
#
# 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  US

from __future__ import with_statement
import gconf
import gtk
import os
import locale
import logging

from xml.etree.cElementTree import ElementTree
from gettext import gettext as _

from jarabe.model.network import GSM_USERNAME_PATH, GSM_PASSWORD_PATH, \
                                 GSM_NUMBER_PATH, GSM_APN_PATH, GSM_PIN_PATH, \
                                 GSM_PUK_PATH

from cpsection.modemconfiguration.config import PROVIDERS_PATH, \
                                                PROVIDERS_FORMAT_SUPPORTED, \
                                                COUNTRY_CODES_PATH

_country_code, _encoding = locale.getdefaultlocale()
if _country_code is not None:
    _tags = _country_code.lower().split('_')
    LANG = _tags[0]
    if len(_tags) > 1 and len(_tags[1]) == 2:
        COUNTRY_CODE = _tags[1]
    else:
        COUNTRY_CODE = None
else:
    LANG = None
    COUNTRY_CODE = None

LANG_NS_ATTR = '{http://www.w3.org/XML/1998/namespace}lang'

def get_username():
    client = gconf.client_get_default()
    return client.get_string(GSM_USERNAME_PATH) or ''

def get_password():
    client = gconf.client_get_default()
    return client.get_string(GSM_PASSWORD_PATH) or ''

def get_number():
    client = gconf.client_get_default()
    return client.get_string(GSM_NUMBER_PATH) or ''

def get_apn():
    client = gconf.client_get_default()
    return client.get_string(GSM_APN_PATH) or ''

def get_pin():
    client = gconf.client_get_default()
    return client.get_string(GSM_PIN_PATH) or ''

def get_puk():
    client = gconf.client_get_default()
    return client.get_string(GSM_PUK_PATH) or ''

def set_username(username):
    client = gconf.client_get_default()
    client.set_string(GSM_USERNAME_PATH, username)

def set_password(password):
    client = gconf.client_get_default()
    client.set_string(GSM_PASSWORD_PATH, password)

def set_number(number):
    client = gconf.client_get_default()
    client.set_string(GSM_NUMBER_PATH, number)

def set_apn(apn):
    client = gconf.client_get_default()
    client.set_string(GSM_APN_PATH, apn)

def set_pin(pin):
    client = gconf.client_get_default()
    client.set_string(GSM_PIN_PATH, pin)

def set_puk(puk):
    client = gconf.client_get_default()
    client.set_string(GSM_PUK_PATH, puk)

def has_providers_db():
    if not os.path.isfile(COUNTRY_CODES_PATH):
        logging.debug("Mobile broadband provider database: Country " \
                          "codes path %s not found.", COUNTRY_CODES_PATH)
        return False
    try:
        tree = ElementTree(file=PROVIDERS_PATH)
    except (IOError, SyntaxError), e:
        logging.debug("Mobile broadband provider database: Could not read " \
                          "provider information %s error=%s", PROVIDERS_PATH)
        return False
    else:
        elem = tree.getroot()
        if elem is None or elem.get('format') != PROVIDERS_FORMAT_SUPPORTED:
            logging.debug("Mobile broadband provider database: Could not " \
                          "read provider information. %s is wrong format.",
                          elem.get('format'))
            return False
        return True


class CountryListStore(gtk.ListStore):
    def __init__(self):
        gtk.ListStore.__init__(self, str, object)
        codes = {}
        with open(COUNTRY_CODES_PATH) as codes_file:
            for line in codes_file:
                if line.startswith('#'):
                    continue
                code, name = line.split('\t')[:2]
                codes[code.lower()] = name.strip()
        etree = ElementTree(file=PROVIDERS_PATH).getroot()
        self._country_idx = None
        i = 0
        for elem in etree.findall('.//country'):
            code = elem.attrib['code']
            if code == COUNTRY_CODE:
                self._country_idx = i
            else:
                i += 1
            if code in codes:
                self.append((codes[code], elem))
            else:
                self.append((code, elem))

    def get_row_providers(self, row):
        return self[row][1]

    def guess_country_row(self):
        if self._country_idx is not None:
            return self._country_idx
        else:
            return -1

class ProviderListStore(gtk.ListStore):
    def __init__(self, elem):
        gtk.ListStore.__init__(self, str, object)
        for provider_elem in elem.findall('.//provider'):
            apns = provider_elem.findall('.//apn')
            if not apns:
                # Skip carriers with CDMA entries only
                continue
            names = provider_elem.findall('.//name')
            for name in names:
                if name.get(LANG_NS_ATTR) is None:
                    # serviceproviders.xml default value
                    provider_name = name.text
                elif name.get(LANG_NS_ATTR) == LANG:
                    # Great! We found a name value for our locale!
                    provider_name = name.text
                    break
            self.append((provider_name, apns))

    def get_row_plans(self, row):
        return self[row][1]

class PlanListStore(gtk.ListStore):
    DEFAULT_NUMBER = '*99#'

    def __init__(self, elems):
        gtk.ListStore.__init__(self, str, object)
        for apn_elem in elems:
            plan = {}
            names = apn_elem.findall('.//name')
            if names:
                for name in names:
                    if name.get(LANG_NS_ATTR) is None:
                        plan['name'] = name.text
                    elif name.get(LANG_NS_ATTR) == LANG:
                        plan['name'] = name.text
                        break
            else:
                plan['name'] = _('Default')
            plan['apn'] = apn_elem.get('value')
            user = apn_elem.find('.//username')
            if user is not None:
                plan['username'] = user.text
            else:
                plan['username'] = ''
            passwd = apn_elem.find('.//password')
            if passwd is not None:
                plan['password'] = passwd.text
            else:
                plan['password'] = ''

            plan['number'] = self.DEFAULT_NUMBER

            self.append((plan['name'], plan))

    def get_row_plan(self, row):
        return self[row][1]