2008-04-16 Johannes Schmid <jhs@gnome.org>
[anjuta-git-plugin.git] / libanjuta / anjuta-session.c
blob3986f8cd9ee98a2e44de6497850c128a3ec86cda
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>
5 *
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>
32 #include <libgnome/gnome-config.h>
34 #include "anjuta-session.h"
35 #include "anjuta-utils.h"
37 struct _AnjutaSessionPriv {
38 gchar *dir_path;
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_free (cobj->priv);
52 G_OBJECT_CLASS(parent_class)->finalize(object);
55 static void
56 anjuta_session_class_init (AnjutaSessionClass *klass)
58 GObjectClass *object_class = G_OBJECT_CLASS (klass);
60 parent_class = g_type_class_peek_parent (klass);
61 object_class->finalize = anjuta_session_finalize;
64 static void
65 anjuta_session_instance_init (AnjutaSession *obj)
67 obj->priv = g_new0 (AnjutaSessionPriv, 1);
68 obj->priv->dir_path = NULL;
71 /**
72 * anjuta_session_new:
73 * @session_directory: Directory where session is loaded from/saved to.
75 * Created a new session object. @session_directory is the directory
76 * where session information will be stored or loaded in case of existing
77 * session.
79 * Returns: an #AnjutaSession Object
81 AnjutaSession*
82 anjuta_session_new (const gchar *session_directory)
84 AnjutaSession *obj;
86 g_return_val_if_fail (session_directory != NULL, NULL);
87 g_return_val_if_fail (g_path_is_absolute (session_directory), NULL);
89 obj = ANJUTA_SESSION (g_object_new (ANJUTA_TYPE_SESSION, NULL));
90 obj->priv->dir_path = g_strdup (session_directory);
91 return obj;
94 ANJUTA_TYPE_BOILERPLATE (AnjutaSession, anjuta_session, G_TYPE_OBJECT)
96 /**
97 * anjuta_session_get_session_directory:
98 * @session: an #AnjutaSession object
100 * Returns the directory corresponding to this session object.
102 * Returns: session directory
104 const gchar*
105 anjuta_session_get_session_directory (AnjutaSession *session)
107 return session->priv->dir_path;
111 * anjuta_session_get_session_filename:
112 * @session: an #AnjutaSession object
114 * Gets the session filename corresponding to this session object.
116 * Returns: session (absolute) filename
118 gchar*
119 anjuta_session_get_session_filename (AnjutaSession *session)
121 g_return_val_if_fail (ANJUTA_IS_SESSION (session), NULL);
123 return g_build_filename (session->priv->dir_path,
124 "anjuta.session", NULL);
127 static gchar*
128 anjuta_session_get_key_path (AnjutaSession *session, const gchar *section,
129 const gchar *key)
131 gchar *key_path, *filename;
133 filename = anjuta_session_get_session_filename (session);
134 key_path = g_strdup_printf ("=%s=/%s/%s", filename, section, key);
135 g_free (filename);
136 return key_path;
140 * anjuta_session_sync:
141 * @session: an #AnjutaSession object
143 * Synchronizes session object with session file
145 void
146 anjuta_session_sync (AnjutaSession *session)
148 gchar *filename;
149 gchar *path;
151 g_return_if_fail (ANJUTA_IS_SESSION (session));
153 filename = anjuta_session_get_session_filename (session);
154 path = g_strdup_printf ("=%s=", filename);
155 gnome_config_sync_file (path);
156 g_free (filename);
157 g_free (path);
161 * anjuta_session_clear:
162 * @session: an #AnjutaSession object
164 * Clears the session.
166 void
167 anjuta_session_clear (AnjutaSession *session)
169 gchar *path;
170 gchar *filename, *cmd;
172 g_return_if_fail (ANJUTA_IS_SESSION (session));
174 filename = anjuta_session_get_session_filename (session);
175 path = g_strdup_printf ("=%s=", filename);
176 gnome_config_clean_file (path);
177 g_free (filename);
178 g_free (path);
180 anjuta_session_sync (session);
182 cmd = g_strconcat ("mkdir -p ", session->priv->dir_path, NULL);
183 system (cmd);
184 g_free (cmd);
186 cmd = g_strconcat ("rm -fr ", session->priv->dir_path, "/*", NULL);
187 system (cmd);
188 g_free (cmd);
192 * anjuta_session_clear_section:
193 * @session: an #AnjutaSession object.
194 * @section: Section to clear.
196 * Clears the given section in session object.
198 void
199 anjuta_session_clear_section (AnjutaSession *session,
200 const gchar *section)
202 gchar *filename, *section_path;
204 g_return_if_fail (ANJUTA_IS_SESSION (session));
205 g_return_if_fail (section != NULL);
207 filename = anjuta_session_get_session_filename (session);
208 section_path = g_strdup_printf ("=%s=/%s", filename, section);
209 gnome_config_clean_section (section_path);
210 g_free (filename);
214 * anjuta_session_set_int:
215 * @session: an #AnjutaSession object
216 * @section: Section.
217 * @key: Key name.
218 * @value: Key value
220 * Set an integer @value to @key in given @section.
222 void
223 anjuta_session_set_int (AnjutaSession *session, const gchar *section,
224 const gchar *key, gint value)
226 gchar *key_path;
228 g_return_if_fail (ANJUTA_IS_SESSION (session));
229 g_return_if_fail (section != NULL);
230 g_return_if_fail (key != NULL);
232 key_path = anjuta_session_get_key_path (session, section, key);
233 gnome_config_set_int (key_path, value);
234 g_free (key_path);
238 * anjuta_session_set_float:
239 * @session: an #AnjutaSession object
240 * @section: Section.
241 * @key: Key name.
242 * @value: Key value
244 * Set a float @value to @key in given @section.
246 void
247 anjuta_session_set_float (AnjutaSession *session, const gchar *section,
248 const gchar *key, gfloat value)
250 gchar *key_path;
252 g_return_if_fail (ANJUTA_IS_SESSION (session));
253 g_return_if_fail (section != NULL);
254 g_return_if_fail (key != NULL);
256 key_path = anjuta_session_get_key_path (session, section, key);
257 gnome_config_set_float (key_path, value);
258 g_free (key_path);
262 * anjuta_session_set_string:
263 * @session: an #AnjutaSession object
264 * @section: Section.
265 * @key: Key name.
266 * @value: Key value
268 * Set a string @value to @key in given @section.
270 void
271 anjuta_session_set_string (AnjutaSession *session, const gchar *section,
272 const gchar *key, const gchar *value)
274 gchar *key_path;
276 g_return_if_fail (ANJUTA_IS_SESSION (session));
277 g_return_if_fail (section != NULL);
278 g_return_if_fail (key != NULL);
280 key_path = anjuta_session_get_key_path (session, section, key);
281 gnome_config_set_string (key_path, value);
282 g_free (key_path);
286 * anjuta_session_set_string_list:
287 * @session: an #AnjutaSession object
288 * @section: Section.
289 * @key: Key name.
290 * @value: Key value
292 * Set a list of strings @value to @key in given @section.
294 void
295 anjuta_session_set_string_list (AnjutaSession *session,
296 const gchar *section,
297 const gchar *key, GList *value)
299 gchar *key_path, *value_str;
300 GString *str;
301 GList *node;
302 gboolean first_item = TRUE;
304 g_return_if_fail (ANJUTA_IS_SESSION (session));
305 g_return_if_fail (section != NULL);
306 g_return_if_fail (key != NULL);
308 key_path = anjuta_session_get_key_path (session, section, key);
309 str = g_string_new ("");
310 node = value;
311 while (node)
313 if (node->data && strlen (node->data) > 0)
315 if (first_item)
316 first_item = FALSE;
317 else
318 g_string_append (str, "%%%");
319 g_string_append (str, node->data);
321 node = g_list_next (node);
324 value_str = g_string_free (str, FALSE);
325 gnome_config_set_string (key_path, value_str);
327 g_free (value_str);
328 g_free (key_path);
332 * anjuta_session_get_int:
333 * @session: an #AnjutaSession object
334 * @section: Section.
335 * @key: Key name.
337 * Get an integer @value of @key in given @section.
339 * Returns: Key value
341 gint
342 anjuta_session_get_int (AnjutaSession *session, const gchar *section,
343 const gchar *key)
345 gchar *key_path;
346 gint value;
348 g_return_val_if_fail (ANJUTA_IS_SESSION (session), 0);
349 g_return_val_if_fail (section != NULL, 0);
350 g_return_val_if_fail (key != NULL, 0);
352 key_path = anjuta_session_get_key_path (session, section, key);
353 value = gnome_config_get_int (key_path);
354 g_free (key_path);
355 return value;
359 * anjuta_session_get_float:
360 * @session: an #AnjutaSession object
361 * @section: Section.
362 * @key: Key name.
364 * Get a float @value of @key in given @section.
366 * Returns: Key value
368 gfloat
369 anjuta_session_get_float (AnjutaSession *session, const gchar *section,
370 const gchar *key)
372 gchar *key_path;
373 gfloat value;
375 g_return_val_if_fail (ANJUTA_IS_SESSION (session), 0);
376 g_return_val_if_fail (section != NULL, 0);
377 g_return_val_if_fail (key != NULL, 0);
379 key_path = anjuta_session_get_key_path (session, section, key);
380 value = gnome_config_get_float (key_path);
381 g_free (key_path);
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 *key_path;
400 gchar *value;
402 g_return_val_if_fail (ANJUTA_IS_SESSION (session), NULL);
403 g_return_val_if_fail (section != NULL, NULL);
404 g_return_val_if_fail (key != NULL, NULL);
406 key_path = anjuta_session_get_key_path (session, section, key);
407 value = gnome_config_get_string (key_path);
408 g_free (key_path);
409 return value;
413 * anjuta_session_get_string_list:
414 * @session: an #AnjutaSession object
415 * @section: Section.
416 * @key: Key name.
418 * Get a list of strings @value of @key in given @section.
420 * Returns: Key value
422 GList*
423 anjuta_session_get_string_list (AnjutaSession *session,
424 const gchar *section,
425 const gchar *key)
427 gchar *key_path, *val, **str, **ptr;
428 GList *value;
430 g_return_val_if_fail (ANJUTA_IS_SESSION (session), NULL);
431 g_return_val_if_fail (section != NULL, NULL);
432 g_return_val_if_fail (key != NULL, NULL);
434 key_path = anjuta_session_get_key_path (session, section, key);
435 val = gnome_config_get_string (key_path);
437 value = NULL;
438 if (val)
440 str = g_strsplit (val, "%%%", -1);
441 if (str)
443 ptr = str;
444 while (*ptr)
446 if (strlen (*ptr) > 0)
447 value = g_list_prepend (value, g_strdup (*ptr));
448 ptr++;
450 g_strfreev (str);
452 g_free (val);
454 g_free (key_path);
456 return g_list_reverse (value);