[gnome-user-share] obexpush: use GApplication for unique instance



commit cc4d8fdee2d9ba85a548fd36e4226fed9dcab88b
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Nov 10 04:01:14 2014 -0800

    obexpush: use GApplication for unique instance
    
    Similar to what we just did for webdav, stop using an X selection to
    ensure an unique instance of this.
    Also, cleanup the code a little bit in the process.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=739872

 src/Makefile.am           |    5 +-
 src/user_share-obexpush.c |  154 ++++++++++++++++++---------------------------
 2 files changed, 63 insertions(+), 96 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 963f32c..f39a161 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -23,7 +23,6 @@ INCLUDES=                                             \
        -I$(top_builddir)                               \
        $(EXTENSION_CFLAGS)                             \
        $(USER_SHARE_CFLAGS)                            \
-       $(X_CFLAGS)                                     \
        $(WARN_CFLAGS)
 
 gnome_user_share_webdav_SOURCES =      \
@@ -52,9 +51,7 @@ endif
 
 gnome_user_share_obexpush_LDADD =      \
        libuser-share-common.la         \
-       $(USER_SHARE_LIBS)              \
-       $(SELINUX_LIBS)                 \
-       $(X_LIBS) $(X_PRE_LIBS) -lX11 $(X_EXTRA_LIBS)
+       $(USER_SHARE_LIBS)
 
 nautilus_extensiondir = $(NAUTILUSDIR)
 nautilus_extension_LTLIBRARIES = libnautilus-share-extension.la
diff --git a/src/user_share-obexpush.c b/src/user_share-obexpush.c
index 9b4d696..e366d0e 100644
--- a/src/user_share-obexpush.c
+++ b/src/user_share-obexpush.c
@@ -23,25 +23,17 @@
 
 #include "config.h"
 
-#include <gdk/gdkx.h>
-#include <gtk/gtk.h>
 #include <glib/gi18n.h>
 #include <glib/gstdio.h>
 #include <gio/gio.h>
 #include <glib-unix.h>
-#include <X11/Xlib.h>
 
 #include "user_share-common.h"
 #include "user_share-private.h"
-#include "user_share-common.h"
-#include "http.h"
 #include "obexpush.h"
 
-#include <stdarg.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
 #include <signal.h>
+#include <stdlib.h>
 #include <unistd.h>
 
 #include <bluetooth-client.h>
@@ -57,22 +49,10 @@ static GDBusProxy *session_proxy = NULL;
 static gboolean has_console = TRUE;
 
 #define OBEX_ENABLED (has_console)
-
-static void
-obex_services_start (void)
-{
-       if (has_console == FALSE)
-               return;
-
-       if (g_settings_get_boolean (settings, FILE_SHARING_BLUETOOTH_OBEXPUSH_ENABLED) == TRUE) {
-               obex_agent_up ();
-       }
-}
-
-static void
-obex_services_shutdown (void)
+static gboolean
+obex_service_should_run (void)
 {
-       obex_agent_down ();
+       return OBEX_ENABLED && g_settings_get_boolean (settings, FILE_SHARING_BLUETOOTH_OBEXPUSH_ENABLED);
 }
 
 static void
@@ -88,10 +68,10 @@ session_properties_changed_cb (GDBusProxy      *session,
                has_console = g_variant_get_boolean (v);
                g_debug ("Received session is active change: now %s", has_console ? "active" : "inactive");
 
-               if (has_console)
-                       obex_services_start ();
+               if (obex_service_should_run ())
+                       obex_agent_up ();
                else
-                       obex_services_shutdown ();
+                       obex_agent_down ();
 
                g_variant_unref (v);
        }
@@ -140,12 +120,11 @@ session_init (void)
 static void
 file_sharing_bluetooth_obexpush_enabled_changed (void)
 {
-       if (g_settings_get_boolean (settings,
-                                  FILE_SHARING_BLUETOOTH_OBEXPUSH_ENABLED) == FALSE) {
+       if (obex_service_should_run ()) {
+               obex_agent_up ();
+       } else {
                obex_agent_down ();
                _exit (0);
-       } else if (OBEX_ENABLED) {
-               obex_agent_up ();
        }
 }
 
@@ -169,7 +148,7 @@ file_sharing_bluetooth_obexpush_notify_changed (void)
 }
 
 static void
