gtkappchooserbutton.c 24.8 KB
Newer Older
1
/*
2
 * gtkappchooserbutton.h: an app-chooser combobox
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Copyright (C) 2010 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with the Gnome Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors: Cosimo Cecchi <ccecchi@redhat.com>
 */

Matthias Clasen's avatar
Matthias Clasen committed
24 25 26 27 28 29 30 31
/**
 * SECTION:gtkappchooserbutton
 * @Title: GtkAppChooserButton
 * @Short_description: A button to launch an application chooser dialog
 *
 * The #GtkAppChooserButton is a widget that lets the user select
 * an application. It implements the #GtkAppChooser interface.
 */
Matthias Clasen's avatar
Matthias Clasen committed
32
#include "config.h"
33

34
#include "gtkappchooserbutton.h"
35 36

#include "gtkappchooser.h"
37
#include "gtkappchooserdialog.h"
38 39 40 41 42
#include "gtkappchooserprivate.h"
#include "gtkcelllayout.h"
#include "gtkcellrendererpixbuf.h"
#include "gtkcellrenderertext.h"
#include "gtkcombobox.h"
43 44
#include "gtkdialog.h"
#include "gtkintl.h"
45
#include "gtkmarshalers.h"
46 47 48

enum {
  PROP_CONTENT_TYPE = 1,
49
  PROP_SHOW_DIALOG_ITEM,
50
  PROP_HEADING
51 52
};

53 54 55 56 57
enum {
  SIGNAL_CUSTOM_ITEM_ACTIVATED,
  NUM_SIGNALS
};

58 59 60
enum {
  COLUMN_APP_INFO,
  COLUMN_NAME,
61
  COLUMN_LABEL,
62 63 64 65 66 67
  COLUMN_ICON,
  COLUMN_CUSTOM,
  COLUMN_SEPARATOR,
  NUM_COLUMNS,
};

68
#define CUSTOM_ITEM_OTHER_APP "gtk-internal-item-other-app"
69 70 71

static void app_chooser_iface_init (GtkAppChooserIface *iface);

72
static void real_insert_custom_item (GtkAppChooserButton *self,
73
                                     const gchar *name,
74 75 76 77
                                     const gchar *label,
                                     GIcon *icon,
                                     gboolean custom,
                                     GtkTreeIter *iter);
78

79
static void real_insert_separator (GtkAppChooserButton *self,
80 81
                                   gboolean custom,
                                   GtkTreeIter *iter);
82

83 84
static guint signals[NUM_SIGNALS] = { 0, };

85
G_DEFINE_TYPE_WITH_CODE (GtkAppChooserButton, gtk_app_chooser_button, GTK_TYPE_COMBO_BOX,
Matthias Clasen's avatar
Matthias Clasen committed
86 87
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_APP_CHOOSER,
                                                app_chooser_iface_init));
88

89
struct _GtkAppChooserButtonPrivate {
90
  GtkListStore *store;
91 92

  gchar *content_type;
93
  gchar *heading;
94 95
  gint last_active;
  gboolean show_dialog_item;
96 97

  GHashTable *custom_item_names;
98 99 100 101
};

static gboolean
row_separator_func (GtkTreeModel *model,
Matthias Clasen's avatar
Matthias Clasen committed
102 103
                    GtkTreeIter  *iter,
                    gpointer      user_data)
104 105 106 107
{
  gboolean separator;

  gtk_tree_model_get (model, iter,
Matthias Clasen's avatar
Matthias Clasen committed
108 109
                      COLUMN_SEPARATOR, &separator,
                      -1);
110 111 112 113

  return separator;
}

114 115
static void
get_first_iter (GtkListStore *store,
Matthias Clasen's avatar
Matthias Clasen committed
116
                GtkTreeIter  *iter)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
{
  GtkTreeIter iter2;

  if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), iter))
    {
      /* the model is empty, append */
      gtk_list_store_append (store, iter);
    }
  else
    {
      gtk_list_store_insert_before (store, &iter2, iter);
      *iter = iter2;
    }
}

132
typedef struct {
133
  GtkAppChooserButton *self;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  GAppInfo *info;
  gint active_index;
} SelectAppData;

static void
select_app_data_free (SelectAppData *data)
{
  g_clear_object (&data->self);
  g_clear_object (&data->info);

  g_slice_free (SelectAppData, data);
}

