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: Store local user settings
25 * @stability: Unstable
26 * @include: libanjuta/anjuta-session.h
28 * A anjuta session contains local user settings, by example the list of files
29 * open in one project. These settings are stored in
30 * a .ini-like config file in a directory named session. Other libraries can
31 * store their own settings in another format in the same directory.
37 #include "anjuta-session.h"
38 #include "anjuta-utils.h"
40 struct _AnjutaSessionPriv
{
45 static gpointer
*parent_class
= NULL
;
48 anjuta_session_finalize (GObject
*object
)
51 cobj
= ANJUTA_SESSION (object
);
53 g_free (cobj
->priv
->dir_path
);
54 g_key_file_free (cobj
->priv
->key_file
);
57 G_OBJECT_CLASS(parent_class
)->finalize(object
);
61 anjuta_session_class_init (AnjutaSessionClass
*klass
)
63 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
65 parent_class
= g_type_class_peek_parent (klass
);
66 object_class
->finalize
= anjuta_session_finalize
;
70 anjuta_session_instance_init (AnjutaSession
*obj
)
72 obj
->priv
= g_new0 (AnjutaSessionPriv
, 1);
73 obj
->priv
->dir_path
= NULL
;
78 * @session_directory: Directory where session is loaded from/saved to.
80 * Created a new session object. @session_directory is the directory
81 * where session information will be stored or loaded in case of existing
84 * Returns: an #AnjutaSession Object
87 anjuta_session_new (const gchar
*session_directory
)
92 g_return_val_if_fail (session_directory
!= NULL
, NULL
);
93 g_return_val_if_fail (g_path_is_absolute (session_directory
), NULL
);
95 obj
= ANJUTA_SESSION (g_object_new (ANJUTA_TYPE_SESSION
, NULL
));
96 obj
->priv
->dir_path
= g_strdup (session_directory
);
98 obj
->priv
->key_file
= g_key_file_new ();
100 filename
= anjuta_session_get_session_filename (obj
);
101 g_key_file_load_from_file (obj
->priv
->key_file
, filename
,
102 G_KEY_FILE_NONE
, NULL
);
108 ANJUTA_TYPE_BOILERPLATE (AnjutaSession
, anjuta_session
, G_TYPE_OBJECT
)
111 * anjuta_session_get_session_directory:
112 * @session: an #AnjutaSession object
114 * Returns the directory corresponding to this session object.
116 * Returns: session directory
119 anjuta_session_get_session_directory (AnjutaSession
*session
)
121 return session
->priv
->dir_path
;
125 * anjuta_session_get_session_filename:
126 * @session: an #AnjutaSession object
128 * Gets the session filename corresponding to this session object.
130 * Returns: session (absolute) filename
133 anjuta_session_get_session_filename (AnjutaSession
*session
)
135 g_return_val_if_fail (ANJUTA_IS_SESSION (session
), NULL
);
137 return g_build_filename (session
->priv
->dir_path
,
138 "anjuta.session", NULL
);
142 * anjuta_session_sync:
143 * @session: an #AnjutaSession object
145 * Synchronizes session object with session file
148 anjuta_session_sync (AnjutaSession
*session
)
150 gchar
*filename
, *data
;
152 g_return_if_fail (ANJUTA_IS_SESSION (session
));
154 filename
= anjuta_session_get_session_filename (session
);
155 data
= g_key_file_to_data (session
->priv
->key_file
, NULL
, NULL
);
156 g_file_set_contents (filename
, data
, -1, NULL
);
163 * anjuta_session_clear:
164 * @session: an #AnjutaSession object
166 * Clears the session.
169 anjuta_session_clear (AnjutaSession
*session
)
174 g_return_if_fail (ANJUTA_IS_SESSION (session
));
176 g_key_file_free (session
->priv
->key_file
);
177 session
->priv
->key_file
= g_key_file_new ();
179 anjuta_session_sync (session
);
181 quoted
= g_shell_quote (session
->priv
->dir_path
);
182 cmd
= g_strconcat ("rm -fr ", quoted
, NULL
);
186 cmd
= g_strconcat ("mkdir -p ", quoted
, NULL
);
193 * anjuta_session_clear_section:
194 * @session: an #AnjutaSession object.
195 * @section: Section to clear.
197 * Clears the given section in session object.
200 anjuta_session_clear_section (AnjutaSession
*session
,
201 const gchar
*section
)
203 g_return_if_fail (ANJUTA_IS_SESSION (session
));
204 g_return_if_fail (section
!= NULL
);
206 g_key_file_remove_group (session
->priv
->key_file
, section
, NULL
);
210 * anjuta_session_set_int:
211 * @session: an #AnjutaSession object
216 * Set an integer @value to @key in given @section.
219 anjuta_session_set_int (AnjutaSession
*session
, const gchar
*section
,
220 const gchar
*key
, gint value
)
222 g_return_if_fail (ANJUTA_IS_SESSION (session
));
223 g_return_if_fail (section
!= NULL
);
224 g_return_if_fail (key
!= NULL
);
228 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
232 g_key_file_set_integer (session
->priv
->key_file
, section
, key
, value
);
236 * anjuta_session_set_float:
237 * @session: an #AnjutaSession object
242 * Set a float @value to @key in given @section.
245 anjuta_session_set_float (AnjutaSession
*session
, const gchar
*section
,
246 const gchar
*key
, gfloat value
)
248 g_return_if_fail (ANJUTA_IS_SESSION (session
));
249 g_return_if_fail (section
!= NULL
);
250 g_return_if_fail (key
!= NULL
);
254 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
258 g_key_file_set_double (session
->priv
->key_file
, section
, key
, 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
)
274 g_return_if_fail (ANJUTA_IS_SESSION (session
));
275 g_return_if_fail (section
!= NULL
);
276 g_return_if_fail (key
!= NULL
);
280 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
284 g_key_file_set_string (session
->priv
->key_file
, section
, key
, value
);
288 * anjuta_session_set_string_list:
289 * @session: an #AnjutaSession object
294 * Set a list of strings @value to @key in given @section.
297 anjuta_session_set_string_list (AnjutaSession
*session
,
298 const gchar
*section
,
299 const gchar
*key
, GList
*value
)
304 gboolean first_item
= TRUE
;
306 g_return_if_fail (ANJUTA_IS_SESSION (session
));
307 g_return_if_fail (section
!= NULL
);
308 g_return_if_fail (key
!= NULL
);
312 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
316 str
= g_string_new ("");
320 /* Keep empty string */
321 if (node
->data
!= NULL
)
326 g_string_append (str
, "%%%");
327 g_string_append (str
, node
->data
);
329 node
= g_list_next (node
);
332 value_str
= g_string_free (str
, FALSE
);
333 g_key_file_set_string (session
->priv
->key_file
, section
, key
, value_str
);
339 * anjuta_session_get_int:
340 * @session: an #AnjutaSession object
344 * Get an integer @value of @key in given @section.
349 anjuta_session_get_int (AnjutaSession
*session
, const gchar
*section
,
354 g_return_val_if_fail (ANJUTA_IS_SESSION (session
), 0);
355 g_return_val_if_fail (section
!= NULL
, 0);
356 g_return_val_if_fail (key
!= NULL
, 0);
358 value
= g_key_file_get_integer (session
->priv
->key_file
, section
, key
, NULL
);
364 * anjuta_session_get_float:
365 * @session: an #AnjutaSession object
369 * Get a float @value of @key in given @section.
374 anjuta_session_get_float (AnjutaSession
*session
, const gchar
*section
,
379 g_return_val_if_fail (ANJUTA_IS_SESSION (session
), 0);
380 g_return_val_if_fail (section
!= NULL
, 0);
381 g_return_val_if_fail (key
!= NULL
, 0);
383 value
= (float)g_key_file_get_double (session
->priv
->key_file
, section
, key
, NULL
);
389 * anjuta_session_get_string:
390 * @session: an #AnjutaSession object
394 * Get a string @value of @key in given @section.
399 anjuta_session_get_string (AnjutaSession
*session
, const gchar
*section
,
404 g_return_val_if_fail (ANJUTA_IS_SESSION (session
), NULL
);
405 g_return_val_if_fail (section
!= NULL
, NULL
);
406 g_return_val_if_fail (key
!= NULL
, NULL
);
408 value
= g_key_file_get_string (session
->priv
->key_file
, section
, key
, NULL
);
414 * anjuta_session_get_string_list:
415 * @session: an #AnjutaSession object
419 * Get a list of strings @value of @key in given @section.
424 anjuta_session_get_string_list (AnjutaSession
*session
,
425 const gchar
*section
,
428 gchar
*val
, **str
, **ptr
;
431 g_return_val_if_fail (ANJUTA_IS_SESSION (session
), NULL
);
432 g_return_val_if_fail (section
!= NULL
, NULL
);
433 g_return_val_if_fail (key
!= NULL
, NULL
);
435 val
= g_key_file_get_string (session
->priv
->key_file
, section
, key
, NULL
);
441 str
= g_strsplit (val
, "%%%", -1);
447 /* Keep empty string */
448 value
= g_list_prepend (value
, g_strdup (*ptr
));
456 return g_list_reverse (value
);
461 * anjuta_session_get_relative_uri_from_file:
462 * @session: an #AnjutaSession object
464 * @fragment: an optional fragment
466 * Return an URI relative to the session directory file with an optional
468 * It is useful to keep only relative file paths in a session file to be able
469 * to copy the whole project without breaking references.
471 * Returns: (transfer full): A string that has to be freed with g_free().
474 anjuta_session_get_relative_uri_from_file (AnjutaSession
*session
,
476 const gchar
*fragment
)
482 parent
= g_file_new_for_path (session
->priv
->dir_path
);
483 for (level
= 0; (parent
!= NULL
) && !g_file_equal (file
, parent
) && !g_file_has_prefix (file
, parent
); level
++)
485 GFile
*next
= g_file_get_parent (parent
);
486 g_object_unref (parent
);
492 uri
= g_file_get_uri (file
);
498 if (g_file_equal (file
, parent
))
504 path
= g_file_get_relative_path (parent
, file
);
505 uri
= g_uri_escape_string (path
, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH
, TRUE
);
515 buffer
= g_new (gchar
, len
+ level
* 3 + 1);
516 for (ptr
= buffer
; level
; level
--)
518 memcpy (ptr
, ".." G_DIR_SEPARATOR_S
, 3);
521 memcpy (ptr
, uri
, len
+ 1);
528 if (fragment
!= NULL
)
530 gchar
*with_fragment
;
532 with_fragment
= g_strconcat (uri
, "#", fragment
, NULL
);
542 * anjuta_session_get_file_from_relative_uri:
543 * @session: an #AnjutaSession object
544 * @uri: a relative URI from a key
545 * @fragment: (allow-none): fragment part of the URI if existing, can be %NULL
547 * Return a GFile corresponding to the URI and and optional fragment,
548 * normally read from a session key.
549 * The path is expected to be relative to the session directory but it works
550 * with an absolute URI, in this case it returns the same file than
551 * g_file_new_for_uri.
552 * It is useful to keep only relative file paths in a session file to be able
553 * to copy the whole project without breaking references.
555 * Returns: (transfer full): A new GFile that has to be freed with g_object_unref().
558 anjuta_session_get_file_from_relative_uri (AnjutaSession
*session
,
560 const gchar
**fragment
)
565 scheme
=g_uri_parse_scheme (uri
);
569 file
= g_file_new_for_uri (uri
);
573 gchar
*parent_uri
= g_filename_to_uri (session
->priv
->dir_path
, NULL
, NULL
);
576 full_uri
= g_strconcat (parent_uri
, G_DIR_SEPARATOR_S
, uri
, NULL
);
577 file
= g_file_new_for_uri (full_uri
);
581 if (fragment
!= NULL
)
583 *fragment
= strchr (uri
, '#');
584 if (*fragment
!= NULL
) (*fragment
)++;