[gnome-disk-utility/new-ui] Factor common bits of GduErrorDialog into a new abstract GduDialog class



commit a265c4a5f74940b45bc2d9bf9b11a1102885a4dd
Author: David Zeuthen <davidz redhat com>
Date:   Wed Sep 30 17:42:14 2009 -0400

    Factor common bits of GduErrorDialog into a new abstract GduDialog class
    
    The idea is that the GduDialog class should be used as a base-class
    for dialogs that act on specific devices.

 src/gdu-gtk/Makefile.am        |    2 +
 src/gdu-gtk/gdu-dialog.c       |  193 ++++++++++++++++++++++++++++++++++++++++
 src/gdu-gtk/gdu-dialog.h       |   62 +++++++++++++
 src/gdu-gtk/gdu-error-dialog.c |   79 ++---------------
 src/gdu-gtk/gdu-error-dialog.h |    7 +-
 src/gdu-gtk/gdu-gtk-types.h    |    2 +
 src/gdu-gtk/gdu-gtk.h          |    1 +
 7 files changed, 270 insertions(+), 76 deletions(-)
---
diff --git a/src/gdu-gtk/Makefile.am b/src/gdu-gtk/Makefile.am
index 8e804f7..716e7be 100644
--- a/src/gdu-gtk/Makefile.am
+++ b/src/gdu-gtk/Makefile.am
@@ -43,6 +43,7 @@ libgdu_gtkinclude_HEADERS =              				\
 	gdu-error-dialog.h						\
 	gdu-button-element.h						\
 	gdu-button-table.h						\
+	gdu-dialog.h							\
 	$(NULL)
 
 libgdu_gtk_la_SOURCES =                 	               				\
@@ -63,6 +64,7 @@ libgdu_gtk_la_SOURCES =                 	               				\
 	gdu-error-dialog.h			gdu-error-dialog.c			\
 	gdu-button-element.h			gdu-button-element.c			\
 	gdu-button-table.h			gdu-button-table.c			\
+	gdu-dialog.h				gdu-dialog.c				\
 	$(NULL)
 
 libgdu_gtk_la_CPPFLAGS = 				\
