summaryrefslogtreecommitdiffstats
path: root/widgets/src/DiskOverview.c
blob: 6ebdb9f36011cca8fc53721a1b77f218d3ebc520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * Copyright (C) 2011  Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Chris Lumens <clumens@redhat.com>
 */

#include <string.h>

#include "DiskOverview.h"
#include "intl.h"

/**
 * SECTION: DiskOverview
 * @title: AnacondaDiskOverview
 * @short_description: A widget that displays basic information about a disk
 *
 * A #AnacondaDiskOverview is a potentially selectable widget that displays a
 * disk device's size, kind, and a prominant icon based on the kind of device.
 * This widget can come in different sizes, depending on where it needs to be
 * used.
 *
 * As a #AnacondaDiskOverview is a subclass of a #GtkEventBox, any signals
 * may be caught.  The #GtkWidget::button-press-event signal is already
 * handled internally to change the background color, but may also be handled
 * by user code in order to take some action based on the disk clicked upon.
 */
enum {
    PROP_DESCRIPTION = 1,
    PROP_KIND,
    PROP_CAPACITY,
    PROP_OS,
    PROP_POPUP_INFO
};

/* Defaults for each property. */
#define DEFAULT_DESCRIPTION   N_("New Device")
#define DEFAULT_KIND          N_("drive-harddisk")
#define DEFAULT_CAPACITY      N_("0 MB")
#define DEFAULT_OS            ""
#define DEFAULT_POPUP_INFO    ""

struct _AnacondaDiskOverviewPrivate {
    GtkWidget *vbox;
    GtkWidget *kind;
    GtkWidget *description_label;
    GtkWidget *capacity_label;
    GtkWidget *os_label;
    GtkWidget *tooltip;

    gboolean selected;
};

G_DEFINE_TYPE(AnacondaDiskOverview, anaconda_disk_overview, GTK_TYPE_EVENT_BOX)

gboolean anaconda_disk_overview_clicked(AnacondaDiskOverview *widget, GdkEvent *event);
static void anaconda_disk_overview_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
static void anaconda_disk_overview_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void anaconda_disk_overview_set_selected_flag(AnacondaDiskOverview *widget);

static void anaconda_disk_overview_class_init(AnacondaDiskOverviewClass *klass) {
    GObjectClass *object_class = G_OBJECT_CLASS(klass);

    object_class->set_property = anaconda_disk_overview_set_property;
    object_class->get_property = anaconda_disk_overview_get_property;

    /**
     * AnacondaDiskOverview:kind:
     *
     * The :kind string specifies what type of disk device this is, used to
     * figure out what icon to be displayed.  This should be something like
     * "drive-harddisk", "drive-removable-media", etc.
     *
     * Since: 1.0
     */
    g_object_class_install_property(object_class,
                                    PROP_KIND,
                                    g_param_spec_string("kind",
                                                        P_("kind"),
                                                        P_("Drive kind icon"),
                                                        DEFAULT_KIND,
                                                        G_PARAM_READWRITE));

    /**
     * AnacondaDiskOverview:description:
     *
     * The :description string is a very basic description of the device
     * and is displayed in bold letters under the icon.
     *
     * Since: 1.0
     */
    g_object_class_install_property(object_class,
                                    PROP_DESCRIPTION,
                                    g_param_spec_string("description",
                                                        P_("Description"),
                                                        P_("The drive description"),
                                                        DEFAULT_DESCRIPTION,
                                                        G_PARAM_READWRITE));

    /**
     * AnacondaDiskOverview:capacity:
     *
     * The :capacity string is the total size of the disk, plus units.
     *
     * Since: 1.0
     */
    g_object_class_install_property(object_class,
                                    PROP_CAPACITY,
                                    g_param_spec_string("capacity",
                                                        P_("Capacity"),
                                                        P_("The drive size (including units)"),
                                                        DEFAULT_CAPACITY,
                                                        G_PARAM_READWRITE));

    /**
     * AnacondaDiskOverview:os:
     *
     * The :os string describes any operating system found on this device.
     *
     * Since: 1.0
     */
    g_object_class_install_property(object_class,
                                    PROP_OS,
                                    g_param_spec_string("os",
                                                        P_("Operating System"),
                                                        P_("Installed OS on this drive"),
                                                        DEFAULT_OS,
                                                        G_PARAM_READWRITE));

    /**
     * AnacondaDiskOverview:popup-info:
     *
     * The :popup-info string is text that should appear in a tooltip when the
     * #AnacondaDiskOverview is hovered over.  For normal disk devices, this
     * could be available space information.  For more complex devics, this
     * could be WWID, LUN, and so forth.
     *
     * Since: 1.0
     */
    g_object_class_install_property(object_class,
                                    PROP_POPUP_INFO,
                                    g_param_spec_string("popup-info",
                                                        P_("Detailed Disk Information"),
                                                        P_("Tooltip information for this drive"),
                                                        DEFAULT_POPUP_INFO,
                                                        G_PARAM_READWRITE));

    g_type_class_add_private(object_class, sizeof(AnacondaDiskOverviewPrivate));
}

