Added initial Spanish translation
[anjuta-git-plugin.git] / src / main.c
blobc48e59e81b49bb3c6dcc4cd5aac7ce03f82b17d9
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <gnome.h>
32 #include <gtk/gtkwindow.h>
33 #include <libgnomevfs/gnome-vfs-utils.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 N_("WIDTHxHEIGHT+XOFF+YOFF")
74 "no-splash", 's', 0, G_OPTION_ARG_NONE,
75 &no_splash,
76 N_("Do not show the splashscreen"),
77 NULL
80 "no-client", 'c', 0, G_OPTION_ARG_NONE,
81 &no_client,
82 N_("Start a new instance and do not open the file in a existing"),
83 NULL
86 "no-session", 'n', 0, G_OPTION_ARG_NONE,
87 &no_session,
88 N_("Do not open last session on startup"),
89 NULL
92 "no-files", 'f', 0, G_OPTION_ARG_NONE,
93 &no_files,
94 N_("Do not open last project and files on startup"),
95 NULL
98 "proper-shutdown", 'p', 0, G_OPTION_ARG_NONE,
99 &proper_shutdown,
100 N_("Shutdown anjuta properly releasing all resources (for debugging)"),
101 NULL
104 G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY,
105 &anjuta_filenames,
106 NULL,
107 NULL
109 {NULL}
112 static gchar*
113 get_real_path (const gchar *file_name)
115 if (file_name)
117 gchar path[PATH_MAX+1];
118 gchar *uri_scheme;
120 uri_scheme = gnome_vfs_get_uri_scheme (file_name);
121 if (!uri_scheme)
123 memset(path, '\0', PATH_MAX+1);
124 realpath(file_name, path);
125 return g_strdup (path);
127 g_free (uri_scheme);
128 return g_strdup (file_name);
130 else
131 return NULL;
134 static void
135 get_command_line_args ()
137 int i;
138 if (anjuta_filenames)
139 for (i = 0; anjuta_filenames[i]; i++)
140 file_list = g_list_append (file_list,
141 get_real_path (anjuta_filenames[i]));
144 static GdkDisplay *
145 display_open_if_needed (const gchar *name)
147 GSList *displays;
148 GSList *l;
149 GdkDisplay *display = NULL;
151 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
153 for (l = displays; l != NULL; l = l->next)
155 if (strcmp (gdk_display_get_name ((GdkDisplay *) l->data), name) == 0)
157 display = l->data;
158 break;
162 g_slist_free (displays);
164 return display != NULL ? display : gdk_display_open (name);
167 /* serverside */
168 static void
169 on_message_received (const char *message, gpointer data)
171 gchar **commands;
172 gchar **params;
173 gchar *display_name;
174 gint screen_number;
175 gint i;
176 GdkDisplay *display;
177 GdkScreen *screen;
179 g_return_if_fail (message != NULL);
181 commands = g_strsplit (message, "\v", -1);
183 /* header */
184 params = g_strsplit (commands[0], "\t", 4);
185 startup_timestamp = atoi (params[0]); /* CHECK if this is safe */
186 display_name = g_strdup (params[1]);
187 screen_number = atoi (params[2]);
189 display = display_open_if_needed (display_name);
190 screen = gdk_display_get_screen (display, screen_number);
191 g_free (display_name);
193 g_strfreev (params);
195 /* body */
196 gtk_window_present (GTK_WINDOW (app));
197 i = 1;
198 while (commands[i])
200 params = g_strsplit (commands[i], "\t", -1);
202 if (strcmp (params[0], "OPEN-URIS") == 0)
204 gint n_uris, i;
205 gchar **uris;
207 line_position = atoi (params[1]);
209 n_uris = atoi (params[2]);
210 uris = g_strsplit (params[3], " ", n_uris);
212 for (i = 0; i < n_uris; i++)
213 file_list = g_list_prepend (file_list, uris[i]);
214 file_list = g_list_reverse (file_list);
216 /* the list takes ownerhip of the strings,
217 * only free the array */
218 g_free (uris);
220 else
222 g_warning ("Unexpected bacon command");
225 g_strfreev (params);
226 ++i;
229 g_strfreev (commands);
231 /* execute the commands */
233 while (file_list != NULL)
235 IAnjutaFileLoader* file =
236 anjuta_shell_get_interface(ANJUTA_SHELL(app), IAnjutaFileLoader, NULL);
237 gchar* uri = g_strdup_printf("%s:%d", (gchar*) file_list->data, line_position);
239 ianjuta_file_loader_load(file, file_list->data, FALSE, NULL);
240 g_free(uri);
241 file_list = g_list_next(file_list);
244 /* free the file list and reset to default */
245 g_list_foreach (file_list, (GFunc) g_free, NULL);
246 g_list_free (file_list);
247 file_list = NULL;
249 line_position = 0;
252 /* clientside */
253 static void
254 send_bacon_message (void)
256 GdkScreen *screen;
257 GdkDisplay *display;
258 const gchar *display_name;
259 gint screen_number;
260 GString *command;
262 screen = gdk_screen_get_default ();
263 display = gdk_screen_get_display (screen);
265 display_name = gdk_display_get_name (display);
266 screen_number = gdk_screen_get_number (screen);
268 command = g_string_new (NULL);
270 /* header */
271 g_string_append_printf (command,
272 "%" G_GUINT32_FORMAT "\t%s\t%d",
273 startup_timestamp,
274 display_name,
275 screen_number);
277 /* OPEN_URIS command, optionally specify line_num and encoding */
278 if (file_list)
280 GList *l;
282 command = g_string_append_c (command, '\v');
283 command = g_string_append (command, "OPEN-URIS");
285 g_string_append_printf (command,
286 "\t%d\t%d\t",
287 line_position,
288 g_list_length (file_list));
290 for (l = file_list; l != NULL; l = l->next)
292 command = g_string_append (command, l->data);
293 if (l->next != NULL)
294 command = g_string_append_c (command, ' ');
298 bacon_message_connection_send (connection,
299 command->str);
301 g_string_free (command, TRUE);
305 main (int argc, char *argv[])
307 GnomeProgram *program;
308 GOptionContext *context;
309 gchar *data_dir;
310 char *im_file;
312 context = g_option_context_new (_("- Integrated Development Environment"));
313 #ifdef ENABLE_NLS
314 setlocale (LC_ALL, "");
315 bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
316 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
317 textdomain (GETTEXT_PACKAGE);
318 g_option_context_add_main_entries (context, anjuta_options, PACKAGE);
319 #else
320 g_option_context_add_main_entries (context, anjuta_options, NULL);
321 #endif
323 data_dir = g_strdup (PACKAGE_DATA_DIR);
324 data_dir[strlen (data_dir) - strlen (PACKAGE) - 1] = '\0';
326 /* Initialize threads, if possible */
327 #ifdef G_THREADS_ENABLED
328 if (!g_thread_supported()) g_thread_init(NULL);
329 #else
330 #warning "Some plugins won't work without thread support"
331 #endif
333 /* Initialize gnome program */
334 g_option_context_parse (context, &argc, &argv, NULL);
335 program = gnome_program_init (PACKAGE, VERSION,
336 LIBGNOMEUI_MODULE, argc, argv,
337 GNOME_PARAM_GOPTION_CONTEXT, context,
338 GNOME_PARAM_HUMAN_READABLE_NAME,
339 _("Integrated Development Environment"),
340 GNOME_PARAM_APP_DATADIR, data_dir,
341 NULL);
342 g_free (data_dir);
344 /* Get the command line files */
345 get_command_line_args ();
347 connection = bacon_message_connection_new ("anjuta");
349 if (connection != NULL)
351 if (!bacon_message_connection_get_is_server (connection) &&
352 no_client ==FALSE)
354 DEBUG_PRINT("Client");
355 send_bacon_message ();
357 /* we never popup a window... tell startup-notification
358 * that we are done.
360 gdk_notify_startup_complete ();
362 bacon_message_connection_free (connection);
364 exit (0);
366 else
368 DEBUG_PRINT("Server");
369 bacon_message_connection_set_callback (connection,
370 on_message_received,
371 NULL);
374 else
375 g_warning ("Cannot create the 'anjuta' connection.");
377 g_set_application_name (_("Anjuta"));
378 gtk_window_set_default_icon_name ("anjuta");
379 gtk_window_set_auto_startup_notification(FALSE);
381 im_file = anjuta_res_get_pixmap_file (ANJUTA_PIXMAP_SPLASH_SCREEN);
383 /* Initialize applicatoin */
384 app = anjuta_new (argv[0], file_list, no_splash, no_session, no_files,
385 im_file, proper_shutdown, anjuta_geometry);
387 g_free (im_file);
388 gtk_window_set_role (GTK_WINDOW (app), "anjuta-app");
390 /* Run Anjuta application */
391 gtk_window_set_auto_startup_notification(TRUE);
392 gtk_widget_show (GTK_WIDGET (app));
393 gtk_main();
394 return 0;