Debian Codegraph @

xlgui/widgets/menuitems.py

raw
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 Collection of useful stock MenuItems for use with xlgui.widgets.menu
29 """
30
31 # TODO: how should we document standardization of context's
32 # selected-(items|tracks) ?
33
34 from gi.repository import Gio
35 from gi.repository import GLib
36 from gi.repository import Gtk
37
38
39 from xl import common, player, playlist, settings, trax
40 from xl.nls import gettext as _
41 from xlgui.widgets import dialogs, rating, menu
42 from xlgui import panel, properties
43
44 ### TRACKS ITEMS ###
45 # These items act on a set of Tracks, by default 'selected-tracks' from
46 # the parent's context, but custom accessors are allowed via the
47 # get_tracks_func kwarg
48
49
50 def generic_get_playlist_func(parent, context):
51 return context.get('selected-playlist', None)
52
53
54 def generic_get_tracks_func(parent, context):
55 return context.get('selected-tracks', [])
56
57
58 class RatingMenuItem(menu.MenuItem):
59 """
60 A menu item displaying rating images
61 and allowing for selection of ratings
62 """
63
64 def __init__(self, name, after, get_tracks_func=generic_get_tracks_func):
65 menu.MenuItem.__init__(self, name, self.factory, after)
66 self.get_tracks_func = get_tracks_func
67 self.rating_set = False
68
69 # TODO: For accessibility it would be nice to add mnemonics or some
70 # other key shortcut thing to the RatingMenu, e.g. "+" and "-"
71
72 def factory(self, menu, parent, context):
73 # don't show rating widget for computed track selections (see #340)
74 if context.get('needs-computing'):
75 return
76 item = rating.RatingMenuItem()
77 item.connect('show', self.on_show, menu, parent, context)
78 self._rating_changed_id = item.connect(
79 'rating-changed', self.on_rating_changed, menu, parent, context
80 )
81
82 return item
83
84 # @common.threaded
85 # TODO: ASYNC
86 # most of this function isn't safe to use from a (interacts with UI elements)
87 def on_show(self, widget, menu, parent, context):
88 """
89 Updates the menu item on show
90 """
91 tracks = self.get_tracks_func(parent, context)
92 rating = trax.util.get_rating_from_tracks(tracks)
93 widget.disconnect(self._rating_changed_id)
94 widget.props.rating = rating
95 self._rating_changed_id = widget.connect(
96 'rating-changed', self.on_rating_changed, menu, parent, context
97 )
98
99 def on_rating_changed(self, widget, rating, menu, parent, context):
100 """
101 Passes the 'rating-changed' signal
102 """
103 tracks = self.get_tracks_func(parent, context)
104 for track in tracks:
105 track.set_rating(rating)
106
107
108 def _enqueue_cb(widget, name, parent, context, get_tracks_func):
109 tracks = get_tracks_func(parent, context)
110 player.QUEUE.extend(tracks)
111
112
113 def EnqueueMenuItem(name, after, get_tracks_func=generic_get_tracks_func):
114 return menu.simple_menu_item(
115 name,
116 after,
117 _("En_queue"),
118 'list-add',
119 _enqueue_cb,
120 callback_args=[get_tracks_func],
121 )
122
123
124 # TODO: move logic into (GUI?) playlist
125
126
127 def _append_cb(widget, name, parent, context, get_tracks_func, replace=False):
128 from xlgui import main
129
130 page = main.get_selected_playlist()
131 if not page:
132 return
133 pl = page.playlist
134 if replace:
135 pl.clear()
136 offset = len(pl)
137 tracks = get_tracks_func(parent, context)
138 # This is well-intentioned, but it leads to odd effects (see #147)
139 # -> this is more proof that the playlist needs to handle this logic
140 # sort_by, reverse = page.view.get_sort_by()
141 # tracks = trax.sort_tracks(sort_by, tracks, reverse=reverse,
142 # artist_compilations=True)
143 pl.extend(tracks)
144 if settings.get_option('playlist/append_menu_starts_playback', False):
145 if not player.PLAYER.current:
146 page.view.play_track_at(offset, tracks[0])
147
148
149 def ReplaceCurrentMenuItem(name, after, get_tracks_func=generic_get_tracks_func):
150 return menu.simple_menu_item(
151 name,
152 after,
153 _("_Replace Current"),
154 None,
155 _append_cb,
156 callback_args=[get_tracks_func, True],
157 )
158
159
160 def AppendMenuItem(name, after, get_tracks_func=generic_get_tracks_func):
161 return menu.simple_menu_item(
162 name,
163 after,
164 _("_Append to Current"),
165 'list-add',
166 _append_cb,
167 callback_args=[get_tracks_func],
168 )
169
170
171 def _properties_cb(widget, name, parent, context, get_tracks_func):
172 tracks = get_tracks_func(parent, context)
173 if tracks:
174 assert isinstance(parent, panel.Panel)
175 properties.TrackPropertiesDialog(parent.parent, tracks)
176
177
178 def PropertiesMenuItem(name, after, get_tracks_func=generic_get_tracks_func):
179 return menu.simple_menu_item(
180 name,
181 after,
182 _("_Track Properties"),
183 'document-properties',
184 _properties_cb,
185 callback_args=[get_tracks_func],
186 )
187
188
189 def _open_directory_cb(widget, name, parent, context, get_tracks_func):
190 try:
191 track = get_tracks_func(parent, context)[0]
192 except IndexError:
193 return
194 common.open_file_directory(track.get_loc_for_io())
195
196
197 def OpenDirectoryMenuItem(name, after, get_tracks_func=generic_get_tracks_func):
198 return menu.simple_menu_item(
199 name,
200 after,
201 _("_Open Directory"),
202 'folder-open',
203 _open_directory_cb,
204 callback_args=[get_tracks_func],
205 )
206
207
208 def generic_trash_tracks_func(parent, context, tracks):
209 for track in tracks:
210 gfile = Gio.File.new_for_uri(track.get_loc_for_io())
211 gfile.trash()
212
213
214 def generic_delete_tracks_func(parent, context, tracks):
215 for track in tracks:
216 gfile = Gio.File.new_for_uri(track.get_loc_for_io())
217 gfile.delete()
218
219
220 def _on_trash_tracks(
221 widget,
222 name,
223 parent,
224 context,
225 get_tracks_func,
226 trash_tracks_func,
227 delete_tracks_func,
228 ):
229 tracks = get_tracks_func(parent, context)
230
231 try:
232 trash_tracks_func(parent, context, tracks)
233 except GLib.Error:
234 dialog = Gtk.MessageDialog(
235 parent=parent.parent,
236 message_type=Gtk.MessageType.WARNING,
237 text=_(
238 'The files cannot be moved to the Trash. '
239 'Delete them permanently from the disk?'
240 ),
241 )
242 dialog.add_buttons(
243 Gtk.STOCK_CANCEL,
244 Gtk.ResponseType.CANCEL,
245 Gtk.STOCK_DELETE,
246 Gtk.ResponseType.OK,
247 )
248 dialog.set_alternative_button_order_from_array(
249 (Gtk.ResponseType.OK, Gtk.ResponseType.CANCEL)
250 )
251
252 if dialog.run() == Gtk.ResponseType.OK:
253 delete_tracks_func(parent, context, tracks)
254
255 dialog.destroy()
256
257
258 def TrashMenuItem(
259 name,
260 after,
261 get_tracks_func=generic_get_tracks_func,
262 trash_tracks_func=generic_trash_tracks_func,
263 delete_tracks_func=generic_delete_tracks_func,
264 ):
265 return menu.simple_menu_item(
266 name,
267 after,
268 _('_Move to Trash'),
269 'user-trash',
270 _on_trash_tracks,
271 callback_args=[get_tracks_func, trash_tracks_func, delete_tracks_func],
272 )
273
274
275 ### END TRACKS ITEMS ###
276
277 ### PLAYLIST ITEMS ###
278 # These items act on a playlist, by default 'selected-playlist' from
279 # the parent's context, but custom accessors are allowed via the
280 # get_pl_func kwarg
281
282
283 def RenamePlaylistMenuItem(name, after, get_pl_func=generic_get_playlist_func):
284 return menu.simple_menu_item(
285 name,
286 after,
287 _('_Rename'),
288 'accessories-text-editor',
289 lambda w, n, o, c: o.rename_playlist(get_pl_func(o, c)),
290 condition_fn=lambda n, p, c: not isinstance(
291 c['selected-playlist'], playlist.SmartPlaylist
292 ),
293 )
294
295
296 def EditPlaylistMenuItem(name, after, get_pl_func=generic_get_playlist_func):
297 return menu.simple_menu_item(
298 name,
299 after,
300 _('_Edit'),
301 'accessories-text-editor',
302 lambda w, n, o, c: o.edit_smart_playlist(get_pl_func(o, c)),
303 condition_fn=lambda n, p, c: isinstance(
304 c['selected-playlist'], playlist.SmartPlaylist
305 ),
306 )
307
308
309 def ExportPlaylistMenuItem(name, after, get_pl_func=generic_get_playlist_func):
310 return menu.simple_menu_item(
311 name,
312 after,
313 _('E_xport Playlist'),
314 'document-save-as',
315 lambda w, n, o, c: dialogs.export_playlist_dialog(get_pl_func(o, c)),
316 )
317
318
319 def ExportPlaylistFilesMenuItem(name, after, get_pl_func=generic_get_playlist_func):
320 return menu.simple_menu_item(
321 name,
322 after,
323 _('Export _Files'),
324 'document-save-as',
325 lambda w, n, o, c: dialogs.export_playlist_files(get_pl_func(o, c)),
326 )
327
328
329 def DeletePlaylistMenuItem(name, after, get_pl_func=generic_get_playlist_func):
330 return menu.simple_menu_item(
331 name,
332 after,
333 _('_Delete Playlist'),
334 'edit-delete',
335 lambda w, n, o, c: o.remove_playlist(get_pl_func(o, c)),
336 )
337
338
339 ### END PLAYLIST ITEMS ###