utils-jbus: adds closure
[src/app-framework-main.git] / src / afm-system-daemon.c
index f02ff3d..25d9456 100644 (file)
@@ -31,6 +31,7 @@
 #include "afm-db.h"
 #include "wgt-info.h"
 #include "wgtpkg-install.h"
+#include "wgtpkg-uninstall.h"
 
 static const char appname[] = "afm-system-daemon";
 static const char *rootdir = NULL;
@@ -65,7 +66,7 @@ const char error_bad_request[] = "\"bad request\"";
 const char error_not_found[] = "\"not found\"";
 const char error_cant_start[] = "\"can't start\"";
 
-static void on_install(struct jreq *jreq, struct json_object *req)
+static void on_install(struct jreq *jreq, struct json_object *req, void *unused)
 {
        const char *wgtfile;
        const char *root;
@@ -81,10 +82,10 @@ static void on_install(struct jreq *jreq, struct json_object *req)
                force = 0;
                break;
        case json_type_object:
-               wgtfile = j_get_string(req, "wgt", NULL);
+               wgtfile = j_string_at(req, "wgt", NULL);
                if (wgtfile != NULL) {
-                       root = j_get_string(req, "root", rootdir);
-                       force = j_get_boolean(req, "force", 0);
+                       root = j_string_at(req, "root", rootdir);
+                       force = j_boolean_at(req, "force", 0);
                        break;
                }
        default:
@@ -94,30 +95,55 @@ static void on_install(struct jreq *jreq, struct json_object *req)
 
        /* install the widget */
        ifo = install_widget(wgtfile, root, force);
-       if (ifo == NULL) {
+       if (ifo == NULL)
                jbus_reply_error_s(jreq, "\"installation failed\"");
-               return;
-       }
+       else {
+               /* build the response */
+               resp = json_object_new_object();
+               if(!resp || !j_add_string(resp, "added", wgt_info_desc(ifo)->idaver))
+                       jbus_reply_error_s(jreq, "\"out of memory but installed!\"");
+               else {
+                       jbus_send_signal_s(jbus, "changed", "true");
+                       jbus_reply_j(jreq, resp);
+               }
 
-       /* build the response */
-       resp = json_object_new_object();
-       if(!resp || !j_add_string(resp, "added", wgt_info_desc(ifo)->idaver)) {
-               json_object_put(resp);
+               /* clean-up */
                wgt_info_unref(ifo);
-               jbus_reply_error_s(jreq, "\"out of memory but installed!\"");
-               return;
+               json_object_put(resp);
        }
-       wgt_info_unref(ifo);
-
-       /* reply and propagate event */
-       jbus_reply_j(jreq, resp);
-       jbus_send_signal_j(jbus, "changed", resp);
-       json_object_put(resp);
 }
 
-static void on_uninstall(struct jreq *jreq, struct json_object *req)
+static void on_uninstall(struct jreq *jreq, struct json_object *req, void *unused)
 {
-       jbus_reply_error_s(jreq, "\"not yet implemented\"");
+       const char *idaver;
+       const char *root;
+       int rc;
+
+       /* scan the request */
+       switch (json_object_get_type(req)) {
+       case json_type_string:
+               idaver = json_object_get_string(req);
+               root = rootdir;
+               break;
+       case json_type_object:
+               idaver = j_string_at(req, "id", NULL);
+               if (idaver != NULL) {
+                       root = j_string_at(req, "root", rootdir);
+                       break;
+               }
+       default:
+               jbus_reply_error_s(jreq, error_bad_request);
+               return;
+       }
+
+       /* install the widget */
+       rc = uninstall_widget(idaver, root);
+       if (rc)
+               jbus_reply_error_s(jreq, "\"uninstallation had error\"");
+       else {
+               jbus_send_signal_s(jbus, "changed", "true");
+               jbus_reply_s(jreq, "true");
+       }
 }
 
 static int daemonize()
@@ -191,13 +217,13 @@ int main(int ac, char **av)
        }
 
        /* init service */
-       jbus = create_jbus(0, AFM_SYSTEM_DBUS_PATH);
+       jbus = create_jbus_system(AFM_SYSTEM_DBUS_PATH);
        if (!jbus) {
                ERROR("create_jbus failed");
                return 1;
        }
-       if(jbus_add_service_j(jbus, "install", on_install)
-       || jbus_add_service_j(jbus, "uninstall", on_uninstall)) {
+       if(jbus_add_service_j(jbus, "install", on_install, NULL)
+       || jbus_add_service_j(jbus, "uninstall", on_uninstall, NULL)) {
                ERROR("adding services failed");
                return 1;
        }