Commit 9af5d619 authored by Carlos Garnacho's avatar Carlos Garnacho Committed by Martyn Russell

tracker-extract: Use GSettings for configuration storage.

parent 63465689
......@@ -20,32 +20,11 @@
#include "config.h"
#include <libtracker-common/tracker-keyfile-object.h>
#include <libtracker-common/tracker-enum-types.h>
#include <libtracker-common/tracker-enums.h>
#include "tracker-config.h"
#define TRACKER_CONFIG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TRACKER_TYPE_CONFIG, TrackerConfigPrivate))
/* GKeyFile defines */
#define GROUP_GENERAL "General"
/* Default values */
#define DEFAULT_VERBOSITY 0
#define DEFAULT_MAX_BYTES 1048576 /* 1Mbyte */
#define ABSOLUTE_MAX_BYTES 10485760 /* 10 Mbytes (GB#616845) */
typedef struct {
/* General */
gint verbosity;
gint max_bytes;
} TrackerConfigPrivate;
typedef struct {
GType type;
const gchar *property;
const gchar *group;
const gchar *key;
} ObjectToKeyFile;
static void config_set_property (GObject *object,
guint param_id,
const GValue *value,
......@@ -56,26 +35,20 @@ static void config_get_property (GObject *object,
GParamSpec *pspec);
static void config_finalize (GObject *object);
static void config_constructed (GObject *object);
static void config_load (TrackerConfig *config);
static gboolean config_save (TrackerConfig *config);
static void config_create_with_defaults (TrackerConfig *config,
GKeyFile *key_file,
gboolean overwrite);
enum {
PROP_0,
/* General */
PROP_VERBOSITY,
PROP_MAX_BYTES
};
static ObjectToKeyFile conversions[] = {
{ G_TYPE_INT, "verbosity", GROUP_GENERAL, "Verbosity" },
{ G_TYPE_INT, "max-bytes", GROUP_GENERAL, "MaxBytes" },
static TrackerConfigMigrationEntry migration[] = {
{ G_TYPE_ENUM, "General", "Verbosity", "verbosity" },
{ G_TYPE_INT, "General", "MaxBytes", "max-bytes" },
{ 0 }
};
G_DEFINE_TYPE (TrackerConfig, tracker_config, TRACKER_TYPE_CONFIG_FILE);
G_DEFINE_TYPE (TrackerConfig, tracker_config, G_TYPE_SETTINGS);
static void
tracker_config_class_init (TrackerConfigClass *klass)
......@@ -90,25 +63,21 @@ tracker_config_class_init (TrackerConfigClass *klass)
/* General */
g_object_class_install_property (object_class,
PROP_VERBOSITY,
g_param_spec_int ("verbosity",
"Log verbosity",
" Log verbosity (0=errors, 1=minimal, 2=detailed, 3=debug)",
0,
3,
DEFAULT_VERBOSITY,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_param_spec_enum ("verbosity",
"Log verbosity",
"Log verbosity (0=errors, 1=minimal, 2=detailed, 3=debug)",
TRACKER_TYPE_VERBOSITY,
TRACKER_VERBOSITY_ERRORS,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_MAX_BYTES,
g_param_spec_int ("max-bytes",
"Max Bytes",
" Maximum number of UTF-8 bytes to extract per file [0->10485760]",
0,
ABSOLUTE_MAX_BYTES,
DEFAULT_MAX_BYTES,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_type_class_add_private (object_class, sizeof (TrackerConfigPrivate));
"Maximum number of UTF-8 bytes to extract per file [0->10485760]",
0, 1024 * 1024 * 10,
1024 * 1024,
G_PARAM_READWRITE));
}
static void
......@@ -123,15 +92,14 @@ config_set_property (GObject *object,
GParamSpec *pspec)
{
switch (param_id) {
/* General */
case PROP_VERBOSITY:
tracker_config_set_verbosity (TRACKER_CONFIG (object),
g_value_get_int (value));
g_settings_set_enum (G_SETTINGS (object), "verbosity",
g_value_get_enum (value));
break;
case PROP_MAX_BYTES:
tracker_config_set_max_bytes (TRACKER_CONFIG (object),
g_value_get_int (value));
g_settings_set_int (G_SETTINGS (object), "max-bytes",
g_value_get_int (value));
break;
default:
......@@ -146,18 +114,15 @@ config_get_property (GObject *object,
GValue *value,
GParamSpec *pspec)
{
TrackerConfigPrivate *priv;
priv = TRACKER_CONFIG_GET_PRIVATE (object);
switch (param_id) {
/* General */
case PROP_VERBOSITY:
g_value_set_int (value, priv->verbosity);
g_value_set_enum (value,
g_settings_get_enum (G_SETTINGS (object), "verbosity"));
break;
case PROP_MAX_BYTES:
g_value_set_int (value, priv->max_bytes);
g_value_set_int (value,
g_settings_get_int (G_SETTINGS (object), "max-bytes"));
break;
default:
......@@ -179,196 +144,67 @@ config_finalize (GObject *object)
static void
config_constructed (GObject *object)
{
(G_OBJECT_CLASS (tracker_config_parent_class)->constructed) (object);
config_load (TRACKER_CONFIG (object));
}
TrackerConfigFile *config_file;
static void
config_create_with_defaults (TrackerConfig *config,
GKeyFile *key_file,
gboolean overwrite)
{
gint i;
g_message ("Loading defaults into GKeyFile...");
for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
gboolean has_key;
has_key = g_key_file_has_key (key_file,
conversions[i].group,
conversions[i].key,
NULL);
if (!overwrite && has_key) {
continue;
}
switch (conversions[i].type) {
case G_TYPE_INT:
g_key_file_set_integer (key_file,
conversions[i].group,
conversions[i].key,
tracker_keyfile_object_default_int (config,
conversions[i].property));
break;
default:
g_assert_not_reached ();
}
g_key_file_set_comment (key_file,
conversions[i].group,
conversions[i].key,
tracker_keyfile_object_blurb (config,
conversions[i].property),
NULL);
}
}
static void
config_load (TrackerConfig *config)
{
TrackerConfigFile *file;
gint i;
file = TRACKER_CONFIG_FILE (config);
config_create_with_defaults (config, file->key_file, FALSE);
if (!file->file_exists) {
tracker_config_file_save (file);
}
for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
gboolean has_key;
has_key = g_key_file_has_key (file->key_file,
conversions[i].group,
conversions[i].key,
NULL);
switch (conversions[i].type) {
case G_TYPE_INT:
tracker_keyfile_object_load_int (G_OBJECT (file),
conversions[i].property,
file->key_file,
conversions[i].group,
conversions[i].key);
break;
default:
g_assert_not_reached ();
break;
}
}
}
static gboolean
config_save (TrackerConfig *config)
{
TrackerConfigFile *file;
gint i;
(G_OBJECT_CLASS (tracker_config_parent_class)->constructed) (object);
file = TRACKER_CONFIG_FILE (config);
g_settings_delay (G_SETTINGS (object));
if (!file->key_file) {
g_critical ("Could not save config, GKeyFile was NULL, has the config been loaded?");
/* Migrate keyfile-based configuration */
config_file = tracker_config_file_new ();
return FALSE;
if (config_file) {
tracker_config_file_migrate (config_file, G_SETTINGS (object), migration);
g_object_unref (config_file);
}
g_message ("Setting details to GKeyFile object...");
for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
switch (conversions[i].type) {
case G_TYPE_INT:
tracker_keyfile_object_save_int (file,
conversions[i].property,
file->key_file,
conversions[i].group,
conversions[i].key);
break;
default:
g_assert_not_reached ();
break;
}
}
return tracker_config_file_save (file);
}
TrackerConfig *
tracker_config_new (void)
{
return g_object_new (TRACKER_TYPE_CONFIG, NULL);
}
gboolean
tracker_config_save (TrackerConfig *config)
{
g_return_val_if_fail (TRACKER_IS_CONFIG (config), FALSE);
return config_save (config);
return g_object_new (TRACKER_TYPE_CONFIG,
"schema", "org.freedesktop.Tracker.Extractor",
"path", "/org/freedesktop/tracker/extractor/",
NULL);
}
gint
tracker_config_get_verbosity (TrackerConfig *config)
{
TrackerConfigPrivate *priv;
g_return_val_if_fail (TRACKER_IS_CONFIG (config), DEFAULT_VERBOSITY);
gint verbosity;
priv = TRACKER_CONFIG_GET_PRIVATE (config);
g_return_val_if_fail (TRACKER_IS_CONFIG (config), TRACKER_VERBOSITY_ERRORS);
return priv->verbosity;
g_object_get (config, "verbosity", &verbosity, NULL);
return verbosity;
}
void
tracker_config_set_verbosity (TrackerConfig *config,
gint value)
{
TrackerConfigPrivate *priv;
g_return_if_fail (TRACKER_IS_CONFIG (config));
if (!tracker_keyfile_object_validate_int (config, "verbosity", value)) {
return;
}
priv = TRACKER_CONFIG_GET_PRIVATE (config);
priv->verbosity = value;
g_object_notify (G_OBJECT (config), "verbosity");
g_object_set (G_OBJECT (config), "verbosity", value, NULL);
}
gint
tracker_config_get_max_bytes (TrackerConfig *config)
{
TrackerConfigPrivate *priv;
g_return_val_if_fail (TRACKER_IS_CONFIG (config), DEFAULT_MAX_BYTES);
gint max_bytes;
priv = TRACKER_CONFIG_GET_PRIVATE (config);
g_return_val_if_fail (TRACKER_IS_CONFIG (config), 0);
return priv->max_bytes;
g_object_get (config, "max-bytes", &max_bytes, NULL);
return max_bytes;
}
void
tracker_config_set_max_bytes (TrackerConfig *config,
gint value)
{
TrackerConfigPrivate *priv;
g_return_if_fail (TRACKER_IS_CONFIG (config));
if (!tracker_keyfile_object_validate_int (config, "max-bytes", value)) {
return;
}
priv = TRACKER_CONFIG_GET_PRIVATE (config);
priv->max_bytes = value;
g_object_notify (G_OBJECT (config), "max-bytes");
g_object_set (G_OBJECT (config), "max-bytes", value, NULL);
}
......@@ -37,17 +37,17 @@ typedef struct TrackerConfig TrackerConfig;
typedef struct TrackerConfigClass TrackerConfigClass;
struct TrackerConfig {
TrackerConfigFile parent;
GSettings parent;
gpointer priv;
};
struct TrackerConfigClass {
TrackerConfigFileClass parent_class;
GSettingsClass parent_class;
};
GType tracker_config_get_type (void) G_GNUC_CONST;
TrackerConfig *tracker_config_new (void);
gboolean tracker_config_save (TrackerConfig *config);
gint tracker_config_get_verbosity (TrackerConfig *config);
gint tracker_config_get_max_bytes (TrackerConfig *config);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment