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 - m@ioalex.net
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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);
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_defaults[0] = 2.0;
264  infos->douglas_peucker_thresholds_defaults[1] = 5.0;
265  infos->douglas_peucker_thresholds_defaults[2] = 8.0;
266  infos->douglas_peucker_thresholds_defaults[3] = 11.0;
267  infos->douglas_peucker_thresholds_defaults[4] = 15.0;
268  infos->douglas_peucker_thresholds_defaults[5] = 22.0;
269 
270  infos->douglas_peucker_thresholds[0] = infos->douglas_peucker_thresholds_defaults[0];
271  infos->douglas_peucker_thresholds[1] = infos->douglas_peucker_thresholds_defaults[1];
272  infos->douglas_peucker_thresholds[2] = infos->douglas_peucker_thresholds_defaults[2];
273  infos->douglas_peucker_thresholds[3] = infos->douglas_peucker_thresholds_defaults[3];
274  infos->douglas_peucker_thresholds[4] = infos->douglas_peucker_thresholds_defaults[4];
275  infos->douglas_peucker_thresholds[5] = infos->douglas_peucker_thresholds_defaults[5];
276 
277  infos->debug_is_active = FALSE;
278 
279  infos->silence_threshold_value = SPLT_DEFAULT_PARAM_THRESHOLD;
280  infos->silence_offset_value = SPLT_DEFAULT_PARAM_OFFSET;
281  infos->silence_number_of_tracks = SPLT_DEFAULT_PARAM_TRACKS;
282  infos->silence_minimum_length = SPLT_DEFAULT_PARAM_MINIMUM_LENGTH;
283  infos->silence_minimum_track_length = SPLT_DEFAULT_PARAM_MINIMUM_TRACK_LENGTH;
284  infos->silence_remove_silence_between_tracks = FALSE;
285 
286  infos->freedb_table_number = 0;
287  infos->freedb_selected_id = -1;
288 
289  infos->playlist_tree_number = 0;
290  infos->multiple_files_tree_number = 0;
291 
292  infos->freedb_search_results = NULL;
293 
294  infos->split_file_mode = FILE_MODE_SINGLE;
295 
296  infos->outputdirname = NULL;
297 
298  gint i = 0;
299  for (i = 0; i < 6;i++)
300  {
301  infos->preview_indexes[i].index = 0;
302  infos->preview_indexes[i].data = NULL;
303  }
304 
305  infos->timeout_value = DEFAULT_TIMEOUT_VALUE;
306 
307  ui->infos = infos;
308 }
309 
310 static gui_status *ui_status_new(ui_state *ui)
311 {
312  gui_status *status = g_malloc0(sizeof(gui_status));
313 
314  status->splitting = FALSE;
315  status->process_in_progress = FALSE;
316  status->mouse_on_progress_bar = FALSE;
317 
318  status->currently_compute_douglas_peucker_filters = FALSE;
319  status->show_silence_wave = FALSE;
320 
321  status->playing = FALSE;
322  status->timer_active = FALSE;
323  status->quick_preview_end_splitpoint = -1;
324  status->preview_start_splitpoint = -1;
325 
326  status->move_time = 0;
327 
328  status->button1_pressed = FALSE;
329  status->button2_pressed = FALSE;
330 
331  status->quick_preview = FALSE;
332 
333  status->button_x = 0;
334  status->button_x2 = 0;
335  status->button_y = 0;
336  status->button_y2 = 0;
337 
338  status->move_splitpoints = FALSE;
339  status->splitpoint_to_move = -1;
340  status->remove_splitpoints = FALSE;
341  status->select_splitpoints = FALSE;
342  status->check_splitpoint = FALSE;
343 
344  status->first_splitpoint_selected = -1;
345 
346  status->spin_mins = 0;
347  status->spin_secs = 0;
348  status->spin_hundr_secs = 0;
349 
350  g_snprintf(status->current_description, 255, "%s", _("description here"));
351 
352  status->preview_start_position = 0;
353  status->timeout_id = 0;
354 
355  status->currently_scanning_for_silence = FALSE;
356 
357  status->filename_to_split = NULL;
358 
359  status->douglas_callback_counter = 0;
360 
361  status->stream = FALSE;
362  status->only_press_pause = FALSE;
363 
364  status->change_volume = TRUE;
365  status->on_the_volume_button = FALSE;
366  status->file_browsed = FALSE;
367 
368  status->preview_row = 0;
369  status->selected_split_mode = SELECTED_SPLIT_NORMAL;
370 
371  status->should_trim = FALSE;
372 
373  status->file_selection_changed = FALSE;
374 
375  status->stop_split = FALSE;
376 
377  status->previous_distance_by_time = NULL;
378  status->previous_zoom_coeff = -2;
379  status->previous_interpolation_level = -2;
380  status->previous_first_time_drawed = -2;
381  status->previous_first_x_drawed = -2;
382  status->previous_second_x_drawed = -2;
383  status->previous_second_time_drawed = -2;
384 
385  return status;
386 }
387 
388 static player_infos *ui_player_infos_new()
389 {
390  player_infos *pi = g_malloc0(sizeof(player_infos));
391 
392 #ifndef NO_GSTREAMER
393  pi->song_artist = NULL;
394  pi->song_title = NULL;
395  pi->rate = 0;
396  pi->play = NULL;
397  pi->bus = NULL;
398  pi->_gstreamer_is_running = FALSE;
399 #endif
400 
401 #ifndef NO_AUDACIOUS
402  pi->dbus_proxy = NULL;
403  pi->dbus_connection = NULL;
404 #endif
405 
406  //snackamp
407  pi->in = NULL;
408  pi->out = NULL;
409  pi->connected = FALSE;
410 
411  return pi;
412 }
413 
414 static gui_state *ui_gui_new()
415 {
416  gui_state *gui = g_malloc0(sizeof(gui_state));
417 
418  gui->margin = 4;
419  gui->real_erase_split_length = 12;
420  gui->real_move_split_length = 16;
421  gui->real_checkbox_length = 12;
422  gui->real_wave_length = 96;
423 
424  gui->splitpoints_window = NULL;
425  gui->preferences_window = NULL;
426  gui->split_files_window = NULL;
427  gui->freedb_window = NULL;
428 
429  return gui;
430 }
431 
432 static void ui_main_window_free(ui_main_window **main_win)
433 {
434  if (!main_win || !*main_win)
435  {
436  return;
437  }
438 
439  g_free(*main_win);
440  *main_win = NULL;
441 }
442 
443 static void ui_infos_free(ui_infos **infos)
444 {
445  if (!infos || !*infos)
446  {
447  return;
448  }
449 
450  ui_main_window_free(&(*infos)->main_win);
451 
452  if ((*infos)->browser_directory)
453  {
454  g_free((*infos)->browser_directory);
455  (*infos)->browser_directory = NULL;
456  }
457 
458  if ((*infos)->text_options_list)
459  {
460  g_list_free((*infos)->text_options_list);
461  }
462 
463  if ((*infos)->silence_points)
464  {
465  g_free((*infos)->silence_points);
466  (*infos)->silence_points = NULL;
467  (*infos)->number_of_silence_points = 0;
468  }
469 
470  g_array_free((*infos)->preview_time_windows, TRUE);
471 
472  g_free(*infos);
473  *infos = NULL;
474 }
475 
476 static void ui_status_free(gui_status **status)
477 {
478  if (!status || !*status)
479  {
480  return;
481  }
482 
483  if ((*status)->previous_distance_by_time != NULL)
484  {
485  g_hash_table_destroy((*status)->previous_distance_by_time);
486  (*status)->previous_distance_by_time = NULL;
487  }
488 
489  g_free(*status);
490  *status = NULL;
491 }
492 
493 static void ui_player_infos_free(player_infos **pi)
494 {
495  if (!pi || !*pi)
496  {
497  return;
498  }
499 
500  g_free(*pi);
501  *pi = NULL;
502 }
503 
504 static void ui_gui_free(gui_state **gui)
505 {
506  if (!gui|| !*gui)
507  {
508  return;
509  }
510 
511  g_free(*gui);
512  *gui = NULL;
513 }
514