mp3splt-gtk

- Main Page
- Related Pages
- Data Structures
- Files

- File List
- Globals

multiple_files_window.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-2013 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 * Batch processing internals
35 *
36 * this file is for management for the the multiple files
37 * mode that currently allows only for batch processing.
38 *********************************************************/
39
40 #include "multiple_files_window.h"
41
42 #define MY_GTK_RESPONSE 200
43
44 static gboolean file_exists_in_multiple_files_table(const gchar *filename, ui_state *ui);
45
47 static GtkTreeModel *create_multiple_files_model()
48 {
49 GtkListStore *model =
50 gtk_list_store_new(MULTIPLE_FILES_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
51 return GTK_TREE_MODEL(model);
52 }
53
54 static GtkTreeView *create_multiple_files_tree()
55 {
56 return GTK_TREE_VIEW(gtk_tree_view_new_with_model(create_multiple_files_model()));
57 }
58
59 static void create_multiple_files_columns(GtkTreeView *multiple_files_tree)
60 {
61 GtkCellRendererText *renderer = GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
62 GtkTreeViewColumn *filename_column = gtk_tree_view_column_new_with_attributes
63 (_("Complete filename"), GTK_CELL_RENDERER(renderer), "text", MULTIPLE_COL_FILENAME, NULL);
64 gtk_tree_view_insert_column(multiple_files_tree,
65 GTK_TREE_VIEW_COLUMN(filename_column), MULTIPLE_COL_FILENAME);
66
67 gtk_tree_view_column_set_alignment(GTK_TREE_VIEW_COLUMN(filename_column), 0.5);
68 gtk_tree_view_column_set_sizing(GTK_TREE_VIEW_COLUMN(filename_column),
69 GTK_TREE_VIEW_COLUMN_AUTOSIZE);
70 gtk_tree_view_column_set_sort_column_id(filename_column, MULTIPLE_COL_FILENAME);
71 }
72
73 static void multiple_files_open_button_event(GtkWidget *widget, gpointer data)
74 {
75 gtk_dialog_response(GTK_DIALOG(data), MY_GTK_RESPONSE);
76 }
77
78 void multiple_files_add_button_event(GtkWidget *widget, ui_state *ui)
79 {
80 GtkWidget *file_chooser = gtk_file_chooser_dialog_new(_("Choose files or directories"),
81 NULL,
82 GTK_FILE_CHOOSER_ACTION_OPEN,
83 GTK_STOCK_CANCEL,
84 GTK_RESPONSE_CANCEL,
85 NULL);
86
87 wh_set_browser_directory_handler(ui, file_chooser);
88
89 GtkWidget *button = gtk_dialog_add_button(GTK_DIALOG(file_chooser),
90 GTK_STOCK_ADD, MY_GTK_RESPONSE);
91 gtk_button_set_use_stock(GTK_BUTTON(button), TRUE);
92 g_signal_connect(G_OBJECT(button), "clicked",
93 G_CALLBACK(multiple_files_open_button_event), file_chooser);
94 g_signal_connect(G_OBJECT(file_chooser), "file-activated",
95 G_CALLBACK(multiple_files_open_button_event), file_chooser);
96
97 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(file_chooser), TRUE);
98
99 GtkFileFilter *our_filter = gtk_file_filter_new();
100 gtk_file_filter_set_name(our_filter, _("mp3, ogg vorbis and flac files (*.mp3 *.ogg *.flac)"));
101 gtk_file_filter_add_pattern(our_filter, "*.mp3");
102 gtk_file_filter_add_pattern(our_filter, "*.MP3");
103 gtk_file_filter_add_pattern(our_filter, "*.ogg");
104 gtk_file_filter_add_pattern(our_filter, "*.OGG");
105 gtk_file_filter_add_pattern(our_filter, "*.flac");
106 gtk_file_filter_add_pattern(our_filter, "*.FLAC");
107 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), our_filter);
108
109 our_filter = gtk_file_filter_new();
110 gtk_file_filter_set_name(our_filter, _("mp3 files (*.mp3)"));
111 gtk_file_filter_add_pattern(our_filter, "*.mp3");
112 gtk_file_filter_add_pattern(our_filter, "*.MP3");
113 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), our_filter);
114
115 our_filter = gtk_file_filter_new();
116 gtk_file_filter_set_name(our_filter, _("ogg vorbis files (*.ogg)"));
117 gtk_file_filter_add_pattern(our_filter, "*.ogg");
118 gtk_file_filter_add_pattern(our_filter, "*.OGG");
119 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), our_filter);
120
121 our_filter = gtk_file_filter_new();
122 gtk_file_filter_set_name(our_filter, _("flac files (*.flac)"));
123 gtk_file_filter_add_pattern(our_filter, "*.flac");
124 gtk_file_filter_add_pattern(our_filter, "*.FLAC");
125 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), our_filter);
126
127 //all files filter
128 our_filter = gtk_file_filter_new();
129 gtk_file_filter_set_name(our_filter, _("All Files"));
130 gtk_file_filter_add_pattern(our_filter, "*");
131 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), our_filter);
132
133 if (gtk_dialog_run(GTK_DIALOG(file_chooser)) == MY_GTK_RESPONSE)
134 {
135 GSList *files = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(file_chooser));
136 if (!files)
137 {
138 gtk_widget_destroy(file_chooser);
139 return;
140 }
141
142 import_files_to_batch_and_free(files, ui);
143 }
144
145 gtk_widget_destroy(file_chooser);
146 }
147
148 void multiple_files_add_filename(const gchar *filename, ui_state *ui)
149 {
150 if (file_exists_in_multiple_files_table(filename, ui))
151 {
152 return;
153 }
154
155 GtkTreeModel *model = gtk_tree_view_get_model(ui->gui->multiple_files_tree);
156
157 GtkTreeIter iter;
158 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
159 gtk_list_store_set(GTK_LIST_STORE(model), &iter, MULTIPLE_COL_FILENAME, filename, -1);
160
161 ui->infos->multiple_files_tree_number++;
162 }
163
164 static gboolean file_exists_in_multiple_files_table(const gchar *filename, ui_state *ui)
165 {
166 if (filename == NULL)
167 {
168 return FALSE;
169 }
170
171 GtkTreeModel *model = gtk_tree_view_get_model(ui->gui->multiple_files_tree);
172
173 GtkTreeIter iter;
174 if (!gtk_tree_model_get_iter_first(model, &iter))
175 {
176 return FALSE;
177 }
178
179 while (TRUE)
180 {
181 gchar *fname = NULL;
182 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
183 MULTIPLE_COL_FILENAME, &fname, -1);
184
185 if (strcmp(filename, fname) == 0)
186 {
187 g_free(fname);
188 return TRUE;
189 }
190
191 g_free(fname);
192
193 if (!gtk_tree_model_iter_next(model, &iter))
194 {
195 break;
196 }
197 }
198
199 return FALSE;
200 }
201
202 static void multiple_files_remove_button_event(GtkWidget *widget, ui_state *ui)
203 {
204 GtkTreeModel *model = gtk_tree_view_get_model(ui->gui->multiple_files_tree);
205 GtkTreeSelection *selection = gtk_tree_view_get_selection(ui->gui->multiple_files_tree);
206 GList *selected_list = gtk_tree_selection_get_selected_rows(selection, &model);
207
208 while (g_list_length(selected_list) > 0)
209 {
210 GList *current_element = g_list_last(selected_list);
211 GtkTreePath *path = current_element->data;
212 GtkTreeIter iter;
213 gtk_tree_model_get_iter(model, &iter, path);
214
215 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
216 selected_list = g_list_remove(selected_list, path);
217 ui->infos->multiple_files_tree_number--;
218
219 gtk_tree_path_free(path);
220 }
221
222 if (ui->infos->multiple_files_tree_number == 0)
223 {
224 gtk_widget_set_sensitive(ui->gui->multiple_files_remove_all_files_button, FALSE);
225 }
226
227 gtk_widget_set_sensitive(ui->gui->multiple_files_remove_file_button,FALSE);
228
229 g_list_foreach(selected_list, (GFunc)gtk_tree_path_free, NULL);
230 g_list_free(selected_list);
231 }
232
233 static void multiple_files_remove_all_button_event(GtkWidget *widget, ui_state *ui)
234 {
235 GtkTreeModel *model = gtk_tree_view_get_model(ui->gui->multiple_files_tree);
236 while (ui->infos->multiple_files_tree_number > 0)
237 {
238 GtkTreeIter iter;
239 gtk_tree_model_get_iter_first(model, &iter);
240 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
241 ui->infos->multiple_files_tree_number--;
242 }
243
244 gtk_widget_set_sensitive(ui->gui->multiple_files_remove_all_files_button, FALSE);
245 gtk_widget_set_sensitive(ui->gui->multiple_files_remove_file_button, FALSE);
246 }
247
248 void batch_file_mode_split_button_event(GtkWidget *widget, ui_state *ui)
249 {
250 set_split_file_mode_safe(FILE_MODE_MULTIPLE, ui);
251 split_button_event(widget, ui);
252 }
253
254 static GtkWidget *create_multiple_files_buttons_hbox(ui_state *ui)
255 {
256 GtkWidget *hbox = wh_hbox_new();
257
258 //button for adding file(s)
259 GtkWidget *multiple_files_add_button =
260 wh_create_cool_button(GTK_STOCK_DIRECTORY, _("_Add files or directories"), FALSE);
261 gtk_widget_set_tooltip_text(multiple_files_add_button, _("Only the supported file types are added"));
262 gtk_box_pack_start(GTK_BOX(hbox), multiple_files_add_button, FALSE, FALSE, 5);
263 gtk_widget_set_sensitive(multiple_files_add_button, TRUE);
264 g_signal_connect(G_OBJECT(multiple_files_add_button), "clicked",
265 G_CALLBACK(multiple_files_add_button_event), ui);
266
267 //button for removing a file
268 GtkWidget *multiple_files_remove_file_button =
269 wh_create_cool_button(GTK_STOCK_REMOVE, _("_Remove selected"),FALSE);
270 ui->gui->multiple_files_remove_file_button = multiple_files_remove_file_button;
271 gtk_box_pack_start(GTK_BOX(hbox),
272 multiple_files_remove_file_button, FALSE, FALSE, 5);
273 gtk_widget_set_sensitive(multiple_files_remove_file_button, FALSE);
274 g_signal_connect(G_OBJECT(multiple_files_remove_file_button), "clicked",
275 G_CALLBACK(multiple_files_remove_button_event), ui);
276
277 //button for removing a file
278 GtkWidget *multiple_files_remove_all_files_button =
279 wh_create_cool_button(GTK_STOCK_CLEAR, _("R_emove all"),FALSE);
280 ui->gui->multiple_files_remove_all_files_button = multiple_files_remove_all_files_button;
281 gtk_box_pack_start(GTK_BOX(hbox), multiple_files_remove_all_files_button,
282 FALSE, FALSE, 5);
283 gtk_widget_set_sensitive(multiple_files_remove_all_files_button, FALSE);
284 g_signal_connect(G_OBJECT(multiple_files_remove_all_files_button), "clicked",
285 G_CALLBACK(multiple_files_remove_all_button_event), ui);
286
287 GtkWidget *split_button = wh_create_cool_button(GTK_STOCK_EXECUTE,_("Batch split !"), FALSE);
288 gtk_widget_set_tooltip_text(split_button, _("Split all the files"));
289 g_signal_connect(G_OBJECT(split_button), "clicked",
290 G_CALLBACK(batch_file_mode_split_button_event), ui);
291 gtk_box_pack_end(GTK_BOX(hbox), split_button, FALSE, FALSE, 4);
292
293 return hbox;
294 }
295
296 static void multiple_files_selection_changed(GtkTreeSelection *selec, ui_state *ui)
297 {
298 GtkTreeModel *model = gtk_tree_view_get_model(ui->gui->multiple_files_tree);
299 GtkTreeSelection *selection = gtk_tree_view_get_selection(ui->gui->multiple_files_tree);
300 GList *selected_list = gtk_tree_selection_get_selected_rows(selection, &model);
301
302 if (g_list_length(selected_list) > 0)
303 {
304 gtk_widget_set_sensitive(ui->gui->multiple_files_remove_file_button, TRUE);
305 }
306 else
307 {
308 gtk_widget_set_sensitive(ui->gui->multiple_files_remove_file_button, FALSE);
309 }
310 }
311
312 GtkWidget *create_multiple_files_component(ui_state *ui)
313 {
314 GtkWidget *vbox = wh_vbox_new();
315
316 GtkTreeView *multiple_files_tree = create_multiple_files_tree();
317 dnd_add_drag_data_received_to_widget(GTK_WIDGET(multiple_files_tree), DND_BATCH_MODE_AUDIO_FILES, ui);
318 ui->gui->multiple_files_tree = multiple_files_tree;
319
320 GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
321 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_NONE);
322 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolled_window),
323 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
324 gtk_box_pack_end(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 2);
325
326 //create columns
327 create_multiple_files_columns(multiple_files_tree);
328
329 //add the tree to the scrolled window
330 gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(multiple_files_tree));
331
332 //selection for the tree
333 GtkTreeSelection *multiple_files_tree_selection = gtk_tree_view_get_selection(multiple_files_tree);
334 g_signal_connect(G_OBJECT(multiple_files_tree_selection), "changed",
335 G_CALLBACK(multiple_files_selection_changed), ui);
336 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(multiple_files_tree_selection),
337 GTK_SELECTION_MULTIPLE);
338
339 GtkWidget *buttons_hbox = create_multiple_files_buttons_hbox(ui);
340 gtk_box_pack_start(GTK_BOX(vbox), buttons_hbox, FALSE, FALSE, 2);
341
342 return vbox;
343 }
344
ui_state
Definition: ui_types.h:485
split_button_event
void split_button_event(GtkWidget *widget, ui_state *ui)
event for the split button
Definition: main_window.c:428
wh_create_cool_button
GtkWidget * wh_create_cool_button(gchar *stock_id, gchar *label_text, gint toggle_or_not)
creates a cool button with image from stock
Definition: widgets_helper.c:348