~mvo/software-center/banner-qml

« back to all changes in this revision

Viewing changes to softwarecenter/ui/gtk3/widgets/recommendations.py

  • Committer: Michael Vogt
  • Date: 2012-04-20 09:41:25 UTC
  • mfrom: (2976.4.12 software-center)
  • Revision ID: michael.vogt@ubuntu.com-20120420094125-di9sda80xk7k6nx4
merged lp:~gary-lasker/software-center/recommendations-sso-login-lp973612 with a FIXME for the race condition when the network state changed between the API call and the check in the code

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
from softwarecenter.ui.gtk3.em import StockEms
26
26
from softwarecenter.ui.gtk3.widgets.containers import (FramedHeaderBox,
27
27
                                                       FlowableGrid)
 
28
from softwarecenter.ui.gtk3.utils import get_parent_xid
28
29
from softwarecenter.db.categories import (RecommendedForYouCategory,
29
30
                                          AppRecommendationsCategory)
30
31
from softwarecenter.backend.recagent import RecommenderAgent
 
32
from softwarecenter.backend.login_sso import get_sso_backend
 
33
from softwarecenter.backend.ubuntusso import get_ubuntu_sso_backend
 
34
from softwarecenter.enums import SOFTWARE_CENTER_NAME_KEYRING
31
35
from softwarecenter.utils import utf8
 
36
from softwarecenter.netstatus import network_state_is_connected
32
37
 
33
38
LOG = logging.getLogger(__name__)
34
39
 
150
155
        self.set_header_label(_(u"Recommended For You"))
151
156
        self.recommended_for_you_content = None
152
157
        if self.recommender_agent.is_opted_in():
153
 
            self._update_recommended_for_you_content()
 
158
            self._try_sso_login()
154
159
        else:
155
160
            self._show_opt_in_view()
156
161
 
186
191
        self.opt_in_to_recommendations_service()
187
192
 
188
193
    def opt_in_to_recommendations_service(self):
 
194
        # first we verify the ubuntu sso login/oath status, and if that is good
189
195
        # we upload the user profile here, and only after this is finished
190
196
        # do we fire the request for recommendations and finally display
191
197
        # them here -- a spinner is shown for this process (the spec
192
198
        # wants a progress bar, but we don't have access to real-time
193
199
        # progress info)
194
 
        self._upload_user_profile_and_get_recommendations()
 
200
        self._try_sso_login()
195
201
 
196
202
    def opt_out_of_recommendations_service(self):
197
203
        # tell the backend that the user has opted out
205
211
        self.emit("recommendations-opt-out")
206
212
        self._disconnect_recommender_listeners()
207
213
 
 
214
    def _try_sso_login(self):
 
215
        # display the SSO login dialog if needed
 
216
        # FIXME: consider improving the text in the SSO dialog, for now
 
217
        #        we simply reuse the opt-in text from the panel since we
 
218
        #        are well past string freeze
 
219
        self.spinner_notebook.show_spinner()
 
220
        self.sso = get_sso_backend(get_parent_xid(self),
 
221
                                   SOFTWARE_CENTER_NAME_KEYRING,
 
222
                                   self.RECOMMENDATIONS_OPT_IN_TEXT)
 
223
        self.sso.connect("login-successful", self._maybe_login_successful)
 
224
        self.sso.connect("login-failed", self._login_failed)
 
225
        self.sso.connect("login-canceled", self._login_canceled)
 
226
        self.sso.login_or_register()
 
227
 
 
228
    def _maybe_login_successful(self, sso, oauth_result):
 
229
        self.ssoapi = get_ubuntu_sso_backend()
 
230
        self.ssoapi.connect("whoami", self._whoami_done)
 
231
        self.ssoapi.connect("error", self._whoami_error)
 
232
        # this will automatically verify the keyring token and retrigger
 
233
        # login (once) if its expired
 
234
        self.ssoapi.whoami()
 
235
 
 
236
    def _whoami_done(self, ssologin, result):
 
237
        # we are all squared up with SSO login, now we can proceed with the
 
238
        # recommendations display, or the profile upload if this is an
 
239
        # initial opt-in
 
240
        if self.recommender_agent.is_opted_in():
 
241
            self._update_recommended_for_you_content()
 
242
        else:
 
243
            self._upload_user_profile_and_get_recommendations()
 
244
 
 
245
    def _whoami_error(self, ssologin, e):
 
246
        self.spinner_notebook.hide_spinner()
 
247
        # FIXME: there is a race condition here if the network state changed
 
248
        #        between the call and this check, to fix this properly the
 
249
        #        spawn_helper/piston-generic-helper will need to return
 
250
        #        better error information though
 
251
        if not network_state_is_connected():
 
252
            # if there is an error in the SSO whois, first just check if we
 
253
            # have network access and if we do no, just hide the panel
 
254
            self._hide_recommended_for_you_panel()
 
255
        else:
 
256
            # an error that is not network related indicates that the user's
 
257
            # token has likely been revoked or invalidated on the server, for
 
258
            # this case we want to reset the user's opt-in status
 
259
            self.opt_out_of_recommendations_service()
 
260
        
 
261
    def _login_failed(self, sso):
 
262
        # if the user cancels out of the SSO dialog, reset everything to the
 
263
        # opt-in view state
 
264
        self.spinner_notebook.hide_spinner()
 
265
        self.opt_out_of_recommendations_service()
 
266
 
 
267
    def _login_canceled(self, sso):
 
268
        # if the user cancels out of the SSO dialog, reset everything to the
 
269
        # opt-in view state
 
270
        self.spinner_notebook.hide_spinner()
 
271
        self.opt_out_of_recommendations_service()
 
272
 
208
273
    def _upload_user_profile_and_get_recommendations(self):
209
274
        # initiate upload of the user profile here
210
275
        self._upload_user_profile()