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

import os
import logging
from gettext import gettext as _

import gtk
import gobject

from sugar.graphics import style

from jarabe.controlpanel.sectionview import SectionView

APPLY_TIMEOUT = 1000

class EntryWithLabel(gtk.HBox):
    __gtype_name__ = "SugarEntryWithLabel"

    def __init__(self, label_text):
        gtk.HBox.__init__(self, spacing=style.DEFAULT_SPACING)

        self._timeout_sid = 0
        self._changed_handler = None
        self._is_valid = True

        label = gtk.Label(label_text)
        label.modify_fg(gtk.STATE_NORMAL,
                        style.COLOR_SELECTION_GREY.get_gdk_color())
        self.pack_start(label, True, True)
        label.show()

        self._entry = gtk.Entry(25)
        self._entry.connect('changed', self.__entry_changed_cb)
        self._entry.set_width_chars(25)
        self.pack_start(self._entry, expand=False)
        self._entry.show()

    def __entry_changed_cb(self, widget, data=None):
        if self._timeout_sid:
            gobject.source_remove(self._timeout_sid)
        self._timeout_sid = gobject.timeout_add(APPLY_TIMEOUT, 
                                                self.__timeout_cb)

    def __timeout_cb(self):
        self._timeout_sid = 0

        if self._entry.get_text() == self.get_value():
            return False

        try:
            self.set_value(self._entry.get_text()) 
        except ValueError:
            self._is_valid = False
        else:
            self._is_valid = True

        self.notify('is-valid')

        return False

    def set_text_from_model(self):
        self._entry.set_text(self.get_value()) 

    def get_value(self):
        raise NotImplementedError

    def set_value(self):
        raise NotImplementedError    

    def _get_is_valid(self):
        return self._is_valid
    is_valid = gobject.property(type=bool, getter=_get_is_valid, default=True)

class UsernameEntry(EntryWithLabel):
    def __init__(self, model):
        EntryWithLabel.__init__(self, _('Username:'))
        self._model = model

    def get_value(self):
        return self._model.get_username()

    def set_value(self, username):
        return self._model.set_username(username)

class PasswordEntry(EntryWithLabel):
    def __init__(self, model):
        EntryWithLabel.__init__(self, _('Password:'))
        self._model = model

    def get_value(self):
        return self._model.get_password()

    def set_value(self, password):
        return self._model.set_password(password)

class NumberEntry(EntryWithLabel):
    def __init__(self, model):
        EntryWithLabel.__init__(self, _('Number:'))
        self._model = model

    def get_value(self):
        return self._model.get_number()

    def set_value(self, number):
        return self._model.set_number(number)

class ApnEntry(EntryWithLabel):
    def __init__(self, model):
        EntryWithLabel.__init__(self, _('APN:'))
        self._model = model

    def get_value(self):
        return self._model.get_apn()

    def set_value(self, apn):
        return self._model.set_apn(apn)

class ModemConfiguration(SectionView):
    def __init__(self, model, alerts=None):
        SectionView.__init__(self)

        self._model = model
        self.restart_alerts = alerts

        self.set_border_width(style.DEFAULT_SPACING)
        self.set_spacing(style.DEFAULT_SPACING)

        self._username_entry = UsernameEntry(model)
        self._username_entry.connect('notify::is-valid',
                                     self.__notify_is_valid_cb)
        self.pack_start(self._username_entry, expand=False)
        self._username_entry.show()

        self._password_entry = PasswordEntry(model)
        self._password_entry.connect('notify::is-valid',
                                     self.__notify_is_valid_cb)
        self.pack_start(self._password_entry, expand=False)
        self._password_entry.show()

        self._number_entry = NumberEntry(model)
        self._number_entry.connect('notify::is-valid',
                                   self.__notify_is_valid_cb)
        self.pack_start(self._number_entry, expand=False)
        self._number_entry.show()

        self._apn_entry = ApnEntry(model)
        self._apn_entry.connect('notify::is-valid',
                                self.__notify_is_valid_cb)
        self.pack_start(self._apn_entry, expand=False)
        self._apn_entry.show()

        self.setup()

    def setup(self):
        self._username_entry.set_text_from_model()
        self._password_entry.set_text_from_model()
        self._number_entry.set_text_from_model()
        self._apn_entry.set_text_from_model()

        self.needs_restart = False

    def undo(self):
        self._model.undo()

    def _validate(self):
        if self._username_entry.is_valid and \
            self._password_entry.is_valid and \
                self._number_entry.is_valid and \
                    self._apn_entry.is_valid:
                        self.props.is_valid = True
        else:
            self.props.is_valid = False

    def __notify_is_valid_cb(self, entry, pspec):
        if entry.is_valid:
            self.needs_restart = True
        self._validate()