static gboolean
select_application_func_cb (GtkTreeModel *model,
149 150 151
                            GtkTreePath *path,
                            GtkTreeIter *iter,
                            gpointer user_data)
152 153 154 155
{
  SelectAppData *data = user_data;
  GAppInfo *app_to_match = data->info, *app = NULL;
  gboolean custom;
156
  gboolean result;
157 158

  gtk_tree_model_get (model, iter,
159 160 161
                      COLUMN_APP_INFO, &app,
                      COLUMN_CUSTOM, &custom,
                      -1);
162

163
  /* custom items are always after GAppInfos, so iterating further here
164 165 166
   * is just useless.
   */
  if (custom)
167 168
    result = TRUE;
  else if (g_app_info_equal (app, app_to_match))
169 170
    {
      gtk_combo_box_set_active_iter (GTK_COMBO_BOX (data->self), iter);
171
      result = TRUE;
172
    }
173 174 175 176
  else
    result = FALSE;

  g_object_unref (app);
177

178
  return result;
179 180 181
}

static void
182 183
gtk_app_chooser_button_select_application (GtkAppChooserButton *self,
                                           GAppInfo *info)
184 185 186 187 188 189 190 191
{
  SelectAppData *data;

  data = g_slice_new0 (SelectAppData);
  data->self = g_object_ref (self);
  data->info = g_object_ref (info);

  gtk_tree_model_foreach (GTK_TREE_MODEL (self->priv->store),
192
                          select_application_func_cb, data);
193 194 195 196 197 198

  select_app_data_free (data);
}

static void
other_application_dialog_response_cb (GtkDialog *dialog,
199 200
                                      gint response_id,
                                      gpointer user_data)
201
{
202
  GtkAppChooserButton *self = user_data;
203 204 205 206 207 208 209
  GAppInfo *info;

  if (response_id != GTK_RESPONSE_OK)
    {
      /* reset the active item, otherwise we are stuck on
       * 'Other application...'
       */
210
      gtk_combo_box_set_active (GTK_COMBO_BOX (self), self->priv->last_active);
211 212 213 214 215 216 217 218
      gtk_widget_destroy (GTK_WIDGET (dialog));
      return;
    }

  info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (dialog));

  /* refresh the combobox to get the new application */
  gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));
219
  gtk_app_chooser_button_select_application (self, info);
220 221 222 223 224

  g_object_unref (info);
}

static void
225
other_application_item_activated_cb (GtkAppChooserButton *self)
226 227 228 229 230 231
{
  GtkWidget *dialog, *widget;
  GtkWindow *toplevel;

  toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (self)));
  dialog = gtk_app_chooser_dialog_new_for_content_type (toplevel, GTK_DIALOG_DESTROY_WITH_PARENT,
232
                                                        self->priv->content_type);
233 234

  gtk_window_set_modal (GTK_WINDOW (dialog), gtk_window_get_modal (toplevel));
235 236
  gtk_app_chooser_dialog_set_heading (GTK_APP_CHOOSER_DIALOG (dialog),
                                      self->priv->heading);
237

238 239
  widget = gtk_app_chooser_dialog_get_widget (GTK_APP_CHOOSER_DIALOG (dialog));
  g_object_set (widget,
240 241 242
                "show-fallback", TRUE,
                "show-other", TRUE,
                NULL);
243 244 245
  gtk_widget_show (dialog);

  g_signal_connect (dialog, "response",
246
                    G_CALLBACK (other_application_dialog_response_cb), self);
247 248 249
}

static void
250 251
gtk_app_chooser_button_ensure_dialog_item (GtkAppChooserButton *self,
                                           GtkTreeIter *prev_iter)
252
{
253
  GtkTreeIter iter, iter2;
254

255
  if (!self->priv->show_dialog_item || !self->priv->content_type)
256 257
    return;

258 259 260 261 262
  if (prev_iter == NULL)
    gtk_list_store_append (self->priv->store, &iter);
  else
    gtk_list_store_insert_after (self->priv->store, &iter, prev_iter);

263
  real_insert_separator (self, FALSE, &iter);
264
  iter2 = iter;
265

266
  gtk_list_store_insert_after (self->priv->store, &iter, &iter2);
267
  real_insert_custom_item (self, CUSTOM_ITEM_OTHER_APP,
268
                           _("Other application..."), NULL,
269
                           FALSE, &iter);
270 271
}

