~ubuntu-branches/debian/sid/gnac/sid

« back to all changes in this revision

Viewing changes to src/profiles/gnac-profiles-manager.c

  • Committer: Package Import Robot
  • Author(s): Khalid El Fathi
  • Date: 2012-04-14 20:15:31 UTC
  • Revision ID: package-import@ubuntu.com-20120414201531-2gcfq8fs94kgp9qr
Tags: upstream-0.2.4
ImportĀ upstreamĀ versionĀ 0.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of GNAC - Gnome Audio Converter
 
3
 *
 
4
 * Copyright (C) 2007 - 2012 Gnac
 
5
 *    
 
6
 *    - DUPASQUIER  Benoit    <bdupasqu@src.gnome.org>
 
7
 *    - JOAQUIM     David     <djoaquim@src.gnome.org>
 
8
 *    - ROUX        Alexandre <alexroux@src.gnome.org>
 
9
 *
 
10
 * GNAC is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation; either version 3 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * GNAC is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with GNAC; if not, write to the Free Software
 
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 
23
 * Boston, MA  02110-1301  USA
 
24
 */
 
25
 
 
26
#ifdef HAVE_CONFIG_H
 
27
#include "config.h"
 
28
#endif
 
29
 
 
30
#include <gio/gio.h>
 
31
#include <glib/gi18n.h>
 
32
 
 
33
#include "gnac-main.h"
 
34
#include "gnac-profiles.h"
 
35
#include "gnac-profiles-default.h"
 
36
#include "gnac-profiles-manager.h"
 
37
#include "gnac-settings.h"
 
38
#include "gnac-ui.h"
 
39
#include "gnac-ui-utils.h"
 
40
#include "gnac-utils.h"
 
41
#include "libgnac-debug.h"
 
42
 
 
43
/* Translators: Suffix added to a copied profile: 'profile (copy).xml' */
 
44
#define GNAC_COPY_SUFFIX N_(" (copy)")
 
45
 
 
46
enum {
 
47
  COL_NAME,
 
48
  COL_FORMAT,
 
49
  COL_EXTENSION,
 
50
  COL_PROFILE,
 
51
  NUM_COLS
 
52
};
 
53
 
 
54
enum {
 
55
  DND_TARGET_URI,
 
56
  DND_TARGET_PLAIN
 
57
};
 
58
 
 
59
typedef struct {
 
60
  gchar **uris;
 
61
  guint   info;
 
62
} ThreadCopyData;
 
63
 
 
64
typedef struct {
 
65
  const gchar  *path;
 
66
  const gchar  *name;
 
67
  GError      **error;
 
68
  gboolean      toggle;
 
69
} CopyData;
 
70
 
 
71
GtkBuilder *profiles_mgr_builder = NULL;
 
72
 
 
73
static GtkTargetEntry target_list[] =  {
 
74
  { "text/uri-list", 0, DND_TARGET_URI   },
 
75
  { "text/plain"   , 0, DND_TARGET_PLAIN }
 
76
};
 
77
 
 
78
static guint n_targets = G_N_ELEMENTS(target_list);
 
79
 
 
80
static void
 
81
gnac_profiles_mgr_populate(void);
 
82
 
 
83
static void
 
84
gnac_profiles_mgr_import_default_profiles(void);
 
85
 
 
86
static void
 
87
gnac_profiles_mgr_clear(void);
 
88
 
 
89
static void
 
90
gnac_profiles_mgr_insert(gpointer profile);
 
91
 
 
92
static void
 
93
gnac_profiles_mgr_remove(GtkTreeRowReference *ref);
 
94
 
 
95
static void
 
96
gnac_profiles_mgr_delete_profile_file(const gchar *name);
 
97
 
 
98
static void
 
99
gnac_profiles_mgr_get(GtkTreeRowReference  *ref,
 
100
                      gint                  col_number,
 
101
                      AudioProfileGeneric **profile);
 
102
 
 
103
static void
 
104
gnac_profiles_mgr_set(GtkTreeRowReference *ref, ...);
 
105
 
 
106
static GList *
 
107
gnac_profiles_mgr_get_selected_rows(void);
 
108
 
 
109
static void
 
110
gnac_profiles_mgr_activate_buttons(gboolean activate);
 
111
 
 
112
static void
 
113
gnac_profiles_mgr_set_progress_bar_fraction(gdouble fraction);
 
114
 
 
115
static void
 
116
gnac_profiles_mgr_display_status_message(const gchar *ok,
 
117
                                         const gchar *error);
 
118
 
 
119
static void
 
120
gnac_profiles_mgr_set_window_sensitive(gboolean sensitive);
 
121
 
 
122
static void
 
123
gnac_profiles_mgr_copy_and_load_files(gpointer data);
 
124
 
 
125
static gchar **
 
126
gnac_profiles_mgr_get_selected_uris(void);
 
127
 
 
128
static void
 
129
gnac_profiles_mgr_on_drag_profile_copied(goffset  current_num_bytes,
 
130
                                         goffset  total_num_bytes,
 
131
                                         gpointer user_data);
 
132
 
 
133
static void
 
134
gnac_profiles_mgr_on_edit_profile(GtkWidget *widget,
 
135
                                  gpointer   data);
 
136
 
 
137
static void
 
138
gnac_profiles_mgr_on_new_profile(GtkWidget *widget,
 
139
                                 gpointer   data);
 
140
 
 
141
static void
 
142
gnac_profiles_mgr_on_treeselection_changed(void);
 
143
 
 
144
 
 
145
static GObject *
 
146
gnac_profiles_mgr_get_object(const gchar *object_name)
 
147
{
 
148
  return gnac_ui_utils_get_object(profiles_mgr_builder, object_name);
 
149
}
 
150
 
 
151
 
 
152
static GtkWidget *
 
153
gnac_profiles_mgr_get_widget(const gchar *widget_name)
 
154
{
 
155
  return gnac_ui_utils_get_widget(profiles_mgr_builder, widget_name);
 
156
}
 
157
 
 
158
 
 
159
static void
 
160
gnac_profiles_mgr_set_widget_sensitive(const gchar *widget_name,
 
161
                                       gboolean     sensitive)
 
162
{
 
163
  gnac_ui_utils_set_widget_sensitive(profiles_mgr_builder, widget_name, sensitive);
 
164
}
 
165
 
 
166
 
 
167
static void
 
168
gnac_profiles_mgr_show_widget(const gchar *widget_name,
 
169
                              gboolean     show)
 
170
{
 
171
  GtkWidget *widget = gnac_profiles_mgr_get_widget(widget_name);
 
172
  if (show) {
 
173
    gtk_widget_show_all(widget);
 
174
  } else {
 
175
    gtk_widget_hide(widget);
 
176
  }
 
177
}
 
178
 
 
179
 
 
180
static void
 
181
gnac_profiles_mgr_show_description_frame(gboolean show)
 
182
{
 
183
  gnac_profiles_mgr_show_widget("frame-description", show);
 
184
}
 
185
 
 
186
 
 
187
static void
 
