summaryrefslogtreecommitdiff
path: root/src/preferences.c
blob: 6ca6d7065742aa78552d11d5e07bc529a6ad588f (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  File-Roller
 *
 *  Copyright (C) 2001, 2003 Free Software Foundation, Inc.
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 */

#include <string.h>
#include <mateconf/mateconf-client.h>
#include "typedefs.h"
#include "preferences.h"
#include "main.h"
#include "file-utils.h"
#include "mateconf-utils.h"
#include "fr-window.h"


#define DIALOG_PREFIX "/apps/file-roller/dialogs/"


typedef struct {
	int   i_value;
	char *s_value;
} EnumStringTable;


static int
get_enum_from_string (EnumStringTable *table,
		      const char      *s_value)
{
	int i;

	/* return the first value if s_value is invalid */

	if (s_value == NULL)
		return table[0].i_value;

	for (i = 0; table[i].s_value != NULL; i++)
		if (strcmp (s_value, table[i].s_value) == 0)
			return table[i].i_value;

	return table[0].i_value;
}


static char *
get_string_from_enum (EnumStringTable *table,
		      int              i_value)
{
	int i;

	for (i = 0; table[i].s_value != NULL; i++)
		if (i_value == table[i].i_value)
			return table[i].s_value;

	/* return the first value if i_value is invalid */

	return table[0].s_value;
}


/* --------------- */


static EnumStringTable sort_method_table [] = {
	{ FR_WINDOW_SORT_BY_NAME, "name" },
	{ FR_WINDOW_SORT_BY_SIZE, "size" },
	{ FR_WINDOW_SORT_BY_TYPE, "type" },
	{ FR_WINDOW_SORT_BY_TIME, "time" },
	{ FR_WINDOW_SORT_BY_PATH, "path" },
	{ 0, NULL }
};

static EnumStringTable sort_type_table [] = {
	{ GTK_SORT_ASCENDING, "ascending" },
	{ GTK_SORT_DESCENDING, "descending" },
	{ 0, NULL }
};

static EnumStringTable list_mode_table [] = {
	{ FR_WINDOW_LIST_MODE_FLAT, "all_files" },
	{ FR_WINDOW_LIST_MODE_AS_DIR, "as_folder" },
	{ 0, NULL }
};

static EnumStringTable compression_level_table [] = {
	{ FR_COMPRESSION_VERY_FAST, "very_fast" },
	{ FR_COMPRESSION_FAST, "fast" },
	{ FR_COMPRESSION_NORMAL, "normal" },
	{ FR_COMPRESSION_MAXIMUM, "maximum" },
	{ 0, NULL }
};


/* --------------- */


FrWindowSortMethod
preferences_get_sort_method (void)
{
	char *s_value;
	int   i_value;

	s_value = eel_mateconf_get_string (PREF_LIST_SORT_METHOD, "name");
	i_value = get_enum_from_string (sort_method_table, s_value);
	g_free (s_value);

	return (FrWindowSortMethod) i_value;
}


void
preferences_set_sort_method (FrWindowSortMethod i_value)
{
	char *s_value;

	s_value = get_string_from_enum (sort_method_table, i_value);
	eel_mateconf_set_string (PREF_LIST_SORT_METHOD, s_value);
}


GtkSortType
preferences_get_sort_type (void)
{
	char *s_value;
	int   i_value;

	s_value = eel_mateconf_get_string (PREF_LIST_SORT_TYPE, "ascending");
	i_value = get_enum_from_string (sort_type_table, s_value);
	g_free (s_value);

	return (GtkSortType) i_value;
}


void
preferences_set_sort_type (GtkSortType i_value)
{
	char *s_value;

	s_value = get_string_from_enum (sort_type_table, i_value);
	eel_mateconf_set_string (PREF_LIST_SORT_TYPE, s_value);
}


FrWindowListMode
preferences_get_list_mode (void)
{
	char *s_value;
	int   i_value;

	s_value = eel_mateconf_get_string (PREF_LIST_MODE, "as_folder");
	i_value = get_enum_from_string (list_mode_table, s_value);
	g_free (s_value);

	return (FrWindowListMode) i_value;
}


void
preferences_set_list_mode (FrWindowListMode i_value)
{
	char *s_value;

	s_value = get_string_from_enum (list_mode_table, i_value);
	eel_mateconf_set_string (PREF_LIST_MODE, s_value);
}


FrCompression
preferences_get_compression_level (void)
{
	char *s_value;
	int   i_value;

	s_value = eel_mateconf_get_string (PREF_ADD_COMPRESSION_LEVEL, "normal");
	i_value = get_enum_from_string (compression_level_table, s_value);
	g_free (s_value);

	return (FrCompression) i_value;
}


void
preferences_set_compression_level (FrCompression i_value)
{
	char *s_value;

	s_value = get_string_from_enum (compression_level_table, i_value);
	eel_mateconf_set_string (PREF_ADD_COMPRESSION_LEVEL, s_value);
}


static void
set_dialog_property_int (const char *dialog,
			 const char *property, 
			 int         value)
{
	char *key;

	key = g_strconcat (DIALOG_PREFIX, dialog, "/", property, NULL);
	eel_mateconf_set_integer (key, value);
	g_free (key);
}


void
pref_util_save_window_geometry (GtkWindow  *window,
				const char *dialog)
{
	int x, y, width, height;

	gtk_window_get_position (window, &x, &y);
	set_dialog_property_int (dialog, "x", x);
	set_dialog_property_int (dialog, "y", y);

	gtk_window_get_size (window, &width, &height);
	set_dialog_property_int (dialog, "width", width);
	set_dialog_property_int (dialog, "height", height);
}


static int
get_dialog_property_int (const char *dialog,
			 const char *property)
{
	char *key;
	int   value;

	key = g_strconcat (DIALOG_PREFIX, dialog, "/", property, NULL);
	value = eel_mateconf_get_integer (key, -1);
	g_free (key);

	return value;
}


void
pref_util_restore_window_geometry (GtkWindow  *window,
				   const char *dialog)
{
	int x, y, width, height;

	x = get_dialog_property_int (dialog, "x");
	y = get_dialog_property_int (dialog, "y");
	width = get_dialog_property_int (dialog, "width");
	height = get_dialog_property_int (dialog, "height");

	if (width != -1 && height != 1)
		gtk_window_set_default_size (window, width, height);

	gtk_window_present (window);
}