1# Copyright (C) 2011 Mathias Brodala
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2, or (at your option)
6# any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16
17from gi.repository import Gdk
18from gi.repository import GLib
19from gi.repository import Gtk
20
21import os.path
22
23import pylast
24
25from xl.nls import gettext as _
26from xl import common, settings
27from xlgui import icons
28from xlgui.preferences import widgets
29from xlgui.widgets import dialogs
30
31name = _('Last.fm Loved Tracks')
32basedir = os.path.dirname(os.path.realpath(__file__))
33ui = os.path.join(basedir, "lastfmlove_preferences.ui")
34icons.MANAGER.add_icon_name_from_directory('lastfm', os.path.join(basedir, 'icons'))
35icon = 'lastfm'
36
37
38class APIKeyPreference(widgets.Preference):
39    name = 'plugin/lastfmlove/api_key'
40
41
42class APISecretPrefence(widgets.Preference):
43    name = 'plugin/lastfmlove/api_secret'
44
45
46class RequestAccessPermissionButton(widgets.Button):
47    name = 'plugin/lastfmlove/request_access_permission'
48
49    def __init__(self, preferences, widget):
50        """
51        Sets up the message
52        """
53        widgets.Button.__init__(self, preferences, widget)
54
55        self.message = dialogs.MessageBar(
56            parent=preferences.builder.get_object('preferences_box'),
57            buttons=Gtk.ButtonsType.CLOSE,
58        )
59        self.errors = {pylast.STATUS_INVALID_API_KEY: _('The API key is invalid.')}
60
61    @common.threaded
62    def check_connection(self):
63        """
64        Checks API key and secret for validity
65        and opens the URI for access permission
66        """
67        api_key = settings.get_option('plugin/lastfmlove/api_key', 'K')
68
69        try:
70            pylast.LastFMNetwork(
71                api_key=api_key,
72                api_secret=settings.get_option('plugin/lastfmlove/api_secret', 'S'),
73                username=settings.get_option('plugin/ascrobbler/user', ''),
74                password_hash=settings.get_option('plugin/ascrobbler/password', ''),
75            )
76        except pylast.WSError as e:
77            GLib.idle_add(
78                self.message.show_error,
79                self.errors[int(e.get_id())],
80                _('Please make sure the entered data is correct.'),
81            )
82        else:
83            application_launched = Gtk.show_uri(
84                Gdk.Screen.get_default(),
85                'http://www.last.fm/api/auth?api_key={0}'.format(api_key),
86                Gdk.CURRENT_TIME,
87            )
88
89            if not application_launched:
90                url = 'http://www.last.fm/api/auth?api_key={0}'.format(api_key)
91                GLib.idle_add(
92                    self.message.show_warning,
93                    _('Could not start web browser'),
94                    _(
95                        'Please copy the following URL and '
96                        'open it with your web browser:\n'
97                        '<b><a href="{url}">{url}</a></b>'
98                    ).format(url=url),
99                )
100
101    def on_clicked(self, button):
102        """
103        Initiates the check for validity
104        """
105        self.check_connection()
106