188
gnac_profiles_mgr_show_import_progressbar(gboolean show)
 
189
{
 
190
  gnac_profiles_mgr_show_widget("frame-import", show);
 
191
}
 
192
 
 
193
 
 
194
static void
 
195
gnac_profiles_mgr_set_parent(void)
 
196
{
 
197
  GtkWindow *parent = GTK_WINDOW(gnac_ui_get_widget("main_window"));
 
198
  GtkWindow *window = GTK_WINDOW(
 
199
      gnac_profiles_mgr_get_widget("profile_manager_window"));
 
200
  gtk_window_set_transient_for(window, parent);
 
201
  gnac_profiles_properties_set_parent(window);
 
202
}
 
203
 
 
204
 
 
205
void
 
206
gnac_profiles_mgr_init(void)
 
207
{
 
208
  profiles_mgr_builder = gnac_ui_utils_create_gtk_builder(
 
209
      PKGDATADIR "/profiles/gnac-profiles-manager.xml");
 
210
 
 
211
  GtkTreeView *view = GTK_TREE_VIEW(
 
212
      gnac_profiles_mgr_get_widget("profile_treeview"));
 
213
  GtkTreeSelection *selection = gtk_tree_view_get_selection(view);
 
214
 
 
215
  g_signal_connect(G_OBJECT(selection), "changed", 
 
216
      G_CALLBACK(gnac_profiles_mgr_on_treeselection_changed), NULL);
 
217
 
 
218
  gtk_drag_dest_set(GTK_WIDGET(view), GTK_DEST_DEFAULT_ALL, target_list,
 
219
      n_targets, GDK_ACTION_COPY);
 
220
 
 
221
  gtk_drag_source_set(GTK_WIDGET(view), GDK_BUTTON1_MASK, target_list,
 
222
      n_targets, GDK_ACTION_COPY);
 
223
 
 
224
  gnac_profiles_properties_init();
 
225
  gnac_profiles_mgr_set_parent();
 
226
 
 
227
  gnac_profiles_mgr_show_import_progressbar(FALSE);
 
228
  gnac_profiles_mgr_display_status_message(NULL, NULL);
 
229
 
 
230
  gnac_profiles_mgr_populate();
 
231
}
 
232
 
 
233
 
 
234
static GFile *
 
235
gnac_profiles_mgr_get_profiles_dir(void)
 
236
{
 
237
  saved_profiles_dir = g_build_filename(g_get_user_data_dir(),
 
238
      PACKAGE, "profiles", NULL);
 
239
  GFile *dir = g_file_new_for_path(saved_profiles_dir);
 
240
  if (g_file_query_exists(dir, NULL)) {
 
241
    return dir;
 
242
  }
 
243
 
 
244
  GError *error = NULL;
 
245
 
 
246
  if (!g_file_make_directory_with_parents(dir, NULL, &error)) {
 
247
    libgnac_warning("%s: %s",
 
248
        _("Unable to create the profiles directory"), error->message);
 
249
    g_clear_error(&error);
 
250
    return NULL;
 
251
  }
 
252
 
 
253
  return dir;
 
254
}
 
255
 
 
256
 
 
257
void
 
258
gnac_profiles_mgr_list_profiles(void)
 
259
{
 
260
  GFile *dir = gnac_profiles_mgr_get_profiles_dir();
 
261
 
 
262
  GError *error = NULL;
 
263
  GFileEnumerator *files = g_file_enumerate_children(dir,
 
264
      G_FILE_ATTRIBUTE_STANDARD_NAME ","
 
265
      G_FILE_ATTRIBUTE_STANDARD_TYPE,
 
266
      G_FILE_QUERY_INFO_NONE, NULL, &error);
 
267
 
 
268
  if (!files && error) {
 
269
    g_clear_error(&error);
 
270
    /* no profiles found, try to import the default ones */
 
271
    gnac_profiles_mgr_import_default_profiles();
 
272
    files = g_file_enumerate_children(dir,
 
273
        G_FILE_ATTRIBUTE_STANDARD_NAME ","
 
274
        G_FILE_ATTRIBUTE_STANDARD_TYPE,
 
275
        G_FILE_QUERY_INFO_NONE, NULL, &error);
 
276
    if (!files && error) {
 
277
      g_printerr(_("No profiles available"));
 
278
      libgnac_warning("%s", error->message);
 
279
      g_clear_error(&error);
 
280
      return;
 
281
    }
 
282
  }
 
283
 
 
284
  g_print(_("Available audio profiles:"));
 
285
  g_print("\n\n");
 
286
 
 
287
  gchar *last_used_profile = gnac_settings_get_string(
 
288
      GNAC_KEY_LAST_USED_PROFILE);
 
289
 
 
290
  GFile *profile_file = g_file_enumerator_get_container(files);
 
291
  gchar *profile_file_path = g_file_get_path(profile_file);
 
292
 
 
293
  GFileInfo *file_info;
 
294
  GSList *profiles = NULL;
 
295
 
 
296
  while ((file_info = g_file_enumerator_next_file(files, NULL, NULL))) {
 
297
    if (g_file_info_get_file_type(file_info) == G_FILE_TYPE_REGULAR) {
 
298
      AudioProfileGeneric *profile;
 
299
      const gchar *profile_file_name = g_file_info_get_name(file_info);
 
300
      gchar *profile_file_full_path = g_build_filename(profile_file_path,
 
301
          profile_file_name, NULL);
 
302
      gnac_profiles_default_load_generic_audio_profile(profile_file_full_path,
 
303
          &profile);
 
304
      if (profile) {
 
305
        gpointer name = (profile->generic)->name;
 
306
        profiles = g_slist_prepend(profiles, name);
 
307
      }
 
308
 
 
309
      g_free(profile_file_full_path);
 
310
    }
 
311
 
 
312
    g_object_unref(file_info);
 
313
  }
 
314
 
 
315
  g_free(profile_file_path);
 
316
 
 
317
  profiles = g_slist_reverse(profiles);
 
318
 
 
319
  guint count = g_slist_length(profiles);
 
320
  if (count == 0) {
 
321
    g_print("\t");
 
322
    g_print(_("No profiles available"));
 
323
    g_print("\n");
 
324
  } else {
 
325
    /* check if last_used_profile exists */
 
326
    if (!g_slist_find_custom(profiles, last_used_profile,
 
327
        (GCompareFunc) g_strcmp0))
 
328
    {
 
329
      last_used_profile = NULL;
 
330
    }
 
331
 
 
332
    GSList *tmp;
 
333
    for (tmp = profiles; tmp; tmp = g_slist_next(tmp)) {
 
334
      gint pos = g_slist_position(profiles, tmp);
 
335
      gchar *count_str = g_strdup_printf("%u", pos+1);
 
336
      /* if last_used_profile is not set, assume the
 
337
       * first profile was last used */
 
338
      gchar *name = tmp->data;
 
339
      gboolean found = ((pos == 0 && !last_used_profile)
 
340
          || gnac_utils_str_equal(name, last_used_profile));
 
341
      g_print("\t%2s) %s\n", found ? "*" : count_str, name);
 
342
      g_free(count_str);
 
343
    }
 
344
  }
 
345
 
 
346
  g_print("\n");
 
347
 
 
348
  g_slist_free(profiles);
 
349
  g_free(last_used_profile);
 
350
  g_object_unref(dir);
 
351
  g_file_enumerator_close(files, NULL, NULL);
 
352
  g_object_unref(files);
 
353
}
 
