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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
# Copyright (C) 2007, Red Hat, Inc.
#
# 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 os
import os.path
import logging
from gettext import gettext as _
import pwd
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
from gi.repository import GConf
from sugar3 import env
from sugar3 import profile
from sugar3.graphics import style
from sugar3.graphics.icon import Icon
from sugar3.graphics.xocolor import XoColor
from jarabe.intro import colorpicker
def create_profile(name, age, color=None):
if not color:
color = XoColor()
client = GConf.Client.get_default()
client.set_string('/desktop/sugar/user/nick', name)
# Algorithm to generate the timestamp of the birthday of the
# XO-user ::
#
# timestamp = current_timestamp - [age * (365 * 24 * 60 * 60)]
#
# Note that, this timestamp may actually (in worst-case) be
# off-target by 1 year, but that is ok, since we want an
# "approximate" age of the XO-user (for statistics-collection).
import time
current_timestamp = time.time()
xo_user_age_as_timestamp = int(age) * 365 * 24 * 60 * 60
approx_timestamp_at_user_birthday = current_timestamp - xo_user_age_as_timestamp
client.set_int('/desktop/sugar/user/birth_timestamp', int(approx_timestamp_at_user_birthday))
# Done.
client.set_string('/desktop/sugar/user/color', color.to_string())
client.suggest_sync()
if profile.get_pubkey() and profile.get_profile().privkey_hash:
logging.info('Valid key pair found, skipping generation.')
return
# Generate keypair
import commands
keypath = os.path.join(env.get_profile_path(), 'owner.key')
if os.path.exists(keypath):
os.rename(keypath, keypath + '.broken')
logging.warning('Existing private key %s moved to %s.broken',
keypath, keypath)
if os.path.exists(keypath + '.pub'):
os.rename(keypath + '.pub', keypath + '.pub.broken')
logging.warning('Existing public key %s.pub moved to %s.pub.broken',
keypath, keypath)
logging.debug("Generating user keypair")
cmd = "ssh-keygen -q -t dsa -f %s -C '' -N ''" % (keypath, )
(s, o) = commands.getstatusoutput(cmd)
if s != 0:
logging.error('Could not generate key pair: %d %s', s, o)
logging.debug("User keypair generated")
class _Page(Gtk.VBox):
__gproperties__ = {
'valid': (bool, None, None, False, GObject.PARAM_READABLE),
}
def __init__(self):
Gtk.VBox.__init__(self)
self.valid = False
def set_valid(self, valid):
self.valid = valid
self.notify('valid')
def do_get_property(self, pspec):
if pspec.name == 'valid':
return self.valid
def activate(self):
pass
class _NamePage(_Page):
def __init__(self, intro):
_Page.__init__(self)
self._intro = intro
alignment = Gtk.Alignment.new(0.5, 0.5, 0, 0)
self.pack_start(alignment, expand=True, fill=True, padding=0)
hbox = Gtk.HBox(spacing=style.DEFAULT_SPACING)
alignment.add(hbox)
label = Gtk.Label(label=_('Name:'))
hbox.pack_start(label, False, True, 0)
self._entry = Gtk.Entry()
self._entry.connect('notify::text', self._text_changed_cb)
self._entry.set_size_request(style.zoom(300), -1)
self._entry.set_max_length(45)
hbox.pack_start(self._entry, False, True, 0)
def _text_changed_cb(self, entry, pspec):
valid = len(entry.props.text.strip()) > 0
self.set_valid(valid)
def get_name(self):
return self._entry.props.text
def set_name(self, new_name):
self._entry.props.text = new_name
def activate(self):
self._entry.grab_focus()
class _AgePage(_Page):
def __init__(self, intro):
_Page.__init__(self)
self._intro = intro
self._max_age = 100
alignment = Gtk.Alignment.new(0.5, 0.5, 0, 0)
self.pack_start(alignment, expand=True, fill=True, padding=0)
hbox = Gtk.HBox(spacing=style.DEFAULT_SPACING)
alignment.add(hbox)
label = Gtk.Label(_('Age:'))
hbox.pack_start(label, False, True, 0)
adjustment = Gtk.Adjustment(0, 0, self._max_age, 1, 0, 0)
self._entry = Gtk.SpinButton(adjustment=adjustment)
self._entry.props.editable = True
self._entry.connect('notify::text', self._text_changed_cb)
self._entry.set_max_length(15)
hbox.pack_start(self._entry, False, True, 0)
label = Gtk.Label(_('years'))
hbox.pack_start(label, False, True, 0)
def _text_changed_cb(self, entry, pspec):
valid = False
if entry.props.text.isdigit():
int_value = int(entry.props.text)
valid = ((int_value > 0) and (int_value <= self._max_age))
self.set_valid(valid)
def get_age(self):
return int(self._entry.props.text)
def activate(self):
self._entry.grab_focus()
class _ColorPage(_Page):
def __init__(self):
_Page.__init__(self)
vbox = Gtk.VBox(spacing=style.DEFAULT_SPACING)
self.pack_start(vbox, expand=True, fill=False, padding=0)
self._label = Gtk.Label(label=_('Click to change color:'))
vbox.pack_start(self._label, True, True, 0)
self._cp = colorpicker.ColorPicker()
vbox.pack_start(self._cp, True, True, 0)
self._color = self._cp.get_color()
self.set_valid(True)
def get_color(self):
return self._cp.get_color()
class _IntroBox(Gtk.VBox):
__gsignals__ = {
'done': (GObject.SignalFlags.RUN_FIRST, None,
([GObject.TYPE_PYOBJECT, GObject.TYPE_PYOBJECT, GObject.TYPE_PYOBJECT])),
}
PAGE_NAME = 0
PAGE_AGE = 1
PAGE_COLOR = 2
PAGE_FIRST = PAGE_NAME
PAGE_LAST = PAGE_COLOR
def __init__(self):
Gtk.VBox.__init__(self)
self.set_border_width(style.zoom(30))
self._page = self.PAGE_NAME
self._name_page = _NamePage(self)
self._age_page = _AgePage(self)
self._color_page = _ColorPage()
self._current_page = None
self._next_button = None
client = GConf.Client.get_default()
default_nick = client.get_string('/desktop/sugar/user/default_nick')
if default_nick != 'disabled':
self._page = self.PAGE_COLOR
if default_nick == 'system':
pwd_entry = pwd.getpwuid(os.getuid())
default_nick = (pwd_entry.pw_gecos.split(',')[0] or
pwd_entry.pw_name)
self._name_page.set_name(default_nick)
self._setup_page()
def _setup_page(self):
for child in self.get_children():
self.remove(child)
if self._page == self.PAGE_NAME:
self._current_page = self._name_page
if self._page == self.PAGE_AGE:
self._current_page = self._age_page
elif self._page == self.PAGE_COLOR:
self._current_page = self._color_page
self.pack_start(self._current_page, True, True, 0)
button_box = Gtk.HButtonBox()
if self._page == self.PAGE_FIRST:
button_box.set_layout(Gtk.ButtonBoxStyle.END)
else:
button_box.set_layout(Gtk.ButtonBoxStyle.EDGE)
back_button = Gtk.Button(_('Back'))
image = Icon(icon_name='go-left')
back_button.set_image(image)
back_button.connect('clicked', self._back_activated_cb)
button_box.pack_start(back_button, True, True, 0)
self._next_button = Gtk.Button()
image = Icon(icon_name='go-right')
self._next_button.set_image(image)
if self._page == self.PAGE_LAST:
self._next_button.set_label(_('Done'))
self._next_button.connect('clicked', self._done_activated_cb)
else:
self._next_button.set_label(_('Next'))
self._next_button.connect('clicked', self._next_activated_cb)
self._current_page.activate()
self._update_next_button()
button_box.pack_start(self._next_button, True, True, 0)
self._current_page.connect('notify::valid',
self._page_valid_changed_cb)
self.pack_start(button_box, False, True, 0)
self.show_all()
def _update_next_button(self):
self._next_button.set_sensitive(self._current_page.props.valid)
def _page_valid_changed_cb(self, page, pspec):
self._update_next_button()
def _back_activated_cb(self, widget):
self.back()
def back(self):
if self._page != self.PAGE_FIRST:
self._page -= 1
self._setup_page()
def _next_activated_cb(self, widget):
self.next()
def next(self):
if self._page == self.PAGE_LAST:
self.done()
if self._current_page.props.valid:
self._page += 1
self._setup_page()
def _done_activated_cb(self, widget):
self.done()
def done(self):
name = self._name_page.get_name()
age = self._age_page.get_age()
color = self._color_page.get_color()
self.emit('done', name, age, color)
class IntroWindow(Gtk.Window):
__gtype_name__ = 'SugarIntroWindow'
__gsignals__ = {
'done': (GObject.SignalFlags.RUN_FIRST, None, ([])),
}
def __init__(self):
Gtk.Window.__init__(self)
self.props.decorated = False
self.maximize()
self._intro_box = _IntroBox()
self._intro_box.connect('done', self._done_cb)
self.add(self._intro_box)
self._intro_box.show()
self.connect('key-press-event', self.__key_press_cb)
def _done_cb(self, box, name, age, color):
self.hide()
GObject.idle_add(self._create_profile_cb, name, age, color)
def _create_profile_cb(self, name, age, color):
create_profile(name, age, color)
self.emit("done")
return False
def __key_press_cb(self, widget, event):
if Gdk.keyval_name(event.keyval) == 'Return':
self._intro_box.next()
return True
elif Gdk.keyval_name(event.keyval) == 'Escape':
self._intro_box.back()
return True
return False
if __name__ == '__main__':
w = IntroWindow()
w.show()
w.connect('destroy', Gtk.main_quit)
Gtk.main()
|