| 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 |
|
| 28 |
import logging |
| 29 |
from typing import Dict, Union |
| 30 |
|
| 31 |
from gi.repository import Gtk |
| 32 |
from gi.repository import Gdk |
| 33 |
from gi.repository import GdkPixbuf |
| 34 |
from gi.repository import GObject |
| 35 |
|
| 36 |
from xl import common, event, radio, settings, trax |
| 37 |
from xl.nls import gettext as _ |
| 38 |
from xl.playlist import Playlist, SmartPlaylist |
| 39 |
from xlgui import icons, panel |
| 40 |
from xlgui.panel import menus |
| 41 |
from xlgui.widgets import dialogs |
| 42 |
from xlgui.widgets.common import DragTreeView |
| 43 |
from xlgui.widgets.smart_playlist_editor import SmartPlaylistEditor |
| 44 |
|
| 45 |
logger = logging.getLogger(__name__) |
| 46 |
|
| 47 |
|
| 48 |
class TrackWrapper: |
| 49 |
def __init__(self, track, playlist): |
| 50 |
self.track = track |
| 51 |
self.playlist = playlist |
| 52 |
|
| 53 |
def __str__(self): |
| 54 |
text = self.track.get_tag_raw('title') |
| 55 |
if text is not None: |
| 56 |
text = ' / '.join(text) |
| 57 |
|
| 58 |
if text: |
| 59 |
artists = self.track.get_tag_raw('artist') |
| 60 |
if artists: |
| 61 |
text += ' - ' + ' / '.join(artists) |
| 62 |
return text |
| 63 |
return self.track.get_loc_for_io() |
| 64 |
|
| 65 |
|
| 66 |
class BasePlaylistPanelMixin(GObject.Object): |
| 67 |
""" |
| 68 |
Base playlist tree object. |
| 69 |
|
| 70 |
Used by the radio and playlists panels to display playlists |
| 71 |
""" |
| 72 |
|
| 73 |
# HACK: Notice that this is not __gsignals__; descendants need to manually |
| 74 |
# merge this in. This is because new PyGObject doesn't like __gsignals__ |
| 75 |
# coming from mixin. See: |
| 76 |
# * https://bugs.launchpad.net/bugs/714484 |
| 77 |
# * http://www.daa.com.au/pipermail/pygtk/2011-February/019394.html |
| 78 |
_gsignals_ = { |
| 79 |
'playlist-selected': (GObject.SignalFlags.RUN_LAST, None, (object,)), |
| 80 |
'tracks-selected': (GObject.SignalFlags.RUN_LAST, None, (object,)), |
| 81 |
'append-items': (GObject.SignalFlags.RUN_LAST, None, (object, bool)), |
| 82 |
'replace-items': (GObject.SignalFlags.RUN_LAST, None, (object,)), |
| 83 |
'queue-items': (GObject.SignalFlags.RUN_LAST, None, (object,)), |
| 84 |
} |
| 85 |
|
| 86 |
# Cache for custom playlists |
| 87 |
playlist_nodes: Dict[Playlist, Gtk.TreeIter] |
| 88 |
|
| 89 |
# Mapping to keep track of open "are you sure you want to delete" dialogs |
| 90 |
deletion_dialogs: Dict[Union[Playlist, SmartPlaylist], Gtk.Dialog] |
| 91 |
|
| 92 |
def __init__(self): |
| 93 |
""" |
| 94 |
Initializes the mixin |
| 95 |
""" |
| 96 |
GObject.Object.__init__(self) |
| 97 |
self.playlist_nodes = {} |
| 98 |
self.track_image = icons.MANAGER.pixbuf_from_icon_name( |
| 99 |
'audio-x-generic', Gtk.IconSize.SMALL_TOOLBAR |
| 100 |
) |
| 101 |
self.deletion_dialogs = {} |
| 102 |
|
| 103 |
def remove_playlist(self, ignored=None): |
| 104 |
""" |
| 105 |
Removes the selected playlist from the UI |
| 106 |
and from the underlying manager |
| 107 |
""" |
| 108 |
selected_playlist = self.tree.get_selected_page(raw=True) |
| 109 |
if selected_playlist is None: |
| 110 |
return |
| 111 |
dialog = self.deletion_dialogs.get(selected_playlist) |
| 112 |
if dialog: |
| 113 |
dialog.present() |
| 114 |
return |
| 115 |
|
| 116 |
def on_response(dialog, response): |
| 117 |
if response == Gtk.ResponseType.YES: |
| 118 |
if isinstance(selected_playlist, SmartPlaylist): |
| 119 |
self.smart_manager.remove_playlist(selected_playlist.name) |
| 120 |
else: |
| 121 |
self.playlist_manager.remove_playlist(selected_playlist.name) |
| 122 |
# Remove from {playlist: iter} cache. |
| 123 |
del self.playlist_nodes[selected_playlist] |
| 124 |
# Remove from UI. |
| 125 |
selection = self.tree.get_selection() |
| 126 |
(model, iter) = selection.get_selected() |
| 127 |
self.model.remove(iter) |
| 128 |
del self.deletion_dialogs[selected_playlist] |
| 129 |
dialog.destroy() |
| 130 |
|
| 131 |
dialog = Gtk.MessageDialog( |
| 132 |
buttons=Gtk.ButtonsType.YES_NO, |
| 133 |
message_type=Gtk.MessageType.QUESTION, |
| 134 |
text=_('Delete the playlist "%s"?') % selected_playlist.name, |
| 135 |
transient_for=self.parent, |
| 136 |
) |
| 137 |
dialog.connect('response', on_response) |
| 138 |
self.deletion_dialogs[selected_playlist] = dialog |
| 139 |
dialog.present() |
| 140 |
|
| 141 |
def rename_playlist(self, playlist): |
| 142 |
""" |
| 143 |
Renames the playlist |
| 144 |
""" |
| 145 |
|
| 146 |
if playlist is None: |
| 147 |
return |
| 148 |
|
| 149 |
# Ask for new name |
| 150 |
dialog = dialogs.TextEntryDialog( |
| 151 |
_("Enter the new name you want for your playlist"), |
| 152 |
_("Rename Playlist"), |
| 153 |
playlist.name, |
| 154 |
parent=self.parent, |
| 155 |
) |
| 156 |
|
| 157 |
result = dialog.run() |
| 158 |
name = dialog.get_value() |
| 159 |
|
| 160 |
dialog.destroy() |
| 161 |
|
| 162 |
if result != Gtk.ResponseType.OK or name == '': |
| 163 |
return |
| 164 |
|
| 165 |
if name in self.playlist_manager.playlists: |
| 166 |
# name is already in use |
| 167 |
dialogs.error( |
| 168 |
self.parent, _("The playlist name you entered is already in use.") |
| 169 |
) |
| 170 |
return |
| 171 |
|
| 172 |
selection = self.tree.get_selection() |
| 173 |
(model, iter) = selection.get_selected() |
| 174 |
model.set_value(iter, 1, name) |
| 175 |
|
| 176 |
# Update the manager as well |
| 177 |
self.playlist_manager.rename_playlist(playlist, name) |
| 178 |
|
| 179 |
def open_selected_playlist(self): |
| 180 |
selection = self.tree.get_selection() |
| 181 |
(model, iter) = selection.get_selected() |
| 182 |
self.open_item(self.tree, model.get_path(iter), None) |
| 183 |
|
| 184 |
def on_rating_changed(self, widget, rating): |
| 185 |
""" |
| 186 |
Updates the rating of the selected tracks |
| 187 |
""" |
| 188 |
tracks = self.get_selected_tracks() |
| 189 |
|
| 190 |
for track in tracks: |
| 191 |
track.set_rating(rating) |
| 192 |
|
| 193 |
maximum = settings.get_option('rating/maximum', 5) |
| 194 |
event.log_event('rating_changed', self, 100 * rating / maximum) |
| 195 |
|
| 196 |
def open_item(self, tree, path, col): |
| 197 |
""" |
| 198 |
Called when the user double clicks on a playlist, |
| 199 |
also called when the user double clicks on a track beneath |
| 200 |
a playlist. When they active a track it opens the playlist |
| 201 |
and starts playing that track |
| 202 |
""" |
| 203 |
iter = self.model.get_iter(path) |
| 204 |
item = self.model.get_value(iter, 2) |
| 205 |
if item is not None: |
| 206 |
if isinstance(item, (Playlist, SmartPlaylist)): |
| 207 |
# for smart playlists |
| 208 |
if hasattr(item, 'get_playlist'): |
| 209 |
try: |
| 210 |
item = item.get_playlist(self.collection) |
| 211 |
except Exception as e: |
| 212 |
logger.exception("Error loading smart playlist") |
| 213 |
dialogs.error( |
| 214 |
self.parent, _("Error loading smart playlist: %s") % str(e) |
| 215 |
) |
| 216 |
return |
| 217 |
else: |
| 218 |
# Get an up to date copy |
| 219 |
item = self.playlist_manager.get_playlist(item.name) |
| 220 |
# item.set_is_custom(True) |
| 221 |
|
| 222 |
# self.controller.main.add_playlist(item) |
| 223 |
self.emit('playlist-selected', item) |
| 224 |
else: |
| 225 |
self.emit('append-items', [item.track], True) |
| 226 |
|
| 227 |
def add_new_playlist(self, tracks=[], name=None): |
| 228 |
""" |
| 229 |
Adds a new playlist to the list of playlists. If name is |
| 230 |
None or the name conflicts with an existing playlist, the |
| 231 |
user will be queried for a new name. |
| 232 |
|
| 233 |
Returns the name of the new playlist, or None if it was |
| 234 |
not added. |
| 235 |
""" |
| 236 |
if name: |
| 237 |
if name in self.playlist_manager.playlists: |
| 238 |
name = dialogs.ask_for_playlist_name( |
| 239 |
self.get_panel().get_toplevel(), self.playlist_manager, name |
| 240 |
) |
| 241 |
else: |
| 242 |
if tracks: |
| 243 |
artists = [] |
| 244 |
composers = [] |
| 245 |
albums = [] |
| 246 |
|
| 247 |
for track in tracks: |
| 248 |
artist = track.get_tag_display('artist', artist_compilations=False) |
| 249 |
|
| 250 |
if artist is not None: |
| 251 |
artists += [artist] |
| 252 |
|
| 253 |
composer = track.get_tag_display( |
| 254 |
'composer', artist_compilations=False |
| 255 |
) |
| 256 |
|
| 257 |
if composer is not None: |
| 258 |
composers += composer |
| 259 |
|
| 260 |
album = track.get_tag_display('album') |
| 261 |
|
| 262 |
if album is not None: |
| 263 |
albums += album |
| 264 |
|
| 265 |
artists = list(set(artists))[:3] |
| 266 |
composers = list(set(composers))[:3] |
| 267 |
albums = list(set(albums))[:3] |
| 268 |
|
| 269 |
if len(artists) > 0: |
| 270 |
name = artists[0] |
| 271 |
|
| 272 |
if len(artists) > 2: |
| 273 |
# TRANSLATORS: Playlist title suggestion with more |
| 274 |
# than two values |
| 275 |
name = _('%(first)s, %(second)s and others') % { |
| 276 |
'first': artists[0], |
| 277 |
'second': artists[1], |
| 278 |
} |
| 279 |
elif len(artists) > 1: |
| 280 |
# TRANSLATORS: Playlist title suggestion with two values |
| 281 |
name = _('%(first)s and %(second)s') % { |
| 282 |
'first': artists[0], |
| 283 |
'second': artists[1], |
| 284 |
} |
| 285 |
elif len(composers) > 0: |
| 286 |
name = composers[0] |
| 287 |
|
| 288 |
if len(composers) > 2: |
| 289 |
# TRANSLATORS: Playlist title suggestion with more |
| 290 |
# than two values |
| 291 |
name = _('%(first)s, %(second)s and others') % { |
| 292 |
'first': composers[0], |
| 293 |
'second': composers[1], |
| 294 |
} |
| 295 |
elif len(composers) > 1: |
| 296 |
# TRANSLATORS: Playlist title suggestion with two values |
| 297 |
name = _('%(first)s and %(second)s') % { |
| 298 |
'first': composers[0], |
| 299 |
'second': composers[1], |
| 300 |
} |
| 301 |
elif len(albums) > 0: |
| 302 |
name = albums[0] |
| 303 |
|
| 304 |
if len(albums) > 2: |
| 305 |
# TRANSLATORS: Playlist title suggestion with more |
| 306 |
# than two values |
| 307 |
name = _('%(first)s, %(second)s and others') % { |
| 308 |
'first': albums[0], |
| 309 |
'second': albums[1], |
| 310 |
} |
| 311 |
elif len(albums) > 1: |
| 312 |
# TRANSLATORS: Playlist title suggestion with two values |
| 313 |
name = _('%(first)s and %(second)s') % { |
| 314 |
'first': albums[0], |
| 315 |
'second': albums[1], |
| 316 |
} |
| 317 |
else: |
| 318 |
name = '' |
| 319 |
|
| 320 |
name = dialogs.ask_for_playlist_name( |
| 321 |
self.get_panel().get_toplevel(), self.playlist_manager, name |
| 322 |
) |
| 323 |
|
| 324 |
if name is not None: |
| 325 |
# Create the playlist from all of the tracks |
| 326 |
new_playlist = Playlist(name) |
| 327 |
new_playlist.extend(tracks) |
| 328 |
# We are adding a completely new playlist with tracks so we save it |
| 329 |
self.playlist_manager.save_playlist(new_playlist) |
| 330 |
|
| 331 |
return name |
| 332 |
|
| 333 |
def _load_playlist_nodes(self, playlist): |
| 334 |
""" |
| 335 |
Loads the playlist tracks into the node for the specified playlist |
| 336 |
""" |
| 337 |
if playlist not in self.playlist_nodes: |
| 338 |
return |
| 339 |
|
| 340 |
expanded = self.tree.row_expanded( |
| 341 |
self.model.get_path(self.playlist_nodes[playlist]) |
| 342 |
) |
| 343 |
|
| 344 |
self._clear_node(self.playlist_nodes[playlist]) |
| 345 |
parent = self.playlist_nodes[playlist] |
| 346 |
for track in playlist: |
| 347 |
if not track: |
| 348 |
continue |
| 349 |
wrapper = TrackWrapper(track, playlist) |
| 350 |
row = (self.track_image, str(wrapper), wrapper) |
| 351 |
self.model.append(parent, row) |
| 352 |
|
| 353 |
if expanded: |
| 354 |
self.tree.expand_row( |
| 355 |
self.model.get_path(self.playlist_nodes[playlist]), False |
| 356 |
) |
| 357 |
|
| 358 |
def remove_selected_track(self): |
| 359 |
""" |
| 360 |
Removes the selected track from its playlist |
| 361 |
and saves the playlist |
| 362 |
""" |
| 363 |
selection = self.tree.get_selection() |
| 364 |
(model, iter) = selection.get_selected() |
| 365 |
track = model.get_value(iter, 2) |
| 366 |
if isinstance(track, TrackWrapper): |
| 367 |
del track.playlist[track.playlist.index(track.track)] |
| 368 |
# Update the list |
| 369 |
self.model.remove(iter) |
| 370 |
# TODO do we save the playlist after this?? |
| 371 |
self.playlist_manager.save_playlist(track.playlist, overwrite=True) |
| 372 |
|
| 373 |
|
| 374 |
class PlaylistsPanel(panel.Panel, BasePlaylistPanelMixin): |
| 375 |
""" |
| 376 |
The playlists panel |
| 377 |
""" |
| 378 |
|
| 379 |
__gsignals__ = BasePlaylistPanelMixin._gsignals_ |
| 380 |
|
| 381 |
ui_info = ('playlists.ui', 'PlaylistsPanel') |
| 382 |
|
| 383 |
def __init__(self, parent, playlist_manager, smart_manager, collection, name): |
| 384 |
""" |
| 385 |
Initializes the playlists panel |
| 386 |
|
| 387 |
@param playlist_manager: The playlist manager |
| 388 |
""" |
| 389 |
panel.Panel.__init__(self, parent, name, _('Playlists')) |
| 390 |
BasePlaylistPanelMixin.__init__(self) |
| 391 |
self.playlist_manager = playlist_manager |
| 392 |
self.smart_manager = smart_manager |
| 393 |
self.collection = collection |
| 394 |
self.box = self.builder.get_object('PlaylistsPanel') |
| 395 |
|
| 396 |
self.playlist_name_info = 500 |
| 397 |
self.track_target = Gtk.TargetEntry.new("text/uri-list", 0, 0) |
| 398 |
self.playlist_target = Gtk.TargetEntry.new( |
| 399 |
"playlist_name", Gtk.TargetFlags.SAME_WIDGET, self.playlist_name_info |
| 400 |
) |
| 401 |
self.deny_targets = [Gtk.TargetEntry.new('', 0, 0)] |
| 402 |
|
| 403 |
self.tree = PlaylistDragTreeView(self) |
| 404 |
self.tree.connect('row-activated', self.open_item) |
| 405 |
self.tree.set_headers_visible(False) |
| 406 |
self.tree.connect('drag-motion', self.drag_motion) |
| 407 |
self.tree.drag_source_set( |
| 408 |
Gdk.ModifierType.BUTTON1_MASK, |
| 409 |
[self.track_target, self.playlist_target], |
| 410 |
Gdk.DragAction.COPY | Gdk.DragAction.MOVE, |
| 411 |
) |
| 412 |
|
| 413 |
self.scroll = Gtk.ScrolledWindow() |
| 414 |
self.scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) |
| 415 |
self.scroll.add(self.tree) |
| 416 |
self.scroll.set_shadow_type(Gtk.ShadowType.IN) |
| 417 |
self.box.pack_start(self.scroll, True, True, 0) |
| 418 |
self.box.show_all() |
| 419 |
|
| 420 |
pb = Gtk.CellRendererPixbuf() |
| 421 |
cell = Gtk.CellRendererText() |
| 422 |
if settings.get_option('gui/ellipsize_text_in_panels', False): |
| 423 |
from gi.repository import Pango |
| 424 |
|
| 425 |
cell.set_property('ellipsize-set', True) |
| 426 |
cell.set_property('ellipsize', Pango.EllipsizeMode.END) |
| 427 |
col = Gtk.TreeViewColumn('Text') |
| 428 |
col.pack_start(pb, False) |
| 429 |
col.pack_start(cell, True) |
| 430 |
col.set_attributes(pb, pixbuf=0) |
| 431 |
col.set_attributes(cell, text=1) |
| 432 |
self.tree.append_column(col) |
| 433 |
self.model = Gtk.TreeStore(GdkPixbuf.Pixbuf, str, object) |
| 434 |
self.tree.set_model(self.model) |
| 435 |
|
| 436 |
# icons |
| 437 |
self.folder = icons.MANAGER.pixbuf_from_icon_name( |
| 438 |
'folder', Gtk.IconSize.SMALL_TOOLBAR |
| 439 |
) |
| 440 |
self.playlist_image = icons.MANAGER.pixbuf_from_icon_name( |
| 441 |
'music-library', Gtk.IconSize.SMALL_TOOLBAR |
| 442 |
) |
| 443 |
|
| 444 |
# menus |
| 445 |
self.playlist_menu = menus.PlaylistsPanelPlaylistMenu(self) |
| 446 |
self.smart_menu = menus.PlaylistsPanelPlaylistMenu(self) |
| 447 |
self.default_menu = menus.PlaylistPanelMenu(self) |
| 448 |
|
| 449 |
self.track_menu = menus.TrackPanelMenu(self) |
| 450 |
|
| 451 |
self._connect_events() |
| 452 |
self._load_playlists() |
| 453 |
|
| 454 |
@property |
| 455 |
def menu(self): |
| 456 |
""" |
| 457 |
Gets a menu for the selected item |
| 458 |
:return: xlgui.widgets.menu.Menu or None if do not have it |
| 459 |
""" |
| 460 |
model, it = self.tree.get_selection().get_selected() |
| 461 |
pl = model[it][2] |
| 462 |
return ( |
| 463 |
self.playlist_menu |
| 464 |
if isinstance(pl, Playlist) |
| 465 |
else ( |
| 466 |
self.smart_menu |
| 467 |
if isinstance(pl, SmartPlaylist) |
| 468 |
else ( |
| 469 |
self.track_menu |
| 470 |
if isinstance(pl, TrackWrapper) |
| 471 |
else self.default_menu |
| 472 |
) |
| 473 |
) |
| 474 |
) |
| 475 |
|
| 476 |
def _connect_events(self): |
| 477 |
event.add_ui_callback(self.refresh_playlists, 'track_tags_changed') |
| 478 |
event.add_ui_callback( |
| 479 |
self._on_playlist_added, 'playlist_added', self.playlist_manager |
| 480 |
) |
| 481 |
|
| 482 |
self.tree.connect('key-release-event', self.on_key_released) |
| 483 |
|
| 484 |
def _playlist_properties(self): |
| 485 |
pl = self.tree.get_selected_page(raw=True) |
| 486 |
if isinstance(pl, SmartPlaylist): |
| 487 |
self.edit_selected_smart_playlist() |
| 488 |
|
| 489 |
def refresh_playlists(self, type, track, tags): |
| 490 |
""" |
| 491 |
wrapper so that multiple events dont cause multiple |
| 492 |
reloads in quick succession |
| 493 |
""" |
| 494 |
if settings.get_option('gui/sync_on_tag_change', True) and tags & { |
| 495 |
'title', |
| 496 |
'artist', |
| 497 |
}: |
| 498 |
self._refresh_playlists() |
| 499 |
|
| 500 |
@common.glib_wait(500) |
| 501 |
def _refresh_playlists(self): |
| 502 |
""" |
| 503 |
Callback for when tags have changed and the playlists |
| 504 |
need refreshing. |
| 505 |
""" |
| 506 |
if settings.get_option('gui/sync_on_tag_change', True): |
| 507 |
for playlist in self.playlist_nodes: |
| 508 |
self._load_playlist_nodes(playlist) |
| 509 |
|
| 510 |
def _on_playlist_added(self, type, object, playlist_name): |
| 511 |
new_playlist = self.playlist_manager.get_playlist(playlist_name) |
| 512 |
|
| 513 |
for oldpl in self.playlist_nodes: |
| 514 |
if oldpl.name == playlist_name: # Name already exists |
| 515 |
if oldpl is not new_playlist: |
| 516 |
node = self.playlist_nodes[oldpl] |
| 517 |
# Replace the playlist object in {playlist: iter} cache. |
| 518 |
del self.playlist_nodes[oldpl] |
| 519 |
self.playlist_nodes[new_playlist] = node |
| 520 |
# Replace the playlist object in tree model. |
| 521 |
self.model[node][2] = new_playlist |
| 522 |
break |
| 523 |
else: # Name doesn't exist yet |
| 524 |
self.playlist_nodes[new_playlist] = self.model.append( |
| 525 |
self.custom, [self.playlist_image, playlist_name, new_playlist] |
| 526 |
) |
| 527 |
self.tree.expand_row(self.model.get_path(self.custom), False) |
| 528 |
|
| 529 |
# Refresh the playlist subnodes. |
| 530 |
self._load_playlist_nodes(new_playlist) |
| 531 |
|
| 532 |
def _load_playlists(self): |
| 533 |
""" |
| 534 |
Loads the currently saved playlists |
| 535 |
""" |
| 536 |
self.smart = self.model.append(None, [self.folder, _("Smart Playlists"), None]) |
| 537 |
|
| 538 |
self.custom = self.model.append( |
| 539 |
None, [self.folder, _("Custom Playlists"), None] |
| 540 |
) |
| 541 |
|
| 542 |
names = sorted(self.smart_manager.playlists) |
| 543 |
for name in names: |
| 544 |
self.model.append( |
| 545 |
self.smart, |
| 546 |
[self.playlist_image, name, self.smart_manager.get_playlist(name)], |
| 547 |
) |
| 548 |
|
| 549 |
names = sorted(self.playlist_manager.playlists) |
| 550 |
for name in names: |
| 551 |
playlist = self.playlist_manager.get_playlist(name) |
| 552 |
self.playlist_nodes[playlist] = self.model.append( |
| 553 |
self.custom, [self.playlist_image, name, playlist] |
| 554 |
) |
| 555 |
self._load_playlist_nodes(playlist) |
| 556 |
|
| 557 |
self.tree.expand_row(self.model.get_path(self.smart), False) |
| 558 |
self.tree.expand_row(self.model.get_path(self.custom), False) |
| 559 |
|
| 560 |
def import_playlist(self): |
| 561 |
""" |
| 562 |
Shows a dialog to ask the user to import a new playlist |
| 563 |
""" |
| 564 |
|
| 565 |
def _on_playlists_selected(dialog, playlists): |
| 566 |
for playlist in playlists: |
| 567 |
self.add_new_playlist(playlist, playlist.name) |
| 568 |
|
| 569 |
dialog = dialogs.PlaylistImportDialog(parent=self.parent) |
| 570 |
dialog.connect('playlists-selected', _on_playlists_selected) |
| 571 |
dialog.show() |
| 572 |
|
| 573 |
def add_smart_playlist(self): |
| 574 |
""" |
| 575 |
Shows a dialog for adding a new smart playlist |
| 576 |
""" |
| 577 |
pl = SmartPlaylistEditor.create( |
| 578 |
self.collection, self.smart_manager, self.parent |
| 579 |
) |
| 580 |
if pl: |
| 581 |
self.model.append(self.smart, [self.playlist_image, pl.name, pl]) |
| 582 |
|
| 583 |
def edit_selected_smart_playlist(self): |
| 584 |
""" |
| 585 |
Shows a dialog for editing the currently selected smart playlist |
| 586 |
""" |
| 587 |
pl = self.tree.get_selected_page(raw=True) |
| 588 |
self.edit_smart_playlist(pl) |
| 589 |
|
| 590 |
def edit_smart_playlist(self, pl): |
| 591 |
""" |
| 592 |
Shows a dialog for editing a smart playlist |
| 593 |
""" |
| 594 |
pl = SmartPlaylistEditor.edit( |
| 595 |
pl, self.collection, self.smart_manager, self.parent |
| 596 |
) |
| 597 |
if pl: |
| 598 |
selection = self.tree.get_selection() |
| 599 |
model, it = selection.get_selected() |
| 600 |
model.set_value(it, 1, pl.name) |
| 601 |
model.set_value(it, 2, pl) |
| 602 |
|
| 603 |
def drag_data_received(self, tv, context, x, y, selection, info, etime): |
| 604 |
""" |
| 605 |
Called when someone drags some thing onto the playlist panel |
| 606 |
""" |
| 607 |
if info == self.playlist_name_info: |
| 608 |
# We are being dragged a playlist so |
| 609 |
# we have to reorder them |
| 610 |
playlist_name = selection.get_text() |
| 611 |
drag_source = self.tree.get_selected_page() |
| 612 |
# verify names |
| 613 |
if drag_source is not None: |
| 614 |
if drag_source.name == playlist_name: |
| 615 |
drop_info = tv.get_dest_row_at_pos(x, y) |
| 616 |
drag_source_iter = self.playlist_nodes[drag_source] |
| 617 |
if drop_info: |
| 618 |
path, position = drop_info |
| 619 |
drop_target_iter = self.model.get_iter(path) |
| 620 |
drop_target = self.model.get_value(drop_target_iter, 2) |
| 621 |
if position == Gtk.TreeViewDropPosition.BEFORE: |
| 622 |
# Put the playlist before drop_target |
| 623 |
self.model.move_before(drag_source_iter, drop_target_iter) |
| 624 |
self.playlist_manager.move( |
| 625 |
playlist_name, drop_target.name, after=False |
| 626 |
) |
| 627 |
else: |
| 628 |
# put the playlist after drop_target |
| 629 |
self.model.move_after(drag_source_iter, drop_target_iter) |
| 630 |
self.playlist_manager.move( |
| 631 |
playlist_name, drop_target.name, after=True |
| 632 |
) |
| 633 |
# Even though we are doing a move we still don't |
| 634 |
# call the delete method because we take care |
| 635 |
# of it above by moving instead of inserting/deleting |
| 636 |
context.finish(True, False, etime) |
| 637 |
else: |
| 638 |
self._drag_data_received_uris(tv, context, x, y, selection, info, etime) |
| 639 |
|
| 640 |
def _drag_data_received_uris(self, tv, context, x, y, selection, info, etime): |
| 641 |
""" |
| 642 |
Called by drag_data_received when the user drags URIs onto us |
| 643 |
""" |
| 644 |
locs = list(selection.get_uris()) |
| 645 |
drop_info = tv.get_dest_row_at_pos(x, y) |
| 646 |
if drop_info: |
| 647 |
path, position = drop_info |
| 648 |
iter = self.model.get_iter(path) |
| 649 |
drop_target = self.model.get_value(iter, 2) |
| 650 |
|
| 651 |
# if the current item is a track, use the parent playlist |
| 652 |
insert_index = None |
| 653 |
if isinstance(drop_target, TrackWrapper): |
| 654 |
current_playlist = drop_target.playlist |
| 655 |
drop_target_index = current_playlist.index(drop_target.track) |
| 656 |
# Adjust insert position based on drop position |
| 657 |
if ( |
| 658 |
position == Gtk.TreeViewDropPosition.BEFORE |
| 659 |
or position == Gtk.TreeViewDropPosition.INTO_OR_BEFORE |
| 660 |
): |
| 661 |
# By default adding tracks inserts it before so we do not |
| 662 |
# have to modify the insert index |
| 663 |
insert_index = drop_target_index |
| 664 |
else: |
| 665 |
# If we want to go after we have to append 1 |
| 666 |
insert_index = drop_target_index + 1 |
| 667 |
else: |
| 668 |
current_playlist = drop_target |
| 669 |
|
| 670 |
# Since the playlist do not have very good support for |
| 671 |
# duplicate tracks we have to perform some trickery |
| 672 |
# to make this work properly in all cases |
| 673 |
try: |
| 674 |
remove_track_index = current_playlist.index( |
| 675 |
self.tree.get_selected_track() |
| 676 |
) |
| 677 |
except ValueError: |
| 678 |
remove_track_index = None |
| 679 |
if insert_index is not None and remove_track_index is not None: |
| 680 |
# Since remove_track_index will be removed before |
| 681 |
# the new track is inserted we have to offset the |
| 682 |
# insert index |
| 683 |
if insert_index > remove_track_index: |
| 684 |
insert_index = insert_index - 1 |
| 685 |
|
| 686 |
# Delete the track before adding the other one |
| 687 |
# so we do not get duplicates |
| 688 |
# right now the playlist does not support |
| 689 |
# duplicate tracks very well |
| 690 |
if context.get_selected_action() == Gdk.DragAction.MOVE: |
| 691 |
# On a move action the second True makes the |
| 692 |
# drag_data_delete function called |
| 693 |
context.finish(True, True, etime) |
| 694 |
else: |
| 695 |
context.finish(True, False, etime) |
| 696 |
|
| 697 |
# Add the tracks we found to the internal playlist |
| 698 |
# TODO: have it pass in existing tracks? |
| 699 |
(tracks, playlists) = self.tree.get_drag_data(locs) |
| 700 |
|
| 701 |
if insert_index is not None: |
| 702 |
current_playlist[insert_index:insert_index] = tracks |
| 703 |
else: |
| 704 |
current_playlist.extend(tracks) |
| 705 |
|
| 706 |
self._load_playlist_nodes(current_playlist) |
| 707 |
|
| 708 |
# Do we save in the case when a user drags a file onto a playlist |
| 709 |
# in the playlist panel? note that the playlist does not have to |
| 710 |
# be open for this to happen |
| 711 |
self.playlist_manager.save_playlist(current_playlist, overwrite=True) |
| 712 |
else: |
| 713 |
# If the user dragged files prompt for a new playlist name |
| 714 |
# else if they dragged a playlist add the playlist |
| 715 |
|
| 716 |
# We don't want the tracks in the playlists to be added to the |
| 717 |
# master tracks list so we pass in False |
| 718 |
(tracks, playlists) = self.tree.get_drag_data(locs, False) |
| 719 |
# First see if they dragged any playlist files |
| 720 |
for new_playlist in playlists: |
| 721 |
# We are adding a completely new playlist with tracks so |
| 722 |
# we save it. This will trigger playlist_added. |
| 723 |
self.playlist_manager.save_playlist(new_playlist, overwrite=True) |
| 724 |
|
| 725 |
# After processing playlist proceed to ask the user for the |
| 726 |
# name of the new playlist to add and add the tracks to it |
| 727 |
if len(tracks) > 0: |
| 728 |
self.add_new_playlist(tracks) |
| 729 |
|
| 730 |
def drag_data_delete(self, tv, context): |
| 731 |
""" |
| 732 |
Called after a drag data operation is complete |
| 733 |
and we want to delete the source data |
| 734 |
""" |
| 735 |
if Gdk.drag_drop_succeeded(context): |
| 736 |
self.remove_selected_track() |
| 737 |
|
| 738 |
def drag_get_data(self, tv, context, selection_data, info, time): |
| 739 |
""" |
| 740 |
Called when someone drags something from the playlist |
| 741 |
""" |
| 742 |
# TODO based on info determine what we set in selection_data |
| 743 |
if info == self.playlist_name_info: |
| 744 |
pl = self.tree.get_selected_page() |
| 745 |
if pl is not None: |
| 746 |
selection_data.set_text(pl.name, len(pl.name)) |
| 747 |
else: |
| 748 |
pl = self.tree.get_selected_page() |
| 749 |
if pl is not None: |
| 750 |
tracks = pl[:] |
| 751 |
else: |
| 752 |
tracks = self.tree.get_selected_tracks() |
| 753 |
|
| 754 |
if not tracks: |
| 755 |
return |
| 756 |
|
| 757 |
for track in tracks: |
| 758 |
DragTreeView.dragged_data[track.get_loc_for_io()] = track |
| 759 |
|
| 760 |
uris = trax.util.get_uris_from_tracks(tracks) |
| 761 |
selection_data.set_uris(uris) |
| 762 |
|
| 763 |
def drag_motion(self, tv, context, x, y, time): |
| 764 |
""" |
| 765 |
Sets the appropriate drag action based on what we are hovering over |
| 766 |
|
| 767 |
hovering over playlists causes the copy action to occur |
| 768 |
hovering over tracks within the same playlist causes the move |
| 769 |
action to occur |
| 770 |
hovering over tracks within different playlist causes the move |
| 771 |
action to occur |
| 772 |
|
| 773 |
Called on the destination widget |
| 774 |
""" |
| 775 |
# Reset any target to be default to moving tracks |
| 776 |
self.tree.enable_model_drag_dest([self.track_target], Gdk.DragAction.DEFAULT) |
| 777 |
# Determine where the drag is coming from |
| 778 |
dragging_playlist = False |
| 779 |
if tv == self.tree: |
| 780 |
selected_playlist = self.tree.get_selected_page() |
| 781 |
if selected_playlist is not None: |
| 782 |
dragging_playlist = True |
| 783 |
|
| 784 |
# Find out where they are dropping onto |
| 785 |
drop_info = tv.get_dest_row_at_pos(x, y) |
| 786 |
if drop_info: |
| 787 |
path, position = drop_info |
| 788 |
iter = self.model.get_iter(path) |
| 789 |
drop_target = self.model.get_value(iter, 2) |
| 790 |
|
| 791 |
if isinstance(drop_target, Playlist): |
| 792 |
if dragging_playlist: |
| 793 |
# If we drag onto we copy, if we drag between we move |
| 794 |
if ( |
| 795 |
position == Gtk.TreeViewDropPosition.INTO_OR_BEFORE |
| 796 |
or position == Gtk.TreeViewDropPosition.INTO_OR_AFTER |
| 797 |
): |
| 798 |
Gdk.drag_status(context, Gdk.DragAction.COPY, time) |
| 799 |
else: |
| 800 |
Gdk.drag_status(context, Gdk.DragAction.MOVE, time) |
| 801 |
# Change target as well |
| 802 |
self.tree.enable_model_drag_dest( |
| 803 |
[self.playlist_target], Gdk.DragAction.DEFAULT |
| 804 |
) |
| 805 |
else: |
| 806 |
Gdk.drag_status(context, Gdk.DragAction.COPY, time) |
| 807 |
elif isinstance(drop_target, TrackWrapper): |
| 808 |
# We are dragging onto another track |
| 809 |
# make it a move operation if we are only dragging |
| 810 |
# tracks within our widget |
| 811 |
# We do a copy if we are draggin from another playlist |
| 812 |
if Gtk.drag_get_source_widget(context) == tv and not dragging_playlist: |
| 813 |
Gdk.drag_status(context, Gdk.DragAction.MOVE, time) |
| 814 |
else: |
| 815 |
Gdk.drag_status(context, Gdk.DragAction.COPY, time) |
| 816 |
else: |
| 817 |
# Prevent drop operation by changing the targets |
| 818 |
self.tree.enable_model_drag_dest( |
| 819 |
self.deny_targets, Gdk.DragAction.DEFAULT |
| 820 |
) |
| 821 |
return False |
| 822 |
return True |
| 823 |
else: # No drop info |
| 824 |
if dragging_playlist: |
| 825 |
Gdk.drag_status(context, Gdk.DragAction.MOVE, time) |
| 826 |
# Change target as well |
| 827 |
self.tree.enable_model_drag_dest( |
| 828 |
[self.playlist_target], Gdk.DragAction.DEFAULT |
| 829 |
) |
| 830 |
return True |
| 831 |
return False |
| 832 |
|
| 833 |
def on_key_released(self, widget, event): |
| 834 |
""" |
| 835 |
Called when a key is released in the tree |
| 836 |
""" |
| 837 |
if event.keyval == Gdk.KEY_Menu: |
| 838 |
(mods, paths) = self.tree.get_selection().get_selected_rows() |
| 839 |
if paths and paths[0]: |
| 840 |
iter = self.model.get_iter(paths[0]) |
| 841 |
pl = self.model.get_value(iter, 2) |
| 842 |
# Based on what is selected determines what |
| 843 |
# menu we will show |
| 844 |
if isinstance(pl, Playlist): |
| 845 |
Gtk.Menu.popup( |
| 846 |
self.playlist_menu, None, None, None, None, 0, event.time |
| 847 |
) |
| 848 |
elif isinstance(pl, SmartPlaylist): |
| 849 |
Gtk.Menu.popup( |
| 850 |
self.smart_menu, None, None, None, None, 0, event.time |
| 851 |
) |
| 852 |
elif isinstance(pl, TrackWrapper): |
| 853 |
Gtk.Menu.popup( |
| 854 |
self.track_menu, None, None, None, None, 0, event.time |
| 855 |
) |
| 856 |
else: |
| 857 |
Gtk.Menu.popup( |
| 858 |
self.default_menu, None, None, None, None, 0, event.time |
| 859 |
) |
| 860 |
return True |
| 861 |
|
| 862 |
if event.keyval == Gdk.KEY_Left: |
| 863 |
(mods, paths) = self.tree.get_selection().get_selected_rows() |
| 864 |
if paths and paths[0]: |
| 865 |
self.tree.collapse_row(paths[0]) |
| 866 |
return True |
| 867 |
|
| 868 |
if event.keyval == Gdk.KEY_Right: |
| 869 |
(mods, paths) = self.tree.get_selection().get_selected_rows() |
| 870 |
if paths and paths[0]: |
| 871 |
self.tree.expand_row(paths[0], False) |
| 872 |
return True |
| 873 |
|
| 874 |
if event.keyval == Gdk.KEY_Delete: |
| 875 |
(mods, paths) = self.tree.get_selection().get_selected_rows() |
| 876 |
if paths and paths[0]: |
| 877 |
iter = self.model.get_iter(paths[0]) |
| 878 |
pl = self.model.get_value(iter, 2) |
| 879 |
# Based on what is selected determines what |
| 880 |
# menu we will show |
| 881 |
if isinstance(pl, (Playlist, SmartPlaylist)): |
| 882 |
self.remove_playlist(pl) |
| 883 |
elif isinstance(pl, TrackWrapper): |
| 884 |
self.remove_selected_track() |
| 885 |
return True |
| 886 |
return False |
| 887 |
|
| 888 |
def _clear_node(self, node): |
| 889 |
""" |
| 890 |
Clears a node of all children |
| 891 |
""" |
| 892 |
iter = self.model.iter_children(node) |
| 893 |
while True: |
| 894 |
if not iter: |
| 895 |
break |
| 896 |
self.model.remove(iter) |
| 897 |
iter = self.model.iter_children(node) |
| 898 |
|
| 899 |
|
| 900 |
class PlaylistDragTreeView(DragTreeView): |
| 901 |
""" |
| 902 |
Custom DragTreeView to retrieve data from playlists |
| 903 |
""" |
| 904 |
|
| 905 |
def __init__(self, container, receive=True, source=True): |
| 906 |
DragTreeView.__init__(self, container, receive, source) |
| 907 |
self.show_cover_drag_icon = False |
| 908 |
|
| 909 |
def get_selection_empty(self): |
| 910 |
'''Returns True if there are no selected items''' |
| 911 |
return self.get_selection().count_selected_rows() == 0 |
| 912 |
|
| 913 |
def get_selection_is_computed(self): |
| 914 |
""" |
| 915 |
Returns True if selection is a Smart Playlist |
| 916 |
""" |
| 917 |
item = self.get_selected_item(raw=True) |
| 918 |
return isinstance(item, SmartPlaylist) |
| 919 |
|
| 920 |
def get_selected_tracks(self): |
| 921 |
""" |
| 922 |
Used by the menu, just basically gets the selected |
| 923 |
playlist and returns the tracks in it |
| 924 |
""" |
| 925 |
playlist = self.get_selected_page() |
| 926 |
|
| 927 |
if playlist is not None: |
| 928 |
return [track for track in playlist] |
| 929 |
else: |
| 930 |
return [self.get_selected_track()] |
| 931 |
|
| 932 |
return None |
| 933 |
|
| 934 |
def get_selected_page(self, raw=False): |
| 935 |
""" |
| 936 |
Retrieves the currently selected playlist in |
| 937 |
the playlists panel. If a non-playlist is |
| 938 |
selected it returns None |
| 939 |
|
| 940 |
@return: the playlist |
| 941 |
""" |
| 942 |
item = self.get_selected_item(raw=raw) |
| 943 |
|
| 944 |
if isinstance(item, (Playlist, SmartPlaylist)): |
| 945 |
return item |
| 946 |
else: |
| 947 |
return None |
| 948 |
|
| 949 |
def get_selected_track(self): |
| 950 |
item = self.get_selected_item() |
| 951 |
|
| 952 |
if not item: |
| 953 |
return None |
| 954 |
|
| 955 |
if isinstance(item, TrackWrapper): |
| 956 |
return item.track |
| 957 |
else: |
| 958 |
return None |
| 959 |
|
| 960 |
def get_selected_item(self, raw=False): |
| 961 |
(model, iter) = self.get_selection().get_selected() |
| 962 |
|
| 963 |
if not iter: |
| 964 |
return None |
| 965 |
|
| 966 |
item = model.get_value(iter, 2) |
| 967 |
|
| 968 |
# for smart playlists |
| 969 |
if isinstance(item, SmartPlaylist): |
| 970 |
if raw: |
| 971 |
return item |
| 972 |
try: |
| 973 |
return item.get_playlist(self.container.collection) |
| 974 |
except Exception: |
| 975 |
return None |
| 976 |
if isinstance(item, radio.RadioItem): |
| 977 |
if raw: |
| 978 |
return item |
| 979 |
return item.get_playlist() |
| 980 |
elif isinstance(item, Playlist): |
| 981 |
return item |
| 982 |
elif isinstance(item, TrackWrapper): |
| 983 |
return item |
| 984 |
else: |
| 985 |
return None |