diff --git a/src/gdu-gtk/gdu-dialog.c b/src/gdu-gtk/gdu-dialog.c
new file mode 100644
index 0000000..fbe4761
--- /dev/null
+++ b/src/gdu-gtk/gdu-dialog.c
@@ -0,0 +1,193 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/* gdu-dialog.c
+ *
+ * Copyright (C) 2009 David Zeuthen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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 <glib/gi18n.h>
+#include <atasmart.h>
+#include <glib/gstdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "gdu-dialog.h"
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+struct GduDialogPrivate
+{
+        GduPresentable *presentable;
+        GduDevice *device;
+};
+
+enum
+{
+        PROP_0,
+        PROP_PRESENTABLE,
+        PROP_DRIVE_DEVICE,
+        PROP_VOLUME_DEVICE,
+};
+
+G_DEFINE_ABSTRACT_TYPE (GduDialog, gdu_dialog, GTK_TYPE_DIALOG)
+
+static void
+gdu_dialog_finalize (GObject *object)
+{
+        GduDialog *dialog = GDU_DIALOG (object);
+
+        if (dialog->priv->presentable != NULL) {
+                g_object_unref (dialog->priv->presentable);
+        }
+        if (dialog->priv->device != NULL) {
+                g_object_unref (dialog->priv->device);
+        }
+
+        if (G_OBJECT_CLASS (gdu_dialog_parent_class)->finalize != NULL)
+                G_OBJECT_CLASS (gdu_dialog_parent_class)->finalize (object);
+}
+
+static void
+gdu_dialog_get_property (GObject    *object,
+                         guint       property_id,
+                         GValue     *value,
+                         GParamSpec *pspec)
+{
+        GduDialog *dialog = GDU_DIALOG (object);
+
+        switch (property_id) {
+        case PROP_PRESENTABLE:
+                g_value_set_object (value, dialog->priv->presentable);
+                break;
+
+        default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                break;
+        }
+}
+
+static void
+gdu_dialog_set_property (GObject      *object,
+                               guint         property_id,
+                               const GValue *value,
+                               GParamSpec   *pspec)
+{
+        GduDialog *dialog = GDU_DIALOG (object);
+        GduDevice *device;
+        GduPool *pool;
+
+        switch (property_id) {
+        case PROP_PRESENTABLE:
+                if (g_value_get_object (value) != NULL) {
+                        g_warn_if_fail (dialog->priv->presentable == NULL);
+                        dialog->priv->presentable = g_value_dup_object (value);
+                }
+                break;
+
+        case PROP_DRIVE_DEVICE:
+                device = GDU_DEVICE (g_value_get_object (value));
+                if (device != NULL) {
+                        pool = gdu_device_get_pool (device);
+                        g_warn_if_fail (dialog->priv->presentable == NULL);
+                        dialog->priv->presentable = gdu_pool_get_drive_by_device (pool, device);
+                        g_object_unref (pool);
+                }
+                break;
+
+        case PROP_VOLUME_DEVICE:
+                device = GDU_DEVICE (g_value_get_object (value));
+                if (device != NULL) {
+                        pool = gdu_device_get_pool (device);
+                        g_warn_if_fail (dialog->priv->presentable == NULL);
+                        dialog->priv->presentable = gdu_pool_get_volume_by_device (pool, device);
+                        g_object_unref (pool);
+                }
+                break;
+
+        default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                break;
+        }
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+static void
+gdu_dialog_class_init (GduDialogClass *klass)
+{
+        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+        g_type_class_add_private (klass, sizeof (GduDialogPrivate));
+
+        object_class->get_property = gdu_dialog_get_property;
+        object_class->set_property = gdu_dialog_set_property;
+        object_class->finalize     = gdu_dialog_finalize;
+
+        g_object_class_install_property (object_class,
+                                         PROP_PRESENTABLE,
+                                         g_param_spec_object ("presentable",
+                                                              NULL,
+                                                              NULL,
+                                                              GDU_TYPE_PRESENTABLE,
+                                                              G_PARAM_READABLE |
+                                                              G_PARAM_WRITABLE |
+                                                              G_PARAM_CONSTRUCT_ONLY));
+
+        g_object_class_install_property (object_class,
+                                         PROP_DRIVE_DEVICE,
+                                         g_param_spec_object ("drive-device",
+                                                              NULL,
+                                                              NULL,
+                                                              GDU_TYPE_DEVICE,
+                                                              G_PARAM_WRITABLE |
+                                                              G_PARAM_CONSTRUCT_ONLY));
+
+        g_object_class_install_property (object_class,
+                                         PROP_VOLUME_DEVICE,
+                                         g_param_spec_object ("volume-device",
+                                                              NULL,
+                                                              NULL,
+                                                              GDU_TYPE_DEVICE,
+                                                              G_PARAM_WRITABLE |
+                                                              G_PARAM_CONSTRUCT_ONLY));
+}
+
+static void
+gdu_dialog_init (GduDialog *dialog)
+{
+        dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog, GDU_TYPE_DIALOG, GduDialogPrivate);
+}
+
+GduPresentable *
+gdu_dialog_get_presentable (GduDialog *dialog)
+{
+        return dialog->priv->presentable;
+}
+
+GduDevice *
+gdu_dialog_get_device (GduDialog *dialog)
+{
+        if (dialog->priv->device == NULL) {
+                dialog->priv->device = gdu_presentable_get_device (dialog->priv->presentable);
+        }
+        return dialog->priv->device;
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
diff --git a/src/gdu-gtk/gdu-dialog.h b/src/gdu-gtk/gdu-dialog.h
new file mode 100644
index 0000000..61a2d8d
--- /dev/null
+++ b/src/gdu-gtk/gdu-dialog.h
@@ -0,0 +1,62 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/* gdu-dialog.h
+ *
+ * Copyright (C) 2009 David Zeuthen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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.
+ */
+
+#if !defined (__GDU_GTK_INSIDE_GDU_GTK_H) && !defined (GDU_GTK_COMPILATION)
+#error "Only <gdu-gtk/gdu-gtk.h> can be included directly, this file may disappear or change contents."
+#endif
+
+#ifndef __GDU_DIALOG_H
+#define __GDU_DIALOG_H
+
+#include <gdu-gtk/gdu-gtk-types.h>
+
+G_BEGIN_DECLS
+
+#define GDU_TYPE_DIALOG            gdu_dialog_get_type()
+#define GDU_DIALOG(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GDU_TYPE_DIALOG, GduDialog))
+#define GDU_DIALOG_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GDU_TYPE_DIALOG, GduDialogClass))
+#define GDU_IS_DIALOG(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GDU_TYPE_DIALOG))
+#define GDU_IS_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDU_TYPE_DIALOG))
+#define GDU_DIALOG_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GDU_TYPE_DIALOG, GduDialogClass))
+
+typedef struct GduDialogClass   GduDialogClass;
+typedef struct GduDialogPrivate GduDialogPrivate;
+
+struct GduDialog
+{
+        GtkDialog parent;
+
+        /*< private >*/
+        GduDialogPrivate *priv;
+};
+
+struct GduDialogClass
+{
+        GtkDialogClass parent_class;
+};
+
+GType           gdu_dialog_get_type        (void) G_GNUC_CONST;
+GduPresentable *gdu_dialog_get_presentable (GduDialog *dialog);
+GduDevice      *gdu_dialog_get_device      (GduDialog *dialog);
+
+G_END_DECLS
+
+#endif /* __GDU_DIALOG_H */
diff --git a/src/gdu-gtk/gdu-error-dialog.c b/src/gdu-gtk/gdu-error-dialog.c
index 70516eb..04c2cea 100644
--- a/src/gdu-gtk/gdu-error-dialog.c
+++ b/src/gdu-gtk/gdu-error-dialog.c
@@ -1,5 +1,5 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
-/* gdu-ata-smart-dialog.c
+/* gdu-error-dialog.c
  *
  * Copyright (C) 2009 David Zeuthen
  *
@@ -67,7 +67,6 @@ gdu_error_get_type (void)
 
 struct GduErrorDialogPrivate
 {
-        GduPresentable *presentable;
         gchar          *message;
         GError         *error;
 };
@@ -75,24 +74,18 @@ struct GduErrorDialogPrivate
 enum
 {
         PROP_0,
-        PROP_PRESENTABLE,
-        PROP_DRIVE_DEVICE,
-        PROP_VOLUME_DEVICE,
         PROP_MESSAGE,
         PROP_ERROR
 };
 
 
-G_DEFINE_TYPE (GduErrorDialog, gdu_error_dialog, GTK_TYPE_DIALOG)
+G_DEFINE_TYPE (GduErrorDialog, gdu_error_dialog, GDU_TYPE_DIALOG)
 
 static void
 gdu_error_dialog_finalize (GObject *object)
 {
         GduErrorDialog *dialog = GDU_ERROR_DIALOG (object);
 
-        if (dialog->priv->presentable != NULL) {
-                g_object_unref (dialog->priv->presentable);
-        }
         g_free (dialog->priv->message);
         g_error_free (dialog->priv->error);
 
@@ -109,10 +102,6 @@ gdu_error_dialog_get_property (GObject    *object,
         GduErrorDialog *dialog = GDU_ERROR_DIALOG (object);
 
         switch (property_id) {
-        case PROP_PRESENTABLE:
-                g_value_set_object (value, dialog->priv->presentable);
-                break;
-
         case PROP_MESSAGE:
                 g_value_set_string (value, dialog->priv->message);
                 break;
@@ -134,37 +123,8 @@ gdu_error_dialog_set_property (GObject      *object,
                                GParamSpec   *pspec)
 {
         GduErrorDialog *dialog = GDU_ERROR_DIALOG (object);
-        GduDevice *device;
-        GduPool *pool;
 
         switch (property_id) {
-        case PROP_PRESENTABLE:
-                if (g_value_get_object (value) != NULL) {
-                        g_warn_if_fail (dialog->priv->presentable == NULL);
-                        dialog->priv->presentable = g_value_dup_object (value);
-                }
-                break;
-
-        case PROP_DRIVE_DEVICE:
-                device = GDU_DEVICE (g_value_get_object (value));
-                if (device != NULL) {
-                        pool = gdu_device_get_pool (device);
-                        g_warn_if_fail (dialog->priv->presentable == NULL);
-                        dialog->priv->presentable = gdu_pool_get_drive_by_device (pool, device);
-                        g_object_unref (pool);
-                }
-                break;
-
-        case PROP_VOLUME_DEVICE:
-                device = GDU_DEVICE (g_value_get_object (value));
-                if (device != NULL) {
-                        pool = gdu_device_get_pool (device);
-                        g_warn_if_fail (dialog->priv->presentable == NULL);
-                        dialog->priv->presentable = gdu_pool_get_volume_by_device (pool, device);
-                        g_object_unref (pool);
-                }
-                break;
-
         case PROP_MESSAGE:
                 dialog->priv->message = g_value_dup_string (value);
                 break;
@@ -175,6 +135,7 @@ gdu_error_dialog_set_property (GObject      *object,
 
         default:
                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                break;
         }
 }
 
@@ -212,7 +173,7 @@ gdu_error_dialog_constructed (GObject *object)
 
         content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
 
-        icon = gdu_presentable_get_icon (dialog->priv->presentable);
+        icon = gdu_presentable_get_icon (gdu_dialog_get_presentable (GDU_DIALOG (dialog)));
         error_icon = g_themed_icon_new (GTK_STOCK_DIALOG_ERROR);
         emblem = g_emblem_new (icon);
         emblemed_icon = g_emblemed_icon_new (error_icon,
@@ -269,8 +230,8 @@ gdu_error_dialog_constructed (GObject *object)
         if (error_msg == NULL)
                 error_msg = _("Unknown error");
 
-        name = gdu_presentable_get_name (dialog->priv->presentable);
-        vpd_name = gdu_presentable_get_vpd_name (dialog->priv->presentable);
+        name = gdu_presentable_get_name (gdu_dialog_get_presentable (GDU_DIALOG (dialog)));
+        vpd_name = gdu_presentable_get_vpd_name (gdu_dialog_get_presentable (GDU_DIALOG (dialog)));
         s = g_strdup_printf (_("An error occured while performing an operation "
                                "on \"%s\" (%s): %s"),
                              name,
@@ -308,34 +269,6 @@ gdu_error_dialog_class_init (GduErrorDialogClass *klass)
         object_class->finalize     = gdu_error_dialog_finalize;
 
         g_object_class_install_property (object_class,
-                                         PROP_PRESENTABLE,
-                                         g_param_spec_object ("presentable",
-                                                              NULL,
-                                                              NULL,
-                                                              GDU_TYPE_PRESENTABLE,
-                                                              G_PARAM_READABLE |
-                                                              G_PARAM_WRITABLE |
-                                                              G_PARAM_CONSTRUCT_ONLY));
-
-        g_object_class_install_property (object_class,
-                                         PROP_DRIVE_DEVICE,
-                                         g_param_spec_object ("drive-device",
-                                                              NULL,
-                                                              NULL,
-                                                              GDU_TYPE_DEVICE,
-                                                              G_PARAM_WRITABLE |
-                                                              G_PARAM_CONSTRUCT_ONLY));
-
-        g_object_class_install_property (object_class,
-                                         PROP_VOLUME_DEVICE,
-                                         g_param_spec_object ("volume-device",
-                                                              NULL,
-                                                              NULL,
-                                                              GDU_TYPE_DEVICE,
-                                                              G_PARAM_WRITABLE |
-                                                              G_PARAM_CONSTRUCT_ONLY));
-
-        g_object_class_install_property (object_class,
                                          PROP_MESSAGE,
                                          g_param_spec_string ("message",
                                                               NULL,
diff --git a/src/gdu-gtk/gdu-error-dialog.h b/src/gdu-gtk/gdu-error-dialog.h
index 77494f5..06b812c 100644
--- a/src/gdu-gtk/gdu-error-dialog.h
+++ b/src/gdu-gtk/gdu-error-dialog.h
@@ -1,5 +1,5 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
-/* gdu-ata-smart-dialog.h
+/* gdu-error-dialog.h
  *
  * Copyright (C) 2009 David Zeuthen
  *
@@ -27,6 +27,7 @@
 #define __GDU_ERROR_DIALOG_H
 
 #include <gdu-gtk/gdu-gtk-types.h>
+#include <gdu-gtk/gdu-dialog.h>
 
 G_BEGIN_DECLS
 
@@ -42,7 +43,7 @@ typedef struct GduErrorDialogPrivate GduErrorDialogPrivate;
 
 struct GduErrorDialog
 {
-        GtkDialog parent;
+        GduDialog parent;
 
         /*< private >*/
         GduErrorDialogPrivate *priv;
