Commit 47d77924 authored by Sirbu Lavinia Stefania's avatar Sirbu Lavinia Stefania Committed by Ernestas Kulik

mime-application-chooser: port to G_DECLARE* type

Currently we are using the old GObject class declarations, which have two
problems.

One problem is that we cannot use smart pointers like g_autoptr. The other
problem is the boilerplate code generated that makes the code less readable,
so harder to understand.

To fix this use G_DECLARE* type.

https://bugzilla.gnome.org/show_bug.cgi?id=771928
parent a1b358be
...@@ -36,8 +36,10 @@ ...@@ -36,8 +36,10 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <gio/gio.h> #include <gio/gio.h>
struct _NautilusMimeApplicationChooserDetails struct _NautilusMimeApplicationChooser
{ {
GtkBox parent_instance;
GList *files; GList *files;
char *content_type; char *content_type;
...@@ -46,7 +48,7 @@ struct _NautilusMimeApplicationChooserDetails ...@@ -46,7 +48,7 @@ struct _NautilusMimeApplicationChooserDetails
GtkWidget *entry; GtkWidget *entry;
GtkWidget *set_as_default_button; GtkWidget *set_as_default_button;
GtkWidget *open_with_widget; GtkWidget *open_with_widget;
GtkWidget *add_button; GtkWidget *add_button;
}; };
enum enum
...@@ -69,14 +71,14 @@ add_clicked_cb (GtkButton *button, ...@@ -69,14 +71,14 @@ add_clicked_cb (GtkButton *button,
gchar *message; gchar *message;
GError *error = NULL; GError *error = NULL;
info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (chooser->details->open_with_widget)); info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (chooser->open_with_widget));
if (info == NULL) if (info == NULL)
{ {
return; return;
} }
g_app_info_set_as_last_used_for_type (info, chooser->details->content_type, &error); g_app_info_set_as_last_used_for_type (info, chooser->content_type, &error);
if (error != NULL) if (error != NULL)
{ {
...@@ -90,7 +92,7 @@ add_clicked_cb (GtkButton *button, ...@@ -90,7 +92,7 @@ add_clicked_cb (GtkButton *button,
} }
else else
{ {
gtk_app_chooser_refresh (GTK_APP_CHOOSER (chooser->details->open_with_widget)); gtk_app_chooser_refresh (GTK_APP_CHOOSER (chooser->open_with_widget));
g_signal_emit_by_name (nautilus_signaller_get_current (), "mime-data-changed"); g_signal_emit_by_name (nautilus_signaller_get_current (), "mime-data-changed");
} }
...@@ -105,13 +107,13 @@ remove_clicked_cb (GtkMenuItem *item, ...@@ -105,13 +107,13 @@ remove_clicked_cb (GtkMenuItem *item,
GError *error; GError *error;
GAppInfo *info; GAppInfo *info;
info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (chooser->details->open_with_widget)); info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (chooser->open_with_widget));
if (info) if (info)
{ {
error = NULL; error = NULL;
if (!g_app_info_remove_supports_type (info, if (!g_app_info_remove_supports_type (info,
chooser->details->content_type, chooser->content_type,
&error)) &error))
{ {
eel_show_error_dialog (_("Could not forget association"), eel_show_error_dialog (_("Could not forget association"),
...@@ -120,7 +122,7 @@ remove_clicked_cb (GtkMenuItem *item, ...@@ -120,7 +122,7 @@ remove_clicked_cb (GtkMenuItem *item,
g_error_free (error); g_error_free (error);
} }
gtk_app_chooser_refresh (GTK_APP_CHOOSER (chooser->details->open_with_widget)); gtk_app_chooser_refresh (GTK_APP_CHOOSER (chooser->open_with_widget));
g_object_unref (info); g_object_unref (info);
} }
...@@ -155,8 +157,8 @@ reset_clicked_cb (GtkButton *button, ...@@ -155,8 +157,8 @@ reset_clicked_cb (GtkButton *button,
chooser = NAUTILUS_MIME_APPLICATION_CHOOSER (user_data); chooser = NAUTILUS_MIME_APPLICATION_CHOOSER (user_data);
g_app_info_reset_type_associations (chooser->details->content_type); g_app_info_reset_type_associations (chooser->content_type);
gtk_app_chooser_refresh (GTK_APP_CHOOSER (chooser->details->open_with_widget)); gtk_app_chooser_refresh (GTK_APP_CHOOSER (chooser->open_with_widget));
g_signal_emit_by_name (nautilus_signaller_get_current (), "mime-data-changed"); g_signal_emit_by_name (nautilus_signaller_get_current (), "mime-data-changed");
} }
...@@ -170,9 +172,9 @@ set_as_default_clicked_cb (GtkButton *button, ...@@ -170,9 +172,9 @@ set_as_default_clicked_cb (GtkButton *button,
GError *error = NULL; GError *error = NULL;
gchar *message = NULL; gchar *message = NULL;
info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (chooser->details->open_with_widget)); info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (chooser->open_with_widget));
g_app_info_set_as_default_for_type (info, chooser->details->content_type, g_app_info_set_as_default_for_type (info, chooser->content_type,
&error); &error);
if (error != NULL) if (error != NULL)
...@@ -186,7 +188,7 @@ set_as_default_clicked_cb (GtkButton *button, ...@@ -186,7 +188,7 @@ set_as_default_clicked_cb (GtkButton *button,
g_object_unref (info); g_object_unref (info);
gtk_app_chooser_refresh (GTK_APP_CHOOSER (chooser->details->open_with_widget)); gtk_app_chooser_refresh (GTK_APP_CHOOSER (chooser->open_with_widget));
g_signal_emit_by_name (nautilus_signaller_get_current (), "mime-data-changed"); g_signal_emit_by_name (nautilus_signaller_get_current (), "mime-data-changed");
} }
...@@ -234,15 +236,15 @@ application_selected_cb (GtkAppChooserWidget *widget, ...@@ -234,15 +236,15 @@ application_selected_cb (GtkAppChooserWidget *widget,
NautilusMimeApplicationChooser *chooser = user_data; NautilusMimeApplicationChooser *chooser = user_data;
GAppInfo *default_app; GAppInfo *default_app;
default_app = g_app_info_get_default_for_type (chooser->details->content_type, FALSE); default_app = g_app_info_get_default_for_type (chooser->content_type, FALSE);
if (default_app != NULL) if (default_app != NULL)
{ {
gtk_widget_set_sensitive (chooser->details->set_as_default_button, gtk_widget_set_sensitive (chooser->set_as_default_button,
!g_app_info_equal (info, default_app)); !g_app_info_equal (info, default_app));
g_object_unref (default_app); g_object_unref (default_app);
} }
gtk_widget_set_sensitive (chooser->details->add_button, gtk_widget_set_sensitive (chooser->add_button,
app_info_can_add (info, chooser->details->content_type)); app_info_can_add (info, chooser->content_type));
} }
static void static void
...@@ -252,11 +254,11 @@ nautilus_mime_application_chooser_apply_labels (NautilusMimeApplicationChooser * ...@@ -252,11 +254,11 @@ nautilus_mime_application_chooser_apply_labels (NautilusMimeApplicationChooser *
gint num_files; gint num_files;
NautilusFile *file; NautilusFile *file;
num_files = g_list_length (chooser->details->files); num_files = g_list_length (chooser->files);
file = chooser->details->files->data; file = chooser->files->data;
/* here we assume all files are of the same content type */ /* here we assume all files are of the same content type */
if (g_content_type_is_unknown (chooser->details->content_type)) if (g_content_type_is_unknown (chooser->content_type))
{ {
extension = nautilus_file_get_extension (file); extension = nautilus_file_get_extension (file);
...@@ -265,7 +267,7 @@ nautilus_mime_application_chooser_apply_labels (NautilusMimeApplicationChooser * ...@@ -265,7 +267,7 @@ nautilus_mime_application_chooser_apply_labels (NautilusMimeApplicationChooser *
} }
else else
{ {
description = g_content_type_get_description (chooser->details->content_type); description = g_content_type_get_description (chooser->content_type);
} }
if (num_files > 1) if (num_files > 1)
...@@ -286,7 +288,7 @@ nautilus_mime_application_chooser_apply_labels (NautilusMimeApplicationChooser * ...@@ -286,7 +288,7 @@ nautilus_mime_application_chooser_apply_labels (NautilusMimeApplicationChooser *
g_free (display_name); g_free (display_name);
} }
gtk_label_set_markup (GTK_LABEL (chooser->details->label), label); gtk_label_set_markup (GTK_LABEL (chooser->label), label);
g_free (label); g_free (label);
g_free (extension); g_free (extension);
...@@ -303,27 +305,27 @@ nautilus_mime_application_chooser_build_ui (NautilusMimeApplicationChooser *choo ...@@ -303,27 +305,27 @@ nautilus_mime_application_chooser_build_ui (NautilusMimeApplicationChooser *choo
gtk_box_set_spacing (GTK_BOX (chooser), 0); gtk_box_set_spacing (GTK_BOX (chooser), 0);
gtk_box_set_homogeneous (GTK_BOX (chooser), FALSE); gtk_box_set_homogeneous (GTK_BOX (chooser), FALSE);
chooser->details->label = gtk_label_new (""); chooser->label = gtk_label_new ("");
gtk_label_set_xalign (GTK_LABEL (chooser->details->label), 0); gtk_label_set_xalign (GTK_LABEL (chooser->label), 0);
gtk_label_set_line_wrap (GTK_LABEL (chooser->details->label), TRUE); gtk_label_set_line_wrap (GTK_LABEL (chooser->label), TRUE);
gtk_label_set_line_wrap_mode (GTK_LABEL (chooser->details->label), gtk_label_set_line_wrap_mode (GTK_LABEL (chooser->label),
PANGO_WRAP_WORD_CHAR); PANGO_WRAP_WORD_CHAR);
gtk_label_set_max_width_chars (GTK_LABEL (chooser->details->label), 60); gtk_label_set_max_width_chars (GTK_LABEL (chooser->label), 60);
gtk_box_pack_start (GTK_BOX (chooser), chooser->details->label, gtk_box_pack_start (GTK_BOX (chooser), chooser->label,
FALSE, FALSE, 0); FALSE, FALSE, 0);
gtk_widget_show (chooser->details->label); gtk_widget_show (chooser->label);
chooser->details->open_with_widget = gtk_app_chooser_widget_new (chooser->details->content_type); chooser->open_with_widget = gtk_app_chooser_widget_new (chooser->content_type);
gtk_app_chooser_widget_set_show_default (GTK_APP_CHOOSER_WIDGET (chooser->details->open_with_widget), gtk_app_chooser_widget_set_show_default (GTK_APP_CHOOSER_WIDGET (chooser->open_with_widget),
TRUE); TRUE);
gtk_app_chooser_widget_set_show_fallback (GTK_APP_CHOOSER_WIDGET (chooser->details->open_with_widget), gtk_app_chooser_widget_set_show_fallback (GTK_APP_CHOOSER_WIDGET (chooser->open_with_widget),
TRUE); TRUE);
gtk_app_chooser_widget_set_show_other (GTK_APP_CHOOSER_WIDGET (chooser->details->open_with_widget), gtk_app_chooser_widget_set_show_other (GTK_APP_CHOOSER_WIDGET (chooser->open_with_widget),
TRUE); TRUE);
gtk_box_pack_start (GTK_BOX (chooser), chooser->details->open_with_widget, gtk_box_pack_start (GTK_BOX (chooser), chooser->open_with_widget,
TRUE, TRUE, 6); TRUE, TRUE, 6);
gtk_widget_show (chooser->details->open_with_widget); gtk_widget_show (chooser->open_with_widget);
box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL); box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
gtk_box_set_spacing (GTK_BOX (box), 6); gtk_box_set_spacing (GTK_BOX (box), 6);
...@@ -345,7 +347,7 @@ nautilus_mime_application_chooser_build_ui (NautilusMimeApplicationChooser *choo ...@@ -345,7 +347,7 @@ nautilus_mime_application_chooser_build_ui (NautilusMimeApplicationChooser *choo
chooser); chooser);
gtk_widget_show (button); gtk_widget_show (button);
gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
chooser->details->add_button = button; chooser->add_button = button;
button = gtk_button_new_with_label (_("Set as default")); button = gtk_button_new_with_label (_("Set as default"));
g_signal_connect (button, "clicked", g_signal_connect (button, "clicked",
...@@ -354,22 +356,22 @@ nautilus_mime_application_chooser_build_ui (NautilusMimeApplicationChooser *choo ...@@ -354,22 +356,22 @@ nautilus_mime_application_chooser_build_ui (NautilusMimeApplicationChooser *choo
gtk_widget_show (button); gtk_widget_show (button);
gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
chooser->details->set_as_default_button = button; chooser->set_as_default_button = button;
/* initialize sensitivity */ /* initialize sensitivity */
info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (chooser->details->open_with_widget)); info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (chooser->open_with_widget));
if (info != NULL) if (info != NULL)
{ {
application_selected_cb (GTK_APP_CHOOSER_WIDGET (chooser->details->open_with_widget), application_selected_cb (GTK_APP_CHOOSER_WIDGET (chooser->open_with_widget),
info, chooser); info, chooser);
g_object_unref (info); g_object_unref (info);
} }
g_signal_connect (chooser->details->open_with_widget, g_signal_connect (chooser->open_with_widget,
"application-selected", "application-selected",
G_CALLBACK (application_selected_cb), G_CALLBACK (application_selected_cb),
chooser); chooser);
g_signal_connect (chooser->details->open_with_widget, g_signal_connect (chooser->open_with_widget,
"populate-popup", "populate-popup",
G_CALLBACK (populate_popup_cb), G_CALLBACK (populate_popup_cb),
chooser); chooser);
...@@ -378,9 +380,6 @@ nautilus_mime_application_chooser_build_ui (NautilusMimeApplicationChooser *choo ...@@ -378,9 +380,6 @@ nautilus_mime_application_chooser_build_ui (NautilusMimeApplicationChooser *choo
static void static void
nautilus_mime_application_chooser_init (NautilusMimeApplicationChooser *chooser) nautilus_mime_application_chooser_init (NautilusMimeApplicationChooser *chooser)
{ {
chooser->details = G_TYPE_INSTANCE_GET_PRIVATE (chooser, NAUTILUS_TYPE_MIME_APPLICATION_CHOOSER,
NautilusMimeApplicationChooserDetails);
gtk_orientable_set_orientation (GTK_ORIENTABLE (chooser), gtk_orientable_set_orientation (GTK_ORIENTABLE (chooser),
GTK_ORIENTATION_VERTICAL); GTK_ORIENTATION_VERTICAL);
} }
...@@ -406,8 +405,8 @@ nautilus_mime_application_chooser_finalize (GObject *object) ...@@ -406,8 +405,8 @@ nautilus_mime_application_chooser_finalize (GObject *object)
chooser = NAUTILUS_MIME_APPLICATION_CHOOSER (object); chooser = NAUTILUS_MIME_APPLICATION_CHOOSER (object);
g_free (chooser->details->content_type); g_free (chooser->content_type);
nautilus_file_list_free (chooser->details->files); nautilus_file_list_free (chooser->files);
G_OBJECT_CLASS (nautilus_mime_application_chooser_parent_class)->finalize (object); G_OBJECT_CLASS (nautilus_mime_application_chooser_parent_class)->finalize (object);
} }
...@@ -424,7 +423,7 @@ nautilus_mime_application_chooser_get_property (GObject *object, ...@@ -424,7 +423,7 @@ nautilus_mime_application_chooser_get_property (GObject *object,
{ {
case PROP_CONTENT_TYPE: case PROP_CONTENT_TYPE:
{ {
g_value_set_string (value, chooser->details->content_type); g_value_set_string (value, chooser->content_type);
} }
break; break;
...@@ -448,13 +447,13 @@ nautilus_mime_application_chooser_set_property (GObject *object, ...@@ -448,13 +447,13 @@ nautilus_mime_application_chooser_set_property (GObject *object,
{ {
case PROP_CONTENT_TYPE: case PROP_CONTENT_TYPE:
{ {
chooser->details->content_type = g_value_dup_string (value); chooser->content_type = g_value_dup_string (value);
} }
break; break;
case PROP_FILES: case PROP_FILES:
{ {
chooser->details->files = nautilus_file_list_copy (g_value_get_pointer (value)); chooser->files = nautilus_file_list_copy (g_value_get_pointer (value));
} }
break; break;
...@@ -490,8 +489,6 @@ nautilus_mime_application_chooser_class_init (NautilusMimeApplicationChooserClas ...@@ -490,8 +489,6 @@ nautilus_mime_application_chooser_class_init (NautilusMimeApplicationChooserClas
G_PARAM_STATIC_STRINGS); G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties); g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
g_type_class_add_private (class, sizeof (NautilusMimeApplicationChooserDetails));
} }
GtkWidget * GtkWidget *
......
...@@ -26,26 +26,15 @@ ...@@ -26,26 +26,15 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#define NAUTILUS_TYPE_MIME_APPLICATION_CHOOSER (nautilus_mime_application_chooser_get_type ()) G_BEGIN_DECLS
#define NAUTILUS_MIME_APPLICATION_CHOOSER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NAUTILUS_TYPE_MIME_APPLICATION_CHOOSER, NautilusMimeApplicationChooser))
#define NAUTILUS_MIME_APPLICATION_CHOOSER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NAUTILUS_TYPE_MIME_APPLICATION_CHOOSER, NautilusMimeApplicationChooserClass))
#define NAUTILUS_IS_MIME_APPLICATION_CHOOSER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NAUTILUS_TYPE_MIME_APPLICATION_CHOOSER)
typedef struct _NautilusMimeApplicationChooser NautilusMimeApplicationChooser; #define NAUTILUS_TYPE_MIME_APPLICATION_CHOOSER (nautilus_mime_application_chooser_get_type ())
typedef struct _NautilusMimeApplicationChooserClass NautilusMimeApplicationChooserClass;
typedef struct _NautilusMimeApplicationChooserDetails NautilusMimeApplicationChooserDetails;
struct _NautilusMimeApplicationChooser { G_DECLARE_FINAL_TYPE (NautilusMimeApplicationChooser, nautilus_mime_application_chooser, NAUTILUS, MIME_APPLICATION_CHOOSER, GtkBox)
GtkBox parent;
NautilusMimeApplicationChooserDetails *details;
};
struct _NautilusMimeApplicationChooserClass {
GtkBoxClass parent_class;
};
GType nautilus_mime_application_chooser_get_type (void);
GtkWidget * nautilus_mime_application_chooser_new (GList *files, GtkWidget * nautilus_mime_application_chooser_new (GList *files,
const char *mime_type); const char *mime_type);
G_END_DECLS
#endif /* NAUTILUS_MIME_APPLICATION_CHOOSER_H */ #endif /* NAUTILUS_MIME_APPLICATION_CHOOSER_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment