summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSøren Sandmann Pedersen <ssp@dhcp-100-2-40.bos.redhat.com>2009-04-12 16:35:23 -0400
committerSøren Sandmann Pedersen <ssp@dhcp-100-2-40.bos.redhat.com>2009-04-12 16:35:23 -0400
commitde30d95d3f9f601ea2804547726e6d7ac3907dc4 (patch)
treeda5997ffbc181726e52bd13f3fe1474062473351
parent72718c0c9863f03af05fd04b09a026afc4cd75de (diff)
Initial rough support for dbus uniqueness
-rw-r--r--siv.c170
1 files changed, 111 insertions, 59 deletions
diff --git a/siv.c b/siv.c
index e779fef..ea583b2 100644
--- a/siv.c
+++ b/siv.c
@@ -14,6 +14,8 @@ struct App
int n_windows;
GtkWidget * chooser;
+
+ GHashTable *windows_by_filename;
};
void
@@ -342,12 +344,16 @@ process_options (int argc, char **argv)
return NULL;
}
- if (g_path_is_absolute (option))
- name = g_strdup (option);
- else
- name = g_build_filename (g_get_current_dir(), option, NULL);
-
- g_ptr_array_add (result, name);
+ /* Don't open more than 32 windows */
+ if (i < 32)
+ {
+ if (g_path_is_absolute (option))
+ name = g_strdup (option);
+ else
+ name = g_build_filename (g_get_current_dir(), option, NULL);
+
+ g_ptr_array_add (result, name);
+ }
}
g_ptr_array_add (result, NULL);
@@ -355,7 +361,54 @@ process_options (int argc, char **argv)
return (char **)g_ptr_array_free (result, FALSE);
}
+static gboolean
+open_file (App *app, const char *filename, GPtrArray *err_files)
+{
+ SivWindow *window;
+ GError *err = NULL;
+
+ if ((window = g_hash_table_lookup (app->windows_by_filename, filename)))
+ {
+ /* FIXME: present the window here */
+ return TRUE;
+ }
+
+ window = window_new (app);
+
+ if (!window_load_file (window, filename, &err))
+ {
+ if (err_files)
+ g_ptr_array_add (err_files, g_strdup (filename));
+
+ g_error_free (err);
+
+ window_free (window);
+
+ return FALSE;
+ }
+ else
+ {
+ window_show (window, GDK_CURRENT_TIME);
+
+ return TRUE;
+ }
+}
+
static void
+on_open (App *app, const char *filename)
+{
+ if (!open_file (app, filename, NULL))
+ {
+ /* FIXME: show error
+ *
+ * However, it would be better if we could show
+ * all the errors at once; so we need to support
+ * asking to load many files at once
+ */
+ }
+}
+
+static App *
app_new (int argc, char **argv)
{
GPtrArray *err_files = g_ptr_array_new ();
@@ -366,6 +419,8 @@ app_new (int argc, char **argv)
filenames = process_options (argc, argv);
app = g_new0 (App, 1);
+
+ app->windows_by_filename = g_hash_table_new (g_str_hash, g_str_equal);
/* When a window is created, it increases the app->n_windows counter;
* when it is destroyed it decreases it. The problem is if the first
@@ -380,29 +435,7 @@ app_new (int argc, char **argv)
if (filenames[0])
{
for (i = 0; filenames[i] != NULL; ++i)
- {
- SivWindow *window;
- GError *err = NULL;
- char *filename = filenames[i];
-
- /* Don't open more than 32 windows */
- if (i > 32)
- break;
-
- window = window_new (app);
-
- if (!window_load_file (window, filename, &err))
- {
- g_ptr_array_add (err_files, g_strdup (filename));
- g_error_free (err);
-
- window_free (window);
- }
- else
- {
- window_show (window, GDK_CURRENT_TIME);
- }
- }
+ open_file (app, filenames[i], err_files);
}
else
{
@@ -420,28 +453,25 @@ app_new (int argc, char **argv)
exit (1);
else
--app->n_windows;
+
+ return app;
}
-static nul_dbus_service_t *
-make_service (gpointer data)
+static nul_dbus_object_t *
+make_object (gpointer data)
{
- return nul_dbus_session_service (
- "dk.au.daimi.sandmann",
+ return nul_dbus_object (
+ "/app",
+ data,
- nul_dbus_object (
- "/dk/au/daimi/sandmann/siv",
- NULL, /* object data */
+ nul_dbus_interface (
+ "dk.au.daimi.sandmann.siv",
- nul_dbus_interface (
- "dk.au.daimi.sandmann.siv",
-
- nul_dbus_method (
- "open",
- (nul_dbus_function_t)NULL, /* on_open,*/
- nul_dbus_parameter_in ("filename", nul_dbus_type_string()),
- nul_dbus_parameter_out ("result", nul_dbus_type_int32()),
- NULL),
-
+ nul_dbus_method (
+ "open",
+ (nul_dbus_function_t)on_open, /* on_open,*/
+ nul_dbus_parameter_in ("filename", nul_dbus_type_string()),
+ nul_dbus_parameter_out ("result", nul_dbus_type_int32()),
NULL),
NULL),
@@ -469,24 +499,46 @@ main (int argc,
if (!getenv ("G_SLICE"))
putenv ("G_SLICE=always_malloc");
- service = make_service (NULL);
-
- gtk_init (&argc, &argv);
-
- if (!g_file_test (GLADE_FILE, G_FILE_TEST_EXISTS))
+ service = nul_dbus_session_service ("dk.au.daimi.sandmann.siv", NULL);
+ if (nul_dbus_service_start (service) /* FIXME: or if it failed due to
+ lack of dbus daemon */)
{
- app_show_warning (
- NULL,
- "<i>Running \"make install\" may solve this problem.</i>",
- "<b>"APPLICATION_NAME
- " was not compiled or installed correctly.</b>");
+ gtk_init (&argc, &argv);
- return FALSE;
+ if (!g_file_test (GLADE_FILE, G_FILE_TEST_EXISTS))
+ {
+ app_show_warning (
+ NULL,
+ "<i>Running \"make install\" may solve this problem.</i>",
+ "<b>"APPLICATION_NAME
+ " was not compiled or installed correctly.</b>");
+
+ return FALSE;
+ }
+
+ nul_dbus_service_add_object (
+ service, make_object (app_new (argc, argv)));
+
+ gtk_main ();
}
+ else
+ {
+ char **files;
+ int i;
+
+ g_print ("existing process\n");
- app_new (argc, argv);
+ files = process_options (argc, argv);
+
+ nul_dbus_service_add_object (service, make_object (NULL));
- gtk_main ();
+ for (i = 0; files[i] != NULL; ++i)
+ {
+ nul_dbus_invoke (service, "/app/dk.au.daimi.sandmann.siv.open",
+ NULL/* callback */, NULL/* data */,
+ files[i]);
+ }
+ }
return 0;
}