mp3splt-gtk

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

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-2012 Alexandru Munteanu
7 * Contact: io_fx@yahoo.fr
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 <string.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44
45 #include <gtk/gtk.h>
46 #include <glib/gi18n.h>
47 #include <glib/gstdio.h>
48
49 #include <libmp3splt/mp3splt.h>
50
51 #include "player.h"
52 #include "player_tab.h"
53 #include "preferences_tab.h"
54 #include "special_split.h"
55 #include "combo_helper.h"
56 #include "radio_helper.h"
57 #include "ui_manager.h"
58
59 extern GtkWidget *player_combo_box;
60 extern GtkWidget *player_refresh_rate_spinner;
61 extern gint selected_player;
62 extern GList *player_pref_list;
63 extern GtkWidget *radio_button;
64
65 extern GtkWidget *file_mode_radio_button;
66
67 extern GtkWidget *frame_mode;
68 extern GtkWidget *adjust_mode;
69 extern GtkWidget *names_from_filename;
70 extern GtkWidget *spinner_adjust_gap;
71 extern GtkWidget *spinner_adjust_offset;
72 extern GtkWidget *spinner_adjust_threshold;
73 extern GtkWidget *create_dirs_from_output_files;
74 extern GtkWidget *spinner_time;
75 extern GtkWidget *spinner_equal_tracks;
76
77 extern GtkWidget *output_entry;
78 extern GtkWidget *output_label;
79 extern GtkWidget *radio_output;
80 extern GtkWidget *tags_radio;
81 extern GtkWidget *tags_version_radio;
82
83 extern splt_state *the_state;
84
85 extern GtkWidget *replace_underscore_by_space_check_box;
86 extern GtkComboBox *artist_text_properties_combo;
87 extern GtkComboBox *album_text_properties_combo;
88 extern GtkComboBox *title_text_properties_combo;
89 extern GtkComboBox *comment_text_properties_combo;
90 extern GtkComboBox *genre_combo;
91 extern GtkWidget *comment_tag_entry;
92 extern GtkWidget *regex_entry;
93 extern GtkWidget *test_regex_fname_entry;
94
95 extern ui_state *ui;
96
105 gchar *get_preferences_filename()
106 {
107 gchar mp3splt_dir[14] = ".mp3splt-gtk";
108
109 //used to see if the directory exists
110 struct stat buffer;
111 gint status;
112
113 //home directory
114 gchar *home_dir = g_strdup(g_get_home_dir());
115
116 #ifdef __WIN32__
117 //manage c:\ because the gtk dir returns us "c:\"
118 //and the normal directories without the "\"
119 if (home_dir[strlen(home_dir)-1] == '\\')
120 {
121 home_dir[strlen(home_dir)-1] = '\0';
122 }
123 #endif
124
125 gchar *mp3splt_dir_with_path = NULL;
126 gint malloc_number = strlen(home_dir) + strlen(mp3splt_dir)+2;
127 mp3splt_dir_with_path = malloc(malloc_number *sizeof(gchar *));
128 g_snprintf(mp3splt_dir_with_path, malloc_number,
129 "%s%s%s", home_dir,G_DIR_SEPARATOR_S,
130 mp3splt_dir);
131
132 if (home_dir)
133 {
134 g_free(home_dir);
135 home_dir = NULL;
136 }
137
138 gint fname_malloc_number = strlen(mp3splt_dir_with_path)+30;
139 gchar *filename = malloc(fname_malloc_number*sizeof(gchar *));
140
141 status = g_stat(mp3splt_dir_with_path, &buffer);
142 //if it is not a directory
143 if ((status != 0) || (S_ISDIR(buffer.st_mode) == 0))
144 {
145 //if its a file
146 if ((status == 0) && (S_ISREG(buffer.st_mode) != 0))
147 {
148 gchar *backup_file;
149 malloc_number = strlen(mp3splt_dir_with_path)+5;
150 backup_file = malloc(malloc_number*sizeof(gchar *));
151 snprintf(backup_file,malloc_number,
152 "%s%s", mp3splt_dir_with_path,".bak");
153 //we rename the file
154 g_rename(mp3splt_dir_with_path,
155 backup_file);
156 g_free(backup_file);
157 }
158 //if it is not a directory and not a file, we suppose we can
159 //create the directory
160 #ifdef __WIN32__
161 g_mkdir(mp3splt_dir_with_path, 0775);
162 #else
163 g_mkdir(mp3splt_dir_with_path,
164 S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
165 #endif
166 }
167
168 //the config filename+path
169 g_snprintf(filename,fname_malloc_number,
170 "%s%smp3splt-gtk_prefs",
171 mp3splt_dir_with_path, G_DIR_SEPARATOR_S);
172
173 if (mp3splt_dir_with_path)
174 {
175 g_free(mp3splt_dir_with_path);
176 mp3splt_dir_with_path = NULL;
177 }
178
179 return filename;
180 }
181
184 void load_preferences()
185 {
186 GKeyFile *key_file = g_key_file_new();
187
188 gchar *filename = get_preferences_filename();
189
190 //load config
191 g_key_file_load_from_file(key_file, filename, G_KEY_FILE_KEEP_COMMENTS, NULL);
192
193 if (filename)
194 {
195 g_free(filename);
196 filename = NULL;
197 }
198
199 #ifdef __WIN32__
200 //language
201 gchar *file_string = g_key_file_get_string(key_file, "general", "language", NULL);
202 GString *lang = g_string_new(file_string);
203
204 //0 = german, 1 = french, 2 = english
205 gint list_number = 2;
206 if (g_string_equal(lang,g_string_new("de")) ||
207 g_string_equal(lang,g_string_new("de_DE")))
208 {
209 list_number = 0;
210 }
211 else if (g_string_equal(lang, g_string_new("fr")) ||
212 g_string_equal(lang, g_string_new("fr_FR")))
213 {
214 list_number = 1;
215 }
216
217 GSList *radio_button_list =
218 gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio_button));
219 GtkWidget *our_button = (GtkWidget *)
220 g_slist_nth_data(radio_button_list, list_number);
221 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(our_button), TRUE);
222
223 g_free(file_string);
224 g_string_free(lang, TRUE);
225 file_string = NULL;
226 lang = NULL;
227 #endif
228
229 // If outputdirectory_get()!=NULL the path where to output the split file
230 // to has been set from command line
231 if(outputdirectory_get()==NULL)
232 {
233 // No output_path from command-line => get the path from the preferences
234 gchar *save_path = g_key_file_get_string(key_file, "split", "save_path", NULL);
235 {
236 if (save_path != NULL)
237 {
238 outputdirectory_set(save_path);
239 }
240 g_free(save_path);
241 save_path = NULL;
242 }
243 }
244
245 //player
246 gint item = g_key_file_get_integer(key_file, "player", "default_player",NULL);
247 ch_set_active_value(GTK_COMBO_BOX(player_combo_box), item);
248
249 item = g_key_file_get_integer(key_file, "player", "refresh_rate", NULL);
250 gtk_spin_button_set_value(GTK_SPIN_BUTTON(player_refresh_rate_spinner), item);
251 update_timeout_value(NULL, NULL);
252
253 //frame mode
254 item = g_key_file_get_boolean(key_file, "split", "frame_mode", NULL);
255 if (item)
256 {
257 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frame_mode),TRUE);
258 }
259 else
260 {
261 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frame_mode),FALSE);
262 }
263
264 //adjust mode
265 item = g_key_file_get_boolean(key_file, "split", "adjust_mode", NULL);
266 if (item)
267 {
268 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(adjust_mode),TRUE);
269 }
270 else
271 {
272 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(adjust_mode),FALSE);
273 }
274
275 item = g_key_file_get_boolean(key_file, "output", "splitpoint_names_from_filename", NULL);
276 if (item)
277 {
278 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(names_from_filename),TRUE);
279 }
280 else
281 {
282 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(names_from_filename),FALSE);
283 }
284
285 //adjust threshold
286 gfloat item2;
287 item = g_key_file_get_integer(key_file, "split", "adjust_threshold", NULL);
288 item2 = item/100 + (item%100)/100.;
289 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinner_adjust_threshold), item2);
290 //adjust offset
291 item = g_key_file_get_integer(key_file, "split", "adjust_offset", NULL);
292 item2 = item/100 + (item%100)/100.;
293 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinner_adjust_offset),
294 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(spinner_adjust_gap), item);
298
299 //tags options
300 gint tag_pref_file = g_key_file_get_integer(key_file, "split", "tags", NULL);
301 GtkWidget *radio = rh_get_radio_from_value(tags_radio, tag_pref_file);
302 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio), TRUE);
303
304 //replace underscores by spaces
305 item = g_key_file_get_boolean(key_file, "split", "replace_underscore_by_space", NULL);
306 if (item)
307 {
308 gtk_toggle_button_set_active(
309 GTK_TOGGLE_BUTTON(replace_underscore_by_space_check_box), TRUE);
310 }
311 else
312 {
313 gtk_toggle_button_set_active(
314 GTK_TOGGLE_BUTTON(replace_underscore_by_space_check_box), FALSE);
315 }
316
317 //artist text properties
318 item = g_key_file_get_integer(key_file, "split", "artist_text_properties",NULL);
319 if (item)
320 {
321 ch_set_active_value(artist_text_properties_combo, item);
322 }
323 else
324 {
325 ch_set_active_value(artist_text_properties_combo, SPLT_NO_CONVERSION);
326 }
327 //album text properties
328 item = g_key_file_get_integer(key_file, "split", "album_text_properties",NULL);
329 if (item)
330 {
331 ch_set_active_value(album_text_properties_combo, item);
332 }
333 else
334 {
335 ch_set_active_value(album_text_properties_combo, SPLT_NO_CONVERSION);
336 }
337 //title text properties
338 item = g_key_file_get_integer(key_file, "split", "title_text_properties",NULL);
339 if (item)
340 {
341 ch_set_active_value(title_text_properties_combo, item);
342 }
343 else
344 {
345 ch_set_active_value(title_text_properties_combo, SPLT_NO_CONVERSION);
346 }
347 //comment text properties
348 item = g_key_file_get_integer(key_file, "split", "comment_text_properties",NULL);
349 if (item)
350 {
351 ch_set_active_value(comment_text_properties_combo, item);
352 }
353 else
354 {
355 ch_set_active_value(comment_text_properties_combo, SPLT_NO_CONVERSION);
356 }
357
358 //genre
359 gchar *default_genre = g_key_file_get_string(key_file, "split", "genre", NULL);
360 if (default_genre)
361 {
362 ch_set_active_str_value(genre_combo, default_genre);
363 g_free(default_genre);
364 default_genre = NULL;
365 }
366 else
367 {
368 ch_set_active_str_value(genre_combo, SPLT_UNDEFINED_GENRE);
369 }
370
371 //default comment tag
372 gchar *default_comment_tag = g_key_file_get_string(key_file, "split", "default_comment_tag", NULL);
373 if (default_comment_tag)
374 {
375 gtk_entry_set_text(GTK_ENTRY(comment_tag_entry), default_comment_tag);
376 g_free(default_comment_tag);
377 default_comment_tag = NULL;
378 }
379
380 //regexp to parse filename into tags
381 gchar *tags_from_fname_regex =
382 g_key_file_get_string(key_file, "split", "tags_from_filename_regex", NULL);
383 if (tags_from_fname_regex)
384 {
385 gtk_entry_set_text(GTK_ENTRY(regex_entry), tags_from_fname_regex);
386 g_free(tags_from_fname_regex);
387 tags_from_fname_regex = NULL;
388 }
389
390 gchar *test_regex_fname =
391 g_key_file_get_string(key_file, "split", "test_regex_fname", NULL);
392 if (test_regex_fname)
393 {
394 gtk_entry_set_text(GTK_ENTRY(test_regex_fname_entry), test_regex_fname);
395 g_free(test_regex_fname);
396 test_regex_fname = NULL;
397 }
398
399 //tags version
400 tag_pref_file = g_key_file_get_integer(key_file, "split", "tags_version", NULL);
401
402 GSList *tags_version_radio_button_list =
403 gtk_radio_button_get_group(GTK_RADIO_BUTTON(tags_version_radio));
404 GtkWidget *the_selection =
405 (GtkWidget *)g_slist_nth_data(tags_version_radio_button_list, tag_pref_file);
406 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(the_selection), TRUE);
407
408 //default output format or not
409 gint default_output_format = g_key_file_get_boolean(key_file, "output",
410 "default_output_format", NULL);
411 GSList *output_radio_button_list =
412 gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio_output));
413 GtkWidget *our_selection =
414 (GtkWidget *)g_slist_nth_data(output_radio_button_list, default_output_format);
415 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(our_selection), TRUE);
416 if (default_output_format)
417 {
418 gtk_widget_set_sensitive(GTK_WIDGET(output_entry), FALSE);
419 gtk_widget_set_sensitive(GTK_WIDGET(output_label), FALSE);
420 }
421
422 //output format
423 gchar *output_format = g_key_file_get_string(key_file, "output", "output_format", NULL);
424 if (output_format)
425 {
426 gtk_entry_set_text(GTK_ENTRY(output_entry), output_format);
427 g_free(output_format);
428 output_format = NULL;
429 }
430
431 //create directories if needed
432 item = g_key_file_get_boolean(key_file, "output", "create_dirs_if_needed", NULL);
433 if (item)
434 {
435 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(create_dirs_from_output_files), TRUE);
436 }
437 else
438 {
439 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(create_dirs_from_output_files), FALSE);
440 }
441
442 //type of split: split mode
443 gint split_mode = g_key_file_get_integer(key_file, "split", "split_mode", NULL);
444 select_split_mode(split_mode);
445
446 //time value
447 gint time_value = g_key_file_get_integer(key_file, "split",
448 "split_mode_time_value", NULL);
449 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinner_time), time_value);
450
451 //file mode
452 gint file_mode = g_key_file_get_integer(key_file, "split",
453 "file_mode", NULL);
454 GSList *file_mode_radio_button_list =
455 gtk_radio_button_get_group(GTK_RADIO_BUTTON(file_mode_radio_button));
456 our_selection =
457 (GtkWidget *)g_slist_nth_data(file_mode_radio_button_list, file_mode);
458 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(our_selection), TRUE);
459
460 //equal time tracks value
461 gint equal_tracks = g_key_file_get_integer(key_file, "split",
462 "split_mode_equal_time_tracks", NULL);
463 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinner_equal_tracks), equal_tracks);
464
465 gint root_x = g_key_file_get_integer(key_file, "gui", "root_x_position", NULL);
466 gint root_y = g_key_file_get_integer(key_file, "gui", "root_y_position", NULL);
467 if (root_x && root_y)
468 {
469 ui_set_main_win_position(ui, root_x, root_y);
470 }
471
472 gint width = g_key_file_get_integer(key_file, "gui", "width", NULL);
473 gint height = g_key_file_get_integer(key_file, "gui", "height", NULL);
474 if (width && height)
475 {
476 ui_set_main_win_size(ui, width, height);
477 }
478
479 gchar *browser_directory =
480 g_key_file_get_string(key_file, "gui", "browser_directory", NULL);
481 if (browser_directory)
482 {
483 ui_set_browser_directory(ui, browser_directory);
484 g_free(browser_directory);
485 }
486
487 g_key_file_free(key_file);
488 key_file = NULL;
489 }
490
491 /* \brief writes a default configuration file
492
493 Also is used to write good values on a bad existing configuration file
494 */
495 void write_default_preferences_file()
496 {
497 gchar *filename = get_preferences_filename();
498
499 GKeyFile *my_key_file = g_key_file_new();
500 g_key_file_load_from_file(my_key_file, filename, G_KEY_FILE_KEEP_COMMENTS, NULL);
501
502 #ifdef __WIN32__
503 gchar *file_string = NULL;
504
505 //default language
506 if (!g_key_file_has_key(my_key_file, "general", "language",NULL))
507 {
508 g_key_file_set_string(my_key_file, "general", "language", "en");
509 g_key_file_set_comment(my_key_file, "general", "language",
510 "\n language of the gui: en = english, fr = french, de = german",
511 NULL);
512 }
513 //if we have the key, but we have ugly values
514 else
515 {
516 file_string = g_key_file_get_string(my_key_file, "general", "language", NULL);
517 GString * lang_char = g_string_new(file_string);
518
519 if((!g_string_equal(lang_char,g_string_new("en")))
520 &&(!g_string_equal(lang_char, g_string_new("fr")))
521 &&(!g_string_equal(lang_char, g_string_new("fr_FR")))
522 &&(!g_string_equal(lang_char, g_string_new("de")))
523 &&(!g_string_equal(lang_char, g_string_new("de_DE")))
524 )
525 {
526 g_key_file_set_string(my_key_file, "general", "language", "en");
527 g_key_file_set_comment(my_key_file, "general", "language",
528 "\n language of the gui: en = english, fr_FR = french, de_DE = german",
529 NULL);
530 }
531
532 g_free(file_string);
533 g_string_free(lang_char, TRUE);
534 file_string = NULL;
535 lang_char = NULL;
536 }
537 #endif
538
539 //frame mode
540 if (!g_key_file_has_key(my_key_file, "split", "frame_mode",NULL))
541 {
542 g_key_file_set_boolean(my_key_file, "split", "frame_mode", FALSE);
543 }
544
545 //adjust mode
546 if (!g_key_file_has_key(my_key_file, "split", "adjust_mode",NULL))
547 {
548 g_key_file_set_boolean(my_key_file, "split", "adjust_mode", FALSE);
549 }
550
551 gint item;
552 gfloat item2;
553 //adjust threshold
554 if (!g_key_file_has_key(my_key_file, "split", "adjust_threshold",NULL))
555 {
556 g_key_file_set_integer(my_key_file, "split", "adjust_threshold",
557 (int)(SPLT_DEFAULT_PARAM_THRESHOLD * 100));
558 }
559 //if we have the key but we have ugly values
560 else
561 {
562 item = g_key_file_get_integer(my_key_file, "split", "adjust_threshold", NULL);
563 //convert to float
564 item2 = item/100 + (item%100)/100.;
565
566 if ((item2 < -96) || (item2 > 0))
567 {
568 g_key_file_set_integer(my_key_file, "split", "adjust_threshold",
569 (int)(SPLT_DEFAULT_PARAM_THRESHOLD * 100));
570 }
571 }
572
573 //adjust offset
574 if (!g_key_file_has_key(my_key_file, "split", "adjust_offset",NULL))
575 {
576 g_key_file_set_integer(my_key_file, "split", "adjust_offset",
577 (int)(SPLT_DEFAULT_PARAM_OFFSET * 100));
578 }
579 //if we have the key but we have ugly values
580 else
581 {
582 item = g_key_file_get_integer(my_key_file, "split", "adjust_offset", NULL);
583 //convert to float
584 item2 = item/100 + (item%100)/100.;
585
586 //if ugly values
587 if ((item2 < -2) || (item2 > 2))
588 {
589 g_key_file_set_integer(my_key_file, "split", "adjust_offset",
590 (int)(SPLT_DEFAULT_PARAM_OFFSET * 100));
591 }
592 }
593
594 //adjust gap
595 if (!g_key_file_has_key(my_key_file, "split", "adjust_gap",NULL))
596 {
597 g_key_file_set_integer(my_key_file, "split", "adjust_gap", SPLT_DEFAULT_PARAM_GAP);
598 }
599 //if we have the key but we have ugly values
600 else
601 {
602 item = g_key_file_get_integer(my_key_file, "split", "adjust_gap", NULL);
603
604 //if ugly values
605 if ((item < 0) || (item > 2000))
606 {
607 g_key_file_set_integer(my_key_file, "split", "adjust_gap",
608 SPLT_DEFAULT_PARAM_GAP);
609 }
610 }
611
612 //tags options
613 if (!g_key_file_has_key(my_key_file, "split", "tags",NULL))
614 {
615 g_key_file_set_integer(my_key_file, "split", "tags", 1);
616 g_key_file_set_comment(my_key_file, "split", "tags",
617 "\n 0 - No tags, 1 - Default tags, 2 - Original tags, 3 - Tags from filename", NULL);
618 }
619
620 //tags version
621 if (!g_key_file_has_key(my_key_file, "split", "tags_version",NULL))
622 {
623 g_key_file_set_integer(my_key_file, "split", "tags_version", 0);
624 g_key_file_set_comment(my_key_file, "split", "tags_version",
625 "\n 0 - same tags version as the input file, 1 - ID3v1 tags,"
626 " 2 - ID3v2 tags, 3 - ID3v1 & ID3v2 tags",
627 NULL);
628 }
629
630 //default player
631 if (!g_key_file_has_key(my_key_file, "player", "default_player",NULL))
632 {
633 g_key_file_set_integer(my_key_file, "player", "default_player",
634 PLAYER_GSTREAMER);
635 g_key_file_set_comment (my_key_file, "player", "default_player",
636 "\n 1 = PLAYER_AUDACIOUS, 2 = PLAYER_SNACKAMP, 3 = PLAYER_GSTREAMER",
637 NULL);
638 }
639 else
640 {
641 //check if we support selected player
642 gint the_player = g_key_file_get_integer(my_key_file, "player",
643 "default_player", NULL);
644 if (the_player == PLAYER_AUDACIOUS)
645 {
646 #ifdef NO_AUDACIOUS
647 g_key_file_set_integer(my_key_file, "player", "default_player",
648 PLAYER_SNACKAMP);
649 #endif
650 }
651 //if the value do not make sense
652 else if ((the_player > PLAYER_GSTREAMER) || (the_player < 0))
653 {
654 g_key_file_set_integer(my_key_file, "player", "default_player",
655 PLAYER_GSTREAMER);
656 }
657 }
658
659 if (!g_key_file_has_key(my_key_file, "player", "refresh_rate", NULL))
660 {
661 g_key_file_set_integer(my_key_file, "player", "refresh_rate", DEFAULT_TIMEOUT_VALUE);
662 }
663
664 //output format
665 if (!g_key_file_has_key(my_key_file, "output", "output_format",NULL))
666 {
667 g_key_file_set_string(my_key_file, "output", "output_format",
668 SPLT_DEFAULT_OUTPUT);
669 g_key_file_set_comment (my_key_file, "output", "output_format",
670 "\n the output format, contains @a,"
671 "@b, @g, @p, @t and @n, see the program for"
672 " more details",
673 NULL);
674 }
675
676 //default output path boolean
677 if (!g_key_file_has_key(my_key_file, "output", "default_output_format",NULL))
678 {
679 g_key_file_set_boolean(my_key_file, "output", "default_output_format", TRUE);
680 g_key_file_set_comment(my_key_file, "output", "default_output_format",
681 "\n can be true or false"
682 " - if we use the default output or"
683 " not for cddb, cue and freedb search",
684 NULL);
685 }
686
687 //frame mode
688 if (!g_key_file_has_key(my_key_file, "output", "create_dirs_if_needed", NULL))
689 {
690 g_key_file_set_boolean(my_key_file, "output", "create_dirs_if_needed", TRUE);
691 }
692
693 //split save path (output dir)
694 if (!g_key_file_has_key(my_key_file, "split", "save_path",NULL))
695 {
696 #ifdef __WIN32__
697 const gchar *home_dir = g_get_home_dir();
698 gint dir_malloc_number = strlen(home_dir)+ 10;
699 gchar *default_dir = malloc(dir_malloc_number*sizeof(gchar *));
700 g_snprintf(default_dir, dir_malloc_number, "%s\\Desktop",home_dir);
701
702 //see if the directory exists
703 struct stat buffer;
704 gint status = g_stat(default_dir, &buffer);
705 if ((status == 0) && (S_ISDIR(buffer.st_mode) == 0))
706 {
707 g_snprintf(default_dir,dir_malloc_number, "%s",home_dir);
708 }
709 #else
710 const gchar *default_dir = g_get_home_dir();
711 #endif
712
713 g_key_file_set_string(my_key_file, "split", "save_path", default_dir);
714 g_key_file_set_comment(my_key_file, "split", "save_path",
715 "\n this is the path where you will find your split files ",
716 NULL);
717
718 #ifdef __WIN32__
719 g_free(default_dir);
720 #endif
721 }
722
723 //type of split: split mode
724 if (!g_key_file_has_key(my_key_file, "split", "split_mode",NULL))
725 {
726 g_key_file_set_integer(my_key_file, "split", "split_mode", 3);
727 g_key_file_set_comment(my_key_file, "split", "split_mode",
728 "\n 0 - error mode, 1 - wrap mode, 2 - time mode, 3 - normal mode, 4 - equal time tracks",
729 NULL);
730 }
731
732 //type of split: time value
733 if (!g_key_file_has_key(my_key_file, "split", "split_mode_time_value",NULL))
734 {
735 g_key_file_set_integer(my_key_file, "split", "split_mode_time_value", 60);
736 g_key_file_set_comment(my_key_file, "split", "split_mode_time_value",
737 "\n value in seconds to split every X seconds (for the time split)",
738 NULL);
739 }
740
741 //type of split: file mode
742 if (!g_key_file_has_key(my_key_file, "split", "file_mode",NULL))
743 {
744 g_key_file_set_integer(my_key_file, "split", "file_mode", 1);
745 g_key_file_set_comment(my_key_file, "split", "file_mode",
746 "\n 0 - multiple files, 1 - single file",
747 NULL);
748 }
749
750 //equal time tracks
751 if (!g_key_file_has_key(my_key_file, "split", "split_mode_equal_time_tracks",NULL))
752 {
753 g_key_file_set_integer(my_key_file, "split", "split_mode_equal_time_tracks", 10);
754 g_key_file_set_comment(my_key_file, "split", "split_mode_equal_time_tracks",
755 "\n number of tracks when to split in X tracks (for the equal time tracks split)",
756 NULL);
757 }
758
759 gchar *key_data = g_key_file_to_data(my_key_file, NULL, NULL);
760
761 //write content to the preferences file
762 FILE *preferences_file = (FILE *)fopen(filename,"w");
763 g_fprintf(preferences_file,"%s", key_data);
764 fclose(preferences_file);
765 preferences_file = NULL;
766
767 if (filename)
768 {
769 g_free(filename);
770 filename = NULL;
771 }
772
773 g_free(key_data);
774 key_data = NULL;
775 g_key_file_free(my_key_file);
776 }
777
782 void check_pref_file()
783 {
784 //used to see if the file exists
785 struct stat buffer;
786 gint status;
787
788 gchar *pref_file = get_preferences_filename();
789
790 status = stat(pref_file, &buffer);
791 if ((status == 0) &&
792 (S_ISREG(buffer.st_mode) == 0) &&
793 (S_ISDIR(buffer.st_mode) != 0))
794 {
795 //backup the directory
796 gint malloc_number = strlen(pref_file)+5;
797 gchar *backup_dir = malloc(malloc_number * sizeof(gchar *));
798 snprintf(backup_dir,malloc_number,
799 "%s%s",pref_file,".bak");
800 //rename the directory
801 g_rename(pref_file, backup_dir);
802 g_free(backup_dir);
803 backup_dir = NULL;
804 }
805
806 if (pref_file)
807 {
808 g_free(pref_file);
809 pref_file = NULL;
810 }
811
812 write_default_preferences_file();
813 }
814
816 void set_language()
817 {
818 GKeyFile *key_file = g_key_file_new();
819 //filename
820 gchar *filename = get_preferences_filename();
821
822 //load config
823 g_key_file_load_from_file(key_file, filename,
824 G_KEY_FILE_KEEP_COMMENTS,
825 NULL);
826
827 if (filename)
828 {
829 g_free(filename);
830 filename = NULL;
831 }
832
833 gchar *lang = g_key_file_get_string(key_file, "general", "language", NULL);
834
835 //NOTE: current function is only used for windows: code needs cleanup ?
836 #ifdef __WIN32__
837 gchar lang_env[32] = { '\0' };
838 g_snprintf(lang_env, 32, "LANG=%s", lang);
839 putenv(lang_env);
840 #else
841 setenv("LANGUAGE", lang,1);
842 #endif
843
844 //freeing memory
845 g_free(lang);
846 g_key_file_free(key_file);
847 }
848