gtkappchooserdialog.c 26.1 KB
Newer Older
1
/*
2
 * gtkappchooserdialog.c: an app-chooser dialog
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * Copyright (C) 2004 Novell, Inc.
 * Copyright (C) 2007, 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
Javier Jardón's avatar
Javier Jardón committed
18
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 20 21 22 23 24
 *
 * Authors: Dave Camp <dave@novell.com>
 *          Alexander Larsson <alexl@redhat.com>
 *          Cosimo Cecchi <ccecchi@redhat.com>
 */

Matthias Clasen's avatar
Matthias Clasen committed
25 26 27 28 29 30 31 32 33
/**
 * SECTION:gtkappchooserdialog
 * @Title: GtkAppChooserDialog
 * @Short_description: An application chooser dialog
 *
 * #GtkAppChooserDialog shows a #GtkAppChooserWidget inside a #GtkDialog.
 *
 * Note that #GtkAppChooserDialog does not have any interesting methods
 * of its own. Instead, you should get the embedded #GtkAppChooserWidget
34 35
 * using gtk_app_chooser_dialog_get_widget() and call its methods if
 * the generic #GtkAppChooser interface is not sufficient for your needs.
36 37 38
 *
 * To set the heading that is shown above the #GtkAppChooserWidget,
 * use gtk_app_chooser_dialog_set_heading().
Matthias Clasen's avatar
Matthias Clasen committed
39
 */
Matthias Clasen's avatar
Matthias Clasen committed
40
#include "config.h"
41

42
#include "gtkappchooserdialog.h"
43 44

#include "gtkintl.h"
45
#include "gtkappchooser.h"
Matthias Clasen's avatar
Matthias Clasen committed
46 47 48
#include "gtkappchooserprivate.h"

#include "gtkmessagedialog.h"
49
#include "gtksettings.h"
Matthias Clasen's avatar
Matthias Clasen committed
50 51 52
#include "gtklabel.h"
#include "gtkbbox.h"
#include "gtkbutton.h"
53 54 55
#include "gtkentry.h"
#include "gtktogglebutton.h"
#include "gtkstylecontext.h"
Matthias Clasen's avatar
Matthias Clasen committed
56
#include "gtkmenuitem.h"
57 58
#include "gtkheaderbar.h"
#include "gtkdialogprivate.h"
59
#include "gtksearchbar.h"
60
#include "gtksizegroup.h"
61 62 63 64 65 66 67

#include <string.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>

#define sure_string(s) ((const char *) ((s) != NULL ? (s) : ""))

68
struct _GtkAppChooserDialogPrivate {
69 70
  char *content_type;
  GFile *gfile;
71
  char *heading;
72 73

  GtkWidget *label;
74
  GtkWidget *inner_box;
75 76 77

  GtkWidget *open_label;

78 79
  GtkWidget *search_bar;
  GtkWidget *search_entry;
80
  GtkWidget *app_chooser_widget;
81
  GtkWidget *show_more_button;
82
  GtkWidget *software_button;
83

84 85
  GtkSizeGroup *buttons;

86
  gboolean show_more_clicked;
87
  gboolean dismissed;
88 89 90 91 92
};

enum {
  PROP_GFILE = 1,
  PROP_CONTENT_TYPE,
93
  PROP_HEADING
94 95
};

96 97
static void gtk_app_chooser_dialog_iface_init (GtkAppChooserIface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkAppChooserDialog, gtk_app_chooser_dialog, GTK_TYPE_DIALOG,
98
                         G_ADD_PRIVATE (GtkAppChooserDialog)
Matthias Clasen's avatar
Matthias Clasen committed
99 100
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_APP_CHOOSER,
                                                gtk_app_chooser_dialog_iface_init));
101

102

103 104 105 106 107 108 109
static void
add_or_find_application (GtkAppChooserDialog *self)
{
  GAppInfo *app;

  app = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self));

110 111 112 113 114 115 116 117 118
  if (app)
    {
      /* we don't care about reporting errors here */
      if (self->priv->content_type)
        g_app_info_set_as_last_used_for_type (app,
                                              self->priv->content_type,
                                              NULL);
      g_object_unref (app);
    }
119 120 121 122
}

static void
gtk_app_chooser_dialog_response (GtkDialog *dialog,
Matthias Clasen's avatar
Matthias Clasen committed
123 124
                                 gint       response_id,
                                 gpointer   user_data)