354
 
 
355
 
 
356
static void
 
357
gnac_profiles_mgr_populate(void)
 
358
 
359
  gnac_profiles_mgr_clear();
 
360
 
 
361
  GError *error = NULL;
 
362
 
 
363
  GFile *dir = g_file_new_for_path(saved_profiles_dir);
 
364
  if (!g_file_query_exists(dir, NULL)) {
 
365
    if (!g_file_make_directory_with_parents(dir, NULL, &error)) {
 
366
      const gchar *msg = N_("Unable to create the profiles directory");
 
367
      libgnac_warning("%s %s: %s",
 
368
          msg, _("You may not be able to save your profiles"), error->message);
 
369
      gnac_profiles_mgr_display_status_message(NULL, msg);
 
370
      g_clear_error(&error);
 
371
      return;
 
372
    } else {
 
373
      gnac_profiles_mgr_import_default_profiles();
 
374
    }
 
375
  }
 
376
 
 
377
  GFileEnumerator *files = g_file_enumerate_children(dir,
 
378
      G_FILE_ATTRIBUTE_STANDARD_NAME ","
 
379
      G_FILE_ATTRIBUTE_STANDARD_TYPE,
 
380
      G_FILE_QUERY_INFO_NONE, NULL, &error);
 
381
  if (!files && error) {
 
382
    const gchar *msg = N_("Unable to browse the profiles directory");
 
383
    libgnac_warning("%s: %s", msg, error->message);
 
384
    gnac_profiles_mgr_display_status_message(NULL, msg);
 
385
    g_clear_error(&error);
 
386
    return;
 
387
  }
 
388
 
 
389
  GFileInfo *file_info = g_file_enumerator_next_file(files, NULL, NULL);
 
390
  if (!file_info) {
 
391
    gnac_profiles_mgr_import_default_profiles();
 
392
    g_file_enumerator_close(files, NULL, NULL);
 
393
    files = g_file_enumerate_children(dir,
 
394
        G_FILE_ATTRIBUTE_STANDARD_NAME ","
 
395
        G_FILE_ATTRIBUTE_STANDARD_TYPE,
 
396
        G_FILE_QUERY_INFO_NONE, NULL, &error);
 
397
    file_info = g_file_enumerator_next_file(files, NULL, NULL);
 
398
  }
 
399
 
 
400
  GFile *profile_file = g_file_enumerator_get_container(files);
 
401
  gchar *profile_file_path = g_file_get_path(profile_file);
 
402
 
 
403
  while (file_info) {
 
404
    if (g_file_info_get_file_type(file_info) == G_FILE_TYPE_REGULAR) {
 
405
      const gchar *profile_file_name = g_file_info_get_name(file_info);
 
406
      gchar *profile_file_full_path = g_build_filename(profile_file_path,
 
407
          profile_file_name, NULL);
 
408
      gpointer profile = gnac_profiles_properties_load_profile_from_file(
 
409
          profile_file_full_path, profile_file_name, &error);
 
410
 
 
411
      g_free(profile_file_full_path);
 
412
 
 
413
      if (profile) {
 
414
        gnac_profiles_mgr_insert(profile);
 
415
      } else if (error) {
 
416
        libgnac_warning("%s", error->message);
 
417
        g_clear_error(&error);
 
418
      }
 
419
    }
 
420
 
 
421
    g_object_unref(file_info);
 
422
    file_info = g_file_enumerator_next_file(files, NULL, NULL);
 
423
  }
 
424
 
 
425
  gnac_profiles_mgr_display_status_message(NULL, NULL);
 
426
 
 
427
  g_object_unref(dir);
 
428
  g_free(profile_file_path);
 
429
  g_file_enumerator_close(files, NULL, NULL);
 
430
  g_object_unref(files);
 
431
}
 
432
 
 
433
 
 
434
static GFileEnumerator *
 
435
gnac_profiles_mgr_get_default_profiles_enumerator(void)
 
436
{
 
437
  GFile *dir = g_file_new_for_path(PKGDATADIR "/profiles/default");
 
438
  if (!g_file_query_exists(dir, NULL)) {
 
439
    libgnac_warning("%s", _("Unable to find the default profiles directory"));
 
440
    return NULL;
 
441
  }
 
442
  
 
443
  GError *error = NULL;
 
444
  GFileEnumerator *files = g_file_enumerate_children(dir,
 
445
      G_FILE_ATTRIBUTE_STANDARD_NAME ","
 
446
      G_FILE_ATTRIBUTE_STANDARD_TYPE,
 
447
      G_FILE_QUERY_INFO_NONE, NULL, &error);
 
448
  if (!files) {
 
449
    libgnac_warning("%s: %s",
 
450
        _("Unable to browse the default profiles directory"),
 
451
        error->message);
 
452
    g_clear_error(&error);
 
453
    g_object_unref(dir);
 
454
    return NULL;
 
455
  }
 
456
 
 
457
  g_object_unref(dir);
 
458
 
 
459
  return files;
 
460
}
 
461
 
 
462
 
 
463
static void
 
464
gnac_profiles_mgr_import_default_profiles(void)
 
465
{
 
466
  gnac_profiles_mgr_clear();
 
467
 
 
468
  GFileEnumerator *files = gnac_profiles_mgr_get_default_profiles_enumerator();
 
469
 
 
470
  GFile *profile_file = g_file_enumerator_get_container(files);
 
471
  gchar *profile_file_path = g_file_get_path(profile_file);
 
472
 
 
473
  GFileInfo *file_info;
 
474
  while ((file_info = g_file_enumerator_next_file(files, NULL, NULL))) {
 
475
    if (g_file_info_get_file_type(file_info) == G_FILE_TYPE_REGULAR) {
 
476
      GError *error = NULL;
 
477
      const gchar *profile_file_name = g_file_info_get_name(file_info);
 
478
      gchar *profile_file_full_path = g_build_filename(profile_file_path,
 
479
          profile_file_name, NULL);
 
480
      gpointer profile = gnac_profiles_properties_load_profile_from_file(
 
481
          profile_file_full_path, profile_file_name, &error);
 
482
 
 
483
      if (profile) {
 
484
        gnac_profiles_properties_save_profile(profile);
 
485
      } else if (error) {
 
486
        libgnac_warning("%s", error->message);
 
487
        g_clear_error(&error);
 
488
      }
 
489
 
 
490
      g_free(profile_file_full_path);
 
491
      gnac_profiles_properties_free_audio_profile(profile);
 
492
    }
 
493
 
 
494
    g_free(profile_file_path);
 
495
    g_object_unref(file_info);
 
496
  }
 
497
 
 
498
  g_file_enumerator_close(files, NULL, NULL);
 
499
  g_object_unref(files);
 
500
}
 
