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