Commit 40721293 authored by Martyn Russell's avatar Martyn Russell

Moved config functions and modules around

Now we have tracker-config-manager which is used to do all file based
operations for the config file. We also have tracker-config-utils
which does all the GKeyFile<-->GObject work for us. 

Perhaps these should be renamed at some point to:

  tracker-config-file
  tracker-object-keyfile

To be more aligned to what they actually do.
parent f6024a8b
......@@ -1504,7 +1504,7 @@ Plugins:
Evolution plugin (data-push): $have_evolution_plugin
KMail plugin (data-push): $enable_kmail_push_module
Warning:
Warning:2
You must make sure SQLite is compiled with --enable-threadsafe
......
......@@ -57,6 +57,8 @@ libtracker_common_la_SOURCES = \
$(os_sources) \
$(power_sources) \
$(storage_sources) \
tracker-config-manager.c \
tracker-config-utils.c \
tracker-class.c \
tracker-dbus.c \
tracker-file-utils.c \
......@@ -88,6 +90,8 @@ noinst_HEADERS = \
tracker-albumart.h
libtracker_commoninclude_HEADERS = \
tracker-config-manager.h \
tracker-config-utils.h \
tracker-class.h \
tracker-common.h \
tracker-file-utils.h \
......
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2009, Nokia (urho.konttori@nokia.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include "tracker-config-manager.h"
#define TRACKER_CONFIG_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TRACKER_TYPE_CONFIG_MANAGER, TrackerConfigManagerPrivate))
typedef struct _TrackerConfigManagerPrivate TrackerConfigManagerPrivate;
struct _TrackerConfigManagerPrivate {
gchar *domain;
};
static void config_finalize (GObject *object);
static void config_load (TrackerConfigManager *config);
static gboolean config_save (TrackerConfigManager *config);
static void config_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec);
static void config_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
static void config_constructed (GObject *object);
enum {
PROP_0,
PROP_DOMAIN
};
G_DEFINE_TYPE (TrackerConfigManager, tracker_config_manager, G_TYPE_OBJECT);
static void
tracker_config_manager_class_init (TrackerConfigManagerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->get_property = config_get_property;
object_class->set_property = config_set_property;
object_class->finalize = config_finalize;
object_class->constructed = config_constructed;
g_object_class_install_property (object_class,
PROP_DOMAIN,
g_param_spec_string ("domain",
"Config domain",
"The prefix before .cfg for the filename",
g_get_application_name (),
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_type_class_add_private (object_class, sizeof (TrackerConfigManagerPrivate));
}
static void
tracker_config_manager_init (TrackerConfigManager *manager)
{
manager->key_file = g_key_file_new ();
}
static void
config_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
{
TrackerConfigManagerPrivate *priv;
priv = TRACKER_CONFIG_MANAGER_GET_PRIVATE (object);
switch (param_id) {
case PROP_DOMAIN:
g_value_set_string (value, priv->domain);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
};
}
static void
config_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
{
TrackerConfigManagerPrivate *priv;
priv = TRACKER_CONFIG_MANAGER_GET_PRIVATE (object);
switch (param_id) {
case PROP_DOMAIN:
g_free (priv->domain);
priv->domain = g_strdup (g_value_get_string (value));
g_object_notify (object, "domain");
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
};
}
static void
config_finalize (GObject *object)
{
TrackerConfigManager *manager;
TrackerConfigManagerPrivate *priv;
manager = TRACKER_CONFIG_MANAGER (object);
priv = TRACKER_CONFIG_MANAGER_GET_PRIVATE (manager);
if (manager->key_file) {
g_key_file_free (manager->key_file);
}
if (manager->monitor) {
g_object_unref (manager->monitor);
}
if (manager->file) {
g_object_unref (manager->file);
}
g_free (priv->domain);
(G_OBJECT_CLASS (tracker_config_manager_parent_class)->finalize) (object);
}
static void
config_constructed (GObject *object)
{
config_load (TRACKER_CONFIG_MANAGER (object));
}
static gchar *
config_dir_ensure_exists_and_return (void)
{
gchar *directory;
directory = g_build_filename (g_get_user_config_dir (),
"tracker",
NULL);
if (!g_file_test (directory, G_FILE_TEST_EXISTS)) {
g_print ("Creating config directory:'%s'\n", directory);
if (g_mkdir_with_parents (directory, 0700) == -1) {
g_critical ("Could not create configuration directory");
g_free (directory);
return NULL;
}
}
return directory;
}
static void
config_changed_cb (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
gpointer user_data)
{
TrackerConfigManager *manager;
gchar *filename;
manager = TRACKER_CONFIG_MANAGER (user_data);
/* Do we recreate if the file is deleted? */
switch (event_type) {
case G_FILE_MONITOR_EVENT_CHANGED:
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
manager->file_exists = TRUE;
filename = g_file_get_path (file);
g_message ("Config file changed:'%s', reloading settings...",
filename);
g_free (filename);
config_load (manager);
break;
case G_FILE_MONITOR_EVENT_DELETED:
manager->file_exists = FALSE;
break;
case G_FILE_MONITOR_EVENT_CREATED:
case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
case G_FILE_MONITOR_EVENT_UNMOUNTED:
manager->file_exists = TRUE;
default:
break;
}
}
static void
config_load (TrackerConfigManager *manager)
{
TrackerConfigManagerPrivate *priv;
GError *error = NULL;
gchar *basename;
gchar *filename;
gchar *directory;
/* Check we have a config file and if not, create it based on
* the default settings.
*/
directory = config_dir_ensure_exists_and_return ();
if (!directory) {
return;
}
priv = TRACKER_CONFIG_MANAGER_GET_PRIVATE (manager);
basename = g_strdup_printf ("%s.cfg", priv->domain);
filename = g_build_filename (directory, basename, NULL);
g_free (basename);
g_free (directory);
/* Add file monitoring for changes */
if (!manager->file) {
manager->file = g_file_new_for_path (filename);
}
if (!manager->monitor) {
g_message ("Setting up monitor for changes to config file:'%s'",
filename);
manager->monitor = g_file_monitor_file (manager->file,
G_FILE_MONITOR_NONE,
NULL,
NULL);
g_signal_connect (manager->monitor, "changed",
G_CALLBACK (config_changed_cb),
manager);
}
/* Load options */
g_key_file_load_from_file (manager->key_file,
filename,
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS,
&error);
/* We force an overwrite in cases of error */
manager->file_exists = error ? FALSE : TRUE;
if (error) {
g_error_free (error);
}
g_free (filename);
}
static gboolean
config_save (TrackerConfigManager *manager)
{
GError *error = NULL;
gchar *filename;
gchar *data;
gsize size;
if (!manager->key_file) {
g_critical ("Could not save config, GKeyFile was NULL, has the config been loaded?");
return FALSE;
}
g_message ("Setting details to GKeyFile object...");
/* FIXME: Get to GKeyFile from object properties */
g_message ("Saving config to disk...");
/* Do the actual saving to disk now */
data = g_key_file_to_data (manager->key_file, &size, &error);
if (error) {
g_warning ("Could not get config data to write to file, %s",
error->message);
g_error_free (error);
return FALSE;
}
filename = g_file_get_path (manager->file);
g_file_set_contents (filename, data, size, &error);
g_free (data);
if (error) {
g_warning ("Could not write %" G_GSIZE_FORMAT " bytes to file '%s', %s",
size,
filename,
error->message);
g_free (filename);
g_error_free (error);
return FALSE;
}
g_message ("Wrote config to '%s' (%" G_GSIZE_FORMAT " bytes)",
filename,
size);
g_free (filename);
return TRUE;
}
/**
* tracker_config_manager_new:
*
* Creates a new GObject for handling Tracker's config file.
*
* Return value: A new TrackerConfigManager object. Must be unreferenced when
* finished with.
*/
TrackerConfigManager *
tracker_config_manager_new (const gchar *domain)
{
TrackerConfigManager *config;
config = g_object_new (TRACKER_TYPE_CONFIG_MANAGER,
"domain", domain,
NULL);
return config;
}
/**
* tracker_config_manager_save:
* @config: a #TrackerConfigManager
*
* Writes the configuration stored in TrackerConfigManager to disk.
*
* Return value: %TRUE on success, %FALSE otherwise.
*/
gboolean
tracker_config_manager_save (TrackerConfigManager *config)
{
g_return_val_if_fail (TRACKER_IS_CONFIG_MANAGER (config), FALSE);
return config_save (config);
}
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2009 Nokia
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __TRACKER_CONFIG_MANAGER_H__
#define __TRACKER_CONFIG_MANAGER_H__
#include <glib-object.h>
#include <gio/gio.h>
G_BEGIN_DECLS
#define TRACKER_TYPE_CONFIG_MANAGER (tracker_config_manager_get_type ())
#define TRACKER_CONFIG_MANAGER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), TRACKER_TYPE_CONFIG_MANAGER, TrackerConfigManager))
#define TRACKER_CONFIG_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), TRACKER_TYPE_CONFIG_MANAGER, TrackerConfigManagerClass))
#define TRACKER_IS_CONFIG_MANAGER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), TRACKER_TYPE_CONFIG_MANAGER))
#define TRACKER_IS_CONFIG_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), TRACKER_TYPE_CONFIG_MANAGER))
#define TRACKER_CONFIG_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), TRACKER_TYPE_CONFIG_MANAGER, TrackerConfigManagerClass))
typedef struct _TrackerConfigManager TrackerConfigManager;
typedef struct _TrackerConfigManagerClass TrackerConfigManagerClass;
struct _TrackerConfigManager {
GObject parent;
GFile *file;
GFileMonitor *monitor;
gboolean file_exists;
GKeyFile *key_file;
};
struct _TrackerConfigManagerClass {
GObjectClass parent_class;
};
GType tracker_config_manager_get_type (void) G_GNUC_CONST;
TrackerConfigManager *tracker_config_manager_new (const gchar *domain);
gboolean tracker_config_manager_save (TrackerConfigManager *config);
const gchar * tracker_config_manager_get_property_blurb (TrackerConfigManager *config,
const gchar *property);
gboolean tracker_config_manager_boolean_default (TrackerConfigManager *config,
const gchar *property);
gint tracker_config_manager_int_default (TrackerConfigManager *config,
const gchar *property);
gboolean tracker_config_manager_int_validate (TrackerConfigManager *config,
const gchar *property,
gint value);
GSList * tracker_config_manager_string_list_to_gslist (const gchar **value,
gboolean is_directory_list);
void tracker_config_manager_load_int (TrackerConfigManager *config,
const gchar *property,
GKeyFile *key_file,
const gchar *group,
const gchar *key);
void tracker_config_manager_load_boolean (TrackerConfigManager *config,
const gchar *property,
GKeyFile *key_file,
const gchar *group,
const gchar *key);
void tracker_config_manager_load_string (TrackerConfigManager *config,
const gchar *property,
GKeyFile *key_file,
const gchar *group,
const gchar *key);
void tracker_config_manager_load_string_list (TrackerConfigManager *config,
const gchar *property,
GKeyFile *key_file,
const gchar *group,
const gchar *key);
void tracker_config_manager_save_int (TrackerConfigManager *config,
const gchar *property,
GKeyFile *key_file,
const gchar *group,
const gchar *key);
void tracker_config_manager_save_boolean (TrackerConfigManager *config,
const gchar *property,
GKeyFile *key_file,
const gchar *group,
const gchar *key);
void tracker_config_manager_save_string (TrackerConfigManager *config,
const gchar *property,
GKeyFile *key_file,
const gchar *group,
const gchar *key);
void tracker_config_manager_save_string_list (TrackerConfigManager *config,
const gchar *property,
GKeyFile *key_file,
const gchar *group,
const gchar *key);
G_END_DECLS
#endif /* __TRACKER_CONFIG_MANAGER_H__ */
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2009, Nokia (urho.konttori@nokia.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <libtracker-common/tracker-file-utils.h>
#include <libtracker-common/tracker-type-utils.h>
#include "tracker-config-utils.h"
static GSList *
config_string_list_to_gslist (const gchar **value,
gboolean is_directory_list)
{
GSList *list = NULL;
gint i;
if (!value) {
return NULL;
}
for (i = 0; value[i]; i++) {
const gchar *str;
gchar *validated;
str = value[i];
if (!str || str[0] == '\0') {
continue;
}
if (!is_directory_list) {
list = g_slist_prepend (list, g_strdup (str));
continue;
}
/* For directories we validate any special characters,
* for example '~' and '../../'
*/
validated = tracker_path_evaluate_name (str);
if (validated) {
list = g_slist_prepend (list, validated);
}
}
return g_slist_reverse (list);
}
const gchar *
tracker_config_blurb (gpointer config,
const gchar *property)
{
GObjectClass *klass;
GParamSpec *spec;
g_return_val_if_fail (G_IS_OBJECT (config), NULL);
g_return_val_if_fail (property != NULL, NULL);
klass = G_OBJECT_GET_CLASS (config);
spec = g_object_class_find_property (G_OBJECT_CLASS (klass), property);
g_return_val_if_fail (spec != NULL, NULL);
return g_param_spec_get_blurb (spec);
}
gboolean
tracker_config_default_boolean (gpointer config,
const gchar *property)
{
GObjectClass *klass;
GParamSpec *spec;
GParamSpecBoolean *bspec;
g_return_val_if_fail (G_IS_OBJECT (config), FALSE);
g_return_val_if_fail (property != NULL, FALSE);
klass = G_OBJECT_GET_CLASS (config);
spec = g_object_class_find_property (G_OBJECT_CLASS (klass), property);
g_return_val_if_fail (spec != NULL, FALSE);