/usr/lib/python3/dist-packages/gnomemusic/view.py is in gnome-music 3.10.3-0ubuntu1.
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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | # Copyright (c) 2013 Vadim Rutkovsky <vrutkovs@redhat.com>
# Copyright (c) 2013 Eslam Mostafa <cseslam@gmail.com>
# Copyright (c) 2013 Sai Suman Prayaga <suman.sai14@gmail.com>
# Copyright (c) 2013 Arnel A. Borja <kyoushuu@yahoo.com>
# Copyright (c) 2013 Shivani Poddar <shivani.poddar92@gmail.com>
# Copyright (c) 2013 Seif Lotfy <seif@lotfy.com>
# Copyright (c) 2013 Giovanni Campagna <scampa.giovanni@gmail.com>
# Copyright (c) 2013 Jackson Isaac <jacksonisaac2008@gmail.com>
# Copyright (c) 2013 Guillaume Quintard <guillaume.quintard@gmail.com>
#
# GNOME Music is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# GNOME Music is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with GNOME Music; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# The GNOME Music authors hereby grant permission for non-GPL compatible
# GStreamer plugins to be used and distributed together with GStreamer
# and GNOME Music. This permission is above and beyond the permissions
# granted by the GPL license by which GNOME Music is covered. If you
# modify this code, you may extend this exception to your version of the
# code, but you are not obligated to do so. If you do not wish to do so,
# delete this exception statement from your version.
from gi.repository import Gtk
from gi.repository import GObject
from gi.repository import Gd
from gi.repository import Grl
from gi.repository import Pango
from gi.repository import GLib
from gi.repository import GdkPixbuf
from gi.repository import Tracker
from gi.repository import Gio
from gettext import gettext as _
from gnomemusic.grilo import grilo
import gnomemusic.widgets as Widgets
from gnomemusic.query import Query
from gnomemusic.albumArtCache import AlbumArtCache as albumArtCache
tracker = Tracker.SparqlConnection.get(None)
if Gtk.get_minor_version() > 8:
from gi.repository.Gtk import Stack, StackTransitionType
else:
from gi.repository.Gd import Stack, StackTransitionType
class ViewContainer(Stack):
if Gtk.Widget.get_default_direction() is not Gtk.TextDirection.RTL:
nowPlayingIconName = 'media-playback-start-symbolic'
else:
nowPlayingIconName = 'media-playback-start-rtl-symbolic'
errorIconName = 'dialog-error-symbolic'
starIconName = 'starred-symbolic'
countQuery = None
filter = None
def __init__(self, title, header_bar, selection_toolbar, useStack=False):
Stack.__init__(self,
transition_type=StackTransitionType.CROSSFADE)
self._grid = Gtk.Grid(orientation=Gtk.Orientation.HORIZONTAL)
self._iconWidth = 128
self._iconHeight = 128
self._offset = 0
self._adjustmentValueId = 0
self._adjustmentChangedId = 0
self._scrollbarVisibleId = 0
self._model = Gtk.ListStore(
GObject.TYPE_STRING,
GObject.TYPE_STRING,
GObject.TYPE_STRING,
GObject.TYPE_STRING,
GdkPixbuf.Pixbuf,
GObject.TYPE_OBJECT,
GObject.TYPE_BOOLEAN,
GObject.TYPE_INT,
GObject.TYPE_STRING,
GObject.TYPE_BOOLEAN,
GObject.TYPE_BOOLEAN
)
self.view = Gd.MainView(
shadow_type=Gtk.ShadowType.NONE
)
self.view.set_view_type(Gd.MainViewType.ICON)
self.filter = self._model.filter_new(None)
self.view.set_model(self.filter)
self.vadjustment = self.view.get_vadjustment()
self.selection_toolbar = selection_toolbar
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
box.pack_start(self.view, True, True, 0)
if useStack:
self.stack = Stack(
transition_type=StackTransitionType.SLIDE_RIGHT,
)
dummy = Gtk.Frame(visible=False)
self.stack.add_named(dummy, 'dummy')
self.stack.add_named(box, 'artists')
self.stack.set_visible_child_name('dummy')
self._grid.add(self.stack)
else:
self._grid.add(box)
self._cached_count = -1
self._loadMore = Widgets.LoadMoreButton(self._get_remaining_item_count)
box.pack_end(self._loadMore.widget, False, False, 0)
self._loadMore.widget.connect('clicked', self._populate)
self.view.connect('item-activated', self._on_item_activated)
self.view.connect('selection-mode-request', self._on_selection_mode_request)
self._cursor = None
self.header_bar = header_bar
self.header_bar._select_button.connect(
'toggled', self._on_header_bar_toggled)
self.header_bar._cancel_button.connect(
'clicked', self._on_cancel_button_clicked)
self.title = title
self.add(self._grid)
self.show_all()
self._items = []
self._loadMore.widget.hide()
self._connect_view()
self.cache = albumArtCache.get_default()
self._symbolicIcon = self.cache.make_default_icon(self._iconHeight,
self._iconWidth)
self._init = False
grilo.connect('ready', self._on_grilo_ready)
self.header_bar.header_bar.connect('state-changed',
self._on_state_changed)
self.view.connect('view-selection-changed',
self._on_view_selection_changed)
self._discovering_urls = {}
def _get_remaining_item_count(self):
if self._cached_count < 0:
self._cached_count = Widgets.get_count(self.countQuery)
return self._cached_count - self._offset
def _on_header_bar_toggled(self, button):
if button.get_active():
self.view.set_selection_mode(True)
self.header_bar.set_selection_mode(True)
self.selection_toolbar.eventbox.set_visible(True)
self.selection_toolbar._add_to_playlist_button.sensitive = False
else:
self.view.set_selection_mode(False)
self.header_bar.set_selection_mode(False)
self.selection_toolbar.eventbox.set_visible(False)
def _on_cancel_button_clicked(self, button):
self.view.set_selection_mode(False)
self.header_bar.set_selection_mode(False)
def _on_grilo_ready(self, data=None):
if (self.header_bar.get_stack().get_visible_child() == self
and not self._init):
self._populate()
self.header_bar.get_stack().connect('notify::visible-child',
self._on_headerbar_visible)
def _on_headerbar_visible(self, widget, param):
if self == widget.get_visible_child() and not self._init:
self._populate()
def _on_view_selection_changed(self, widget):
items = self.view.get_selection()
self.selection_toolbar\
._add_to_playlist_button.set_sensitive(len(items) > 0)
def _populate(self, data=None):
self._init = True
self.populate()
def _on_state_changed(self, widget, data=None):
pass
def _connect_view(self):
self._adjustmentValueId = self.vadjustment.connect(
'value-changed',
self._on_scrolled_win_change)
def _on_scrolled_win_change(self, data=None):
vScrollbar = self.view.get_vscrollbar()
revealAreaHeight = 32
#if there's no vscrollbar, or if it's not visible, hide the button
if not vScrollbar or not vScrollbar.get_visible():
self._loadMore.set_block(True)
return
value = self.vadjustment.get_value()
upper = self.vadjustment.get_upper()
page_size = self.vadjustment.get_page_size()
end = False
#special case self values which happen at construction
if (value == 0) and (upper == 1) and (page_size == 1):
end = False
else:
end = not (value < (upper - page_size - revealAreaHeight))
if self._get_remaining_item_count() <= 0:
end = False
self._loadMore.set_block(not end)
def populate(self):
print('populate')
def _on_discovered(self, info, error, _iter):
if error:
self._model.set(_iter, [8, 10], [self.errorIconName, True])
def _add_item(self, source, param, item):
if not item:
return
self._offset += 1
artist = item.get_string(Grl.METADATA_KEY_ARTIST)\
or item.get_author()\
or _("Unknown Artist")
title = albumArtCache.get_media_title(item)
item.set_title(title)
def add_new_item():
_iter = self._model.append(None)
icon_name = self.nowPlayingIconName
if item.get_url():
try:
self.player.discoverer.discover_uri(item.get_url())
except:
print('failed to discover url ' + item.get_url())
icon_name = self.errorIconName
self._model.set(_iter,
[0, 1, 2, 3, 4, 5, 7, 8, 9, 10],
[str(item.get_id()), '', title,
artist, self._symbolicIcon, item,
0, icon_name, False, icon_name == self.errorIconName])
GLib.idle_add(self._update_album_art, item, _iter)
GLib.idle_add(add_new_item)
def _insert_album_art(self, item, cb_item, itr, x=False):
if item and cb_item and not item.get_thumbnail():
if cb_item.get_thumbnail():
item.set_thumbnail(cb_item.get_thumbnail())
albumArtCache.get_default().lookup(
item,
self._iconWidth,
self._iconHeight,
self._on_lookup_ready, itr)
def _update_album_art(self, item, itr):
grilo.get_album_art_for_album_id(
item.get_id(),
lambda source, count, cb_item, x, y, z:
self._insert_album_art(item, cb_item, itr, True)
)
def _on_lookup_ready(self, icon, path, _iter):
if icon:
self._model.set_value(
_iter, 4,
albumArtCache.get_default()._make_icon_frame(icon))
self.view.queue_draw()
def _add_list_renderers(self):
pass
def _on_item_activated(self, widget, id, path):
pass
def _on_selection_mode_request(self, *args):
self.header_bar._select_button.clicked()
#Class for the Empty View
class Empty(Stack):
def __init__(self, header_bar, player):
Stack.__init__(self,
transition_type=StackTransitionType.CROSSFADE)
builder = Gtk.Builder()
builder.add_from_resource('/org/gnome/Music/NoMusic.ui')
music_folder_path = GLib.get_user_special_dir(GLib.USER_DIRECTORY_MUSIC)
widget = builder.get_object('container')
label = builder.get_object('label1')
label.set_label(_("No Music found!\n Put some files into the folder %s") % music_folder_path)
self.add(widget)
self.show_all()
class Albums(ViewContainer):
def __init__(self, header_bar, selection_toolbar, player):
ViewContainer.__init__(self, _("Albums"), header_bar,
selection_toolbar)
self.view.set_view_type(Gd.MainViewType.ICON)
self.countQuery = Query.ALBUMS_COUNT
self._albumWidget = Widgets.AlbumWidget(player)
self.player = player
self.add(self._albumWidget)
def _back_button_clicked(self, widget, data=None):
self.set_visible_child(self._grid)
def _on_item_activated(self, widget, id, path):
child_path = self.filter.convert_path_to_child_path(path)
_iter = self._model.get_iter(child_path)
title = self._model.get_value(_iter, 2)
artist = self._model.get_value(_iter, 3)
item = self._model.get_value(_iter, 5)
self._albumWidget.update(artist, title, item,
self.header_bar, self.selection_toolbar)
self.header_bar.set_state(0)
escaped_title = albumArtCache.get_media_title(item)
self.header_bar.header_bar.set_title(escaped_title)
self.header_bar.header_bar.sub_title = artist
self.set_visible_child(self._albumWidget)
def populate(self):
if grilo.tracker:
GLib.idle_add(grilo.populate_albums, self._offset, self._add_item)
class Songs(ViewContainer):
def __init__(self, header_bar, selection_toolbar, player):
ViewContainer.__init__(self, _("Songs"), header_bar, selection_toolbar)
self.countQuery = Query.SONGS_COUNT
self._items = {}
self.monitors = []
self.isStarred = None
self.iter_to_clean = None
self.view.set_view_type(Gd.MainViewType.LIST)
self.view.get_generic_view().get_style_context()\
.add_class('songs-list')
self._iconHeight = 32
self._iconWidth = 32
self.cache = albumArtCache.get_default()
self._symbolicIcon = self.cache.make_default_icon(self._iconHeight,
self._iconWidth)
self._add_list_renderers()
self.player = player
self.player.connect('playlist-item-changed', self.update_model)
def _on_item_activated(self, widget, id, path):
_iter = self.filter.get_iter(path)
child_iter = self.filter.convert_iter_to_child_iter(_iter)
if self._model.get_value(child_iter, 8) != self.errorIconName:
self.player.set_playlist('Songs', None, self.filter, _iter, 5)
self.player.set_playing(True)
def update_model(self, player, playlist, currentIter):
if self.iter_to_clean:
self._model.set_value(self.iter_to_clean, 10, False)
if playlist != self.filter:
return False
child_iter = self.filter.convert_iter_to_child_iter(currentIter)
self._model.set_value(child_iter, 10, True)
if self._model.get_value(child_iter, 8) != self.errorIconName:
self.iter_to_clean = child_iter.copy()
return False
def _add_item(self, source, param, item):
if not item:
return
self._offset += 1
item.set_title(albumArtCache.get_media_title(item))
artist = item.get_string(Grl.METADATA_KEY_ARTIST)\
or item.get_author()\
or _("Unknown Artist")
_iter = self._model.insert_with_valuesv(
-1,
[2, 3, 5, 8, 9, 10],
[albumArtCache.get_media_title(item),
artist, item, self.nowPlayingIconName, False, False])
self.player.discover_item(item, self._on_discovered, _iter)
g_file = Gio.file_new_for_uri(item.get_url())
self.monitors.append(g_file.monitor_file(Gio.FileMonitorFlags.NONE,
None))
self.monitors[(self._offset - 1)].connect('changed',
self._on_item_changed, _iter)
def _on_item_changed(self, monitor, file1, file2, event, _iter):
if self._model.iter_is_valid(_iter):
if event == Gio.FileMonitorEvent.DELETED:
self._model.set(_iter, [8, 10], [self.errorIconName, True])
def _add_list_renderers(self):
list_widget = self.view.get_generic_view()
cols = list_widget.get_columns()
cells = cols[0].get_cells()
cells[2].set_visible(False)
now_playing_symbol_renderer = Gtk.CellRendererPixbuf(xalign=1.0)
column_now_playing = Gtk.TreeViewColumn()
column_now_playing.set_property('fixed_width', 24)
column_now_playing.pack_start(now_playing_symbol_renderer, False)
column_now_playing.add_attribute(now_playing_symbol_renderer,
'visible', 10)
column_now_playing.add_attribute(now_playing_symbol_renderer,
'icon_name', 8)
list_widget.insert_column(column_now_playing, 0)
title_renderer = Gtk.CellRendererText(
xpad=0,
xalign=0.0,
yalign=0.5,
height=48,
ellipsize=Pango.EllipsizeMode.END
)
list_widget.add_renderer(title_renderer,
self._on_list_widget_title_render, None)
cols[0].add_attribute(title_renderer, 'text', 2)
star_renderer = Gtk.CellRendererPixbuf(
xpad=32,
icon_name=self.starIconName
)
list_widget.add_renderer(star_renderer,
self._on_list_widget_star_render, None)
cols[0].add_attribute(star_renderer, 'visible', 9)
duration_renderer = Gd.StyledTextRenderer(
xpad=32,
xalign=1.0
)
duration_renderer.add_class('dim-label')
list_widget.add_renderer(duration_renderer,
self._on_list_widget_duration_render, None)
artist_renderer = Gd.StyledTextRenderer(
xpad=32,
ellipsize=Pango.EllipsizeMode.END
)
artist_renderer.add_class('dim-label')
list_widget.add_renderer(artist_renderer,
self._on_list_widget_artist_render, None)
cols[0].add_attribute(artist_renderer, 'text', 3)
type_renderer = Gd.StyledTextRenderer(
xpad=32,
ellipsize=Pango.EllipsizeMode.END
)
type_renderer.add_class('dim-label')
list_widget.add_renderer(type_renderer,
self._on_list_widget_type_render, None)
def _on_list_widget_title_render(self, col, cell, model, _iter, data):
pass
def _on_list_widget_star_render(self, col, cell, model, _iter, data):
pass
def _on_list_widget_duration_render(self, col, cell, model, _iter, data):
item = model.get_value(_iter, 5)
if item:
seconds = item.get_duration()
minutes = seconds // 60
seconds %= 60
cell.set_property('text', '%i:%02i' % (minutes, seconds))
def _on_list_widget_artist_render(self, col, cell, model, _iter, data):
pass
def _on_list_widget_type_render(self, coll, cell, model, _iter, data):
item = model.get_value(_iter, 5)
if item:
cell.set_property('text', item.get_string(Grl.METADATA_KEY_ALBUM) or _("Unknown Album"))
def populate(self):
if grilo.tracker:
GLib.idle_add(grilo.populate_songs, self._offset, self._add_item)
class Playlist(ViewContainer):
def __init__(self, header_bar, selection_toolbar, player):
ViewContainer.__init__(self, _("Playlists"), header_bar,
selection_toolbar)
class Artists (ViewContainer):
def __init__(self, header_bar, selection_toolbar, player):
ViewContainer.__init__(self, _("Artists"), header_bar,
selection_toolbar, True)
self.artists_counter = 0
self.player = player
self._artists = {}
self.countQuery = Query.ARTISTS_COUNT
self.artistAlbumsStack = Stack(
transition_type=StackTransitionType.CROSSFADE,
)
self._artistAlbumsWidget = Gtk.Frame(
shadow_type=Gtk.ShadowType.NONE,
hexpand=True
)
self.artistAlbumsStack.add_named(self._artistAlbumsWidget, "artists")
self.artistAlbumsStack.set_visible_child_name("artists")
self.view.set_view_type(Gd.MainViewType.LIST)
self.view.set_hexpand(False)
self.view.get_style_context().add_class('artist-panel')
self.view.get_generic_view().get_selection().set_mode(
Gtk.SelectionMode.SINGLE)
self._grid.attach(Gtk.Separator(orientation=Gtk.Orientation.VERTICAL),
1, 0, 1, 1)
self._grid.attach(self.artistAlbumsStack, 2, 0, 2, 2)
self._add_list_renderers()
if (Gtk.Settings.get_default().get_property(
'gtk_application_prefer_dark_theme')):
self.view.get_generic_view().get_style_context().\
add_class('artist-panel-dark')
else:
self.view.get_generic_view().get_style_context().\
add_class('artist-panel-white')
self.show_all()
def _populate(self, data=None):
selection = self.view.get_generic_view().get_selection()
if not selection.get_selected()[1]:
self._allIter = self._model.insert_with_valuesv(-1, [2], [_("All Artists")])
self._last_selection = self._allIter
self._artists[_("All Artists").lower()] =\
{'iter': self._allIter, 'albums': []}
selection.select_path(self._model.get_path(self._allIter))
self.view.emit('item-activated', '0',
self._model.get_path(self._allIter))
self._init = True
self.populate()
def _add_list_renderers(self):
list_widget = self.view.get_generic_view()
cols = list_widget.get_columns()
cells = cols[0].get_cells()
cells[1].set_visible(False)
cells[2].set_visible(False)
type_renderer = Gd.StyledTextRenderer(
xpad=16,
ypad=16,
ellipsize=Pango.EllipsizeMode.END,
xalign=0.0,
width=220
)
list_widget.add_renderer(type_renderer, lambda *args: None, None)
cols[0].clear_attributes(type_renderer)
cols[0].add_attribute(type_renderer, 'text', 2)
def _on_item_activated(self, widget, item_id, path):
# Prepare a new artistAlbumsWidget here
self.new_artistAlbumsWidget = Gtk.Frame(
shadow_type=Gtk.ShadowType.NONE,
hexpand=True
)
child_name = "artists_%i" % self.artists_counter
self.artistAlbumsStack.add_named(self.new_artistAlbumsWidget, child_name)
child_path = self.filter.convert_path_to_child_path(path)
_iter = self._model.get_iter(child_path)
self._last_selection = _iter
artist = self._model.get_value(_iter, 2)
albums = self._artists[artist.lower()]['albums']
self.artistAlbums = None
if (self._model.get_string_from_iter(_iter) ==
self._model.get_string_from_iter(self._allIter)):
self.artistAlbums = Widgets.AllArtistsAlbums(self.player)
else:
self.artistAlbums = Widgets.ArtistAlbums(artist, albums,
self.player)
self.new_artistAlbumsWidget.add(self.artistAlbums)
self.new_artistAlbumsWidget.show()
# Switch visible child
child_name = "artists_%i" % self.artists_counter
self.artists_counter += 1
# Replace previous widget
self._artistAlbumsWidget = self.new_artistAlbumsWidget
GLib.idle_add(self.artistAlbumsStack.set_visible_child_name, child_name)
def _add_item(self, source, param, item):
if item is None:
return
self._offset += 1
artist = item.get_string(Grl.METADATA_KEY_ARTIST)\
or item.get_author()\
or _("Unknown Artist")
if not artist.lower() in self._artists:
_iter = self._model.insert_with_valuesv(-1, [2], [artist])
self._artists[artist.lower()] = {'iter': _iter, 'albums': []}
self._artists[artist.lower()]['albums'].append(item)
def populate(self):
if grilo.tracker:
GLib.idle_add(grilo.populate_artists, self._offset, self._add_item)
def _on_header_bar_toggled(self, button):
ViewContainer._on_header_bar_toggled(self, button)
if button.get_active():
self._last_selection =\
self.view.get_generic_view().get_selection().get_selected()[1]
self.view.get_generic_view().get_selection().set_mode(
Gtk.SelectionMode.NONE)
else:
self.view.get_generic_view().get_selection().set_mode(
Gtk.SelectionMode.SINGLE)
if self._last_selection is not None:
self.view.get_generic_view().get_selection().select_iter(
self._last_selection)
|