libanjuta: Fix some warnings and made AnjutaPluginDescription a boxed type
[anjuta.git] / libanjuta / anjuta-session.c
blobd2104ee0e9b3390d5734b3d70d4dc15c6671ae4a
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta-session.c
4 * Copyright (c) 2005 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 Library 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 /**
22 * SECTION:anjuta-session
23 * @short_description: Program session
24 * @see_also:
25 * @stability: Unstable
26 * @include: libanjuta/anjuta-session.h
30 #include <stdlib.h>
31 #include <string.h>
33 #include "anjuta-session.h"
34 #include "anjuta-utils.h"
36 struct _AnjutaSessionPriv {
37 gchar *dir_path;
38 GKeyFile *key_file;
41 static gpointer *parent_class = NULL;
43 static void
44 anjuta_session_finalize (GObject *object)
46 AnjutaSession *cobj;
47 cobj = ANJUTA_SESSION (object);
49 g_free (cobj->priv->dir_path);
50 g_key_file_free (cobj->priv->key_file);
51 g_free (cobj->priv);
53 G_OBJECT_CLASS(parent_class)->finalize(object);
56 static void
57 anjuta_session_class_init (AnjutaSessionClass *klass)
59 GObjectClass *object_class = G_OBJECT_CLASS (klass);
61 parent_class = g_type_class_peek_parent (klass);
62 object_class->finalize = anjuta_session_finalize;
65 static void
66 anjuta_session_instance_init (AnjutaSession *obj)
68 obj->priv = g_new0 (AnjutaSessionPriv, 1);
69 obj->priv->dir_path = NULL;
72 /**
73 * anjuta_session_new:
74 * @session_directory: Directory where session is loaded from/saved to.
76 * Created a new session object. @session_directory is the directory
77 * where session information will be stored or loaded in case of existing
78 * session.
80 * Returns: an #AnjutaSession Object
82 AnjutaSession*
83 anjuta_session_new (const gchar *session_directory)
85 AnjutaSession *obj;
86 gchar *filename;
88 g_return_val_if_fail (session_directory != NULL, NULL);
89 g_return_val_if_fail (g_path_is_absolute (session_directory), NULL);
91 obj = ANJUTA_SESSION (g_object_new (ANJUTA_TYPE_SESSION, NULL));
92 obj->priv->dir_path = g_strdup (session_directory);
94 obj->priv->key_file = g_key_file_new ();
96 filename = anjuta_session_get_session_filename (obj);
97 g_key_file_load_from_file (obj->priv->key_file, filename,
98 G_KEY_FILE_NONE, NULL);
99 g_free (filename);
101 return obj;
104 ANJUTA_TYPE_BOILERPLATE (AnjutaSession, anjuta_session, G_TYPE_OBJECT)
107 * anjuta_session_get_session_directory:
108 * @session: an #AnjutaSession object
110 * Returns the directory corresponding to this session object.
112 * Returns: session directory
114 const gchar*
115 anjuta_session_get_session_directory (AnjutaSession *session)
117 return session->priv->dir_path;
121 * anjuta_session_get_session_filename:
122 * @session: an #AnjutaSession object
124 * Gets the session filename corresponding to this session object.
126 * Returns: session (absolute) filename
128 gchar*
129 anjuta_session_get_session_filename (AnjutaSession *session)
131 g_return_val_if_fail (ANJUTA_IS_SESSION (session), NULL);
133 return g_build_filename (session->priv->dir_path,
134 "anjuta.session", NULL);
138 * anjuta_session_sync:
139 * @session: an #AnjutaSession object
141 * Synchronizes session object with session file
143 void
144 anjuta_session_sync (AnjutaSession *session)
146 gchar *filename, *data;
148 g_return_if_fail (ANJUTA_IS_SESSION (session));
150 filename = anjuta_session_get_session_filename (session);
151 data = g_key_file_to_data (session->priv->key_file, NULL, NULL);
152 g_file_set_contents (filename, data, -1, NULL);
154 g_free (filename);
155 g_free (data);
159 * anjuta_session_clear:
160 * @session: an #AnjutaSession object
162 * Clears the session.
164 void
165 anjuta_session_clear (AnjutaSession *session)
167 gchar *cmd;
168 gchar *quoted;
169 gint ret;
171 g_return_if_fail (ANJUTA_IS_SESSION (session));
173 g_key_file_free (session->priv->key_file);
174 session->priv->key_file = g_key_file_new ();
176 anjuta_session_sync (session);
178 quoted = g_shell_quote (session->priv->dir_path);
179 cmd = g_strconcat ("rm -fr ", quoted, NULL);
180 ret = system (cmd);
181 g_free (cmd);
183 cmd = g_strconcat ("mkdir -p ", quoted, NULL);
184 ret = system (cmd);
185 g_free (cmd);
186 g_free (quoted);
190 * anjuta_session_clear_section:
191 * @session: an #AnjutaSession object.
192 * @section: Section to clear.
194 * Clears the given section in session object.
196 void
197 anjuta_session_clear_section (AnjutaSession *session,
198 const gchar *section)
200 g_return_if_fail (ANJUTA_IS_SESSION (session));
201 g_return_if_fail (section != NULL);
203 g_key_file_remove_group (session->priv->key_file, section, NULL);
207 * anjuta_session_set_int:
208 * @session: an #AnjutaSession object
209 * @section: Section.
210 * @key: Key name.
211 * @value: Key value
213 * Set an integer @value to @key in given @section.
215 void
216 anjuta_session_set_int (AnjutaSession *session, const gchar *section,
217 const gchar *key, gint value)
219 g_return_if_fail (ANJUTA_IS_SESSION (session));
220 g_return_if_fail (section != NULL);
221 g_return_if_fail (key != NULL);
223 if (!value)
225 g_key_file_remove_key (session->priv->key_file, section, key, NULL);
226 return;
229 g_key_file_set_integer (session->priv->key_file, section, key, value);
233 * anjuta_session_set_float:
234 * @session: an #AnjutaSession object
235 * @section: Section.
236 * @key: Key name.
237 * @value: Key value
239 * Set a float @value to @key in given @section.
241 void
242 anjuta_session_set_float (AnjutaSession *session, const gchar *section,
243 const gchar *key, gfloat value)
245 g_return_if_fail (ANJUTA_IS_SESSION (session));
246 g_return_if_fail (section != NULL);
247 g_return_if_fail (key != NULL);
249 if (!value)
251 g_key_file_remove_key (session->priv->key_file, section, key, NULL);
252 return;
255 g_key_file_set_double (session->priv->key_file, section, key, value);
259 * anjuta_session_set_string:
260 * @session: an #AnjutaSession object
261 * @section: Section.
262 * @key: Key name.
263 * @value: Key value
265 * Set a string @value to @key in given @section.
267 void
268 anjuta_session_set_string (AnjutaSession *session, const gchar *section,
269 const gchar *key, const gchar *value)
271 g_return_if_fail (ANJUTA_IS_SESSION (session));
272 g_return_if_fail (section != NULL);
273 g_return_if_fail (key != NULL);
275 if (!value)
277 g_key_file_remove_key (session->priv->key_file, section, key, NULL);
278 return;
281 g_key_file_set_string (session->priv->key_file, section, key, value);
285 * anjuta_session_set_string_list:
286 * @session: an #AnjutaSession object
287 * @section: Section.
288 * @key: Key name.
289 * @value: Key value
291 * Set a list of strings @value to @key in given @section.
293 void
294 anjuta_session_set_string_list (AnjutaSession *session,
295 const gchar *section,
296 const gchar *key, GList *value)
298 gchar *value_str;
299 GString *str;
300 GList *node;
301 gboolean first_item = TRUE;
303 g_return_if_fail (ANJUTA_IS_SESSION (session));
304 g_return_if_fail (section != NULL);
305 g_return_if_fail (key != NULL);
307 if (!value)
309 g_key_file_remove_key (session->priv->key_file, section, key, NULL);
310 return;
313 str = g_string_new ("");
314 node = value;
315 while (node)
317 /* Keep empty string */
318 if (node->data != NULL)
320 if (first_item)
321 first_item = FALSE;
322 else
323 g_string_append (str, "%%%");
324 g_string_append (str, node->data);
326 node = g_list_next (node);
329 value_str = g_string_free (str, FALSE);
330 g_key_file_set_string (session->priv->key_file, section, key, value_str);
332 g_free (value_str);
336 * anjuta_session_get_int:
337 * @session: an #AnjutaSession object
338 * @section: Section.
339 * @key: Key name.
341 * Get an integer @value of @key in given @section.
343 * Returns: Key value
345 gint
346 anjuta_session_get_int (AnjutaSession *session, const gchar *section,
347 const gchar *key)
349 gint value;
351 g_return_val_if_fail (ANJUTA_IS_SESSION (session), 0);
352 g_return_val_if_fail (section != NULL, 0);
353 g_return_val_if_fail (key != NULL, 0);
355 value = g_key_file_get_integer (session->priv->key_file, section, key, NULL);
357 return value;
361 * anjuta_session_get_float:
362 * @session: an #AnjutaSession object
363 * @section: Section.
364 * @key: Key name.
366 * Get a float @value of @key in given @section.
368 * Returns: Key value
370 gfloat
371 anjuta_session_get_float (AnjutaSession *session, const gchar *section,
372 const gchar *key)
374 gfloat value;
376 g_return_val_if_fail (ANJUTA_IS_SESSION (session), 0);
377 g_return_val_if_fail (section != NULL, 0);
378 g_return_val_if_fail (key != NULL, 0);
380 value = (float)g_key_file_get_double (session->priv->key_file, section, key, NULL);
382 return value;
386 * anjuta_session_get_string:
387 * @session: an #AnjutaSession object
388 * @section: Section.
389 * @key: Key name.
391 * Get a string @value of @key in given @section.
393 * Returns: Key value
395 gchar*
396 anjuta_session_get_string (AnjutaSession *session, const gchar *section,
397 const gchar *key)
399 gchar *value;
401 g_return_val_if_fail (ANJUTA_IS_SESSION (session), NULL);
402 g_return_val_if_fail (section != NULL, NULL);
403 g_return_val_if_fail (key != NULL, NULL);
405 value = g_key_file_get_string (session->priv->key_file, section, key, NULL);
407 return value;
411 * anjuta_session_get_string_list:
412 * @session: an #AnjutaSession object
413 * @section: Section.
414 * @key: Key name.
416 * Get a list of strings @value of @key in given @section.
418 * Returns: Key value
420 GList*
421 anjuta_session_get_string_list (AnjutaSession *session,
422 const gchar *section,
423 const gchar *key)
425 gchar *val, **str, **ptr;
426 GList *value;
428 g_return_val_if_fail (ANJUTA_IS_SESSION (session), NULL);
429 g_return_val_if_fail (section != NULL, NULL);
430 g_return_val_if_fail (key != NULL, NULL);
432 val = g_key_file_get_string (session->priv->key_file, section, key, NULL);
435 value = NULL;
436 if (val)
438 str = g_strsplit (val, "%%%", -1);
439 if (str)
441 ptr = str;
442 while (*ptr)
444 /* Keep empty string */
445 value = g_list_prepend (value, g_strdup (*ptr));
446 ptr++;
448 g_strfreev (str);
450 g_free (val);
453 return g_list_reverse (value);
458 * anjuta_session_get_relative_uri_from_file:
459 * @session: an #AnjutaSession object
460 * @file: a GFile
461 * @fragment: an optional fragment
463 * Return an URI relative to the session directory file with an optional
464 * fragment.
465 * It is useful to keep only relative file paths in a session file to be able
466 * to copy the whole project without breaking references.
468 * Returns: A string that has to be freed with g_free().
470 gchar *
471 anjuta_session_get_relative_uri_from_file (AnjutaSession *session,
472 GFile *file,
473 const gchar *fragment)
475 GFile *parent;
476 gchar *uri;
477 gint level;
479 parent = g_file_new_for_path (session->priv->dir_path);
480 for (level = 0; (parent != NULL) && !g_file_has_prefix (file, parent); level++)
482 GFile *next = g_file_get_parent (parent);
483 g_object_unref (parent);
484 parent = next;
487 if (parent == NULL)
489 uri = g_file_get_uri (file);
491 else
493 gchar *path;
495 path = g_file_get_relative_path (parent, file);
496 uri = g_uri_escape_string (path, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE);
497 g_free (path);
498 if (level != 0)
500 gsize len;
501 gchar *buffer;
502 gchar *ptr;
504 len = strlen (uri);
505 buffer = g_new (gchar, len + level * 3 + 1);
506 for (ptr = buffer; level; level--)
508 memcpy (ptr, ".." G_DIR_SEPARATOR_S, 3);
509 ptr += 3;
511 memcpy (ptr, uri, len + 1);
512 g_free (uri);
514 uri = buffer;
518 if (fragment != NULL)
520 gchar *with_fragment;
522 with_fragment = g_strconcat (uri, "#", fragment, NULL);
523 g_free (uri);
524 uri = with_fragment;
527 return uri;
532 * anjuta_session_get_file_from_relative_uri:
533 * @session: an #AnjutaSession object
534 * @uri: a relative URI from a key
535 * @fragment: fragment part of the URI if existing, cal be NULL
537 * Return a GFile corresponding to the URI and and optional fragment,
538 * normally read from a session key.
539 * The path is expected to be relative to the session directory but it works
540 * with an absolute URI, in this case it returns the same file than
541 * g_file_new_for_uri.
542 * It is useful to keep only relative file paths in a session file to be able
543 * to copy the whole project without breaking references.
545 * Returns: A new GFile that has to be freed with g_object_unref().
547 GFile*
548 anjuta_session_get_file_from_relative_uri (AnjutaSession *session,
549 const gchar *uri,
550 const gchar **fragment)
552 GFile *file;
553 gchar *scheme;
555 scheme =g_uri_parse_scheme (uri);
556 if (scheme != NULL)
558 free (scheme);
559 file = g_file_new_for_uri (uri);
561 else
563 gchar *parent_uri = g_filename_to_uri (session->priv->dir_path, NULL, NULL);
564 gchar *full_uri;
566 full_uri = g_strconcat (parent_uri, G_DIR_SEPARATOR_S, uri, NULL);
567 file = g_file_new_for_uri (full_uri);
568 g_free (full_uri);
569 g_free (parent_uri);
571 if (fragment != NULL)
573 *fragment = strchr (uri, '#');
574 if (*fragment != NULL) (*fragment)++;
577 return file;