| 1 |
# Copyright (C) 2008-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 |
# |
| 18 |
# The developers of the Exaile media player hereby grant permission |
| 19 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 20 |
# distributed together with GStreamer and Exaile. This permission is |
| 21 |
# above and beyond the permissions granted by the GPL license by which |
| 22 |
# Exaile is covered. If you modify this code, you may extend this |
| 23 |
# exception to your version of the code, but you are not obligated to |
| 24 |
# do so. If you do not wish to do so, delete this exception statement |
| 25 |
# from your version. |
| 26 |
|
| 27 |
from gi.repository import Gtk |
| 28 |
|
| 29 |
from xl import main, playlist, settings |
| 30 |
|
| 31 |
from xl.nls import gettext as _ |
| 32 |
|
| 33 |
from . import dialogs |
| 34 |
from .filter import ( |
| 35 |
EntryField, |
| 36 |
FilterDialog, |
| 37 |
ComboEntryField, |
| 38 |
MultiEntryField, |
| 39 |
NullField, |
| 40 |
QuotedEntryField, |
| 41 |
SpinButtonAndComboField, |
| 42 |
SpinLabelField, |
| 43 |
) |
| 44 |
|
| 45 |
import logging |
| 46 |
|
| 47 |
logger = logging.getLogger(__name__) |
| 48 |
|
| 49 |
|
| 50 |
def N_(x): |
| 51 |
return x |
| 52 |
|
| 53 |
|
| 54 |
class EntrySecondsField(MultiEntryField): |
| 55 |
def __init__(self): |
| 56 |
MultiEntryField.__init__(self, (50, _('seconds'))) |
| 57 |
|
| 58 |
|
| 59 |
class EntryAndEntryField(MultiEntryField): |
| 60 |
def __init__(self): |
| 61 |
# TRANSLATORS: Logical AND used for smart playlists |
| 62 |
MultiEntryField.__init__(self, (50, _('and'), 50)) |
| 63 |
|
| 64 |
|
| 65 |
class EntryDaysField(MultiEntryField): |
| 66 |
def __init__(self): |
| 67 |
MultiEntryField.__init__(self, (50, _('days'))) |
| 68 |
|
| 69 |
|
| 70 |
class PlaylistField(ComboEntryField): |
| 71 |
def __init__(self): |
| 72 |
playlists = [] |
| 73 |
playlists.extend(main.exaile().smart_playlists.list_playlists()) |
| 74 |
playlists.extend(main.exaile().playlists.list_playlists()) |
| 75 |
playlists.sort() |
| 76 |
ComboEntryField.__init__(self, playlists) |
| 77 |
|
| 78 |
|
| 79 |
DATE_FIELDS = [N_('seconds'), N_('minutes'), N_('hours'), N_('days'), N_('weeks')] |
| 80 |
|
| 81 |
|
| 82 |
class SpinDateField(SpinButtonAndComboField): |
| 83 |
def __init__(self): |
| 84 |
SpinButtonAndComboField.__init__(self, DATE_FIELDS) |
| 85 |
|
| 86 |
|
| 87 |
class SpinSecondsField(SpinLabelField): |
| 88 |
def __init__(self): |
| 89 |
SpinLabelField.__init__(self, _('seconds')) |
| 90 |
|
| 91 |
|
| 92 |
class SpinRating(SpinLabelField): |
| 93 |
def __init__(self): |
| 94 |
SpinLabelField.__init__(self, '', settings.get_option("rating/maximum", 5), 0) |
| 95 |
|
| 96 |
|
| 97 |
class SpinNothing(SpinLabelField): |
| 98 |
def __init__(self): |
| 99 |
SpinLabelField.__init__(self, '') |
| 100 |
|
| 101 |
|
| 102 |
# This sets up the CRITERIA for all the available types of tags |
| 103 |
# that exaile supports. The actual CRITERIA dict is populated |
| 104 |
# using xl.metadata.tags.tag_data. |
| 105 |
# |
| 106 |
# NOTE: The following strings are already marked for translation in _TRANS and |
| 107 |
# _NMAP, and will be really translated by filtergui; no need to clutter the |
| 108 |
# code here. |
| 109 |
_criteria_types = { |
| 110 |
# fmt: off |
| 111 |
|
| 112 |
# TODO |
| 113 |
'bitrate': [ |
| 114 |
('is', SpinNothing), |
| 115 |
('less than', SpinNothing), |
| 116 |
('greater than', SpinNothing), |
| 117 |
('between', EntryAndEntryField), |
| 118 |
('at least', SpinNothing), |
| 119 |
('at most', SpinNothing), |
| 120 |
('is set', NullField), |
| 121 |
('is not set', NullField), |
| 122 |
], |
| 123 |
|
| 124 |
'image': None, |
| 125 |
|
| 126 |
'int': [ |
| 127 |
('is', SpinNothing), |
| 128 |
('less than', SpinNothing), |
| 129 |
('greater than', SpinNothing), |
| 130 |
('between', EntryAndEntryField), |
| 131 |
('at least', SpinNothing), |
| 132 |
('at most', SpinNothing), |
| 133 |
('is set', NullField), |
| 134 |
('is not set', NullField), |
| 135 |
], |
| 136 |
|
| 137 |
'location': [ |
| 138 |
('is', QuotedEntryField), |
| 139 |
('is not', QuotedEntryField), |
| 140 |
('contains', QuotedEntryField), |
| 141 |
('does not contain', QuotedEntryField), |
| 142 |
('regex', QuotedEntryField), |
| 143 |
('not regex', QuotedEntryField), |
| 144 |
], |
| 145 |
|
| 146 |
'text': [ |
| 147 |
('is', EntryField), |
| 148 |
('is not', EntryField), |
| 149 |
('contains', EntryField), |
| 150 |
('does not contain', EntryField), |
| 151 |
('contains word', EntryField), |
| 152 |
('does not contain word', EntryField), |
| 153 |
('regex', EntryField), |
| 154 |
('not regex', EntryField), |
| 155 |
('is set', NullField), |
| 156 |
('is not set', NullField), |
| 157 |
], |
| 158 |
|
| 159 |
'time': [ |
| 160 |
('at least', SpinSecondsField), |
| 161 |
('at most', SpinSecondsField), |
| 162 |
('is', SpinSecondsField), |
| 163 |
('is not', SpinSecondsField), |
| 164 |
], |
| 165 |
|
| 166 |
'timestamp': [ |
| 167 |
('in the last', SpinDateField), |
| 168 |
('not in the last', SpinDateField), |
| 169 |
], |
| 170 |
|
| 171 |
'__playlist': [ |
| 172 |
('Track is in', PlaylistField), |
| 173 |
('Track not in', PlaylistField), |
| 174 |
], |
| 175 |
|
| 176 |
'__rating': [ |
| 177 |
('greater than', SpinRating), |
| 178 |
('less than', SpinRating), |
| 179 |
('at least', SpinRating), |
| 180 |
('at most', SpinRating), |
| 181 |
], |
| 182 |
# fmt: on |
| 183 |
} |
| 184 |
|
| 185 |
# aliases |
| 186 |
_criteria_types['datetime'] = _criteria_types['text'] # TODO: fix |
| 187 |
_criteria_types['multiline'] = _criteria_types['text'] |
| 188 |
_criteria_types['dblnum'] = _criteria_types['int'] |
| 189 |
|
| 190 |
|
| 191 |
# This gets populated below. Only add special tags/searches that don't have a |
| 192 |
# valid entry in tag_data |
| 193 |
CRITERIA = [ |
| 194 |
('Rating', _criteria_types['__rating']), |
| 195 |
('Playlist', _criteria_types['__playlist']), |
| 196 |
] |
| 197 |
|
| 198 |
# NOTE: We use N_ (fake gettext) because these strings are translated later by |
| 199 |
# the filter GUI. If we use _ (real gettext) here, filtergui will try to |
| 200 |
# translate already-translated strings, which makes no sense. This is partly due |
| 201 |
# to the old design of storing untranslated strings (instead of operators) in |
| 202 |
# the dynamic playlist database. |
| 203 |
|
| 204 |
_TRANS = { |
| 205 |
# TRANSLATORS: True if haystack is equal to needle |
| 206 |
N_('is'): '==', |
| 207 |
# TRANSLATORS: True if haystack is not equal to needle |
| 208 |
N_('is not'): '!==', |
| 209 |
# TRANSLATORS: True if the specified tag is present (uses the NullField |
| 210 |
# to compare to __null__) |
| 211 |
N_('is set'): '<!==>', |
| 212 |
# TRANSLATORS: True if the specified tag is not present (uses the NullField |
| 213 |
# to compare to __null__) |
| 214 |
N_('is not set'): '<==>', |
| 215 |
# TRANSLATORS: True if haystack contains needle |
| 216 |
N_('contains'): '=', |
| 217 |
# TRANSLATORS: True if haystack does not contain needle |
| 218 |
N_('does not contain'): '!=', |
| 219 |
# TRANSLATORS: True if haystack contains whole word |
| 220 |
N_('contains word'): 'w=', |
| 221 |
# TRANSLATORS: True if haystack does not contain whole word |
| 222 |
N_('does not contain word'): '!w=', |
| 223 |
# TRANSLATORS: True if haystack matches regular expression |
| 224 |
N_('regex'): '~', |
| 225 |
# TRANSLATORS: True if haystack does not match regular expression |
| 226 |
N_('not regex'): '!~', |
| 227 |
# TRANSLATORS: Example: rating >= 5 |
| 228 |
N_('at least'): '>=', |
| 229 |
# TRANSLATORS: Example: rating <= 3 |
| 230 |
N_('at most'): '<=', |
| 231 |
# TRANSLATORS: Example: year < 1999 |
| 232 |
N_('before'): '<', |
| 233 |
# TRANSLATORS: Example: year > 2002 |
| 234 |
N_('after'): '>', |
| 235 |
# TRANSLATORS: Example: 1980 <= year <= 1987 |
| 236 |
N_('between'): '><', |
| 237 |
N_('greater than'): '>', |
| 238 |
N_('less than'): '<', |
| 239 |
# TRANSLATORS: Example: track has been added in the last 2 days |
| 240 |
N_('in the last'): '>=', |
| 241 |
# TRANSLATORS: Example: track has not been added in the last 5 hours |
| 242 |
N_('not in the last'): '<', |
| 243 |
# TRANSLATORS: True if a track is contained in the specified playlist |
| 244 |
N_('Track is in'): 'pin', |
| 245 |
# TRANSLATORS: True if a track is not contained in the specified playlist |
| 246 |
N_('Track not in'): '!pin', |
| 247 |
} |
| 248 |
|
| 249 |
# This table is a reverse lookup for the actual tag name from a display |
| 250 |
# name. |
| 251 |
# This gets populated below. Only add special tags/searches here. |
| 252 |
_NMAP = { |
| 253 |
N_('Rating'): '__rating', # special |
| 254 |
N_('Playlist'): '__playlist', # not a real tag |
| 255 |
} |
| 256 |
|
| 257 |
_REV_NMAP = {} |
| 258 |
|
| 259 |
|
| 260 |
# update the tables based on the globally stored tag list |
| 261 |
def __update_maps(): |
| 262 |
from xl.metadata.tags import tag_data |
| 263 |
|
| 264 |
for tag, data in tag_data.items(): |
| 265 |
if data is None: |
| 266 |
continue |
| 267 |
|
| 268 |
# don't catch this KeyError -- if it fails, fix it! |
| 269 |
criteria = _criteria_types[data.type] |
| 270 |
|
| 271 |
if criteria is None: |
| 272 |
continue |
| 273 |
|
| 274 |
CRITERIA.append((data.name, criteria)) |
| 275 |
|
| 276 |
_NMAP[data.name] = tag |
| 277 |
|
| 278 |
for k, v in _NMAP.items(): |
| 279 |
if v in _REV_NMAP: |
| 280 |
raise ValueError("_REV_NMAP Internal error: '%s', '%s'" % (k, v)) |
| 281 |
_REV_NMAP[v] = k |
| 282 |
|
| 283 |
|
| 284 |
__update_maps() |
| 285 |
|
| 286 |
|
| 287 |
class SmartPlaylistEditor: |
| 288 |
@classmethod |
| 289 |
def create(cls, collection, smart_manager, parent=None): |
| 290 |
""" |
| 291 |
Shows a dialog to create a new smart playlist |
| 292 |
|
| 293 |
:param collection: Collection object |
| 294 |
:param smart_manager: SmartPlaylistManager object |
| 295 |
:param parent: Dialog parent |
| 296 |
|
| 297 |
:returns: New smart playlist, or None |
| 298 |
""" |
| 299 |
dialog = FilterDialog(_('Add Smart Playlist'), parent, CRITERIA) |
| 300 |
dialog.set_transient_for(parent) |
| 301 |
|
| 302 |
cls._attach_sort_widgets(dialog, None, None) |
| 303 |
|
| 304 |
pl = cls._run_edit_dialog(dialog, collection, smart_manager, parent) |
| 305 |
dialog.destroy() |
| 306 |
return pl |
| 307 |
|
| 308 |
@classmethod |
| 309 |
def edit(cls, pl, collection, smart_manager, parent=None): |
| 310 |
""" |
| 311 |
Shows a dialog to edit a smart playlist |
| 312 |
|
| 313 |
:param collection: Collection object |
| 314 |
:param smart_manager: SmartPlaylistManager object |
| 315 |
:param parent: Dialog parent |
| 316 |
|
| 317 |
:returns: New smart playlist, or None |
| 318 |
""" |
| 319 |
if not isinstance(pl, playlist.SmartPlaylist): |
| 320 |
return |
| 321 |
|
| 322 |
from xl.metadata.tags import tag_data |
| 323 |
|
| 324 |
params = pl.search_params |
| 325 |
state = [] |
| 326 |
|
| 327 |
for param in params: |
| 328 |
(field, op, value) = param |
| 329 |
rev_field = _REV_NMAP[field] |
| 330 |
|
| 331 |
# because there are duplicates in _TRANS, cannot create a reverse |
| 332 |
# mapping. Instead, search in set of criteria defined for the type |
| 333 |
data_type = field |
| 334 |
data = tag_data.get(field) |
| 335 |
if data is not None: |
| 336 |
data_type = data.type |
| 337 |
|
| 338 |
for ct in _criteria_types[data_type]: |
| 339 |
rev_op = ct[0] |
| 340 |
if _TRANS[rev_op] == op: |
| 341 |
break |
| 342 |
else: |
| 343 |
dialogs.error(parent, "Invalid operand for %s, omitting" % rev_field) |
| 344 |
continue |
| 345 |
|
| 346 |
state.append(([rev_field, rev_op], value)) |
| 347 |
|
| 348 |
state.reverse() |
| 349 |
|
| 350 |
dialog = FilterDialog(_('Edit Smart Playlist'), parent, CRITERIA) |
| 351 |
|
| 352 |
dialog.set_transient_for(parent) |
| 353 |
dialog.set_name(pl.name) |
| 354 |
dialog.set_match_any(pl.get_or_match()) |
| 355 |
dialog.set_limit(pl.get_return_limit()) |
| 356 |
dialog.set_random(pl.get_random_sort()) |
| 357 |
dialog.set_state(state) |
| 358 |
|
| 359 |
cls._attach_sort_widgets(dialog, *pl.get_sort_tags()) |
| 360 |
|
| 361 |
pl = cls._run_edit_dialog(dialog, collection, smart_manager, parent, orig_pl=pl) |
| 362 |
dialog.destroy() |
| 363 |
return pl |
| 364 |
|
| 365 |
@classmethod |
| 366 |
def _run_edit_dialog(cls, dialog, collection, smart_manager, parent, orig_pl=None): |
| 367 |
'''internal helper function''' |
| 368 |
|
| 369 |
while True: |
| 370 |
result = dialog.run() |
| 371 |
dialog.hide() |
| 372 |
|
| 373 |
if result != Gtk.ResponseType.ACCEPT: |
| 374 |
return |
| 375 |
|
| 376 |
name = dialog.get_name() |
| 377 |
matchany = dialog.get_match_any() |
| 378 |
limit = dialog.get_limit() |
| 379 |
state = dialog.get_state() |
| 380 |
random = dialog.get_random() |
| 381 |
sort_tags = cls._get_sort_tags(dialog) |
| 382 |
|
| 383 |
if not name: |
| 384 |
dialogs.error( |
| 385 |
parent, _("You did " "not enter a name for your playlist") |
| 386 |
) |
| 387 |
continue |
| 388 |
|
| 389 |
if not orig_pl or name != orig_pl.name: |
| 390 |
try: |
| 391 |
pl = smart_manager.get_playlist(name) |
| 392 |
dialogs.error( |
| 393 |
parent, _("The " "playlist name you entered is already in use.") |
| 394 |
) |
| 395 |
continue |
| 396 |
except ValueError: |
| 397 |
pass # playlist didn't exist |
| 398 |
|
| 399 |
pl = playlist.SmartPlaylist(name, collection) |
| 400 |
pl.set_or_match(matchany) |
| 401 |
pl.set_return_limit(limit) |
| 402 |
pl.set_random_sort(random) |
| 403 |
pl.set_sort_tags(*sort_tags) |
| 404 |
|
| 405 |
for item in state: |
| 406 |
(field, op) = item[0] |
| 407 |
value = item[1] |
| 408 |
pl.add_param(_NMAP[field], _TRANS[op], value) |
| 409 |
|
| 410 |
if orig_pl: |
| 411 |
smart_manager.remove_playlist(pl.name) |
| 412 |
|
| 413 |
smart_manager.save_playlist(pl) |
| 414 |
return pl |
| 415 |
|
| 416 |
@classmethod |
| 417 |
def _attach_sort_widgets(cls, dialog, tag, reverse): |
| 418 |
# Add sort widgets |
| 419 |
from xl.metadata.tags import tag_data |
| 420 |
|
| 421 |
dialog.sort_enable = sort_enable = Gtk.CheckButton.new_with_label(_('Sort by:')) |
| 422 |
|
| 423 |
def _on_sort_enable_changed(ck): |
| 424 |
sort_tags.set_sensitive(ck.get_active()) |
| 425 |
sort_order.set_sensitive(ck.get_active()) |
| 426 |
|
| 427 |
sort_enable.connect('toggled', _on_sort_enable_changed) |
| 428 |
|
| 429 |
dialog.sort_tags = sort_tags = Gtk.ComboBoxText.new() |
| 430 |
sort_tags.set_wrap_width(5) |
| 431 |
for k, v in sorted(tag_data.items()): |
| 432 |
if v: |
| 433 |
sort_tags.append(k, v.translated_name) |
| 434 |
|
| 435 |
dialog.sort_order = sort_order = Gtk.ComboBoxText.new() |
| 436 |
sort_order.append("False", _("Ascending")) |
| 437 |
sort_order.append("True", _("Descending")) |
| 438 |
|
| 439 |
box = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 5) |
| 440 |
box.pack_start(sort_enable, False, False, 0) |
| 441 |
box.pack_start(sort_tags, False, False, 0) |
| 442 |
box.pack_start(sort_order, False, False, 0) |
| 443 |
box.show_all() |
| 444 |
|
| 445 |
dialog.get_content_area().pack_start(box, False, False, 5) |
| 446 |
|
| 447 |
# Set it up |
| 448 |
if tag is None: |
| 449 |
sort_enable.set_active(False) |
| 450 |
sort_order.set_active_id("False") |
| 451 |
else: |
| 452 |
sort_enable.set_active(True) |
| 453 |
sort_tags.set_active_id(tag) |
| 454 |
sort_order.set_active_id(str(reverse)) |
| 455 |
|
| 456 |
_on_sort_enable_changed(sort_enable) |
| 457 |
|
| 458 |
@classmethod |
| 459 |
def _get_sort_tags(cls, dialog): |
| 460 |
if not dialog.sort_enable.get_active(): |
| 461 |
return None, False |
| 462 |
tag = dialog.sort_tags.get_active_id() |
| 463 |
order = dialog.sort_order.get_active_id() == 'True' |
| 464 |
return tag, order |