mp3splt-gtk: src/multiple_files_window.c Source File

mp3splt-gtk 0.9.3.1519

Loading...
Searching...
No Matches
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-2014 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
44static gboolean file_exists_in_multiple_files_table(const gchar *filename, ui_state *ui);
45
47static 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
54static GtkTreeView *create_multiple_files_tree()
55{
56 return GTK_TREE_VIEW(gtk_tree_view_new_with_model(create_multiple_files_model()));
57}
58
59static 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
73static void multiple_files_open_button_event(GtkWidget *widget, gpointer data)
74{
75 gtk_dialog_response(GTK_DIALOG(data), MY_GTK_RESPONSE);
76}
77
78void 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 _("_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), "Add", MY_GTK_RESPONSE);
90 g_signal_connect(G_OBJECT(button), "clicked",
91 G_CALLBACK(multiple_files_open_button_event), file_chooser);
92 g_signal_connect(G_OBJECT(file_chooser), "file-activated",
93 G_CALLBACK(multiple_files_open_button_event), file_chooser);
94
95 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(file_chooser), TRUE);
96
97 GtkFileFilter *our_filter = gtk_file_filter_new();
98 gtk_file_filter_set_name(our_filter, _("mp3, ogg vorbis and flac files (*.mp3 *.ogg *.flac)"));
99 gtk_file_filter_add_pattern(our_filter, "*.mp3");
100 gtk_file_filter_add_pattern(our_filter, "*.MP3");
101 gtk_file_filter_add_pattern(our_filter, "*.ogg");
102 gtk_file_filter_add_pattern(our_filter, "*.OGG");
103 gtk_file_filter_add_pattern(our_filter, "*.flac");
104 gtk_file_filter_add_pattern(our_filter, "*.FLAC");
105 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), our_filter);
106
107 our_filter = gtk_file_filter_new();
108 gtk_file_filter_set_name(our_filter, _("mp3 files (*.mp3)"));
109 gtk_file_filter_add_pattern(our_filter, "*.mp3");
110 gtk_file_filter_add_pattern(our_filter, "*.MP3");
111 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), our_filter);
112
113 our_filter = gtk_file_filter_new();
114 gtk_file_filter_set_name(our_filter, _("ogg vorbis files (*.ogg)"));
115 gtk_file_filter_add_pattern(our_filter, "*.ogg");
116 gtk_file_filter_add_pattern(our_filter, "*.OGG");
117 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), our_filter);
118
119 our_filter = gtk_file_filter_new();
120 gtk_file_filter_set_name(our_filter, _("flac files (*.flac)"));
121 gtk_file_filter_add_pattern(our_filter, "*.flac");
122 gtk_file_filter_add_pattern(our_filter, "*.FLAC");
123 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), our_filter);
124
125 //all files filter
126 our_filter = gtk_file_filter_new();
127 gtk_file_filter_set_name(our_filter, _("All Files"));
128 gtk_file_filter_add_pattern(our_filter, "*");
129 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), our_filter);
130
131 if (gtk_dialog_run(GTK_DIALOG(file_chooser)) == MY_GTK_RESPONSE)
132 {
133 GSList *files = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(file_chooser));
134 if (!files)
135 {
136 gtk_widget_destroy(file_chooser);
137 return;
138 }
139
140 import_files_to_batch_and_free(files, ui);
141 }
142
143 gtk_widget_destroy(file_chooser);
144}
145
146void multiple_files_add_filename(const gchar *filename, ui_state *ui)
147{
148 if (file_exists_in_multiple_files_table(filename, ui))
149 {
150 return;
151 }
152
153 GtkTreeModel *model = gtk_tree_view_get_model(ui->gui->multiple_files_tree);
154
155 GtkTreeIter iter;
156 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
157 gtk_list_store_set(GTK_LIST_STORE(model), &iter, MULTIPLE_COL_FILENAME, filename, -1);
158
159 ui->infos->multiple_files_tree_number++;
160}
161
162static gboolean file_exists_in_multiple_files_table(const gchar *filename, ui_state *ui)
163{
164 if (filename == NULL)
165 {
166 return FALSE;
167 }
168
169 GtkTreeModel *model = gtk_tree_view_get_model(ui->gui->multiple_files_tree);
170
171 GtkTreeIter iter;
172 if (!gtk_tree_model_get_iter_first(model, &iter))
173 {
174 return FALSE;
175 }
176
177 while (TRUE)
178 {
179 gchar *fname = NULL;
180 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
181 MULTIPLE_COL_FILENAME, &fname, -1);
182
183 if (strcmp(filename, fname) == 0)
184 {
185 g_free(fname);
186 return TRUE;
187 }
188
189 g_free(fname);
190
191 if (!gtk_tree_model_iter_next(model, &iter))
192 {
193 break;
194 }
195 }
196
197 return FALSE;
198}
199
200static void multiple_files_remove_button_event(GtkWidget *widget, ui_state *ui)
201{
202 GtkTreeModel *model = gtk_tree_view_get_model(ui->gui->multiple_files_tree);
203 GtkTreeSelection *selection = gtk_tree_view_get_selection(ui->gui->multiple_files_tree);
204 GList *selected_list = gtk_tree_selection_get_selected_rows(selection, &model);
205
206 while (g_list_length(selected_list) > 0)
207 {
208 GList *current_element = g_list_last(selected_list);
209 GtkTreePath *path = current_element->data;
210 GtkTreeIter iter;
211 gtk_tree_model_get_iter(model, &iter, path);
212
213 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
214 selected_list = g_list_remove(selected_list, path);
215 ui->infos->multiple_files_tree_number--;
216
217 gtk_tree_path_free(path);
218 }
219
220 if (ui->infos->multiple_files_tree_number == 0)
221 {
222 gtk_widget_set_sensitive(ui->gui->multiple_files_remove_all_files_button, FALSE);
223 }
224
225 gtk_widget_set_sensitive(ui->gui->multiple_files_remove_file_button,FALSE);
226
227 g_list_foreach(selected_list, (GFunc)gtk_tree_path_free, NULL);
228 g_list_free(selected_list);
229}
230
231static void multiple_files_remove_all_button_event(GtkWidget *widget, ui_state *ui)
232{
233 GtkTreeModel *model = gtk_tree_view_get_model(ui->gui->multiple_files_tree);
234 while (ui->infos->multiple_files_tree_number > 0)
235 {
236 GtkTreeIter iter;
237 gtk_tree_model_get_iter_first(model, &iter);
238 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
239 ui->infos->multiple_files_tree_number--;
240 }
241
242 gtk_widget_set_sensitive(ui->gui->multiple_files_remove_all_files_button, FALSE);
243 gtk_widget_set_sensitive(ui->gui->multiple_files_remove_file_button, FALSE);
244}
245
246void batch_file_mode_split_button_event(GtkWidget *widget, ui_state *ui)
247{
248 set_split_file_mode(FILE_MODE_MULTIPLE, ui);
249 split_button_event(widget, ui);
250}
251
252static GtkWidget *create_multiple_files_buttons_hbox(ui_state *ui)
253{
254 GtkWidget *hbox = wh_hbox_new();
255
256 //button for adding file(s)
257 GtkWidget *multiple_files_add_button =
258 wh_create_cool_button("folder", _("_Add files or directories"), FALSE);
259 gtk_widget_set_tooltip_text(multiple_files_add_button, _("Only the supported file types are added"));
260 gtk_box_pack_start(GTK_BOX(hbox), multiple_files_add_button, FALSE, FALSE, 5);
261 gtk_widget_set_sensitive(multiple_files_add_button, TRUE);
262 g_signal_connect(G_OBJECT(multiple_files_add_button), "clicked",
263 G_CALLBACK(multiple_files_add_button_event), ui);
264
265 //button for removing a file
266 GtkWidget *multiple_files_remove_file_button =
267 wh_create_cool_button("list-remove", _("_Remove selected"),FALSE);
268 ui->gui->multiple_files_remove_file_button = multiple_files_remove_file_button;
269 gtk_box_pack_start(GTK_BOX(hbox),
270 multiple_files_remove_file_button, FALSE, FALSE, 5);
271 gtk_widget_set_sensitive(multiple_files_remove_file_button, FALSE);
272 g_signal_connect(G_OBJECT(multiple_files_remove_file_button), "clicked",
273 G_CALLBACK(multiple_files_remove_button_event), ui);
274
275 //button for removing a file
276 GtkWidget *multiple_files_remove_all_files_button =
277 wh_create_cool_button("edit-clear", _("R_emove all"),FALSE);
278 ui->gui->multiple_files_remove_all_files_button = multiple_files_remove_all_files_button;
279 gtk_box_pack_start(GTK_BOX(hbox), multiple_files_remove_all_files_button,
280 FALSE, FALSE, 5);
281 gtk_widget_set_sensitive(multiple_files_remove_all_files_button, FALSE);
282 g_signal_connect(G_OBJECT(multiple_files_remove_all_files_button), "clicked",
283 G_CALLBACK(multiple_files_remove_all_button_event), ui);
284
285 GtkWidget *split_button = wh_create_cool_button("system-run",_("Batch split"), FALSE);
286 gtk_widget_set_tooltip_text(split_button, _("Split all the files"));
287 g_signal_connect(G_OBJECT(split_button), "clicked",
288 G_CALLBACK(batch_file_mode_split_button_event), ui);
289 gtk_box_pack_end(GTK_BOX(hbox), split_button, FALSE, FALSE, 4);
290
291 return hbox;
292}
293
294static void multiple_files_selection_changed(GtkTreeSelection *selec, ui_state *ui)
295{
296 GtkTreeModel *model = gtk_tree_view_get_model(ui->gui->multiple_files_tree);
297 GtkTreeSelection *selection = gtk_tree_view_get_selection(ui->gui->multiple_files_tree);
298 GList *selected_list = gtk_tree_selection_get_selected_rows(selection, &model);
299
300 if (g_list_length(selected_list) > 0)
301 {
302 gtk_widget_set_sensitive(ui->gui->multiple_files_remove_file_button, TRUE);
303 }
304 else
305 {
306 gtk_widget_set_sensitive(ui->gui->multiple_files_remove_file_button, FALSE);
307 }
308}
309
310GtkWidget *create_multiple_files_component(ui_state *ui)
311{
312 GtkWidget *vbox = wh_vbox_new();
313
314 GtkTreeView *multiple_files_tree = create_multiple_files_tree();
315 dnd_add_drag_data_received_to_widget(GTK_WIDGET(multiple_files_tree), DND_BATCH_MODE_AUDIO_FILES, ui);
316 ui->gui->multiple_files_tree = multiple_files_tree;
317
318 GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
319 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_NONE);
320 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolled_window),
321 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
322 gtk_box_pack_end(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 2);
323
324 //create columns
325 create_multiple_files_columns(multiple_files_tree);
326
327 //add the tree to the scrolled window
328 gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(multiple_files_tree));
329
330 //selection for the tree
331 GtkTreeSelection *multiple_files_tree_selection = gtk_tree_view_get_selection(multiple_files_tree);
332 g_signal_connect(G_OBJECT(multiple_files_tree_selection), "changed",
333 G_CALLBACK(multiple_files_selection_changed), ui);
334 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(multiple_files_tree_selection),
335 GTK_SELECTION_MULTIPLE);
336
337 GtkWidget *buttons_hbox = create_multiple_files_buttons_hbox(ui);
338 gtk_box_pack_start(GTK_BOX(vbox), buttons_hbox, FALSE, FALSE, 2);
339
340 return vbox;
341}
342
split_button_event
void split_button_event(GtkWidget *widget, ui_state *ui)
event for the split button
Definition main_window.c:371
ui_state
Definition ui_types.h:509