501
 
 
502
 
 
503
GList *
 
504
gnac_profiles_mgr_get_profiles_list(void)
 
505
{
 
506
  GtkTreeIter iter;
 
507
  GtkTreeModel *model = GTK_TREE_MODEL(
 
508
      gnac_profiles_mgr_get_object("liststore"));
 
509
  gboolean has_next = gtk_tree_model_get_iter_first(model, &iter);
 
510
 
 
511
  GList *list = NULL;
 
512
  while (has_next) {
 
513
    AudioProfileGeneric *profile;
 
514
    gtk_tree_model_get(model, &iter, COL_PROFILE, &profile, -1);
 
515
    list = g_list_prepend(list, profile);
 
516
    has_next = gtk_tree_model_iter_next(model, &iter);
 
517
  }
 
518
 
 
519
  return g_list_reverse(list);
 
520
}
 
521
 
 
522
 
 
523
void
 
524
gnac_profiles_mgr_destroy(void)
 
525
{
 
526
  if (profiles_mgr_builder) {
 
527
    gnac_profiles_mgr_clear();
 
528
    GtkWidget *widget = gnac_profiles_mgr_get_widget("profile_manager_window");
 
529
    gtk_widget_destroy(widget);
 
530
    g_object_unref(profiles_mgr_builder);
 
531
  }
 
532
 
 
533
  gnac_profiles_properties_destroy();
 
534
}
 
535
 
 
536
 
 
537
void
 
538
gnac_profiles_mgr_show(void)
 
539
{
 
540
  static gboolean first_show = TRUE;
 
541
 
 
542
  if (!first_show) {
 
543
    gnac_profiles_mgr_populate();
 
544
  } else {
 
545
    first_show = FALSE;
 
546
  }
 
547
 
 
548
  GtkWidget *window = gnac_profiles_mgr_get_widget("profile_manager_window");
 
549
  gtk_widget_show(window);
 
550
 
 
551
  GList *selected = gnac_profiles_mgr_get_selected_rows();
 
552
  if (!selected) {
 
553
    gnac_profiles_mgr_show_description_frame(FALSE);
 
554
    gnac_profiles_mgr_activate_buttons(FALSE);
 
555
  }
 
556
 
 
557
  g_list_free_full(selected, (GDestroyNotify) gtk_tree_row_reference_free);
 
558
}
 
559
 
 
560
 
 
561
static void
 
562
gnac_profiles_mgr_clear(void)
 
563
{
 
564
  GtkTreeIter iter;
 
565
  GtkTreeModel *model = GTK_TREE_MODEL(
 
566
      gnac_profiles_mgr_get_object("liststore"));
 
567
  gboolean has_next = gtk_tree_model_get_iter_first(model, &iter);
 
568
 
 
569
  while (has_next) {
 
570
    AudioProfileGeneric *profile;
 
571
    gtk_tree_model_get(model, &iter, COL_PROFILE, &profile, -1);
 
572
    gnac_profiles_properties_free_audio_profile(profile);
 
573
    has_next = gtk_tree_model_iter_next(model, &iter);
 
574
  }
 
575
 
 
576
  gtk_list_store_clear(GTK_LIST_STORE(model));
 
577
}
 
578
 
 
579
 
 
580
static void
 
581
gnac_profiles_mgr_insert(gpointer profile)
 
582
{
 
583
  GtkListStore *model = GTK_LIST_STORE(
 
584
      gnac_profiles_mgr_get_object("liststore"));
 
585
  AudioProfileGeneric *generic = AUDIO_PROFILE_GET_GENERIC(profile);
 
586
  gchar *formatted_name = gnac_profiles_properties_filter_text_for_display(
 
587
      generic->name, MAX_NAME_DISPLAY_SIZE);
 
588
  GtkTreeIter iter;
 
589
 
 
590
  gtk_list_store_append(model, &iter);
 
591
  gtk_list_store_set(model, &iter,
 
592
      COL_NAME, formatted_name,
 
593
      COL_FORMAT, generic->format_name,
 
594
      COL_EXTENSION, generic->extension,
 
595
      COL_PROFILE, profile,
 
596
      -1);
 
597
 
 
598
  g_free(formatted_name);
 
599
}
 
600
 
 
601
 
 
602
static void
 
603
gnac_profiles_mgr_remove(GtkTreeRowReference *ref)
 
604
{
 
605
  g_return_if_fail(ref);
 
606
 
 
607
  GtkTreeModel *model = GTK_TREE_MODEL(
 
608
      gnac_profiles_mgr_get_object("liststore"));
 
609
  GtkTreePath *path = gtk_tree_row_reference_get_path(ref);
 
610
  if (!path) return;
 
611
 
 
612
  GtkTreeIter iter;
 
613
 
 
614
  if (gtk_tree_model_get_iter(model, &iter, path)) {
 
615
    AudioProfileGeneric *profile;
 
616
    gtk_tree_model_get(model, &iter, COL_PROFILE, &profile, -1);
 
617
    gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
 
618
    gnac_profiles_properties_free_audio_profile(profile);
 
619
  }
 
620
 
 
621
  gtk_tree_path_free(path);
 
622
}
 
623
 
 
624
 
 
625
static void
 
626
gnac_profiles_mgr_get(GtkTreeRowReference  *ref,
 
627
                      gint                  col_number,
 
628
                      AudioProfileGeneric **profile)
 
629
{
 
630
  g_return_if_fail(ref);
 
631
 
 
632
  GtkTreeModel *model = GTK_TREE_MODEL(
 
633
      gnac_profiles_mgr_get_object("liststore"));
 
634
  GtkTreePath *path = gtk_tree_row_reference_get_path(ref);
 
635
  if (!path) return;
 
636
 
 
637
  GtkTreeIter iter;
 
638
 
 
639
  if (gtk_tree_model_get_iter(model, &iter, path)) {
 
640
    gtk_tree_model_get(model, &iter, col_number, profile, -1);
 
641
  }
 
642
 
 
643
  gtk_tree_path_free(path);
 
644
}
 
645
 
 
646
 
 
647
static void
 
648
gnac_profiles_mgr_set(GtkTreeRowReference *ref, ...)
 
649
{
 
650
  g_return_if_fail(ref);
 
651
 
 
652
  GtkListStore *list_store = GTK_LIST_STORE(
 
653
      gnac_profiles_mgr_get_object("liststore"));
 
654
  GtkTreePath *path = gtk_tree_row_reference_get_path(ref);
 
655
  if (!path) return;
 
656
 
 
657
  GtkTreeIter iter;
 
658
  va_list     ap;
 
659
 
 
660
  if (gtk_tree_model_get_iter(GTK_TREE_MODEL(list_store), &iter, path)) {
 
661
    va_start(ap, ref);
 
662
    gtk_list_store_set_valist(list_store, &iter, ap);
 
663
  }
 
664
 
 
665
  gtk_tree_path_free(path);
 
666
}
 
667
 
 
668
 
 
669
static GList *
 
670
gnac_profiles_mgr_get_selected_rows(void)
 
