static gboolean zak_bks_db_plugin_initialize (ZakBksPluggable *pluggable, const gchar *cnc_string);
-static GdaDataModel *zak_bks_db_plugin_path_get_data_model (ZakBksPluggable *pluggable, const gchar *path);
-static gchar *zak_bks_db_plugin_path_get_value_from_db (ZakBksPluggable *pluggable, const gchar *path);
-static void zak_bks_db_plugin_get_children (ZakBksPluggable *pluggable, GNode *parentNode, gint idParent, gchar *path);
-
#define ZAK_BKS_DB_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_BKS_TYPE_DB_PLUGIN, ZakBksDBPluginPrivate))
typedef struct _ZakBksDBPluginPrivate ZakBksDBPluginPrivate;
gchar *cnc_string;
GdaEx *gdaex;
-
- gint id_bksg;
- gchar *name;
- gchar *description;
- gchar *root;
-
- gchar chrquot;
};
G_DEFINE_DYNAMIC_TYPE_EXTENDED (ZakBksDBPlugin,
const GValue *value,
GParamSpec *pspec)
{
- gchar *sql;
-
ZakBksDBPlugin *plugin = ZAK_BKS_DB_PLUGIN (object);
ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (plugin);
ZakBksDBPlugin *plugin = ZAK_BKS_DB_PLUGIN (pluggable);
ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (plugin);
- GString *gstr_cnc_string;
- gchar **strs;
- guint i;
- guint l;
-
- gchar *sql;
- GdaDataModel *dm;
-
- gchar *cnc_string_;
-
- cnc_string_ = g_strdup_printf ("%s;", cnc_string);
- strs = g_strsplit (cnc_string_, ";", -1);
- g_free (cnc_string_);
-
- gstr_cnc_string = g_string_new ("");
-
- l = g_strv_length (strs);
- for (i = 0; i < l; i++)
- {
- if (g_str_has_prefix (strs[i], "BKS_NAME="))
- {
- priv->name = g_strdup (strs[i] + strlen ("BKS_NAME="));
- if (priv->name[strlen (priv->name)] == ';')
- {
- priv->name[strlen (priv->name)] = '\0';
- }
- }
- else
- {
- g_string_append (gstr_cnc_string, strs[i]);
- g_string_append (gstr_cnc_string, ";");
- }
- }
- if (priv->cnc_string != NULL)
- {
- g_free (priv->cnc_string);
- }
- priv->cnc_string = g_strdup (gstr_cnc_string->str);
-
- g_string_free (gstr_cnc_string, TRUE);
- g_strfreev (strs);
-
- if (priv->name == NULL)
- {
- priv->name = g_strdup ("Default");
- }
+ priv->cnc_string = g_strdup (cnc_string);
priv->gdaex = gdaex_new_from_string (priv->cnc_string);
- priv->chrquot = gdaex_get_chr_quoting (priv->gdaex);
-
- /* check if bksg exists */
- sql = g_strdup_printf ("SELECT id, name"
- " FROM bksgs"
- " WHERE name = '%s'",
- gdaex_strescape (priv->name, NULL));
- dm = gdaex_query (priv->gdaex, sql);
- g_free (sql);
- if (dm != NULL || gda_data_model_get_n_rows (dm) > 0)
- {
- priv->id_bksg = gdaex_data_model_get_value_integer_at (dm, 0, 0);
- }
- if (dm != NULL)
- {
- g_object_unref (dm);
- }
-
- return (priv->gdaex != NULL && priv->name != NULL ? TRUE : FALSE);
-}
-
-static GdaDataModel
-*zak_bks_db_plugin_path_get_data_model (ZakBksPluggable *pluggable, const gchar *path)
-{
- gchar **tokens;
- gchar *sql;
- gchar *token;
- guint i;
- guint id_parent;
- GdaDataModel *dm;
-
- ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable);
-
- if (path == NULL) return NULL;
-
- dm = NULL;
-
- tokens = g_strsplit (path, "/", 0);
- if (tokens == NULL) return NULL;
-
- i = 0;
- id_parent = 0;
- while (tokens[i] != NULL)
- {
- token = g_strstrip (g_strdup (tokens[i]));
- if (strcmp (token, "") != 0)
- {
- sql = g_strdup_printf ("SELECT *"
- " FROM %cvalues%c"
- " WHERE id_bksgs = %d"
- " AND id_parent = %d"
- " AND %ckey%c = '%s'",
- priv->chrquot, priv->chrquot,
- priv->id_bksg,
- id_parent,
- priv->chrquot, priv->chrquot,
- gdaex_strescape (token, NULL));
- dm = gdaex_query (priv->gdaex, sql);
- g_free (sql);
- if (dm == NULL || gda_data_model_get_n_rows (dm) != 1)
- {
- /* TO DO */
- g_warning ("Unable to find key «%s».", token);
- if (dm != NULL)
- {
- g_object_unref (dm);
- dm = NULL;
- }
- break;
- }
- id_parent = gdaex_data_model_get_field_value_integer_at (dm, 0, "id");
- }
- g_free (token);
-
- i++;
- }
- g_strfreev (tokens);
-
- return dm;
-}
-
-static gchar
-*zak_bks_db_plugin_path_get_value_from_db (ZakBksPluggable *pluggable, const gchar *path)
-{
- gchar *ret;
- GdaDataModel *dm;
-
- ret = NULL;
-
- dm = zak_bks_db_plugin_path_get_data_model (pluggable, path);
- if (dm != NULL)
- {
- ret = gdaex_data_model_get_field_value_stringify_at (dm, 0, "value");
- g_object_unref (dm);
- }
- return ret;
-}
-
-static void
-zak_bks_db_plugin_get_children (ZakBksPluggable *pluggable, GNode *parentNode, gint idParent, gchar *path)
-{
- gchar *sql;
- GdaDataModel *dm;
-
- ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable);
-
- sql = g_strdup_printf ("SELECT *"
- " FROM %cvalues%c"
- " WHERE id_bksgs = %d"
- " AND id_parent = %d",
- priv->chrquot, priv->chrquot,
- priv->id_bksg,
- idParent);
-
- dm = gdaex_query (priv->gdaex, sql);
- g_free (sql);
- if (dm != NULL)
- {
- guint i;
- guint rows;
-
- rows = gda_data_model_get_n_rows (dm);
- for (i = 0; i < rows; i++)
- {
- GNode *newNode;
- /* ZakBksKey *ck = g_new0 (ZakBksKey, 1); */
-
- /* ck->bksg = g_strdup (priv->name); */
- /* ck->key = g_strdup (gdaex_data_model_get_field_value_stringify_at (dm, i, "key")); */
- /* ck->value = g_strdup (gdaex_data_model_get_field_value_stringify_at (dm, i, "value")); */
- /* ck->description = g_strdup (gdaex_data_model_get_field_value_stringify_at (dm, i, "description")); */
- /* ck->path = g_strdup (path); */
-
- /* newNode = g_node_append_data (parentNode, ck); */
-
- /* zak_bks_db_plugin_get_children (pluggable, */
- /* newNode, */
- /* gdaex_data_model_get_field_value_integer_at (dm, i, "id"), */
- /* g_strconcat (path, (g_strcmp0 (path, "") == 0 ? "" : "/"), ck->key, NULL)); */
- }
- g_object_unref (dm);
- }
+ return (priv->gdaex != NULL ? TRUE : FALSE);
}
static GList
-*zak_bks_db_plugin_get_bksgs_list (ZakBksPluggable *pluggable,
- const gchar *filter)
-{
- GList *lst;
-
- GdaDataModel *dmZakBksgs;
-
- gchar *sql;
- gchar *where;
-
- guint row;
- guint rows;
-
- ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable);
-
- lst = NULL;
-
- if (priv->gdaex == NULL)
- {
- return NULL;
- }
-
- if (filter != NULL && strcmp (g_strstrip (g_strdup (filter)), "") != 0)
- {
- where = g_strdup_printf (" WHERE name LIKE '%s'", filter);
- }
- else
- {
- where = g_strdup ("");
- }
-
- sql = g_strdup_printf ("SELECT * FROM bksgs%s", where);
- g_free (where);
-
- dmZakBksgs = gdaex_query (priv->gdaex, sql);
- g_free (sql);
- if (dmZakBksgs != NULL)
- {
- rows = gda_data_model_get_n_rows (dmZakBksgs);
- if (rows > 0)
- {
- for (row = 0; row < rows; row++)
- {
- /* ZakBksBks *bks; */
- /* bks = g_new0 (ZakBksBks, 1); */
- /* bks->name = gdaex_data_model_get_field_value_stringify_at (dmZakBksgs, row, "name"); */
- /* bks->description = gdaex_data_model_get_field_value_stringify_at (dmZakBksgs, row, "description"); */
- /* lst = g_list_append (lst, bks); */
- }
- }
- else
- {
- lst = g_list_append (lst, NULL);
- }
- }
-
- return lst;
-}
-
-static gchar
-*zak_bks_db_plugin_path_get_value (ZakBksPluggable *pluggable, const gchar *path)
-{
- gchar *ret;
- gchar *path_;
-
- ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable);
-
- ret = NULL;
-
- if (path_ == NULL)
- {
- return NULL;
- }
-
- ret = zak_bks_db_plugin_path_get_value_from_db (pluggable, path_);
-
- return ret;
-}
-
-static gboolean
-zak_bks_db_plugin_path_set_value (ZakBksPluggable *pluggable, const gchar *path, const gchar *value)
-{
- GdaDataModel *dm;
- gchar *sql;
- gboolean ret;
-
- dm = zak_bks_db_plugin_path_get_data_model (pluggable, path);
-
- ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable);
-
- ret = FALSE;
- if (dm != NULL && gda_data_model_get_n_rows (dm) > 0)
- {
- sql = g_strdup_printf ("UPDATE %cvalues%c"
- " SET value = '%s'"
- " WHERE id_bksgs = %d"
- " AND id = %d",
- priv->chrquot, priv->chrquot,
- gdaex_strescape (value, NULL),
- priv->id_bksg,
- gdaex_data_model_get_field_value_integer_at (dm, 0, "id"));
- ret = (gdaex_execute (priv->gdaex, sql) >= 0);
- g_free (sql);
- }
- else
- {
- g_warning ("Path %s doesn't exists.", path);
- }
- if (dm != NULL)
- {
- g_object_unref (dm);
- }
-
- return ret;
-}
-
-GNode
-*zak_bks_db_plugin_get_tree (ZakBksPluggable *pluggable)
-{
- gchar *path;
- GNode *node;
-
- ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable);
-
- path = g_strdup ("");
-
- /* ZakBksKey *ck = g_new0 (ZakBksKey, 1); */
-
- /* ck->bksg = g_strdup (priv->name); */
- /* ck->path = ""; */
- /* ck->key = g_strdup ("/"); */
- /* ck->value = ""; */
- /* ck->description = ""; */
-
- /* node = g_node_new (ck); */
-
- zak_bks_db_plugin_get_children (pluggable, node, 0, path);
-
- return node;
-}
-
-static ZakBksBks
-*zak_bks_db_plugin_add_bksg (ZakBksPluggable *pluggable, const gchar *name, const gchar *description)
-{
- ZakBksBks *cc;
-
- gchar *sql;
- gint id;
-
- ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable);
-
- cc = NULL;
-
- id = gdaex_get_new_id (priv->gdaex, "bksgs", "id", NULL);
-
- sql = g_strdup_printf ("INSERT INTO bksgs"
- " VALUES (%d, '%s', '%s')",
- id,
- gdaex_strescape (name, NULL),
- gdaex_strescape (description, NULL));
- if (gdaex_execute (priv->gdaex, sql) > 0)
- {
- /* cc = g_new0 (ZakBksBks, 1); */
- /* cc->name = g_strdup (name); */
- /* cc->description = g_strdup (description); */
- }
-
- g_free (sql);
-
- return cc;
-}
-
-static gboolean
-zak_bks_db_plugin_set_bksg (ZakBksPluggable *pluggable,
- const gchar *name,
- const gchar *description)
-{
- gboolean ret;
- gchar *sql;
-
- ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable);
-
- g_return_val_if_fail (name != NULL, FALSE);
-
- ret = TRUE;
-
- sql = g_strdup_printf ("UPDATE bksgs"
- " SET name = '%s'"
- " WHERE id = %d",
- gdaex_strescape (name, NULL),
- priv->id_bksg);
- if (gdaex_execute (priv->gdaex, sql) < 1)
- {
- ret = FALSE;
- }
- g_free (sql);
- priv->name = g_strdup (name);
-
- if (description != NULL)
- {
- sql = g_strdup_printf ("UPDATE bksgs"
- " SET description = '%s'"
- " WHERE id = %d",
- gdaex_strescape (description, NULL),
- priv->id_bksg);
- if (gdaex_execute (priv->gdaex, sql) < 1)
- {
- ret = FALSE;
- }
- g_free (sql);
- priv->description = g_strdup (description);
- }
-
- return ret;
-}
-
-static gboolean
-zak_bks_db_plugin_delete_id_from_db_values (ZakBksPluggable *pluggable, gint id)
+*zak_bks_db_plugin_get_bookmarks (ZakBksPluggable *pluggable)
{
- gboolean ret;
- gchar *sql;
-
- ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable);
-
- sql = g_strdup_printf ("DELETE FROM %cvalues%c"
- " WHERE id_bksgs = %d"
- " AND id = %d",
- priv->chrquot, priv->chrquot,
- priv->id_bksg,
- id);
- if (gdaex_execute (priv->gdaex, sql) >= 0)
- {
- ret = TRUE;
- }
- else
- {
- ret = FALSE;
- }
- g_free (sql);
-
- return ret;
}
-static gboolean
-zak_bks_db_plugin_remove_path (ZakBksPluggable *pluggable, const gchar *path)
+static void
+zak_bks_db_plugin_add_bookmark (ZakBksPluggable *pluggable, ZakBksBookmark *bk)
{
- gboolean ret = FALSE;
- GdaDataModel *dm;
-
- ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable);
-
- dm = zak_bks_db_plugin_path_get_data_model (pluggable, path);
-
- if (dm != NULL && gda_data_model_get_n_rows (dm) > 0)
- {
- gchar *path_ = g_strdup (path);
-
- /* removing every child key */
- GNode *node;
- gint id = gdaex_data_model_get_field_value_integer_at (dm, 0, "id");
-
- /* ZakBksKey *ck = g_new0 (ZakBksKey, 1); */
-
- /* ck->bksg = g_strdup (priv->name); */
- /* ck->path = path_; */
- /* ck->key = gdaex_data_model_get_field_value_stringify_at (dm, 0, "key"); */
- /* ck->value = ""; */
- /* ck->description = ""; */
-
- /* node = g_node_new (ck); */
- zak_bks_db_plugin_get_children (pluggable, node, id, path_);
-
- /* g_node_traverse (node, G_POST_ORDER, G_TRAVERSE_ALL, -1, zak_bks_db_plugin_remove_path_traverse_func, (gpointer)pluggable); */
-
- /* removing the path */
- ret = zak_bks_db_plugin_delete_id_from_db_values (pluggable, id);
- }
- else
- {
- g_warning ("Path %s doesn't exists.", path);
- }
- if (dm != NULL)
- {
- g_object_unref (dm);
- }
- return ret;
}
-static gboolean
-zak_bks_db_plugin_remove (ZakBksPluggable *pluggable)
+static void
+zak_bks_db_plugin_remove_bookmark (ZakBksPluggable *pluggable, ZakBksBookmark *bk)
{
- gboolean ret;
- gchar *sql;
-
- ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable);
-
- ret = TRUE;
- sql = g_strdup_printf ("DELETE FROM %cvalues%c WHERE id_bksgs = %d",
- priv->chrquot,
- priv->chrquot,
- priv->id_bksg);
- if (gdaex_execute (priv->gdaex, sql) == -1)
- {
- g_free (sql);
- ret = FALSE;
- }
- else
- {
- g_free (sql);
- sql = g_strdup_printf ("DELETE FROM bksgs WHERE id = %d",
- priv->id_bksg);
- if (gdaex_execute (priv->gdaex, sql) == -1)
- {
- ret = FALSE;
- }
- g_free (sql);
- }
- return ret;
}
static void
zak_bks_pluggable_iface_init (ZakBksPluggableInterface *iface)
{
iface->initialize = zak_bks_db_plugin_initialize;
+ iface->get_bookmarks = zak_bks_db_plugin_get_bookmarks;
+ iface->add_bookmark = zak_bks_db_plugin_add_bookmark;
+ iface->remove_bookmark = zak_bks_db_plugin_remove_bookmark;
}
static void