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
33 #include "anjuta-session.h"
34 #include "anjuta-utils.h"
36 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
);
50 g_key_file_free (cobj
->priv
->key_file
);
53 G_OBJECT_CLASS(parent_class
)->finalize(object
);
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
;
66 anjuta_session_instance_init (AnjutaSession
*obj
)
68 obj
->priv
= g_new0 (AnjutaSessionPriv
, 1);
69 obj
->priv
->dir_path
= NULL
;
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
80 * Returns: an #AnjutaSession Object
83 anjuta_session_new (const gchar
*session_directory
)
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
);
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
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
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
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
);
159 * anjuta_session_clear:
160 * @session: an #AnjutaSession object
162 * Clears the session.
165 anjuta_session_clear (AnjutaSession
*session
)
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
);
181 cmd
= g_strconcat ("rm -fr ", session
->priv
->dir_path
, "/*", NULL
);
187 * anjuta_session_clear_section:
188 * @session: an #AnjutaSession object.
189 * @section: Section to clear.
191 * Clears the given section in session object.
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
210 * Set an integer @value to @key in given @section.
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
);
222 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
226 g_key_file_set_integer (session
->priv
->key_file
, section
, key
, value
);
230 * anjuta_session_set_float:
231 * @session: an #AnjutaSession object
236 * Set a float @value to @key in given @section.
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
);
248 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
252 g_key_file_set_double (session
->priv
->key_file
, section
, key
, value
);
256 * anjuta_session_set_string:
257 * @session: an #AnjutaSession object
262 * Set a string @value to @key in given @section.
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
);
274 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
278 g_key_file_set_string (session
->priv
->key_file
, section
, key
, value
);
282 * anjuta_session_set_string_list:
283 * @session: an #AnjutaSession object
288 * Set a list of strings @value to @key in given @section.
291 anjuta_session_set_string_list (AnjutaSession
*session
,
292 const gchar
*section
,
293 const gchar
*key
, GList
*value
)
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
);
306 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
310 str
= g_string_new ("");
314 if (node
->data
&& strlen (node
->data
) > 0)
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
);
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
,
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
);
357 * anjuta_session_get_float:
358 * @session: an #AnjutaSession object
362 * Get a float @value of @key in given @section.
367 anjuta_session_get_float (AnjutaSession
*session
, const gchar
*section
,
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
);
382 * anjuta_session_get_string:
383 * @session: an #AnjutaSession object
387 * Get a string @value of @key in given @section.
392 anjuta_session_get_string (AnjutaSession
*session
, const gchar
*section
,
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
);
407 * anjuta_session_get_string_list:
408 * @session: an #AnjutaSession object
412 * Get a list of strings @value of @key in given @section.
417 anjuta_session_get_string_list (AnjutaSession
*session
,
418 const gchar
*section
,
421 gchar
*val
, **str
, **ptr
;
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
);
434 str
= g_strsplit (val
, "%%%", -1);
440 if (strlen (*ptr
) > 0)
441 value
= g_list_prepend (value
, g_strdup (*ptr
));
449 return g_list_reverse (value
);