summary refs log tree commit diff stats
path: root/lib/std/appdirs.nim
blob: 963451efe2cbf66a4482bfb8fa272db623f5c7da (plain) (blame)
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
## This module implements helpers for determining special directories used by apps.

## .. importdoc:: paths.nim

from std/private/osappdirs import nil
import std/paths
import std/envvars

proc getHomeDir*(): Path {.inline, tags: [ReadEnvEffect, ReadIOEffect].} =
  ## Returns the home directory of the current user.
  ##
  ## This proc is wrapped by the `expandTilde proc`_
  ## for the convenience of processing paths coming from user configuration files.
  ##
  ## See also:
  ## * `getConfigDir proc`_
  ## * `getTempDir proc`_
  result = Path(osappdirs.getHomeDir())

proc getDataDir*(): Path {.inline, tags: [ReadEnvEffect, ReadIOEffect].} =
  ## Returns the data directory of the current user for applications.
  ## 
  ## On non-Windows OSs, this proc conforms to the XDG Base Directory
  ## spec. Thus, this proc returns the value of the `XDG_DATA_HOME` environment
  ## variable if it is set, otherwise it returns the default configuration
  ## directory ("~/.local/share" or "~/Library/Application Support" on macOS).
  ## 
  ## See also:
  ## * `getHomeDir proc`_
  ## * `getConfigDir proc`_
  ## * `getTempDir proc`_
  ## * `expandTilde proc`_
  ## * `getCurrentDir proc`_
  ## * `setCurrentDir proc`_
  result = Path(osappdirs.getDataDir())

proc getConfigDir*(): Path {.inline, tags: [ReadEnvEffect, ReadIOEffect].} =
  ## Returns the config directory of the current user for applications.
  ##
  ## On non-Windows OSs, this proc conforms to the XDG Base Directory
  ## spec. Thus, this proc returns the value of the `XDG_CONFIG_HOME` environment
  ## variable if it is set, otherwise it returns the default configuration
  ## directory ("~/.config/").
  ##
  ## An OS-dependent trailing slash is always present at the end of the
  ## returned string: `\\` on Windows and `/` on all other OSs.
  ##
  ## See also:
  ## * `getHomeDir proc`_
  ## * `getTempDir proc`_
  result = Path(osappdirs.getConfigDir())

proc getCacheDir*(): Path {.inline.} =
  ## Returns the cache directory of the current user for applications.
  ##
  ## This makes use of the following environment variables:
  ##
  ## * On Windows: `getEnv("LOCALAPPDATA")`
  ##
  ## * On macOS: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")`
  ##
  ## * On other platforms: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")`
  ##
  ## **See also:**
  ## * `getHomeDir proc`_
  ## * `getTempDir proc`_
  ## * `getConfigDir proc`_
  # follows https://crates.io/crates/platform-dirs
  result = Path(osappdirs.getCacheDir())

proc getCacheDir*(app: Path): Path {.inline.} =
  ## Returns the cache directory for an application `app`.
  ##
  ## * On Windows, this uses: `getCacheDir() / app / "cache"`
  ## * On other platforms, this uses: `getCacheDir() / app`
  result = Path(osappdirs.getCacheDir(app.string))

proc getTempDir*(): Path {.inline, tags: [ReadEnvEffect, ReadIOEffect].} =
  ## Returns the temporary directory of the current user for applications to
  ## save temporary files in.
  ##
  ## On Windows, it calls [GetTempPath](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppathw).
  ## On Posix based platforms, it will check `TMPDIR`, `TEMP`, `TMP` and `TEMPDIR` environment variables in order.
  ## On all platforms, `/tmp` will be returned if the procs fails.
  ##
  ## You can override this implementation
  ## by adding `-d:tempDir=mytempname` to your compiler invocation.
  ##
  ## .. Note:: This proc does not check whether the returned path exists.
  ##
  ## See also:
  ## * `getHomeDir proc`_
  ## * `getConfigDir proc`_
  result = Path(osappdirs.getTempDir())
