1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) 2000 Naba Kumar <naba@gnome.org>
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)
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
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
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>
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
;
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 */
58 anjuta_plugin_manager_get_remembered_plugins (plugin_manager
);
59 anjuta_preferences_set (app
->preferences
,
60 ANJUTA_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
));
76 case ANJUTA_SAVE_PROMPT_RESPONSE_DISCARD
:
77 case ANJUTA_SAVE_PROMPT_RESPONSE_SAVE_CLOSE
:
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
95 current_profile
= anjuta_profile_manager_get_current (profile_manager
);
96 if (strcmp (anjuta_profile_get_name (current_profile
), "user") == 0)
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
));
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
));
119 on_anjuta_destroy (GtkWidget
* w
, gpointer data
)
121 DEBUG_PRINT ("%s", "AnjutaApp destroy event");
128 on_profile_scoped (AnjutaProfileManager
*profile_manager
,
129 AnjutaProfile
*profile
, AnjutaApp
*app
)
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)
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
;
148 session_dir
= USER_SESSION_PATH_NEW
;
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",
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
);
172 on_profile_descoped (AnjutaProfileManager
*profile_manager
,
173 AnjutaProfile
*profile
, AnjutaApp
*app
)
177 DEBUG_PRINT ("Profile descoped: %s", anjuta_profile_get_name (profile
));
178 if (strcmp (anjuta_profile_get_name (profile
), USER_PROFILE_NAME
) != 0)
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.
195 extract_project_from_session (const gchar
* session_dir
)
197 AnjutaSession
*session
;
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 */
208 for (node
= files
; node
!= NULL
; node
= g_list_next (node
))
210 gchar
*uri
= node
->data
;
213 if (anjuta_util_is_project_file (uri
))
215 g_free (project_uri
);
229 /* FIXME: Clean this mess */
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
;
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);
253 anjuta_status_set_splash (status
, im_file
, 100);
255 anjuta_status_disable_splash (status
, no_splash
);
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
),
267 profile_manager
= anjuta_shell_get_profile_manager (ANJUTA_SHELL (app
),
270 /* Restore remembered plugins */
272 anjuta_preferences_get (app
->preferences
, ANJUTA_REMEMBERED_PLUGINS
);
273 if (remembered_plugins
)
274 anjuta_plugin_manager_set_remembered_plugins (plugin_manager
,
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
,
285 anjuta_util_dialog_error (GTK_WINDOW (app
), "%s", error
->message
);
286 g_error_free (error
);
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
,
300 anjuta_util_dialog_error (GTK_WINDOW (app
), "%s", error
->message
);
301 g_error_free (error
);
305 anjuta_profile_set_sync_file (profile
, session_profile
);
306 g_object_unref (session_profile
);
307 g_free (profile_name
);
310 anjuta_profile_manager_freeze (profile_manager
);
311 anjuta_profile_manager_push (profile_manager
, profile
, &error
);
314 anjuta_util_dialog_error (GTK_WINDOW (app
), "%s", error
->message
);
315 g_error_free (error
);
319 if (files
|| geometry
)
322 AnjutaSession
*session
;
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
);
334 anjuta_session_set_string (session
, "Anjuta", "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
);
348 files_load
= g_list_prepend (files_load
, *node
);
353 anjuta_session_set_string_list (session
, "File Loader", "Files",
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
);
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 */
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 */
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",
388 anjuta_session_sync (session
);
389 g_object_unref (session
);
391 /* Otherwise, load session normally */
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 */
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
);
417 anjuta_util_dialog_error (GTK_WINDOW (app
), "%s", error
->message
);
418 g_error_free (error
);
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
);