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-2013 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  ui->importing_cue_from_configuration_directory = FALSE;
119 
120  return ui;
121 }
122 
123 void ui_state_free(ui_state *ui)
124 {
125  if (!ui) { return; }
126 
127  ui_infos_free(&ui->infos);
128  pm_free(&ui->preferences);
129 
130  if (ui->mp3splt_state)
131  {
132  mp3splt_free_state(ui->mp3splt_state);
133  }
134 
135  g_array_free(ui->splitpoints, TRUE);
136 
137  ui_status_free(&ui->status);
138  ui_gui_free(&ui->gui);
139  ui_player_infos_free(&ui->pi);
140 
141  clear_mutex(&ui->variables_mutex);
142 
143  g_free(ui);
144 }
145 
146 void ui_register_spinner_int_preference(gchar *main_key, gchar *second_key,
147  gint default_value, GtkWidget *spinner,
148  void (*update_spinner_value_cb)(GtkWidget *spinner, gpointer data),
149  gpointer user_data_for_cb, ui_state *ui)
150 {
151  pm_register_spinner_int_preference(main_key, second_key,
152  default_value, spinner, update_spinner_value_cb, user_data_for_cb, ui->preferences);
153 }
154 
155 void ui_register_range_preference(gchar *main_key, gchar *second_key,
156  gint default_value, GtkWidget *range,
157  void (*update_adjustment_value)(GtkAdjustment *adjustment, gpointer data),
158  gpointer user_data_for_cb, ui_state *ui)
159 {
160  pm_register_range_preference(main_key, second_key,
161  default_value, range, update_adjustment_value, user_data_for_cb, ui->preferences);
162 }
163 
164 void ui_load_preferences(ui_state *ui)
165 {
166  load_preferences(ui);
167 }
168 
169 void ui_save_preferences(GtkWidget *dummy_widget, ui_state *ui)
170 {
171  save_preferences(ui);
172 }
173 
174 void ui_fail(ui_state *ui, const gchar *message, ...)
175 {
176  if (message != NULL)
177  {
178  gchar formatted_message[1024] = { '\0' };
179 
180  va_list ap;
181  va_start(ap, message);
182  g_vsnprintf(formatted_message, 1024, message, ap);
183  va_end(ap);
184 
185  fprintf(stderr, formatted_message);
186  fflush(stderr);
187  }
188 
189  ui->return_code = EXIT_FAILURE;
190 
191  ui_state_free(ui);
192 
193  exit(1);
194 }
195 
196 static void ui_main_window_new(ui_infos *infos)
197 {
198  ui_main_window *main_win = g_malloc0(sizeof(ui_main_window));
199 
200  main_win->root_x_pos = 0;
201  main_win->root_y_pos = 0;
202 
203  main_win->width = UI_DEFAULT_WIDTH;
204  main_win->height = UI_DEFAULT_HEIGHT;
205 
206  infos->main_win = main_win;
207 }
208 
209 static void ui_infos_new(ui_state *ui)
210 {
211  ui_infos *infos = g_malloc0(sizeof(ui_infos));
212 
213  ui_main_window_new(infos);
214 
215  infos->browser_directory = NULL;
216  infos->text_options_list = NULL;
217 
218  infos->silence_points = NULL;
219  infos->malloced_num_of_silence_points = 0;
220  infos->number_of_silence_points = 0;
221 
222  infos->player_seconds = 0;
223  infos->player_minutes = 0;
224  infos->player_hundr_secs = 0;
225  infos->player_seconds2 = 0;
226  infos->player_minutes2 = 0;
227  infos->player_hundr_secs2 = 0;
228 
229  infos->total_time = 0;
230  infos->current_time = 0;
231 
232  infos->splitnumber = 0;
233  infos->width_drawing_area = 0;
234  infos->zoom_coeff = 2.0;
235  infos->zoom_coeff_old = 2.0;
236 
237  infos->hundr_secs_th = 20;
238  infos->tens_of_secs_th = 3 * 100;
239  infos->secs_th = 40 * 100;
240  infos->ten_secs_th = 3 * 6000;
241  infos->minutes_th = 20 * 6000;
242  infos->ten_minutes_th = 3 * 3600 * 100;
243 
244  infos->one_minute_time = 1 * 6000;
245  infos->three_minutes_time = 3 * 6000;
246  infos->six_minutes_time = 6 * 6000;
247  infos->ten_minutes_time = 10 * 6000;
248  infos->twenty_minutes_time = 20 * 6000;
249  infos->fourty_minutes_time = 40 * 6000;
250 
251  GArray *preview_time_windows = g_array_new(TRUE, TRUE, sizeof(gint));
252  g_array_append_val(preview_time_windows, infos->one_minute_time);
253  g_array_append_val(preview_time_windows, infos->three_minutes_time);
254  g_array_append_val(preview_time_windows, infos->six_minutes_time);
255  g_array_append_val(preview_time_windows, infos->ten_minutes_time);
256  g_array_append_val(preview_time_windows, infos->twenty_minutes_time);
257  g_array_append_val(preview_time_windows, infos->fourty_minutes_time);
258  infos->preview_time_windows = preview_time_windows;
259 
260  infos->filtered_points_presence = NULL;
261  infos->silence_wave_number_of_points_threshold = DEFAULT_SILENCE_WAVE_NUMBER_OF_POINTS_THRESHOLD;
262 
263  infos->selected_player = PLAYER_GSTREAMER;
264 
265  infos->douglas_peucker_thresholds_defaults[0] = 2.0;
266  infos->douglas_peucker_thresholds_defaults[1] = 5.0;
267  infos->douglas_peucker_thresholds_defaults[2] = 8.0;
268  infos->douglas_peucker_thresholds_defaults[3] = 11.0;
269  infos->douglas_peucker_thresholds_defaults[4] = 15.0;
270  infos->douglas_peucker_thresholds_defaults[5] = 22.0;
271 
272  infos->douglas_peucker_thresholds[0] = infos->douglas_peucker_thresholds_defaults[0];
273  infos->douglas_peucker_thresholds[1] = infos->douglas_peucker_thresholds_defaults[1];
274  infos->douglas_peucker_thresholds[2] = infos->douglas_peucker_thresholds_defaults[2];
275  infos->douglas_peucker_thresholds[3] = infos->douglas_peucker_thresholds_defaults[3];
276  infos->douglas_peucker_thresholds[4] = infos->douglas_peucker_thresholds_defaults[4];
277  infos->douglas_peucker_thresholds[5] = infos->douglas_peucker_thresholds_defaults[5];
278 
279  infos->debug_is_active = FALSE;
280 
281  infos->silence_threshold_value = SPLT_DEFAULT_PARAM_THRESHOLD;
282  infos->silence_offset_value = SPLT_DEFAULT_PARAM_OFFSET;
283  infos->silence_number_of_tracks = SPLT_DEFAULT_PARAM_TRACKS;
284  infos->silence_minimum_length = SPLT_DEFAULT_PARAM_MINIMUM_LENGTH;
285  infos->silence_minimum_track_length = SPLT_DEFAULT_PARAM_MINIMUM_TRACK_LENGTH;
286  infos->silence_remove_silence_between_tracks = FALSE;
287 
288  infos->freedb_table_number = 0;
289  infos->freedb_selected_id = -1;
290 
291  infos->playlist_tree_number = 0;
292  infos->multiple_files_tree_number = 0;
293 
294  infos->freedb_search_results = NULL;
295 
296  infos->split_file_mode = FILE_MODE_SINGLE;
297 
298  infos->outputdirname = NULL;
299 
300  gint i = 0;
301  for (i = 0; i < 6;i++)
302  {
303  infos->preview_indexes[i].index = 0;
304  infos->preview_indexes[i].data = NULL;
305  }
306 
307  infos->timeout_value = DEFAULT_TIMEOUT_VALUE;
308 
309  ui->infos = infos;
310 }
311 
312 static gui_status *ui_status_new(ui_state *ui)
313 {
314  gui_status *status = g_malloc0(sizeof(gui_status));
315 
316  status->splitting = FALSE;
317  status->process_in_progress = FALSE;
318  status->mouse_on_progress_bar = FALSE;
319 
320  status->currently_compute_douglas_peucker_filters = FALSE;
321  status->show_silence_wave = FALSE;
322 
323  status->playing = FALSE;
324  status->timer_active = FALSE;
325  status->quick_preview_end_splitpoint = -1;
326  status->preview_start_splitpoint = -1;
327 
328  status->move_time = 0;
329 
330  status->button1_pressed = FALSE;
331  status->button2_pressed = FALSE;
332 
333  status->quick_preview = FALSE;
334 
335  status->button_x = 0;
336  status->button_x2 = 0;
337  status->button_y = 0;
338  status->button_y2 = 0;
339 
340  status->move_splitpoints = FALSE;
341  status->splitpoint_to_move = -1;
342  status->remove_splitpoints = FALSE;
343  status->select_splitpoints = FALSE;
344  status->check_splitpoint = FALSE;
345 
346  status->first_splitpoint_selected = -1;
347 
348  status->spin_mins = 0;
349  status->spin_secs = 0;
350  status->spin_hundr_secs = 0;
351 
352  g_snprintf(status->current_description, 255, "%s", _("description here"));
353 
354  status->preview_start_position = 0;
355  status->timeout_id = 0;
356 
357  status->currently_scanning_for_silence = FALSE;
358 
359  status->filename_to_split = NULL;
360 
361  status->douglas_callback_counter = 0;
362 
363  status->stream = FALSE;
364  status->only_press_pause = FALSE;
365 
366  status->change_volume = TRUE;
367  status->on_the_volume_button = FALSE;
368  status->file_browsed = FALSE;
369 
370  status->preview_row = 0;
371  status->selected_split_mode = SELECTED_SPLIT_NORMAL;
372 
373  status->should_trim = FALSE;
374 
375  status->file_selection_changed = FALSE;
376 
377  status->stop_split = FALSE;
378 
379  status->previous_distance_by_time = NULL;
380  status->previous_zoom_coeff = -2;
381  status->previous_interpolation_level = -2;
382  status->previous_first_time_drawed = -2;
383  status->previous_first_x_drawed = -2;
384  status->previous_second_x_drawed = -2;
385  status->previous_second_time_drawed = -2;
386 
387  return status;
388 }
389 
390 static player_infos *ui_player_infos_new()
391 {
392  player_infos *pi = g_malloc0(sizeof(player_infos));
393 
394 #ifndef NO_GSTREAMER
395  pi->song_artist = NULL;
396  pi->song_title = NULL;
397  pi->rate = 0;
398  pi->play = NULL;
399  pi->bus = NULL;
400  pi->_gstreamer_is_running = FALSE;
401 #endif
402 
403 #ifndef NO_AUDACIOUS
404  pi->dbus_proxy = NULL;
405  pi->dbus_connection = NULL;
406 #endif
407 
408  //snackamp
409  pi->in = NULL;
410  pi->out = NULL;
411  pi->connected = FALSE;
412 
413  return pi;
414 }
415 
416 static gui_state *ui_gui_new()
417 {
418  gui_state *gui = g_malloc0(sizeof(gui_state));
419 
420  gui->margin = 4;
421  gui->real_erase_split_length = 12;
422  gui->real_move_split_length = 16;
423  gui->real_checkbox_length = 12;
424  gui->real_wave_length = 96;
425 
426  gui->splitpoints_window = NULL;
427  gui->preferences_window = NULL;
428  gui->split_files_window = NULL;
429  gui->freedb_window = NULL;
430 
431  return gui;
432 }
433 
434 static void ui_main_window_free(ui_main_window **main_win)
435 {
436  if (!main_win || !*main_win)
437  {
438  return;
439  }
440 
441  g_free(*main_win);
442  *main_win = NULL;
443 }
444 
445 static void ui_infos_free(ui_infos **infos)
446 {
447  if (!infos || !*infos)
448  {
449  return;
450  }
451 
452  ui_main_window_free(&(*infos)->main_win);
453 
454  if ((*infos)->browser_directory)
455  {
456  g_free((*infos)->browser_directory);
457  (*infos)->browser_directory = NULL;
458  }
459 
460  if ((*infos)->text_options_list)
461  {
462  g_list_free((*infos)->text_options_list);
463  }
464 
465  if ((*infos)->silence_points)
466  {
467  g_free((*infos)->silence_points);
468  (*infos)->silence_points = NULL;
469  (*infos)->number_of_silence_points = 0;
470  }
471 
472  g_array_free((*infos)->preview_time_windows, TRUE);
473 
474  g_free(*infos);
475  *infos = NULL;
476 }
477 
478 static void ui_status_free(gui_status **status)
479 {
480  if (!status || !*status)
481  {
482  return;
483  }
484 
485  if ((*status)->previous_distance_by_time != NULL)
486  {
487  g_hash_table_destroy((*status)->previous_distance_by_time);
488  (*status)->previous_distance_by_time = NULL;
489  }
490 
491  g_free(*status);
492  *status = NULL;
493 }
494 
495 static void ui_player_infos_free(player_infos **pi)
496 {
497  if (!pi || !*pi)
498  {
499  return;
500  }
501 
502  g_free(*pi);
503  *pi = NULL;
504 }
505 
506 static void ui_gui_free(gui_state **gui)
507 {
508  if (!gui|| !*gui)
509  {
510  return;
511  }
512 
513  g_free(*gui);
514  *gui = NULL;
515 }
516 
void load_preferences(ui_state *ui)
Read the preferences from the preferences file.