forked from albertz/music-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Preferences.py
147 lines (117 loc) · 3.95 KB
/
Preferences.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
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
from utils import UserAttrib, Event, initBy, safe_property
import Traits
class Preferences(object):
def __init__(self):
self._sampleRateStr = None
self.lastFm_update(self.__class__.lastFm)
@UserAttrib(type=Traits.OneLineText, autosizeWidth=True)
@property
def soundDeviceLabel(self):
return "Preferred sound device:"
@UserAttrib(type=Traits.EditableText, autosizeWidth=True, alignRight=True)
def preferredSoundDevice(self, updateText=None):
from State import state
player = state.player
if updateText is not None:
player.preferredSoundDevice = updateText
from appinfo import config
config.preferredSoundDevice = updateText
config.save()
return player.preferredSoundDevice
@UserAttrib(type=Traits.OneLineText, variableWidth=True)
@property
def soundDeviceDescr(self):
return "(Playback needs to be stopped to set this into effect.)"
@UserAttrib(type=Traits.OneLineText, autosizeWidth=True)
@property
def actualSoundDeviceLabel(self):
return "Current sound device:"
@UserAttrib(type=Traits.OneLineText, autosizeWidth=True, withBorder=True, alignRight=True)
@safe_property
@property
def actualSoundDevice(self):
from State import state
return state.player.actualSoundDevice
@UserAttrib(type=Traits.OneLineText, autosizeWidth=True)
@property
def availableSoundDevicesLabel(self):
return "Available sound devices:"
@UserAttrib(type=Traits.Table(keys=("Name",)), autosizeWidth=True, alignRight=True)
@safe_property
@property
def availableSoundDevices(self):
import musicplayer
l = musicplayer.getSoundDevices()
return [{"Name": dev} for dev in l]
@UserAttrib(type=Traits.OneLineText, autosizeWidth=True)
@property
def sampleRateLabel(self):
return "Sample rate in Hz:"
@property
def _sampleRate(self):
from State import state
return state.player.outSamplerate
@UserAttrib(type=Traits.EditableText,
alignRight=True, variableWidth=True,
width=200 # this forces a min-width
)
def sampleRate(self, updateText=None):
if updateText is not None and self._sampleRateStr != updateText:
self._sampleRateStr = updateText
if self._sampleRateStr is not None:
return self._sampleRateStr
rate = str(self._sampleRate / 1000)
return rate + "k"
@sampleRate.setUpdateEvent
@initBy
def sampleRate_updateEvent(self): return Event()
@UserAttrib(type=Traits.Action, name="apply", alignRight=True, variableWidth=False)
def applySampleRate(self):
self.sampleRate_updateEvent.push()
self._sampleRateStr, rate = None, self._sampleRateStr
if rate is None: return
rate = rate.strip()
if rate[-1:] == "k":
factor = 1000
rate = rate[:-1]
else:
factor = 1
try: rate = int(rate) * factor
except Exception: return # no valid integer
# do some very basic check on the number.
# later, our musicplayer module should allow us to check this.
if rate in (44100,48000,88200,96000,176400,192000): pass
else: return
from State import state
state.player.playing = False # can only change that when not playing
state.player.outSamplerate = rate
from appinfo import config
config.sampleRate = rate
config.save()
def lastFm_update(self, attrib):
from appinfo import config
if config.lastFm:
attrib.name = "Last.fm is enabled. Disable it."
else:
attrib.name = "Last.fm is disabled. Enable it."
@UserAttrib(type=Traits.Action, updateHandler=lastFm_update, variableWidth=False)
def lastFm(self):
self.lastFm_updateEvent.push()
from appinfo import config
config.lastFm = not config.lastFm
config.save()
from State import getModule
getModule("tracker_lastfm").reload()
@lastFm.setUpdateEvent
@initBy
def lastFm_updateEvent(self): return Event()
@UserAttrib(type=Traits.Action, alignRight=True, name="reset Last.fm login", variableWidth=False)
def resetLastFm(self):
import lastfm, os, sys
try:
os.remove(lastfm.StoredSession.TOKEN_FILE)
except Exception:
sys.excepthook(*sys.exc_info())
prefs = Preferences()
import gui
gui.registerRootObj(obj=prefs, name="Preferences", priority=-5)