[PATCH 09/17] grilo-test-ui.py: Implemented search_cb



---
 tools/python/grilo-test-ui.py |  113 ++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 111 insertions(+), 2 deletions(-)

diff --git a/tools/python/grilo-test-ui.py b/tools/python/grilo-test-ui.py
index 09c6004..5cc5e7e 100644
--- a/tools/python/grilo-test-ui.py
+++ b/tools/python/grilo-test-ui.py
@@ -6,17 +6,35 @@ from gi.repository import GdkPixbuf
 
 class MainWindow(Gtk.Window):
 
+    BROWSE_FLAGS = (Grl.MetadataResolutionFlags) (Grl.MetadataResolutionFlags.FAST_ONLY |
+                                                  Grl.MetadataResolutionFlags.IDLE_RELAY)
+    BROWSE_CHUNK_SIZE = 100
+    BROWSE_MAX_COUNT = 2 * BROWSE_CHUNK_SIZE
+
+    BROWSE_KEYS = []
+
     def __init__(self):
         super(MainWindow, self).__init__(type=Gtk.WindowType.TOPLEVEL)
         self.connect('destroy', self._quit)
         self.set_title('Grilo Test UI using introspection')
 
         Grl.init([])
+        self.BROWSE_KEYS = self._lookup_keys()
+
         self._setup_ui()
         self._load_plugins()
 
+        self._ui_state = UIState()
+
         self.show_all()
 
+    def _lookup_keys(self):
+        registry = Grl.PluginRegistry.get_default()
+        key_id = registry.lookup_metadata_key('id')
+        key_title = registry.lookup_metadata_key('title')
+        key_childcount = registry.lookup_metadata_key('childcount')
+        return [key_id, key_title, key_childcount]
+
     def _load_plugins(self):
         registry = Grl.PluginRegistry.get_default()
         registry.connect('source-added',
@@ -165,7 +183,83 @@ class MainWindow(Gtk.Window):
             source = self._search_combo.get_model().get_value(iter,
                                                               ComboBoxStore.SOURCE_COLUMN)
             search_text = self._search_text.get_text()
-            print search_text, source
+            self.search(source, search_text)
+
+    def _search_cb(self, source, operation_id, media, remaining, data, error):
+        state = data
+        state.count += 1
+
+        if media:
+            icon = None #get_icon_for_media (media);
+            name = media.get_title()
+            if isinstance(media, Grl.MediaBox):
+                childcount = media.get_childcount()
+                type = BrowserListStore.OBJECT_TYPE_CONTAINER
+                if childcount == Grl.METADATA_KEY_CHILDCOUNT_UNKNOWN:
+                    childcount = '?'
+                name = ('%(name)s (%(count)s)' % {'name':name,
+                                                  'count':childcount})
+            else:
+                type = BrowserListStore.OBJECT_TYPE_MEDIA
+            model = self._browser_window.get_browser().get_model()
+            model.append((source, media, type, name, icon))
+
+        if remaining == 0:
+            state.offset += state.count
+            if not self._ui_state.multiple and \
+               state.count >= self.BROWSE_CHUNK_SIZE and \
+               state.offset < self.BROWSE_MAX_COUNT:
+                state.count = 0
+                next_search_id = source.search(state.text,
+                                               self.BROWSE_KEYS,
+                                               state.offset,
+                                               self.BROWSE_CHUNK_SIZE,
+                                               self.BROWSE_FLAGS,
+                                               self._search_cb,
+                                               state)
+                self._operation_started(source, next_search_id, False)
+            else:
+                self._operation_finished()
+                print ('**** search finished %(id)s ****' % {'id':operation_id})
+
+    def _cancel_current_operation(self):
+        if self._ui_state.op_ongoing:
+            if not self._ui_state.multiple:
+                self._ui_state.op_source.cancel(self._ui_state.cur_op_id)
+            else:
+                Grl.multiple_cancel(self._ui_state.cur_op_id)
+            self._ui_state.op_ongoing = False
+
+    def _operation_started(self, source, search_id, multiple):
+        self._ui_state = UIState(op_ongoing=True,
+                                 cur_op_source=source,
+                                 cur_op_id=search_id,
+                                 multiple=multiple)
+
+    def _operation_finished(self):
+        self._ui_state.op_ongoing = False
+
+    def search(self, source, text):
+        self._cancel_current_operation()
+        state = OperationState(text)
+        if source:
+            search_id = source.search(text,
+                                      self.BROWSE_KEYS,
+                                      0,
+                                      self.BROWSE_CHUNK_SIZE,
+                                      self.BROWSE_FLAGS,
+                                      self._search_cb,
+                                      state)
+        else:
+            search_id = Grl.multiple_search(None,
+                                            text,
+                                            self.BROWSE_KEYS,
+                                            self.BROWSE_MAX_COUNT,
+                                            self.BROWSE_FLAGS,
+                                            self._search_cb,
+                                            state)
+        self._clear_panes()
+        self._operation_started(source, search_id, source is None)
 
     def run(self):
         Gtk.main()
@@ -173,6 +267,21 @@ class MainWindow(Gtk.Window):
     def _quit(self, *args):
         Gtk.main_quit()
 
+class UIState(object):
+    def __init__(self, op_ongoing=False, cur_op_source=None,
+                 cur_op_id= -1, multiple=False):
+        self.op_ongoing = op_ongoing
+        self.cur_op_source = cur_op_source
+        self.cur_op_id = cur_op_id
+        self.multiple = multiple
+
+
+class OperationState(object):
+    def __init__(self, text, offset=0, count=0):
+        self.text = text
+        self.offset = offset
+        self.count = count
+
 class TextComboBox(Gtk.ComboBox):
     def __init__(self):
         super(TextComboBox, self).__init__()
@@ -257,7 +366,7 @@ class BrowserListStore(Gtk.ListStore):
 
     def __init__(self):
         super(BrowserListStore, self).__init__('GObject',
-                                               object,
+                                               'GObject',
                                                int,
                                                str,
                                                GdkPixbuf.Pixbuf)
-- 
1.7.0.4



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]