125 126 127 128 129 130 131 132
{
  GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (dialog);

  switch (response_id)
    {
    case GTK_RESPONSE_OK:
      add_or_find_application (self);
      break;
133 134 135
    case GTK_RESPONSE_CANCEL:
    case GTK_RESPONSE_DELETE_EVENT:
      self->priv->dismissed = TRUE;
136
    default:
137 138 139 140
      break;
    }
}

141
static void
142
widget_application_selected_cb (GtkAppChooserWidget *widget,
Matthias Clasen's avatar
Matthias Clasen committed
143 144
                                GAppInfo            *app_info,
                                gpointer             user_data)
145
{
146
  GtkDialog *self = user_data;
147

148
  gtk_dialog_set_response_sensitive (self, GTK_RESPONSE_OK, TRUE);
149 150 151
}

static void
152
widget_application_activated_cb (GtkAppChooserWidget *widget,
Matthias Clasen's avatar
Matthias Clasen committed
153 154
                                 GAppInfo            *app_info,
                                 gpointer             user_data)
155
{
156
  GtkAppChooserDialog *self = user_data;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

  gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_OK);
}

static char *
get_extension (const char *basename)
{
  char *p;

  p = strrchr (basename, '.');

  if (p && *(p + 1) != '\0')
    return g_strdup (p + 1);

  return NULL;
}

static void
175
set_dialog_properties (GtkAppChooserDialog *self)
176
{
Matthias Clasen's avatar
Matthias Clasen committed
177 178 179 180
  gchar *name;
  gchar *extension;
  gchar *description;
  gchar *string;
181
  gboolean unknown;
182 183 184 185
  gchar *title;
  gchar *subtitle;
  gboolean use_header;
  GtkWidget *header;
186 187 188 189

  name = NULL;
  extension = NULL;
  description = NULL;
190
  unknown = TRUE;
191 192 193 194 195 196 197

  if (self->priv->gfile != NULL)
    {
      name = g_file_get_basename (self->priv->gfile);
      extension = get_extension (name);
    }

198 199 200 201 202 203
  if (self->priv->content_type)
    {
      description = g_content_type_get_description (self->priv->content_type);
      unknown = g_content_type_is_unknown (self->priv->content_type);
    }

204
  if (name != NULL)
205
    {
206
      title = g_strdup (_("Select Application"));
207
      /* Translators: %s is a filename */
208 209
      subtitle = g_strdup_printf (_("Opening “%s”."), name);
      string = g_strdup_printf (_("No applications found for “%s”"), name);
210 211 212
    }
  else
    {
213
      title = g_strdup (_("Select Application"));
214
      /* Translators: %s is a file type description */
215 216 217 218
      subtitle = g_strdup_printf (_("Opening “%s” files."), 
                                  unknown ? self->priv->content_type : description);
      string = g_strdup_printf (_("No applications found for “%s” files"),
                                unknown ? self->priv->content_type : description);
219 220
    }

221 222 223 224 225 226 227 228 229 230 231
  g_object_get (self, "use-header-bar", &use_header, NULL); 
  if (use_header)
    {
      header = gtk_dialog_get_header_bar (GTK_DIALOG (self));
      gtk_header_bar_set_title (GTK_HEADER_BAR (header), title);
      gtk_header_bar_set_subtitle (GTK_HEADER_BAR (header), subtitle);
    }
  else
    {
      gtk_window_set_title (GTK_WINDOW (self), _("Select Application"));
    }
232

233
  if (self->priv->heading != NULL)
234 235 236 237
    {
      gtk_label_set_markup (GTK_LABEL (self->priv->label), self->priv->heading);
      gtk_widget_show (self->priv->label);
    }
238
  else
239 240 241
    {
      gtk_widget_hide (self->priv->label);
    }
242

243
  gtk_app_chooser_widget_set_default_text (GTK_APP_CHOOSER_WIDGET (self->priv->app_chooser_widget),
244
                                           string);
245

246 247
  g_free (title);
  g_free (subtitle);
248 249 250
  g_free (name);
  g_free (extension);
  g_free (description);
251
  g_free (string);
252 253
}

254 255
static void
show_more_button_clicked_cb (GtkButton *button,
Matthias Clasen's avatar
Matthias Clasen committed
256
                             gpointer   user_data)
