mp3splt-gtk
ui_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 - io_fx@yahoo.fr
7  *
8  * http://mp3splt.sourceforge.net/
9  *
10  *********************************************************/
11 
12 /**********************************************************
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27  * USA.
28  *
29  *********************************************************/
30 
31 #include "ui_manager.h"
32 
33 static void ui_main_window_new(ui_infos *infos);
34 static void ui_infos_new(ui_state *ui);
35 static gui_status *ui_status_new();
36 static gui_state *ui_gui_new();
37 static player_infos *ui_player_infos_new();
38 
39 static void ui_main_window_free(ui_main_window **main_win);
40 static void ui_infos_free(ui_infos **infos);
41 static void ui_status_free(gui_status **status);
42 static void ui_gui_free(gui_state **gui);
43 static void ui_player_infos_free(player_infos **pi);
44 
45 void ui_set_browser_directory(ui_state *ui, const gchar *directory)
46 {
47  ui_infos *infos = ui->infos;
48 
49  if (infos->browser_directory)
50  {
51  g_free(infos->browser_directory);
52  infos->browser_directory = NULL;
53  }
54 
55  if (directory == NULL)
56  {
57  infos->browser_directory = NULL;
58  return;
59  }
60 
61  infos->browser_directory = g_strdup(directory);
62 }
63 
64 const gchar *ui_get_browser_directory(ui_state *ui)
65 {
66  return ui->infos->browser_directory;
67 }
68 
69 void ui_set_main_win_position(ui_state *ui, gint x, gint y)
70 {
71  if (x == 0 && y == 0)
72  {
73  return;
74  }
75 
76  ui_main_window *main_win = ui->infos->main_win;
77  main_win->root_x_pos = x;
78  main_win->root_y_pos = y;
79 }
80 
81 void ui_set_main_win_size(ui_state *ui, gint width, gint height)
82 {
83  ui_main_window *main_win = ui->infos->main_win;
84  main_win->width = width;
85  main_win->height = height;
86 }
87 
88 const ui_main_window *ui_get_main_window_infos(ui_state *ui)
89 {
90  return ui->infos->main_win;
91 }
92 
93 ui_state *ui_state_new()
94 {
95  ui_state *ui = g_malloc0(sizeof(ui_state));
96 
97  ui_infos_new(ui);
98  ui->preferences = pm_state_new();
99 
100  gint error = SPLT_OK;
101  ui->mp3splt_state = mp3splt_new_state(&error);
102  if (error < 0)
103  {
104  ui_fail(ui, "mp3splt state initialization failed\n", NULL);
105  }
106 
107  ui->splitpoints = g_array_new(FALSE, FALSE, sizeof(Split_point));
108  ui->files_to_split = NULL;
109 
110  ui->status = ui_status_new();
111  ui->gui = ui_gui_new();
112  ui->pi = ui_player_infos_new();
113 
114  ui->return_code = EXIT_SUCCESS;
115 
116  init_mutex(&ui->variables_mutex);
117 
118  return ui;
119 }
120 
121 void ui_state_free(ui_state *ui)
122 {
123  if (!ui) { return; }
124 
125  ui_infos_free(&ui->infos);
126  pm_free(&ui->preferences);
127 
128  if (ui->mp3splt_state)
129  {
130  mp3splt_free_state(ui->mp3splt_state, NULL);
131  }
132 
133  g_array_free(ui->splitpoints, TRUE);
134 
135  ui_status_free(&ui->status);
136  ui_gui_free(&ui->gui);
137  ui_player_infos_free(&ui->pi);
138 
139  clear_mutex(&ui->variables_mutex);
140 
141  g_free(ui);
142 }
143 
144 void ui_register_spinner_int_preference(gchar *main_key, gchar *second_key,
145  gint default_value, GtkWidget *spinner,
146  void (*update_spinner_value_cb)(GtkWidget *spinner, gpointer data),
147  gpointer user_data_for_cb, ui_state *ui)
148 {
149  pm_register_spinner_int_preference(main_key, second_key,
150  default_value, spinner, update_spinner_value_cb, user_data_for_cb, ui->preferences);
151 }
152 
153 void ui_register_range_preference(gchar *main_key, gchar *second_key,
154  gint default_value, GtkWidget *range,
155  void (*update_adjustment_value)(GtkAdjustment *adjustment, gpointer data),
156  gpointer user_data_for_cb, ui_state *ui)
157 {
158  pm_register_range_preference(main_key, second_key,
159  default_value, range, update_adjustment_value, user_data_for_cb, ui->preferences);
160 }
161 
162 void ui_load_preferences(ui_state *ui)
163 {
164  load_preferences(ui);
165 }
166 
167 void ui_save_preferences(GtkWidget *dummy_widget, ui_state *ui)
168 {
169  save_preferences(ui);
170 }
171 
172 void ui_fail(ui_state *ui, const gchar *message, ...)
173 {
174  if (message != NULL)
175  {
176  gchar formatted_message[1024] = { '\0' };
177 
178  va_list ap;
179  va_start(ap, message);
180  g_vsnprintf(formatted_message, 1024, message, ap);
181  va_end(ap);
182 
183  fprintf(stderr, formatted_message);
184  fflush(stderr);
185  }
186 
187  ui->return_code = EXIT_FAILURE;
188 
189  ui_state_free(ui);
190 
191  exit(1);
192 }
193 
194 static void ui_main_window_new(ui_infos *infos)
195 {
196  ui_main_window *main_win = g_malloc0(sizeof(ui_main_window));
197 
198  main_win->root_x_pos = 0;
199  main_win->root_y_pos = 0;
200 
201  main_win->width = UI_DEFAULT_WIDTH;
202  main_win->height = UI_DEFAULT_HEIGHT;
203 
204  infos->main_win = main_win;
205 }
206 
207 static void ui_infos_new(ui_state *ui)
208 {
209  ui_infos *infos = g_malloc0(sizeof(ui_infos));
210 
211  ui_main_window_new(infos);
212 
213  infos->browser_directory = NULL;
214  infos->text_options_list = NULL;
215 
216  infos->silence_points = NULL;
217  infos->malloced_num_of_silence_points = 0;
218  infos->number_of_silence_points = 0;
219 
220  infos->player_seconds = 0;
221  infos->player_minutes = 0;
222  infos->player_hundr_secs = 0;
223  infos->player_seconds2 = 0;
224  infos->player_minutes2 = 0;
225  infos->player_hundr_secs2 = 0;
226 
227  infos->total_time = 0;
228  infos->current_time = 0;
229 
230  infos->splitnumber = 0;
231  infos->width_drawing_area = 0;
232  infos->zoom_coeff = 2.0;
233  infos->zoom_coeff_old = 2.0;
234 
235  infos->hundr_secs_th = 20;
236  infos->tens_of_secs_th = 3 * 100;
237  infos->secs_th = 40 * 100;
238  infos->ten_secs_th = 3 * 6000;
239  infos->minutes_th = 20 * 6000;
240  infos->ten_minutes_th = 3 * 3600 * 100;
241 
242  infos->one_minute_time = 1 * 6000;
243  infos->three_minutes_time = 3 * 6000;
244  infos->six_minutes_time = 6 * 6000;
245  infos->ten_minutes_time = 10 * 6000;
246  infos->twenty_minutes_time = 20 * 6000;
247  infos->fourty_minutes_time = 40 * 6000;
248 
249  GArray *preview_time_windows = g_array_new(TRUE, TRUE, sizeof(gint));
250  g_array_append_val(preview_time_windows, infos->one_minute_time);
251  g_array_append_val(preview_time_windows, infos->three_minutes_time);
252  g_array_append_val(preview_time_windows, infos->six_minutes_time);
253  g_array_append_val(preview_time_windows, infos->ten_minutes_time);
254  g_array_append_val(preview_time_windows, infos->twenty_minutes_time);
255  g_array_append_val(preview_time_windows, infos->fourty_minutes_time);
256  infos->preview_time_windows = preview_time_windows;
257 
258  infos->filtered_points_presence = NULL;
259  infos->silence_wave_number_of_points_threshold = DEFAULT_SILENCE_WAVE_NUMBER_OF_POINTS_THRESHOLD;
260 
261  infos->selected_player = PLAYER_GSTREAMER;
262 
263  infos->douglas_peucker_thresholds[0] = 2.0;
264  infos->douglas_peucker_thresholds[1] = 5.0;
265  infos->douglas_peucker_thresholds[2] = 8.0;
266  infos->douglas_peucker_thresholds[3] = 11.0;
267  infos->douglas_peucker_thresholds[4] = 15.0;
268 
269  infos->douglas_peucker_thresholds_defaults[0] = 2.0;
270  infos->douglas_peucker_thresholds_defaults[1] = 5.0;
271  infos->douglas_peucker_thresholds_defaults[2] = 8.0;
272  infos->douglas_peucker_thresholds_defaults[3] = 11.0;
273  infos->douglas_peucker_thresholds_defaults[4] = 15.0;
274 
275  infos->debug_is_active = FALSE;
276 
277  infos->silence_threshold_value = SPLT_DEFAULT_PARAM_THRESHOLD;
278  infos->silence_offset_value = SPLT_DEFAULT_PARAM_OFFSET;
279  infos->silence_number_of_tracks = SPLT_DEFAULT_PARAM_TRACKS;
280  infos->silence_minimum_length = SPLT_DEFAULT_PARAM_MINIMUM_LENGTH;
281  infos->silence_minimum_track_length = SPLT_DEFAULT_PARAM_MINIMUM_TRACK_LENGTH;
282  infos->silence_remove_silence_between_tracks = FALSE;
283 
284  infos->freedb_table_number = 0;
285  infos->freedb_selected_id = -1;
286 
287  infos->playlist_tree_number = 0;
288  infos->multiple_files_tree_number = 0;
289 
290  infos->freedb_search_results = NULL;
291 
292  infos->split_file_mode = FILE_MODE_SINGLE;
293 
294  infos->outputdirname = NULL;
295 
296  gint i = 0;
297  for (i = 0; i < 6;i++)
298  {
299  infos->preview_indexes[i].index = 0;
300  infos->preview_indexes[i].data = NULL;
301  }
302 
303  infos->timeout_value = DEFAULT_TIMEOUT_VALUE;
304 
305  ui->infos = infos;
306 }
307 
308 static gui_status *ui_status_new(ui_state *ui)
309 {
310  gui_status *status = g_malloc0(sizeof(gui_status));
311 
312  status->splitting = FALSE;
313  status->process_in_progress = FALSE;
314  status->mouse_on_progress_bar = FALSE;
315 
316  status->currently_compute_douglas_peucker_filters = FALSE;
317  status->show_silence_wave = FALSE;
318 
319  status->playing = FALSE;
320  status->timer_active = FALSE;
321  status->quick_preview_end_splitpoint = -1;
322  status->preview_start_splitpoint = -1;
323 
324  status->move_time = 0;
325 
326  status->button1_pressed = FALSE;
327  status->button2_pressed = FALSE;
328 
329  status->quick_preview = FALSE;
330 
331  status->button_x = 0;
332  status->button_x2 = 0;
333  status->button_y = 0;
334  status->button_y2 = 0;
335 
336  status->move_splitpoints = FALSE;
337  status->splitpoint_to_move = -1;
338  status->remove_splitpoints = FALSE;
339  status->select_splitpoints = FALSE;
340  status->check_splitpoint = FALSE;
341 
342  status->first_splitpoint_selected = -1;
343 
344  status->spin_mins = 0;
345  status->spin_secs = 0;
346  status->spin_hundr_secs = 0;
347 
348  g_snprintf(status->current_description, 255, "%s", _("description here"));
349 
350  status->preview_start_position = 0;
351  status->timeout_id = 0;
352 
353  status->currently_scanning_for_silence = FALSE;
354 
355  status->filename_to_split = NULL;
356 
357  status->douglas_callback_counter = 0;
358 
359  status->stream = FALSE;
360  status->only_press_pause = FALSE;
361 
362  status->change_volume = TRUE;
363  status->on_the_volume_button = FALSE;
364  status->file_browsed = FALSE;
365 
366  status->preview_row = 0;
367  status->selected_split_mode = SELECTED_SPLIT_NORMAL;
368 
369  status->should_trim = FALSE;
370 
371  status->file_selection_changed = FALSE;
372 
373  status->stop_split = FALSE;
374 
375  return status;
376 }
377 
378 static player_infos *ui_player_infos_new()
379 {
380  player_infos *pi = g_malloc0(sizeof(player_infos));
381 
382 #ifndef NO_GSTREAMER
383  pi->song_artist = NULL;
384  pi->song_title = NULL;
385  pi->rate = 0;
386  pi->play = NULL;
387  pi->bus = NULL;
388  pi->_gstreamer_is_running = FALSE;
389 #endif
390 
391 #ifndef NO_AUDACIOUS
392  pi->dbus_proxy = NULL;
393  pi->dbus_connection = NULL;
394 #endif
395 
396  //snackamp
397  pi->in = NULL;
398  pi->out = NULL;
399  pi->connected = FALSE;
400 
401  return pi;
402 }
403 
404 static gui_state *ui_gui_new()
405 {
406  gui_state *gui = g_malloc0(sizeof(gui_state));
407 
408  gui->margin = 4;
409  gui->real_erase_split_length = 12;
410  gui->real_move_split_length = 16;
411  gui->real_checkbox_length = 12;
412  gui->real_wave_length = 96;
413 
414  gui->splitpoints_window = NULL;
415  gui->preferences_window = NULL;
416  gui->split_files_window = NULL;
417  gui->freedb_window = NULL;
418 
419  return gui;
420 }
421 
422 static void ui_main_window_free(ui_main_window **main_win)
423 {
424  if (!main_win || !*main_win)
425  {
426  return;
427  }
428 
429  g_free(*main_win);
430  *main_win = NULL;
431 }
432 
433 static void ui_infos_free(ui_infos **infos)
434 {
435  if (!infos || !*infos)
436  {
437  return;
438  }
439 
440  ui_main_window_free(&(*infos)->main_win);
441 
442  if ((*infos)->browser_directory)
443  {
444  g_free((*infos)->browser_directory);
445  (*infos)->browser_directory = NULL;
446  }
447 
448  if ((*infos)->text_options_list)
449  {
450  g_list_free((*infos)->text_options_list);
451  }
452 
453  if ((*infos)->silence_points)
454  {
455  g_free((*infos)->silence_points);
456  (*infos)->silence_points = NULL;
457  (*infos)->number_of_silence_points = 0;
458  }
459 
460  g_array_free((*infos)->preview_time_windows, TRUE);
461 
462  g_free(*infos);
463  *infos = NULL;
464 }
465 
466 static void ui_status_free(gui_status **status)
467 {
468  if (!status || !*status)
469  {
470  return;
471  }
472 
473  g_free(*status);
474  *status = NULL;
475 }
476 
477 static void ui_player_infos_free(player_infos **pi)
478 {
479  if (!pi || !*pi)
480  {
481  return;
482  }
483 
484  g_free(*pi);
485  *pi = NULL;
486 }
487 
488 static void ui_gui_free(gui_state **gui)
489 {
490  if (!gui|| !*gui)
491  {
492  return;
493  }
494 
495  g_free(*gui);
496  *gui = NULL;
497 }
498