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