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
)
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
);
183 cmd
= g_strconcat ("mkdir -p ", quoted
, NULL
);
190 * anjuta_session_clear_section:
191 * @session: an #AnjutaSession object.
192 * @section: Section to clear.
194 * Clears the given section in session object.
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
213 * Set an integer @value to @key in given @section.
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
);
225 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
229 g_key_file_set_integer (session
->priv
->key_file
, section
, key
, value
);
233 * anjuta_session_set_float:
234 * @session: an #AnjutaSession object
239 * Set a float @value to @key in given @section.
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
);
251 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
255 g_key_file_set_double (session
->priv
->key_file
, section
, key
, value
);
259 * anjuta_session_set_string:
260 * @session: an #AnjutaSession object
265 * Set a string @value to @key in given @section.
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
);
277 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
281 g_key_file_set_string (session
->priv
->key_file
, section
, key
, value
);
285 * anjuta_session_set_string_list:
286 * @session: an #AnjutaSession object
291 * Set a list of strings @value to @key in given @section.
294 anjuta_session_set_string_list (AnjutaSession
*session
,
295 const gchar
*section
,
296 const gchar
*key
, GList
*value
)
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
);
309 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
313 str
= g_string_new ("");
317 /* Keep empty string */
318 if (node
->data
!= NULL
)
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
);
336 * anjuta_session_get_int:
337 * @session: an #AnjutaSession object
341 * Get an integer @value of @key in given @section.
346 anjuta_session_get_int (AnjutaSession
*session
, const gchar
*section
,
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
);
361 * anjuta_session_get_float:
362 * @session: an #AnjutaSession object
366 * Get a float @value of @key in given @section.
371 anjuta_session_get_float (AnjutaSession
*session
, const gchar
*section
,
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
);
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
,
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
);
411 * anjuta_session_get_string_list:
412 * @session: an #AnjutaSession object
416 * Get a list of strings @value of @key in given @section.
421 anjuta_session_get_string_list (AnjutaSession
*session
,
422 const gchar
*section
,
425 gchar
*val
, **str
, **ptr
;
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
);
438 str
= g_strsplit (val
, "%%%", -1);
444 /* Keep empty string */
445 value
= g_list_prepend (value
, g_strdup (*ptr
));
453 return g_list_reverse (value
);