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
)
175 g_return_if_fail (ANJUTA_IS_SESSION (session
));
177 g_key_file_free (session
->priv
->key_file
);
178 session
->priv
->key_file
= g_key_file_new ();
180 anjuta_session_sync (session
);
182 quoted
= g_shell_quote (session
->priv
->dir_path
);
183 cmd
= g_strconcat ("rm -fr ", quoted
, NULL
);
187 cmd
= g_strconcat ("mkdir -p ", quoted
, NULL
);
194 * anjuta_session_clear_section:
195 * @session: an #AnjutaSession object.
196 * @section: Section to clear.
198 * Clears the given section in session object.
201 anjuta_session_clear_section (AnjutaSession
*session
,
202 const gchar
*section
)
204 g_return_if_fail (ANJUTA_IS_SESSION (session
));
205 g_return_if_fail (section
!= NULL
);
207 g_key_file_remove_group (session
->priv
->key_file
, section
, NULL
);
211 * anjuta_session_set_int:
212 * @session: an #AnjutaSession object
217 * Set an integer @value to @key in given @section.
220 anjuta_session_set_int (AnjutaSession
*session
, const gchar
*section
,
221 const gchar
*key
, gint value
)
223 g_return_if_fail (ANJUTA_IS_SESSION (session
));
224 g_return_if_fail (section
!= NULL
);
225 g_return_if_fail (key
!= NULL
);
229 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
233 g_key_file_set_integer (session
->priv
->key_file
, section
, key
, value
);
237 * anjuta_session_set_float:
238 * @session: an #AnjutaSession object
243 * Set a float @value to @key in given @section.
246 anjuta_session_set_float (AnjutaSession
*session
, const gchar
*section
,
247 const gchar
*key
, gfloat value
)
249 g_return_if_fail (ANJUTA_IS_SESSION (session
));
250 g_return_if_fail (section
!= NULL
);
251 g_return_if_fail (key
!= NULL
);
255 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
259 g_key_file_set_double (session
->priv
->key_file
, section
, key
, value
);
263 * anjuta_session_set_string:
264 * @session: an #AnjutaSession object
269 * Set a string @value to @key in given @section.
272 anjuta_session_set_string (AnjutaSession
*session
, const gchar
*section
,
273 const gchar
*key
, const gchar
*value
)
275 g_return_if_fail (ANJUTA_IS_SESSION (session
));
276 g_return_if_fail (section
!= NULL
);
277 g_return_if_fail (key
!= NULL
);
281 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
285 g_key_file_set_string (session
->priv
->key_file
, section
, key
, value
);
289 * anjuta_session_set_string_list:
290 * @session: an #AnjutaSession object
295 * Set a list of strings @value to @key in given @section.
298 anjuta_session_set_string_list (AnjutaSession
*session
,
299 const gchar
*section
,
300 const gchar
*key
, GList
*value
)
305 gboolean first_item
= TRUE
;
307 g_return_if_fail (ANJUTA_IS_SESSION (session
));
308 g_return_if_fail (section
!= NULL
);
309 g_return_if_fail (key
!= NULL
);
313 g_key_file_remove_key (session
->priv
->key_file
, section
, key
, NULL
);
317 str
= g_string_new ("");
321 /* Keep empty string */
322 if (node
->data
!= NULL
)
327 g_string_append (str
, "%%%");
328 g_string_append (str
, node
->data
);
330 node
= g_list_next (node
);
333 value_str
= g_string_free (str
, FALSE
);
334 g_key_file_set_string (session
->priv
->key_file
, section
, key
, value_str
);
340 * anjuta_session_get_int:
341 * @session: an #AnjutaSession object
345 * Get an integer @value of @key in given @section.
350 anjuta_session_get_int (AnjutaSession
*session
, const gchar
*section
,
355 g_return_val_if_fail (ANJUTA_IS_SESSION (session
), 0);
356 g_return_val_if_fail (section
!= NULL
, 0);
357 g_return_val_if_fail (key
!= NULL
, 0);
359 value
= g_key_file_get_integer (session
->priv
->key_file
, section
, key
, NULL
);
365 * anjuta_session_get_float:
366 * @session: an #AnjutaSession object
370 * Get a float @value of @key in given @section.
375 anjuta_session_get_float (AnjutaSession
*session
, const gchar
*section
,
380 g_return_val_if_fail (ANJUTA_IS_SESSION (session
), 0);
381 g_return_val_if_fail (section
!= NULL
, 0);
382 g_return_val_if_fail (key
!= NULL
, 0);
384 value
= (float)g_key_file_get_double (session
->priv
->key_file
, section
, key
, NULL
);
390 * anjuta_session_get_string:
391 * @session: an #AnjutaSession object
395 * Get a string @value of @key in given @section.
400 anjuta_session_get_string (AnjutaSession
*session
, const gchar
*section
,
405 g_return_val_if_fail (ANJUTA_IS_SESSION (session
), NULL
);
406 g_return_val_if_fail (section
!= NULL
, NULL
);
407 g_return_val_if_fail (key
!= NULL
, NULL
);
409 value
= g_key_file_get_string (session
->priv
->key_file
, section
, key
, NULL
);
415 * anjuta_session_get_string_list:
416 * @session: an #AnjutaSession object
420 * Get a list of strings @value of @key in given @section.
425 anjuta_session_get_string_list (AnjutaSession
*session
,
426 const gchar
*section
,
429 gchar
*val
, **str
, **ptr
;
432 g_return_val_if_fail (ANJUTA_IS_SESSION (session
), NULL
);
433 g_return_val_if_fail (section
!= NULL
, NULL
);
434 g_return_val_if_fail (key
!= NULL
, NULL
);
436 val
= g_key_file_get_string (session
->priv
->key_file
, section
, key
, NULL
);
442 str
= g_strsplit (val
, "%%%", -1);
448 /* Keep empty string */
449 value
= g_list_prepend (value
, g_strdup (*ptr
));
457 return g_list_reverse (value
);
462 * anjuta_session_get_relative_uri_from_file:
463 * @session: an #AnjutaSession object
465 * @fragment: an optional fragment
467 * Return an URI relative to the session directory file with an optional
469 * It is useful to keep only relative file paths in a session file to be able
470 * to copy the whole project without breaking references.
472 * Returns: A string that has to be freed with g_free().
475 anjuta_session_get_relative_uri_from_file (AnjutaSession
*session
,
477 const gchar
*fragment
)
483 parent
= g_file_new_for_path (session
->priv
->dir_path
);
484 for (level
= 0; (parent
!= NULL
) && !g_file_has_prefix (file
, parent
); level
++)
486 GFile
*next
= g_file_get_parent (parent
);
487 g_object_unref (parent
);
493 uri
= g_file_get_uri (file
);
499 path
= g_file_get_relative_path (parent
, file
);
500 uri
= g_uri_escape_string (path
, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH
, TRUE
);
509 buffer
= g_new (gchar
, len
+ level
* 3 + 1);
510 for (ptr
= buffer
; level
; level
--)
512 memcpy (ptr
, ".." G_DIR_SEPARATOR_S
, 3);
515 memcpy (ptr
, uri
, len
+ 1);
522 if (fragment
!= NULL
)
524 gchar
*with_fragment
;
526 with_fragment
= g_strconcat (uri
, "#", fragment
, NULL
);
536 * anjuta_session_get_file_from_relative_uri:
537 * @session: an #AnjutaSession object
538 * @uri: a relative URI from a key
539 * @fragment: fragment part of the URI if existing, cal be NULL
541 * Return a GFile corresponding to the URI and and optional fragment,
542 * normally read from a session key.
543 * The path is expected to be relative to the session directory but it works
544 * with an absolute URI, in this case it returns the same file than
545 * g_file_new_for_uri.
546 * It is useful to keep only relative file paths in a session file to be able
547 * to copy the whole project without breaking references.
549 * Returns: A new GFile that has to be freed with g_object_unref().
552 anjuta_session_get_file_from_relative_uri (AnjutaSession
*session
,
554 const gchar
**fragment
)
559 scheme
=g_uri_parse_scheme (uri
);
563 file
= g_file_new_for_uri (uri
);
567 gchar
*parent_uri
= g_filename_to_uri (session
->priv
->dir_path
, NULL
, NULL
);
570 full_uri
= g_strconcat (parent_uri
, G_DIR_SEPARATOR_S
, uri
, NULL
);
571 file
= g_file_new_for_uri (full_uri
);
575 if (fragment
!= NULL
)
577 *fragment
= strchr (uri
, '#');
578 if (*fragment
!= NULL
) (*fragment
)++;