257
{
258
  GtkAppChooserDialog *self = user_data;
259

260
  g_object_set (self->priv->app_chooser_widget,
Matthias Clasen's avatar
Matthias Clasen committed
261 262 263 264
                "show-recommended", TRUE,
                "show-fallback", TRUE,
                "show-other", TRUE,
                NULL);
265 266

  gtk_widget_hide (self->priv->show_more_button);
267 268 269 270
  self->priv->show_more_clicked = TRUE;
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
271 272 273
widget_notify_for_button_cb (GObject    *source,
                             GParamSpec *pspec,
                             gpointer    user_data)
274
{
275 276
  GtkAppChooserDialog *self = user_data;
  GtkAppChooserWidget *widget = GTK_APP_CHOOSER_WIDGET (source);
277 278
  gboolean should_hide;

279
  should_hide = gtk_app_chooser_widget_get_show_other (widget) ||
280 281 282 283
    self->priv->show_more_clicked;

  if (should_hide)
    gtk_widget_hide (self->priv->show_more_button);
284 285
}

286 287
static void
forget_menu_item_activate_cb (GtkMenuItem *item,
Matthias Clasen's avatar
Matthias Clasen committed
288
                              gpointer     user_data)
289 290 291 292 293 294 295 296
{
  GtkAppChooserDialog *self = user_data;
  GAppInfo *info;

  info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self));

  if (info != NULL)
    {
Matthias Clasen's avatar
Matthias Clasen committed
297
      g_app_info_remove_supports_type (info, self->priv->content_type, NULL);
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

      gtk_app_chooser_refresh (GTK_APP_CHOOSER (self));

      g_object_unref (info);
    }
}

static GtkWidget *
build_forget_menu_item (GtkAppChooserDialog *self)
{
  GtkWidget *retval;

  retval = gtk_menu_item_new_with_label (_("Forget association"));
  gtk_widget_show (retval);

  g_signal_connect (retval, "activate",
Matthias Clasen's avatar
Matthias Clasen committed
314
                    G_CALLBACK (forget_menu_item_activate_cb), self);
315 316 317 318 319 320

  return retval;
}

static void
widget_populate_popup_cb (GtkAppChooserWidget *widget,
Matthias Clasen's avatar
Matthias Clasen committed
321 322 323
                          GtkMenu             *menu,
                          GAppInfo            *info,
                          gpointer             user_data)
324 325 326 327 328 329 330 331 332 333 334
{
  GtkAppChooserDialog *self = user_data;
  GtkWidget *menu_item;

  if (g_app_info_can_remove_supports_type (info))
    {
      menu_item = build_forget_menu_item (self);
      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
    }
}

335 336 337 338 339 340 341 342
static gboolean
key_press_event_cb (GtkWidget    *widget,
                    GdkEvent     *event,
                    GtkSearchBar *bar)
{
  return gtk_search_bar_handle_event (bar, event);
}

343
static void
344
construct_appchooser_widget (GtkAppChooserDialog *self)
345
{
346
  GAppInfo *info;
347

348
  /* Need to build the appchooser widget after, because of the content-type construct-only property */
349
  self->priv->app_chooser_widget = gtk_app_chooser_widget_new (self->priv->content_type);
350
  gtk_box_pack_start (GTK_BOX (self->priv->inner_box), self->priv->app_chooser_widget, TRUE, TRUE, 0);
351
  gtk_widget_show (self->priv->app_chooser_widget);
352

353
  g_signal_connect (self->priv->app_chooser_widget, "application-selected",
Matthias Clasen's avatar
Matthias Clasen committed
354
                    G_CALLBACK (widget_application_selected_cb), self);
355
  g_signal_connect (self->priv->app_chooser_widget, "application-activated",
Matthias Clasen's avatar
Matthias Clasen committed
356
                    G_CALLBACK (widget_application_activated_cb), self);
357
  g_signal_connect (self->priv->app_chooser_widget, "notify::show-other",
Matthias Clasen's avatar
Matthias Clasen committed
358
                    G_CALLBACK (widget_notify_for_button_cb), self);
359
  g_signal_connect (self->priv->app_chooser_widget, "populate-popup",
Matthias Clasen's avatar
Matthias Clasen committed
360
                    G_CALLBACK (widget_populate_popup_cb), self);
361

362
  /* Add the custom button to the new appchooser */
363 364 365 366 367
  gtk_box_pack_start (GTK_BOX (self->priv->inner_box),
		      self->priv->show_more_button, FALSE, FALSE, 0);

  gtk_box_pack_start (GTK_BOX (self->priv->inner_box),
		      self->priv->software_button, FALSE, FALSE, 0);
368

369
  info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
370
  gtk_dialog_set_response_sensitive (GTK_DIALOG (self), GTK_RESPONSE_OK, info != NULL);
371 372
  if (info)
    g_object_unref (info);
373 374 375 376 377

  _gtk_app_chooser_widget_set_search_entry (GTK_APP_CHOOSER_WIDGET (self->priv->app_chooser_widget),
                                            GTK_ENTRY (self->priv->search_entry));
  g_signal_connect (self, "key-press-event",
                    G_CALLBACK (key_press_event_cb), self->priv->search_bar);
378 379 380
}

