From d4e83ae45ac8eba973d78124a37e764e42619244 Mon Sep 17 00:00:00 2001
From: Jonathan Corwin <j@corwin.co.uk>
Date: Fri, 20 May 2011 00:09:42 +0100
Subject: [PATCH] Remote web add to service

---
 openlp/core/lib/mediamanageritem.py      | 20 ++++++++++----------
 openlp/plugins/remotes/html/index.html   |  2 +-
 openlp/plugins/remotes/html/openlp.js    | 22 +++++++++++++++-------
 openlp/plugins/remotes/lib/httpserver.py | 13 ++++++++++++-
 4 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/openlp/core/lib/mediamanageritem.py b/openlp/core/lib/mediamanageritem.py
index 2e514935f..a16754452 100644
--- a/openlp/core/lib/mediamanageritem.py
+++ b/openlp/core/lib/mediamanageritem.py
@@ -506,18 +506,18 @@ class MediaManagerItem(QtGui.QWidget):
             # multiple service items?
             if self.singleServiceItem or self.remoteTriggered:
                 log.debug(u'%s Add requested', self.plugin.name)
-                serviceItem = self.buildServiceItem(None, True)
-                if serviceItem:
-                    serviceItem.from_plugin = False
-                    self.parent.serviceManager.addServiceItem(serviceItem,
-                        replace=self.remoteTriggered)
+                self.addToService([None], self.remoteTriggered)
             else:
                 items = self.listView.selectedIndexes()
-                for item in items:
-                    serviceItem = self.buildServiceItem(item, True)
-                    if serviceItem:
-                        serviceItem.from_plugin = False
-                        self.parent.serviceManager.addServiceItem(serviceItem)
+                self.addToService(items)
+
+    def addToService(self, items, replace=None):
+        for item in items:
+            serviceItem = self.buildServiceItem(item, True)
+            if serviceItem:
+                serviceItem.from_plugin = False
+                self.parent.serviceManager.addServiceItem(serviceItem,
+                    replace=replace)
 
     def onAddEditClick(self):
         """
diff --git a/openlp/plugins/remotes/html/index.html b/openlp/plugins/remotes/html/index.html
index fd7fb3715..bfb0d5ea4 100644
--- a/openlp/plugins/remotes/html/index.html
+++ b/openlp/plugins/remotes/html/index.html
@@ -109,7 +109,7 @@
       <input type="search" name="search-text" id="search-text" value="" />
     </div>
     <a href="#" id="search-submit" data-role="button">Search</a>
-    <ul data-role="listview" data-inset="true">
+    <ul data-role="listview" data-inset="true" data-split-icon="plus">
   </div>
 </div>
 </body>
diff --git a/openlp/plugins/remotes/html/openlp.js b/openlp/plugins/remotes/html/openlp.js
index 4eee047a4..4688fb499 100644
--- a/openlp/plugins/remotes/html/openlp.js
+++ b/openlp/plugins/remotes/html/openlp.js
@@ -219,9 +219,13 @@ window.OpenLP = {
         } 
         else {
             $.each(data.results.items, function (idx, value) {
-              var li = $("<li data-icon=\"false\">").append(
-                $("<a href=\"#\">").attr("value", value[0]).text(value[1]));
-              li.children("a").click(OpenLP.goLive);
+              var li = $("<li>");
+              li.append($("<a href=\"#\">").text(value[1]).click(function () {
+                OpenLP.goLive(value[0]);
+              }));
+              li.append($("<a href=\"#\">").click(function () {
+                OpenLP.addToService(value[0]);
+              }));
               ul.append(li);
             });
         }
@@ -230,17 +234,21 @@ window.OpenLP = {
     );
     return false;
   },
-  goLive: function (event) {
-    var slide = OpenLP.getElement(event);
-    var id = slide.attr("value");
+  goLive: function (id) {
     var text = JSON.stringify({"request": {"id": id}});
     $.getJSON(
       "/api/" + $("#search-plugin").val() + "/live",
       {"data": text})
     $.mobile.changePage("slide-controller");
     return false;
+  },
+  addToService: function (id) {
+    var text = JSON.stringify({"request": {"id": id}});
+    $.getJSON(
+      "/api/" + $("#search-plugin").val() + "/add",
+      {"data": text})
+    return false;
   }
-
 }
 // Service Manager
 $("#service-manager").live("pagebeforeshow", OpenLP.loadService);
diff --git a/openlp/plugins/remotes/lib/httpserver.py b/openlp/plugins/remotes/lib/httpserver.py
index 351e7f905..d17f924c8 100644
--- a/openlp/plugins/remotes/lib/httpserver.py
+++ b/openlp/plugins/remotes/lib/httpserver.py
@@ -253,7 +253,8 @@ class HttpConnection(object):
             (r'^/api/alert$', self.alert),
             (r'^/api/plugin/(search)$', self.pluginInfo),
             (r'^/api/(.*)/search$', self.search),
-            (r'^/api/(.*)/live$', self.go_live)
+            (r'^/api/(.*)/live$', self.go_live),
+            (r'^/api/(.*)/add$', self.add_to_service)
         ]
         QtCore.QObject.connect(self.socket, QtCore.SIGNAL(u'readyRead()'),
             self.ready_read)
@@ -490,6 +491,16 @@ class HttpConnection(object):
         if plugin.status == PluginStatus.Active and plugin.mediaItem:
             plugin.mediaItem.goLive(id)
 
+    def add_to_service(self, type):
+        """
+        Add item of type ``type`` to the end of the service
+        """
+        id = json.loads(self.url_params[u'data'][0])[u'request'][u'id']
+        plugin = self.parent.parent.pluginManager.get_plugin_by_name(type)
+        if plugin.status == PluginStatus.Active and plugin.mediaItem:
+            item_id = plugin.mediaItem.createItemFromId(id)
+            plugin.mediaItem.addToService([item_id])
+
     def send_response(self, response):
         http = u'HTTP/1.1 %s\r\n' % response.code
         for header, value in response.headers.iteritems():