mp3splt-gtk
libmp3splt_manager.c
1 /**********************************************************
2  *
3  * mp3splt-gtk -- utility based on mp3splt,
4  * for mp3/ogg splitting without decoding
5  *
6  * Copyright: (C) 2005-2012 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 #include "libmp3splt_manager.h"
33 
34 static void lmanager_change_window_progress_bar(splt_progress *p_bar, void *data);
35 static void lmanager_put_message_from_library(const char *message, splt_message_type mess_type, void *data);
36 
37 void lmanager_init_and_find_plugins(ui_state *ui)
38 {
39  mp3splt_set_progress_function(ui->mp3splt_state, lmanager_change_window_progress_bar, ui);
40  mp3splt_set_split_filename_function(ui->mp3splt_state, lmanager_put_split_filename, ui);
41  mp3splt_set_message_function(ui->mp3splt_state, lmanager_put_message_from_library, ui);
42 
43  mp3splt_set_int_option(ui->mp3splt_state, SPLT_OPT_DEBUG_MODE, SPLT_FALSE);
44  mp3splt_set_int_option(ui->mp3splt_state,
46 
47  gint error = mp3splt_find_plugins(ui->mp3splt_state);
48  if (error < 0)
49  {
50  char *error_from_library = mp3splt_get_strerror(ui->mp3splt_state, error);
51  if (error_from_library == NULL) { return; }
52  ui_fail(ui, error_from_library);
53  }
54 }
55 
56 void lmanager_stop_split(ui_state *ui)
57 {
58  gint err = SPLT_OK;
59  mp3splt_stop_split(ui->mp3splt_state, &err);
61 }
62 
63 static gboolean lmanager_put_split_filename_idle(ui_with_fname *ui_fname)
64 {
65  char *filename = ui_fname->fname;
66  ui_state *ui = ui_fname->ui;
67 
68  add_split_row(filename, ui);
69 
70  gint fname_status_size = (strlen(filename) + 255);
71  gchar *fname_status = g_malloc(sizeof(char) * fname_status_size);
72  g_snprintf(fname_status, fname_status_size, _(" File '%s' created"), filename);
73 
74  put_status_message(fname_status, ui);
75 
76  if (fname_status)
77  {
78  free(fname_status);
79  fname_status = NULL;
80  }
81 
82  gtk_widget_set_sensitive(ui->gui->queue_files_button, TRUE);
83  gtk_widget_set_sensitive(ui->gui->remove_all_files_button, TRUE);
84 
85 #ifdef __WIN32__
86  while (gtk_events_pending())
87  {
88  gtk_main_iteration();
89  }
90  gdk_flush();
91 #endif
92 
93  if (filename)
94  {
95  g_free(filename);
96  }
97  g_free(ui_fname);
98 
99  return FALSE;
100 }
101 
103 void lmanager_put_split_filename(const char *filename, int progress_data, void *data)
104 {
105  ui_state *ui = (ui_state *)data;
106 
107  ui_with_fname *ui_fname = g_malloc0(sizeof(ui_with_fname));
108  ui_fname->ui = ui;
109  ui_fname->fname = NULL;
110  if (filename)
111  {
112  ui_fname->fname = strdup(filename);
113  }
114 
115  gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE,
116  (GSourceFunc)lmanager_put_split_filename_idle, ui_fname, NULL);
117 }
118 
119 static gboolean lmanager_put_message_from_library_idle(ui_with_message *ui_message)
120 {
121  splt_message_type mess_type = ui_message->mess_type;
122  ui_state *ui = ui_message->ui;
123 
124  char *mess = ui_message->message;
125  if (mess)
126  {
127  gint i = 0;
128  //replace '\n' with ' '
129  for (i = 0;i < strlen(mess);i++)
130  {
131  if (mess[i] == '\n')
132  {
133  mess[i] = ' ';
134  }
135  }
136 
137  put_status_message_with_type(mess, mess_type, ui);
138 
139 #ifdef __WIN32__
140  while (gtk_events_pending())
141  {
142  gtk_main_iteration();
143  }
144  gdk_flush();
145 #endif
146 
147  g_free(mess);
148  mess = NULL;
149  }
150 
151  g_free(ui_message);
152 
153  return FALSE;
154 }
155 
157 static void lmanager_put_message_from_library(const char *message, splt_message_type mess_type, void *data)
158 {
159  ui_state *ui = (ui_state *)data;
160 
161  ui_with_message *ui_message = g_malloc0(sizeof(ui_with_message));
162  ui_message->ui = ui;
163  ui_message->message = NULL;
164  if (message)
165  {
166  ui_message->message = strdup(message);
167  }
168  ui_message->mess_type = mess_type;
169 
170  gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE,
171  (GSourceFunc)lmanager_put_message_from_library_idle, ui_message, NULL);
172 }
173 
174 static gboolean lmanager_change_window_progress_bar_idle(ui_with_p_bar *ui_p_bar)
175 {
176  ui_state *ui = ui_p_bar->ui;
177 
178  gchar progress_text[1024] = " ";
179 
180  switch (ui_p_bar->progress_type)
181  {
183  g_snprintf(progress_text,1023, _(" preparing \"%s\" (%d of %d)"),
184  ui_p_bar->filename_shorted,
185  ui_p_bar->current_split,
186  ui_p_bar->max_splits);
187  break;
189  g_snprintf(progress_text,1023, _(" creating \"%s\" (%d of %d)"),
190  ui_p_bar->filename_shorted,
191  ui_p_bar->current_split,
192  ui_p_bar->max_splits);
193  break;
195  g_snprintf(progress_text,1023, _(" searching for sync errors..."));
196  break;
198  if (get_currently_scanning_for_silence_safe(ui))
199  {
200  g_snprintf(progress_text,1023, _("Computing amplitude wave data..."));
201  }
202  else
203  {
204  g_snprintf(progress_text,1023,
205  _("S: %02d, Level: %.2f dB; scanning for silence..."),
206  ui_p_bar->silence_found_tracks, ui_p_bar->silence_db_level);
207  }
208  break;
209  default:
210  g_snprintf(progress_text,1023, " ");
211  break;
212  }
213 
214  gchar printed_value[1024] = { '\0' };
215  g_snprintf(printed_value, 1023, "%6.2f %% %s", ui_p_bar->percent_progress * 100, progress_text);
216 
217  gtk_progress_bar_set_fraction(ui->gui->percent_progress_bar, ui_p_bar->percent_progress);
218  gtk_progress_bar_set_text(ui->gui->percent_progress_bar, printed_value);
219 
220 #ifdef __WIN32__
221  while (gtk_events_pending())
222  {
223  gtk_main_iteration();
224  }
225  gdk_flush();
226 #endif
227 
228  if (ui_p_bar->filename_shorted)
229  {
230  g_free(ui_p_bar->filename_shorted);
231  }
232  g_free(ui_p_bar);
233 
234  return FALSE;
235 }
236 
238 static void lmanager_change_window_progress_bar(splt_progress *p_bar, void *data)
239 {
240  ui_state *ui = (ui_state *) data;
241 
242  ui_with_p_bar *ui_p_bar = g_malloc0(sizeof(ui_with_p_bar));
243  ui_p_bar->ui = ui;
244 
245  ui_p_bar->progress_type = p_bar->progress_type;
246  ui_p_bar->filename_shorted = NULL;
247  if (p_bar->filename_shorted[0] != '\0')
248  {
249  ui_p_bar->filename_shorted = strdup(p_bar->filename_shorted);
250  }
251  ui_p_bar->current_split = p_bar->current_split;
252  ui_p_bar->max_splits = p_bar->max_splits;
253  ui_p_bar->silence_found_tracks = p_bar->silence_found_tracks;
254  ui_p_bar->silence_db_level = p_bar->silence_db_level;
255  ui_p_bar->percent_progress = p_bar->percent_progress;
256 
257  gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE,
258  (GSourceFunc)lmanager_change_window_progress_bar_idle, ui_p_bar, NULL);
259 }
260