static void
381
set_gfile_and_content_type (GtkAppChooserDialog *self,
382
                            GFile               *file)
383 384 385 386 387 388 389 390 391
{
  GFileInfo *info;

  if (file == NULL)
    return;

  self->priv->gfile = g_object_ref (file);

  info = g_file_query_info (self->priv->gfile,
Matthias Clasen's avatar
Matthias Clasen committed
392 393
                            G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
                            0, NULL, NULL);
394 395 396 397 398 399
  self->priv->content_type = g_strdup (g_file_info_get_content_type (info));

  g_object_unref (info);
}

static GAppInfo *
400
gtk_app_chooser_dialog_get_app_info (GtkAppChooser *object)
401
{
402
  GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
403
  return gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
404 405
}

406
static void
407
gtk_app_chooser_dialog_refresh (GtkAppChooser *object)
408
{
409
  GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
410

411
  gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
412 413
}

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
static void
show_error_dialog (const gchar *primary,
                   const gchar *secondary,
                   GtkWindow   *parent)
{
  GtkWidget *message_dialog;

  message_dialog = gtk_message_dialog_new (parent, 0,
                                           GTK_MESSAGE_ERROR,
                                           GTK_BUTTONS_OK,
                                           NULL);
  g_object_set (message_dialog,
                "text", primary,
                "secondary-text", secondary,
                NULL);
  gtk_dialog_set_default_response (GTK_DIALOG (message_dialog), GTK_RESPONSE_OK);

  gtk_widget_show (message_dialog);

  g_signal_connect (message_dialog, "response",
                    G_CALLBACK (gtk_widget_destroy), NULL);
}

static void
software_button_clicked_cb (GtkButton           *button,
                            GtkAppChooserDialog *self)
{
  GSubprocess *process;
  GError *error = NULL;
  gchar *option;

  if (self->priv->content_type)
    option = g_strconcat ("--search=", self->priv->content_type, NULL);
  else
    option = g_strdup ("--mode=overview");

  process = g_subprocess_new (0, &error, "gnome-software", option, NULL);
  if (!process)
    {
      show_error_dialog (_("Failed to start GNOME Software"),
                         error->message, GTK_WINDOW (self));
      g_error_free (error);
    }
  else
    g_object_unref (process);

  g_free (option);
}

