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
|
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007 Johann Prieur <johann.prieur@gmail.com>
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import storage
import scenario
from papyon.service.ContentRoaming.constants import *
from papyon.service.ContentRoaming.scenario import *
from papyon.util.async import *
import gobject
import imghdr
import logging
logger = logging.getLogger("papyon.service.content_roaming")
__all__ = ['ContentRoaming', 'ContentRoamingState', 'ContentRoamingError']
class ContentRoaming(gobject.GObject):
__gsignals__ = {
"error" : (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
(object,)),
}
__gproperties__ = {
"state" : (gobject.TYPE_INT,
"State",
"The state of the addressbook.",
0, 2, ContentRoamingState.NOT_SYNCHRONIZED,
gobject.PARAM_READABLE),
"display-name" : (gobject.TYPE_STRING,
"Display name",
"The user's display name on storage",
"",
gobject.PARAM_READABLE),
"personal-message" : (gobject.TYPE_STRING,
"Personal message",
"The user's personal message on storage",
"",
gobject.PARAM_READABLE),
"display-picture" : (gobject.TYPE_PYOBJECT,
"Display picture",
"The user's display picture on storage",
gobject.PARAM_READABLE)
}
def __init__(self, sso, ab, proxies=None):
"""The content roaming object"""
gobject.GObject.__init__(self)
self._storage = storage.Storage(sso, proxies)
self._ab = ab
self.__state = ContentRoamingState.NOT_SYNCHRONIZED
self.__display_name = ''
self.__personal_message = ''
self.__display_picture = None
self._profile_id = None
self._expression_profile_id = None
self._display_picture_id = None
# Properties
def __get_state(self):
return self.__state
def __set_state(self, state):
self.__state = state
self.notify("state")
state = property(__get_state)
_state = property(__get_state, __set_state)
@property
def display_name(self):
return self.__display_name
@property
def personal_message(self):
return self.__personal_message
@property
def display_picture(self):
return self.__display_picture
### Public API -----------------------------------------------------------
def sync(self):
if self._state != ContentRoamingState.NOT_SYNCHRONIZED:
return
self._state = ContentRoamingState.SYNCHRONIZING
logger.info("Synchronizing")
gp = GetStoredProfileScenario(self._storage,
(self.__get_dn_and_pm_cb,),
(self.__get_display_picture_cb,),
(self.__common_errback,))
gp.cid = self._ab.profile.cid
gp()
def store(self, display_name=None, personal_message=None,
display_picture=None, callback=None, errback=None):
if display_name is None:
display_name = self.__display_name
if personal_message is None:
personal_message = self.__personal_message
picture_type = None
if display_picture is not None:
picture_type = imghdr.what('', display_picture)
if picture_type is None:
picture_type = 'png'
display_picture = ('image/%s' % picture_type, display_picture)
def store_profile_cb():
self.__display_name = display_name
self.__personal_message = personal_message
self.__display_picture = display_picture
run(callback)
logger.info("Store profile (dn='%s', pm='%s', dp='%s')" %
(display_name, personal_message, picture_type))
up = StoreProfileScenario(self._storage,
(store_profile_cb,),
(self.__common_errback, errback),
self._ab.profile.cid,
self._profile_id,
self._expression_profile_id,
self._display_picture_id)
up.display_name = display_name
up.personal_message = personal_message
up.display_picture = display_picture
up()
### End of public API ----------------------------------------------------
# Callbacks
def __get_dn_and_pm_cb(self, profile_id, expression_profile_id,
display_name, personal_message, display_picture_id):
logger.info("Retrieved store profile")
self._profile_id = profile_id
self._expression_profile_id = expression_profile_id
self._display_picture_id = display_picture_id
self.__display_name = display_name
self.notify("display-name")
self.__personal_message = personal_message
self.notify("personal-message")
if self._display_picture_id is None:
self._state = ContentRoamingState.SYNCHRONIZED
def __get_display_picture_cb(self, type, data):
logger.info("Retrieved display picture")
self.__display_picture = (type, data)
self.notify("display-picture")
self._state = ContentRoamingState.SYNCHRONIZED
def __common_errback(self, error, errback, *args):
logger.error("Content Roaming service got error: %s" % str(error))
run(errback, error)
self.emit("error", error)
gobject.type_register(ContentRoaming)
if __name__ == '__main__':
import sys
import getpass
import signal
import gobject
import logging
from papyon.service.SingleSignOn import *
from papyon.service.AddressBook import *
logging.basicConfig(level=logging.DEBUG)
if len(sys.argv) < 2:
account = raw_input('Account: ')
else:
account = sys.argv[1]
if len(sys.argv) < 3:
password = getpass.getpass('Password: ')
else:
password = sys.argv[2]
mainloop = gobject.MainLoop(is_running=True)
signal.signal(signal.SIGTERM,
lambda *args: gobject.idle_add(mainloop.quit()))
def address_book_state_changed(address_book, pspec, sso):
if address_book.state == AddressBookState.SYNCHRONIZED:
def content_roaming_state_changed(cr, pspec):
if cr.state == ContentRoamingState.SYNCHRONIZED:
print "Content roaming service is now synchronized"
# print cr.display_picture
# type, data = cr.display_picture
# path = '/home/jprieur/projects/papyon.rewrite/papyon/service/ContentRoaming/argh.%s' % type.split('/')[1]
# f = open(path, 'w')
# f.write(data)
# path = '/home/jprieur/projects/papyon.rewrite/papyon/service/ContentRoaming/test.jpeg'
# f = open(path, 'r')
# cr.store("Pouet pouet", "Brainy lala brainy...", f.read())
cr = ContentRoaming(sso, address_book)
cr.connect("notify::state", content_roaming_state_changed)
cr.sync()
sso = SingleSignOn(account, password)
address_book = AddressBook(sso)
address_book.connect("notify::state", address_book_state_changed, sso)
address_book.sync()
while mainloop.is_running():
try:
mainloop.run()
except KeyboardInterrupt:
mainloop.quit()
|