Implement fetching
[anjuta-git-plugin.git] / src / main.c
blobf9563ff027cf1dadac1d7c374b762253f002af09
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;
181 g_return_if_fail (message != NULL);
183 commands = g_strsplit (message, "\v", -1);
185 /* header */
186 params = g_strsplit (commands[0], "\t", 4);
187 startup_timestamp = atoi (params[0]); /* CHECK if this is safe */
188 display_name = g_strdup (params[1]);
189 screen_number = atoi (params[2]);
191 display = display_open_if_needed (display_name);
192 screen = gdk_display_get_screen (display, screen_number);
193 g_free (display_name);
195 g_strfreev (params);
197 /* body */
198 gtk_window_present (GTK_WINDOW (app));
199 i = 1;
200 while (commands[i])
202 params = g_strsplit (commands[i], "\t", -1);
204 if (strcmp (params[0], "OPEN-URIS") == 0)
206 gint n_uris, i;
207 gchar **uris;
209 line_position = atoi (params[1]);
211 n_uris = atoi (params[2]);
212 uris = g_strsplit (params[3], " ", n_uris);
214 for (i = 0; i < n_uris; i++)
215 file_list = g_list_prepend (file_list, uris[i]);
216 file_list = g_list_reverse (file_list);
218 /* the list takes ownerhip of the strings,
219 * only free the array */
220 g_free (uris);
222 else
224 g_warning ("Unexpected bacon command");
227 g_strfreev (params);
228 ++i;
231 g_strfreev (commands);
233 /* execute the commands */
235 while (file_list != NULL)
237 IAnjutaFileLoader* file =
238 anjuta_shell_get_interface(ANJUTA_SHELL(app), IAnjutaFileLoader, NULL);
239 gchar* uri = g_strdup_printf("%s:%d", (gchar*) file_list->data, line_position);
241 ianjuta_file_loader_load(file, file_list->data, FALSE, NULL);
242 g_free(uri);
243 file_list = g_list_next(file_list);
246 /* free the file list and reset to default */
247 g_list_foreach (file_list, (GFunc) g_free, NULL);
248 g_list_free (file_list);
249 file_list = NULL;
251 line_position = 0;
254 /* clientside */
255 static void
256 send_bacon_message (void)
258 GdkScreen *screen;
259 GdkDisplay *display;
260 const gchar *display_name;
261 gint screen_number;
262 GString *command;
264 screen = gdk_screen_get_default ();
265 display = gdk_screen_get_display (screen);
267 display_name = gdk_display_get_name (display);
268 screen_number = gdk_screen_get_number (screen);
270 command = g_string_new (NULL);
272 /* header */
273 g_string_append_printf (command,
274 "%" G_GUINT32_FORMAT "\t%s\t%d",
275 startup_timestamp,
276 display_name,
277 screen_number);
279 /* OPEN_URIS command, optionally specify line_num and encoding */
280 if (file_list)
282 GList *l;
284 command = g_string_append_c (command, '\v');
285 command = g_string_append (command, "OPEN-URIS");
287 g_string_append_printf (command,
288 "\t%d\t%d\t",
289 line_position,
290 g_list_length (file_list));
292 for (l = file_list; l != NULL; l = l->next)
294 command = g_string_append (command, l->data);
295 if (l->next != NULL)
296 command = g_string_append_c (command, ' ');
300 bacon_message_connection_send (connection,
301 command->str);
303 g_string_free (command, TRUE);
307 main (int argc, char *argv[])
309 GnomeProgram *program;
310 GOptionContext *context;
311 gchar *data_dir;
312 char *im_file;
314 context = g_option_context_new (_("- Integrated Development Environment"));
315 #ifdef ENABLE_NLS
316 setlocale (LC_ALL, "");
317 bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
318 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
319 textdomain (GETTEXT_PACKAGE);
320 g_option_context_add_main_entries (context, anjuta_options, PACKAGE);
321 #else
322 g_option_context_add_main_entries (context, anjuta_options, NULL);
323 #endif
325 data_dir = g_strdup (PACKAGE_DATA_DIR);
326 data_dir[strlen (data_dir) - strlen (PACKAGE) - 1] = '\0';
328 /* Initialize threads, if possible */
329 #ifdef G_THREADS_ENABLED
330 if (!g_thread_supported()) g_thread_init(NULL);
331 #else
332 #warning "Some plugins won't work without thread support"
333 #endif
335 /* Initialize gnome program */
336 g_option_context_parse (context, &argc, &argv, NULL);
337 program = gnome_program_init (PACKAGE, VERSION,
338 LIBGNOMEUI_MODULE, argc, argv,
339 GNOME_PARAM_GOPTION_CONTEXT, context,
340 GNOME_PARAM_HUMAN_READABLE_NAME,
341 _("Integrated Development Environment"),
342 GNOME_PARAM_APP_DATADIR, data_dir,
343 GNOME_PARAM_NONE);
344 g_free (data_dir);
346 /* Get the command line files */
347 get_command_line_args ();
349 connection = bacon_message_connection_new ("anjuta");
351 if (connection != NULL)
353 if (!bacon_message_connection_get_is_server (connection) &&
354 no_client ==FALSE)
356 DEBUG_PRINT("Client");
357 send_bacon_message ();
359 /* we never popup a window... tell startup-notification
360 * that we are done.
362 gdk_notify_startup_complete ();
364 bacon_message_connection_free (connection);
366 exit (0);
368 else
370 DEBUG_PRINT("Server");
371 bacon_message_connection_set_callback (connection,
372 on_message_received,
373 NULL);
376 else
377 g_warning ("Cannot create the 'anjuta' connection.");
379 g_set_application_name (_("Anjuta"));
380 gtk_window_set_default_icon_name ("anjuta");
381 gtk_window_set_auto_startup_notification(FALSE);
383 im_file = anjuta_res_get_pixmap_file (ANJUTA_PIXMAP_SPLASH_SCREEN);
385 /* Initialize applicatoin */
386 app = anjuta_new (argv[0], file_list, no_splash, no_session, no_files,
387 im_file, proper_shutdown, anjuta_geometry);
389 g_free (im_file);
390 gtk_window_set_role (GTK_WINDOW (app), "anjuta-app");
392 /* Run Anjuta application */
393 gtk_window_set_auto_startup_notification(TRUE);
394 gtk_widget_show (GTK_WIDGET (app));
395 gtk_main();
397 gnome_vfs_shutdown();
399 return 0;