671
{
 
672
  GtkTreeModel *model = GTK_TREE_MODEL(
 
673
      gnac_profiles_mgr_get_object("liststore"));
 
674
  GtkTreeView *view = GTK_TREE_VIEW(
 
675
      gnac_profiles_mgr_get_widget("profile_treeview"));
 
676
  GtkTreeSelection *selection = gtk_tree_view_get_selection(view);
 
677
 
 
678
  /* Convert path to GtkTreeRowReference */
 
679
  GList *next = gtk_tree_selection_get_selected_rows(selection, &model);
 
680
  GList *next_temp = next;
 
681
  GList *row_references = NULL;
 
682
 
 
683
  while (next) {
 
684
    row_references = g_list_prepend(row_references,
 
685
        gtk_tree_row_reference_new(model, next->data));
 
686
    next = g_list_next(next);
 
687
  }
 
688
 
 
689
  row_references = g_list_reverse(row_references);
 
690
 
 
691
  g_list_free_full(next_temp, (GDestroyNotify) gtk_tree_path_free);
 
692
 
 
693
  return row_references;
 
694
}
 
695
 
 
696
 
 
697
static void
 
698
gnac_profiles_mgr_activate_buttons(gboolean activate)
 
699
{
 
700
  gnac_profiles_mgr_set_widget_sensitive("copy_button", activate);
 
701
  gnac_profiles_mgr_set_widget_sensitive("edit_button", activate);
 
702
  gnac_profiles_mgr_set_widget_sensitive("delete_button", activate);
 
703
}
 
704
 
 
705
 
 
706
static void
 
707
gnac_profiles_mgr_set_window_sensitive(gboolean sensitive)
 
708
{
 
709
  gnac_profiles_mgr_set_widget_sensitive("frame-profiles-list", sensitive);
 
710
  gnac_profiles_mgr_set_widget_sensitive("frame-description", sensitive);
 
711
  gnac_profiles_mgr_set_widget_sensitive("frame-status", sensitive);
 
712
  gnac_profiles_mgr_set_widget_sensitive("close_button", sensitive);
 
713
}
 
714
 
 
715
 
 
716
static void
 
717
gnac_profiles_mgr_set_progress_bar_fraction(gdouble fraction)
 
718
{
 
719
  GtkWidget *widget = gnac_profiles_mgr_get_widget("progressbar-import");
 
720
  gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(widget), fraction);
 
721
}
 
722
 
 
723
 
 
724
static void
 
725
gnac_profiles_mgr_display_status_message(const gchar *success_message,
 
726
                                         const gchar *error_message)
 
727
{
 
728
  GtkWidget *frame_status = gnac_profiles_mgr_get_widget("frame-status");
 
729
 
 
730
  if (!success_message && !error_message) {
 
731
    gtk_widget_hide(frame_status);
 
732
    return;
 
733
  }
 
734
 
 
735
  gtk_widget_show_all(frame_status);
 
736
 
 
737
  GtkWidget *label_ok = gnac_profiles_mgr_get_widget("label-status-ok");
 
738
  GtkWidget *image_ok = gnac_profiles_mgr_get_widget("image-status-ok");
 
739
 
 
740
  if (success_message) {
 
741
    gtk_label_set_markup(GTK_LABEL(label_ok), success_message);
 
742
    gtk_widget_show(label_ok);
 
743
    gtk_widget_show(image_ok);
 
744
  } else {
 
745
    gtk_widget_hide(label_ok);
 
746
    gtk_widget_hide(image_ok);
 
747
  }
 
748
 
 
749
  GtkWidget *label_error = gnac_profiles_mgr_get_widget("label-status-error");
 
750
  GtkWidget *image_error = gnac_profiles_mgr_get_widget("image-status-error");
 
751
 
 
752
  if (error_message) {
 
753
    gtk_label_set_markup(GTK_LABEL(label_error), error_message);
 
754
    gtk_widget_show(label_error);
 
755
    gtk_widget_show(image_error);
 
756
  } else {
 
757
    gtk_widget_hide(label_error);
 
758
    gtk_widget_hide(image_error);
 
759
  }
 
760
}
 
761
 
 
762
 
 
763
void
 
764
gnac_profiles_mgr_on_drag_data_received(GtkWidget        *widget,
 
765
                                        GdkDragContext   *context,
 
766
                                        gint              x,
 
767
                                        gint              y,
 
768
                                        GtkSelectionData *selection_data,
 
769
                                        guint             info,
 
770
                                        guint             time,
 
771
                                        gpointer          data)
 
772
{
 
773
  gchar **uris = g_uri_list_extract_uris((const gchar *)
 
774
      gtk_selection_data_get_data(selection_data));
 
775
  if (!uris) {
 
776
    gtk_drag_finish(context, FALSE, FALSE, time);
 
777
    return;
 
778
  }
 
779
 
 
780
  ThreadCopyData  *tcopy_data = g_malloc(sizeof(ThreadCopyData));
 
781
  tcopy_data->uris = uris;
 
782
  tcopy_data->info = info;
 
783
 
 
784
  GError *error = NULL;
 
785
 
 
786
  g_thread_create((GThreadFunc) gnac_profiles_mgr_copy_and_load_files,
 
787
      tcopy_data, TRUE, &error);
 
788
  if (error) {
 
789
    libgnac_debug("Failed to create thread: %s", error->message);
 
790
    gnac_profiles_mgr_display_status_message(NULL,
 
791
        _("Impossible to import file(s)"));
 
792
    g_clear_error(&error);
 
793
  }
 
794
 
 
795
  gtk_drag_finish(context, TRUE, FALSE, time);
 
796
}
 
797
 
 
798
 
 
799
static gchar *
 
800
gnac_profiles_mgr_get_file_name(GFile *file,
 
801
                                guint  target_type)
 
802
{
 
803
  GError *error = NULL;
 
804
 
 
805
  GFileInfo *file_info = g_file_query_info(file,
 
806
      G_FILE_ATTRIBUTE_STANDARD_NAME ","
 
807
      G_FILE_ATTRIBUTE_STANDARD_TYPE,
 
808
      G_FILE_QUERY_INFO_NONE, NULL, &error);
 
809
  if (error) {
 
810
    libgnac_warning("%s", error->message);
 
811
    g_clear_error(&error);
 
812
    return NULL;
 
813
  }
 
814
 
 
815
  gchar *name = NULL;
 
816
 
 
817
  switch (target_type)
 
818
  {
 
819
    case DND_TARGET_URI:
 
820
      if (g_file_info_get_file_type(file_info) == G_FILE_TYPE_REGULAR) {
 
821
        name = g_strdup(g_file_info_get_name(file_info));
 
822
      }
 
823
      break;
 
824
 
 
825
    case DND_TARGET_PLAIN:
 
826
      if (g_file_info_get_file_type(file_info) == G_FILE_TYPE_UNKNOWN) {
 
827
        name = g_file_get_basename(file);
 
828
      }
 
829
      break;
 
830
  }
 
831
 
 
832
  g_object_unref(file_info);
 
833
 
 
834
  return name;
 
835
}
 
