/usr/share/pyshared/quodlibet/qltk/songsmenu.py is in exfalso 3.0.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | # Copyright 2006 Joe Wreschnig
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation
from gi.repository import Gtk, Gdk
from quodlibet import qltk
from quodlibet.util import print_exc
from quodlibet.qltk.delete import DeleteDialog
from quodlibet.qltk.information import Information
from quodlibet.qltk.properties import SongProperties
from quodlibet.qltk.x import SeparatorMenuItem
from quodlibet.plugins import PluginManager
from quodlibet.plugins.songsmenu import SongsMenuPlugin
from quodlibet.util.songwrapper import ListWrapper, check_wrapper_changed
class SongsMenuPluginHandler(object):
def __init__(self):
self.__plugins = []
def Menu(self, library, parent, songs):
songs = ListWrapper(songs)
parent = qltk.get_top_parent(parent)
attrs = ['plugin_song', 'plugin_songs',
'plugin_album', 'plugin_albums']
if len(songs) == 1:
attrs.append('plugin_single_song')
last = (songs and songs[-1]) or None
for song in songs:
if song.album_key != last.album_key:
break
last = song
else:
attrs.append('plugin_single_album')
items = []
kinds = self.__plugins
kinds.sort(key=lambda plugin: plugin.PLUGIN_ID)
for Kind in kinds:
usable = max([callable(getattr(Kind, s)) for s in attrs])
if usable:
try:
items.append(Kind(songs, library, parent))
except:
print_e("Couldn't initalise song plugin %s. Stack trace:"
% Kind)
print_exc()
items = filter(lambda i: i.initialized, items)
if items:
menu = Gtk.Menu()
for item in items:
try:
menu.append(item)
args = (library, parent, songs)
if item.get_submenu():
for subitem in item.get_submenu().get_children():
subitem.connect_object(
'activate', self.__handle, item, *args)
else:
item.connect('activate', self.__handle, *args)
except:
print_exc()
item.destroy()
else:
menu = None
return menu
def __get_albums(self, songs):
albums = {}
for song in songs:
key = song.album_key
if key not in albums:
albums[key] = []
albums[key].append(song)
albums = albums.values()
map(list.sort, albums)
return albums
def handle(self, plugin_id, library, parent, songs):
"""Start a song menu plugin directly without a menu"""
for plugin in self.__plugins:
if plugin.PLUGIN_ID == plugin_id:
songs = ListWrapper(songs)
try:
plugin = plugin(songs, library, parent)
except Exception:
print_exc()
else:
self.__handle(plugin, library, parent, songs)
return
def __handle(self, plugin, library, parent, songs):
if len(songs) == 0:
return
try:
if len(songs) == 1 and callable(plugin.plugin_single_song):
try:
ret = plugin.plugin_single_song(songs[0])
except Exception:
print_exc()
else:
if ret:
return
if callable(plugin.plugin_song):
try:
ret = map(plugin.plugin_song, songs)
except Exception:
print_exc()
else:
if max(ret):
return
if callable(plugin.plugin_songs):
try:
ret = plugin.plugin_songs(songs)
except Exception:
print_exc()
else:
if ret:
return
if max(map(callable, (plugin.plugin_single_album,
plugin.plugin_album, plugin.plugin_albums))):
albums = self.__get_albums(songs)
if callable(plugin.plugin_single_album) and len(albums) == 1:
try:
ret = plugin.plugin_single_album(albums[0])
except Exception:
print_exc()
else:
if ret:
return
if callable(plugin.plugin_album):
try:
ret = map(plugin.plugin_album, albums)
except Exception:
print_exc()
else:
if max(ret):
return
if callable(plugin.plugin_albums):
try:
ret = plugin.plugin_albums(albums)
except Exception:
print_exc()
else:
if ret:
return
finally:
check_wrapper_changed(library, parent, filter(None, songs))
def plugin_handle(self, plugin):
return issubclass(plugin, SongsMenuPlugin)
def plugin_enable(self, plugin, obj):
self.__plugins.append(plugin)
def plugin_disable(self, plugin):
self.__plugins.remove(plugin)
class SongsMenu(Gtk.Menu):
__accels = Gtk.AccelGroup()
plugins = SongsMenuPluginHandler()
@classmethod
def init_plugins(cls):
PluginManager.instance.register_handler(cls.plugins)
def __init__(self, library, songs, plugins=True, playlists=True,
queue=True, devices=True, remove=True, delete=False,
edit=True, accels=None, parent=None):
super(SongsMenu, self).__init__()
# The library may actually be a librarian; if it is, use it,
# otherwise find the real librarian.
librarian = getattr(library, 'librarian', library)
if plugins:
submenu = self.plugins.Menu(librarian, parent, songs)
if submenu is not None:
b = qltk.MenuItem(_("_Plugins"), Gtk.STOCK_EXECUTE)
self.append(b)
b.set_submenu(submenu)
self.append(SeparatorMenuItem())
in_lib = True
can_add = True
is_file = True
for song in songs:
if song not in library:
in_lib = False
if not song.can_add:
can_add = False
if not song.is_file:
is_file = False
self.separate()
if playlists:
# Needed here to avoid a circular import; most browsers use
# a SongsMenu, but SongsMenu needs access to the playlist
# browser for this item.
# FIXME: Two things are now importing browsers, so we need
# some kind of inversion of control here.
from quodlibet import browsers
try:
submenu = browsers.playlists.Menu(songs, parent)
except AttributeError:
pass
else:
b = qltk.MenuItem(_("_Add to Playlist"), Gtk.STOCK_ADD)
b.set_sensitive(can_add)
b.set_submenu(submenu)
self.append(b)
if queue:
b = qltk.MenuItem(_("Add to _Queue"), Gtk.STOCK_ADD)
b.connect('activate', self.__enqueue, songs)
if accels is not None:
key, val = Gtk.accelerator_parse("<ctrl>Return")
b.add_accelerator(
'activate', accels, key, val, Gtk.AccelFlags.VISIBLE)
self.append(b)
b.set_sensitive(can_add)
if devices:
from quodlibet import browsers
try:
browsers.media
except AttributeError:
pass
else:
if browsers.media.MediaDevices in browsers.browsers:
submenu = browsers.media.Menu(songs, library)
b = qltk.MenuItem(_("_Copy to Device"), Gtk.STOCK_COPY)
b.set_sensitive(can_add and len(submenu) > 0)
b.set_submenu(submenu)
self.append(b)
if remove or delete or edit:
self.separate()
if remove:
b = qltk.MenuItem(_("_Remove from library"), Gtk.STOCK_REMOVE)
if callable(remove):
b.connect_object('activate', remove, songs)
else:
b.connect('activate', self.__remove, songs, library)
b.set_sensitive(in_lib)
self.append(b)
if delete:
b = Gtk.ImageMenuItem(Gtk.STOCK_DELETE, use_stock=True)
if callable(delete):
b.connect_object('activate', delete, songs)
else:
b.connect('activate', self.__delete, songs, librarian)
b.set_sensitive(is_file)
self.append(b)
if edit:
b = qltk.MenuItem(_("Edit _Tags"), Gtk.STOCK_PROPERTIES)
if accels is not None:
key, val = Gtk.accelerator_parse("<alt>Return")
b.add_accelerator(
'activate', accels, key, val, Gtk.AccelFlags.VISIBLE)
b.connect_object(
'activate', SongProperties, librarian, songs, parent)
self.append(b)
b = Gtk.ImageMenuItem(Gtk.STOCK_INFO, use_stock=True)
if accels is not None:
b.add_accelerator('activate', accels, ord('I'),
Gdk.ModifierType.CONTROL_MASK,
Gtk.AccelFlags.VISIBLE)
b.connect_object('activate', Information, librarian, songs, parent)
self.append(b)
self.connect_object('selection-done', Gtk.Menu.destroy, self)
def separate(self):
if not self.get_children():
return
elif not isinstance(self.get_children()[-1], Gtk.SeparatorMenuItem):
self.append(SeparatorMenuItem())
def preseparate(self):
if not self.get_children():
return
elif not isinstance(self.get_children()[0], Gtk.SeparatorMenuItem):
self.prepend(SeparatorMenuItem())
def __remove(self, item, songs, library):
library.remove(set(songs))
def __enqueue(self, item, songs):
songs = filter(lambda s: s.can_add, songs)
if songs:
from quodlibet import app
app.window.playlist.enqueue(songs)
def __delete(self, item, songs, library):
songs = set(songs)
files = [song["~filename"] for song in songs]
d = DeleteDialog(None, files)
removed = dict.fromkeys(d.run())
d.destroy()
removed = filter(lambda s: s["~filename"] in removed, songs)
if removed:
try:
library.librarian.remove(removed)
except AttributeError:
library.remove(removed)
|