chiark / gitweb /
dbus: add generic DEFINE_BUS_PROPERTY_APPEND_ENUM() macro for enum properties
authorLennart Poettering <lennart@poettering.net>
Sat, 10 Apr 2010 15:38:58 +0000 (17:38 +0200)
committerLennart Poettering <lennart@poettering.net>
Sat, 10 Apr 2010 16:00:27 +0000 (18:00 +0200)
dbus-job.c
dbus-unit.c
dbus.h

index d73b125..9c6a798 100644 (file)
@@ -39,39 +39,8 @@ static const char introspection[] =
         BUS_INTROSPECTABLE_INTERFACE
         "</node>";
 
-static int bus_job_append_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
-        Job *j = data;
-        const char *state;
-
-        assert(m);
-        assert(i);
-        assert(property);
-        assert(j);
-
-        state = job_state_to_string(j->state);
-
-        if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
-                return -ENOMEM;
-
-        return 0;
-}
-
-static int bus_job_append_type(Manager *m, DBusMessageIter *i, const char *property, void *data) {
-        Job *j = data;
-        const char *type;
-
-        assert(m);
-        assert(i);
-        assert(property);
-        assert(j);
-
-        type = job_type_to_string(j->type);
-
-        if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &type))
-                return -ENOMEM;
-
-        return 0;
-}
+DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_state, job_state, JobState);
+DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_type, job_type, JobType);
 
 static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) {
         Job *j = data;
@@ -108,10 +77,10 @@ static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *prope
 
 static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusMessage *message) {
         const BusProperty properties[] = {
-                { "org.freedesktop.systemd1.Job", "Id",      bus_property_append_uint32, "u",    &j->id },
-                { "org.freedesktop.systemd1.Job", "State",   bus_job_append_state,       "s",    j      },
-                { "org.freedesktop.systemd1.Job", "JobType", bus_job_append_type,        "s",    j      },
-                { "org.freedesktop.systemd1.Job", "Unit",    bus_job_append_unit,        "(so)", j      },
+                { "org.freedesktop.systemd1.Job", "Id",      bus_property_append_uint32, "u",    &j->id    },
+                { "org.freedesktop.systemd1.Job", "State",   bus_job_append_state,       "s",    &j->state },
+                { "org.freedesktop.systemd1.Job", "JobType", bus_job_append_type,        "s",    &j->type  },
+                { "org.freedesktop.systemd1.Job", "Unit",    bus_job_append_unit,        "(so)", j         },
                 { NULL, NULL, NULL, NULL, NULL }
         };
 
index 4ce94e2..de5236a 100644 (file)
@@ -94,22 +94,7 @@ static int bus_unit_append_description(Manager *m, DBusMessageIter *i, const cha
         return 0;
 }
 
-static int bus_unit_append_load_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
-        Unit *u = data;
-        const char *state;
-
-        assert(m);
-        assert(i);
-        assert(property);
-        assert(u);
-
-        state = unit_load_state_to_string(u->meta.load_state);
-
-        if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
-                return -ENOMEM;
-
-        return 0;
-}
+DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
 
 static int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
         Unit *u = data;
@@ -215,7 +200,7 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message
         const BusProperty properties[] = {
                 { "org.freedesktop.systemd1.Unit", "Id",                   bus_unit_append_id,           "s",    u                               },
                 { "org.freedesktop.systemd1.Unit", "Description",          bus_unit_append_description,  "s",    u                               },
-                { "org.freedesktop.systemd1.Unit", "LoadState",            bus_unit_append_load_state,   "s",    u                               },
+                { "org.freedesktop.systemd1.Unit", "LoadState",            bus_unit_append_load_state,   "s",    &u->meta.load_state             },
                 { "org.freedesktop.systemd1.Unit", "ActiveState",          bus_unit_append_active_state, "s",    u                               },
                 { "org.freedesktop.systemd1.Unit", "FragmentPath",         bus_property_append_string,   "s",    u->meta.fragment_path           },
                 { "org.freedesktop.systemd1.Unit", "ActiveEnterTimestamp", bus_property_append_uint64,   "t",    &u->meta.active_enter_timestamp },
diff --git a/dbus.h b/dbus.h
index 1e71971..06b7e20 100644 (file)
--- a/dbus.h
+++ b/dbus.h
@@ -86,4 +86,21 @@ void bus_unit_send_removed_signal(Unit *u);
 void bus_job_send_change_signal(Job *j);
 void bus_job_send_removed_signal(Job *j);
 
+#define DEFINE_BUS_PROPERTY_APPEND_ENUM(function,name,type)            \
+        static int function(Manager *m, DBusMessageIter *i, const char *property, void *data) { \
+                const char *value;                                      \
+                type *field = data;                                     \
+                                                                        \
+                assert(m);                                              \
+                assert(i);                                              \
+                assert(property);                                       \
+                                                                        \
+                value = name##_to_string(*field);                       \
+                                                                        \
+                if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &value)) \
+                        return -ENOMEM;                                 \
+                                                                        \
+                return 0;                                               \
+        }
+
 #endif