symbol-db: fixed a regression for local symbols displaying.
[anjuta.git] / src / anjuta.c
blob9e0d0a8c8db3c76d6e79ffe0f48ca006edd06e22
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta.c
4 * Copyright (C) 2000 Naba Kumar <naba@gnome.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <string.h>
22 #include <libanjuta/anjuta-shell.h>
23 #include <libanjuta/anjuta-debug.h>
24 #include <libanjuta/anjuta-utils.h>
25 #include <libanjuta/anjuta-save-prompt.h>
26 #include <libanjuta/anjuta-plugin-manager.h>
27 #include <libanjuta/interfaces/ianjuta-file-loader.h>
28 #include <libanjuta/interfaces/ianjuta-file.h>
30 #include "anjuta.h"
32 #define ANJUTA_REMEMBERED_PLUGINS "anjuta.remembered.plugins"
33 #define ANJUTA_SESSION_SKIP_LAST "anjuta.session.skip.last"
34 #define ANJUTA_SESSION_SKIP_LAST_FILES "anjuta.session.skip.last.files"
35 #define USER_SESSION_PATH_NEW (anjuta_util_get_user_cache_file_path ("session/", NULL))
37 #define DEFAULT_PROFILE "file://"PACKAGE_DATA_DIR"/profiles/default.profile"
38 #define USER_PROFILE_NAME "user"
40 static gchar *system_restore_session = NULL;
42 static gboolean
43 on_anjuta_delete_event (AnjutaApp *app, GdkEvent *event, gpointer data)
45 AnjutaPluginManager *plugin_manager;
46 AnjutaProfileManager *profile_manager;
47 AnjutaProfile *current_profile;
48 AnjutaSavePrompt *save_prompt;
49 gchar *remembered_plugins;
51 DEBUG_PRINT ("%s", "AnjutaApp delete event");
53 plugin_manager = anjuta_shell_get_plugin_manager (ANJUTA_SHELL (app), NULL);
54 profile_manager = anjuta_shell_get_profile_manager (ANJUTA_SHELL (app), NULL);
56 /* Save remembered plugins */
57 remembered_plugins =
58 anjuta_plugin_manager_get_remembered_plugins (plugin_manager);
59 anjuta_preferences_set (app->preferences,
60 ANJUTA_REMEMBERED_PLUGINS,
61 remembered_plugins);
62 g_free (remembered_plugins);
64 /* Check for unsaved data */
65 save_prompt = anjuta_save_prompt_new (GTK_WINDOW (app));
66 anjuta_shell_save_prompt (ANJUTA_SHELL (app), save_prompt, NULL);
68 if (anjuta_save_prompt_get_items_count (save_prompt) > 0)
70 switch (gtk_dialog_run (GTK_DIALOG (save_prompt)))
72 case ANJUTA_SAVE_PROMPT_RESPONSE_CANCEL:
73 gtk_widget_destroy (GTK_WIDGET (save_prompt));
74 /* Do not exit now */
75 return TRUE;
76 case ANJUTA_SAVE_PROMPT_RESPONSE_DISCARD:
77 case ANJUTA_SAVE_PROMPT_RESPONSE_SAVE_CLOSE:
78 /* exit now */
79 break;
82 /* Wait for files to be really saved (asyncronous operation) */
83 if (app->save_count > 0)
85 g_message ("Waiting for %d file(s) to be saved!", app->save_count);
86 while (app->save_count > 0)
88 g_main_context_iteration (NULL, TRUE);
92 /* If current active profile is "user", save current session as
93 * default session
95 current_profile = anjuta_profile_manager_get_current (profile_manager);
96 if (strcmp (anjuta_profile_get_name (current_profile), "user") == 0)
98 gchar *session_dir;
99 session_dir = USER_SESSION_PATH_NEW;
100 anjuta_shell_session_save (ANJUTA_SHELL (app), session_dir, NULL);
101 g_free (session_dir);
104 anjuta_shell_notify_exit (ANJUTA_SHELL (app), NULL);
106 gtk_widget_destroy (GTK_WIDGET (save_prompt));
108 /* Shutdown */
109 if (g_object_get_data (G_OBJECT (app), "__proper_shutdown"))
111 gtk_widget_hide (GTK_WIDGET (app));
112 anjuta_plugin_manager_unload_all_plugins
113 (anjuta_shell_get_plugin_manager (ANJUTA_SHELL (app), NULL));
115 return FALSE;
118 static void
119 on_anjuta_destroy (GtkWidget * w, gpointer data)
121 DEBUG_PRINT ("%s", "AnjutaApp destroy event");
123 gtk_widget_hide (w);
124 gtk_main_quit ();
127 static void
128 on_profile_scoped (AnjutaProfileManager *profile_manager,
129 AnjutaProfile *profile, AnjutaApp *app)
131 gchar *session_dir;
132 static gboolean first_time = TRUE;
134 DEBUG_PRINT ("Profile scoped: %s", anjuta_profile_get_name (profile));
135 if (strcmp (anjuta_profile_get_name (profile), USER_PROFILE_NAME) != 0)
136 return;
138 DEBUG_PRINT ("%s", "User profile loaded; Restoring user session");
140 /* If profile scoped to "user", restore user session */
141 if (system_restore_session)
143 session_dir = system_restore_session;
144 system_restore_session = NULL;
146 else
148 session_dir = USER_SESSION_PATH_NEW;
151 if (first_time)
153 first_time = FALSE;
155 else
157 /* Clear the files list since we don't want to load them later */
158 AnjutaSession *session;
159 session = anjuta_session_new (session_dir);
160 anjuta_session_set_string_list (session, "File Loader",
161 "Files", NULL);
162 anjuta_session_sync (session);
163 g_object_unref (session);
166 /* Restore session */
167 anjuta_shell_session_load (ANJUTA_SHELL (app), session_dir, NULL);
168 g_free (session_dir);
171 static void
172 on_profile_descoped (AnjutaProfileManager *profile_manager,
173 AnjutaProfile *profile, AnjutaApp *app)
175 gchar *session_dir;
177 DEBUG_PRINT ("Profile descoped: %s", anjuta_profile_get_name (profile));
178 if (strcmp (anjuta_profile_get_name (profile), USER_PROFILE_NAME) != 0)
179 return;
181 DEBUG_PRINT ("%s", "User profile descoped; Saving user session");
183 /* If profile descoped from is "user", save user session */
184 session_dir = USER_SESSION_PATH_NEW;
186 /* Save current session */
187 anjuta_shell_session_save (ANJUTA_SHELL (app), session_dir, NULL);
188 g_free (session_dir);
191 /* This extracts the project URI saved in the session so that it could be
192 * loaded sequencially. Returned string must be freed.
194 static gchar*
195 extract_project_from_session (const gchar* session_dir)
197 AnjutaSession *session;
198 GList *node, *files;
199 gchar *project_uri = NULL;
201 session = anjuta_session_new (session_dir);
203 files = anjuta_session_get_string_list (session, "File Loader", "Files");
204 g_object_unref (session);
206 /* Find project file */
207 node = files;
208 for (node = files; node != NULL; node = g_list_next (node))
210 gchar *uri = node->data;
211 if (uri)
213 if (anjuta_util_is_project_file (uri))
215 g_free (project_uri);
216 project_uri = uri;
218 else
220 g_free (uri);
224 g_list_free (files);
226 return project_uri;
229 /* FIXME: Clean this mess */
230 AnjutaApp*
231 anjuta_new (gchar *prog_name, gchar **files, gboolean no_splash,
232 gboolean no_session, gboolean no_files,
233 const gchar *im_file,
234 gboolean proper_shutdown, const gchar *geometry)
236 AnjutaPluginManager *plugin_manager;
237 AnjutaProfileManager *profile_manager;
238 AnjutaApp *app;
239 AnjutaStatus *status;
240 AnjutaProfile *profile;
241 GFile *session_profile;
242 gchar *remembered_plugins;
243 gchar *project_uri = NULL;
244 gchar *profile_name = NULL;
245 GError *error = NULL;
247 /* Initialize application */
248 app = ANJUTA_APP (anjuta_app_new ());
249 status = anjuta_shell_get_status (ANJUTA_SHELL (app), NULL);
250 anjuta_status_progress_add_ticks (status, 1);
252 if (im_file)
253 anjuta_status_set_splash (status, im_file, 100);
254 if (no_splash)
255 anjuta_status_disable_splash (status, no_splash);
257 if (proper_shutdown)
259 g_object_set_data (G_OBJECT (app), "__proper_shutdown", "1");
261 g_signal_connect (G_OBJECT (app), "delete_event",
262 G_CALLBACK (on_anjuta_delete_event), NULL);
263 g_signal_connect (G_OBJECT (app), "destroy",
264 G_CALLBACK (on_anjuta_destroy), NULL);
265 plugin_manager = anjuta_shell_get_plugin_manager (ANJUTA_SHELL (app),
266 NULL);
267 profile_manager = anjuta_shell_get_profile_manager (ANJUTA_SHELL (app),
268 NULL);
270 /* Restore remembered plugins */
271 remembered_plugins =
272 anjuta_preferences_get (app->preferences, ANJUTA_REMEMBERED_PLUGINS);
273 if (remembered_plugins)
274 anjuta_plugin_manager_set_remembered_plugins (plugin_manager,
275 remembered_plugins);
276 g_free (remembered_plugins);
278 /* Prepare profile */
279 profile = anjuta_profile_new (USER_PROFILE_NAME, plugin_manager);
280 session_profile = g_file_new_for_uri (DEFAULT_PROFILE);
281 anjuta_profile_add_plugins_from_xml (profile, session_profile,
282 TRUE, &error);
283 if (error)
285 anjuta_util_dialog_error (GTK_WINDOW (app), "%s", error->message);
286 g_error_free (error);
287 error = NULL;
289 g_object_unref (session_profile);
291 /* Load user session profile */
292 profile_name = g_path_get_basename (DEFAULT_PROFILE);
293 session_profile = anjuta_util_get_user_cache_file (profile_name, NULL);
294 if (g_file_query_exists (session_profile, NULL))
296 anjuta_profile_add_plugins_from_xml (profile, session_profile,
297 FALSE, &error);
298 if (error)
300 anjuta_util_dialog_error (GTK_WINDOW (app), "%s", error->message);
301 g_error_free (error);
302 error = NULL;
305 anjuta_profile_set_sync_file (profile, session_profile);
306 g_object_unref (session_profile);
307 g_free (profile_name);
309 /* Load profile */
310 anjuta_profile_manager_freeze (profile_manager);
311 anjuta_profile_manager_push (profile_manager, profile, &error);
312 if (error)
314 anjuta_util_dialog_error (GTK_WINDOW (app), "%s", error->message);
315 g_error_free (error);
316 error = NULL;
319 if (files || geometry)
321 gchar *session_dir;
322 AnjutaSession *session;
323 gchar **node;
324 GList *files_load = NULL;
326 /* Reset default session */
327 session_dir = USER_SESSION_PATH_NEW;
329 project_uri = extract_project_from_session (session_dir);
331 session = anjuta_session_new (session_dir);
332 anjuta_session_clear (session);
333 if (geometry)
334 anjuta_session_set_string (session, "Anjuta", "Geometry",
335 geometry);
337 /* Identify non-project files and set them for loading in session */
338 for (node = files; *node != NULL; node++)
340 if (anjuta_util_is_project_file (*node))
342 /* Pick up the first project file for loading later */
343 g_free (project_uri);
344 project_uri = g_strdup (*node);
346 else
348 files_load = g_list_prepend (files_load, *node);
351 if (files_load)
353 anjuta_session_set_string_list (session, "File Loader", "Files",
354 files_load);
355 g_list_foreach (files_load, (GFunc)g_free, NULL);
356 g_list_free (files_load);
358 anjuta_session_sync (session);
359 g_object_unref (session);
360 g_free (session_dir);
362 else
364 gchar *session_dir;
365 AnjutaSession *session;
367 /* Load user session */
368 session_dir = USER_SESSION_PATH_NEW;
370 /* If preferences is set to not load last session, clear it */
371 if (no_session ||
372 anjuta_preferences_get_bool (app->preferences,
373 ANJUTA_SESSION_SKIP_LAST))
375 /* Reset default session */
376 session = anjuta_session_new (session_dir);
377 anjuta_session_clear (session);
378 g_object_unref (session);
380 /* If preferences is set to not load last project, clear it */
381 else if (no_files ||
382 anjuta_preferences_get_bool (app->preferences,
383 ANJUTA_SESSION_SKIP_LAST_FILES))
385 session = anjuta_session_new (session_dir);
386 anjuta_session_set_string_list (session, "File Loader",
387 "Files", NULL);
388 anjuta_session_sync (session);
389 g_object_unref (session);
391 /* Otherwise, load session normally */
392 else
394 project_uri = extract_project_from_session (session_dir);
396 g_free (session_dir);
399 /* Prepare for session save and load on profile change */
400 g_signal_connect (profile_manager, "profile-scoped",
401 G_CALLBACK (on_profile_scoped), app);
402 /* Load project file */
403 if (project_uri)
405 GFile* file = g_file_new_for_uri (project_uri);
406 IAnjutaFileLoader *loader;
407 loader = anjuta_shell_get_interface (ANJUTA_SHELL (app),
408 IAnjutaFileLoader, NULL);
409 ianjuta_file_loader_load (loader, file, FALSE, NULL);
410 g_free (project_uri);
411 g_object_unref (file);
413 anjuta_profile_manager_thaw (profile_manager, &error);
415 if (error)
417 anjuta_util_dialog_error (GTK_WINDOW (app), "%s", error->message);
418 g_error_free (error);
419 error = NULL;
421 g_signal_connect (profile_manager, "profile-descoped",
422 G_CALLBACK (on_profile_descoped), app);
424 anjuta_status_progress_tick (status, NULL, _("Loaded Session…"));
425 anjuta_status_disable_splash (status, TRUE);
426 return app;