forked from BramBonne/LastfmPlaycount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlastfmplaycountconfig.py
118 lines (98 loc) · 4.16 KB
/
lastfmplaycountconfig.py
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
from configparser import RawConfigParser
from os import path
import rb
from gi.repository import GObject, Gtk, Gio, PeasGtk
DCONF_DIR = 'org.gnome.rhythmbox.plugins.lastfmplaycount'
class Config(GObject.GObject, PeasGtk.Configurable):
"""
Read and write configuration data for Last.fm playcount sync plugin
"""
__gtype_name__ = 'LastfmplaycountConfig'
def __init__(self):
self._parse_username()
self.settings = Gio.Settings(DCONF_DIR)
def do_create_configure_widget(self):
"""
Called when the configuration UI button is pressed
"""
print("Creating configuration dialog")
builder = Gtk.Builder()
builder.add_from_file(rb.find_plugin_file(self, "lastfmplaycount-prefs.ui"))
dialog = builder.get_object('lastfmplaycountsync-preferences')
if self.get_username() is not None:
builder.get_object('username').set_markup('Detected username: ' + self.get_username())
builder.get_object('update_playcounts').set_active(self.get_update_playcounts())
builder.get_object('update_ratings').set_active(self.get_update_ratings())
builder.get_object('loved_rating').set_range(0,5)
builder.get_object('loved_rating').set_value(5)
builder.get_object('rating_box').set_sensitive(False)
callbacks = {
"update_playcounts_toggled" : self._update_playcounts_toggled,
"update_ratings_toggled" : self._update_ratings_toggled,
}
builder.connect_signals(callbacks)
return dialog
def get_username(self):
"""
@return the user's Last.fm username
"""
if not hasattr(self, '_username') or self._username is None:
# If the username was not filled in before, check if it is now
self._username = self._parse_username()
return self._username
def get_update_playcounts(self):
"""
@return Whether the user has specified that he wants his playcounts updated
"""
return self.settings["update-playcounts"]
def set_update_playcounts(self, update):
"""
Sets whether the user wants his playcounts to be updated
@param update True if the user wants his playcounts to be updated
"""
print("Setting updating of playcounts to %r" % update)
self.settings["update-playcounts"] = update
def get_update_ratings(self):
"""
@return Whether the user has specified that he wants his ratings updated
"""
return self.settings["update-ratings"]
def set_update_ratings(self, update):
"""
Sets whether the user wants his ratings to be updated
@param update True if the user wants his ratings to be updated
"""
print( "Setting updating of ratings to %r" % update)
self.settings["update-ratings"] = update
def _parse_username(self):
"""
Get the username from the session file of rhythmbox' audioscrobbler
plugin as per http://mail.gnome.org/archives/rhythmbox-devel/2011-December/msg00029.html
"""
username_config_parser = RawConfigParser()
# Expanduser expands '~' into '/home/<username>/'
as_session = open(path.expanduser('~/.local/share/rhythmbox/audioscrobbler/sessions'), 'r')
username_config_parser.readfp(as_session)
try:
username = username_config_parser.get('Last.fm', 'username')
print("Parsed Last.fm username: %s" % username)
self._username = username
except:
print("Error: last.fm sessions file could not be parsed. Username set to 'None'")
self._username = None
def _update_playcounts_toggled(self, widget):
"""
Callback function
@param widget The widget containing the toggle button
"""
enabled = widget.get_active()
print(enabled)
self.set_update_playcounts(enabled)
def _update_ratings_toggled(self, widget):
"""
Callback function
@param widget The widget containing the toggle button
"""
enabled = widget.get_active()
print(enabled)
self.set_update_ratings(enabled)