1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
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.
22 * SECTION:anjuta-session
23 * @short_description: Program session
25 * @stability: Unstable
26 * @include: libanjuta/anjuta-session.h
32 #include <libgnome/gnome-config.h>
34 #include "anjuta-session.h"
35 #include "anjuta-utils.h"
37 struct _AnjutaSessionPriv
{
41 static gpointer
*parent_class
= NULL
;
44 anjuta_session_finalize (GObject
*object
)
47 cobj
= ANJUTA_SESSION (object
);
49 g_free (cobj
->priv
->dir_path
);
52 G_OBJECT_CLASS(parent_class
)->finalize(object
);
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
;
65 anjuta_session_instance_init (AnjutaSession
*obj
)
67 obj
->priv
= g_new0 (AnjutaSessionPriv
, 1);
68 obj
->priv
->dir_path
= NULL
;
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
79 * Returns: an #AnjutaSession Object
82 anjuta_session_new (const gchar
*session_directory
)
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
);
94 ANJUTA_TYPE_BOILERPLATE (AnjutaSession
, anjuta_session
, G_TYPE_OBJECT
)
97 * anjuta_session_get_session_directory:
98 * @session: an #AnjutaSession object
100 * Returns the directory corresponding to this session object.
102 * Returns: session directory
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
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
);
128 anjuta_session_get_key_path (AnjutaSession
*session
, const gchar
*section
,
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
);
140 * anjuta_session_sync:
141 * @session: an #AnjutaSession object
143 * Synchronizes session object with session file
146 anjuta_session_sync (AnjutaSession
*session
)
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
);
161 * anjuta_session_clear:
162 * @session: an #AnjutaSession object
164 * Clears the session.
167 anjuta_session_clear (AnjutaSession
*session
)
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
);
180 anjuta_session_sync (session
);
182 cmd
= g_strconcat ("mkdir -p ", session
->priv
->dir_path
, NULL
);
186 cmd
= g_strconcat ("rm -fr ", session
->priv
->dir_path
, "/*", NULL
);
192 * anjuta_session_clear_section:
193 * @session: an #AnjutaSession object.
194 * @section: Section to clear.
196 * Clears the given section in session object.
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
);
214 * anjuta_session_set_int:
215 * @session: an #AnjutaSession object
220 * Set an integer @value to @key in given @section.
223 anjuta_session_set_int (AnjutaSession
*session
, const gchar
*section
,
224 const gchar
*key
, gint value
)
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
);
238 * anjuta_session_set_float:
239 * @session: an #AnjutaSession object
244 * Set a float @value to @key in given @section.
247 anjuta_session_set_float (AnjutaSession
*session
, const gchar
*section
,
248 const gchar
*key
, gfloat value
)
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
);
262 * anjuta_session_set_string:
263 * @session: an #AnjutaSession object
268 * Set a string @value to @key in given @section.
271 anjuta_session_set_string (AnjutaSession
*session
, const gchar
*section
,
272 const gchar
*key
, const gchar
*value
)
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
);
286 * anjuta_session_set_string_list:
287 * @session: an #AnjutaSession object
292 * Set a list of strings @value to @key in given @section.
295 anjuta_session_set_string_list (AnjutaSession
*session
,
296 const gchar
*section
,
297 const gchar
*key
, GList
*value
)
299 gchar
*key_path
, *value_str
;
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 ("");
313 if (node
->data
&& strlen (node
->data
) > 0)
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
);
332 * anjuta_session_get_int:
333 * @session: an #AnjutaSession object
337 * Get an integer @value of @key in given @section.
342 anjuta_session_get_int (AnjutaSession
*session
, const gchar
*section
,
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
);
359 * anjuta_session_get_float:
360 * @session: an #AnjutaSession object
364 * Get a float @value of @key in given @section.
369 anjuta_session_get_float (AnjutaSession
*session
, const gchar
*section
,
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
);
386 * anjuta_session_get_string:
387 * @session: an #AnjutaSession object
391 * Get a string @value of @key in given @section.
396 anjuta_session_get_string (AnjutaSession
*session
, const gchar
*section
,
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
);
413 * anjuta_session_get_string_list:
414 * @session: an #AnjutaSession object
418 * Get a list of strings @value of @key in given @section.
423 anjuta_session_get_string_list (AnjutaSession
*session
,
424 const gchar
*section
,
427 gchar
*key_path
, *val
, **str
, **ptr
;
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
);
440 str
= g_strsplit (val
, "%%%", -1);
446 if (strlen (*ptr
) > 0)
447 value
= g_list_prepend (value
, g_strdup (*ptr
));
456 return g_list_reverse (value
);