272
static void
273
gtk_app_chooser_button_populate (GtkAppChooserButton *self)
274 275 276
{
  GList *recommended_apps = NULL, *l;
  GAppInfo *app;
277
  GtkTreeIter iter, iter2;
278
  GIcon *icon;
279
  gboolean cycled_recommended;
280

Fridrich Strba's avatar
Fridrich Strba committed
281
#ifndef G_OS_WIN32
282 283
  if (self->priv->content_type)
    recommended_apps = g_app_info_get_recommended_for_type (self->priv->content_type);
Fridrich Strba's avatar
Fridrich Strba committed
284
#endif
285
  cycled_recommended = FALSE;
286

287 288 289 290 291 292 293
  for (l = recommended_apps; l != NULL; l = l->next)
    {
      app = l->data;

      icon = g_app_info_get_icon (app);

      if (icon == NULL)
Matthias Clasen's avatar
Matthias Clasen committed
294
        icon = g_themed_icon_new ("application-x-executable");
295
      else
Matthias Clasen's avatar
Matthias Clasen committed
296
        g_object_ref (icon);
297

298
      if (cycled_recommended)
Matthias Clasen's avatar
Matthias Clasen committed
299
        {
300 301
          gtk_list_store_insert_after (self->priv->store, &iter2, &iter);
          iter = iter2;
Matthias Clasen's avatar
Matthias Clasen committed
302
        }
303
      else
Matthias Clasen's avatar
Matthias Clasen committed
304
        {
305 306
          get_first_iter (self->priv->store, &iter);
          cycled_recommended = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
307
        }
308

309
      gtk_list_store_set (self->priv->store, &iter,
Matthias Clasen's avatar
Matthias Clasen committed
310
                          COLUMN_APP_INFO, app,
311
                          COLUMN_LABEL, g_app_info_get_name (app),
Matthias Clasen's avatar
Matthias Clasen committed
312 313 314
                          COLUMN_ICON, icon,
                          COLUMN_CUSTOM, FALSE,
                          -1);
315 316 317 318

      g_object_unref (icon);
    }

319 320 321 322 323
  if (!cycled_recommended)
    gtk_app_chooser_button_ensure_dialog_item (self, NULL);
  else
    gtk_app_chooser_button_ensure_dialog_item (self, &iter);

324 325 326 327
  gtk_combo_box_set_active (GTK_COMBO_BOX (self), 0);
}

static void
328
gtk_app_chooser_button_build_ui (GtkAppChooserButton *self)
329 330
{
  GtkCellRenderer *cell;
331
  GtkCellArea *area;
332 333

  gtk_combo_box_set_model (GTK_COMBO_BOX (self),
Matthias Clasen's avatar
Matthias Clasen committed
334
                           GTK_TREE_MODEL (self->priv->store));
335

336 337
  area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (self));

338
  gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
Matthias Clasen's avatar
Matthias Clasen committed
339
                                        row_separator_func, NULL, NULL);
340 341

  cell = gtk_cell_renderer_pixbuf_new ();
342 343 344 345 346
  gtk_cell_area_add_with_properties (area, cell,
                                     "align", FALSE,
                                     "expand", FALSE,
                                     "fixed-size", FALSE,
                                     NULL);
347
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), cell,
Matthias Clasen's avatar
Matthias Clasen committed
348 349
                                  "gicon", COLUMN_ICON,
                                  NULL);
350 351

  cell = gtk_cell_renderer_text_new ();
352 353 354 355
  gtk_cell_area_add_with_properties (area, cell,
                                     "align", FALSE,
                                     "expand", TRUE,
                                     NULL);
356
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), cell,
357
                                  "text", COLUMN_LABEL,
Matthias Clasen's avatar
Matthias Clasen committed
358
                                  NULL);
359

360
  gtk_app_chooser_button_populate (self);
361 362 363
}

