File: filter-utils.c

package info (click to toggle)
sylfilter 0.8-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,124 kB
  • sloc: ansic: 12,806; sh: 8,910; makefile: 349
file content (196 lines) | stat: -rw-r--r-- 3,531 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* SylFilter - a message filter
 *
 * Copyright (C) 2011-2012 Hiroyuki Yamamoto
 * Copyright (C) 2011-2012 Sylpheed Development Team
 */

#include "config.h"

#include <glib.h>

#ifdef BUILTIN_LIBSYLPH
#  include "libsylph/utils.h"
#else
#  include <sylph/utils.h>
#endif

#ifdef G_OS_WIN32
#  include <windows.h>
#  include <wchar.h>
#  include <shlobj.h>
#endif

#include "filter.h"
#include "filter-utils.h"


char *xfilter_utils_get_file_contents(const char *file)
{
	char *contents = NULL;

	g_return_val_if_fail(file != NULL, NULL);

	if (g_file_get_contents(file, &contents, NULL, NULL))
		return contents;
	else
		return NULL;
}

void xfilter_free_mem(void *mem)
{
	g_free(mem);
}

const char *xfilter_utils_get_home_dir(void)
{
	return get_home_dir();
}

static char *base_dir = NULL;

const char *xfilter_utils_get_base_dir(void)
{
	if (!base_dir) {
		base_dir = g_strdup(xfilter_utils_get_default_base_dir());
	}

	return base_dir;
}

int xfilter_utils_set_base_dir(const char *path)
{
	const char *tmpdir;
	const char *rcdir;

#ifdef G_OS_WIN32
	if (path) {
		char *upath = g_locale_to_utf8(path, -1, NULL, NULL, NULL);
		if (xfilter_utils_mkdir(upath) < 0) {
			g_free(upath);
			return -1;
		}

		if (base_dir)
			g_free(base_dir);
		base_dir = upath;
	} else {
		path = xfilter_utils_get_default_base_dir();

		if (xfilter_utils_mkdir(path) < 0)
			return -1;

		if (base_dir)
			g_free(base_dir);
		base_dir = g_strdup(path);
	}
#else
	if (!path)
		path = xfilter_utils_get_default_base_dir();

	if (xfilter_utils_mkdir(path) < 0)
		return -1;

	if (base_dir)
		g_free(base_dir);
	base_dir = g_strdup(path);
#endif

	if (xfilter_get_app_mode() == XF_APP_MODE_STANDALONE) {
		set_rc_dir(base_dir);
	} else {
		/* if default rc dir not exist, use sylfilter base dir */
		rcdir = get_rc_dir();
		if (!is_dir_exist(rcdir)) {
			set_rc_dir(base_dir);
		}
	}

	tmpdir = get_tmp_dir();
	xfilter_utils_mkdir(tmpdir);

	return 0;
}

static const char *xfilter_utils_get_appdata_dir(void)
{
#ifdef G_OS_WIN32
	static char *appdata_dir = NULL;

	if (appdata_dir)
		return appdata_dir;
	else {
		HRESULT hr;
		wchar_t path[MAX_PATH + 1];

		hr = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, path);
		if (hr == S_OK)
			appdata_dir = g_utf16_to_utf8(path, -1, NULL, NULL, NULL);
		else
			return g_get_home_dir();
	}

	return appdata_dir;
#else
	return g_get_home_dir();
#endif
}

const char *xfilter_utils_get_default_base_dir(void)
{
	static char *default_base_dir = NULL;

	if (default_base_dir)
		return default_base_dir;
	else {
		const char *parent;

		parent = xfilter_utils_get_appdata_dir();
		default_base_dir = g_strconcat(parent, G_DIR_SEPARATOR_S,
#ifdef G_OS_WIN32
					       "SylFilter"
#else
					       ".sylfilter"
#endif
					       , NULL);
	}

	return default_base_dir;
}

int xfilter_utils_mkdir(const char *path)
{
	if (is_dir_exist(path))
		return 0;

	return g_mkdir(path, 0700);
}

static GHashTable *conf_table = NULL;

static void kv_destroy_func(gpointer data)
{
	g_free(data);
}

void xfilter_set_conf_value(const char *key, const char *value)
{
	if (!conf_table)
		conf_table = g_hash_table_new_full(g_str_hash, g_str_equal, kv_destroy_func, kv_destroy_func);

	g_hash_table_replace(conf_table, g_strdup(key), g_strdup(value));
}

const char *xfilter_get_conf_value(const char *key)
{
	if (!conf_table)
		return NULL;
	return (char *)g_hash_table_lookup(conf_table, key);
}

void xfilter_conf_value_clear(void)
{
	if (conf_table) {
		g_hash_table_destroy(conf_table);
		conf_table = NULL;
	}
}