> return 1; } void api_register_command(const char *command_name, int min_args, int max_args, const char **synopsis, const char *description, const char *arguments[][2], const char **examples, void *callback, void(*callback_func)(PluginCommand *command, gchar **args)) { PluginCommand *command = malloc(sizeof(PluginCommand)); command->command_name = command_name; command->min_args = min_args; command->max_args = max_args; command->callback = callback; command->callback_func = callback_func; CommandHelp *help = malloc(sizeof(CommandHelp)); int i = 0; for (i = 0; synopsis[i] != NULL; i++) { help->synopsis[i] = strdup(synopsis[i]); } help->synopsis[i] = NULL; help->desc = strdup(description); for (i = 0; arguments[i][0] != NULL; i++) { help->args[i][0] = strdup(arguments[i][0]); help->args[i][1] = strdup(arguments[i][1]); } help->args[i][0] = NULL; help->args[i][1] = NULL; for (i = 0; examples[i] != NULL; i++) { help->examples[i] = strdup(examples[i]); } help->examples[i] = NULL; command->help = help; callbacks_add_command(command); } void api_register_timed(void *callback, int interval_seconds, void (*callback_func)(PluginTimedFunction *timed_function)) { PluginTimedFunction *timed_function = malloc(sizeof(PluginTimedFunction)); timed_function->callback = callback; timed_function->callback_func = callback_func; timed_function->interval_seconds = interval_seconds; timed_function->timer = g_timer_new(); callbacks_add_timed(timed_function); } void api_register_ac(const char *key, char **items) { autocompleters_add(key, items); } void api_notify(const char *message, const char *category, int timeout_ms) { notify(message, timeout_ms, category); } void api_send_line(char *line) { ProfWin *current = wins_get_current(); cmd_process_input(current, line); } char * api_get_current_recipient(void) { ProfWin *current = wins_get_current(); if (current->type == WIN_CHAT) { ProfChatWin *chatwin = (ProfChatWin*)current; assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK); return chatwin->barejid; } else { return NULL; } } char * api_get_current_muc(void) { ProfWin *current = wins_get_current(); if (current->type == WIN_MUC) { ProfMucWin *mucwin = (ProfMucWin*)current; assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK); return mucwin->roomjid; } else { return NULL; } } void api_log_debug(const char *message) { log_debug("%s", message); } void api_log_info(const char *message) { log_info("%s", message); } void api_log_warning(const char *message) { log_warning("%s", message); } void api_log_error(const char *message) { log_error("%s", message); } int api_win_exists(const char *tag) { return (wins_get_plugin(tag) != NULL); } void api_win_create(const char *tag, void *callback, void(*callback_func)(PluginWindowCallback *window_callback, const char *tag, const char * const line)) { PluginWindowCallback *window = malloc(sizeof(PluginWindowCallback)); window->callback = callback; window->callback_func = callback_func; callbacks_add_window_handler(tag, window); wins_new_plugin(tag); // set status bar active ProfPluginWin *pluginwin = wins_get_plugin(tag); int num = wins_get_num((ProfWin*)pluginwin); status_bar_active(num); } int api_win_focus(const char *tag) { if (tag == NULL) { log_warning("%s", "prof_win_focus failed, tag is NULL"); return 0; } ProfPluginWin *pluginwin = wins_get_plugin(tag); if (pluginwin == NULL) { log_warning("prof_win_focus failed, no window with tag: %s", tag); return 0; } ui_focus_win((ProfWin*)pluginwin); return 1; } int api_win_show(const char *tag, const char *line) { if (tag == NULL) { log_warning("%s", "prof_win_show failed, tag is NULL"); return 0; } if (line == NULL) { log_warning("%s", "prof_win_show failed, line is NULL"); return 0; } ProfPluginWin *pluginwin = wins_get_plugin(tag); if (pluginwin == NULL) { log_warning("prof_win_show failed, no window with tag: %s", tag); return 0; } ProfWin *window = (ProfWin*)pluginwin; win_print(window, '!', 0, NULL, 0, 0, "", line); return 1; } int api_win_show_themed(const char *tag, const char *const group, const char *const key, const char *const def, const char *line) { if (tag == NULL) { log_warning("%s", "prof_win_show_themed failed, tag is NULL"); return 0; } if (line == NULL) { log_warning("%s", "prof_win_show_themed failed, line is NULL"); return 0; } ProfPluginWin *pluginwin = wins_get_plugin(tag); if (pluginwin == NULL) { log_warning("prof_win_show_themed failed, no window with tag: %s", tag); return 0; } theme_item_t themeitem = plugin_themes_get(group, key, def); ProfWin *window = (ProfWin*)pluginwin; win_print(window, '!', 0, NULL, 0, themeitem, "", line); return 1; }