Integrate removing files with the file manager
[anjuta-git-plugin.git] / src / main.c
blob3ad12dbff4e37f6a4d18f5cf9eb527f0bafcfadd
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 main.c
4 Copyright (C) 2000 Naba Kumar <naba@gnome.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #ifdef ENABLE_NLS
26 # include <locale.h>
27 #endif
29 #include <sys/stat.h>
31 #include <libgnome/gnome-program.h>
32 #include <gtk/gtkwindow.h>
33 #include <libgnomevfs/gnome-vfs.h>
34 #include <libanjuta/resources.h>
35 #include <libanjuta/anjuta-debug.h>
36 #include <libanjuta/interfaces/ianjuta-file-loader.h>
38 #include "anjuta.h"
39 #include "bacon-message-connection.h"
41 #ifdef ENABLE_NLS
42 #include <locale.h>
43 #endif
45 #define ANJUTA_PIXMAP_SPLASH_SCREEN "anjuta_splash.png"
47 /* App */
48 static AnjutaApp *app = NULL;
50 /* Bacon */
51 static guint32 startup_timestamp = 0;
52 static BaconMessageConnection *connection;
54 /* Command line options */
55 /* command line */
56 static gint line_position = 0;
57 static GList *file_list = NULL;
58 static gboolean no_splash = 0;
59 static gboolean no_client = 0;
60 static gboolean no_session = 0;
61 static gboolean no_files = 0;
62 static gboolean proper_shutdown = 0;
63 static gchar *anjuta_geometry = NULL;
64 static gchar **anjuta_filenames = NULL;
66 static const GOptionEntry anjuta_options[] = {
68 "geometry", 'g', 0, G_OPTION_ARG_STRING,
69 &anjuta_geometry,
70 N_("Specify the size and location of the main window"),
71 /* This is the format you can specify the size andposition
72 * of the window on command line */
73 N_("WIDTHxHEIGHT+XOFF+YOFF")
76 "no-splash", 's', 0, G_OPTION_ARG_NONE,
77 &no_splash,
78 N_("Do not show the splashscreen"),
79 NULL
82 "no-client", 'c', 0, G_OPTION_ARG_NONE,
83 &no_client,
84 N_("Start a new instance and do not open the file in a existing"),
85 NULL
88 "no-session", 'n', 0, G_OPTION_ARG_NONE,
89 &no_session,
90 N_("Do not open last session on startup"),
91 NULL
94 "no-files", 'f', 0, G_OPTION_ARG_NONE,
95 &no_files,
96 N_("Do not open last project and files on startup"),
97 NULL
100 "proper-shutdown", 'p', 0, G_OPTION_ARG_NONE,
101 &proper_shutdown,
102 N_("Shutdown anjuta properly releasing all resources (for debugging)"),
103 NULL
106 G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY,
107 &anjuta_filenames,
108 NULL,
109 NULL
111 {NULL}
114 static gchar*
115 get_real_path (const gchar *file_name)
117 if (file_name)
119 gchar path[PATH_MAX+1];
120 gchar *uri_scheme;
122 uri_scheme = gnome_vfs_get_uri_scheme (file_name);
123 if (!uri_scheme)
125 memset(path, '\0', PATH_MAX+1);
126 realpath(file_name, path);
127 return g_strdup (path);
129 g_free (uri_scheme);
130 return g_strdup (file_name);
132 else
133 return NULL;
136 static void
137 get_command_line_args ()
139 int i;
140 if (anjuta_filenames)
141 for (i = 0; anjuta_filenames[i]; i++)
142 file_list = g_list_append (file_list,
143 get_real_path (anjuta_filenames[i]));
146 static GdkDisplay *
147 display_open_if_needed (const gchar *name)
149 GSList *displays;
150 GSList *l;
151 GdkDisplay *display = NULL;
153 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
155 for (l = displays; l != NULL; l = l->next)
157 if (strcmp (gdk_display_get_name ((GdkDisplay *) l->data), name) == 0)
159 display = l->data;
160 break;
164 g_slist_free (displays);
166 return display != NULL ? display : gdk_display_open (name);
169 /* serverside */
170 static void
171 on_message_received (const char *message, gpointer data)
173 gchar **commands;
174 gchar **params;
175 gchar *display_name;
176 gint screen_number;
177 gint i;
178 GdkDisplay *display;
179 GdkScreen *screen;
180 GList* file;
182 g_return_if_fail (message != NULL);
184 commands = g_strsplit (message, "\v", -1);
186 /* header */
187 params = g_strsplit (commands[0], "\t", 4);
188 startup_timestamp = atoi (params[0]); /* CHECK if this is safe */
189 display_name = g_strdup (params[1]);
190 screen_number = atoi (params[2]);
192 display = display_open_if_needed (display_name);
193 screen = gdk_display_get_screen (display, screen_number);
194 g_free (display_name);
196 g_strfreev (params);
198 /* body */
199 gtk_window_present (GTK_WINDOW (app));
200 i = 1;
201 while (commands[i])
203 params = g_strsplit (commands[i], "\t", -1);
205 if (strcmp (params[0], "OPEN-URIS") == 0)
207 gint n_uris, i;
208 gchar **uris;
210 line_position = atoi (params[1]);
212 n_uris = atoi (params[2]);
213 uris = g_strsplit (params[3], " ", n_uris);
215 for (i = 0; i < n_uris; i++)
216 file_list = g_list_prepend (file_list, uris[i]);
217 file_list = g_list_reverse (file_list);
219 /* the list takes ownerhip of the strings,
220 * only free the array */
221 g_free (uris);
223 else
225 g_warning ("Unexpected bacon command");
228 g_strfreev (params);
229 ++i;
232 g_strfreev (commands);
234 /* execute the commands */
236 for (file = file_list; file != NULL; file = g_list_next (file))
238 IAnjutaFileLoader* loader =
239 anjuta_shell_get_interface(ANJUTA_SHELL(app), IAnjutaFileLoader, NULL);
240 GFile* gfile = g_file_new_for_commandline_arg(file->data);
241 if (gfile)
243 ianjuta_file_loader_load(loader, gfile, FALSE, NULL);
244 g_object_unref (gfile);
248 /* free the file list and reset to default */
249 g_list_foreach (file_list, (GFunc) g_free, NULL);
250 g_list_free (file_list);
251 file_list = NULL;
253 line_position = 0;
256 /* clientside */
257 static void
258 send_bacon_message (void)
260 GdkScreen *screen;
261 GdkDisplay *display;
262 const gchar *display_name;
263 gint screen_number;
264 GString *command;
266 screen = gdk_screen_get_default ();
267 display = gdk_screen_get_display (screen);
269 display_name = gdk_display_get_name (display);
270 screen_number = gdk_screen_get_number (screen);
272 command = g_string_new (NULL);
274 /* header */
275 g_string_append_printf (command,
276 "%" G_GUINT32_FORMAT "\t%s\t%d",
277 startup_timestamp,
278 display_name,
279 screen_number);
281 /* OPEN_URIS command, optionally specify line_num and encoding */
282 if (file_list)
284 GList *l;
286 command = g_string_append_c (command, '\v');
287 command = g_string_append (command, "OPEN-URIS");
289 g_string_append_printf (command,
290 "\t%d\t%d\t",
291 line_position,
292 g_list_length (file_list));
294 for (l = file_list; l != NULL; l = l->next)
296 command = g_string_append (command, l->data);
297 if (l->next != NULL)
298 command = g_string_append_c (command, ' ');
302 bacon_message_connection_send (connection,
303 command->str);
305 g_string_free (command, TRUE);
309 main (int argc, char *argv[])
311 GnomeProgram *program;
312 GOptionContext *context;
313 gchar *data_dir;
314 char *im_file;
316 context = g_option_context_new (_("- Integrated Development Environment"));
317 #ifdef ENABLE_NLS
318 setlocale (LC_ALL, "");
319 bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
320 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
321 textdomain (GETTEXT_PACKAGE);
322 g_option_context_add_main_entries (context, anjuta_options, PACKAGE);
323 #else
324 g_option_context_add_main_entries (context, anjuta_options, NULL);
325 #endif
327 data_dir = g_strdup (PACKAGE_DATA_DIR);
328 data_dir[strlen (data_dir) - strlen (PACKAGE) - 1] = '\0';
330 /* Initialize threads, if possible */
331 #ifdef G_THREADS_ENABLED
332 if (!g_thread_supported()) g_thread_init(NULL);
333 #else
334 #warning "Some plugins won't work without thread support"
335 #endif
337 /* Initialize gnome program */
338 g_option_context_parse (context, &argc, &argv, NULL);
339 program = gnome_program_init (PACKAGE, VERSION,
340 LIBGNOMEUI_MODULE, argc, argv,
341 GNOME_PARAM_GOPTION_CONTEXT, context,
342 GNOME_PARAM_HUMAN_READABLE_NAME,
343 _("Integrated Development Environment"),
344 GNOME_PARAM_APP_DATADIR, data_dir,
345 GNOME_PARAM_NONE);
346 g_free (data_dir);
348 /* Get the command line files */
349 get_command_line_args ();
351 connection = bacon_message_connection_new ("anjuta");
353 if (connection != NULL)
355 if (!bacon_message_connection_get_is_server (connection) &&
356 no_client == FALSE)
358 DEBUG_PRINT("Client");
359 send_bacon_message ();
361 /* we never popup a window... tell startup-notification
362 * that we are done.
364 gdk_notify_startup_complete ();
366 bacon_message_connection_free (connection);
368 exit (0);
370 else
372 DEBUG_PRINT("Server");
373 bacon_message_connection_set_callback (connection,
374 on_message_received,
375 NULL);
378 else
379 g_warning ("Cannot create the 'anjuta' connection.");
381 g_set_application_name (_("Anjuta"));
382 gtk_window_set_default_icon_name ("anjuta");
383 gtk_window_set_auto_startup_notification(FALSE);
385 im_file = anjuta_res_get_pixmap_file (ANJUTA_PIXMAP_SPLASH_SCREEN);
387 /* Initialize applicatoin */
388 app = anjuta_new (argv[0], file_list, no_splash, no_session, no_files,
389 im_file, proper_shutdown, anjuta_geometry);
391 g_free (im_file);
392 gtk_window_set_role (GTK_WINDOW (app), "anjuta-app");
394 /* Run Anjuta application */
395 gtk_window_set_auto_startup_notification(TRUE);
396 gtk_widget_show (GTK_WIDGET (app));
397 gtk_main();
399 gnome_vfs_shutdown();
401 return 0;