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-2013 Alexandru Munteanu
7  * Contact: m@ioalex.net
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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,
45  SPLT_OPT_SET_FILE_FROM_CUE_IF_FILE_TAG_FOUND, SPLT_TRUE);
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 = mp3splt_stop_split(ui->mp3splt_state);
60 }
61 
62 static gboolean lmanager_put_split_filename_idle(ui_with_fname *ui_fname)
63 {
64  char *filename = ui_fname->fname;
65  ui_state *ui = ui_fname->ui;
66 
67  add_split_row(filename, ui);
68 
69  gint fname_status_size = (strlen(filename) + 255);
70  gchar *fname_status = g_malloc(sizeof(char) * fname_status_size);
71  g_snprintf(fname_status, fname_status_size, _(" File '%s' created"), filename);
72 
73  put_status_message(fname_status, ui);
74 
75  if (fname_status)
76  {
77  free(fname_status);
78  fname_status = NULL;
79  }
80 
81  gtk_widget_set_sensitive(ui->gui->queue_files_button, TRUE);
82  gtk_widget_set_sensitive(ui->gui->remove_all_files_button, TRUE);
83 
84 #ifdef __WIN32__
85  while (gtk_events_pending())
86  {
87  gtk_main_iteration();
88  }
89  gdk_flush();
90 #endif
91 
92  if (filename)
93  {
94  g_free(filename);
95  }
96  g_free(ui_fname);
97 
98  return FALSE;
99 }
100 
102 void lmanager_put_split_filename(const char *filename, void *data)
103 {
104  ui_state *ui = (ui_state *)data;
105 
106  ui_with_fname *ui_fname = g_malloc0(sizeof(ui_with_fname));
107  ui_fname->ui = ui;
108  ui_fname->fname = NULL;
109  if (filename)
110  {
111  ui_fname->fname = strdup(filename);
112  }
113 
114  gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE,
115  (GSourceFunc)lmanager_put_split_filename_idle, ui_fname, NULL);
116 }
117 
118 static gboolean lmanager_put_message_from_library_idle(ui_with_message *ui_message)
119 {
120  splt_message_type mess_type = ui_message->mess_type;
121  ui_state *ui = ui_message->ui;
122 
123  char *mess = ui_message->message;
124  if (mess)
125  {
126  gint i = 0;
127  //replace '\n' with ' '
128  for (i = 0;i < strlen(mess);i++)
129  {
130  if (mess[i] == '\n')
131  {
132  mess[i] = ' ';
133  }
134  }
135 
136  put_status_message_with_type(mess, mess_type, ui);
137 
138 #ifdef __WIN32__
139  while (gtk_events_pending())
140  {
141  gtk_main_iteration();
142  }
143  gdk_flush();
144 #endif
145 
146  g_free(mess);
147  mess = NULL;
148  }
149 
150  g_free(ui_message);
151 
152  return FALSE;
153 }
154 
156 static void lmanager_put_message_from_library(const char *message, splt_message_type mess_type, void *data)
157 {
158  ui_state *ui = (ui_state *)data;
159 
160  ui_with_message *ui_message = g_malloc0(sizeof(ui_with_message));
161  ui_message->ui = ui;
162  ui_message->message = NULL;
163  if (message)
164  {
165  ui_message->message = strdup(message);
166  }
167  ui_message->mess_type = mess_type;
168 
169  gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE,
170  (GSourceFunc)lmanager_put_message_from_library_idle, ui_message, NULL);
171 }
172 
173 static gboolean lmanager_change_window_progress_bar_idle(ui_with_p_bar *ui_p_bar)
174 {
175  ui_state *ui = ui_p_bar->ui;
176 
177  gchar progress_text[1024] = " ";
178 
179  switch (ui_p_bar->progress_type)
180  {
181  case SPLT_PROGRESS_PREPARE:
182  g_snprintf(progress_text,1023, _(" preparing \"%s\" (%d of %d)"),
183  ui_p_bar->filename_shorted,
184  ui_p_bar->current_split,
185  ui_p_bar->max_splits);
186  break;
187  case SPLT_PROGRESS_CREATE:
188  g_snprintf(progress_text,1023, _(" creating \"%s\" (%d of %d)"),
189  ui_p_bar->filename_shorted,
190  ui_p_bar->current_split,
191  ui_p_bar->max_splits);
192  break;
193  case SPLT_PROGRESS_SEARCH_SYNC:
194  g_snprintf(progress_text,1023, _(" searching for sync errors..."));
195  break;
196  case SPLT_PROGRESS_SCAN_SILENCE:
197  if (get_currently_scanning_for_silence_safe(ui))
198  {
199  g_snprintf(progress_text,1023, _("Computing amplitude wave data..."));
200  }
201  else
202  {
203  g_snprintf(progress_text,1023,
204  _("S: %02d, Level: %.2f dB; scanning for silence..."),
205  ui_p_bar->silence_found_tracks, ui_p_bar->silence_db_level);
206  }
207  break;
208  default:
209  g_snprintf(progress_text,1023, " ");
210  break;
211  }
212 
213  gchar printed_value[1024] = { '\0' };
214  g_snprintf(printed_value, 1023, "%6.2f %% %s", ui_p_bar->percent_progress * 100, progress_text);
215 
216  gtk_progress_bar_set_fraction(ui->gui->percent_progress_bar, ui_p_bar->percent_progress);
217  gtk_progress_bar_set_text(ui->gui->percent_progress_bar, printed_value);
218 
219 #ifdef __WIN32__
220  while (gtk_events_pending())
221  {
222  gtk_main_iteration();
223  }
224  gdk_flush();
225 #endif
226 
227  if (ui_p_bar->filename_shorted)
228  {
229  g_free(ui_p_bar->filename_shorted);
230  }
231  g_free(ui_p_bar);
232 
233  return FALSE;
234 }
235 
237 static void lmanager_change_window_progress_bar(splt_progress *p_bar, void *data)
238 {
239  ui_state *ui = (ui_state *) data;
240 
241  ui_with_p_bar *ui_p_bar = g_malloc0(sizeof(ui_with_p_bar));
242  ui_p_bar->ui = ui;
243 
244  ui_p_bar->progress_type = mp3splt_progress_get_type(p_bar);
245  ui_p_bar->filename_shorted = mp3splt_progress_get_filename_shorted(p_bar);
246  ui_p_bar->current_split = mp3splt_progress_get_current_split(p_bar);
247  ui_p_bar->max_splits = mp3splt_progress_get_max_splits(p_bar);
248  ui_p_bar->silence_found_tracks = mp3splt_progress_get_silence_found_tracks(p_bar);
249  ui_p_bar->silence_db_level = mp3splt_progress_get_silence_db_level(p_bar);
250  ui_p_bar->percent_progress = mp3splt_progress_get_percent_progress(p_bar);
251 
252  gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE,
253  (GSourceFunc)lmanager_change_window_progress_bar_idle, ui_p_bar, NULL);
254 }
255 
void print_status_bar_confirmation(gint error, ui_state *ui)
Output an error message from libmp3splt to the status bar.
Definition: main_window.c:1115
void put_status_message_with_type(const gchar *text, splt_message_type mess_type, ui_state *ui)
Output a message to the status message bar.
Definition: main_window.c:278
void put_status_message(const gchar *text, ui_state *ui)
Output a info message to the status message bar.
Definition: main_window.c:264
void add_split_row(const gchar *name, ui_state *ui)
add a row to the table