- so it can be shared in the gui and applet
---
 po/POTFILES.in        |    1 +
 src/gui-gtk/main.c    |  152 +-----------------------------------------
 src/include/libabrt.h |    9 +++
 src/lib/Makefile.am   |    5 +-
 src/lib/problem_api.c |  175 +++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 190 insertions(+), 152 deletions(-)
 create mode 100644 src/lib/problem_api.c

diff --git a/po/POTFILES.in b/po/POTFILES.in
index 9ae9f23..0b8176c 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -12,6 +12,7 @@ src/gui-gtk/abrt.desktop.in
 src/gui-gtk/main.c
 src/lib/abrt_conf.c
 src/lib/hooklib.c
+src/lib/problem_api.c
 src/plugins/abrt-action-analyze-backtrace.c
 src/plugins/abrt-action-analyze-c.c
 src/plugins/abrt-action-analyze-oops.c
diff --git a/src/gui-gtk/main.c b/src/gui-gtk/main.c
index 41aef3c..981b7b4 100644
--- a/src/gui-gtk/main.c
+++ b/src/gui-gtk/main.c
@@ -19,7 +19,6 @@
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
 #include <sys/inotify.h>
-#include <gio/gio.h> /* dbus */
 #if HAVE_LOCALE_H
 # include <locale.h>
 #endif
@@ -154,121 +153,6 @@ static void watch_this_dir(const char *dir_name)
     }
 }
 