/**
 * anaconda_disk_overview_new:
 *
 * Creates a new #AnacondaDiskOverview, which is a potentially selectable
 * widget that displays basic information about a single storage device, be
 * that a regular disk or a more complicated network device.
 *
 * Returns: A new #AnacondaDiskOverview.
 */
GtkWidget *anaconda_disk_overview_new() {
    return g_object_new(ANACONDA_TYPE_DISK_OVERVIEW, NULL);
}

/* Initialize the widgets in a newly allocated DiskOverview. */
static void anaconda_disk_overview_init(AnacondaDiskOverview *widget) {
    char *markup;

    widget->priv = G_TYPE_INSTANCE_GET_PRIVATE(widget,
                                               ANACONDA_TYPE_DISK_OVERVIEW,
                                               AnacondaDiskOverviewPrivate);

    /* Set some properties. */
    widget->priv->selected = FALSE;

    /* Create the vbox. */
    widget->priv->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);

    /* Create the capacity label. */
    widget->priv->capacity_label = gtk_label_new(NULL);
    markup = g_markup_printf_escaped("<span size='large'>%s</span>", _(DEFAULT_CAPACITY));
    gtk_label_set_markup(GTK_LABEL(widget->priv->capacity_label), markup);
    g_free(markup);

    /* Create the spoke's icon. */
    widget->priv->kind = gtk_image_new_from_icon_name(DEFAULT_KIND, GTK_ICON_SIZE_DIALOG);
    gtk_image_set_pixel_size(GTK_IMAGE(widget->priv->kind), 200);

    /* Create the description label. */
    widget->priv->description_label = gtk_label_new(NULL);
    markup = g_markup_printf_escaped("<span weight='bold' size='large'>%s</span>", _(DEFAULT_DESCRIPTION));
    gtk_label_set_markup(GTK_LABEL(widget->priv->description_label), markup);
    g_free(markup);

    /* Create the OS label.  By default there is no operating system, so just
     * create a new label here so we have a place for later, should an OS be
     * specified.
     */
    widget->priv->os_label = gtk_label_new(NULL);

    /* Add everything to the vbox, add the vbox to the widget. */
    gtk_container_add(GTK_CONTAINER(widget->priv->vbox), widget->priv->capacity_label);
    gtk_container_add(GTK_CONTAINER(widget->priv->vbox), widget->priv->kind);
    gtk_container_add(GTK_CONTAINER(widget->priv->vbox), widget->priv->description_label);
    gtk_container_add(GTK_CONTAINER(widget->priv->vbox), widget->priv->os_label);

    gtk_container_add(GTK_CONTAINER(widget), widget->priv->vbox);

    /* We need to handle button-press-event in order to change the background color. */
    g_signal_connect(widget, "button-press-event", G_CALLBACK(anaconda_disk_overview_clicked), NULL);
}

gboolean anaconda_disk_overview_clicked(AnacondaDiskOverview *widget, GdkEvent *event) {
    widget->priv->selected = !widget->priv->selected;
    anaconda_disk_overview_set_selected_flag(widget);
    return FALSE;
}

