root / lib / filter-utils.c @ a1de4e94
History | View | Annotate | Download (3.4 kB)
| 1 | /* SylFilter - a message filter
|
|---|---|
| 2 | * |
| 3 | * Copyright (C) 2011 Hiroyuki Yamamoto |
| 4 | * Copyright (C) 2011 Sylpheed Development Team |
| 5 | */ |
| 6 | |
| 7 | #include <glib.h> |
| 8 | |
| 9 | #ifdef BUILTIN_LIBSYLPH
|
| 10 | # include "libsylph/utils.h" |
| 11 | #else
|
| 12 | # include <sylph/utils.h> |
| 13 | #endif
|
| 14 | |
| 15 | #ifdef G_OS_WIN32
|
| 16 | # include <windows.h> |
| 17 | # include <wchar.h> |
| 18 | # include <shlobj.h> |
| 19 | #endif
|
| 20 | |
| 21 | #include "filter.h" |
| 22 | #include "filter-utils.h" |
| 23 | |
| 24 | |
| 25 | char *xfilter_utils_get_file_contents(const char *file) |
| 26 | {
|
| 27 | char *contents = NULL; |
| 28 | |
| 29 | g_return_val_if_fail(file != NULL, NULL); |
| 30 | |
| 31 | if (g_file_get_contents(file, &contents, NULL, NULL)) |
| 32 | return contents;
|
| 33 | else
|
| 34 | return NULL; |
| 35 | } |
| 36 | |
| 37 | void xfilter_free_mem(void *mem) |
| 38 | {
|
| 39 | g_free(mem); |
| 40 | } |
| 41 | |
| 42 | const char *xfilter_utils_get_home_dir(void) |
| 43 | {
|
| 44 | return get_home_dir();
|
| 45 | } |
| 46 | |
| 47 | static char *base_dir = NULL; |
| 48 | |
| 49 | const char *xfilter_utils_get_base_dir(void) |
| 50 | {
|
| 51 | if (!base_dir) {
|
| 52 | base_dir = g_strdup(xfilter_utils_get_default_base_dir()); |
| 53 | } |
| 54 | |
| 55 | return base_dir;
|
| 56 | } |
| 57 | |
| 58 | int xfilter_utils_set_base_dir(const char *path) |
| 59 | {
|
| 60 | const char *tmpdir; |
| 61 | const char *rcdir; |
| 62 | |
| 63 | #ifdef G_OS_WIN32
|
| 64 | if (path) {
|
| 65 | char *upath = g_locale_to_utf8(path, -1, NULL, NULL, NULL); |
| 66 | if (xfilter_utils_mkdir(upath) < 0) { |
| 67 | g_free(upath); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | if (base_dir)
|
| 72 | g_free(base_dir); |
| 73 | base_dir = upath; |
| 74 | } else {
|
| 75 | path = xfilter_utils_get_default_base_dir(); |
| 76 | |
| 77 | if (xfilter_utils_mkdir(path) < 0) |
| 78 | return -1; |
| 79 | |
| 80 | if (base_dir)
|
| 81 | g_free(base_dir); |
| 82 | base_dir = g_strdup(path); |
| 83 | } |
| 84 | #else
|
| 85 | if (!path)
|
| 86 | path = xfilter_utils_get_default_base_dir(); |
| 87 | |
| 88 | if (xfilter_utils_mkdir(path) < 0) |
| 89 | return -1; |
| 90 | |
| 91 | if (base_dir)
|
| 92 | g_free(base_dir); |
| 93 | base_dir = g_strdup(path); |
| 94 | #endif
|
| 95 | |
| 96 | if (xfilter_get_app_mode() == XF_APP_MODE_STANDALONE) {
|
| 97 | set_rc_dir(base_dir); |
| 98 | } else {
|
| 99 | /* if default rc dir not exist, use sylfilter base dir */
|
| 100 | rcdir = get_rc_dir(); |
| 101 | if (!is_dir_exist(rcdir)) {
|
| 102 | set_rc_dir(base_dir); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | tmpdir = get_tmp_dir(); |
| 107 | xfilter_utils_mkdir(tmpdir); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static const char *xfilter_utils_get_appdata_dir(void) |
| 113 | {
|
| 114 | #ifdef G_OS_WIN32
|
| 115 | static char *appdata_dir = NULL; |
| 116 | |
| 117 | if (appdata_dir)
|
| 118 | return appdata_dir;
|
| 119 | else {
|
| 120 | HRESULT hr; |
| 121 | wchar_t path[MAX_PATH + 1];
|
| 122 | |
| 123 | hr = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, path); |
| 124 | if (hr == S_OK)
|
| 125 | appdata_dir = g_utf16_to_utf8(path, -1, NULL, NULL, NULL); |
| 126 | else
|
| 127 | return g_get_home_dir();
|
| 128 | } |
| 129 | |
| 130 | return appdata_dir;
|
| 131 | #else
|
| 132 | return g_get_home_dir();
|
| 133 | #endif
|
| 134 | } |
| 135 | |
| 136 | const char *xfilter_utils_get_default_base_dir(void) |
| 137 | {
|
| 138 | static char *default_base_dir = NULL; |
| 139 | |
| 140 | if (default_base_dir)
|
| 141 | return default_base_dir;
|
| 142 | else {
|
| 143 | const char *parent; |
| 144 | |
| 145 | parent = xfilter_utils_get_appdata_dir(); |
| 146 | default_base_dir = g_strconcat(parent, G_DIR_SEPARATOR_S, |
| 147 | #ifdef G_OS_WIN32
|
| 148 | "SylFilter"
|
| 149 | #else
|
| 150 | ".sylfilter"
|
| 151 | #endif
|
| 152 | , NULL);
|
| 153 | } |
| 154 | |
| 155 | return default_base_dir;
|
| 156 | } |
| 157 | |
| 158 | int xfilter_utils_mkdir(const char *path) |
| 159 | {
|
| 160 | if (is_dir_exist(path))
|
| 161 | return 0; |
| 162 | |
| 163 | return g_mkdir(path, 0700); |
| 164 | } |
| 165 | |
| 166 | static GHashTable *conf_table = NULL; |
| 167 | |
| 168 | static void kv_destroy_func(gpointer data) |
| 169 | {
|
| 170 | g_free(data); |
| 171 | } |
| 172 | |
| 173 | void xfilter_set_conf_value(const char *key, const char *value) |
| 174 | {
|
| 175 | if (!conf_table)
|
| 176 | conf_table = g_hash_table_new_full(g_str_hash, g_str_equal, kv_destroy_func, kv_destroy_func); |
| 177 | |
| 178 | g_hash_table_replace(conf_table, g_strdup(key), g_strdup(value)); |
| 179 | } |
| 180 | |
| 181 | const char *xfilter_get_conf_value(const char *key) |
| 182 | {
|
| 183 | if (!conf_table)
|
| 184 | return NULL; |
| 185 | return (char *)g_hash_table_lookup(conf_table, key); |
| 186 | } |
| 187 | |
| 188 | void xfilter_conf_value_clear(void) |
| 189 | {
|
| 190 | if (conf_table) {
|
| 191 | g_hash_table_destroy(conf_table); |
| 192 | conf_table = NULL;
|
| 193 | } |
| 194 | } |