mp3splt-gtk: src/import.c Source File

mp3splt-gtk 0.9.3.1519

Loading...
Searching...
No Matches
import.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-2010 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 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28 * USA.
29 *
30 *********************************************************/
31
32/*!********************************************************
33 * \file
34 * The magic behind the splitpoint input
35 *
36 * All functions that are needed in order to read in
37 * cddb, cue or similar files.
38 *********************************************************/
39
40#include "import.h"
41#include "ui_types.h"
42
43static void set_import_filters(GtkFileChooser *chooser);
44static void build_import_filter(GtkFileChooser *chooser,
45 const gchar *filter_name, const gchar *filter_pattern,
46 const gchar *filter_pattern_upper,
47 GList **filters, GtkFileFilter *all_filter);
48static gpointer add_audacity_labels_splitpoints(ui_with_fname *ui_fname);
49static gpointer add_cddb_splitpoints(ui_with_fname *ui_fname);
50static gpointer add_cue_splitpoints(ui_with_fname *ui_fname);
51static gpointer add_plugin_internal_cue_splitpoints(ui_with_fname *ui_fname);
52
54 void import_event(GtkWidget *widget, ui_state *ui)
55{
56 GtkWidget *file_chooser =
57 gtk_file_chooser_dialog_new(_("Choose file to import"),
58 NULL,
59 GTK_FILE_CHOOSER_ACTION_OPEN,
60 _("_Cancel"),
61 GTK_RESPONSE_CANCEL,
62 _("_Open"),
63 GTK_RESPONSE_ACCEPT,
64 NULL);
65
66 wh_set_browser_directory_handler(ui, file_chooser);
67 set_import_filters(GTK_FILE_CHOOSER(file_chooser));
68
69 if (gtk_dialog_run(GTK_DIALOG(file_chooser)) == GTK_RESPONSE_ACCEPT)
70 {
71 gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser));
72
73 import_file(filename, ui, TRUE, TRUE);
74
75 g_free(filename);
76 filename = NULL;
77
78 remove_status_message(ui->gui);
79 }
80
81 gtk_widget_destroy(file_chooser);
82}
83
84static ui_with_fname *create_ui_with_fname(ui_state *ui, const gchar *filename, gboolean show_errors)
85{
86 ui_with_fname *ui_fname = g_malloc0(sizeof(ui_with_fname));
87 ui_fname->ui = ui;
88 ui_fname->fname = strdup(filename);
89 ui_fname->is_checked_output_radio_box = get_checked_output_radio_box(ui);
90 ui_fname->show_errors = show_errors;
91
92 const gchar *output_format = gtk_entry_get_text(GTK_ENTRY(ui->gui->output_entry));
93 if (output_format != NULL)
94 {
95 ui_fname->output_format = g_strdup(output_format);
96 }
97
98 return ui_fname;
99}
100
105 void import_file(gchar *filename, ui_state *ui, gboolean force_import_cue, gboolean show_errors)
106{
107 if (filename == NULL)
108 {
109 return;
110 }
111
112 gchar *ext = strrchr(filename, '.');
113 GString *ext_str = g_string_new(ext);
114
115 g_string_ascii_up(ext_str);
116
117 if (!force_import_cue &&
118 ((strstr(ext_str->str, ".MP3") != NULL) ||
119 (strstr(ext_str->str, ".OGG") != NULL) ||
120 (strstr(ext_str->str, ".FLAC") != NULL)))
121 {
122 file_chooser_ok_event(filename, ui);
123 remove_status_message(ui->gui);
124 }
125 else if ((strstr(ext_str->str, ".CUE") != NULL))
126 {
127 ui_with_fname *ui_fname = create_ui_with_fname(ui, filename, show_errors);
128 create_thread_and_unref((GThreadFunc)add_cue_splitpoints,
129 (gpointer) ui_fname, ui, "import_cue");
130 }
131 else if ((strstr(ext_str->str, ".CDDB") != NULL))
132 {
133 ui_with_fname *ui_fname = create_ui_with_fname(ui, filename, show_errors);
134 create_thread_and_unref((GThreadFunc)add_cddb_splitpoints,
135 (gpointer) ui_fname, ui, "import_cddb");
136 }
137 else if ((strstr(ext_str->str, ".TXT") != NULL))
138 {
139 ui_with_fname *ui_fname = create_ui_with_fname(ui, filename, show_errors);
140 create_thread_and_unref((GThreadFunc)add_audacity_labels_splitpoints,
141 (gpointer) ui_fname, ui, "import_audacity");
142 }
143 else
144 {
145 ui_with_fname *ui_fname = create_ui_with_fname(ui, filename, show_errors);
146 create_thread_and_unref((GThreadFunc)add_plugin_internal_cue_splitpoints,
147 (gpointer) ui_fname, ui, "import_internal");
148 }
149
150 if (ext_str)
151 {
152 g_string_free(ext_str, FALSE);
153 }
154}
155
156void import_cue_file_from_the_configuration_directory(ui_state *ui)
157{
158 gchar *configuration_directory = get_configuration_directory();
159
160 gsize filename_size = strlen(configuration_directory) + 20;
161 gchar *splitpoints_cue_filename = g_malloc(filename_size * sizeof(gchar));
162 g_snprintf(splitpoints_cue_filename, filename_size, "%s%s%s", configuration_directory,
163 G_DIR_SEPARATOR_S, "splitpoints.cue");
164
165 if (file_exists(splitpoints_cue_filename))
166 {
167 ui->importing_cue_from_configuration_directory = TRUE;
168
169 mp3splt_set_int_option(ui->mp3splt_state,
170 SPLT_OPT_CUE_SET_SPLITPOINT_NAMES_FROM_REM_NAME, SPLT_TRUE);
171 import_file(splitpoints_cue_filename, ui, FALSE, FALSE);
172 }
173
174 g_free(configuration_directory);
175 g_free(splitpoints_cue_filename);
176}
177
178static gboolean import_files_to_batch(ui_with_fnames *ui_wf)
179{
180 ui_state *ui = ui_wf->ui;
181 char **splt_filenames = ui_wf->filenames;
182
183 gint i = 0;
184 for (i = 0;i < ui_wf->num_of_filenames;i++)
185 {
186 if (!splt_filenames[i])
187 {
188 continue;
189 }
190
191 multiple_files_add_filename(splt_filenames[i], ui);
192
193 free(splt_filenames[i]);
194 splt_filenames[i] = NULL;
195 }
196
197 free(splt_filenames);
198 splt_filenames = NULL;
199
200 g_free(ui_wf);
201
202 return FALSE;
203}
204
205static gboolean import_files_to_batch_end(ui_state *ui)
206{
207 if (ui->infos->multiple_files_tree_number > 0)
208 {
209 gtk_widget_set_sensitive(ui->gui->multiple_files_remove_all_files_button, TRUE);
210 }
211
212 set_process_in_progress_and_wait_safe(FALSE, ui);
213
214 return FALSE;
215}
216
217static gpointer import_files_to_batch_thread(ui_with_list *ui_wl)
218{
219 ui_state *ui = ui_wl->ui;
220
221 set_process_in_progress_and_wait_safe(TRUE, ui);
222
223 GSList *current_file = ui_wl->list;
224 while (current_file)
225 {
226 gchar *filename = current_file->data;
227
228 int err = SPLT_OK;
229 int num_of_files_found = 0;
230
231 char **splt_filenames =
232 mp3splt_find_filenames(ui->mp3splt_state, filename, &num_of_files_found, &err);
233
234 if (splt_filenames)
235 {
236 ui_with_fnames *ui_wf = g_malloc0(sizeof(ui_with_fnames));
237 ui_wf->ui = ui;
238 ui_wf->filenames = splt_filenames;
239 ui_wf->num_of_filenames = num_of_files_found;
240
241 add_idle(G_PRIORITY_HIGH_IDLE, (GSourceFunc)import_files_to_batch, ui_wf, NULL);
242 }
243
244 g_free(filename);
245 filename = NULL;
246
247 current_file = g_slist_next(current_file);
248 }
249
250 g_slist_free(ui_wl->list);
251
252 g_free(ui_wl);
253
254 add_idle(G_PRIORITY_HIGH_IDLE, (GSourceFunc)import_files_to_batch_end, ui, NULL);
255
256 return NULL;
257}
258
259void import_files_to_batch_and_free(GSList *files, ui_state *ui)
260{
261 ui_with_list *ui_wl = g_malloc0(sizeof(ui_with_list));
262 ui_wl->ui = ui;
263 ui_wl->list = files;
264
265 create_thread_and_unref((GThreadFunc)import_files_to_batch_thread,
266 (gpointer) ui_wl, ui, "import_to_batch");
267}
268
270static void set_import_filters(GtkFileChooser *chooser)
271{
272 GtkFileFilter *all_filter = gtk_file_filter_new();
273 gtk_file_filter_set_name(GTK_FILE_FILTER(all_filter),
274 _("CDDB (*.cddb), CUE (*.cue), Audacity labels (*.txt), internal sheet (*.flac), chapters (*.mp3)"));
275
276 GList *filters = NULL;
277
278 build_import_filter(chooser, _("CDDB files (*.cddb)"), "*.cddb", "*.CDDB",
279 &filters, all_filter);
280 build_import_filter(chooser, _("CUE files (*.cue)"), "*.cue", "*.CUE",
281 &filters, all_filter);
282 build_import_filter(chooser, _("Audacity labels files (*.txt)"), "*.txt", "*.TXT",
283 &filters, all_filter);
284 build_import_filter(chooser, _("FLAC internal sheet (*.flac)"), "*.flac", "*.FLAC",
285 &filters, all_filter);
286 build_import_filter(chooser, _("ID3v2 chapters (*.mp3)"), "*.mp3", "*.MP3",
287 &filters, all_filter);
288 build_import_filter(chooser, _("All files"), "*", NULL, &filters, NULL);
289
290 gtk_file_chooser_add_filter(chooser, all_filter);
291
292 GList *iter = NULL;
293 for (iter = filters; iter != NULL; iter = g_list_next(iter))
294 {
295 gtk_file_chooser_add_filter(chooser, iter->data);
296 }
297}
298
299static void build_import_filter(GtkFileChooser *chooser,
300 const gchar *filter_name, const gchar *filter_pattern,
301 const gchar *filter_pattern_upper,
302 GList **filters, GtkFileFilter *all_filter)
303{
304 GtkFileFilter *filter = gtk_file_filter_new();
305 gtk_file_filter_set_name(GTK_FILE_FILTER(filter), filter_name);
306 gtk_file_filter_add_pattern(GTK_FILE_FILTER(filter), filter_pattern);
307
308 if (filter_pattern_upper)
309 {
310 gtk_file_filter_add_pattern(GTK_FILE_FILTER(filter), filter_pattern_upper);
311 }
312
313 if (all_filter)
314 {
315 gtk_file_filter_add_pattern(GTK_FILE_FILTER(all_filter), filter_pattern);
316 if (filter_pattern_upper)
317 {
318 gtk_file_filter_add_pattern(GTK_FILE_FILTER(all_filter), filter_pattern_upper);
319 }
320 }
321
322 *filters = g_list_append(*filters, filter);
323}
324
325static gboolean add_audacity_labels_splitpoints_end(ui_with_err *ui_err)
326{
327 ui_state *ui = ui_err->ui;
328 gint err = ui_err->err;
329
330 if (err >= 0)
331 {
332 update_splitpoints_from_mp3splt_state(ui);
333 }
334
335 print_status_bar_confirmation(err, ui);
336
337 set_process_in_progress_and_wait_safe(FALSE, ui_err->ui);
338
339 g_free(ui_err);
340
341 return FALSE;
342}
343
344static gpointer add_audacity_labels_splitpoints(ui_with_fname *ui_fname)
345{
346 ui_state *ui = ui_fname->ui;
347
348 set_process_in_progress_and_wait_safe(TRUE, ui);
349
350 gchar *filename = ui_fname->fname;
351 if (ui_fname->output_format) { g_free(ui_fname->output_format); }
352 g_free(ui_fname);
353
354 gint err = mp3splt_import(ui->mp3splt_state, AUDACITY_LABELS_IMPORT, filename);
355 g_free(filename);
356
357 ui_with_err *ui_err = g_malloc0(sizeof(ui_with_err));
358 ui_err->ui = ui;
359 ui_err->err = err;
360
361 add_idle(G_PRIORITY_HIGH_IDLE, (GSourceFunc)add_audacity_labels_splitpoints_end,
362 ui_err, NULL);
363
364 return NULL;
365}
366
367static gboolean add_plugin_internal_cue_splitpoints_end(ui_with_err *ui_err)
368{
369 ui_state *ui = ui_err->ui;
370 gint err = ui_err->err;
371
372 if (err >= 0)
373 {
374 update_splitpoints_from_mp3splt_state(ui);
375 }
376
377 print_status_bar_confirmation(err, ui);
378
379 set_process_in_progress_and_wait_safe(FALSE, ui_err->ui);
380
381 g_free(ui_err);
382
383 return FALSE;
384}
385
386static gpointer add_plugin_internal_cue_splitpoints(ui_with_fname *ui_fname)
387{
388 ui_state *ui = ui_fname->ui;
389
390 set_process_in_progress_and_wait_safe(TRUE, ui);
391
392 update_output_options(ui, ui_fname->is_checked_output_radio_box, ui_fname->output_format);
393
394 gchar *filename = ui_fname->fname;
395 if (ui_fname->output_format) { g_free(ui_fname->output_format); }
396 g_free(ui_fname);
397
398 gint err = mp3splt_import(ui->mp3splt_state, PLUGIN_INTERNAL_IMPORT, filename);
399 g_free(filename);
400
401 ui_with_err *ui_err = g_malloc0(sizeof(ui_with_err));
402 ui_err->ui = ui;
403 ui_err->err = err;
404
405 add_idle(G_PRIORITY_HIGH_IDLE, (GSourceFunc)add_plugin_internal_cue_splitpoints_end,
406 ui_err, NULL);
407
408 return NULL;
409}
410
411static gboolean add_cddb_splitpoints_end(ui_with_err *ui_err)
412{
413 ui_state *ui = ui_err->ui;
414 gint err = ui_err->err;
415
416 if (err >= 0)
417 {
418 update_splitpoints_from_mp3splt_state(ui);
419 }
420
421 print_status_bar_confirmation(err, ui);
422
423 set_process_in_progress_and_wait_safe(FALSE, ui);
424
425 g_free(ui_err);
426
427 return FALSE;
428}
429
431static gpointer add_cddb_splitpoints(ui_with_fname *ui_fname)
432{
433 ui_state *ui = ui_fname->ui;
434
435 set_process_in_progress_and_wait_safe(TRUE, ui);
436
437 update_output_options(ui, ui_fname->is_checked_output_radio_box, ui_fname->output_format);
438
439 gchar *filename = ui_fname->fname;
440 if (ui_fname->output_format) { g_free(ui_fname->output_format); }
441 g_free(ui_fname);
442
443 gint err = mp3splt_import(ui->mp3splt_state, CDDB_IMPORT, filename);
444 g_free(filename);
445
446 ui_with_err *ui_err = g_malloc0(sizeof(ui_with_err));
447 ui_err->ui = ui;
448 ui_err->err = err;
449
450 add_idle(G_PRIORITY_HIGH_IDLE, (GSourceFunc)add_cddb_splitpoints_end,
451 ui_err, NULL);
452
453 return NULL;
454}
455
456static gboolean add_cue_splitpoints_end(ui_with_err *ui_err)
457{
458 ui_state *ui = ui_err->ui;
459
460 if (!ui->importing_cue_from_configuration_directory)
461 {
462 long max_point = 0;
463
464 splt_code err = SPLT_OK;
465 splt_points *points = mp3splt_get_splitpoints(ui->mp3splt_state, &err);
466 print_status_bar_confirmation(err, ui);
467 if (points != NULL)
468 {
469 mp3splt_points_init_iterator(points);
470 const splt_point *point = NULL;
471 while ((point = mp3splt_points_next(points)))
472 {
473 long point_value = mp3splt_point_get_value(point);
474 if (point_value > max_point) { max_point = point_value; }
475 }
476 }
477
478 splt_point *splitpoint = mp3splt_point_new(max_point + (100 * 60 * 100), NULL);
479 mp3splt_point_set_name(splitpoint, _("--- last cue splitpoint ---"));
480 mp3splt_append_splitpoint(ui->mp3splt_state, splitpoint);
481 }
482 else
483 {
484 ui->importing_cue_from_configuration_directory = FALSE;
485 }
486
487 if (ui_err->err >= 0)
488 {
489 update_splitpoints_from_mp3splt_state(ui);
490 }
491
492 if (ui_err->show_errors) {
493 print_status_bar_confirmation(ui_err->err, ui);
494 }
495 else {
496 put_status_message("", ui);
497 }
498
499 mp3splt_set_int_option(ui->mp3splt_state,
500 SPLT_OPT_CUE_SET_SPLITPOINT_NAMES_FROM_REM_NAME, SPLT_FALSE);
501
502 //The cue file has provided libmp3splt with a input filename.
503 //But since we use the filename from the gui instead we need to set
504 //the value the gui uses, too, which we do in the next line.
505 const gchar *filename_to_split = mp3splt_get_filename_to_split(ui->mp3splt_state);
506
507 if (file_exists(filename_to_split))
508 {
509 file_chooser_ok_event(filename_to_split, ui);
510 }
511
512 set_process_in_progress_and_wait_safe(FALSE, ui_err->ui);
513
514 g_free(ui_err);
515
516 return FALSE;
517}
518
520static gpointer add_cue_splitpoints(ui_with_fname *ui_fname)
521{
522 ui_state *ui = ui_fname->ui;
523
524 set_process_in_progress_and_wait_safe(TRUE, ui);
525
526 update_output_options(ui, ui_fname->is_checked_output_radio_box, ui_fname->output_format);
527
528 gchar *filename = ui_fname->fname;
529 if (ui_fname->output_format) { g_free(ui_fname->output_format); }
530 g_free(ui_fname);
531
532 mp3splt_set_filename_to_split(ui->mp3splt_state, NULL);
533
534 gint err = mp3splt_import(ui->mp3splt_state, CUE_IMPORT, filename);
535 g_free(filename);
536
537 ui_with_err *ui_err = g_malloc0(sizeof(ui_with_err));
538 ui_err->ui = ui;
539 ui_err->err = err;
540 ui_err->show_errors = ui_fname->show_errors;
541
542 add_idle(G_PRIORITY_HIGH_IDLE, (GSourceFunc)add_cue_splitpoints_end,
543 ui_err, NULL);
544
545 return NULL;
546}
547
update_splitpoints_from_mp3splt_state
void update_splitpoints_from_mp3splt_state(ui_state *ui)
updates the current splitpoints in ui->mp3splt_state
Definition freedb_window.c:405
import_file
void import_file(gchar *filename, ui_state *ui, gboolean force_import_cue, gboolean show_errors)
Handles the import of an input file (audio or splitpoint).
Definition import.c:105
import_event
void import_event(GtkWidget *widget, ui_state *ui)
What happens if the "Import" button is pressed.
Definition import.c:54
print_status_bar_confirmation
void print_status_bar_confirmation(gint error, ui_state *ui)
Output an error message from libmp3splt to the status bar.
Definition main_window.c:1344
remove_status_message
void remove_status_message(gui_state *gui)
Removes status bar message.
Definition main_window.c:251
put_status_message
void put_status_message(const gchar *text, ui_state *ui)
Output a info message to the status message bar.
Definition main_window.c:263
update_output_options
void update_output_options(ui_state *ui, gboolean is_checked_output_radio_box, gchar *output_format)
Update the output options.
Definition options_manager.c:51
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
ui_state
Definition ui_types.h:509
ui_with_err
Definition ui_types.h:531
ui_with_fname
Definition ui_types.h:545
ui_with_fnames
Definition ui_types.h:565
ui_with_list
Definition ui_types.h:560
file_exists
gint file_exists(const gchar *fname)
check if specified file exists
Definition utilities.c:62