@@ -50,7 +51,7 @@ struct GduErrorDialog
 
 struct GduErrorDialogClass
 {
-        GtkDialogClass parent_class;
+        GduDialogClass parent_class;
 };
 
 GType       gdu_error_dialog_get_type (void) G_GNUC_CONST;
diff --git a/src/gdu-gtk/gdu-gtk-types.h b/src/gdu-gtk/gdu-gtk-types.h
index 843f0ce..d7d7109 100644
--- a/src/gdu-gtk/gdu-gtk-types.h
+++ b/src/gdu-gtk/gdu-gtk-types.h
@@ -50,6 +50,8 @@ typedef struct GduDetailsElement           GduDetailsElement;
 typedef struct GduErrorDialog              GduErrorDialog;
 typedef struct GduButtonElement            GduButtonElement;
 typedef struct GduButtonTable              GduButtonTable;
+typedef struct GduDialog                   GduDialog;
+
 
 G_END_DECLS
 
diff --git a/src/gdu-gtk/gdu-gtk.h b/src/gdu-gtk/gdu-gtk.h
index c5be964..ca38654 100644
--- a/src/gdu-gtk/gdu-gtk.h
+++ b/src/gdu-gtk/gdu-gtk.h
@@ -42,6 +42,7 @@
 #include <gdu-gtk/gdu-error-dialog.h>
 #include <gdu-gtk/gdu-button-element.h>
 #include <gdu-gtk/gdu-button-table.h>
+#include <gdu-gtk/gdu-dialog.h>
 #undef __GDU_GTK_INSIDE_GDU_GTK_H
 
 G_BEGIN_DECLS



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]