836
 
 
837
 
 
838
static void
 
839
gnac_profiles_mgr_display_import_status(guint profiles_ok,
 
840
                                        guint profiles_error)
 
841
{
 
842
  gchar *oks = NULL;
 
843
  gchar *errors = NULL;
 
844
 
 
845
  if (profiles_error > 0) {
 
846
    errors = g_strdup_printf(ngettext("%d file failed to be imported",
 
847
        "%d files failed to be imported", profiles_error), profiles_error);
 
848
  }
 
849
 
 
850
  if (profiles_ok > 0) {
 
851
    oks = g_strdup_printf(ngettext("%d file successfully imported",
 
852
        "%d files successfully imported", profiles_ok), profiles_ok);
 
853
  }
 
854
 
 
855
  gdk_threads_enter();
 
856
    gnac_profiles_mgr_set_window_sensitive(TRUE);
 
857
    gnac_profiles_mgr_show_import_progressbar(FALSE);
 
858
    gnac_profiles_mgr_display_status_message(oks, errors);
 
859
  gdk_threads_leave();
 
860
 
 
861
  g_free(oks);
 
862
  g_free(errors);
 
863
}
 
864
 
 
865
 
 
866
static gboolean
 
867
gnac_profiles_mgr_copy_file(GFile       *src_file,
 
868
                            const gchar *dest_name)
 
869
{
 
870
  GError *error = NULL;
 
871
  gchar *path = g_build_filename(g_get_tmp_dir(), dest_name, NULL);
 
872
  GFile *dest_file = g_file_new_for_path(path);
 
873
  CopyData copy_data = { path, dest_name, &error, FALSE };
 
874
 
 
875
  gdk_threads_enter();
 
876
    gnac_profiles_mgr_set_progress_bar_fraction(0.0);
 
877
  gdk_threads_leave();
 
878
 
 
879
  gboolean success = g_file_copy(src_file, dest_file, G_FILE_COPY_NONE, NULL,
 
880
      (GFileProgressCallback) gnac_profiles_mgr_on_drag_profile_copied,
 
881
      &copy_data, &error);
 
882
  if (error) {
 
883
    libgnac_warning("%s", error->message);
 
884
    g_clear_error(&error);
 
885
  }
 
886
 
 
887
  g_free(path);
 
888
  g_object_unref(dest_file);
 
889
 
 
890
  return success;
 
891
}
 
892
 
 
893
 
 
894
static void
 
895
gnac_profiles_mgr_copy_and_load_files(gpointer data)
 
896
{
 
897
  ThreadCopyData *tcopy_data = (ThreadCopyData *) data;
 
898
  gchar **uris = tcopy_data->uris;
 
899
 
 
900
  gint index = 0;
 
901
  gchar *uri = uris[index];
 
902
 
 
903
  gdk_threads_enter();
 
904
    gnac_profiles_mgr_set_window_sensitive(FALSE);
 
905
    gnac_profiles_mgr_show_import_progressbar(TRUE);
 
906
  gdk_threads_leave();
 
907
  
 
908
  guint profiles_ok = 0;
 
909
  guint profiles_error = 0;
 
910
 
 
911
  while (uri) {
 
912
    GFile *file = g_file_new_for_uri(uri);
 
913
    gchar *name = gnac_profiles_mgr_get_file_name(file, tcopy_data->info);
 
914
 
 
915
    if (!name) {
 
916
      libgnac_warning(
 
917
          _("Impossible to import file \"%s\". File type not supported."),
 
918
          uri);
 
919
      profiles_error++;
 
920
    } else if (gnac_profiles_properties_saved_profiles_contain_name(name)) {
 
921
      libgnac_warning(
 
922
          _("Impossible to load file \"%s\": "
 
923
            "a profile with the same name already exists."), uri);
 
924
      profiles_error++;
 
925
    } else if (gnac_profiles_mgr_copy_file(file, name)) {
 
926
      profiles_ok++;
 
927
    } else {
 
928
      profiles_error++;
 
929
    }
 
930
 
 
931
    g_free(name);
 
932
 
 
933
    index++;
 
934
    uri = uris[index];
 
935
 
 
936
    g_object_unref(file);
 
937
  }
 
938
 
 
939
  gnac_profiles_mgr_display_import_status(profiles_ok, profiles_error);
 
940
 
 
941
  g_strfreev(tcopy_data->uris);
 
942
  g_free(tcopy_data);
 
943
}
 
944
 
 
945
 
 
946
void
 
947
gnac_profiles_mgr_on_drag_profile_copied(goffset  current_num_bytes,
 
948
                                         goffset  total_num_bytes,
 
949
                                         gpointer user_data)
 
950
{
 
951
  /* CLAMP ensures that frac is between 0.0 and 1.0 */
 
952
  gdouble frac = CLAMP(
 
953
      ((gdouble) current_num_bytes) / ((gdouble) total_num_bytes),
 
954
      0.0, 1.0);
 
955
  
 
956
  gdk_threads_enter();
 
957
    gnac_profiles_mgr_set_progress_bar_fraction(frac);
 
958
  gdk_threads_leave();
 
959
 
 
960
  if (current_num_bytes >= total_num_bytes) {
 
961
    CopyData *copy_data = (CopyData *) user_data;
 
962
    if (!copy_data->toggle) {
 
963
      copy_data->toggle = TRUE;
 
964
    } else {
 
965
      AudioProfileGeneric *profile =
 
966
          gnac_profiles_properties_load_profile_from_file(
 
967
              copy_data->path, copy_data->name, copy_data->error);
 
968
 
 
969
      if (profile && !(*(copy_data->error))) {
 
970
        gdk_threads_enter();
 
971
          gnac_profiles_properties_save_profile(profile);
 
972
          gnac_profiles_mgr_insert(profile);
 
973
        gdk_threads_leave();
 
974
      }
 
975
 
 
976
      GFile *file = g_file_new_for_path(copy_data->path);
 
977
 
 
978
      g_file_delete(file, NULL, NULL);
 
979
      g_object_unref(file);
 
980
    } 
 
981
  }
 
982
}
 
983
 
 
984
 
 
985
void
 
986
gnac_profiles_mgr_on_drag_data_get(GtkWidget        *widget,
 
987
                                   GdkDragContext   *drag_context,
 
988
                                   GtkSelectionData *data,
 
989
                                   guint             info,
 
990
                                   guint             time,
 
991
                                   gpointer          user_data)
 
992
{
 
993
  gchar **uris = gnac_profiles_mgr_get_selected_uris();
 
994
  gtk_selection_data_set_uris(data, uris);
 
995
}
 
996
 
 
997
 
 
998
static gchar **
 
999
gnac_profiles_mgr_get_selected_uris(void)
 
