mp3splt-gtk
import.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-2010 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  * The magic behind the splitpoint input
00035  *
00036  * All functions that are needed in order to read in
00037  * cddb, cue or similar files.
00038  *********************************************************/
00039 
00040 #include <string.h>
00041 
00042 #include <gtk/gtk.h>
00043 #include <glib/gi18n.h>
00044 
00045 #include "player_tab.h"
00046 #include "main_win.h"
00047 #include "freedb_tab.h"
00048 #include "import.h"
00049 #include "options_manager.h"
00050 #include "mp3splt-gtk.h"
00051 #include "utilities.h"
00052 #include "ui_manager.h"
00053 #include "widgets_helper.h"
00054 
00055 extern splt_state *the_state;
00056 extern ui_state *ui;
00057 
00058 static void set_import_filters(GtkFileChooser *chooser);
00059 static void build_import_filter(GtkFileChooser *chooser,
00060     const gchar *filter_name, const gchar *filter_pattern,
00061     const gchar *filter_pattern_upper, 
00062     GList **filters, GtkFileFilter *all_filter);
00063 static gpointer add_audacity_labels_splitpoints(gpointer data);
00064 static gpointer add_cddb_splitpoints(gpointer data);
00065 static gpointer add_cue_splitpoints(gpointer data);
00066 
00068 void import_event(GtkWidget *widget, gpointer *data)
00069 {
00070   GtkWidget *file_chooser =
00071     gtk_file_chooser_dialog_new(_("Choose file to import"),
00072         NULL,
00073         GTK_FILE_CHOOSER_ACTION_OPEN,
00074         GTK_STOCK_CANCEL,
00075         GTK_RESPONSE_CANCEL,
00076         GTK_STOCK_OPEN,
00077         GTK_RESPONSE_ACCEPT,
00078         NULL);
00079 
00080   wh_set_browser_directory_handler(ui, file_chooser);
00081 
00082   set_import_filters(GTK_FILE_CHOOSER(file_chooser));
00083 
00084   if (gtk_dialog_run(GTK_DIALOG(file_chooser)) == GTK_RESPONSE_ACCEPT)
00085   {
00086     gchar *filename =
00087       gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser));
00088  
00089     handle_import(filename);
00090 
00091     g_free(filename);
00092     filename = NULL;
00093 
00094     remove_status_message();
00095   }
00096  
00097   gtk_widget_destroy(file_chooser);
00098 }
00099 
00104 void handle_import(gchar *filename)
00105 {
00106   if (filename == NULL)
00107   {
00108     return;
00109   }
00110 
00111   gchar *ext = strrchr(filename, '.');
00112   GString *ext_str = g_string_new(ext);
00113 
00114   g_string_ascii_up(ext_str);
00115 
00116   if ((strstr(ext_str->str, ".MP3") != NULL) ||
00117       (strstr(ext_str->str, ".OGG") != NULL))
00118   {
00119     file_chooser_ok_event(filename);
00120     remove_status_message();
00121   }
00122   else if ((strstr(ext_str->str, ".CUE") != NULL))
00123   {
00124     update_output_options();
00125     create_thread(add_cue_splitpoints, strdup(filename), TRUE, NULL);
00126   }
00127   else if ((strstr(ext_str->str, ".CDDB") != NULL))
00128   {
00129     update_output_options();
00130     create_thread(add_cddb_splitpoints, strdup(filename), TRUE, NULL);
00131   }
00132   else if ((strstr(ext_str->str, ".TXT") != NULL))
00133   {
00134     create_thread(add_audacity_labels_splitpoints, strdup(filename), TRUE, NULL);
00135   }
00136 
00137   if (ext_str)
00138   {
00139     g_string_free(ext_str, FALSE);
00140   }
00141 }
00142 
00144 static void set_import_filters(GtkFileChooser *chooser)
00145 {
00146   GtkFileFilter *all_filter = gtk_file_filter_new();
00147   gtk_file_filter_set_name(GTK_FILE_FILTER(all_filter),
00148       _("CDDB (*.cddb), CUE (*.cue), Audacity labels (*.txt)"));
00149 
00150   GList *filters = NULL;
00151 
00152   build_import_filter(chooser, _("CDDB files (*.cddb)"), "*.cddb", "*.CDDB", 
00153       &filters, all_filter);
00154   build_import_filter(chooser, _("CUE files (*.cue)"), "*.cue", "*.CUE",
00155       &filters, all_filter);
00156   build_import_filter(chooser, _("Audacity labels files (*.txt)"), "*.txt", "*.TXT",
00157       &filters, all_filter);
00158   build_import_filter(chooser, _("All files"), "*", NULL, &filters, NULL);
00159 
00160   gtk_file_chooser_add_filter(chooser, all_filter);
00161 
00162   GList *iter = NULL;
00163   for (iter = filters; iter != NULL; iter = g_list_next(iter))
00164   {
00165     gtk_file_chooser_add_filter(chooser, iter->data);
00166   }
00167 }
00168 
00169 static void build_import_filter(GtkFileChooser *chooser,
00170     const gchar *filter_name, const gchar *filter_pattern,
00171     const gchar *filter_pattern_upper,
00172     GList **filters, GtkFileFilter *all_filter)
00173 {
00174   GtkFileFilter *filter = gtk_file_filter_new();
00175   gtk_file_filter_set_name(GTK_FILE_FILTER(filter), filter_name);
00176 
00177   gtk_file_filter_add_pattern(GTK_FILE_FILTER(filter), filter_pattern);
00178 
00179   if (filter_pattern_upper)
00180   {
00181     gtk_file_filter_add_pattern(GTK_FILE_FILTER(filter), filter_pattern_upper);
00182   }
00183 
00184   if (all_filter)
00185   {
00186     gtk_file_filter_add_pattern(GTK_FILE_FILTER(all_filter), filter_pattern);
00187     if (filter_pattern_upper)
00188     {
00189       gtk_file_filter_add_pattern(GTK_FILE_FILTER(all_filter), filter_pattern_upper);
00190     }
00191   }
00192 
00193   *filters = g_list_append(*filters, filter);
00194 }
00195 
00200 static gpointer add_audacity_labels_splitpoints(gpointer data)
00201 {
00202   gchar *filename = data;
00203 
00204   gint err = SPLT_OK;
00205   mp3splt_put_audacity_labels_splitpoints_from_file(the_state, filename, &err);
00206  
00207   enter_threads();
00208  
00209   if (err >= 0)
00210   {
00211     update_splitpoints_from_the_state();
00212   }
00213  
00214   print_status_bar_confirmation(err);
00215  
00216   exit_threads();
00217 
00218   if (filename)
00219   {
00220     g_free(filename);
00221     filename = NULL;
00222   }
00223 
00224   return NULL;
00225 }
00226 
00228 static gpointer add_cddb_splitpoints(gpointer data)
00229 {
00230   gchar *filename = data;
00231 
00232   gint err = SPLT_OK;
00233   mp3splt_put_cddb_splitpoints_from_file(the_state, filename, &err);
00234 
00235   enter_threads();
00236  
00237   if (err >= 0)
00238   {
00239     update_splitpoints_from_the_state();
00240   }
00241   print_status_bar_confirmation(err);
00242 
00243   exit_threads();
00244 
00245   if (filename)
00246   {
00247     g_free(filename);
00248     filename = NULL;
00249   }
00250 
00251   return NULL;
00252 }
00253 
00255 static gpointer add_cue_splitpoints(gpointer data)
00256 {
00257   gchar *filename = data;
00258 
00259   gint err = SPLT_OK;
00260   mp3splt_set_filename_to_split(the_state, NULL);
00261   mp3splt_put_cue_splitpoints_from_file(the_state, filename, &err);
00262  
00263   enter_threads();
00264  
00265   if (err >= 0)
00266   {
00267     update_splitpoints_from_the_state();
00268   }
00269   print_status_bar_confirmation(err);
00270 
00271   // The cue file has provided libmp3splt with a input filename.
00272   // But since we use the filename from the gui instead we need to set
00273   // the value the gui uses, too, which we do in the next line.
00274   char *filename_to_split = mp3splt_get_filename_to_split(the_state);
00275   if (is_filee(filename_to_split))
00276   {
00277     inputfilename_set(filename_to_split);
00278   }
00279   
00280   exit_threads();
00281   enable_player_buttons();
00282 
00283   return NULL;
00284 }
00285