]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkappchooserdialog.c
GtkWidget::draw() - Document how to get the dirty region
[~andy/gtk] / gtk / gtkappchooserdialog.c
index 181515d7ec5135e72ad082112c12aba0ad6f898f..b63f0df72a9fd3001da45020bd6ec2257ca76c6a 100644 (file)
@@ -15,9 +15,7 @@
  * 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.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  *
  * Authors: Dave Camp <dave@novell.com>
  *          Alexander Larsson <alexl@redhat.com>
@@ -35,6 +33,9 @@
  * of its own. Instead, you should get the embedded #GtkAppChooserWidget
  * using gtk_app_chooser_dialog_get_widget() and call its methods if
  * the generic #GtkAppChooser interface is not sufficient for your needs.
+ *
+ * To set the heading that is shown above the #GtkAppChooserWidget,
+ * use gtk_app_chooser_dialog_set_heading().
  */
 #include "config.h"
 
@@ -74,8 +75,10 @@ struct _GtkAppChooserDialogPrivate {
   GtkWidget *show_more_button;
 
   GtkAppChooserOnline *online;
+  GCancellable *online_cancellable;
 
   gboolean show_more_clicked;
+  gboolean dismissed;
 };
 
 enum {
@@ -121,18 +124,31 @@ search_for_mimetype_ready_cb (GObject      *source,
   GtkAppChooserDialog *self = user_data;
   GError *error = NULL;
 
+  gdk_threads_enter ();
+
   _gtk_app_chooser_online_search_for_mimetype_finish (online, res, &error);
 
-  if (error != NULL)
+  if (self->priv->dismissed)
+    goto out;
+
+  if (error != NULL &&
+      !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
     {
       show_error_dialog (_("Failed to look for applications online"),
                          error->message, GTK_WINDOW (self));
-      g_error_free (error);
     }
   else
     {
+      gtk_widget_set_sensitive (self->priv->online_button, TRUE);
       gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
     }
+
+ out:
+  g_clear_object (&self->priv->online_cancellable);
+  g_clear_error (&error);
+  g_object_unref (self);
+
+  gdk_threads_leave ();
 }
 
 static void
@@ -141,11 +157,15 @@ online_button_clicked_cb (GtkButton *b,
 {
   GtkAppChooserDialog *self = user_data;
 
+  self->priv->online_cancellable = g_cancellable_new ();
+  gtk_widget_set_sensitive (self->priv->online_button, FALSE);
+
   _gtk_app_chooser_online_search_for_mimetype_async (self->priv->online,
-                                                     self->priv->content_type,
-                                                     GTK_WINDOW (self),
-                                                     search_for_mimetype_ready_cb,
-                                                     self);
+                                                    self->priv->content_type,
+                                                    GTK_WINDOW (self),
+                                                     self->priv->online_cancellable,
+                                                    search_for_mimetype_ready_cb,
+                                                    g_object_ref (self));
 }
 
 static void
@@ -155,14 +175,17 @@ app_chooser_online_get_default_ready_cb (GObject *source,
 {
   GtkAppChooserDialog *self = user_data;
 
+  gdk_threads_enter ();
+
   self->priv->online = _gtk_app_chooser_online_get_default_finish (source, res);
 
-  if (self->priv->online != NULL)
+  if (self->priv->online != NULL &&
+      !self->priv->dismissed)
     {
       GtkWidget *action_area;
 
       action_area = gtk_dialog_get_action_area (GTK_DIALOG (self));
-      self->priv->online_button = gtk_button_new_with_label (_("Find applications online"));
+      self->priv->online_button = gtk_button_new_with_mnemonic (_("_Find applications online"));
       gtk_box_pack_start (GTK_BOX (action_area), self->priv->online_button,
                           FALSE, FALSE, 0);
       gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (action_area), self->priv->online_button,
@@ -170,14 +193,23 @@ app_chooser_online_get_default_ready_cb (GObject *source,
       g_signal_connect (self->priv->online_button, "clicked",
                         G_CALLBACK (online_button_clicked_cb), self);
 
+
+      if (!self->priv->content_type)
+       gtk_widget_set_sensitive (self->priv->online_button, FALSE);
+
       gtk_widget_show (self->priv->online_button);
     }
+
+  g_object_unref (self);
+
+  gdk_threads_leave ();
 }
 
 static void
 ensure_online_button (GtkAppChooserDialog *self)
 {
-  _gtk_app_chooser_online_get_default_async (app_chooser_online_get_default_ready_cb, self);
+  _gtk_app_chooser_online_get_default_async (app_chooser_online_get_default_ready_cb,
+                                             g_object_ref (self));
 }
 
 /* An application is valid if:
@@ -200,6 +232,12 @@ check_application (GtkAppChooserDialog  *self,
   command = NULL;
 
   info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
+  if (info == NULL)
+    {
+      *app_out = NULL;
+      return FALSE;
+    }
+
   command = g_app_info_get_executable (info);
 
   g_shell_parse_argv (command, &argc, &argv, &error);
@@ -246,12 +284,25 @@ add_or_find_application (GtkAppChooserDialog *self)
 
   app = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self));
 
-  /* we don't care about reporting errors here */
-  g_app_info_set_as_last_used_for_type (app,
-                                        self->priv->content_type,
-                                        NULL);
+  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);
+    }
+}
 
-  g_object_unref (app);
+static void
+cancel_and_clear_cancellable (GtkAppChooserDialog *self)
+{                                                               
+  if (self->priv->online_cancellable != NULL)
+    {
+      g_cancellable_cancel (self->priv->online_cancellable);
+      g_clear_object (&self->priv->online_cancellable);
+    }
 }
 
 static void
@@ -266,6 +317,10 @@ gtk_app_chooser_dialog_response (GtkDialog *dialog,
     case GTK_RESPONSE_OK:
       add_or_find_application (self);
       break;
+    case GTK_RESPONSE_CANCEL:
+    case GTK_RESPONSE_DELETE_EVENT:
+      cancel_and_clear_cancellable (self);
+      self->priv->dismissed = TRUE;
     default :
       break;
     }
@@ -313,12 +368,14 @@ set_dialog_properties (GtkAppChooserDialog *self)
   gchar *description;
   gchar *default_text;
   gchar *string;
+  gboolean unknown;
   PangoFontDescription *font_desc;
 
   name = NULL;
   extension = NULL;
   label = NULL;
   description = NULL;
+  unknown = TRUE;
 
   if (self->priv->gfile != NULL)
     {
@@ -326,7 +383,12 @@ set_dialog_properties (GtkAppChooserDialog *self)
       extension = get_extension (name);
     }
 
-  description = g_content_type_get_description (self->priv->content_type);
+  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);
+    }
+
   gtk_window_set_title (GTK_WINDOW (self), "");
 
   if (name != NULL)
