mp3splt-gtk
export.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-2010 Alexandru Munteanu
7  * Contact: io_fx@yahoo.fr
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28  * USA.
29  *
30  *********************************************************/
31 
32 /*!********************************************************
33  * \file
34  * The function that allows to export the current list of
35  * splitpoints as a Cue sheet.
36  *********************************************************/
37 
38 #include "export.h"
39 
52 static void export_file(const gchar* filename, ui_state *ui)
53 {
54  FILE *outfile;
55 
56  if ((outfile = fopen(filename,"w"))==0)
57  {
58  put_status_message((gchar *)strerror(errno), ui);
59  return;
60  };
61 
62  if (fprintf(outfile,"REM CREATOR \"MP3SPLT_GTK\"\n") < 0)
63  {
64  put_status_message((gchar *)strerror(errno), ui);
65  return;
66  }
67 
68  if (fprintf(outfile,"REM SPLT_TITLE_IS_FILENAME\n") < 0)
69  {
70  put_status_message((gchar *)strerror(errno), ui);
71  return;
72  }
73 
74  gchar *extension = get_input_filename(ui->gui);
75  gchar *tmp;
76  while ((tmp = strchr(extension,'.')))
77  {
78  extension = ++tmp;
79  }
80 
81  if (fprintf(outfile,"FILE \"%s\" %s\n", get_input_filename(ui->gui), extension) < 0)
82  {
83  put_status_message((gchar *)strerror(errno), ui);
84  return;
85  }
86 
87  GtkTreeModel *model = gtk_tree_view_get_model(ui->gui->tree_view);
88  GtkTreeIter iter;
89  if (!gtk_tree_model_get_iter_first(model, &iter))
90  {
91  goto end;
92  }
93 
94  gint count = 1;
95 
96  do {
97  // All information we need for this track
98  gchar *description;
99  gint mins,secs,hundr;
100  gboolean keep;
101 
102  gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
103  COL_DESCRIPTION, &description,
104  COL_MINUTES, &mins, COL_SECONDS, &secs, COL_HUNDR_SECS, &hundr, COL_CHECK, &keep, -1);
105 
106  // Sometimes libmp3splt introduces an additional split point
107  // way below the end of the file --- that breaks cue import
108  // later => skip all points with extremely high time values.
109  if (mins >= 357850)
110  {
111  continue;
112  }
113 
114  // Output the track header
115  if(fprintf(outfile,"\tTRACK %02i AUDIO\n",count++)<0)
116  {
117  put_status_message((gchar *)strerror(errno), ui);
118  return;
119  };
120 
121  // Output the track description escaping any quotes
122  if(fprintf(outfile,"\t\tTITLE \"")<0)
123  {
124  put_status_message((gchar *)strerror(errno), ui);
125  return;
126  }
127 
128  gchar *outputchar;
129  for (outputchar = description; *outputchar!='\0'; outputchar++)
130  {
131  if (*outputchar == '"')
132  {
133  if (fprintf(outfile,"\\\"") < 0)
134  {
135  put_status_message((gchar *)strerror(errno), ui);
136  return;
137  }
138  }
139  else
140  {
141  if (fprintf(outfile, "%c", *outputchar)<0)
142  {
143  put_status_message((gchar *)strerror(errno), ui);
144  return;
145  }
146  }
147  }
148 
149  if (fprintf(outfile, "\" \n") < 0)
150  {
151  put_status_message((gchar *)strerror(errno), ui);
152  return;
153  }
154 
155  if(!keep)
156  {
157  if (fprintf(outfile, "\t\tREM NOKEEP\n") < 0)
158  {
159  put_status_message((gchar *)strerror(errno), ui);
160  return;
161  }
162  }
163 
164  if (fprintf(outfile, "\t\tINDEX 01 %d:%02d:%02d\n", mins, secs, hundr) < 0)
165  {
166  put_status_message((gchar *)strerror(errno), ui);
167  return;
168  }
169  } while(gtk_tree_model_iter_next(model, &iter));
170 
171 end:
172  fclose(outfile);
173 }
174 
176 void ChooseCueExportFile(GtkWidget *widget, ui_state *ui)
177 {
178  GtkWidget *file_chooser = gtk_file_chooser_dialog_new(_("Select cue file name"),
179  NULL,
180  GTK_FILE_CHOOSER_ACTION_SAVE,
181  GTK_STOCK_CANCEL,
182  GTK_RESPONSE_CANCEL,
183  GTK_STOCK_SAVE,
184  GTK_RESPONSE_ACCEPT,
185  NULL);
186 
187  wh_set_browser_directory_handler(ui, file_chooser);
188 
189  GtkFileFilter *our_filter = gtk_file_filter_new();
190  gtk_file_filter_set_name (our_filter, _("cue files (*.cue)"));
191  gtk_file_filter_add_pattern(our_filter, "*.cue");
192  gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser), our_filter);
193  gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(file_chooser),TRUE);
194 
195  if (gtk_dialog_run(GTK_DIALOG(file_chooser)) == GTK_RESPONSE_ACCEPT)
196  {
197  gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser));
198  export_file(filename, ui);
199  g_free(filename);
200  }
201 
202  gtk_widget_destroy(file_chooser);
203 }
204