mp3splt-gtk
multiple_files.c
Go to the documentation of this file.
00001 /**********************************************************
00002  *
00003  * mp3splt-gtk -- utility based on mp3splt,
00004  *                for mp3/ogg splitting without decoding
00005  *
00006  * Copyright: (C) 2005-2012 Alexandru Munteanu
00007  * Contact: io_fx@yahoo.fr
00008  *
00009  * http://mp3splt.sourceforge.net/
00010  *
00011  *********************************************************/
00012 
00013 /**********************************************************
00014  *
00015  * This program is free software; you can redistribute it and/or
00016  * modify it under the terms of the GNU General Public License
00017  * as published by the Free Software Foundation; either version 2
00018  * of the License, or (at your option) any later version.
00019  *
00020  * This program is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with this program; if not, write to the Free Software
00027  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
00028  * USA.
00029  *
00030  *********************************************************/
00031 
00032 /*!********************************************************
00033  * \file 
00034  * Batch processing internals
00035  *
00036  * this file is for management for the the multiple files
00037  * mode that currently allows only for batch processing.
00038  *********************************************************/
00039 
00040 #include <gtk/gtk.h>
00041 #include <glib/gi18n.h>
00042 #include <libmp3splt/mp3splt.h>
00043 
00044 #include "multiple_files.h"
00045 #include "main_win.h"
00046 #include "ui_manager.h"
00047 #include "widgets_helper.h"
00048 
00049 extern splt_state *the_state;
00050 extern ui_state *ui;
00051 
00052 GtkWidget *multiple_files_tree = NULL;
00053 gint multiple_files_tree_number = 0;
00054 
00055 GtkWidget *multiple_files_remove_file_button = NULL;
00056 GtkWidget *multiple_files_remove_all_files_button = NULL;
00057 
00058 #define MY_GTK_RESPONSE 200
00059 
00061 GtkTreeModel *create_multiple_files_model()
00062 {
00063   GtkListStore *model;
00064 
00065   model = gtk_list_store_new(MULTIPLE_FILES_COLUMNS,
00066                              G_TYPE_STRING,
00067                              G_TYPE_STRING);
00068 
00069   return GTK_TREE_MODEL(model);
00070 }
00071 
00072 GtkTreeView *create_multiple_files_tree()
00073 {
00074   GtkTreeView *tree_view;
00075   GtkTreeModel *model;
00076 
00077   model = (GtkTreeModel *)create_multiple_files_model();
00078   tree_view = (GtkTreeView *)gtk_tree_view_new_with_model(model);
00079 
00080   return tree_view;
00081 }
00082 
00083 void create_multiple_files_columns(GtkTreeView *tree_view)
00084 {
00085   GtkCellRendererText *renderer =
00086     GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new());
00087   GtkTreeViewColumn *filename_column = gtk_tree_view_column_new_with_attributes 
00088     (_("Complete filename"), GTK_CELL_RENDERER(renderer),
00089      "text", MULTIPLE_COL_FILENAME, NULL);
00090   gtk_tree_view_insert_column(GTK_TREE_VIEW(tree_view),
00091       GTK_TREE_VIEW_COLUMN(filename_column),MULTIPLE_COL_FILENAME);
00092 
00093   gtk_tree_view_column_set_alignment(GTK_TREE_VIEW_COLUMN(filename_column), 0.5);
00094   gtk_tree_view_column_set_sizing(GTK_TREE_VIEW_COLUMN(filename_column),
00095       GTK_TREE_VIEW_COLUMN_AUTOSIZE);
00096   gtk_tree_view_column_set_sort_column_id(filename_column, MULTIPLE_COL_FILENAME);
00097 }
00098 
00099 void multiple_files_open_button_event(GtkWidget *widget, gpointer data)
00100 {
00101   gtk_dialog_response(GTK_DIALOG(data), MY_GTK_RESPONSE);
00102 }
00103 
00104 void multiple_files_add_button_event(GtkWidget *widget, gpointer data)
00105 {
00106   GtkWidget *file_chooser;
00107   GtkWidget *our_filter;
00108  
00109   file_chooser = gtk_file_chooser_dialog_new(_("Choose file or directory"),
00110                                               NULL,
00111                                               GTK_FILE_CHOOSER_ACTION_OPEN,
00112                                               GTK_STOCK_CANCEL,
00113                                               GTK_RESPONSE_CANCEL,
00114                                               NULL);
00115 
00116   wh_set_browser_directory_handler(ui, file_chooser);
00117 
00118   GtkWidget *button = gtk_dialog_add_button(GTK_DIALOG(file_chooser),
00119       GTK_STOCK_ADD, MY_GTK_RESPONSE);
00120   gtk_button_set_use_stock(GTK_BUTTON(button), TRUE);
00121   g_signal_connect(G_OBJECT(button), "clicked",
00122       G_CALLBACK(multiple_files_open_button_event), file_chooser);
00123   g_signal_connect(G_OBJECT(file_chooser), "file-activated",
00124       G_CALLBACK(multiple_files_open_button_event), file_chooser);
00125 
00126   gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(file_chooser), TRUE);
00127  
00128   //mp3 & ogg filter
00129   our_filter = (GtkWidget *)gtk_file_filter_new();
00130   gtk_file_filter_set_name (GTK_FILE_FILTER(our_filter), _("mp3 and ogg files (*.mp3 *.ogg)"));
00131   gtk_file_filter_add_pattern(GTK_FILE_FILTER(our_filter), "*.mp3");
00132   gtk_file_filter_add_pattern(GTK_FILE_FILTER(our_filter), "*.MP3");
00133   gtk_file_filter_add_pattern(GTK_FILE_FILTER(our_filter), "*.ogg");
00134   gtk_file_filter_add_pattern(GTK_FILE_FILTER(our_filter), "*.OGG");
00135   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), GTK_FILE_FILTER(our_filter));
00136   //mp3 filter
00137   our_filter = (GtkWidget *)gtk_file_filter_new();
00138   gtk_file_filter_set_name (GTK_FILE_FILTER(our_filter), _("mp3 files (*.mp3)"));
00139   gtk_file_filter_add_pattern(GTK_FILE_FILTER(our_filter), "*.mp3");
00140   gtk_file_filter_add_pattern(GTK_FILE_FILTER(our_filter), "*.MP3");
00141   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), GTK_FILE_FILTER(our_filter));
00142   //ogg filter
00143   our_filter = (GtkWidget *)gtk_file_filter_new();
00144   gtk_file_filter_set_name (GTK_FILE_FILTER(our_filter), _("ogg files (*.ogg)"));
00145   gtk_file_filter_add_pattern(GTK_FILE_FILTER(our_filter), "*.ogg");
00146   gtk_file_filter_add_pattern(GTK_FILE_FILTER(our_filter), "*.OGG");
00147   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), GTK_FILE_FILTER(our_filter));
00148 
00149   //all files filter
00150   our_filter = (GtkWidget *)gtk_file_filter_new();
00151   gtk_file_filter_set_name (GTK_FILE_FILTER(our_filter), _("All Files"));
00152   gtk_file_filter_add_pattern(GTK_FILE_FILTER(our_filter), "*");
00153   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), GTK_FILE_FILTER(our_filter));
00154 
00155   //we push open, ..
00156   if (gtk_dialog_run(GTK_DIALOG(file_chooser)) == MY_GTK_RESPONSE)
00157   {
00158     GSList *files = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(file_chooser));
00159     if (files)
00160     {
00161       gchar *filename = NULL;
00162 
00163       GtkTreeIter iter;
00164       GtkTreeModel *model =
00165         gtk_tree_view_get_model(GTK_TREE_VIEW(multiple_files_tree));
00166 
00167       while (files)
00168       {
00169         filename = files->data;
00170 
00171         int err = SPLT_OK;
00172         int num_of_files_found = 0;
00173         char **splt_filenames =
00174           mp3splt_find_filenames(the_state, filename, &num_of_files_found, &err);
00175         
00176         if (splt_filenames)
00177         {
00178           int i = 0;
00179           for (i = 0;i < num_of_files_found;i++)
00180           {
00181             gtk_list_store_append(GTK_LIST_STORE(model), &iter);
00182 
00183             gtk_list_store_set (GTK_LIST_STORE(model), 
00184                 &iter,
00185                 MULTIPLE_COL_FILENAME, splt_filenames[i],
00186                 -1);
00187             multiple_files_tree_number++;
00188 
00189             if (splt_filenames[i])
00190             {
00191               free(splt_filenames[i]);
00192               splt_filenames[i] = NULL;
00193             }
00194           }
00195 
00196           free(splt_filenames);
00197           splt_filenames = NULL;
00198         }
00199 
00200         g_free(filename);
00201         filename = NULL;
00202 
00203         files = g_slist_next(files);
00204       }
00205       g_slist_free(files);
00206 
00207       if (multiple_files_tree_number > 0)
00208       {
00209         gtk_widget_set_sensitive(multiple_files_remove_all_files_button, TRUE);
00210       }
00211     }
00212   }
00213 
00214   gtk_widget_destroy(file_chooser);
00215 }
00216 
00217 void multiple_files_remove_button_event(GtkWidget *widget, gpointer data)
00218 {
00219   GtkTreeIter iter;
00220   GtkTreeModel *model;
00221   GtkTreePath *path;
00222   GList *selected_list = NULL;
00223   GList *current_element = NULL;
00224   GtkTreeSelection *selection;
00225   
00226   model = gtk_tree_view_get_model(GTK_TREE_VIEW(multiple_files_tree));
00227   selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(multiple_files_tree));
00228   selected_list = gtk_tree_selection_get_selected_rows(selection, &model);
00229   
00230   //the name of the file that we have clicked on
00231   gchar *filename = NULL;
00232   
00233   while (g_list_length(selected_list) > 0)
00234   {
00235     //get the last element
00236     current_element = g_list_last(selected_list);
00237     path = current_element->data;
00238     //get the iter correspondig to the path
00239     gtk_tree_model_get_iter(model, &iter, path);
00240     gtk_tree_model_get(model, &iter, MULTIPLE_COL_FILENAME, &filename, -1);
00241     //remove the path from the selected list
00242     gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
00243     selected_list = g_list_remove(selected_list, path);
00244     multiple_files_tree_number--;
00245 
00246     //free memory
00247     gtk_tree_path_free(path);
00248     g_free(filename);
00249   }
00250   
00251   if (multiple_files_tree_number == 0)
00252   {
00253     gtk_widget_set_sensitive(multiple_files_remove_all_files_button, FALSE);
00254   }
00255   
00256   gtk_widget_set_sensitive(multiple_files_remove_file_button,FALSE);
00257   
00258   //free the selected elements
00259   g_list_foreach(selected_list, (GFunc)gtk_tree_path_free, NULL);
00260   g_list_free(selected_list);  
00261 }
00262 
00263 void multiple_files_remove_all_button_event(GtkWidget *widget, gpointer data)
00264 {
00265   GtkTreeIter iter;
00266   GtkTreeModel *model;
00267   
00268   model = gtk_tree_view_get_model(GTK_TREE_VIEW(multiple_files_tree));
00269   
00270   //filename to erase
00271   gchar *filename = NULL;
00272   //for all the splitnumbers
00273   while (multiple_files_tree_number > 0)
00274   {
00275     gtk_tree_model_get_iter_first(model, &iter);
00276     gtk_tree_model_get(model, &iter, MULTIPLE_COL_FILENAME, &filename, -1);
00277     gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
00278     multiple_files_tree_number--;
00279     g_free(filename);
00280   }
00281   
00282   gtk_widget_set_sensitive(multiple_files_remove_all_files_button,FALSE);
00283   gtk_widget_set_sensitive(multiple_files_remove_file_button,FALSE);
00284 }
00285 
00286 GtkWidget *create_multiple_files_buttons_hbox()
00287 {
00288   GtkWidget *hbox = gtk_hbox_new(FALSE,0);
00289 
00290   //button for adding file(s)
00291   GtkWidget *multiple_files_add_button = (GtkWidget *)
00292     create_cool_button(GTK_STOCK_ADD, _("_Add files"), FALSE);
00293   gtk_box_pack_start(GTK_BOX(hbox), multiple_files_add_button, FALSE, FALSE, 5);
00294   gtk_widget_set_sensitive(multiple_files_add_button, TRUE);
00295   g_signal_connect(G_OBJECT(multiple_files_add_button), "clicked",
00296                    G_CALLBACK(multiple_files_add_button_event), NULL);
00297 
00298   //button for removing a file
00299   multiple_files_remove_file_button = (GtkWidget *)
00300     create_cool_button(GTK_STOCK_DELETE, _("_Remove selected entries"),FALSE);
00301   gtk_box_pack_start(GTK_BOX(hbox),
00302       multiple_files_remove_file_button, FALSE, FALSE, 5);
00303   gtk_widget_set_sensitive(multiple_files_remove_file_button,FALSE);
00304   g_signal_connect(G_OBJECT(multiple_files_remove_file_button), "clicked",
00305                    G_CALLBACK(multiple_files_remove_button_event), NULL);
00306   
00307   //button for removing a file
00308   multiple_files_remove_all_files_button = (GtkWidget *)
00309     create_cool_button(GTK_STOCK_DELETE, _("R_emove all entries"),FALSE);
00310   gtk_box_pack_start(GTK_BOX(hbox), multiple_files_remove_all_files_button,
00311       FALSE, FALSE, 5);
00312   gtk_widget_set_sensitive(multiple_files_remove_all_files_button,FALSE);
00313   g_signal_connect(G_OBJECT(multiple_files_remove_all_files_button), "clicked",
00314                    G_CALLBACK(multiple_files_remove_all_button_event), NULL);
00315 
00316   return hbox;
00317 }
00318 
00319 void multiple_files_selection_changed(GtkTreeSelection *selec, gpointer data)
00320 {
00321   GtkTreeModel *model;
00322   GtkTreeSelection *selection;
00323   GList *selected_list = NULL;
00324   
00325   model = gtk_tree_view_get_model(GTK_TREE_VIEW(multiple_files_tree));
00326   selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(multiple_files_tree));
00327   selected_list = gtk_tree_selection_get_selected_rows(selection, &model);
00328 
00329   if (g_list_length(selected_list) > 0)
00330   {
00331     gtk_widget_set_sensitive(multiple_files_remove_file_button,TRUE);
00332   }
00333   else
00334   {
00335     gtk_widget_set_sensitive(multiple_files_remove_file_button,FALSE);
00336   }
00337 }
00338 
00339 GtkWidget *create_multiple_files_component()
00340 {
00341   GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
00342 
00343   multiple_files_tree = (GtkWidget *)create_multiple_files_tree();
00344 
00345   GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
00346   gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_NONE);
00347   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolled_window),
00348       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
00349   gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
00350 
00351   //create columns
00352   create_multiple_files_columns(GTK_TREE_VIEW(multiple_files_tree));
00353 
00354   //add the tree to the scrolled window
00355   gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(multiple_files_tree));
00356 
00357   //selection for the tree
00358   GtkWidget *multiple_files_tree_selection = (GtkWidget *)
00359     gtk_tree_view_get_selection(GTK_TREE_VIEW(multiple_files_tree));
00360   g_signal_connect(G_OBJECT(multiple_files_tree_selection), "changed",
00361                    G_CALLBACK(multiple_files_selection_changed), NULL);
00362   gtk_tree_selection_set_mode(GTK_TREE_SELECTION(multiple_files_tree_selection),
00363                               GTK_SELECTION_MULTIPLE);
00364 
00365   //bottom horizontal box with buttons
00366   GtkWidget *buttons_hbox = (GtkWidget *)create_multiple_files_buttons_hbox();
00367   gtk_box_pack_start(GTK_BOX(vbox), buttons_hbox, FALSE, FALSE, 2);
00368 
00369   return vbox;
00370 }
00371