[gnome-music] Update songs when they are being added or deleted.
- From: Vadim Rutkovsky <vrutkovsky src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-music] Update songs when they are being added or deleted.
- Date: Fri, 7 Mar 2014 10:04:03 +0000 (UTC)
commit 5d16c7b03b3e09a83161fede26b1d6d010025e85
Author: Sai Suman Prayaga <suman sai14 gmail com>
Date: Fri Mar 7 03:38:46 2014 +0530
Update songs when they are being added or deleted.
https://bugzilla.gnome.org/show_bug.cgi?id=700726
gnomemusic/grilo.py | 29 +++++++++++++++++++-
gnomemusic/toolbar.py | 8 +++++-
gnomemusic/view.py | 26 +++++++++++++++++
gnomemusic/window.py | 72 +++++++++++++++++++++++++++++++++++-------------
4 files changed, 113 insertions(+), 22 deletions(-)
---
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index cf852ac..b93b00f 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -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')
diff --git a/gnomemusic/toolbar.py b/gnomemusic/toolbar.py
index 2890ae5..09d9541 100644
--- a/gnomemusic/toolbar.py
+++ b/gnomemusic/toolbar.py
@@ -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:
diff --git a/gnomemusic/view.py b/gnomemusic/view.py
index b52a403..b969b36 100644
--- a/gnomemusic/view.py
+++ b/gnomemusic/view.py
@@ -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]:
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index 194d64d..d433332 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -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
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]