2010-11-23 16:23:37 +00:00
|
|
|
/* testappchooser.c
|
2010-11-16 12:47:51 +00:00
|
|
|
* Copyright (C) 2010 Red Hat, Inc.
|
|
|
|
* Authors: Cosimo Cecchi
|
|
|
|
*
|
|
|
|
* 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
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2010-11-16 12:47:51 +00:00
|
|
|
*/
|
|
|
|
|
2012-11-06 19:51:15 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2010-11-16 12:47:51 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
static GtkWidget *toplevel;
|
|
|
|
static GFile *file;
|
2010-11-22 23:53:03 +00:00
|
|
|
static GtkWidget *grid, *file_l, *open;
|
2010-11-17 20:38:05 +00:00
|
|
|
static GtkWidget *radio_file, *radio_content, *dialog;
|
2010-11-23 16:23:37 +00:00
|
|
|
static GtkWidget *app_chooser_widget;
|
2011-09-10 05:40:05 +00:00
|
|
|
static GtkWidget *def, *recommended, *fallback, *other, *all;
|
2010-11-16 17:04:33 +00:00
|
|
|
|
2010-11-16 12:47:51 +00:00
|
|
|
static void
|
2010-11-16 17:04:33 +00:00
|
|
|
dialog_response (GtkDialog *d,
|
2020-07-24 13:54:49 +00:00
|
|
|
int response_id,
|
2010-11-24 21:50:00 +00:00
|
|
|
gpointer user_data)
|
2010-11-16 12:47:51 +00:00
|
|
|
{
|
2010-11-16 17:04:33 +00:00
|
|
|
GAppInfo *app_info;
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *name;
|
2010-11-16 17:04:33 +00:00
|
|
|
|
2010-11-16 12:47:51 +00:00
|
|
|
g_print ("Response: %d\n", response_id);
|
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
if (response_id == GTK_RESPONSE_OK)
|
|
|
|
{
|
2010-11-23 16:23:37 +00:00
|
|
|
app_info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (d));
|
2011-08-29 03:50:50 +00:00
|
|
|
if (app_info)
|
|
|
|
{
|
|
|
|
name = g_app_info_get_name (app_info);
|
|
|
|
g_print ("Application selected: %s\n", name);
|
|
|
|
g_object_unref (app_info);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_print ("No application selected\n");
|
2010-11-16 17:04:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (d));
|
2010-11-22 23:53:03 +00:00
|
|
|
dialog = NULL;
|
2010-11-16 12:47:51 +00:00
|
|
|
}
|
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
static void
|
2010-11-22 23:53:03 +00:00
|
|
|
bind_props (void)
|
|
|
|
{
|
2011-09-10 05:40:05 +00:00
|
|
|
g_object_bind_property (def, "active",
|
|
|
|
app_chooser_widget, "show-default",
|
|
|
|
G_BINDING_SYNC_CREATE);
|
2010-11-22 23:53:03 +00:00
|
|
|
g_object_bind_property (recommended, "active",
|
2010-11-24 21:50:00 +00:00
|
|
|
app_chooser_widget, "show-recommended",
|
|
|
|
G_BINDING_SYNC_CREATE);
|
2010-11-22 23:53:03 +00:00
|
|
|
g_object_bind_property (fallback, "active",
|
2010-11-24 21:50:00 +00:00
|
|
|
app_chooser_widget, "show-fallback",
|
|
|
|
G_BINDING_SYNC_CREATE);
|
2010-11-22 23:53:03 +00:00
|
|
|
g_object_bind_property (other, "active",
|
2010-11-24 21:50:00 +00:00
|
|
|
app_chooser_widget, "show-other",
|
|
|
|
G_BINDING_SYNC_CREATE);
|
2010-11-22 23:53:03 +00:00
|
|
|
g_object_bind_property (all, "active",
|
2010-11-24 21:50:00 +00:00
|
|
|
app_chooser_widget, "show-all",
|
|
|
|
G_BINDING_SYNC_CREATE);
|
2010-11-22 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prepare_dialog (void)
|
2010-11-16 12:47:51 +00:00
|
|
|
{
|
2010-11-16 17:04:33 +00:00
|
|
|
gboolean use_file = FALSE;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *content_type = NULL;
|
2010-11-16 12:47:51 +00:00
|
|
|
|
2010-11-16 18:21:23 +00:00
|
|
|
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radio_file)))
|
2010-11-17 20:38:05 +00:00
|
|
|
use_file = TRUE;
|
2010-11-16 17:04:33 +00:00
|
|
|
else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radio_content)))
|
2010-11-17 20:38:05 +00:00
|
|
|
use_file = FALSE;
|
2010-11-16 12:47:51 +00:00
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
if (use_file)
|
|
|
|
{
|
2010-11-24 23:32:05 +00:00
|
|
|
dialog = gtk_app_chooser_dialog_new (GTK_WINDOW (toplevel), 0, file);
|
2010-11-16 12:47:51 +00:00
|
|
|
}
|
2010-11-16 17:04:33 +00:00
|
|
|
else
|
|
|
|
{
|
2010-11-16 18:21:23 +00:00
|
|
|
GFileInfo *info;
|
|
|
|
|
|
|
|
info = g_file_query_info (file,
|
2010-11-24 21:50:00 +00:00
|
|
|
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
|
|
|
|
0, NULL, NULL);
|
2010-11-16 18:21:23 +00:00
|
|
|
content_type = g_strdup (g_file_info_get_content_type (info));
|
|
|
|
|
|
|
|
g_object_unref (info);
|
|
|
|
|
2010-11-23 16:23:37 +00:00
|
|
|
dialog = gtk_app_chooser_dialog_new_for_content_type (GTK_WINDOW (toplevel),
|
2010-11-25 16:19:00 +00:00
|
|
|
0, content_type);
|
2010-11-16 17:04:33 +00:00
|
|
|
}
|
|
|
|
|
2011-01-21 01:55:59 +00:00
|
|
|
gtk_app_chooser_dialog_set_heading (GTK_APP_CHOOSER_DIALOG (dialog), "Select one already, you <i>fool</i>");
|
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
g_signal_connect (dialog, "response",
|
2010-11-24 21:50:00 +00:00
|
|
|
G_CALLBACK (dialog_response), NULL);
|
2010-11-16 17:04:33 +00:00
|
|
|
|
|
|
|
g_free (content_type);
|
2010-11-22 23:53:03 +00:00
|
|
|
|
2010-11-23 16:23:37 +00:00
|
|
|
app_chooser_widget = gtk_app_chooser_dialog_get_widget (GTK_APP_CHOOSER_DIALOG (dialog));
|
2010-11-22 23:53:03 +00:00
|
|
|
bind_props ();
|
2010-11-16 17:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-11-22 23:53:03 +00:00
|
|
|
display_dialog (void)
|
2010-11-16 17:04:33 +00:00
|
|
|
{
|
2010-11-22 23:53:03 +00:00
|
|
|
if (dialog == NULL)
|
|
|
|
prepare_dialog ();
|
2010-11-24 21:50:00 +00:00
|
|
|
|
2010-11-22 23:53:03 +00:00
|
|
|
gtk_widget_show (dialog);
|
2010-11-16 17:04:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 14:32:38 +00:00
|
|
|
static void
|
|
|
|
on_open_response (GtkWidget *file_chooser,
|
|
|
|
int response)
|
|
|
|
{
|
|
|
|
if (response == GTK_RESPONSE_ACCEPT)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (file_chooser));
|
|
|
|
path = g_file_get_path (file);
|
|
|
|
|
|
|
|
gtk_button_set_label (GTK_BUTTON (file_l), path);
|
|
|
|
|
|
|
|
g_free (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_window_destroy (GTK_WINDOW (file_chooser));
|
|
|
|
|
|
|
|
gtk_widget_set_sensitive (open, TRUE);
|
|
|
|
}
|
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
static void
|
|
|
|
button_clicked (GtkButton *b,
|
2010-11-24 21:50:00 +00:00
|
|
|
gpointer user_data)
|
2010-11-16 17:04:33 +00:00
|
|
|
{
|
|
|
|
GtkWidget *w;
|
2010-11-16 12:47:51 +00:00
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
w = gtk_file_chooser_dialog_new ("Select file",
|
2010-11-24 21:50:00 +00:00
|
|
|
GTK_WINDOW (toplevel),
|
|
|
|
GTK_FILE_CHOOSER_ACTION_OPEN,
|
2013-06-24 21:31:22 +00:00
|
|
|
"_Cancel", GTK_RESPONSE_CANCEL,
|
|
|
|
"_Open", GTK_RESPONSE_ACCEPT,
|
2010-11-24 21:50:00 +00:00
|
|
|
NULL);
|
2010-11-16 17:04:33 +00:00
|
|
|
|
2020-04-30 14:32:38 +00:00
|
|
|
gtk_window_set_modal (GTK_WINDOW (w), TRUE);
|
2010-11-16 17:04:33 +00:00
|
|
|
|
2020-04-30 14:32:38 +00:00
|
|
|
g_signal_connect (w, "response",
|
|
|
|
G_CALLBACK (on_open_response),
|
|
|
|
NULL);
|
2010-11-16 17:04:33 +00:00
|
|
|
|
2020-04-30 14:32:38 +00:00
|
|
|
gtk_window_present (GTK_WINDOW (w));
|
2010-11-16 17:04:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
static void
|
|
|
|
quit_cb (GtkWidget *widget,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
gboolean *done = data;
|
|
|
|
|
|
|
|
*done = TRUE;
|
|
|
|
|
|
|
|
g_main_context_wakeup (NULL);
|
|
|
|
}
|
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
int
|
2010-11-24 21:50:00 +00:00
|
|
|
main (int argc, char **argv)
|
2010-11-16 17:04:33 +00:00
|
|
|
{
|
2010-11-22 23:53:03 +00:00
|
|
|
GtkWidget *w1;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *path;
|
2020-02-10 03:24:47 +00:00
|
|
|
gboolean done = FALSE;
|
2010-11-16 17:04:33 +00:00
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
2010-11-16 17:04:33 +00:00
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
toplevel = gtk_window_new ();
|
2010-11-16 17:04:33 +00:00
|
|
|
grid = gtk_grid_new ();
|
|
|
|
|
2010-11-22 23:53:03 +00:00
|
|
|
w1 = gtk_label_new ("File:");
|
|
|
|
gtk_widget_set_halign (w1, GTK_ALIGN_START);
|
2010-11-16 17:04:33 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid),
|
2010-11-24 21:50:00 +00:00
|
|
|
w1, 0, 0, 1, 1);
|
2010-11-16 17:04:33 +00:00
|
|
|
|
2010-11-18 22:31:13 +00:00
|
|
|
file_l = gtk_button_new ();
|
2020-04-30 14:32:38 +00:00
|
|
|
path = g_build_filename (GTK_SRCDIR, "apple-red.png", NULL);
|
2010-11-18 22:31:13 +00:00
|
|
|
file = g_file_new_for_path (path);
|
|
|
|
gtk_button_set_label (GTK_BUTTON (file_l), path);
|
|
|
|
g_free (path);
|
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
gtk_widget_set_halign (file_l, GTK_ALIGN_START);
|
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), file_l,
|
2010-11-24 21:50:00 +00:00
|
|
|
w1, GTK_POS_RIGHT, 3, 1);
|
2010-11-16 17:04:33 +00:00
|
|
|
g_signal_connect (file_l, "clicked",
|
2010-11-24 21:50:00 +00:00
|
|
|
G_CALLBACK (button_clicked), NULL);
|
2010-11-16 17:04:33 +00:00
|
|
|
|
2010-11-17 20:38:05 +00:00
|
|
|
radio_file = gtk_radio_button_new_with_label (NULL, "Use GFile");
|
2010-11-16 17:04:33 +00:00
|
|
|
radio_content = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio_file),
|
2010-11-24 21:50:00 +00:00
|
|
|
"Use content type");
|
2010-11-16 17:04:33 +00:00
|
|
|
|
|
|
|
gtk_grid_attach (GTK_GRID (grid), radio_file,
|
2010-11-24 21:50:00 +00:00
|
|
|
0, 1, 1, 1);
|
2010-11-16 17:04:33 +00:00
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), radio_content,
|
2010-11-24 21:50:00 +00:00
|
|
|
radio_file, GTK_POS_BOTTOM, 1, 1);
|
2010-11-16 17:04:33 +00:00
|
|
|
|
2010-11-23 16:23:37 +00:00
|
|
|
open = gtk_button_new_with_label ("Trigger App Chooser dialog");
|
2010-11-16 17:04:33 +00:00
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), open,
|
2010-11-24 21:50:00 +00:00
|
|
|
radio_content, GTK_POS_BOTTOM, 1, 1);
|
2010-11-18 22:31:13 +00:00
|
|
|
|
2010-11-22 23:53:03 +00:00
|
|
|
recommended = gtk_check_button_new_with_label ("Show recommended");
|
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), recommended,
|
2010-11-24 21:50:00 +00:00
|
|
|
open, GTK_POS_BOTTOM, 1, 1);
|
2010-11-22 23:53:03 +00:00
|
|
|
g_object_set (recommended, "active", TRUE, NULL);
|
|
|
|
|
|
|
|
fallback = gtk_check_button_new_with_label ("Show fallback");
|
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), fallback,
|
2010-11-24 21:50:00 +00:00
|
|
|
recommended, GTK_POS_RIGHT, 1, 1);
|
2010-11-22 23:53:03 +00:00
|
|
|
|
|
|
|
other = gtk_check_button_new_with_label ("Show other");
|
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), other,
|
2010-11-24 21:50:00 +00:00
|
|
|
fallback, GTK_POS_RIGHT, 1, 1);
|
2010-11-22 23:53:03 +00:00
|
|
|
|
|
|
|
all = gtk_check_button_new_with_label ("Show all");
|
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), all,
|
2010-11-24 21:50:00 +00:00
|
|
|
other, GTK_POS_RIGHT, 1, 1);
|
2010-11-22 23:53:03 +00:00
|
|
|
|
2011-09-10 05:40:05 +00:00
|
|
|
def = gtk_check_button_new_with_label ("Show default");
|
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), def,
|
|
|
|
all, GTK_POS_RIGHT, 1, 1);
|
|
|
|
|
|
|
|
g_object_set (recommended, "active", TRUE, NULL);
|
2010-11-22 23:53:03 +00:00
|
|
|
prepare_dialog ();
|
2010-11-16 17:04:33 +00:00
|
|
|
g_signal_connect (open, "clicked",
|
2010-11-24 21:50:00 +00:00
|
|
|
G_CALLBACK (display_dialog), NULL);
|
2010-11-16 17:04:33 +00:00
|
|
|
|
2020-05-10 21:46:28 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (toplevel), grid);
|
2010-11-16 17:04:33 +00:00
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (toplevel);
|
2020-02-10 03:24:47 +00:00
|
|
|
g_signal_connect (toplevel, "destroy", G_CALLBACK (quit_cb), &done);
|
2010-11-16 12:47:51 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
while (!done)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2010-11-16 12:47:51 +00:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|