2010-11-16 12:47:51 +00:00
|
|
|
/* testopenwith.c
|
|
|
|
* 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
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
static GtkWidget *toplevel;
|
|
|
|
static GFile *file;
|
2010-11-17 18:28:48 +00:00
|
|
|
static GtkWidget *grid, *file_l, *open, *show_mode;
|
2010-11-17 20:38:05 +00:00
|
|
|
static GtkWidget *radio_file, *radio_content, *dialog;
|
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,
|
|
|
|
gint response_id,
|
|
|
|
gpointer user_data)
|
2010-11-16 12:47:51 +00:00
|
|
|
{
|
2010-11-16 17:04:33 +00:00
|
|
|
GAppInfo *app_info;
|
|
|
|
const gchar *name;
|
|
|
|
|
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-17 18:28:48 +00:00
|
|
|
app_info = gtk_open_with_get_app_info (GTK_OPEN_WITH (d));
|
2010-11-16 17:04:33 +00:00
|
|
|
name = g_app_info_get_name (app_info);
|
|
|
|
g_print ("Application selected: %s\n", name);
|
|
|
|
|
|
|
|
g_object_unref (app_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_destroy (GTK_WIDGET (d));
|
2010-11-16 12:47:51 +00:00
|
|
|
}
|
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
static void
|
|
|
|
display_dialog (GtkButton *b,
|
|
|
|
gpointer user_data)
|
2010-11-16 12:47:51 +00:00
|
|
|
{
|
2010-11-16 17:04:33 +00:00
|
|
|
gboolean use_file = FALSE;
|
|
|
|
gchar *content_type = NULL;
|
2010-11-17 18:28:48 +00:00
|
|
|
GtkWidget *open_with_widget;
|
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)
|
|
|
|
{
|
|
|
|
dialog = gtk_open_with_dialog_new (GTK_WINDOW (toplevel),
|
2010-11-17 20:38:05 +00:00
|
|
|
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,
|
|
|
|
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
|
|
|
|
0, NULL, NULL);
|
|
|
|
content_type = g_strdup (g_file_info_get_content_type (info));
|
|
|
|
|
|
|
|
g_object_unref (info);
|
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
dialog = gtk_open_with_dialog_new_for_content_type (GTK_WINDOW (toplevel),
|
2010-11-17 20:38:05 +00:00
|
|
|
0, content_type);
|
2010-11-16 17:04:33 +00:00
|
|
|
}
|
|
|
|
|
2010-11-17 18:28:48 +00:00
|
|
|
open_with_widget = gtk_open_with_dialog_get_widget (GTK_OPEN_WITH_DIALOG (dialog));
|
|
|
|
gtk_open_with_widget_set_show_mode (GTK_OPEN_WITH_WIDGET (open_with_widget),
|
2010-11-17 11:33:59 +00:00
|
|
|
gtk_combo_box_get_active (GTK_COMBO_BOX (show_mode)));
|
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
gtk_widget_show (dialog);
|
|
|
|
|
|
|
|
g_signal_connect (dialog, "response",
|
|
|
|
G_CALLBACK (dialog_response), NULL);
|
|
|
|
|
|
|
|
g_free (content_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-11-17 11:33:59 +00:00
|
|
|
show_mode_changed (GtkComboBox *b,
|
|
|
|
gpointer user_data)
|
2010-11-16 17:04:33 +00:00
|
|
|
{
|
|
|
|
if (dialog != NULL)
|
|
|
|
{
|
2010-11-17 11:33:59 +00:00
|
|
|
gint active;
|
2010-11-16 12:47:51 +00:00
|
|
|
|
2010-11-17 11:33:59 +00:00
|
|
|
active = gtk_combo_box_get_active (b);
|
2010-11-17 18:28:48 +00:00
|
|
|
gtk_open_with_widget_set_show_mode (GTK_OPEN_WITH_WIDGET (gtk_open_with_dialog_get_widget (GTK_OPEN_WITH_DIALOG (dialog))),
|
2010-11-17 11:33:59 +00:00
|
|
|
active);
|
2010-11-16 17:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
button_clicked (GtkButton *b,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GtkWidget *w;
|
|
|
|
gchar *path;
|
2010-11-16 12:47:51 +00:00
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
w = gtk_file_chooser_dialog_new ("Select file",
|
|
|
|
GTK_WINDOW (toplevel),
|
|
|
|
GTK_FILE_CHOOSER_ACTION_OPEN,
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
gtk_dialog_run (GTK_DIALOG (w));
|
|
|
|
file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (w));
|
|
|
|
path = g_file_get_path (file);
|
|
|
|
gtk_button_set_label (GTK_BUTTON (file_l), path);
|
|
|
|
|
|
|
|
gtk_widget_destroy (w);
|
|
|
|
|
|
|
|
gtk_widget_set_sensitive (open, TRUE);
|
|
|
|
|
|
|
|
g_free (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc,
|
|
|
|
char **argv)
|
|
|
|
{
|
|
|
|
GtkWidget *w;
|
2010-11-18 22:31:13 +00:00
|
|
|
gchar *path;
|
2010-11-16 17:04:33 +00:00
|
|
|
|
|
|
|
g_type_init ();
|
|
|
|
gtk_init (&argc, &argv);
|
|
|
|
|
|
|
|
toplevel = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
2010-11-18 22:32:40 +00:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (toplevel), 12);
|
2010-11-16 17:04:33 +00:00
|
|
|
grid = gtk_grid_new ();
|
|
|
|
|
|
|
|
w = gtk_label_new ("File:");
|
|
|
|
gtk_widget_set_halign (w, GTK_ALIGN_START);
|
|
|
|
gtk_grid_attach (GTK_GRID (grid),
|
|
|
|
w, 0, 0, 1, 1);
|
|
|
|
|
2010-11-18 22:31:13 +00:00
|
|
|
file_l = gtk_button_new ();
|
|
|
|
path = g_build_filename (g_get_current_dir (), "apple-red.png", NULL);
|
|
|
|
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,
|
|
|
|
w, GTK_POS_RIGHT, 1, 1);
|
|
|
|
g_signal_connect (file_l, "clicked",
|
|
|
|
G_CALLBACK (button_clicked), NULL);
|
|
|
|
|
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-17 20:38:05 +00:00
|
|
|
"Use content type");
|
2010-11-16 17:04:33 +00:00
|
|
|
|
|
|
|
gtk_grid_attach (GTK_GRID (grid), radio_file,
|
|
|
|
0, 1, 1, 1);
|
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), radio_content,
|
2010-11-17 20:38:05 +00:00
|
|
|
radio_file, GTK_POS_BOTTOM, 1, 1);
|
2010-11-16 17:04:33 +00:00
|
|
|
|
|
|
|
open = gtk_button_new_with_label ("Trigger Open With dialog");
|
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), open,
|
2010-11-17 20:38:05 +00:00
|
|
|
radio_content, GTK_POS_BOTTOM, 1, 1);
|
2010-11-18 22:31:13 +00:00
|
|
|
|
2010-11-16 17:04:33 +00:00
|
|
|
g_signal_connect (open, "clicked",
|
|
|
|
G_CALLBACK (display_dialog), NULL);
|
|
|
|
|
2010-11-17 11:33:59 +00:00
|
|
|
show_mode = gtk_combo_box_text_new ();
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (show_mode), "Recommended only");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (show_mode), "All applications");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (show_mode), "Headings");
|
|
|
|
gtk_grid_attach_next_to (GTK_GRID (grid), show_mode,
|
2010-11-16 17:04:33 +00:00
|
|
|
open, GTK_POS_BOTTOM, 1, 1);
|
2010-11-17 11:33:59 +00:00
|
|
|
gtk_combo_box_set_active (GTK_COMBO_BOX (show_mode), 2);
|
|
|
|
g_signal_connect (show_mode, "changed",
|
|
|
|
G_CALLBACK (show_mode_changed), NULL);
|
2010-11-16 17:04:33 +00:00
|
|
|
|
|
|
|
gtk_container_add (GTK_CONTAINER (toplevel), grid);
|
|
|
|
|
|
|
|
gtk_widget_show_all (toplevel);
|
|
|
|
g_signal_connect (toplevel, "delete-event",
|
|
|
|
G_CALLBACK (gtk_main_quit), NULL);
|
2010-11-16 12:47:51 +00:00
|
|
|
|
|
|
|
gtk_main ();
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|