2010-11-15 22:25:06 +01:00
|
|
|
/*
|
2020-11-07 13:06:23 +01:00
|
|
|
** Copyright (C) 2010-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2010-11-15 22:25:06 +01:00
|
|
|
**
|
|
|
|
** This program 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 3, or (at your option) any
|
|
|
|
** later version.
|
|
|
|
**
|
|
|
|
** This program 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 program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib.h>
|
2020-11-07 13:06:23 +01:00
|
|
|
#include "mu-bookmarks.hh"
|
2010-11-15 22:25:06 +01:00
|
|
|
|
|
|
|
#define MU_BOOKMARK_GROUP "mu"
|
|
|
|
|
2020-11-07 13:06:23 +01:00
|
|
|
struct MuBookmarks {
|
2021-10-20 11:18:15 +02:00
|
|
|
char* _bmpath;
|
|
|
|
GHashTable* _hash;
|
2010-11-15 22:25:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2021-10-20 11:18:15 +02:00
|
|
|
fill_hash(GHashTable* hash, GKeyFile* kfile)
|
2010-11-15 22:25:06 +01:00
|
|
|
{
|
|
|
|
gchar **keys, **cur;
|
2012-09-05 00:23:00 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
keys = g_key_file_get_keys(kfile, MU_BOOKMARK_GROUP, NULL, NULL);
|
2010-11-15 22:25:06 +01:00
|
|
|
if (!keys)
|
|
|
|
return;
|
2012-09-05 00:23:00 +02:00
|
|
|
|
2010-11-15 22:25:06 +01:00
|
|
|
for (cur = keys; *cur; ++cur) {
|
2021-10-20 11:18:15 +02:00
|
|
|
gchar* val;
|
|
|
|
val = g_key_file_get_string(kfile, MU_BOOKMARK_GROUP, *cur, NULL);
|
2010-11-15 22:25:06 +01:00
|
|
|
if (val)
|
2021-10-20 11:18:15 +02:00
|
|
|
g_hash_table_insert(hash, *cur, val);
|
2010-11-15 22:25:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* don't use g_strfreev, because we put them in the hash table;
|
|
|
|
* only free the gchar** itself */
|
2021-10-20 11:18:15 +02:00
|
|
|
g_free(keys);
|
2010-11-15 22:25:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static GHashTable*
|
2021-10-20 11:18:15 +02:00
|
|
|
create_hash_from_key_file(const gchar* bmpath)
|
2010-11-15 22:25:06 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
GKeyFile* kfile;
|
|
|
|
GHashTable* hash;
|
2012-09-05 00:23:00 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
kfile = g_key_file_new();
|
2012-09-05 00:23:00 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!g_key_file_load_from_file(kfile, bmpath, G_KEY_FILE_NONE, NULL)) {
|
|
|
|
g_key_file_free(kfile);
|
2010-11-15 22:25:06 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
|
|
|
fill_hash(hash, kfile);
|
2010-11-15 22:25:06 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_key_file_free(kfile);
|
2010-11-15 22:25:06 +01:00
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2011-01-07 07:31:36 +01:00
|
|
|
MuBookmarks*
|
2021-10-20 11:18:15 +02:00
|
|
|
mu_bookmarks_new(const gchar* bmpath)
|
2010-11-15 22:25:06 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
MuBookmarks* bookmarks;
|
|
|
|
GHashTable* hash;
|
2012-09-05 00:23:00 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_return_val_if_fail(bmpath, NULL);
|
2010-11-15 22:25:06 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
hash = create_hash_from_key_file(bmpath);
|
2010-11-15 22:25:06 +01:00
|
|
|
if (!hash)
|
|
|
|
return NULL;
|
2012-09-05 00:23:00 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
bookmarks = g_new(MuBookmarks, 1);
|
2012-09-05 00:23:00 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
bookmarks->_bmpath = g_strdup(bmpath);
|
2010-11-15 22:25:06 +01:00
|
|
|
bookmarks->_hash = hash;
|
|
|
|
|
|
|
|
return bookmarks;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-10-20 11:18:15 +02:00
|
|
|
mu_bookmarks_destroy(MuBookmarks* bm)
|
2010-11-15 22:25:06 +01:00
|
|
|
{
|
|
|
|
if (!bm)
|
|
|
|
return;
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_free(bm->_bmpath);
|
|
|
|
g_hash_table_destroy(bm->_hash);
|
|
|
|
g_free(bm);
|
2010-11-15 22:25:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const gchar*
|
2021-10-20 11:18:15 +02:00
|
|
|
mu_bookmarks_lookup(MuBookmarks* bm, const gchar* name)
|
2010-11-15 22:25:06 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
g_return_val_if_fail(bm, NULL);
|
|
|
|
g_return_val_if_fail(name, NULL);
|
2010-11-15 22:25:06 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
return (const char*)g_hash_table_lookup(bm->_hash, name);
|
2010-11-15 22:25:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct _BMData {
|
|
|
|
MuBookmarksForeachFunc _func;
|
2021-10-20 11:18:15 +02:00
|
|
|
gpointer _user_data;
|
2010-11-15 22:25:06 +01:00
|
|
|
};
|
|
|
|
typedef struct _BMData BMData;
|
2012-09-05 00:23:00 +02:00
|
|
|
|
2010-11-15 22:25:06 +01:00
|
|
|
static void
|
2021-10-20 11:18:15 +02:00
|
|
|
each_bookmark(const gchar* key, const gchar* val, BMData* bmdata)
|
2010-11-15 22:25:06 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
bmdata->_func(key, val, bmdata->_user_data);
|
2010-11-15 22:25:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-10-20 11:18:15 +02:00
|
|
|
mu_bookmarks_foreach(MuBookmarks* bm, MuBookmarksForeachFunc func, gpointer user_data)
|
2010-11-15 22:25:06 +01:00
|
|
|
{
|
|
|
|
BMData bmdata;
|
2012-09-05 00:23:00 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_return_if_fail(bm);
|
|
|
|
g_return_if_fail(func);
|
2010-11-15 22:25:06 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
bmdata._func = func;
|
2010-11-15 22:25:06 +01:00
|
|
|
bmdata._user_data = user_data;
|
2012-09-05 00:23:00 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_hash_table_foreach(bm->_hash, (GHFunc)each_bookmark, &bmdata);
|
2010-11-15 22:25:06 +01:00
|
|
|
}
|