static void
364
gtk_app_chooser_button_remove_non_custom (GtkAppChooserButton *self)
365 366 367 368 369
{
  GtkTreeModel *model;
  GtkTreeIter iter;
  gboolean custom, res;

Matthias Clasen's avatar
Matthias Clasen committed
370
  model = GTK_TREE_MODEL (self->priv->store);
371 372 373 374 375 376

  if (!gtk_tree_model_get_iter_first (model, &iter))
    return;

  do {
    gtk_tree_model_get (model, &iter,
Matthias Clasen's avatar
Matthias Clasen committed
377 378
                        COLUMN_CUSTOM, &custom,
                        -1);
379 380
    if (custom)
      res = gtk_tree_model_iter_next (model, &iter);
381 382
    else
      res = gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
383 384 385 386
  } while (res);
}

static void
387
gtk_app_chooser_button_changed (GtkComboBox *object)
388
{
389
  GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
390
  GtkTreeIter iter;
391 392 393
  gchar *name = NULL;
  gboolean custom;
  GQuark name_quark;
394

395 396
  if (!gtk_combo_box_get_active_iter (object, &iter))
    return;
397 398

  gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
399 400
                      COLUMN_NAME, &name,
                      COLUMN_CUSTOM, &custom,
Matthias Clasen's avatar
Matthias Clasen committed
401
                      -1);
402

403 404 405 406 407 408
  if (name != NULL)
    {
      if (custom)
        {
          name_quark = g_quark_from_string (name);
          g_signal_emit (self, signals[SIGNAL_CUSTOM_ITEM_ACTIVATED], name_quark, name);
409
          self->priv->last_active = gtk_combo_box_get_active (object);
410 411 412 413 414 415 416 417 418
        }
      else
        {
          /* trigger the dialog internally */
          other_application_item_activated_cb (self);
        }

      g_free (name);
    }
419 420
  else
    self->priv->last_active = gtk_combo_box_get_active (object);
421 422 423
}

static void
424
gtk_app_chooser_button_refresh (GtkAppChooser *object)
425
{
426
  GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
427

428 429
  gtk_app_chooser_button_remove_non_custom (self);
  gtk_app_chooser_button_populate (self);
430 431 432
}

static GAppInfo *
433
gtk_app_chooser_button_get_app_info (GtkAppChooser *object)
434
{
435
  GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (object);
436 437 438 439 440 441 442
  GtkTreeIter iter;
  GAppInfo *info;

  if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
    return NULL;

  gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
Matthias Clasen's avatar
Matthias Clasen committed
443 444
                      COLUMN_APP_INFO, &info,
                      -1);
445 446 447 448 449

  return info;
}

static void
450
gtk_app_chooser_button_constructed (GObject *obj)
451
{
452
  GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
453

454 455
  if (G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->constructed != NULL)
    G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->constructed (obj);
456

457
  gtk_app_chooser_button_build_ui (self);
458 459 460
}