1000
{
 
1001
  GList *selected = gnac_profiles_mgr_get_selected_rows();
 
1002
  if (!selected) return NULL;
 
1003
 
 
1004
  GtkTreeRowReference *reference = (GtkTreeRowReference *) selected->data;
 
1005
  gchar **uris = g_malloc((g_list_length(selected)+1) * sizeof(gchar *));
 
1006
 
 
1007
  guint  i;
 
1008
  GList *temp;
 
1009
 
 
1010
  for (i = 0, temp = selected; temp; temp = g_list_next(temp), i++) {
 
1011
    AudioProfileGeneric *profile;
 
1012
    gnac_profiles_mgr_get(reference, COL_PROFILE, &profile);
 
1013
    profile = profile->generic;
 
1014
    gchar *path = g_strconcat(GNAC_SAVED_PROFILES_URL_WITH_EXT(profile->name),
 
1015
        NULL);
 
1016
    GFile *file = g_file_new_for_path(path);
 
1017
    uris[i] = g_file_get_uri(file); 
 
1018
 
 
1019
    g_free(path);
 
1020
  }
 
1021
 
 
1022
  uris[i] = NULL;
 
1023
 
 
1024
  g_list_free_full(selected, (GDestroyNotify) gtk_tree_row_reference_free);
 
1025
 
 
1026
  return uris;
 
1027
}
 
1028
 
 
1029
 
 
1030
void
 
1031
gnac_profiles_mgr_on_add(GtkWidget *widget,
 
1032
                         gpointer   data)
 
1033
{
 
1034
  gnac_profiles_properties_show(NULL, _("New Profile"),
 
1035
      G_CALLBACK(gnac_profiles_mgr_on_new_profile));
 
1036
}
 
1037
 
 
1038
 
 
1039
static void
 
1040
gnac_profiles_mgr_on_new_profile(GtkWidget *widget,
 
1041
                                 gpointer   data)
 
1042
{
 
1043
  gnac_profiles_mgr_insert(data); 
 
1044
  gnac_profiles_mgr_display_status_message(NULL, NULL);
 
1045
}
 
1046
 
 
1047
 
 
1048
void
 
1049
gnac_profiles_mgr_on_copy(GtkWidget *widget,
 
1050
                          gpointer   data)
 
