~timo-jyrinki/ubuntu/utopic/rhythmbox/enable_grilo_rhythmbox

« back to all changes in this revision

Viewing changes to plugins/magnatune/MagnatuneAccount.py

  • Committer: Package Import Robot
  • Author(s): Michael Biebl
  • Date: 2012-03-12 12:07:21 UTC
  • mfrom: (1.2.28)
  • Revision ID: package-import@ubuntu.com-20120312120721-ubdep6yl0sgfbr7i
Tags: 2.96-1
* New upstream release.
* Update Build-Depends:
  - Bump libgtk-3-dev to (>= 3.2.0).
  - Drop gir1.2-gtk-3.0, pulled via libgtk-3-dev.
  - Bump libglib2.0-dev (>= 2.28.0).
* Re-add magnatune plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
 
2
#
 
3
# Copyright (C) 2012 Jonathan Matthew  <jonathan@d14n.org>
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 2, or (at your option)
 
8
# any later version.
 
9
#
 
10
# The Rhythmbox authors hereby grant permission for non-GPL compatible
 
11
# GStreamer plugins to be used and distributed together with GStreamer
 
12
# and Rhythmbox. This permission is above and beyond the permissions granted
 
13
# by the GPL license by which Rhythmbox is covered. If you modify this code
 
14
# you may extend this exception to your version of the code, but you are not
 
15
# obligated to do so. If you do not wish to do so, delete this exception
 
16
# statement from your version.
 
17
#
 
18
# This program is distributed in the hope that it will be useful,
 
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
# GNU General Public License for more details.
 
22
#
 
23
# You should have received a copy of the GNU General Public License
 
24
# along with this program; if not, write to the Free Software
 
25
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 
26
 
 
27
from gi.repository import Gio, GnomeKeyring
 
28
 
 
29
__instance = None
 
30
 
 
31
def instance():
 
32
        global __instance
 
33
        if __instance is None:
 
34
                __instance = MagnatuneAccount()
 
35
        return __instance
 
36
 
 
37
class MagnatuneAccount(object):
 
38
        def __init__(self):
 
39
                self.keyring_item = None
 
40
                self.settings = Gio.Settings("org.gnome.rhythmbox.plugins.magnatune")
 
41
 
 
42
                self.keyring_attributes = GnomeKeyring.attribute_list_new()
 
43
                GnomeKeyring.attribute_list_append_string(self.keyring_attributes,
 
44
                                                          "rhythmbox-plugin",
 
45
                                                          "magnatune")
 
46
                (result, items) = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET,
 
47
                                                               self.keyring_attributes)
 
48
                if result == GnomeKeyring.Result.OK and len(items) != 0:
 
49
                        (result, item) = GnomeKeyring.item_get_info_sync(None, items[0].item_id)
 
50
                        if result == GnomeKeyring.Result.OK:
 
51
                                self.keyring_item = item
 
52
                        else:
 
53
                                print "Couldn't get keyring item: " + GnomeKeyring.result_to_message(result)
 
54
                else:
 
55
                        print "couldn't search keyring items: " + GnomeKeyring.result_to_message(result)
 
56
 
 
57
        def get(self):
 
58
                if self.keyring_item is None:
 
59
                        return ('none', None, None)
 
60
 
 
61
                account_type = self.settings['account-type']
 
62
                try:
 
63
                        (username, password) = self.keyring_item.get_secret().split("\n")
 
64
                        return (account_type, username, password)
 
65
                except ValueError:
 
66
                        return ('none', None, None)
 
67
 
 
68
        def update(self, username, password):
 
69
                secret = '\n'.join((username, password))
 
70
                if self.keyring_item is not None:
 
71
                        if secret == self.keyring_item.get_secret():
 
72
                                print "account details not changed"
 
73
                                return
 
74
 
 
75
                (result, id) = GnomeKeyring.item_create_sync(None,
 
76
                                                             GnomeKeyring.ItemType.GENERIC_SECRET,
 
77
                                                             "Rhythmbox: Magnatune account information",
 
78
                                                             self.keyring_attributes,
 
79
                                                             secret,
 
80
                                                             True)
 
81
                if result == GnomeKeyring.Result.OK:
 
82
                        if self.keyring_item is None:
 
83
                                (result, item) = GnomeKeyring.item_get_info_sync(None, id)
 
84
                                if result == GnomeKeyring.Result.OK:
 
85
                                        self.keyring_item = item
 
86
                                else:
 
87
                                        print "couldn't fetch keyring itme: " + GnomeKeyring.result_to_message(result)
 
88
                else:
 
89
                        print "couldn't create keyring item: " + GnomeKeyring.result_to_message(result)