Debian Codegraph @

xlgui/preferences/collection.py

raw
1 # Copyright (C) 2010 Adam Olsen
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
17 from xl import xdg, main, metadata, settings
18 from xl.nls import gettext as _
19 from xlgui.preferences import widgets
20 from xlgui import progress
21 from xl.common import SimpleProgressThread
22
23 name = _('Collection')
24 icon = 'folder-music'
25 ui = xdg.get_data_path('ui', 'preferences', 'collection.ui')
26
27
28 def _get_default_strip_list():
29 return []
30 # FIXME: currently, this is broken by the backend not also having access
31 # to the default set here, so we will just NOT set one.
32
33 # TRANSLATORS: Grammatical articles that are ignored while sorting the
34 # collection panel. For example, in French locales this could be
35 # the space-separated list "l' la le les".
36 # If this practice is not common in your locale, simply
37 # translate this to string with single space.
38 default_strip_list = _("the")
39 return [v.lower() for v in default_strip_list.split(' ') if v]
40
41
42 class CollectionStripArtistPreference(widgets.ListPreference):
43 default = _get_default_strip_list()
44 name = 'collection/strip_list'
45
46 def __init__(self, preferences, widget):
47 widgets.ListPreference.__init__(self, preferences, widget)
48 self.widget.connect('populate-popup', self._populate_popup_cb)
49
50 def _get_value(self):
51 """
52 Get the value, overrides the base class function
53 because we don't need shlex parsing. We actually
54 want values like "l'" here.
55 """
56 values = [v.lower() for v in self.widget.get_text().split(' ') if v]
57 return values
58
59 def _populate_popup_cb(self, entry, menu):
60 from gi.repository import Gtk
61
62 entry = Gtk.MenuItem.new_with_mnemonic(_('Reset to _Defaults'))
63 entry.connect('activate', self._reset_to_defaults_cb)
64 entry.show()
65
66 sep = Gtk.SeparatorMenuItem()
67 sep.show()
68
69 menu.attach(entry, 0, 1, 0, 1)
70 menu.attach(sep, 0, 1, 1, 2)
71
72 def _reset_to_defaults_cb(self, item):
73 self.widget.set_text(' '.join(_get_default_strip_list()))
74
75
76 class FileBasedCompilationsPreference(widgets.CheckPreference):
77 default = True
78 name = 'collection/file_based_compilations'
79
80
81 class WriteRatingToAudioFileMetadataPreference(widgets.CheckPreference):
82 default = False
83 name = 'collection/write_rating_to_audio_file_metadata'
84
85
86 class WriteRatingToAudioFileMetadataPOPMemail(widgets.Preference, widgets.Conditional):
87 default = ""
88 name = 'collection/write_rating_to_audio_file_metadata_popm_mail'
89 condition_preference_name = 'collection/write_rating_to_audio_file_metadata'
90
91 def __init__(self, preferences, widget):
92 widgets.Preference.__init__(self, preferences, widget)
93 widgets.Conditional.__init__(self)
94
95 def on_check_condition(self):
96 return self.condition_widget.get_active() is True
97
98
99 class WriteRatingToAudioFileMetadataSyncNow(widgets.Button, widgets.Conditional):
100 default = ""
101 name = "collection/write_rating_to_audio_file_metadata_sync_now"
102 condition_preference_name = 'collection/write_rating_to_audio_file_metadata'
103
104 def __init__(self, preferences, widget):
105 widgets.Button.__init__(self, preferences, widget)
106 widgets.Conditional.__init__(self)
107 self.progress = None
108 self.scan_thread = None
109 self.monitor = None
110
111 def on_check_condition(self):
112 return self.condition_widget.get_active() is True
113
114 def on_clicked(self, button):
115 if self.progress:
116 return
117
118 curr_page = self.preferences.last_page
119 box = self.preferences.builders[curr_page].get_object(
120 'collection/write_rating_to_audio_file_metadata_progress'
121 )
122 self.progress = progress.ProgressManager(box)
123
124 self.scan_thread = SimpleProgressThread(
125 self.track_scan,
126 )
127 self.scan_thread.connect('done', self.on_done)
128 self.monitor = self.progress.add_monitor(
129 self.scan_thread,
130 _("Migrating ratings to audio file metadata"),
131 'document-open',
132 )
133
134 box.show()
135
136 def track_scan(self):
137 exaile = main.exaile()
138 collection = exaile.collection
139 total = len(collection.tracks)
140 i = 0
141
142 for track in collection.tracks:
143 trax = collection.tracks[track]
144 format_data = metadata.get_format(track)
145
146 if '__rating' in format_data.tag_mapping:
147 rating = trax.get_rating()
148 if rating > 0:
149 trax.set_rating(rating)
150
151 i += 1
152 yield i, total
153
154 def on_done(self, a):
155 self.progress.remove_monitor(self.monitor)
156 self.progress = None
157
158
159 class UseLegacyMetadataMappingPreference(widgets.CheckPreference):
160 default = False
161 name = 'collection/use_legacy_metadata_mapping'
162
163
164 class UseLegacyMetadataMappingSyncNow(widgets.Button, widgets.Conditional):
165 default = ""
166 name = "collection/use_legacy_metadata_mapping_sync_now"
167 condition_preference_name = 'collection/use_legacy_metadata_mapping'
168
169 def __init__(self, preferences, widget):
170 widgets.Button.__init__(self, preferences, widget)
171 widgets.Conditional.__init__(self)
172 self.progress = None
173 self.scan_thread = None
174 self.monitor = None
175
176 def on_check_condition(self):
177 return self.condition_widget.get_active() is True
178
179 def on_clicked(self, button):
180 if self.progress:
181 return
182
183 curr_page = self.preferences.last_page
184 box = self.preferences.builders[curr_page].get_object(
185 'collection/use_legacy_metadata_mapping_progress'
186 )
187 self.progress = progress.ProgressManager(box)
188
189 self.scan_thread = SimpleProgressThread(
190 self.track_scan,
191 )
192 self.scan_thread.connect('done', self.on_done)
193 self.monitor = self.progress.add_monitor(
194 self.scan_thread,
195 _("Migrating metadata"),
196 'document-open',
197 )
198
199 box.show()
200
201 def track_scan(self):
202 from xl.metadata import flac, ogg
203
204 exaile = main.exaile()
205 collection = exaile.collection
206 total = len(collection.tracks)
207 i = 0
208
209 for track in collection.tracks:
210 trax = collection.tracks[track]
211 format_data = metadata.get_format(track)
212
213 if not isinstance(format_data, flac.FlacFormat) and not isinstance(
214 format_data, ogg.OggFormat
215 ):
216 continue
217
218 settings.set_option('collection/use_legacy_metadata_mapping', True)
219 bpm = trax.get_tag_disk('tempo')
220 comment = trax.get_tag_disk('description')
221
222 settings.set_option('collection/use_legacy_metadata_mapping', False)
223 trax.set_tag_disk('bpm', bpm)
224 trax.set_tag_disk('comment', comment)
225
226 i += 1
227 yield i, total
228
229 def on_done(self, a):
230 self.progress.remove_monitor(self.monitor)
231 self.progress = None
232 settings.set_option('collection/use_legacy_metadata_mapping', False)
233 self.condition_widget.set_active(False)
234
235
236 # vim:ts=4 et sw=4