1051
{
 
1052
  GList *selected = gnac_profiles_mgr_get_selected_rows();
 
1053
  if (!selected) return;
 
1054
 
 
1055
  GtkTreeRowReference *reference = (GtkTreeRowReference *) selected->data;
 
1056
 
 
1057
  AudioProfileGeneric *profile;
 
1058
  gnac_profiles_mgr_get(reference, COL_PROFILE, &profile);
 
1059
  AudioProfileGeneric *generic = profile->generic;
 
1060
 
 
1061
  gchar *full_path = g_strconcat(GNAC_SAVED_PROFILES_URL_WITH_EXT(generic->name),
 
1062
      NULL);
 
1063
  gchar *new_name = g_strconcat(GNAC_SAVED_PROFILES_URL(generic->name),
 
1064
      gettext(GNAC_COPY_SUFFIX), NULL);
 
1065
  gchar *new_path = g_strconcat(new_name, ".xml", NULL);
 
1066
 
 
1067
  GFile *src = g_file_new_for_path(full_path);
 
1068
  GFile *dst = g_file_new_for_path(new_path);
 
1069
 
 
1070
  g_free(full_path);
 
1071
  
 
1072
  GError *error = NULL;
 
1073
  g_file_copy(src, dst, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
 
1074
  if (error)
 
1075
  {
 
1076
    libgnac_debug("Failed to copy the profile %s: %s",
 
1077
        generic->name, error->message);
 
1078
    gnac_profiles_mgr_display_status_message(
 
1079
        NULL, _("Failed to copy the profile"));
 
1080
 
 
1081
    g_clear_error(&error);
 
1082
    g_object_unref(src);
 
1083
    g_object_unref(dst);
 
1084
    g_free(new_path);
 
1085
    g_free(new_name);
 
1086
    g_list_free_full(selected, (GDestroyNotify) gtk_tree_row_reference_free);
 
1087
 
 
1088
    return;
 
1089
  }
 
1090
 
 
1091
  // FIXME seems we have a leak here
 
1092
  AudioProfileGeneric *copy_profile =
 
1093
      gnac_profiles_properties_load_profile_from_file(
 
1094
          new_path, new_name, &error);
 
1095
  if (copy_profile)
 
1096
  {
 
1097
    (copy_profile->generic)->name = g_strconcat(generic->name, 
 
1098
        gettext(GNAC_COPY_SUFFIX), NULL);
 
1099
    gnac_profiles_properties_save_profile(copy_profile);
 
1100
    gnac_profiles_mgr_insert(copy_profile); 
 
1101
  } 
 
1102
  else if (error) 
 
1103
  {
 
1104
    libgnac_debug("Failed to copy the profile %s: %s",
 
1105
        (copy_profile->generic)->name, error->message);
 
1106
    gnac_profiles_mgr_display_status_message(
 
1107
        NULL, _("Failed to copy the profile"));
 
1108
    g_clear_error(&error);
 
1109
  }
 
1110
 
 
1111
  g_object_unref(src);
 
1112
  g_object_unref(dst);
 
1113
  g_free(new_path);
 
1114
  g_free(new_name);
 
1115
  g_list_free_full(selected, (GDestroyNotify) gtk_tree_row_reference_free);
 
1116
}
 
1117
 
 
1118
 
 
1119
void
 
1120
gnac_profiles_mgr_on_edit(GtkWidget *widget,
 
1121
                          gpointer   data)
 
1122
{
 
1123
  GList *selected = gnac_profiles_mgr_get_selected_rows();
 
1124
  if (!selected) return;
 
1125
 
 
1126
  GtkTreeRowReference *reference = (GtkTreeRowReference *) selected->data;
 
1127
  AudioProfileGeneric *profile;
 
1128
  gnac_profiles_mgr_get(reference, COL_PROFILE, &profile);
 
1129
  profile->generic->user_data = selected->data;
 
1130
 
 
1131
  g_list_free_full(selected, (GDestroyNotify) gtk_tree_row_reference_free);
 
1132
  
 
1133
  gnac_profiles_properties_show(profile, _("Edit Profile"),
 
1134
      G_CALLBACK(gnac_profiles_mgr_on_edit_profile));
 
1135
}
 
1136
 
 
1137
 
 
1138
static void
 
1139
gnac_profiles_mgr_on_edit_profile(GtkWidget *widget,
 
1140
                                  gpointer   data)
 
1141
{
 
1142
  GList *selected = gnac_profiles_mgr_get_selected_rows();
 
1143
  if (!selected) return;
 
1144
 
 
1145
  GtkTreeRowReference *reference = (GtkTreeRowReference *) selected->data;
 
1146
 
 
1147
  AudioProfileGeneric *old_profile;
 
1148
  gnac_profiles_mgr_get(reference, COL_PROFILE, &old_profile);
 
1149
  AudioProfileGeneric *old_generic = old_profile->generic;
 
1150
 
 
1151
  AudioProfileGeneric *profile = (AudioProfileGeneric *) data;
 
1152
  AudioProfileGeneric *generic = profile->generic;
 
1153
 
 
1154
  if (!gnac_utils_str_equal(generic->name, old_generic->name)) {
 
1155
    gnac_profiles_mgr_delete_profile_file(old_generic->name);
 
1156
  }
 
1157
 
 
1158
  gchar *formatted_name = gnac_profiles_properties_filter_text_for_display(
 
1159
      generic->name, MAX_NAME_DISPLAY_SIZE);
 
1160
  gnac_profiles_mgr_set(reference,
 
1161
      COL_NAME, formatted_name,
 
1162
      COL_FORMAT, generic->format_name,
 
1163
      COL_EXTENSION, generic->extension,
 
1164
      COL_PROFILE, profile,
 
1165
      -1);
 
1166
  
 
1167
  g_free(formatted_name);
 
1168
  gnac_profiles_properties_free_audio_profile(old_profile);
 
1169
  g_list_free_full(selected, (GDestroyNotify) gtk_tree_row_reference_free);
 
1170
 
 
1171
  gnac_profiles_mgr_display_status_message(NULL, NULL);
 
1172
 
 
1173
  gnac_profiles_mgr_on_treeselection_changed();
 
1174
}
 
1175
 
 
1176
 
 
1177
void
 
1178
gnac_profiles_mgr_on_remove(GtkWidget *widget,
 
1179
                            gpointer   data)
 
1180
{
 
1181
  GList *selected = gnac_profiles_mgr_get_selected_rows();
 
1182
  if (!selected) return;
 
1183
 
 
1184
  GtkTreeRowReference *reference = (GtkTreeRowReference *) selected->data;
 
1185
 
 
1186
  AudioProfileGeneric *profile;
 
1187
  gnac_profiles_mgr_get(reference, COL_PROFILE, &profile);
 
1188
  gnac_profiles_mgr_delete_profile_file(profile->generic->name);
 
1189
 
 
1190
  gnac_profiles_mgr_display_status_message(NULL, NULL);
 
1191
 
 
1192
  GtkTreeModel *model = gtk_tree_row_reference_get_model(reference);
 
1193
  GtkTreePath *path = gtk_tree_row_reference_get_path(reference);
 
1194
 
 
1195
  GtkTreeIter iter;
 
1196
  gboolean new_selected = FALSE;
 
1197
  if (gtk_tree_model_get_iter(model, &iter, path)) {
 
1198
    if (gtk_tree_model_iter_next(model, &iter)
 
1199
        || (gtk_tree_path_prev(path) &&
 
1200
            gtk_tree_model_get_iter(model, &iter, path)))
 
1201
    {
 
1202
      GtkTreeView *view = GTK_TREE_VIEW(
 
1203
          gnac_profiles_mgr_get_widget("profile_treeview"));
 
1204
      GtkTreeSelection *selection = gtk_tree_view_get_selection(view);
 
1205
      gtk_tree_selection_select_iter(selection, &iter);
 
1206
      new_selected = TRUE;
 
1207
    }
 
1208
  }
 
1209
 
 
1210
  gnac_profiles_mgr_remove(reference);
 
1211
 
 
1212
  gnac_profiles_mgr_activate_buttons(new_selected);
 
1213
  gnac_profiles_mgr_show_description_frame(new_selected);
 
1214
 
 
1215
  gtk_tree_path_free(path);
 
1216
  g_list_free_full(selected, (GDestroyNotify) gtk_tree_row_reference_free);
 
1217
}
 
1218
 
 
1219
 
 
1220
static void
 
1221
gnac_profiles_mgr_delete_profile_file(const gchar *name)
 
1222
{
 
1223
  gchar *full_path = g_strconcat(GNAC_SAVED_PROFILES_URL_WITH_EXT(name), NULL);
 
1224
  GFile *file = g_file_new_for_path(full_path);
 
1225
 
 
1226
  g_file_delete(file, NULL, NULL);
 
1227
 
 
1228
  g_object_unref(file);
 
1229
  g_free(full_path);
 
1230
}
 
1231
 
 
1232
 
 
1233
static void
 
1234
gnac_profiles_mgr_on_treeselection_changed(void)
 
1235
{
 
1236
  GList *selected = gnac_profiles_mgr_get_selected_rows();
 
1237
  if (!selected) return;
 
1238
 
 
1239
  GtkTreeRowReference *reference = (GtkTreeRowReference *) selected->data;
 
1240
 
 
1241
  gnac_profiles_mgr_activate_buttons(TRUE);
 
1242
  gnac_profiles_mgr_display_status_message(NULL, NULL);
 
1243
 
 
1244
  AudioProfileGeneric *profile;
 
1245
  gnac_profiles_mgr_get(reference, COL_PROFILE, &profile);
 
1246
  GtkWidget *widget = gnac_profiles_mgr_get_widget("label-description-content");
 
1247
  gchar *descr = gnac_profiles_properties_filter_text_for_display(
 
1248
      profile->generic->description, MAX_DESCR_DISPLAY_SIZE);
 
1249
  gtk_label_set_text(GTK_LABEL(widget), descr);
 
1250
 
 
1251
  g_free(descr);
 
1252
 
 
1253
  gnac_profiles_mgr_show_description_frame(TRUE);
 
1254
 
 
1255
  g_list_free_full(selected, (GDestroyNotify) gtk_tree_row_reference_free);
 
1256
}
 
1257
 
 
1258
 
 
1259
void
 
1260
gnac_profiles_mgr_on_close(GtkWidget *widget,
 
1261
                           gpointer   data)
 
1262
{
 
1263
  GtkWidget *window = gnac_profiles_mgr_get_widget("profile_manager_window");
 
1264
  gtk_widget_hide(window);
 
1265
  gnac_profiles_populate_combo();
 
1266
}
 
1267
 
 
1268
 
 
1269
gboolean
 
1270
gnac_profiles_mgr_on_delete_event(GtkWidget *widget,
 
1271
                                  GdkEvent  *event,
 
1272
                                  gpointer   user_data)
 
1273
{
 
1274
  gnac_profiles_mgr_on_close(NULL, NULL);
 
1275
  return TRUE;
 
1276
}
 
1277
 
 
1278
 
 
1279
gboolean
 
1280
gnac_profiles_mgr_list_on_key_pressed(GtkWidget   *widget,
 
1281
                                      GdkEventKey *event,
 
1282
                                      gpointer     data)
 
1283
{
 
1284
  if (gnac_ui_utils_event_is_delete_key(event)) {
 
1285
    gnac_profiles_mgr_on_remove(NULL, NULL);
 
1286
    return TRUE;
 
1287
  }
 
1288
 
 
1289
  return FALSE;
 
1290
}
 
1291
 
 
1292
 
 
1293
gboolean
 
1294
gnac_profiles_mgr_on_key_pressed(GtkWidget   *widget,
 
1295
                                 GdkEventKey *event,
 
1296
                                 gpointer     data)
 
1297
{
 
1298
  if (gnac_ui_utils_event_is_escape_key(event)) {
 
1299
    gnac_profiles_mgr_on_close(NULL, NULL);
 
1300
    return TRUE;
 
1301
  }
 
1302
 
 
1303
  return FALSE;
 
1304
}