g_variant_builder_new使用
static void ListExtraVideoDirectories(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation) { int replyCode = 500; cString replyMessage = "Missing extra-video-directories patch"; cStringList dirs; #ifdef EXTRA_VIDEO_DIRECTORIES_PATCH if (!LockExtraVideoDirectories(false)) { replyCode = 550; replyMessage = "Unable to lock extra video directory list"; } else { if (ExtraVideoDirectories.Size() == 0) { replyCode = 550; replyMessage = "no extra video directories in list"; } else { replyCode = 250; replyMessage = ""; for (int i = 0; i < ExtraVideoDirectories.Size(); i++) dirs.Append(strdup(ExtraVideoDirectories.At(i))); } UnlockExtraVideoDirectories(); } #endif GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(isas)")); g_variant_builder_add(builder, "i", replyCode); g_variant_builder_add(builder, "s", *replyMessage); GVariantBuilder *array = g_variant_builder_new(G_VARIANT_TYPE("as")); for (int i = 0; i < dirs.Size(); i++) g_variant_builder_add(array, "s", dirs.At(i)); g_variant_builder_add_value(builder, g_variant_builder_end(array)); g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder)); g_variant_builder_unref(array); g_variant_builder_unref(builder); };
static void List(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation) { if (g_strcmp0(g_dbus_method_invocation_get_interface_name(Invocation), DBUS_VDR_PLUGIN_INTERFACE) == 0) esyslog("dbus2vdr: use of deprecated interface: 'List' should be called with the interface '%s'!", DBUS_VDR_PLUGINMANAGER_INTERFACE); #define EMPTY(s) (s == NULL ? "" : s) GVariantBuilder *array = g_variant_builder_new(G_VARIANT_TYPE("a(ss)")); int index = 0; do { cPlugin *plugin = cPluginManager::GetPlugin(index); if (plugin == NULL) break; const char *name = plugin->Name(); const char *version = plugin->Version(); g_variant_builder_add(array, "(ss)", EMPTY(name), EMPTY(version)); index++; } while (true); #undef EMPTY GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(a(ss))")); g_variant_builder_add_value(builder, g_variant_builder_end(array)); g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder)); g_variant_builder_unref(array); g_variant_builder_unref(builder); }