mp3splt-gtk: src/preferences_manager.c Source File

mp3splt-gtk 0.9.3.1519

Loading...
Searching...
No Matches
preferences_manager.c
Go to the documentation of this file.
1/**********************************************************
2 *
3 * mp3splt-gtk -- utility based on mp3splt,
4 * for mp3/ogg splitting without decoding
5 *
6 * Copyright: (C) 2005-2014 Alexandru Munteanu
7 * Contact: m@ioalex.net
8 *
9 * http://mp3splt.sourceforge.net/
10 *
11 *********************************************************/
12
13/**********************************************************
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * USA.
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
29 *
30 *********************************************************/
31
32/*!********************************************************
33 * \file
34 * Save and read preferences
35 *
36 * This file contains the functions to save the preferences
37 * on the hard disk and to read them again at the next
38 * start of the program.
39 ********************************************************/
40
41#include "all_includes.h"
42
43#include "preferences_manager.h"
44
45static void check_pref_file_and_write_default(ui_state *ui);
46static void pm_free_spinner_int_preferences(GArray *spinner_int_preferences);
47static void pm_free_range_preferences(GArray *range_preferences);
48static void pm_load_spinner_int_preferences(GKeyFile *key_file, preferences_state *pm);
49static void pm_save_spinner_int_preferences(GKeyFile *key_file, preferences_state *pm);
50static void pm_write_default_spinner_int_preferences(GKeyFile *key_file, preferences_state *pm);
51static void pm_load_range_preferences(GKeyFile *key_file, preferences_state *pm);
52static void pm_save_range_preferences(GKeyFile *key_file, preferences_state *pm);
53static void pm_write_default_range_preferences(GKeyFile *key_file, preferences_state *pm);
54
55void pm_register_spinner_int_preference(gchar *main_key, gchar *second_key,
56 gint default_value, GtkWidget *spinner,
57 void (*update_spinner_value_cb)(GtkWidget *spinner, gpointer data),
58 gpointer user_data_for_cb, preferences_state *pm)
59{
60 spinner_int_preference preference;
61
62 preference.main_key = strdup(main_key);
63 preference.second_key = strdup(second_key);
64 preference.default_value = default_value;
65 preference.spinner = spinner;
66 preference.update_spinner_value_cb = update_spinner_value_cb;
67 preference.user_data_for_cb = user_data_for_cb;
68
69 g_array_append_val(pm->spinner_int_preferences, preference);
70}
71
72void pm_register_range_preference(gchar *main_key, gchar *second_key,
73 gint default_value, GtkWidget *range,
74 void (*update_adjustment_value)(GtkAdjustment *adjustment, gpointer data),
75 gpointer user_data_for_cb, preferences_state *pm)
76{
77 range_preference preference;
78
79 preference.main_key = strdup(main_key);
80 preference.second_key = strdup(second_key);
81 preference.default_value = default_value;
82 preference.range = range;
83 preference.update_adjustment_value = update_adjustment_value;
84 preference.user_data_for_cb = user_data_for_cb;
85
86 g_array_append_val(pm->range_preferences, preference);
87}
88
89 preferences_state *pm_state_new()
90{
91 preferences_state *pm = g_malloc0(sizeof(preferences_state));
92
93 pm->spinner_int_preferences = g_array_new(TRUE, TRUE, sizeof(spinner_int_preference));
94 pm->range_preferences = g_array_new(TRUE, TRUE, sizeof(range_preference));
95
96 return pm;
97}
98
99void pm_free(preferences_state **pm)
100{
101 if (!pm || !*pm)
102 {
103 return;
104 }
105
106 pm_free_spinner_int_preferences((*pm)->spinner_int_preferences);
107 pm_free_range_preferences((*pm)->range_preferences);
108
109 g_free(*pm);
110 *pm = NULL;
111}
112
113static void pm_load(GKeyFile *key_file, preferences_state *pm)
114{
115 pm_load_spinner_int_preferences(key_file, pm);
116 pm_load_range_preferences(key_file, pm);
117}
118
119static void pm_save(GKeyFile *key_file, preferences_state *pm)
120{
121 pm_save_spinner_int_preferences(key_file, pm);
122 pm_save_range_preferences(key_file, pm);
123}
124
125static void pm_write_default(GKeyFile *key_file, preferences_state *pm)
126{
127 pm_write_default_spinner_int_preferences(key_file, pm);
128 pm_write_default_range_preferences(key_file, pm);
129}
130
131gchar *get_configuration_directory()
132{
133 gchar mp3splt_dir[14] = ".mp3splt-gtk";
134
135 gchar *home_dir = g_strdup(g_get_home_dir());
136
137#ifdef __WIN32__
138 //manage c:\ because the gtk dir returns us "c:\"
139 //and the normal directories without the "\"
140 if (home_dir[strlen(home_dir)-1] == '\\')
141 {
142 home_dir[strlen(home_dir)-1] = '\0';
143 }
144#endif
145
146 size_t malloc_number = strlen(home_dir) + strlen(mp3splt_dir) + 2;
147 gchar *mp3splt_dir_with_path = malloc(malloc_number * sizeof(gchar));
148 g_snprintf(mp3splt_dir_with_path, malloc_number,
149 "%s%s%s", home_dir, G_DIR_SEPARATOR_S, mp3splt_dir);
150
151 if (home_dir) { g_free(home_dir); }
152
153 struct stat buffer;
154 gint status = g_stat(mp3splt_dir_with_path, &buffer);
155 if ((status != 0) || (S_ISDIR(buffer.st_mode) == 0))
156 {
157 if ((status == 0) && (S_ISREG(buffer.st_mode) != 0))
158 {
159 size_t malloc_number2 = strlen(mp3splt_dir_with_path) + 5;
160 gchar *backup_file = malloc(malloc_number2 * sizeof(gchar));
161 snprintf(backup_file, malloc_number2, "%s%s", mp3splt_dir_with_path,".bak");
162 g_rename(mp3splt_dir_with_path, backup_file);
163 g_free(backup_file);
164 }
165
166 //if it is not a directory and not a file, we suppose we can
167 //create the directory
168#ifdef __WIN32__
169 g_mkdir(mp3splt_dir_with_path, 0775);
170#else
171 g_mkdir(mp3splt_dir_with_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
172#endif
173 }
174
175 return mp3splt_dir_with_path;
176}
177
186gchar *get_preferences_filename()
187{
188 gchar *mp3splt_dir_with_path = get_configuration_directory();
189
190 size_t fname_malloc_number = strlen(mp3splt_dir_with_path) + 30;
191 gchar *filename = malloc(fname_malloc_number * sizeof(gchar));
192
193 g_snprintf(filename, fname_malloc_number, "%s%smp3splt-gtk_prefs",
194 mp3splt_dir_with_path, G_DIR_SEPARATOR_S);
195 if (mp3splt_dir_with_path) { g_free(mp3splt_dir_with_path); }
196
197 return filename;
198}
199
202 void load_preferences(ui_state *ui)
203{
204 check_pref_file_and_write_default(ui);
205
206 GKeyFile *key_file = g_key_file_new();
207
208 gchar *filename = get_preferences_filename();
209 g_key_file_load_from_file(key_file, filename, G_KEY_FILE_KEEP_COMMENTS, NULL);
210 if (filename)
211 {
212 g_free(filename);
213 }
214
215#ifdef __WIN32__
216 //language
217 gchar *file_string = g_key_file_get_string(key_file, "general", "language", NULL);
218 GString *lang = g_string_new(file_string);
219
220 //0 = german, 1 = french, 2 = english
221 gint list_number = 2;
222 if (g_string_equal(lang,g_string_new("de")) || g_string_equal(lang,g_string_new("de_DE")))
223 {
224 list_number = 0;
225 }
226 else if (g_string_equal(lang, g_string_new("fr")) || g_string_equal(lang, g_string_new("fr_FR")))
227 {
228 list_number = 1;
229 }
230
231 GSList *radio_button_list = gtk_radio_button_get_group(GTK_RADIO_BUTTON(ui->gui->radio_button));
232 GtkWidget *our_button = GTK_WIDGET(g_slist_nth_data(radio_button_list, list_number));
233 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(our_button), TRUE);
234
235 g_free(file_string);
236 g_string_free(lang, TRUE);
237 file_string = NULL;
238 lang = NULL;
239#endif
240
241 // If get_output_directory()!=NULL the path where to output the split file
242 // to has been set from command line
243 if (get_output_directory(ui) == NULL)
244 {
245 // No output_path from command-line => get the path from the preferences
246 gchar *save_path = g_key_file_get_string(key_file, "split", "save_path", NULL);
247 if (save_path != NULL)
248 {
249 set_output_directory_and_update_ui(save_path, ui);
250 }
251 g_free(save_path);
252 }
253
254 //player
255 gint item = g_key_file_get_integer(key_file, "player", "default_player",NULL);
256 ch_set_active_value(GTK_COMBO_BOX(ui->gui->player_combo_box), item);
257
258 item = g_key_file_get_boolean(key_file, "player", "amplitude_wave_ticked", NULL);
259 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->gui->silence_wave_check_button), item);
260 ui->status->show_silence_wave = item;
261
262 pm_load(key_file, ui->preferences);
263
264 //frame mode
265 item = g_key_file_get_boolean(key_file, "split", "frame_mode", NULL);
266 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->gui->frame_mode), item);
267
268 //bit reservoir mode
269 item = g_key_file_get_boolean(key_file, "split", "bit_reservoir_mode", NULL);
270 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->gui->bit_reservoir_mode), item);
271
272 //adjust mode
273 item = g_key_file_get_boolean(key_file, "split", "adjust_mode", NULL);
274 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->gui->adjust_mode), item);
275
276 item = g_key_file_get_boolean(key_file, "output", "splitpoint_names_from_filename", NULL);
277 if (item)
278 {
279 gtk_toggle_button_set_active(ui->gui->names_from_filename, TRUE);
280 }
281 else
282 {
283 gtk_toggle_button_set_active(ui->gui->names_from_filename, FALSE);
284 }
285
286 //adjust threshold
287 gdouble item2;
288 item = g_key_file_get_integer(key_file, "split", "adjust_threshold", NULL);
289 item2 = item/100 + (item%100)/100.;
290 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ui->gui->spinner_adjust_threshold), item2);
291 //adjust offset
292 item = g_key_file_get_integer(key_file, "split", "adjust_offset", NULL);
293 item2 = item/100 + (item%100)/100.;
294 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ui->gui->spinner_adjust_offset), item2);
295 //adjust gap
296 item = g_key_file_get_integer(key_file, "split", "adjust_gap", NULL);
297 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ui->gui->spinner_adjust_gap), item);
298 //adjust min
299 item = g_key_file_get_integer(key_file, "split", "adjust_min", NULL);
300 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ui->gui->spinner_adjust_min), item);
301
302 //tags options
303 gint tag_pref_file = g_key_file_get_integer(key_file, "split", "tags", NULL);
304 rh_set_radio_value(ui->gui->tags_radio, tag_pref_file, TRUE);
305
306 //replace underscores by spaces
307 item = g_key_file_get_boolean(key_file, "split", "replace_underscore_by_space", NULL);
308 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->gui->replace_underscore_by_space_check_box),
309 item);
310
311 //artist text properties
312 item = g_key_file_get_integer(key_file, "split", "artist_text_properties",NULL);
313 if (item)
314 {
315 ch_set_active_value(ui->gui->artist_text_properties_combo, item);
316 }
317 else
318 {
319 ch_set_active_value(ui->gui->artist_text_properties_combo, SPLT_NO_CONVERSION);
320 }
321 //album text properties
322 item = g_key_file_get_integer(key_file, "split", "album_text_properties",NULL);
323 if (item)
324 {
325 ch_set_active_value(ui->gui->album_text_properties_combo, item);
326 }
327 else
328 {
329 ch_set_active_value(ui->gui->album_text_properties_combo, SPLT_NO_CONVERSION);
330 }
331 //title text properties
332 item = g_key_file_get_integer(key_file, "split", "title_text_properties",NULL);
333 if (item)
334 {
335 ch_set_active_value(ui->gui->title_text_properties_combo, item);
336 }
337 else
338 {
339 ch_set_active_value(ui->gui->title_text_properties_combo, SPLT_NO_CONVERSION);
340 }
341 //comment text properties
342 item = g_key_file_get_integer(key_file, "split", "comment_text_properties",NULL);
343 if (item)
344 {
345 ch_set_active_value(ui->gui->comment_text_properties_combo, item);
346 }
347 else
348 {
349 ch_set_active_value(ui->gui->comment_text_properties_combo, SPLT_NO_CONVERSION);
350 }
351
352 //genre
353 gchar *default_genre = g_key_file_get_string(key_file, "split", "genre", NULL);
354 if (default_genre)
355 {
356 ch_set_active_str_value(ui->gui->genre_combo, default_genre);
357 g_free(default_genre);
358 }
359 else
360 {
361 ch_set_active_str_value(ui->gui->genre_combo, SPLT_UNDEFINED_GENRE);
362 }
363
364 //default comment tag
365 gchar *default_comment_tag = g_key_file_get_string(key_file, "split", "default_comment_tag", NULL);
366 if (default_comment_tag)
367 {
368 gtk_entry_set_text(GTK_ENTRY(ui->gui->comment_tag_entry), default_comment_tag);
369 g_free(default_comment_tag);
370 }
371
372 //regexp to parse filename into tags
373 gchar *tags_from_fname_regex =
374 g_key_file_get_string(key_file, "split", "tags_from_filename_regex", NULL);
375 if (tags_from_fname_regex)
376 {
377 gtk_entry_set_text(GTK_ENTRY(ui->gui->regex_entry), tags_from_fname_regex);
378 g_free(tags_from_fname_regex);
379 }
380
381 gchar *test_regex_fname =
382 g_key_file_get_string(key_file, "split", "test_regex_fname", NULL);
383 if (test_regex_fname)
384 {
385 gtk_entry_set_text(GTK_ENTRY(ui->gui->test_regex_fname_entry), test_regex_fname);
386 g_free(test_regex_fname);
387 }
388
389 //tags version
390 tag_pref_file = g_key_file_get_integer(key_file, "split", "tags_version", NULL);
391
392 GSList *tags_version_radio_button_list =
393 gtk_radio_button_get_group(GTK_RADIO_BUTTON(ui->gui->tags_version_radio));
394 GtkWidget *the_selection =
395 GTK_WIDGET(g_slist_nth_data(tags_version_radio_button_list, (guint) tag_pref_file));
396 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(the_selection), TRUE);
397
398 //default output format or not
399 gint default_output_format =
400 g_key_file_get_boolean(key_file, "output", "default_output_format", NULL);
401 GSList *output_radio_button_list =
402 gtk_radio_button_get_group(GTK_RADIO_BUTTON(ui->gui->radio_output));
403 GtkWidget *our_selection =
404 GTK_WIDGET(g_slist_nth_data(output_radio_button_list, (guint) default_output_format));
405 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(our_selection), TRUE);
406 if (default_output_format)
407 {
408 gtk_widget_set_sensitive(ui->gui->output_entry, FALSE);
409 gtk_widget_set_sensitive(ui->gui->output_label, FALSE);
410 }
411 else
412 {
413 gtk_widget_set_sensitive(ui->gui->output_default_label, FALSE);
414 }
415
416 //output format
417 gchar *output_format = g_key_file_get_string(key_file, "output", "output_format", NULL);
418 if (output_format)
419 {
420 gtk_entry_set_text(GTK_ENTRY(ui->gui->output_entry), output_format);
421 g_free(output_format);
422 }
423
424 //create directories if needed
425 item = g_key_file_get_boolean(key_file, "output", "create_dirs_if_needed", NULL);
426 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->gui->create_dirs_from_output_files), item);
427
428 //type of split: split mode
429 gint split_mode = g_key_file_get_integer(key_file, "split", "split_mode", NULL);
430 select_split_mode(split_mode, ui);
431
432 //time value
433 gint time_value = g_key_file_get_integer(key_file, "split", "split_mode_time_value", NULL);
434 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ui->gui->spinner_time), time_value);
435
436 //equal time tracks value
437 gint equal_tracks = g_key_file_get_integer(key_file, "split",
438 "split_mode_equal_time_tracks", NULL);
439 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ui->gui->spinner_equal_tracks), equal_tracks);
440
441 gint root_x = g_key_file_get_integer(key_file, "gui", "root_x_position", NULL);
442 gint root_y = g_key_file_get_integer(key_file, "gui", "root_y_position", NULL);
443 if (root_x && root_y)
444 {
445 ui_set_main_win_position(ui, root_x, root_y);
446 }
447
448 gint width = g_key_file_get_integer(key_file, "gui", "width", NULL);
449 gint height = g_key_file_get_integer(key_file, "gui", "height", NULL);
450 if (width && height)
451 {
452 ui_set_main_win_size(ui, width, height);
453 }
454
455 gchar *browser_directory = g_key_file_get_string(key_file, "gui", "browser_directory", NULL);
456 if (browser_directory)
457 {
458 ui_set_browser_directory(ui, browser_directory);
459 g_free(browser_directory);
460 }
461
462 g_key_file_free(key_file);
463}
464
465void save_preferences(ui_state *ui)
466{
467 gchar *filename = get_preferences_filename();
468
469 GKeyFile *my_key_file = g_key_file_new();
470 g_key_file_load_from_file(my_key_file, filename, G_KEY_FILE_KEEP_COMMENTS, NULL);
471
472 //save_path
473 g_key_file_set_string(my_key_file, "split", "save_path", get_output_directory(ui));
474
475 //player
476 g_key_file_set_integer(my_key_file, "player", "default_player", ui->infos->selected_player);
477
478 g_key_file_set_boolean(my_key_file, "player", "amplitude_wave_ticked",
479 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ui->gui->silence_wave_check_button)));
480
481 pm_save(my_key_file, ui->preferences);
482
483#ifdef __WIN32__
484 GString *selected_lang = get_checked_language(ui);
485 g_key_file_set_string(my_key_file, "general", "language", selected_lang->str);
486 g_string_free(selected_lang, TRUE);
487 selected_lang = NULL;
488#endif
489
490 //frame mode
491 g_key_file_set_boolean(my_key_file, "split", "frame_mode",
492 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ui->gui->frame_mode)));
493
494 g_key_file_set_boolean(my_key_file, "split", "bit_reservoir_mode",
495 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ui->gui->bit_reservoir_mode)));
496
497 //adjust mode
498 g_key_file_set_boolean(my_key_file, "split", "adjust_mode",
499 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ui->gui->adjust_mode)));
500
501 //adjust threshold
502 g_key_file_set_integer(my_key_file, "split", "adjust_threshold",
503 (gint) gtk_spin_button_get_value(GTK_SPIN_BUTTON(ui->gui->spinner_adjust_threshold)) * 100);
504 //adjust offset
505 g_key_file_set_integer(my_key_file, "split", "adjust_offset",
506 (gint) gtk_spin_button_get_value(GTK_SPIN_BUTTON(ui->gui->spinner_adjust_offset)) * 100);
507 //adjust gap
508 g_key_file_set_integer(my_key_file, "split", "adjust_gap",
509 (gint) gtk_spin_button_get_value(GTK_SPIN_BUTTON(ui->gui->spinner_adjust_gap)));
510 //adjust min
511 g_key_file_set_integer(my_key_file, "split", "adjust_min",
512 (gint) gtk_spin_button_get_value(GTK_SPIN_BUTTON(ui->gui->spinner_adjust_min)));
513
514 g_key_file_set_boolean(my_key_file, "output", "splitpoint_names_from_filename",
515 gtk_toggle_button_get_active(ui->gui->names_from_filename));
516
517 //output format
518 g_key_file_set_string(my_key_file, "output", "output_format",
519 gtk_entry_get_text(GTK_ENTRY(ui->gui->output_entry)));
520 //default output format
521 g_key_file_set_boolean(my_key_file, "output", "default_output_format",
522 get_checked_output_radio_box(ui));
523 g_key_file_set_boolean(my_key_file, "output", "create_dirs_if_needed",
524 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ui->gui->create_dirs_from_output_files)));
525
526 //tags
527 g_key_file_set_integer(my_key_file, "split", "tags", rh_get_active_value(ui->gui->tags_radio));
528
529 //replace underscores by space
530 g_key_file_set_boolean(my_key_file, "split", "replace_underscore_by_space",
531 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ui->gui->replace_underscore_by_space_check_box)));
532
533 //artist text properties
534 g_key_file_set_integer(my_key_file, "split", "artist_text_properties",
535 ch_get_active_value(ui->gui->artist_text_properties_combo));
536 //album text properties
537 g_key_file_set_integer(my_key_file, "split", "album_text_properties",
538 ch_get_active_value(ui->gui->album_text_properties_combo));
539 //title text properties
540 g_key_file_set_integer(my_key_file, "split", "title_text_properties",
541 ch_get_active_value(ui->gui->title_text_properties_combo));
542 //comment text properties
543 g_key_file_set_integer(my_key_file, "split", "comment_text_properties",
544 ch_get_active_value(ui->gui->comment_text_properties_combo));
545
546 //genre
547 gchar *genre_value = ch_get_active_str_value(ui->gui->genre_combo);
548 if (genre_value != NULL)
549 {
550 g_key_file_set_string(my_key_file, "split", "genre", genre_value);
551 g_free(genre_value);
552 }
553
554 const gchar *comment = gtk_entry_get_text(GTK_ENTRY(ui->gui->comment_tag_entry));
555 if (comment != NULL)
556 {
557 g_key_file_set_string(my_key_file, "split", "default_comment_tag", comment);
558 }
559
560 const gchar *regex_text = gtk_entry_get_text(GTK_ENTRY(ui->gui->regex_entry));
561 if (regex_text != NULL)
562 {
563 g_key_file_set_string(my_key_file, "split", "tags_from_filename_regex", regex_text);
564 }
565
566 const gchar *test_regex_fname = gtk_entry_get_text(GTK_ENTRY(ui->gui->test_regex_fname_entry));
567 if (test_regex_fname != NULL)
568 {
569 g_key_file_set_string(my_key_file, "split", "test_regex_fname", test_regex_fname);
570 }
571
572 //tags version
573 g_key_file_set_integer(my_key_file, "split", "tags_version",
574 get_checked_tags_version_radio_box(ui->gui));
575
576 //type of split: split mode
577 g_key_file_set_integer(my_key_file, "split", "split_mode", get_selected_split_mode(ui));
578 //time value
579 g_key_file_set_integer(my_key_file, "split", "split_mode_time_value",
580 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(ui->gui->spinner_time)));
581 //equal time tracks value
582 g_key_file_set_integer(my_key_file, "split", "split_mode_equal_time_tracks",
583 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(ui->gui->spinner_equal_tracks)));
584
585 const ui_main_window *main_win = ui_get_main_window_infos(ui);
586 g_key_file_set_integer(my_key_file, "gui", "root_x_position", main_win->root_x_pos);
587 g_key_file_set_integer(my_key_file, "gui", "root_y_position", main_win->root_y_pos);
588 g_key_file_set_integer(my_key_file, "gui", "width", main_win->width);
589 g_key_file_set_integer(my_key_file, "gui", "height", main_win->height);
590
591 const char *browser_directory = ui_get_browser_directory(ui);
592 if (browser_directory != NULL)
593 {
594 g_key_file_set_string(my_key_file, "gui", "browser_directory", browser_directory);
595 }
596
597 gchar *key_data = g_key_file_to_data(my_key_file, NULL, NULL);
598
599 //we write to the preference file
600 FILE *preferences_file = (FILE *)g_fopen(filename,"w");
601 g_fprintf(preferences_file,"%s", key_data);
602 fclose(preferences_file);
603
604 g_free(key_data);
605 g_key_file_free(my_key_file);
606
607 if (filename) { g_free(filename); }
608}
609
610/* \brief writes a default configuration file
611
612Also is used to write good values on a bad existing configuration file
613*/
614static void write_default_preferences_file(ui_state *ui)
615{
616 gchar *filename = get_preferences_filename();
617
618 GKeyFile *my_key_file = g_key_file_new();
619 g_key_file_load_from_file(my_key_file, filename, G_KEY_FILE_KEEP_COMMENTS, NULL);
620
621#ifdef __WIN32__
622 //default language
623 if (!g_key_file_has_key(my_key_file, "general", "language",NULL))
624 {
625 g_key_file_set_string(my_key_file, "general", "language", "en");
626 g_key_file_set_comment(my_key_file, "general", "language",
627 "\n language of the gui: en = english, fr = french, de = german", NULL);
628 }
629 //if we have the key, but we have ugly values
630 else
631 {
632 gchar *file_string = g_key_file_get_string(my_key_file, "general", "language", NULL);
633 GString * lang_char = g_string_new(file_string);
634
635 if((!g_string_equal(lang_char,g_string_new("en")))
636 &&(!g_string_equal(lang_char, g_string_new("fr")))
637 &&(!g_string_equal(lang_char, g_string_new("fr_FR")))
638 &&(!g_string_equal(lang_char, g_string_new("de")))
639 &&(!g_string_equal(lang_char, g_string_new("de_DE")))
640 )
641 {
642 g_key_file_set_string(my_key_file, "general", "language", "en");
643 g_key_file_set_comment(my_key_file, "general", "language",
644 "\n language of the gui: en = english, fr_FR = french, de_DE = german", NULL);
645 }
646
647 g_free(file_string);
648 g_string_free(lang_char, TRUE);
649 file_string = NULL;
650 lang_char = NULL;
651 }
652#endif
653
654 //frame mode
655 if (!g_key_file_has_key(my_key_file, "split", "frame_mode",NULL))
656 {
657 g_key_file_set_boolean(my_key_file, "split", "frame_mode", FALSE);
658 }
659
660 if (!g_key_file_has_key(my_key_file, "split", "bit_reservoir_mode",NULL))
661 {
662 g_key_file_set_boolean(my_key_file, "split", "bit_reservoir_mode", FALSE);
663 }
664
665 //adjust mode
666 if (!g_key_file_has_key(my_key_file, "split", "adjust_mode",NULL))
667 {
668 g_key_file_set_boolean(my_key_file, "split", "adjust_mode", FALSE);
669 }
670
671 gint item;
672 gdouble item2;
673 //adjust threshold
674 if (!g_key_file_has_key(my_key_file, "split", "adjust_threshold",NULL))
675 {
676 g_key_file_set_integer(my_key_file, "split", "adjust_threshold",
677 (int)(SPLT_DEFAULT_PARAM_THRESHOLD * 100));
678 }
679 //if we have the key but we have ugly values
680 else
681 {
682 item = g_key_file_get_integer(my_key_file, "split", "adjust_threshold", NULL);
683 //convert to float
684 item2 = item/100 + (item%100)/100.;
685
686 if ((item2 < -96) || (item2 > 0))
687 {
688 g_key_file_set_integer(my_key_file, "split", "adjust_threshold",
689 (int)(SPLT_DEFAULT_PARAM_THRESHOLD * 100));
690 }
691 }
692
693 //adjust offset
694 if (!g_key_file_has_key(my_key_file, "split", "adjust_offset",NULL))
695 {
696 g_key_file_set_integer(my_key_file, "split", "adjust_offset",
697 (int)(SPLT_DEFAULT_PARAM_OFFSET * 100));
698 }
699 //if we have the key but we have ugly values
700 else
701 {
702 item = g_key_file_get_integer(my_key_file, "split", "adjust_offset", NULL);
703 //convert to float
704 item2 = item/100 + (item%100)/100.;
705
706 //if ugly values
707 if ((item2 < -2) || (item2 > 2))
708 {
709 g_key_file_set_integer(my_key_file, "split", "adjust_offset",
710 (int)(SPLT_DEFAULT_PARAM_OFFSET * 100));
711 }
712 }
713
714 //adjust gap
715 if (!g_key_file_has_key(my_key_file, "split", "adjust_gap",NULL))
716 {
717 g_key_file_set_integer(my_key_file, "split", "adjust_gap", SPLT_DEFAULT_PARAM_GAP);
718 }
719 //if we have the key but we have ugly values
720 else
721 {
722 item = g_key_file_get_integer(my_key_file, "split", "adjust_gap", NULL);
723 if ((item < 0) || (item > 2000))
724 {
725 g_key_file_set_integer(my_key_file, "split", "adjust_gap", SPLT_DEFAULT_PARAM_GAP);
726 }
727 }
728
729 //tags options
730 if (!g_key_file_has_key(my_key_file, "split", "tags",NULL))
731 {
732 g_key_file_set_integer(my_key_file, "split", "tags", 1);
733 g_key_file_set_comment(my_key_file, "split", "tags",
734 "\n 0 - No tags, 1 - Default tags, 2 - Original tags, 3 - Tags from filename", NULL);
735 }
736
737 //tags version
738 if (!g_key_file_has_key(my_key_file, "split", "tags_version",NULL))
739 {
740 g_key_file_set_integer(my_key_file, "split", "tags_version", 0);
741 g_key_file_set_comment(my_key_file, "split", "tags_version",
742 "\n 0 - same tags version as the input file, 1 - ID3v1 tags,"
743 " 2 - ID3v2 tags, 3 - ID3v1 & ID3v2 tags",
744 NULL);
745 }
746
747 //default player
748 if (!g_key_file_has_key(my_key_file, "player", "default_player",NULL))
749 {
750 g_key_file_set_integer(my_key_file, "player", "default_player", DEFAULT_PLAYER);
751 g_key_file_set_comment (my_key_file, "player", "default_player",
752 "\n 1 = PLAYER_AUDACIOUS, 2 = PLAYER_SNACKAMP, 3 = PLAYER_GSTREAMER", NULL);
753 }
754 else
755 {
756 //check if we support selected player
757 gint the_player = g_key_file_get_integer(my_key_file, "player", "default_player", NULL);
758 if (the_player == PLAYER_AUDACIOUS) {
759#ifdef NO_AUDACIOUS
760 g_key_file_set_integer(my_key_file, "player", "default_player", DEFAULT_PLAYER);
761#endif
762 }
763 else if (the_player == PLAYER_GSTREAMER) {
764#ifdef NO_GSTREAMER
765 g_key_file_set_integer(my_key_file, "player", "default_player", DEFAULT_PLAYER);
766#endif
767 }
768 //if the value does not make sense
769 else if ((the_player > PLAYER_GSTREAMER) || (the_player < 0)) {
770 g_key_file_set_integer(my_key_file, "player", "default_player", DEFAULT_PLAYER);
771 }
772 }
773
774 pm_write_default(my_key_file, ui->preferences);
775
776 //output format
777 if (!g_key_file_has_key(my_key_file, "output", "output_format",NULL))
778 {
779 g_key_file_set_string(my_key_file, "output", "output_format", SPLT_DEFAULT_OUTPUT);
780 g_key_file_set_comment (my_key_file, "output", "output_format",
781 "\n the output format, contains @a,"
782 "@b, @g, @p, @t and @n, see the program for"
783 " more details", NULL);
784 }
785
786 //default output path boolean
787 if (!g_key_file_has_key(my_key_file, "output", "default_output_format",NULL))
788 {
789 g_key_file_set_boolean(my_key_file, "output", "default_output_format", TRUE);
790 g_key_file_set_comment(my_key_file, "output", "default_output_format",
791 "\n can be true or false"
792 " - if we use the default output or"
793 " not for cddb, cue and freedb search", NULL);
794 }
795
796 //frame mode
797 if (!g_key_file_has_key(my_key_file, "output", "create_dirs_if_needed", NULL))
798 {
799 g_key_file_set_boolean(my_key_file, "output", "create_dirs_if_needed", TRUE);
800 }
801
802 //split save path (output dir)
803 if (!g_key_file_has_key(my_key_file, "split", "save_path",NULL))
804 {
805#ifdef __WIN32__
806 const gchar *home_dir = g_get_home_dir();
807 gint dir_malloc_number = strlen(home_dir)+ 10;
808 gchar *default_dir = malloc(dir_malloc_number*sizeof(gchar *));
809 g_snprintf(default_dir, dir_malloc_number, "%s\\Desktop",home_dir);
810
811 //see if the directory exists
812 struct stat buffer;
813 gint status = g_stat(default_dir, &buffer);
814 if ((status == 0) && (S_ISDIR(buffer.st_mode) == 0))
815 {
816 g_snprintf(default_dir, dir_malloc_number, "%s",home_dir);
817 }
818#else
819 const gchar *default_dir = g_get_home_dir();
820#endif
821
822 g_key_file_set_string(my_key_file, "split", "save_path", default_dir);
823 g_key_file_set_comment(my_key_file, "split", "save_path",
824 "\n this is the path where you will find your split files ",
825 NULL);
826
827#ifdef __WIN32__
828 g_free(default_dir);
829#endif
830 }
831
832 //type of split: split mode
833 if (!g_key_file_has_key(my_key_file, "split", "split_mode",NULL))
834 {
835 g_key_file_set_integer(my_key_file, "split", "split_mode", 3);
836 g_key_file_set_comment(my_key_file, "split", "split_mode",
837 "\n 0 - error mode, 1 - wrap mode, 2 - time mode, 3 - normal mode, 4 - equal time tracks",
838 NULL);
839 }
840
841 //type of split: time value
842 if (!g_key_file_has_key(my_key_file, "split", "split_mode_time_value",NULL))
843 {
844 g_key_file_set_integer(my_key_file, "split", "split_mode_time_value", 60);
845 g_key_file_set_comment(my_key_file, "split", "split_mode_time_value",
846 "\n value in seconds to split every X seconds (for the time split)", NULL);
847 }
848
849 //equal time tracks
850 if (!g_key_file_has_key(my_key_file, "split", "split_mode_equal_time_tracks",NULL))
851 {
852 g_key_file_set_integer(my_key_file, "split", "split_mode_equal_time_tracks", 10);
853 g_key_file_set_comment(my_key_file, "split", "split_mode_equal_time_tracks",
854 "\n number of tracks when to split in X tracks (for the equal time tracks split)", NULL);
855 }
856
857 gchar *key_data = g_key_file_to_data(my_key_file, NULL, NULL);
858
859 FILE *preferences_file = fopen(filename,"w");
860 g_fprintf(preferences_file,"%s", key_data);
861 fclose(preferences_file);
862
863 if (filename)
864 {
865 g_free(filename);
866 }
867
868 g_free(key_data);
869 g_key_file_free(my_key_file);
870}
871
876static void check_pref_file_and_write_default(ui_state *ui)
877{
878 gchar *pref_file = get_preferences_filename();
879
880 struct stat buffer;
881 gint status = stat(pref_file, &buffer);
882 if ((status == 0) &&
883 (S_ISREG(buffer.st_mode) == 0) &&
884 (S_ISDIR(buffer.st_mode) != 0))
885 {
886 size_t malloc_number = strlen(pref_file) + 5;
887 gchar *backup_dir = malloc(malloc_number * sizeof(gchar));
888 snprintf(backup_dir, malloc_number, "%s%s", pref_file, ".bak");
889 g_rename(pref_file, backup_dir);
890 g_free(backup_dir);
891 }
892
893 if (pref_file) { g_free(pref_file); }
894
895 write_default_preferences_file(ui);
896}
897
898static void pm_free_spinner_int_preferences(GArray *spinner_int_preferences)
899{
900 gint i = 0;
901 for (i = 0; i < spinner_int_preferences->len; i++)
902 {
903 spinner_int_preference preference =
904 g_array_index(spinner_int_preferences, spinner_int_preference, i);
905
906 g_free(preference.main_key);
907 preference.main_key = NULL;
908
909 g_free(preference.second_key);
910 preference.second_key = NULL;
911 }
912
913 g_array_free(spinner_int_preferences, TRUE);
914}
915
916static void pm_free_range_preferences(GArray *range_preferences)
917{
918 gint i = 0;
919 for (i = 0; i < range_preferences->len; i++)
920 {
921 spinner_int_preference preference =
922 g_array_index(range_preferences, spinner_int_preference, i);
923
924 g_free(preference.main_key);
925 preference.main_key = NULL;
926
927 g_free(preference.second_key);
928 preference.second_key = NULL;
929 }
930
931 g_array_free(range_preferences, TRUE);
932}
933
934static void pm_load_spinner_int_preferences(GKeyFile *key_file, preferences_state *pm)
935{
936 GArray *spinner_int_preferences = pm->spinner_int_preferences;
937
938 gint i = 0;
939 for (i = 0; i < spinner_int_preferences->len; i++)
940 {
941 spinner_int_preference preference =
942 g_array_index(spinner_int_preferences, spinner_int_preference, i);
943
944 gint value =
945 g_key_file_get_integer(key_file, preference.main_key, preference.second_key, NULL);
946 gtk_spin_button_set_value(GTK_SPIN_BUTTON(preference.spinner), value);
947 preference.update_spinner_value_cb(preference.spinner, preference.user_data_for_cb);
948 }
949}
950
951static void pm_save_spinner_int_preferences(GKeyFile *key_file, preferences_state *pm)
952{
953 GArray *spinner_int_preferences = pm->spinner_int_preferences;
954
955 gint i = 0;
956 for (i = 0; i < spinner_int_preferences->len; i++)
957 {
958 spinner_int_preference preference =
959 g_array_index(spinner_int_preferences, spinner_int_preference, i);
960
961 g_key_file_set_integer(key_file, preference.main_key, preference.second_key,
962 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(preference.spinner)));
963 }
964}
965
966static void pm_write_default_spinner_int_preferences(GKeyFile *key_file, preferences_state *pm)
967{
968 GArray *spinner_int_preferences = pm->spinner_int_preferences;
969
970 gint i = 0;
971 for (i = 0; i < spinner_int_preferences->len; i++)
972 {
973 spinner_int_preference preference =
974 g_array_index(spinner_int_preferences, spinner_int_preference, i);
975
976 if (!g_key_file_has_key(key_file, preference.main_key, preference.second_key, NULL))
977 {
978 g_key_file_set_integer(key_file, preference.main_key, preference.second_key,
979 preference.default_value);
980 }
981 }
982}
983
984static void pm_load_range_preferences(GKeyFile *key_file, preferences_state *pm)
985{
986 GArray *range_preferences = pm->range_preferences;
987
988 gint i = 0;
989 for (i = 0; i < range_preferences->len; i++)
990 {
991 range_preference preference = g_array_index(range_preferences, range_preference, i);
992
993 gint value =
994 g_key_file_get_integer(key_file, preference.main_key, preference.second_key, NULL);
995
996 gtk_range_set_value(GTK_RANGE(preference.range), value);
997 GtkAdjustment *adj = gtk_range_get_adjustment(GTK_RANGE(preference.range));
998 preference.update_adjustment_value(adj, preference.user_data_for_cb);
999 }
1000}
1001
1002static void pm_save_range_preferences(GKeyFile *key_file, preferences_state *pm)
1003{
1004 GArray *range_preferences = pm->range_preferences;
1005
1006 gint i = 0;
1007 for (i = 0; i < range_preferences->len; i++)
1008 {
1009 range_preference preference = g_array_index(range_preferences, range_preference, i);
1010
1011 GtkAdjustment *adj = gtk_range_get_adjustment(GTK_RANGE(preference.range));
1012 g_key_file_set_integer(key_file, preference.main_key, preference.second_key,
1013 (gint)gtk_adjustment_get_value(adj));
1014 }
1015}
1016
1017static void pm_write_default_range_preferences(GKeyFile *key_file, preferences_state *pm)
1018{
1019 GArray *range_preferences = pm->range_preferences;
1020
1021 gint i = 0;
1022 for (i = 0; i < range_preferences->len; i++)
1023 {
1024 range_preference preference = g_array_index(range_preferences, range_preference, i);
1025
1026 if (!g_key_file_has_key(key_file, preference.main_key, preference.second_key, NULL))
1027 {
1028 g_key_file_set_integer(key_file, preference.main_key, preference.second_key,
1029 preference.default_value);
1030 }
1031 }
1032}
1033
load_preferences
void load_preferences(ui_state *ui)
Read the preferences from the preferences file.
Definition preferences_manager.c:202
get_preferences_filename
gchar * get_preferences_filename()
Get the name of the preferences file.
Definition preferences_manager.c:186
get_output_directory
gchar * get_output_directory(ui_state *ui)
Get the name of the output directory.
Definition preferences_window.c:189
get_checked_language
GString * get_checked_language(ui_state *ui)
Returns the selected language.
Definition preferences_window.c:50
get_checked_output_radio_box
gboolean get_checked_output_radio_box(ui_state *ui)
returns the checked output radio box
Definition preferences_window.c:71
get_checked_tags_version_radio_box
gint get_checked_tags_version_radio_box(gui_state *gui)
returns the checked tags radio box
Definition preferences_window.c:90
select_split_mode
void select_split_mode(int split_mode, ui_state *ui)
Set the split mode.
Definition split_mode_window.c:62
preferences_state
Definition ui_types.h:69
range_preference
Definition ui_types.h:60
spinner_int_preference
Definition ui_types.h:51
ui_main_window
Definition ui_types.h:82
ui_state
Definition ui_types.h:509