static void
ensure_software_button (GtkAppChooserDialog *self)
{
466 467 468 469
  gchar *path;

  path = g_find_program_in_path ("gnome-software");
  if (path != NULL)
470 471 472
    gtk_widget_show (self->priv->software_button);
  else
    gtk_widget_hide (self->priv->software_button);
473 474

  g_free (path);
475 476
}

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
static void
setup_search (GtkAppChooserDialog *self)
{
  gboolean use_header;

  g_object_get (self, "use-header-bar", &use_header, NULL);
  if (use_header)
    {
      GtkWidget *button;
      GtkWidget *image;
      GtkWidget *header;

      button = gtk_toggle_button_new ();
      gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
      image = gtk_image_new_from_icon_name ("edit-find-symbolic", GTK_ICON_SIZE_MENU);
      gtk_widget_show (image);
      gtk_container_add (GTK_CONTAINER (button), image);
      gtk_style_context_add_class (gtk_widget_get_style_context (button), "image-button");
      gtk_style_context_remove_class (gtk_widget_get_style_context (button), "text-button");
      gtk_widget_show (button);

      header = gtk_dialog_get_header_bar (GTK_DIALOG (self));
      gtk_header_bar_pack_end (GTK_HEADER_BAR (header), button);
500
      gtk_size_group_add_widget (self->priv->buttons, button);
501 502 503 504

      g_object_bind_property (button, "active",
                              self->priv->search_bar, "search-mode-enabled",
                              G_BINDING_BIDIRECTIONAL);
505 506 507
      g_object_bind_property (self->priv->search_entry, "sensitive",
                              button, "sensitive",
                              G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
508 509 510
    }
}

511
static void
512
gtk_app_chooser_dialog_constructed (GObject *object)
513
{
514
  GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
515

516 517
  if (G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed != NULL)
    G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed (object);
518

519
  construct_appchooser_widget (self);
520
  set_dialog_properties (self);
521
  ensure_software_button (self);
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
  setup_search (self);
}

/* This is necessary do deal with the fact that GtkDialog
 * exposes bits of its internal spacing as style properties,
 * and puts the action area inside the content area.
 * To achieve a flush-top search bar, we need the content
 * area border to be 0, and distribute the spacing to other
 * containers to compensate.
 */
static void
update_spacings (GtkAppChooserDialog *self)
{
  GtkWidget *widget;
  gint content_area_border;
  gint action_area_border;

  gtk_widget_style_get (GTK_WIDGET (self),
                        "content-area-border", &content_area_border,
                        "action-area-border", &action_area_border,
                        NULL);

  widget = gtk_dialog_get_content_area (GTK_DIALOG (self));
  gtk_container_set_border_width (GTK_CONTAINER (widget), 0);

G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  widget = gtk_dialog_get_action_area (GTK_DIALOG (self));
G_GNUC_END_IGNORE_DEPRECATIONS
  gtk_container_set_border_width (GTK_CONTAINER (widget), 5 + content_area_border + action_area_border);

  widget = self->priv->inner_box;
  gtk_container_set_border_width (GTK_CONTAINER (widget), 10 + content_area_border);
}

static void
gtk_app_chooser_dialog_style_updated (GtkWidget *widget)
{
  GTK_WIDGET_CLASS (gtk_app_chooser_dialog_parent_class)->style_updated (widget);

  update_spacings (GTK_APP_CHOOSER_DIALOG (widget));
562 563 564
}

static void
565
gtk_app_chooser_dialog_dispose (GObject *object)
566
{
567
  GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
568 569
  
  g_clear_object (&self->priv->gfile);
570

571 572
  self->priv->dismissed = TRUE;

573 574 575 576 577 578 579
  G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->dispose (object);
}

static void
gtk_app_chooser_dialog_finalize (GObject *object)
{
  GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
580 581 582

  g_free (self->priv->content_type);

583
  G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->finalize (object);
584 585 586
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
587 588 589 590
gtk_app_chooser_dialog_set_property (GObject      *object,
                                     guint         property_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
591
{
592
  GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
593 594 595 596

  switch (property_id)
    {
    case PROP_GFILE:
597
      set_gfile_and_content_type (self, g_value_get_object (value));
598 599
      break;
    case PROP_CONTENT_TYPE:
600 601
      /* don't try to override a value previously set with the GFile */
      if (self->priv->content_type == NULL)
Matthias Clasen's avatar
Matthias Clasen committed
602
        self->priv->content_type = g_value_dup_string (value);
603
      break;
604 605 606
    case PROP_HEADING:
      gtk_app_chooser_dialog_set_heading (self, g_value_get_string (value));
      break;
607 608 609 610 611 612 613
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
614 615 616 617
gtk_app_chooser_dialog_get_property (GObject    *object,
                                     guint       property_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
618
{
619
  GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
620 621 622 623 624

  switch (property_id)
    {
    case PROP_GFILE:
      if (self->priv->gfile != NULL)
Matthias Clasen's avatar
Matthias Clasen committed
625
        g_value_set_object (value, self->priv->gfile);
626 627 628 629
      break;
    case PROP_CONTENT_TYPE:
      g_value_set_string (value, self->priv->content_type);
      break;
630 631 632
    case PROP_HEADING:
      g_value_set_string (value, self->priv->heading);
      break;
633 634 635 636 637 638
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

639
static void
640
gtk_app_chooser_dialog_iface_init (GtkAppChooserIface *iface)
641
{
642 643
  iface->get_app_info = gtk_app_chooser_dialog_get_app_info;
  iface->refresh = gtk_app_chooser_dialog_refresh;
644 645
}

646
static void
647
gtk_app_chooser_dialog_class_init (GtkAppChooserDialogClass *klass)
648
{
649
  GtkWidgetClass *widget_class;
650
  GObjectClass *gobject_class;
651
  GParamSpec *pspec;
652 653

  gobject_class = G_OBJECT_CLASS (klass);
654
  gobject_class->dispose = gtk_app_chooser_dialog_dispose;
655 656 657 658
  gobject_class->finalize = gtk_app_chooser_dialog_finalize;
  gobject_class->set_property = gtk_app_chooser_dialog_set_property;
  gobject_class->get_property = gtk_app_chooser_dialog_get_property;
  gobject_class->constructed = gtk_app_chooser_dialog_constructed;
659

660 661 662
  widget_class = GTK_WIDGET_CLASS (klass);
  widget_class->style_updated = gtk_app_chooser_dialog_style_updated;

663 664
  g_object_class_override_property (gobject_class, PROP_CONTENT_TYPE, "content-type");

Cosimo Cecchi's avatar
Cosimo Cecchi committed
665 666 667 668 669 670 671
  /**
   * GtkAppChooserDialog:gfile:
   *
   * The GFile used by the #GtkAppChooserDialog.
   * The dialog's #GtkAppChooserWidget content type will be guessed from the
   * file, if present.
   */
672
  pspec = g_param_spec_object ("gfile",
Matthias Clasen's avatar
Matthias Clasen committed
673
                               P_("GFile"),
Cosimo Cecchi's avatar
Cosimo Cecchi committed
674
                               P_("The GFile used by the app chooser dialog"),
Matthias Clasen's avatar
Matthias Clasen committed
675 676 677
                               G_TYPE_FILE,
                               G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
                               G_PARAM_STATIC_STRINGS);
678
  g_object_class_install_property (gobject_class, PROP_GFILE, pspec);
679

680 681 682 683 684 685 686 687 688 689 690 691 692
  /**
   * GtkAppChooserDialog:heading:
   *
   * The text to show at the top of the dialog.
   * 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 (gobject_class, PROP_HEADING, pspec);

693 694 695
  /* Bind class to template
   */
  gtk_widget_class_set_template_from_resource (widget_class,
696
					       "/org/gtk/libgtk/ui/gtkappchooserdialog.ui");
697 698
  gtk_widget_class_bind_template_child_private (widget_class, GtkAppChooserDialog, label);
  gtk_widget_class_bind_template_child_private (widget_class, GtkAppChooserDialog, show_more_button);
699
  gtk_widget_class_bind_template_child_private (widget_class, GtkAppChooserDialog, software_button);
700
  gtk_widget_class_bind_template_child_private (widget_class, GtkAppChooserDialog, inner_box);
701 702
  gtk_widget_class_bind_template_child_private (widget_class, GtkAppChooserDialog, search_bar);
  gtk_widget_class_bind_template_child_private (widget_class, GtkAppChooserDialog, search_entry);
703
  gtk_widget_class_bind_template_child_private (widget_class, GtkAppChooserDialog, buttons);
704
  gtk_widget_class_bind_template_callback (widget_class, show_more_button_clicked_cb);
705
  gtk_widget_class_bind_template_callback (widget_class, software_button_clicked_cb);
706 707 708
}

static void
709
gtk_app_chooser_dialog_init (GtkAppChooserDialog *self)
710
{
711
  self->priv = gtk_app_chooser_dialog_get_instance_private (self);
712

713
  gtk_widget_init_template (GTK_WIDGET (self));
714 715 716 717 718 719 720 721
  gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (self));

G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (self),
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
G_GNUC_END_IGNORE_DEPRECATIONS
722

723 724 725 726
  /* we can't override the class signal handler here, as it's a RUN_LAST;
   * we want our signal handler instead to be executed before any user code.
   */
  g_signal_connect (self, "response",
Matthias Clasen's avatar
Matthias Clasen committed
727
                    G_CALLBACK (gtk_app_chooser_dialog_response), NULL);
728 729

  update_spacings (self);
730 731 732
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
733 734 735
set_parent_and_flags (GtkWidget      *dialog,
                      GtkWindow      *parent,
                      GtkDialogFlags  flags)
736 737
{
  if (parent != NULL)
Matthias Clasen's avatar
Matthias Clasen committed
738 739
    gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);

740 741 742 743 744 745 746
  if (flags & GTK_DIALOG_MODAL)
    gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);

  if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
    gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
}

747
/**
748
 * gtk_app_chooser_dialog_new:
749 750 751 752
 * @parent: (allow-none): a #GtkWindow, or %NULL
 * @flags: flags for this dialog
 * @file: a #GFile
 *
Matthias Clasen's avatar
Matthias Clasen committed
753 754
 * Creates a new #GtkAppChooserDialog for the provided #GFile,
 * to allow the user to select an application for it.
755
 *
756
 * Returns: a newly created #GtkAppChooserDialog
757 758 759
 *
 * Since: 3.0
 **/
760
GtkWidget *
Matthias Clasen's avatar
Matthias Clasen committed
761 762 763
gtk_app_chooser_dialog_new (GtkWindow      *parent,
                            GtkDialogFlags  flags,
                            GFile          *file)
764 765 766 767 768
{
  GtkWidget *retval;

  g_return_val_if_fail (G_IS_FILE (file), NULL);

769
  retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
Matthias Clasen's avatar
Matthias Clasen committed
770 771
                         "gfile", file,
                         NULL);
772 773 774 775 776 777

  set_parent_and_flags (retval, parent, flags);

  return retval;
}

778
/**
779
 * gtk_app_chooser_dialog_new_for_content_type:
780 781 782 783
 * @parent: (allow-none): a #GtkWindow, or %NULL
 * @flags: flags for this dialog
 * @content_type: a content type string
 *
Matthias Clasen's avatar
Matthias Clasen committed
784 785
 * Creates a new #GtkAppChooserDialog for the provided content type,
 * to allow the user to select an application for it.
786
 *
787
 * Returns: a newly created #GtkAppChooserDialog
788 789 790
 *
 * Since: 3.0
 **/
791
GtkWidget *
Matthias Clasen's avatar
Matthias Clasen committed
792 793 794
gtk_app_chooser_dialog_new_for_content_type (GtkWindow      *parent,
                                             GtkDialogFlags  flags,
                                             const gchar    *content_type)
795 796 797 798 799
{
  GtkWidget *retval;

  g_return_val_if_fail (content_type != NULL, NULL);

800
  retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
Matthias Clasen's avatar
Matthias Clasen committed
801 802
                         "content-type", content_type,
                         NULL);
803 804 805 806 807

  set_parent_and_flags (retval, parent, flags);

  return retval;
}
808

Matthias Clasen's avatar
Matthias Clasen committed
809 810 811 812 813 814
/**
 * gtk_app_chooser_dialog_get_widget:
 * @self: a #GtkAppChooserDialog
 *
 * Returns the #GtkAppChooserWidget of this dialog.
 *
815
 * Returns: (transfer none): the #GtkAppChooserWidget of @self
Matthias Clasen's avatar
Matthias Clasen committed
816 817 818
 *
 * Since: 3.0
 */
819
GtkWidget *
820
gtk_app_chooser_dialog_get_widget (GtkAppChooserDialog *self)
821
{
822
  g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);
823

824
  return self->priv->app_chooser_widget;
825
}
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843

/**
 * gtk_app_chooser_dialog_set_heading:
 * @self: a #GtkAppChooserDialog
 * @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_dialog_set_heading (GtkAppChooserDialog *self,
                                    const gchar         *heading)
{
  g_return_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self));

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

844 845 846 847 848 849 850 851 852 853 854 855
  if (self->priv->label)
    {
      if (self->priv->heading)
        {
          gtk_label_set_markup (GTK_LABEL (self->priv->label), self->priv->heading);
          gtk_widget_show (self->priv->label);
        }
      else
        {
          gtk_widget_hide (self->priv->label);
        }
    }
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875

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

/**
 * gtk_app_chooser_dialog_get_heading:
 * @self: a #GtkAppChooserDialog
 *
 * 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_dialog_get_heading (GtkAppChooserDialog *self)
{
  g_return_val_if_fail (GTK_IS_APP_CHOOSER_DIALOG (self), NULL);

  return self->priv->heading;
}