From a12a1630736a578de5e0b34a13fb9d24307695fc Mon Sep 17 00:00:00 2001 From: Andre Miranda Date: Wed, 24 May 2017 14:43:35 -0300 Subject: [PATCH] Single instance feature --- lib/screenshooter-actions.c | 2 ++ lib/screenshooter-global.h | 1 + src/main.c | 26 +++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/screenshooter-actions.c b/lib/screenshooter-actions.c index 43ee4e2..a9fa048 100644 --- a/lib/screenshooter-actions.c +++ b/lib/screenshooter-actions.c @@ -63,6 +63,8 @@ gboolean screenshooter_action_idle (ScreenshotData *sd) G_CALLBACK (cb_help_response), NULL); g_signal_connect (dialog, "key-press-event", G_CALLBACK (screenshooter_f1_key), NULL); + g_signal_connect_swapped (sd->application, "activate", + G_CALLBACK (gtk_window_present), dialog); response = gtk_dialog_run (GTK_DIALOG (dialog)); diff --git a/lib/screenshooter-global.h b/lib/screenshooter-global.h index 173f9d4..9dc97a0 100644 --- a/lib/screenshooter-global.h +++ b/lib/screenshooter-global.h @@ -55,6 +55,7 @@ typedef struct gchar *app; gchar *last_user; GdkPixbuf *screenshot; + GtkApplication *application; } ScreenshotData; diff --git a/src/main.c b/src/main.c index c677165..ffea692 100644 --- a/src/main.c +++ b/src/main.c @@ -139,7 +139,7 @@ cb_dialog_response (GtkWidget *dialog, gint response, ScreenshotData *sd) int main (int argc, char **argv) { - GError *cli_error = NULL; + GError *cli_error = NULL, *error = NULL; GFile *default_save_dir; const gchar *rc_file; const gchar *conflict_error = @@ -279,6 +279,28 @@ int main (int argc, char **argv) return EXIT_SUCCESS; } + /* Initialize application to ensure single instance */ + sd->application = gtk_application_new ("org.xfce.screenshooter", 0); + + g_application_register (G_APPLICATION (sd->application), NULL, &error); + if (error != NULL) + { + g_warning ("Unable to register GApplication: %s", error->message); + g_error_free (error); + error = NULL; + } + + if (g_application_get_is_remote (G_APPLICATION (sd->application))) + { + g_warning ("An instance of screenshooter is already present"); + g_application_activate (G_APPLICATION (sd->application)); + g_object_unref (sd->application); + + g_free (sd); + + return EXIT_SUCCESS; + } + /* Read the preferences */ rc_file = xfce_resource_save_location (XFCE_RESOURCE_CONFIG, "xfce4/xfce4-screenshooter", TRUE); screenshooter_read_rc_file (rc_file, sd); @@ -380,6 +402,8 @@ int main (int argc, char **argv) G_CALLBACK (cb_dialog_response), sd); g_signal_connect (dialog, "key-press-event", G_CALLBACK (screenshooter_f1_key), NULL); + g_signal_connect_swapped (sd->application, "activate", + G_CALLBACK (gtk_window_present), dialog); gtk_widget_show (dialog); } -- 2.13.0