-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
89 lines (76 loc) · 2.99 KB
/
config.c
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
#include "config.h"
#include "funcs.h"
#include "errors.h"
#include "strings.h"
#include <stdlib.h>
#include <string.h>
extern global_t global;
int load_config( const char* filename )
{
/// TODO: Optimize this shit!
global.hostname = "";
global.username = "";
global.password = "";
global.save_pass = FALSE;
global.save_login_data = FALSE;
global.auto_connect = FALSE;
global.is_connected = FALSE;
global.x_display = DEFAULT_X_DISPLAY;
global.send_delay = DEFAULT_SEND_DELAY;
GKeyFile* keyfile = g_key_file_new();
if( g_key_file_load_from_file( keyfile, filename, G_KEY_FILE_NONE, &global.error_msg ) )
{
global.hostname = g_key_file_get_string( keyfile, GROUP_LOGIN, HOSTNAME_CFG, NULL );
global.username = g_key_file_get_string( keyfile, GROUP_LOGIN, USERNAME_CFG, NULL );
global.password = g_key_file_get_string( keyfile, GROUP_LOGIN, PASSWORD_CFG, NULL );
global.x_display = g_key_file_get_string( keyfile, GROUP_PREFS, X_DISPLAY_CFG, NULL );
global.send_delay = g_key_file_get_integer( keyfile, GROUP_PREFS, SEND_DELAY_CFG, NULL );
global.save_pass = g_key_file_get_boolean( keyfile, GROUP_PREFS, SAVE_PASS_CFG, NULL );
global.save_login_data = g_key_file_get_boolean( keyfile, GROUP_PREFS, SAVE_LOGIN_CFG, NULL );
global.auto_connect = g_key_file_get_boolean( keyfile, GROUP_PREFS, AUTO_CONNECT_CFG, NULL );
}
else
{
g_warning( "%s\n", global.error_msg->message );
global.error_msg = NULL;
return( FAILURE );
}
return( SUCCESS );
}
int save_config( const char* filename )
{
/// TODO: And this shit to!
GKeyFile* keyfile = g_key_file_new();
g_key_file_set_string( keyfile, GROUP_PREFS, X_DISPLAY_CFG, global.x_display );
g_key_file_set_integer( keyfile, GROUP_PREFS, SEND_DELAY_CFG, global.send_delay );
g_key_file_set_boolean( keyfile, GROUP_PREFS, SAVE_LOGIN_CFG, global.save_login_data );
g_key_file_set_boolean( keyfile, GROUP_PREFS, SAVE_PASS_CFG, global.save_pass );
g_key_file_set_boolean( keyfile, GROUP_PREFS, AUTO_CONNECT_CFG, global.auto_connect );
if( global.save_login_data )
{
g_key_file_set_string( keyfile, GROUP_LOGIN, HOSTNAME_CFG, global.hostname );
g_key_file_set_string( keyfile, GROUP_LOGIN, USERNAME_CFG, global.username );
}
else
{
g_key_file_set_string( keyfile, GROUP_LOGIN, HOSTNAME_CFG, "" );
g_key_file_set_string( keyfile, GROUP_LOGIN, USERNAME_CFG, "" );
}
if( global.save_pass )
{
g_key_file_set_string( keyfile, GROUP_LOGIN, PASSWORD_CFG, global.password );
}
else
{
g_key_file_set_string( keyfile, GROUP_LOGIN, PASSWORD_CFG, "" );
}
char* config = g_key_file_to_data( keyfile, NULL, &global.error_msg );
if( config != NULL )
{
FILE* file = fopen( filename, "w" );
fwrite( config, sizeof(char), strlen(config), file );
free( config );
fclose( file );
}
return( SUCCESS );
}