diff --git a/testing/toxic/CMakeLists.txt b/testing/toxic/CMakeLists.txt index 38f02dc6..13b8692d 100644 --- a/testing/toxic/CMakeLists.txt +++ b/testing/toxic/CMakeLists.txt @@ -10,7 +10,8 @@ add_executable(${exe_name} main.c prompt.c friendlist.c - chat.c) + chat.c + configdir.c) target_link_libraries(${exe_name} curses) diff --git a/testing/toxic/configdir.c b/testing/toxic/configdir.c new file mode 100644 index 00000000..d91d0804 --- /dev/null +++ b/testing/toxic/configdir.c @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2013 Tox project All Rights Reserved. + * + * This file is part of Tox. + * + * Tox is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tox is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tox. If not, see . + * + */ + +#include +#include +#include +#include +#include +#include + +#ifdef WIN32 +#include +#include +#endif + +#ifdef __APPLE__ +#include +#include +#endif + +#include "configdir.h" + +/* + * Retrieves a correct configuration directory, depending on the OS used, with a trailing slash + */ +char *get_user_config_dir(void) +{ + char *user_config_dir; + + #ifdef WIN32 + + char appdata[MAX_PATH]; + HRESULT result = SHGetFolderPath( + NULL, + CSIDL_APPDATA, + NULL, + SHGFP_TYPE_CURRENT, + appdata + ) + if (!result) return NULL; + + user_config_dir = strdup(appdata); + + return user_config_dir; + + #elif defined __APPLE__ + + struct passwd *pass = getpwuid(getuid()); + if (!pass) return NULL; + char *home = pass->pw_dir; + user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + 1); + + if(user_config_dir) { + strcpy(user_config_dir, home); + strcat(user_config_dir, "/Library/Application Support"); + } + return user_config_dir; + + #else + + if (getenv("XDG_CONFIG_HOME")) { + user_config_dir = strdup(getenv("XDG_CONFIG_HOME")); + } else { + user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1); + if (user_config_dir) { + strcpy(user_config_dir, getenv("HOME")); + strcat(user_config_dir, "/.config"); + } + } + return user_config_dir; + + #endif +} + +/* + * Creates the config directory. + */ +int create_user_config_dir(char *path) +{ + + int mkdir_err; + + #ifdef WIN32 + + char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); + strcpy(fullpath, path); + strcat(fullpath, CONFIGDIR); + + mkdir_err = _mkdir(fullpath); + struct __stat64 buf; + if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { + free(fullpath); + return -1; + } + + #else + + mkdir_err = mkdir(path, 0700); + struct stat buf; + + if(mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) { + return -1; + } + + char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); + strcpy(fullpath, path); + strcat(fullpath, CONFIGDIR); + + mkdir_err = mkdir(fullpath, 0700); + + if(mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { + free(fullpath); + return -1; + } + + #endif + + return 0; +} diff --git a/testing/toxic/configdir.h b/testing/toxic/configdir.h new file mode 100644 index 00000000..fad949cf --- /dev/null +++ b/testing/toxic/configdir.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2013 Tox project All Rights Reserved. + * + * This file is part of Tox. + * + * Tox is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tox is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tox. If not, see . + * + */ + +#ifdef _win32 +#define CONFIGDIR "\\toxic\\" +#else +#define CONFIGDIR "/toxic/" +#endif + +#ifndef S_ISDIR +#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) +#endif + +char *get_user_config_dir(void); + +int create_user_config_dir(char *path); diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 7fa9e964..162cce68 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -3,14 +3,23 @@ */ #include +#include #include #include #include #include +#ifdef _win32 +#include +#else +#include +#include +#endif + #include "../../core/Messenger.h" #include "../../core/network.h" +#include "configdir.h" #include "windows.h" extern ToxWindow new_prompt(); @@ -334,9 +343,19 @@ int main(int argc, char *argv[]) { int ch; int f_flag = 0; - char *filename = "data"; + char *user_config_dir = get_user_config_dir(); + char *filename; + int config_err = create_user_config_dir(user_config_dir); + if(config_err) { + filename = "data"; + } else { + filename = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); + strcpy(filename, user_config_dir); + strcat(filename, CONFIGDIR); + strcat(filename, "data"); + } + ToxWindow* a; - int i = 0; for (i = 0; i < argc; ++i) { if (argv[i] == NULL) @@ -354,6 +373,7 @@ int main(int argc, char *argv[]) init_term(); init_tox(); load_data(filename); + free(filename); init_windows(); init_window_status(); @@ -363,6 +383,13 @@ int main(int argc, char *argv[]) "defaulting to 'data' for a keyfile...\n"); attroff(COLOR_PAIR(3) | A_BOLD); } + + if(config_err) { + attron(COLOR_PAIR(3) | A_BOLD); + wprintw(prompt->window, "Unable to determine configuration directory!\n" + "defaulting to 'data' for a keyfile...\n"); + attroff(COLOR_PAIR(3) | A_BOLD); + } while(true) { /* Update tox */