Merge pull request #397 from leeroy2098/merge

Implement proper configuration directories for toxic
This commit is contained in:
irungentoo 2013-08-08 11:37:58 -07:00
commit 249f552e67
4 changed files with 200 additions and 3 deletions

View File

@ -10,7 +10,8 @@ add_executable(${exe_name}
main.c main.c
prompt.c prompt.c
friendlist.c friendlist.c
chat.c) chat.c
configdir.c)
target_link_libraries(${exe_name} target_link_libraries(${exe_name}
curses) curses)

136
testing/toxic/configdir.c Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#ifdef WIN32
#include <shlobj.h>
#include <direct.h>
#endif
#ifdef __APPLE__
#include <unistd.h>
#include <pwd.h>
#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;
}

33
testing/toxic/configdir.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
*/
#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);

View File

@ -3,14 +3,23 @@
*/ */
#include <curses.h> #include <curses.h>
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#ifdef _win32
#include <direct.h>
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif
#include "../../core/Messenger.h" #include "../../core/Messenger.h"
#include "../../core/network.h" #include "../../core/network.h"
#include "configdir.h"
#include "windows.h" #include "windows.h"
extern ToxWindow new_prompt(); extern ToxWindow new_prompt();
@ -334,9 +343,19 @@ int main(int argc, char *argv[])
{ {
int ch; int ch;
int f_flag = 0; int f_flag = 0;
char *filename = "data"; char *user_config_dir = get_user_config_dir();
ToxWindow* a; 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; int i = 0;
for (i = 0; i < argc; ++i) { for (i = 0; i < argc; ++i) {
if (argv[i] == NULL) if (argv[i] == NULL)
@ -354,6 +373,7 @@ int main(int argc, char *argv[])
init_term(); init_term();
init_tox(); init_tox();
load_data(filename); load_data(filename);
free(filename);
init_windows(); init_windows();
init_window_status(); init_window_status();
@ -364,6 +384,13 @@ int main(int argc, char *argv[])
attroff(COLOR_PAIR(3) | A_BOLD); 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) { while(true) {
/* Update tox */ /* Update tox */
do_tox(); do_tox();