mp3splt-gtk

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

- File List
- Globals

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: 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 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 <string.h>
41
42 #include <gtk/gtk.h>
43 #include <glib/gi18n.h>
44
45 #include "player_tab.h"
46 #include "main_win.h"
47 #include "freedb_tab.h"
48 #include "import.h"
49 #include "options_manager.h"
50 #include "mp3splt-gtk.h"
51 #include "utilities.h"
52 #include "ui_manager.h"
53 #include "widgets_helper.h"
54
55 extern splt_state *the_state;
56 extern ui_state *ui;
57
58 static void set_import_filters(GtkFileChooser *chooser);
59 static void build_import_filter(GtkFileChooser *chooser,
60 const gchar *filter_name, const gchar *filter_pattern,
61 const gchar *filter_pattern_upper,
62 GList **filters, GtkFileFilter *all_filter);
63 static gpointer add_audacity_labels_splitpoints(gpointer data);
64 static gpointer add_cddb_splitpoints(gpointer data);
65 static gpointer add_cue_splitpoints(gpointer data);
66
68 void import_event(GtkWidget *widget, gpointer *data)
69 {
70 GtkWidget *file_chooser =
71 gtk_file_chooser_dialog_new(_("Choose file to import"),
72 NULL,
73 GTK_FILE_CHOOSER_ACTION_OPEN,
74 GTK_STOCK_CANCEL,
75 GTK_RESPONSE_CANCEL,
76 GTK_STOCK_OPEN,
77 GTK_RESPONSE_ACCEPT,
78 NULL);
79
80 wh_set_browser_directory_handler(ui, file_chooser);
81
82 set_import_filters(GTK_FILE_CHOOSER(file_chooser));
83
84 if (gtk_dialog_run(GTK_DIALOG(file_chooser)) == GTK_RESPONSE_ACCEPT)
85 {
86 gchar *filename =
87 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser));
88
89 handle_import(filename);
90
91 g_free(filename);
92 filename = NULL;
93
94 remove_status_message();
95 }
96
97 gtk_widget_destroy(file_chooser);
98 }
99
104 void handle_import(gchar *filename)
105 {
106 if (filename == NULL)
107 {
108 return;
109 }
110
111 gchar *ext = strrchr(filename, '.');
112 GString *ext_str = g_string_new(ext);
113
114 g_string_ascii_up(ext_str);
115
116 if ((strstr(ext_str->str, ".MP3") != NULL) ||
117 (strstr(ext_str->str, ".OGG") != NULL))
118 {
119 file_chooser_ok_event(filename);
120 remove_status_message();
121 }
122 else if ((strstr(ext_str->str, ".CUE") != NULL))
123 {
124 update_output_options();
125 create_thread(add_cue_splitpoints, strdup(filename), TRUE, NULL);
126 }
127 else if ((strstr(ext_str->str, ".CDDB") != NULL))
128 {
129 update_output_options();
130 create_thread(add_cddb_splitpoints, strdup(filename), TRUE, NULL);
131 }
132 else if ((strstr(ext_str->str, ".TXT") != NULL))
133 {
134 create_thread(add_audacity_labels_splitpoints, strdup(filename), TRUE, NULL);
135 }
136
137 if (ext_str)
138 {
139 g_string_free(ext_str, FALSE);
140 }
141 }
142
144 static void set_import_filters(GtkFileChooser *chooser)
145 {
146 GtkFileFilter *all_filter = gtk_file_filter_new();
147 gtk_file_filter_set_name(GTK_FILE_FILTER(all_filter),
148 _("CDDB (*.cddb), CUE (*.cue), Audacity labels (*.txt)"));
149
150 GList *filters = NULL;
151
152 build_import_filter(chooser, _("CDDB files (*.cddb)"), "*.cddb", "*.CDDB",
153 &filters, all_filter);
154 build_import_filter(chooser, _("CUE files (*.cue)"), "*.cue", "*.CUE",
155 &filters, all_filter);
156 build_import_filter(chooser, _("Audacity labels files (*.txt)"), "*.txt", "*.TXT",
157 &filters, all_filter);
158 build_import_filter(chooser, _("All files"), "*", NULL, &filters, NULL);
159
160 gtk_file_chooser_add_filter(chooser, all_filter);
161
162 GList *iter = NULL;
163 for (iter = filters; iter != NULL; iter = g_list_next(iter))
164 {
165 gtk_file_chooser_add_filter(chooser, iter->data);
166 }
167 }
168
169 static void build_import_filter(GtkFileChooser *chooser,
170 const gchar *filter_name, const gchar *filter_pattern,
171 const gchar *filter_pattern_upper,
172 GList **filters, GtkFileFilter *all_filter)
173 {
174 GtkFileFilter *filter = gtk_file_filter_new();
175 gtk_file_filter_set_name(GTK_FILE_FILTER(filter), filter_name);
176
177 gtk_file_filter_add_pattern(GTK_FILE_FILTER(filter), filter_pattern);
178
179 if (filter_pattern_upper)
180 {
181 gtk_file_filter_add_pattern(GTK_FILE_FILTER(filter), filter_pattern_upper);
182 }
183
184 if (all_filter)
185 {
186 gtk_file_filter_add_pattern(GTK_FILE_FILTER(all_filter), filter_pattern);
187 if (filter_pattern_upper)
188 {
189 gtk_file_filter_add_pattern(GTK_FILE_FILTER(all_filter), filter_pattern_upper);
190 }
191 }
192
193 *filters = g_list_append(*filters, filter);
194 }
195
200 static gpointer add_audacity_labels_splitpoints(gpointer data)
201 {
202 gchar *filename = data;
203
204 gint err = SPLT_OK;
205 mp3splt_put_audacity_labels_splitpoints_from_file(the_state, filename, &err);
206
207 enter_threads();
208
209 if (err >= 0)
210 {
211 update_splitpoints_from_the_state();
212 }
213
214 print_status_bar_confirmation(err);
215
216 exit_threads();
217
218 if (filename)
219 {
220 g_free(filename);
221 filename = NULL;
222 }
223
224 return NULL;
225 }
226
228 static gpointer add_cddb_splitpoints(gpointer data)
229 {
230 gchar *filename = data;
231
232 gint err = SPLT_OK;
233 mp3splt_put_cddb_splitpoints_from_file(the_state, filename, &err);
234
235 enter_threads();
236
237 if (err >= 0)
238 {
239 update_splitpoints_from_the_state();
240 }
241 print_status_bar_confirmation(err);
242
243 exit_threads();
244
245 if (filename)
246 {
247 g_free(filename);
248 filename = NULL;
249 }
250
251 return NULL;
252 }
253
255 static gpointer add_cue_splitpoints(gpointer data)
256 {
257 gchar *filename = data;
258
259 gint err = SPLT_OK;
260 mp3splt_set_filename_to_split(the_state, NULL);
261 mp3splt_put_cue_splitpoints_from_file(the_state, filename, &err);
262
263 enter_threads();
264
265 if (err >= 0)
266 {
267 update_splitpoints_from_the_state();
268 }
269 print_status_bar_confirmation(err);
270
271 // The cue file has provided libmp3splt with a input filename.
272 // But since we use the filename from the gui instead we need to set
273 // the value the gui uses, too, which we do in the next line.
274 char *filename_to_split = mp3splt_get_filename_to_split(the_state);
275 if (is_filee(filename_to_split))
276 {
277 inputfilename_set(filename_to_split);
278 }
279
280 exit_threads();
281 enable_player_buttons();
282
283 return NULL;
284 }
285