Commit 5d16c7b0 authored by Sai Suman Prayaga's avatar Sai Suman Prayaga Committed by Vadim Rutkovsky

Update songs when they are being added or deleted.

https://bugzilla.gnome.org/show_bug.cgi?id=700726
parent 445e2dad
......@@ -34,7 +34,8 @@ from gnomemusic.query import Query
class Grilo(GObject.GObject):
__gsignals__ = {
'ready': (GObject.SIGNAL_RUN_FIRST, None, ())
'ready': (GObject.SIGNAL_RUN_FIRST, None, ()),
'changes-pending': (GObject.SIGNAL_RUN_FIRST, None, ())
}
METADATA_KEYS = [
......@@ -48,6 +49,9 @@ class Grilo(GObject.GObject):
Grl.METADATA_KEY_THUMBNAIL,
]
CHANGED_MEDIA_MAX_ITEMS = 500
CHANGED_MEDIA_SIGNAL_TIMEOUT = 2000
def __init__(self):
GObject.GObject.__init__(self)
self.playlist_path = GLib.build_filenamev([GLib.get_user_data_dir(),
......@@ -60,6 +64,8 @@ class Grilo(GObject.GObject):
self.sources = {}
self.tracker = None
self.changed_media_ids = []
self.pending_event_id = 0
self.registry = Grl.Registry.get_default()
self.registry.connect('source_added', self._on_source_added)
......@@ -69,6 +75,25 @@ class Grilo(GObject.GObject):
self.registry.load_all_plugins()
except GLib.GError:
print('Failed to load plugins.')
if self.tracker is not None:
print("tracker is not none")
def _on_content_changed(self, mediaSource, changedMedias, changeType, locationUnknown):
if changeType == Grl.SourceChangeType.ADDED or changeType == Grl.SourceChangeType.REMOVED:
self.changed_media_ids.append(changedMedias[0].get_id())
if len(self.changed_media_ids) >= self.CHANGED_MEDIA_MAX_ITEMS:
self.emit_change_signal()
else:
if self.pending_event_id > 0:
GLib.Source.remove(self.pending_event_id)
self.pending_event_id = 0
self.pending_event_id = GLib.timeout_add(self.CHANGED_MEDIA_SIGNAL_TIMEOUT, self.emit_change_signal)
def emit_change_signal(self):
self.changed_media_ids = []
self.pending_event_id = 0
self.emit('changes-pending')
return False
def _on_source_added(self, pluginRegistry, mediaSource):
id = mediaSource.get_id()
......@@ -83,6 +108,8 @@ class Grilo(GObject.GObject):
if self.tracker is not None:
self.emit('ready')
self.tracker.notify_change_start()
self.tracker.connect('content-changed', self._on_content_changed)
def _on_source_removed(self, pluginRegistry, mediaSource):
print('source removed')
......
......@@ -56,7 +56,7 @@ class Toolbar(GObject.GObject):
def __init__(self):
GObject.GObject.__init__(self)
self._stack_switcher = StackSwitcher(margin_top=2, margin_bottom=2)
self._stack_switcher = StackSwitcher(margin_top=2, margin_bottom=2, can_focus=False)
self._stack_switcher.show()
self._ui = Gtk.Builder()
self._ui.add_from_resource('/org/gnome/Music/headerbar.ui')
......@@ -119,6 +119,12 @@ class Toolbar(GObject.GObject):
def get_stack(self):
return self._stack_switcher.get_stack()
def hide_stack(self):
self._stack_switcher.hide()
def show_stack(self):
self._stack_switcher.show()
def set_selection_mode(self, selectionMode):
self._selectionMode = selectionMode
if selectionMode:
......
......@@ -149,6 +149,10 @@ class ViewContainer(Stack):
self._on_view_selection_changed)
self._discovering_urls = {}
grilo.connect('changes-pending', self._on_changes_pending)
def _on_changes_pending(self, data=None):
pass
def _get_remaining_item_count(self):
if self._cached_count < 0:
......@@ -333,6 +337,13 @@ class Albums(ViewContainer):
self.items_selected = []
self.items_selected_callback = None
def _on_changes_pending(self, data=None):
if (self._init):
self._offset = 0
self._cached_count = -1
self._model.clear()
self.populate()
def _back_button_clicked(self, widget, data=None):
self.set_visible_child(self._grid)
......@@ -406,6 +417,13 @@ class Songs(ViewContainer):
self.player = player
self.player.connect('playlist-item-changed', self.update_model)
def _on_changes_pending(self, data=None):
if (self._init):
self._model.clear()
self._offset = 0
self._cached_count = -1
self.populate()
def _on_item_activated(self, widget, id, path):
_iter = self.filter.get_iter(path)
child_iter = self.filter.convert_iter_to_child_iter(_iter)
......@@ -579,6 +597,14 @@ class Artists (ViewContainer):
add_class('artist-panel-white')
self.show_all()
def _on_changes_pending(self, data=None):
if (self._init):
self._model.clear()
self._artists.clear()
self._offset = 0
self._cached_count = -1
self._populate()
def _populate(self, data=None):
selection = self.view.get_generic_view().get_selection()
if not selection.get_selected()[1]:
......
......@@ -39,6 +39,7 @@ from gnomemusic.query import Query
import gnomemusic.view as Views
import gnomemusic.widgets as Widgets
from gnomemusic.playlists import Playlists
from gnomemusic.grilo import grilo
playlist = Playlists.get_default()
tracker = Tracker.SparqlConnection.get(None)
......@@ -95,6 +96,31 @@ class Window(Gtk.ApplicationWindow):
except GLib.GError:
# We cannot grab media keys if no settings daemon is running
pass
grilo.connect('changes-pending', self._on_changes_pending)
def _on_changes_pending(self, data=None):
count = 1
cursor = tracker.query(Query.SONGS_COUNT, None)
if cursor is not None and cursor.next(None):
count = cursor.get_integer(0)
if not count > 0:
print("switching to Empty view")
self._stack.disconnect(self._on_notify_model_id)
self.disconnect(self._key_press_event_id)
for i in range(0, 4):
view = self.views.pop()
view.destroy()
self.toolbar.hide_stack()
self._switch_to_empty_view()
else:
if (self.views[0] == self.views[-1]):
print("switching to player view")
view = self.views.pop()
view.destroy()
self._switch_to_player_view()
self.toolbar._search_button.set_sensitive(True)
self.toolbar._select_button.set_sensitive(True)
self.toolbar.show_stack()
def _on_configure_event(self, widget, event):
size = widget.get_size()
......@@ -160,28 +186,10 @@ class Window(Gtk.ApplicationWindow):
if cursor is not None and cursor.next(None):
count = cursor.get_integer(0)
if count > 0:
self.views.append(Views.Albums(self.toolbar, self.selection_toolbar, self.player))
self.views.append(Views.Artists(self.toolbar, self.selection_toolbar, self.player))
self.views.append(Views.Songs(self.toolbar, self.selection_toolbar, self.player))
self.views.append(Views.Playlist(self.toolbar, self.selection_toolbar, self.player))
for i in self.views:
self._stack.add_titled(i, i.title, i.title)
self.toolbar.set_stack(self._stack)
self.toolbar.searchbar.show()
self._on_notify_model_id = self._stack.connect('notify::visible-child', self._on_notify_mode)
self.connect('destroy', self._notify_mode_disconnect)
self.connect('key_press_event', self._on_key_press)
self.views[0].populate()
self._switch_to_player_view()
#To revert to the No Music View when no songs are found
else:
self.views.append(Views.Empty(self.toolbar, self.player))
self._stack.add_titled(self.views[0], _("Empty"), _("Empty"))
self.toolbar._search_button.set_sensitive(False)
self.toolbar._select_button.set_sensitive(False)
self._switch_to_empty_view()
self.toolbar._search_button.connect('toggled', self._on_search_toggled)
self.toolbar.connect('selection-mode-changed', self._on_selection_mode_changed)
......@@ -196,6 +204,30 @@ class Window(Gtk.ApplicationWindow):
self._box.show()
self.show()
def _switch_to_empty_view(self):
self.views.append(Views.Empty(self.toolbar, self.player))
self._stack.add_titled(self.views[0], _("Empty"), _("Empty"))
self.toolbar._search_button.set_sensitive(False)
self.toolbar._select_button.set_sensitive(False)
def _switch_to_player_view(self):
self.views.append(Views.Albums(self.toolbar, self.selection_toolbar, self.player))
self.views.append(Views.Artists(self.toolbar, self.selection_toolbar, self.player))
self.views.append(Views.Songs(self.toolbar, self.selection_toolbar, self.player))
self.views.append(Views.Playlist(self.toolbar, self.selection_toolbar, self.player))
for i in self.views:
self._stack.add_titled(i, i.title, i.title)
self.toolbar.set_stack(self._stack)
self.toolbar.searchbar.show()
self._on_notify_model_id = self._stack.connect('notify::visible-child', self._on_notify_mode)
self.connect('destroy', self._notify_mode_disconnect)
self._key_press_event_id = self.connect('key_press_event', self._on_key_press)
self.views[0].populate()
def _on_select_all(self, action, param):
if self.toolbar._state != ToolbarState.SINGLE:
model = self._stack.get_visible_child()._model
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment