Updated Spanish translation
[anjuta.git] / libanjuta / anjuta-session.c
blob22a4de2e87cb7bd64617208b090f9b414172a953
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>
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 gint ret;
170 g_return_if_fail (ANJUTA_IS_SESSION (session));
172 g_key_file_free (session->priv->key_file);
173 session->priv->key_file = g_key_file_new ();
175 anjuta_session_sync (session);
177 cmd = g_strconcat ("mkdir -p ", session->priv->dir_path, NULL);
178 ret = system (cmd);
179 g_free (cmd);
181 cmd = g_strconcat ("rm -fr ", session->priv->dir_path, "/*", NULL);
182 ret = system (cmd);
183 g_free (cmd);
187 * anjuta_session_clear_section:
188 * @session: an #AnjutaSession object.
189 * @section: Section to clear.
191 * Clears the given section in session object.
193 void
194 anjuta_session_clear_section (AnjutaSession *session,
195 const gchar *section)
197 g_return_if_fail (ANJUTA_IS_SESSION (session));
198 g_return_if_fail (section != NULL);
200 g_key_file_remove_group (session->priv->key_file, section, NULL);
204 * anjuta_session_set_int:
205 * @session: an #AnjutaSession object
206 * @section: Section.
207 * @key: Key name.
208 * @value: Key value
210 * Set an integer @value to @key in given @section.
212 void
213 anjuta_session_set_int (AnjutaSession *session, const gchar *section,
214 const gchar *key, gint value)
216 g_return_if_fail (ANJUTA_IS_SESSION (session));
217 g_return_if_fail (section != NULL);
218 g_return_if_fail (key != NULL);
220 if (!value)
222 g_key_file_remove_key (session->priv->key_file, section, key, NULL);
223 return;
226 g_key_file_set_integer (session->priv->key_file, section, key, value);
230 * anjuta_session_set_float:
231 * @session: an #AnjutaSession object
232 * @section: Section.
233 * @key: Key name.
234 * @value: Key value
236 * Set a float @value to @key in given @section.
238 void
239 anjuta_session_set_float (AnjutaSession *session, const gchar *section,
240 const gchar *key, gfloat value)
242 g_return_if_fail (ANJUTA_IS_SESSION (session));
243 g_return_if_fail (section != NULL);
244 g_return_if_fail (key != NULL);
246 if (!value)
248 g_key_file_remove_key (session->priv->key_file, section, key, NULL);
249 return;
252 g_key_file_set_double (session->priv->key_file, section, key, value);
256 * anjuta_session_set_string:
257 * @session: an #AnjutaSession object
258 * @section: Section.
259 * @key: Key name.
260 * @value: Key value
262 * Set a string @value to @key in given @section.
264 void
265 anjuta_session_set_string (AnjutaSession *session, const gchar *section,
266 const gchar *key, const gchar *value)
268 g_return_if_fail (ANJUTA_IS_SESSION (session));
269 g_return_if_fail (section != NULL);
270 g_return_if_fail (key != NULL);
272 if (!value)
274 g_key_file_remove_key (session->priv->key_file, section, key, NULL);
275 return;
278 g_key_file_set_string (session->priv->key_file, section, key, value);
282 * anjuta_session_set_string_list:
283 * @session: an #AnjutaSession object
284 * @section: Section.
285 * @key: Key name.
286 * @value: Key value
288 * Set a list of strings @value to @key in given @section.
290 void
291 anjuta_session_set_string_list (AnjutaSession *session,
292 const gchar *section,
293 const gchar *key, GList *value)
295 gchar *value_str;
296 GString *str;
297 GList *node;
298 gboolean first_item = TRUE;
300 g_return_if_fail (ANJUTA_IS_SESSION (session));
301 g_return_if_fail (section != NULL);
302 g_return_if_fail (key != NULL);
304 if (!value)
306 g_key_file_remove_key (session->priv->key_file, section, key, NULL);
307 return;
310 str = g_string_new ("");
311 node = value;
312 while (node)
314 if (node->data && strlen (node->data) > 0)
316 if (first_item)
317 first_item = FALSE;
318 else
319 g_string_append (str, "%%%");
320 g_string_append (str, node->data);
322 node = g_list_next (node);
325 value_str = g_string_free (str, FALSE);
326 g_key_file_set_string (session->priv->key_file, section, key, value_str);
328 g_free (value_str);
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 gint value;
347 g_return_val_if_fail (ANJUTA_IS_SESSION (session), 0);
348 g_return_val_if_fail (section != NULL, 0);
349 g_return_val_if_fail (key != NULL, 0);
351 value = g_key_file_get_integer (session->priv->key_file, section, key, NULL);
353 return value;
357 * anjuta_session_get_float:
358 * @session: an #AnjutaSession object
359 * @section: Section.
360 * @key: Key name.
362 * Get a float @value of @key in given @section.
364 * Returns: Key value
366 gfloat
367 anjuta_session_get_float (AnjutaSession *session, const gchar *section,
368 const gchar *key)
370 gfloat value;
372 g_return_val_if_fail (ANJUTA_IS_SESSION (session), 0);
373 g_return_val_if_fail (section != NULL, 0);
374 g_return_val_if_fail (key != NULL, 0);
376 value = (float)g_key_file_get_double (session->priv->key_file, section, key, NULL);
378 return value;
382 * anjuta_session_get_string:
383 * @session: an #AnjutaSession object
384 * @section: Section.
385 * @key: Key name.
387 * Get a string @value of @key in given @section.
389 * Returns: Key value
391 gchar*
392 anjuta_session_get_string (AnjutaSession *session, const gchar *section,
393 const gchar *key)
395 gchar *value;
397 g_return_val_if_fail (ANJUTA_IS_SESSION (session), NULL);
398 g_return_val_if_fail (section != NULL, NULL);
399 g_return_val_if_fail (key != NULL, NULL);
401 value = g_key_file_get_string (session->priv->key_file, section, key, NULL);
403 return value;
407 * anjuta_session_get_string_list:
408 * @session: an #AnjutaSession object
409 * @section: Section.
410 * @key: Key name.
412 * Get a list of strings @value of @key in given @section.
414 * Returns: Key value
416 GList*
417 anjuta_session_get_string_list (AnjutaSession *session,
418 const gchar *section,
419 const gchar *key)
421 gchar *val, **str, **ptr;
422 GList *value;
424 g_return_val_if_fail (ANJUTA_IS_SESSION (session), NULL);
425 g_return_val_if_fail (section != NULL, NULL);
426 g_return_val_if_fail (key != NULL, NULL);
428 val = g_key_file_get_string (session->priv->key_file, section, key, NULL);
431 value = NULL;
432 if (val)
434 str = g_strsplit (val, "%%%", -1);
435 if (str)
437 ptr = str;
438 while (*ptr)
440 if (strlen (*ptr) > 0)
441 value = g_list_prepend (value, g_strdup (*ptr));
442 ptr++;
444 g_strfreev (str);
446 g_free (val);
449 return g_list_reverse (value);