@@ -340,11 +402,9 @@ set_dialog_properties (GtkAppChooserDialog *self)
     {
       /* Translators: %s is a file type description */
       label = g_strdup_printf (_("Select an application for \"%s\" files"),
-                               g_content_type_is_unknown (self->priv->content_type) ?
-                               self->priv->content_type : description);
+                               unknown ? self->priv->content_type : description);
       string = g_strdup_printf (_("No applications available to open \"%s\" files"),
-                               g_content_type_is_unknown (self->priv->content_type) ?
-                               self->priv->content_type : description);
+                               unknown ? self->priv->content_type : description);
     }
 
   font_desc = pango_font_description_new ();
@@ -459,8 +519,8 @@ build_dialog_ui (GtkAppChooserDialog *self)
 {
   GtkWidget *vbox;
   GtkWidget *vbox2;
-  GtkWidget *label;
   GtkWidget *button, *w;
+  GAppInfo *info;
 
   gtk_container_set_border_width (GTK_CONTAINER (self), 5);
 
@@ -474,7 +534,8 @@ build_dialog_ui (GtkAppChooserDialog *self)
   gtk_widget_show (vbox2);
 
   self->priv->label = gtk_label_new ("");
-  gtk_misc_set_alignment (GTK_MISC (self->priv->label), 0, 0.5);
+  gtk_widget_set_halign (self->priv->label, GTK_ALIGN_START);
+  gtk_widget_set_valign (self->priv->label, GTK_ALIGN_CENTER);
   gtk_label_set_line_wrap (GTK_LABEL (self->priv->label), TRUE);
   gtk_box_pack_start (GTK_BOX (vbox2), self->priv->label,
                       FALSE, FALSE, 0);
@@ -509,23 +570,14 @@ build_dialog_ui (GtkAppChooserDialog *self)
                          GTK_STOCK_CANCEL,
                          GTK_RESPONSE_CANCEL);
 
-  /* Create a custom stock icon */
-  self->priv->button = gtk_button_new ();
-
-  label = gtk_label_new_with_mnemonic (_("_Select"));
-  gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (self->priv->button));
-  gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
-  gtk_widget_show (label);
-  self->priv->open_label = label;
-
-  gtk_container_add (GTK_CONTAINER (self->priv->button),
-                     self->priv->open_label);
+  self->priv->button = gtk_dialog_add_button (GTK_DIALOG (self),
+                                              _("_Select"),
+                                              GTK_RESPONSE_OK);
 
-  gtk_widget_show (self->priv->button);
-  gtk_widget_set_can_default (self->priv->button, TRUE);
-
-  gtk_dialog_add_action_widget (GTK_DIALOG (self),
-                                self->priv->button, GTK_RESPONSE_OK);
+  info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
+  gtk_widget_set_sensitive (self->priv->button, info != NULL);
+  if (info)
+    g_object_unref (info);
 
   gtk_dialog_set_default_response (GTK_DIALOG (self),
                                    GTK_RESPONSE_OK);
@@ -575,9 +627,6 @@ gtk_app_chooser_dialog_constructed (GObject *object)
 {
   GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
 
-  g_assert (self->priv->content_type != NULL ||
-            self->priv->gfile != NULL);
-
   if (G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed != NULL)
     G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed (object);
 
@@ -592,6 +641,7 @@ gtk_app_chooser_dialog_dispose (GObject *object)
   GtkAppChooserDialog *self = GTK_APP_CHOOSER_DIALOG (object);
   
   g_clear_object (&self->priv->gfile);
+  cancel_and_clear_cancellable (self);
   g_clear_object (&self->priv->online);
 
   G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->dispose (object);