-setttings_changed (GSettings *settings,
+settings_changed (GSettings *settings,
                   gchar *path,
                   gpointer data)
 {
@@ -186,90 +165,81 @@ setttings_changed (GSettings *settings,
 static gboolean
 signal_handler (gpointer user_data)
 {
-       gtk_main_quit ();
+       GApplication *app = user_data;
+       g_application_quit (app);
        return FALSE;
 }
 
-static int
-x_io_error_handler (Display *xdisplay)
+static void
+user_share_obexpush_bus_closed (GDBusConnection *connection,
+                               gboolean remote_peer_vanished,
+                               GError *error,
+                               GApplication *app)
 {
        obex_agent_down ();
        _exit (2);
 }
 
-int
-main (int argc, char **argv)
+static void
+user_share_obexpush_activate (GApplication *app)
 {
-       Display *xdisplay;
-       G_GNUC_UNUSED int x_fd;
-       Window selection_owner;
-       Atom xatom;
-
-       bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
-       bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
-       textdomain (GETTEXT_PACKAGE);
+       if (!obex_service_should_run ())
+               return;
 
-       gtk_init (&argc, &argv);
+       /* Initial setting */
+       file_sharing_bluetooth_obexpush_accept_files_changed ();
+       file_sharing_bluetooth_obexpush_notify_changed ();
+       file_sharing_bluetooth_obexpush_enabled_changed ();
+       g_application_hold (app);
+}
 
-       if (g_strcmp0 (g_get_real_name (), "root") == 0) {
-               g_warning ("gnome-user-share cannot be started as root for security reasons.");
-               return 1;
-       }
+static void
+user_share_obexpush_startup (GApplication *app)
+{
+       GDBusConnection *connection;
 
        signal (SIGPIPE, SIG_IGN);
-       g_unix_signal_add (SIGINT, signal_handler, NULL);
-       g_unix_signal_add (SIGHUP, signal_handler, NULL);
-       g_unix_signal_add (SIGTERM, signal_handler, NULL);
+       g_unix_signal_add (SIGINT, signal_handler, app);
+       g_unix_signal_add (SIGHUP, signal_handler, app);
+       g_unix_signal_add (SIGTERM, signal_handler, app);
 
-       xdisplay = GDK_DISPLAY_XDISPLAY (gdk_display_get_default ());
-       if (xdisplay == NULL) {
-               g_warning ("Can't open display");
-               return 1;
-       }
-
-       xatom = XInternAtom (xdisplay, "_GNOME_USER_SHARE", FALSE);
-       selection_owner = XGetSelectionOwner (xdisplay, xatom);
+       connection = g_application_get_dbus_connection (app);
+       g_signal_connect (connection, "closed",
+                         G_CALLBACK (user_share_obexpush_bus_closed), app);
 
-       if (selection_owner != None) {
-               /* There is an owner already, quit */
-               return 1;
-       }
-
-       selection_owner = XCreateSimpleWindow (xdisplay,
-                                              RootWindow (xdisplay, 0),
-                                              0, 0, 1, 1,
-                                              0, 0, 0);
-       XSetSelectionOwner (xdisplay, xatom, selection_owner, CurrentTime);
-
-       if (XGetSelectionOwner (xdisplay, xatom) != selection_owner) {
-               /* Didn't get the selection */
-               return 1;
-       }
+       session_init ();
 
        settings = g_settings_new (GNOME_USER_SHARE_SCHEMAS);
-       if (g_settings_get_boolean (settings, FILE_SHARING_BLUETOOTH_OBEXPUSH_ENABLED) == FALSE)
-               return 1;
-
-       x_fd = ConnectionNumber (xdisplay);
-       XSetIOErrorHandler (x_io_error_handler);
+       g_signal_connect (settings, "changed", G_CALLBACK (settings_changed), NULL);
+}
 
-       if (obex_agent_up () == FALSE)
-               return 1;
+int
+main (int argc, char **argv)
+{
+       GApplication *application;
+       gint res;
 
-       g_signal_connect (settings, "changed", G_CALLBACK(setttings_changed), NULL);
+       bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
+       bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+       textdomain (GETTEXT_PACKAGE);
 
-       session_init ();
+       if (g_strcmp0 (g_get_real_name (), "root") == 0) {
+               g_warning ("gnome-user-share cannot be started as root for security reasons.");
+               return 1;
+       }
 
-       /* Initial setting */
-       file_sharing_bluetooth_obexpush_accept_files_changed ();
-       file_sharing_bluetooth_obexpush_notify_changed ();
-       file_sharing_bluetooth_obexpush_enabled_changed ();
+       application = g_application_new ("org.gnome.user-share.obexpush",
+                                        G_APPLICATION_FLAGS_NONE);
+       g_signal_connect (application, "startup",
+                         G_CALLBACK (user_share_obexpush_startup), NULL);
+       g_signal_connect (application, "activate",
+                         G_CALLBACK (user_share_obexpush_activate), NULL);
 
-       gtk_main ();
+       res = g_application_run (application, argc, argv);
 
-       g_object_unref (settings);
        obex_agent_down ();
+       g_object_unref (application);
 
-       return 0;
+       return res;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]