-static GDBusProxy *get_dbus_proxy(void)
-{
-    GError *error = NULL;
-    GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
-                                         G_DBUS_PROXY_FLAGS_NONE,
-                                         NULL,
-                                         ABRT_DBUS_NAME,
-                                         ABRT_DBUS_OBJECT,
-                                         ABRT_DBUS_IFACE,
-                                         NULL,
-                                         &error);
-    if (error)
-    {
-        error_msg(_("Can't connect to system DBus: %s"), error->message);
-        g_error_free(error);
-        /* proxy is NULL in this case */
-    }
-    return proxy;
-}
-
-static int chown_dir_over_dbus(const char *problem_dir_path)
-{
-    GDBusProxy *proxy = get_dbus_proxy();
-    if (!proxy)
-        return 1;
-
-    GError *error = NULL;
-    g_dbus_proxy_call_sync(proxy,
-                        "ChownProblemDir",
-                        g_variant_new("(s)", problem_dir_path),
-                        G_DBUS_CALL_FLAGS_NONE,
-                        -1,
-                        NULL,
-                        &error);
-    g_object_unref(proxy);
-    if (error)
-    {
-        error_msg(_("Can't chown '%s': %s"), problem_dir_path, error->message);
-        g_error_free(error);
-        return 1;
-    }
-    return 0;
-}
-
-static int delete_problem_dirs_over_dbus(const GList *problem_dir_paths)
-{
-    GDBusProxy *proxy = get_dbus_proxy();
-    if (!proxy)
-        return 1;
-
-    GVariant *parameters = variant_from_string_list(problem_dir_paths);
-
-    GError *error = NULL;
-    g_dbus_proxy_call_sync(proxy,
-                    "DeleteProblem",
-                    parameters,
-                    G_DBUS_CALL_FLAGS_NONE,
-                    -1,
-                    NULL,
-                    &error);
-//g_variant_unref(parameters); -- need this??? no?? why?
-    g_object_unref(proxy);
-    if (error)
-    {
-        error_msg(_("Deleting problem directory failed: %s"), error->message);
-        g_error_free(error);
-        return 1;
-    }
-    return 0;
-}
-
-static problem_data_t *get_problem_data_dbus(const char *problem_dir_path)
-{
-    GDBusProxy *proxy = get_dbus_proxy();
-    if (!proxy)
-        return NULL;
-
-    GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
-    g_variant_builder_add(builder, "s", FILENAME_TIME          );
-    g_variant_builder_add(builder, "s", FILENAME_REASON        );
-    g_variant_builder_add(builder, "s", FILENAME_NOT_REPORTABLE);
-    g_variant_builder_add(builder, "s", FILENAME_COMPONENT     );
-    g_variant_builder_add(builder, "s", FILENAME_EXECUTABLE    );
-    g_variant_builder_add(builder, "s", FILENAME_REPORTED_TO   );
-    GVariant *params = g_variant_new("(sas)", problem_dir_path, builder);
-    g_variant_builder_unref(builder);
-
-    GError *error = NULL;
-    GVariant *result = g_dbus_proxy_call_sync(proxy,
-                                            "GetInfo",
-                                            params,
-                                            G_DBUS_CALL_FLAGS_NONE,
-                                            -1,
-                                            NULL,
-                                            &error);
-    g_object_unref(proxy);
-    if (error)
-    {
-        error_msg(_("Can't get problem data from abrt-dbus: %s"), 
error->message);
-        g_error_free(error);
-        return NULL;
-    }
-
-    problem_data_t *pd = new_problem_data();
-    char *key, *val;
-    GVariantIter *iter;
-    g_variant_get(result, "(a{ss})", &iter);
-    while (g_variant_iter_loop(iter, "{ss}", &key, &val))
-    {
-        add_to_problem_data(pd, key, val);
-    }
-    g_variant_unref(result);
-    return pd;
-}
-
 static void add_directory_to_dirlist(const char *problem_dir_path, gpointer 
data)
 {
     bool use_dbus = (bool)data;
@@ -346,43 +230,9 @@ static void add_directory_to_dirlist(const char 
*problem_dir_path, gpointer data
     VERB1 log("added: %s", problem_dir_path);
 }
 
-static GList *get_problems_over_dbus(void)
-{
-    GDBusProxy *proxy = get_dbus_proxy();
-    if (!proxy)
-        return NULL;
-
-    GError *error = NULL;
-    GVariant *result = g_dbus_proxy_call_sync(proxy,
-                                    g_authorize ? "GetAllProblems" : 
"GetProblems",
-                                    g_variant_new("()"),
-                                    G_DBUS_CALL_FLAGS_NONE,
-                                    -1,
-                                    NULL,
-                                    &error);
-    g_object_unref(proxy);
-    if (error)
-    {
-        error_msg(_("Can't get problem list from abrt-dbus: %s"), 
error->message);
-        g_error_free(error);
-    }
-
-    GList *list = NULL;
-    if (result)
-    {
-        /* Fetch "as" from "(as)" */
-        GVariant *array = g_variant_get_child_value(result, 0);
-        list = string_list_from_variant(array);
-        g_variant_unref(array);
-        g_variant_unref(result);
-    }
-
-    return list;
-}
-
 static void query_dbus_and_add_to_dirlist(void)
 {
-    GList *problem_dirs = get_problems_over_dbus();
+    GList *problem_dirs = get_problems_over_dbus(g_authorize);
 
     if (problem_dirs)
     {
diff --git a/src/include/libabrt.h b/src/include/libabrt.h
index 0af2494..e9594b5 100644
--- a/src/include/libabrt.h
+++ b/src/include/libabrt.h
@@ -6,6 +6,8 @@
 #ifndef LIBABRT_H_
 #define LIBABRT_H_
 
+#include <gio/gio.h> /* dbus */
+#include "abrt-dbus.h"
 /* libreport's internal functions we use: */
 #include <libreport/internal_libreport.h>
 
@@ -82,6 +84,13 @@ void koops_extract_oopses(GList **oops_list, char *buffer, 
size_t buflen);
 #define koops_print_suspicious_strings abrt_koops_print_suspicious_strings
 void koops_print_suspicious_strings(void);
 
+/* dbus client api */
+int chown_dir_over_dbus(const char *problem_dir_path);
+int delete_problem_dirs_over_dbus(const GList *problem_dir_paths);
+problem_data_t *get_problem_data_dbus(const char *problem_dir_path);
+GList *get_problems_over_dbus(bool authorize);
+
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index 0e11a29..f7019ff 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -11,7 +11,8 @@ libabrt_la_SOURCES = \
     kernel.c \
     abrt_glib.c \
     migrate_dirs.c \
-    check_recent_crash_file.c
+    check_recent_crash_file.c \
+    problem_api.c
 
 libabrt_la_CPPFLAGS = \
     -Wall -Wwrite-strings -Werror \
@@ -22,9 +23,11 @@ libabrt_la_CPPFLAGS = \
     -DEVENTS_DIR=\"$(EVENTS_DIR)\" \
     $(GLIB_CFLAGS) \
     $(LIBREPORT_CFLAGS) \
+    $(GIO_CFLAGS) \
     -D_GNU_SOURCE
 libabrt_la_LDFLAGS = \
     -version-info 0:1:0
 libabrt_la_LIBADD = \
     $(GLIB_LIBS) \
+    $(GIO_LIBS) \
     $(LIBREPORT_LIBS)
diff --git a/src/lib/problem_api.c b/src/lib/problem_api.c
new file mode 100644
index 0000000..704a0f9
--- /dev/null
+++ b/src/lib/problem_api.c
@@ -0,0 +1,175 @@
+/*
+    Copyright (C) 2011  ABRT Team
+    Copyright (C) 2011  RedHat inc.
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "libabrt.h"
+
+static GDBusProxy *get_dbus_proxy(void)
+{
+    static GDBusProxy *proxy;
+
+    /* we cache it, so we can't free it! */
+    if (proxy != NULL)
+        return proxy;
+
+    GError *error = NULL;
+    proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
+                                         G_DBUS_PROXY_FLAGS_NONE,
+                                         NULL,
+                                         ABRT_DBUS_NAME,
+                                         ABRT_DBUS_OBJECT,
+                                         ABRT_DBUS_IFACE,
+                                         NULL,
+                                         &error);
+    if (error)
+    {
+        error_msg(_("Can't connect to system DBus: %s"), error->message);
+        g_error_free(error);
+        /* proxy is NULL in this case */
+    }
+    return proxy;
+}
+
+int chown_dir_over_dbus(const char *problem_dir_path)
+{
+    GDBusProxy *proxy = get_dbus_proxy();
+    if (!proxy)
+        return 1;
+
+    GError *error = NULL;
+    g_dbus_proxy_call_sync(proxy,
+                        "ChownProblemDir",
+                        g_variant_new("(s)", problem_dir_path),
+                        G_DBUS_CALL_FLAGS_NONE,
+                        -1,
+                        NULL,
+                        &error);
+
+    if (error)
+    {
+        error_msg(_("Can't chown '%s': %s"), problem_dir_path, error->message);
+        g_error_free(error);
+        return 1;
+    }
+    return 0;
+}
+
+int delete_problem_dirs_over_dbus(const GList *problem_dir_paths)
+{
+    GDBusProxy *proxy = get_dbus_proxy();
+    if (!proxy)
+        return 1;
+
+    GVariant *parameters = variant_from_string_list(problem_dir_paths);
+
+    GError *error = NULL;
+    g_dbus_proxy_call_sync(proxy,
+                    "DeleteProblem",
+                    parameters,
+                    G_DBUS_CALL_FLAGS_NONE,
+                    -1,
+                    NULL,
+                    &error);
+//g_variant_unref(parameters); -- need this??? no?? why?
+
+    if (error)
+    {
+        error_msg(_("Deleting problem directory failed: %s"), error->message);
+        g_error_free(error);
+        return 1;
+    }
+    return 0;
+}
+
+problem_data_t *get_problem_data_dbus(const char *problem_dir_path)
+{
+    GDBusProxy *proxy = get_dbus_proxy();
+    if (!proxy)
+        return NULL;
+
+    GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
+    g_variant_builder_add(builder, "s", FILENAME_TIME          );
+    g_variant_builder_add(builder, "s", FILENAME_REASON        );
+    g_variant_builder_add(builder, "s", FILENAME_NOT_REPORTABLE);
+    g_variant_builder_add(builder, "s", FILENAME_COMPONENT     );
+    g_variant_builder_add(builder, "s", FILENAME_EXECUTABLE    );
+    g_variant_builder_add(builder, "s", FILENAME_REPORTED_TO   );
+    GVariant *params = g_variant_new("(sas)", problem_dir_path, builder);
+    g_variant_builder_unref(builder);
+
+    GError *error = NULL;
+    GVariant *result = g_dbus_proxy_call_sync(proxy,
+                                            "GetInfo",
+                                            params,
+                                            G_DBUS_CALL_FLAGS_NONE,
+                                            -1,
+                                            NULL,
+                                            &error);
+
+    if (error)
+    {
+        error_msg(_("Can't get problem data from abrt-dbus: %s"), 
error->message);
+        g_error_free(error);
+        return NULL;
+    }
+
+    problem_data_t *pd = new_problem_data();
+    char *key, *val;
+    GVariantIter *iter;
+    g_variant_get(result, "(a{ss})", &iter);
+    while (g_variant_iter_loop(iter, "{ss}", &key, &val))
+    {
+        add_to_problem_data(pd, key, val);
+    }
+    g_variant_unref(result);
+    return pd;
+}
+
+GList *get_problems_over_dbus(bool authorize)
+{
+    GDBusProxy *proxy = get_dbus_proxy();
+    if (!proxy)
+        return NULL;
+
+    GError *error = NULL;
+    GVariant *result = g_dbus_proxy_call_sync(proxy,
+                                    authorize ? "GetAllProblems" : 
"GetProblems",
+                                    g_variant_new("()"),
+                                    G_DBUS_CALL_FLAGS_NONE,
+                                    -1,
+                                    NULL,
+                                    &error);
+
+    if (error)
+    {
+        error_msg(_("Can't get problem list from abrt-dbus: %s"), 
error->message);
+        g_error_free(error);
+    }
+
+    GList *list = NULL;
+    if (result)
+    {
+        /* Fetch "as" from "(as)" */
+        GVariant *array = g_variant_get_child_value(result, 0);
+        list = string_list_from_variant(array);
+        g_variant_unref(array);
+        g_variant_unref(result);
+    }
+
+    return list;
+}
\ No newline at end of file
-- 
1.7.10.2

Reply via email to