static void
461 462 463 464
gtk_app_chooser_button_set_property (GObject      *obj,
                                     guint         property_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
465
{
466
  GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
467 468 469 470 471 472

  switch (property_id)
    {
    case PROP_CONTENT_TYPE:
      self->priv->content_type = g_value_dup_string (value);
      break;
473
    case PROP_SHOW_DIALOG_ITEM:
474
      gtk_app_chooser_button_set_show_dialog_item (self, g_value_get_boolean (value));
475
      break;
476 477 478
    case PROP_HEADING:
      gtk_app_chooser_button_set_heading (self, g_value_get_string (value));
      break;
479 480 481 482 483 484 485
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
      break;
    }
}

static void
486 487 488 489
gtk_app_chooser_button_get_property (GObject    *obj,
                                     guint       property_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
490
{
491
  GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
492 493 494 495 496 497

  switch (property_id)
    {
    case PROP_CONTENT_TYPE:
      g_value_set_string (value, self->priv->content_type);
      break;
498 499 500
    case PROP_SHOW_DIALOG_ITEM:
      g_value_set_boolean (value, self->priv->show_dialog_item);
      break;
501 502 503
    case PROP_HEADING:
      g_value_set_string (value, self->priv->heading);
      break;
504 505 506 507 508 509 510
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
      break;
    }
}

static void
511
gtk_app_chooser_button_finalize (GObject *obj)
512
{
513
  GtkAppChooserButton *self = GTK_APP_CHOOSER_BUTTON (obj);
514

515
  g_hash_table_destroy (self->priv->custom_item_names);
516
  g_free (self->priv->content_type);
517
  g_free (self->priv->heading);
518

519 520
  g_object_unref (self->priv->store);

521
  G_OBJECT_CLASS (gtk_app_chooser_button_parent_class)->finalize (obj);
522 523 524 525 526
}

static void
app_chooser_iface_init (GtkAppChooserIface *iface)
{
527 528
  iface->get_app_info = gtk_app_chooser_button_get_app_info;
  iface->refresh = gtk_app_chooser_button_refresh;
529 530 531
}

static void
532
gtk_app_chooser_button_class_init (GtkAppChooserButtonClass *klass)
533 534 535
{
  GObjectClass *oclass = G_OBJECT_CLASS (klass);
  GtkComboBoxClass *combo_class = GTK_COMBO_BOX_CLASS (klass);
536
  GParamSpec *pspec;
537

538 539 540 541
  oclass->set_property = gtk_app_chooser_button_set_property;
  oclass->get_property = gtk_app_chooser_button_get_property;
  oclass->finalize = gtk_app_chooser_button_finalize;
  oclass->constructed = gtk_app_chooser_button_constructed;
542

543
  combo_class->changed = gtk_app_chooser_button_changed;
544 545 546

  g_object_class_override_property (oclass, PROP_CONTENT_TYPE, "content-type");

547 548 549
  /**
   * GtkAppChooserButton:show-dialog-item:
   *
550
   * The #GtkAppChooserButton:show-dialog-item property determines whether the dropdown menu
551 552
   * should show an item that triggers a #GtkAppChooserDialog when clicked.
   */
553 554 555 556 557 558
  pspec =
    g_param_spec_boolean ("show-dialog-item",
                          P_("Include an 'Other...' item"),
                          P_("Whether the combobox should include an item that triggers a GtkAppChooserDialog"),
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
559 560
  g_object_class_install_property (oclass, PROP_SHOW_DIALOG_ITEM, pspec);

561 562 563 564 565 566 567 568 569 570 571 572 573 574
  /**
   * GtkAppChooserButton:heading:
   *
   * The text to show at the top of the dialog that can be
   * opened from the button. The string may contain Pango markup.
   */
  pspec = g_param_spec_string ("heading",
                               P_("Heading"),
                               P_("The text to show at the top of the dialog"),
                               NULL,
                               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (oclass, PROP_HEADING, pspec);


Cosimo Cecchi's avatar
Cosimo Cecchi committed
575 576 577 578 579 580 581 582 583
  /**
   * GtkAppChooserButton::custom-item-activated:
   * @self: the object which received the signal
   * @item_name: the name of the activated item
   *
   * Emitted when a custom item, previously added with
   * gtk_app_chooser_button_append_custom_item(), is activated from the
   * dropdown menu.
   */
584 585 586 587 588 589 590 591 592 593
  signals[SIGNAL_CUSTOM_ITEM_ACTIVATED] =
    g_signal_new ("custom-item-activated",
                  GTK_TYPE_APP_CHOOSER_BUTTON,
                  G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
                  G_STRUCT_OFFSET (GtkAppChooserButtonClass, custom_item_activated),
                  NULL, NULL,
                  _gtk_marshal_VOID__STRING,
                  G_TYPE_NONE,
                  1, G_TYPE_STRING);

594
  g_type_class_add_private (klass, sizeof (GtkAppChooserButtonPrivate));
595 596 597
}

static void
598
gtk_app_chooser_button_init (GtkAppChooserButton *self)
599
{
600 601
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_BUTTON,
                                            GtkAppChooserButtonPrivate);
602 603 604
  self->priv->custom_item_names =
    g_hash_table_new_full (g_str_hash, g_str_equal,
                           g_free, NULL);
605 606 607 608 609 610 611 612

  self->priv->store = gtk_list_store_new (NUM_COLUMNS,
                                          G_TYPE_APP_INFO,
                                          G_TYPE_STRING, /* name */
                                          G_TYPE_STRING, /* label */
                                          G_TYPE_ICON,
                                          G_TYPE_BOOLEAN, /* separator */
                                          G_TYPE_BOOLEAN); /* custom */
613 614
}

615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
static gboolean
app_chooser_button_iter_from_custom_name (GtkAppChooserButton *self,
                                          const gchar *name,
                                          GtkTreeIter *set_me)
{
  GtkTreeIter iter;
  gchar *custom_name = NULL;

  if (!gtk_tree_model_get_iter_first
      (GTK_TREE_MODEL (self->priv->store), &iter))
    return FALSE;

  do {
    gtk_tree_model_get (GTK_TREE_MODEL (self->priv->store), &iter,
                        COLUMN_NAME, &custom_name,
                        -1);

    if (g_strcmp0 (custom_name, name) == 0)
      {
        g_free (custom_name);
        *set_me = iter;

        return TRUE;
      }

    g_free (custom_name);
  } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (self->priv->store), &iter));

  return FALSE;
}

646
static void
647
real_insert_custom_item (GtkAppChooserButton *self,
648
                         const gchar *name,
649 650 651 652 653
			 const gchar *label,
			 GIcon *icon,
			 gboolean custom,
			 GtkTreeIter *iter)
{
654 655 656 657 658 659 660 661 662
  if (custom)
    {
      if (g_hash_table_lookup (self->priv->custom_item_names,
                               name) != NULL)
        {
          g_warning ("Attempting to add custom item %s to GtkAppChooserButton, "
                     "when there's already an item with the same name", name);
          return;
        }
663

664 665 666
      g_hash_table_insert (self->priv->custom_item_names,
                           g_strdup (name), GINT_TO_POINTER (1));
    }
667 668

  gtk_list_store_set (self->priv->store, iter,
669
                      COLUMN_NAME, name,
670
                      COLUMN_LABEL, label,
671 672 673 674
                      COLUMN_ICON, icon,
                      COLUMN_CUSTOM, custom,
                      COLUMN_SEPARATOR, FALSE,
                      -1);
675 676 677
}

static void
678
real_insert_separator (GtkAppChooserButton *self,
679 680 681 682 683 684 685 686 687
		       gboolean custom,
		       GtkTreeIter *iter)
{
  gtk_list_store_set (self->priv->store, iter,
		      COLUMN_CUSTOM, custom,
		      COLUMN_SEPARATOR, TRUE,
		      -1);
}

Matthias Clasen's avatar
Matthias Clasen committed
688
/**
689
 * gtk_app_chooser_button_new:
Matthias Clasen's avatar
Matthias Clasen committed
690 691
 * @content_type: the content type to show applications for
 *
692
 * Creates a new #GtkAppChooserButton for applications
Matthias Clasen's avatar
Matthias Clasen committed
693 694
 * that can handle content of the given type.
 *
695
 * Returns: a newly created #GtkAppChooserButton
Matthias Clasen's avatar
Matthias Clasen committed
696 697 698
 *
 * Since: 3.0
 */
699
GtkWidget *
700
gtk_app_chooser_button_new (const gchar *content_type)
701 702 703
{
  g_return_val_if_fail (content_type != NULL, NULL);

704
  return g_object_new (GTK_TYPE_APP_CHOOSER_BUTTON,
Matthias Clasen's avatar
Matthias Clasen committed
705 706
                       "content-type", content_type,
                       NULL);
707 708
}

Matthias Clasen's avatar
Matthias Clasen committed
709
/**
710 711
 * gtk_app_chooser_button_append_separator:
 * @self: a #GtkAppChooserButton
Matthias Clasen's avatar
Matthias Clasen committed
712 713 714 715 716 717
 *
 * Appends a separator to the list of applications that is shown
 * in the popup.
 *
 * Since: 3.0
 */
718
void
719
gtk_app_chooser_button_append_separator (GtkAppChooserButton *self)
720 721 722
{
  GtkTreeIter iter;

723
  g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
724 725

  gtk_list_store_append (self->priv->store, &iter);
726
  real_insert_separator (self, TRUE, &iter);
727 728
}

Matthias Clasen's avatar
Matthias Clasen committed
729
/**
730 731
 * gtk_app_chooser_button_append_custom_item:
 * @self: a #GtkAppChooserButton
732
 * @name: the name of the custom item
Matthias Clasen's avatar
Matthias Clasen committed
733 734 735 736
 * @label: the label for the custom item
 * @icon: the icon for the custom item
 *
 * Appends a custom item to the list of applications that is shown
737 738 739 740 741
 * in the popup; the item name must be unique per-widget.
 * Clients can use the provided name as a detail for the ::custom-item-activated
 * signal, to add a callback for the activation of a particular
 * custom item in the list.
 * See also gtk_app_chooser_button_append_separator().
Matthias Clasen's avatar
Matthias Clasen committed
742 743 744
 *
 * Since: 3.0
 */
745
void
746 747 748 749
gtk_app_chooser_button_append_custom_item (GtkAppChooserButton *self,
                                           const gchar         *name,
                                           const gchar         *label,
                                           GIcon               *icon)
750 751 752
{
  GtkTreeIter iter;

753
  g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
754
  g_return_if_fail (name != NULL);
755 756

  gtk_list_store_append (self->priv->store, &iter);
757
  real_insert_custom_item (self, name, label, icon, TRUE, &iter);
758 759
}

760
/**
761
 * gtk_app_chooser_button_set_active_custom_item:
762 763 764 765 766
 * @self: a #GtkAppChooserButton
 * @name: the name of the custom item
 *
 * Selects a custom item previously added with
 * gtk_app_chooser_button_append_custom_item().
767 768 769
 *
 * Use gtk_app_chooser_refresh() to bring the selection
 * to its initial state.
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
 *
 * Since: 3.0
 */
void
gtk_app_chooser_button_set_active_custom_item (GtkAppChooserButton *self,
                                               const gchar         *name)
{
  GtkTreeIter iter;

  g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));
  g_return_if_fail (name != NULL);

  if (g_hash_table_lookup (self->priv->custom_item_names, name) == NULL ||
      !app_chooser_button_iter_from_custom_name (self, name, &iter))
    {
      g_warning ("Can't find the item named %s in the app chooser.",
                 name);
      return;
    }

  gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
}

