mp3splt-gtk
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 
42 static void set_import_filters(GtkFileChooser *chooser);
43 static void build_import_filter(GtkFileChooser *chooser,
44  const gchar *filter_name, const gchar *filter_pattern,
45  const gchar *filter_pattern_upper,
46  GList **filters, GtkFileFilter *all_filter);
47 static gpointer add_audacity_labels_splitpoints(ui_with_fname *ui_fname);
48 static gpointer add_cddb_splitpoints(ui_with_fname *ui_fname);
49 static gpointer add_cue_splitpoints(ui_with_fname *ui_fname);
50 
52 void import_event(GtkWidget *widget, ui_state *ui)
53 {
54  GtkWidget *file_chooser =
55  gtk_file_chooser_dialog_new(_("Choose file to import"),
56  NULL,
57  GTK_FILE_CHOOSER_ACTION_OPEN,
58  GTK_STOCK_CANCEL,
59  GTK_RESPONSE_CANCEL,
60  GTK_STOCK_OPEN,
61  GTK_RESPONSE_ACCEPT,
62  NULL);
63 
64  wh_set_browser_directory_handler(ui, file_chooser);
65  set_import_filters(GTK_FILE_CHOOSER(file_chooser));
66 
67  if (gtk_dialog_run(GTK_DIALOG(file_chooser)) == GTK_RESPONSE_ACCEPT)
68  {
69  gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser));
70 
71  import_file(filename, ui);
72 
73  g_free(filename);
74  filename = NULL;
75 
76  remove_status_message(ui->gui);
77  }
78 
79  gtk_widget_destroy(file_chooser);
80 }
81 
86 void import_file(gchar *filename, ui_state *ui)
87 {
88  if (filename == NULL)
89  {
90  return;
91  }
92 
93  gchar *ext = strrchr(filename, '.');
94  GString *ext_str = g_string_new(ext);
95 
96  g_string_ascii_up(ext_str);
97 
98  if ((strstr(ext_str->str, ".MP3") != NULL) ||
99  (strstr(ext_str->str, ".OGG") != NULL) ||
100  (strstr(ext_str->str, ".FLAC") != NULL))
101  {
102  file_chooser_ok_event(filename, ui);
103  remove_status_message(ui->gui);
104  }
105  else if ((strstr(ext_str->str, ".CUE") != NULL))
106  {
107  ui_with_fname *ui_fname = g_malloc0(sizeof(ui_with_fname));
108  ui_fname->ui = ui;
109  ui_fname->fname = strdup(filename);
110  create_thread_with_fname((GThreadFunc)add_cue_splitpoints, ui_fname);
111  }
112  else if ((strstr(ext_str->str, ".CDDB") != NULL))
113  {
114  ui_with_fname *ui_fname = g_malloc0(sizeof(ui_with_fname));
115  ui_fname->ui = ui;
116  ui_fname->fname = strdup(filename);
117  create_thread_with_fname((GThreadFunc)add_cddb_splitpoints, ui_fname);
118  }
119  else if ((strstr(ext_str->str, ".TXT") != NULL))
120  {
121  ui_with_fname *ui_fname = g_malloc0(sizeof(ui_with_fname));
122  ui_fname->ui = ui;
123  ui_fname->fname = strdup(filename);
124  create_thread_with_fname((GThreadFunc)add_audacity_labels_splitpoints, ui_fname);
125  }
126 
127  if (ext_str)
128  {
129  g_string_free(ext_str, FALSE);
130  }
131 }
132 
133 void import_cue_file_from_the_configuration_directory(ui_state *ui)
134 {
135  gchar *configuration_directory = get_configuration_directory();
136 
137  gsize filename_size = strlen(configuration_directory) + 20;
138  gchar *splitpoints_cue_filename = g_malloc(filename_size * sizeof(gchar));
139  g_snprintf(splitpoints_cue_filename, filename_size, "%s%s%s", configuration_directory,
140  G_DIR_SEPARATOR_S, "splitpoints.cue");
141 
142  if (file_exists(splitpoints_cue_filename))
143  {
144  ui->importing_cue_from_configuration_directory = TRUE;
145 
146  mp3splt_set_int_option(ui->mp3splt_state,
147  SPLT_OPT_CUE_SET_SPLITPOINT_NAMES_FROM_REM_NAME, SPLT_TRUE);
148  import_file(splitpoints_cue_filename, ui);
149  }
150 
151  g_free(configuration_directory);
152  g_free(splitpoints_cue_filename);
153 }
154 
155 void import_files_to_batch_and_free(GSList *files, ui_state *ui)
156 {
157  GSList *current_file = files;
158  while (current_file)
159  {
160  gchar *filename = current_file->data;
161 
162  int err = SPLT_OK;
163  int num_of_files_found = 0;
164 
165  char **splt_filenames =
166  mp3splt_find_filenames(ui->mp3splt_state, filename, &num_of_files_found, &err);
167 
168  if (splt_filenames)
169  {
170  gint i = 0;
171  for (i = 0;i < num_of_files_found;i++)
172  {
173  if (!splt_filenames[i])
174  {
175  continue;
176  }
177 
178  multiple_files_add_filename(splt_filenames[i], ui);
179 
180  free(splt_filenames[i]);
181  splt_filenames[i] = NULL;
182  }
183 
184  free(splt_filenames);
185  splt_filenames = NULL;
186  }
187 
188  g_free(filename);
189  filename = NULL;
190 
191  current_file = g_slist_next(current_file);
192  }
193 
194  g_slist_free(files);
195 
196  if (ui->infos->multiple_files_tree_number > 0)
197  {
198  gtk_widget_set_sensitive(ui->gui->multiple_files_remove_all_files_button, TRUE);
199  }
200 }
201 
203 static void set_import_filters(GtkFileChooser *chooser)
204 {
205  GtkFileFilter *all_filter = gtk_file_filter_new();
206  gtk_file_filter_set_name(GTK_FILE_FILTER(all_filter),
207  _("CDDB (*.cddb), CUE (*.cue), Audacity labels (*.txt)"));
208 
209  GList *filters = NULL;
210 
211  build_import_filter(chooser, _("CDDB files (*.cddb)"), "*.cddb", "*.CDDB",
212  &filters, all_filter);
213  build_import_filter(chooser, _("CUE files (*.cue)"), "*.cue", "*.CUE",
214  &filters, all_filter);
215  build_import_filter(chooser, _("Audacity labels files (*.txt)"), "*.txt", "*.TXT",
216  &filters, all_filter);
217  build_import_filter(chooser, _("All files"), "*", NULL, &filters, NULL);
218 
219  gtk_file_chooser_add_filter(chooser, all_filter);
220 
221  GList *iter = NULL;
222  for (iter = filters; iter != NULL; iter = g_list_next(iter))
223  {
224  gtk_file_chooser_add_filter(chooser, iter->data);
225  }
226 }
227 
228 static void build_import_filter(GtkFileChooser *chooser,
229  const gchar *filter_name, const gchar *filter_pattern,
230  const gchar *filter_pattern_upper,
231  GList **filters, GtkFileFilter *all_filter)
232 {
233  GtkFileFilter *filter = gtk_file_filter_new();
234  gtk_file_filter_set_name(GTK_FILE_FILTER(filter), filter_name);
235  gtk_file_filter_add_pattern(GTK_FILE_FILTER(filter), filter_pattern);
236 
237  if (filter_pattern_upper)
238  {
239  gtk_file_filter_add_pattern(GTK_FILE_FILTER(filter), filter_pattern_upper);
240  }
241 
242  if (all_filter)
243  {
244  gtk_file_filter_add_pattern(GTK_FILE_FILTER(all_filter), filter_pattern);
245  if (filter_pattern_upper)
246  {
247  gtk_file_filter_add_pattern(GTK_FILE_FILTER(all_filter), filter_pattern_upper);
248  }
249  }
250 
251  *filters = g_list_append(*filters, filter);
252 }
253 
254 static gboolean add_audacity_labels_splitpoints_end(ui_with_err *ui_err)
255 {
256  ui_state *ui = ui_err->ui;
257  gint err = ui_err->err;
258 
259  if (err >= 0)
260  {
262  }
263 
265 
266  set_process_in_progress_and_wait_safe(FALSE, ui_err->ui);
267 
268  g_free(ui_err);
269 
270  return FALSE;
271 }
272 
273 static gpointer add_audacity_labels_splitpoints(ui_with_fname *ui_fname)
274 {
275  ui_state *ui = ui_fname->ui;
276 
277  set_process_in_progress_and_wait_safe(TRUE, ui);
278 
279  gchar *filename = ui_fname->fname;
280  g_free(ui_fname);
281 
282  gint err = mp3splt_import(ui->mp3splt_state, AUDACITY_LABELS_IMPORT, filename);
283  g_free(filename);
284 
285  ui_with_err *ui_err = g_malloc0(sizeof(ui_with_err));
286  ui_err->ui = ui;
287  ui_err->err = err;
288 
289  gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE, (GSourceFunc)add_audacity_labels_splitpoints_end,
290  ui_err, NULL);
291 
292  return NULL;
293 }
294 
295 static gboolean add_cddb_splitpoints_end(ui_with_err *ui_err)
296 {
297  ui_state *ui = ui_err->ui;
298  gint err = ui_err->err;
299 
300  if (err >= 0)
301  {
303  }
304 
306 
307  set_process_in_progress_and_wait_safe(FALSE, ui);
308 
309  g_free(ui_err);
310 
311  return FALSE;
312 }
313 
315 static gpointer add_cddb_splitpoints(ui_with_fname *ui_fname)
316 {
317  ui_state *ui = ui_fname->ui;
318 
319  set_process_in_progress_and_wait_safe(TRUE, ui);
320 
321  gchar *filename = ui_fname->fname;
322  g_free(ui_fname);
323 
324  enter_threads();
326  exit_threads();
327 
328  gint err = mp3splt_import(ui->mp3splt_state, CDDB_IMPORT, filename);
329  g_free(filename);
330 
331  ui_with_err *ui_err = g_malloc0(sizeof(ui_with_err));
332  ui_err->ui = ui;
333  ui_err->err = err;
334 
335  gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE, (GSourceFunc)add_cddb_splitpoints_end,
336  ui_err, NULL);
337 
338  return NULL;
339 }
340 
341 static gboolean add_cue_splitpoints_end(ui_with_err *ui_err)
342 {
343  ui_state *ui = ui_err->ui;
344 
345  if (!ui->importing_cue_from_configuration_directory)
346  {
347  splt_point *splitpoint = mp3splt_point_new(600000 - 1, NULL);
348  mp3splt_point_set_name(splitpoint, _("--- last cue splitpoint ---"));
349  mp3splt_append_splitpoint(ui->mp3splt_state, splitpoint);
350  }
351  else
352  {
353  ui->importing_cue_from_configuration_directory = FALSE;
354  }
355 
356  if (ui_err->err >= 0)
357  {
359  }
360  print_status_bar_confirmation(ui_err->err, ui);
361 
362  //The cue file has provided libmp3splt with a input filename.
363  //But since we use the filename from the gui instead we need to set
364  //the value the gui uses, too, which we do in the next line.
365  const gchar *filename_to_split = mp3splt_get_filename_to_split(ui->mp3splt_state);
366  if (file_exists(filename_to_split))
367  {
368  file_chooser_ok_event(filename_to_split, ui);
369  }
370 
371  set_process_in_progress_and_wait_safe(FALSE, ui_err->ui);
372 
373  mp3splt_set_int_option(ui->mp3splt_state,
374  SPLT_OPT_CUE_SET_SPLITPOINT_NAMES_FROM_REM_NAME, SPLT_FALSE);
375 
376  g_free(ui_err);
377 
378  return FALSE;
379 }
380 
382 static gpointer add_cue_splitpoints(ui_with_fname *ui_fname)
383 {
384  ui_state *ui = ui_fname->ui;
385 
386  set_process_in_progress_and_wait_safe(TRUE, ui);
387 
388  gchar *filename = ui_fname->fname;
389  g_free(ui_fname);
390 
391  enter_threads();
393  exit_threads();
394 
395  mp3splt_set_filename_to_split(ui->mp3splt_state, NULL);
396 
397  gint err = mp3splt_import(ui->mp3splt_state, CUE_IMPORT, filename);
398  g_free(filename);
399 
400  ui_with_err *ui_err = g_malloc0(sizeof(ui_with_err));
401  ui_err->ui = ui;
402  ui_err->err = err;
403 
404  gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE, (GSourceFunc)add_cue_splitpoints_end,
405  ui_err, NULL);
406 
407  return NULL;
408 }
409 
void update_output_options(ui_state *ui)
Update the output options.
void import_event(GtkWidget *widget, ui_state *ui)
What happens if the &quot;Import&quot; button is pressed.
Definition: import.c:52
void update_splitpoints_from_mp3splt_state(ui_state *ui)
updates the current splitpoints in ui-&gt;mp3splt_state
void print_status_bar_confirmation(gint error, ui_state *ui)
Output an error message from libmp3splt to the status bar.
Definition: main_window.c:1115
gint file_exists(const gchar *fname)
check if specified file exists
Definition: utilities.c:62
void import_file(gchar *filename, ui_state *ui)
Handles the import of an input file (audio or splitpoint)
Definition: import.c:86
void remove_status_message(gui_state *gui)
Removes status bar message.
Definition: main_window.c:252