mp3splt-gtk
export.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 function that allows to export the current list of
00035  * splitpoints as a Cue sheet.
00036  *********************************************************/
00037 
00038 #include <gtk/gtk.h>
00039 #include <glib/gi18n.h>
00040 #include <string.h>
00041 #include <errno.h>
00042 #include <stdio.h>
00043 #include "export.h"
00044 #include "main_win.h"
00045 #include "player_tab.h"
00046 #include "tree_tab.h"
00047 #include "ui_manager.h"
00048 #include "widgets_helper.h"
00049 
00050 extern ui_state *ui;
00051 
00064 void export_file(const gchar* filename)
00065 {
00066   FILE *outfile;
00067   GtkTreeModel *model;
00068   GtkTreeIter iter;
00069   
00070   if((outfile=fopen(filename,"w"))==0)
00071     {
00072       put_status_message((gchar *)strerror(errno));
00073       return;
00074     };
00075 
00076   if(fprintf(outfile,"REM CREATOR \"MP3SPLT_GTK\"\n")<0)
00077     {
00078       put_status_message((gchar *)strerror(errno));
00079       return;
00080     }
00081 
00082   if(fprintf(outfile,"REM SPLT_TITLE_IS_FILENAME\n")<0)
00083     {
00084       put_status_message((gchar *)strerror(errno));
00085       return;
00086     }
00087 
00088   // Determine which type our input file is of.
00089   gchar *extension=inputfilename_get();
00090   gchar *tmp;
00091   while((tmp=strchr(extension,'.')))
00092     {
00093       extension=++tmp;
00094     }
00095 
00096   if(fprintf(outfile,"FILE \"%s\" %s\n",inputfilename_get(),extension)<0)
00097     {
00098       put_status_message((gchar *)strerror(errno));
00099       return;
00100     };
00101 
00102   model = gtk_tree_view_get_model(tree_view);
00103   
00104   //if the table is not empty get iter number
00105   if(gtk_tree_model_get_iter_first(model, &iter))
00106     {
00107       // The track number
00108       gint count = 1;
00109 
00110       do 
00111         {
00112           // All information we need for this track
00113           gchar *description;
00114           gint mins,secs,hundr;
00115           gboolean keep;
00116           
00117           gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
00118                              COL_DESCRIPTION,&description,
00119                              COL_MINUTES, &mins,
00120                              COL_SECONDS, &secs,
00121                              COL_HUNDR_SECS, &hundr,
00122                              COL_CHECK, &keep,
00123                              -1);
00124 
00125           // Sometimes libmp3splt introduces an additional split point
00126           // way below the end of the file --- that breaks cue import
00127           // later => skip all points with extremely high time values.
00128           if(mins<357850)
00129             {
00130               // Output the track header
00131               if(fprintf(outfile,"\tTRACK %02i AUDIO\n",count++)<0)
00132                 {
00133                   put_status_message((gchar *)strerror(errno));
00134                   return;
00135                 };
00136               
00137               
00138               // Output the track description escaping any quotes
00139               if(fprintf(outfile,"\t\tTITLE \"")<0)
00140                 {
00141                   put_status_message((gchar *)strerror(errno));
00142                   return;
00143                 }
00144               
00145               gchar *outputchar;
00146               for(outputchar=description;*outputchar!='\0';outputchar++)
00147                 {
00148                   if(*outputchar=='"')
00149                     {
00150                       if(fprintf(outfile,"\\\"")<0)
00151                         {
00152                           put_status_message((gchar *)strerror(errno));
00153                           return;
00154                         }
00155                     }
00156                   else
00157                     {
00158                       if(fprintf(outfile,"%c",*outputchar)<0)
00159                         {
00160                           put_status_message((gchar *)strerror(errno));
00161                           return;
00162                         }
00163                     }
00164                 }    
00165               if(fprintf(outfile,"\" \n")<0)
00166                 {
00167                   put_status_message((gchar *)strerror(errno));
00168                   return;
00169                 };
00170               
00171               if(!keep)
00172                 {
00173                   if(fprintf(outfile,"\t\tREM NOKEEP\n")<0)
00174                     {
00175                       put_status_message((gchar *)strerror(errno));
00176                       return;
00177                     }
00178                 }
00179               
00180               if(fprintf(outfile,"\t\tINDEX 01 %d:%02d:%02d\n",mins,secs,hundr)<0)
00181                 {
00182                   put_status_message((gchar *)strerror(errno));
00183                   return;
00184                 }
00185             }
00186         } while(gtk_tree_model_iter_next(model, &iter));
00187     }
00188   
00189   fclose(outfile);
00190 }
00191 
00193 void ChooseCueExportFile(GtkWidget *widget, gpointer data)
00194 {
00195   // file chooser
00196   GtkWidget *file_chooser;
00197 
00198   //creates the dialog
00199   file_chooser = gtk_file_chooser_dialog_new(_("Select cue file name"),
00200       NULL,
00201       GTK_FILE_CHOOSER_ACTION_SAVE,
00202       GTK_STOCK_CANCEL,
00203       GTK_RESPONSE_CANCEL,
00204       GTK_STOCK_SAVE,
00205       GTK_RESPONSE_ACCEPT,
00206       NULL);
00207 
00208   wh_set_browser_directory_handler(ui, file_chooser);
00209 
00210   // tells the dialog to list only cue files
00211   GtkWidget *our_filter = (GtkWidget *)gtk_file_filter_new();
00212   gtk_file_filter_set_name (GTK_FILE_FILTER(our_filter), _("cue files (*.cue)"));
00213   gtk_file_filter_add_pattern(GTK_FILE_FILTER(our_filter), "*.cue");
00214   gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), GTK_FILE_FILTER(our_filter));
00215   gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(file_chooser),TRUE);
00216 
00217 
00218   if (gtk_dialog_run(GTK_DIALOG(file_chooser)) == GTK_RESPONSE_ACCEPT)
00219   {
00220     gchar *filename =
00221       gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser));
00222 
00223     //Write the output file
00224     export_file(filename);
00225     g_free(filename);
00226   }
00227   
00228   //destroy the dialog
00229   gtk_widget_destroy(file_chooser);
00230 }
00231