793 794 795 796 797 798 799 800 801 802 803
/**
 * gtk_app_chooser_button_get_show_dialog_item:
 * @self: a #GtkAppChooserButton
 *
 * Returns the current value of the #GtkAppChooserButton:show-dialog-item
 * property.
 *
 * Returns: the value of #GtkAppChooserButton:show-dialog-item
 *
 * Since: 3.0
 */
804
gboolean
805
gtk_app_chooser_button_get_show_dialog_item (GtkAppChooserButton *self)
806
{
807
  g_return_val_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self), FALSE);
808 809 810 811

  return self->priv->show_dialog_item;
}

812
/**
813
 * gtk_app_chooser_button_set_show_dialog_item:
814 815 816 817 818 819 820 821
 * @self: a #GtkAppChooserButton
 * @setting: the new value for #GtkAppChooserButton:show-dialog-item
 *
 * Sets whether the dropdown menu of this button should show an
 * entry to trigger a #GtkAppChooserDialog.
 *
 * Since: 3.0
 */
822
void
823 824
gtk_app_chooser_button_set_show_dialog_item (GtkAppChooserButton *self,
                                             gboolean setting)
825 826 827 828 829 830 831 832 833
{
  if (self->priv->show_dialog_item != setting)
    {
      self->priv->show_dialog_item = setting;

      g_object_notify (G_OBJECT (self), "show-dialog-item");

      gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));
    }
834
}
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

/**
 * gtk_app_chooser_button_set_heading:
 * @self: a #GtkAppChooserButton
 * @heading: a string containing Pango markup
 *
 * Sets the text to display at the top of the dialog.
 * If the heading is not set, the dialog displays a default text.
 */
void
gtk_app_chooser_button_set_heading (GtkAppChooserButton *self,
                                    const gchar         *heading)
{
  g_return_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self));

  g_free (self->priv->heading);
  self->priv->heading = g_strdup (heading);

  g_object_notify (G_OBJECT (self), "heading");
}

/**
 * gtk_app_chooser_button_get_heading:
 * @self: a #GtkAppChooserButton
 *
 * Returns the text to display at the top of the dialog.
 *
 * Returns: the text to display at the top of the dialog, or %NULL, in which
 *     case a default text is displayed
 */
const gchar *
gtk_app_chooser_button_get_heading (GtkAppChooserButton *self)
{
  g_return_val_if_fail (GTK_IS_APP_CHOOSER_BUTTON (self), NULL);

  return self->priv->heading;
}