static void anaconda_disk_overview_set_selected_flag(AnacondaDiskOverview *widget) {
    GtkStateFlags new_state;

    /* This toggles whether the DiskOverview widget is selected or not.  If it's selected,
     * we then let GTK in on that secret and it'll render the background in whatever color
     * the theme CSS says.
     */
    new_state = gtk_widget_get_state_flags(GTK_WIDGET(widget)) & ~GTK_STATE_FLAG_SELECTED;
    if (widget->priv->selected)
        new_state |= GTK_STATE_FLAG_SELECTED;

    gtk_widget_set_state_flags(GTK_WIDGET(widget), new_state, TRUE);
}

static void anaconda_disk_overview_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
    AnacondaDiskOverview *widget = ANACONDA_DISK_OVERVIEW(object);
    AnacondaDiskOverviewPrivate *priv = widget->priv;

    switch(prop_id) {
        case PROP_DESCRIPTION:
            g_value_set_string (value, gtk_label_get_text(GTK_LABEL(priv->description_label)));
            break;

        case PROP_KIND:
            g_value_set_object (value, (GObject *)priv->kind);
            break;

        case PROP_CAPACITY:
            g_value_set_string (value, gtk_label_get_text(GTK_LABEL(priv->capacity_label)));
            break;

        case PROP_OS:
            g_value_set_string (value, gtk_label_get_text(GTK_LABEL(priv->os_label)));
            break;

        case PROP_POPUP_INFO:
            g_value_set_string (value, gtk_widget_get_tooltip_text(GTK_WIDGET(widget)));
            break;
    }
}

static void anaconda_disk_overview_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) {
    AnacondaDiskOverview *widget = ANACONDA_DISK_OVERVIEW(object);
    AnacondaDiskOverviewPrivate *priv = widget->priv;

    switch(prop_id) {
        case PROP_DESCRIPTION: {
            char *markup = g_markup_printf_escaped("<span weight='bold' size='large'>%s</span>", g_value_get_string(value));
            gtk_label_set_markup(GTK_LABEL(priv->description_label), markup);
            g_free(markup);
            break;
        }

        case PROP_KIND:
            gtk_image_set_from_icon_name(GTK_IMAGE(priv->kind), g_value_get_string(value), GTK_ICON_SIZE_DIALOG);
            gtk_image_set_pixel_size(GTK_IMAGE(priv->kind), 200);
            break;

        case PROP_CAPACITY: {
            char *markup = g_markup_printf_escaped("<span size='large'>%s</span>", g_value_get_string(value));
            gtk_label_set_markup(GTK_LABEL(priv->capacity_label), markup);
            g_free(markup);
            break;
        }

        case PROP_OS: {
            /* If no OS is given, set the label to blank.  This will prevent
             * seeing a strange brown blob with no text in the middle of
             * nowhere.
             */
            if (!strcmp(g_value_get_string(value), ""))
               gtk_label_set_text(GTK_LABEL(priv->os_label), NULL);
            else {
                char *markup = g_markup_printf_escaped("<span foreground='white' background='brown'>%s</span>", g_value_get_string(value));
                gtk_label_set_markup(GTK_LABEL(priv->os_label), markup);
                g_free(markup);
                break;
            }
        }

        case PROP_POPUP_INFO: {
            if (!strcmp(g_value_get_string(value), ""))
                gtk_widget_set_has_tooltip(GTK_WIDGET(widget), FALSE);
            else {
                gtk_widget_set_tooltip_text(GTK_WIDGET(widget), g_value_get_string(value));
                break;
            }
        }
    }
}

/**
 * anaconda_disk_overview_get_selected:
 * @widget: a #AnacondaDiskOverview
 *
 * Returns whether or not this disk has been selected by the user.
 *
 * Returns: Whether @widget has been selected.
 *
 * Since: 1.0
 */
gboolean anaconda_disk_overview_get_selected(AnacondaDiskOverview *widget) {
    return widget->priv->selected;
}

/**
 * anaconda_disk_overview_set_selected:
 * @widget: a #AnacondaDiskOverview
 * @is_selected: %TRUE if this disk is selected.
 *
 * Specifies whether the disk shown by this overview has been selected by
 * the user for inclusion in installation.  If so, a special background will
 * be set as a visual indicator.
 *
 * Since: 1.0
 */
void anaconda_disk_overview_set_selected(AnacondaDiskOverview *widget, gboolean is_selected) {
    widget->priv->selected = is_selected;
    anaconda_disk_overview_set_selected_flag(widget);
}