gs-info-bar.c 5.43 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2
 * vi:set noexpandtab tabstop=8 shiftwidth=8:
3 4 5
 *
 * Copyright (C) 2016 Rafał Lużyński <digitalfreak@lingonborough.com>
 *
6
 * SPDX-License-Identifier: GPL-2.0+
7 8 9 10 11 12 13 14 15 16 17 18 19
 */

#include "config.h"

#include "gs-info-bar.h"

struct _GsInfoBar
{
	GtkInfoBar	 parent_instance;

	GtkWidget	*label_title;
	GtkWidget	*label_body;
	GtkWidget	*label_warning;
20
	gboolean	 has_window;
21 22 23 24 25 26 27 28
};

G_DEFINE_TYPE (GsInfoBar, gs_info_bar, GTK_TYPE_INFO_BAR)

enum {
	PROP_0,
	PROP_TITLE,
	PROP_BODY,
29 30
	PROP_WARNING,
	PROP_HAS_WINDOW
31 32
};

33 34 35 36 37 38 39 40 41 42 43 44 45
static gboolean
gs_info_bar_get_has_window (GsInfoBar *bar)
{
	return bar->has_window;
}

static void
gs_info_bar_set_has_window (GsInfoBar *bar,
			    gboolean has_window)
{
	bar->has_window = has_window;
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
static void
gs_info_bar_get_property (GObject *object,
			  guint prop_id,
			  GValue *value,
			  GParamSpec *pspec)
{
	GsInfoBar *infobar = GS_INFO_BAR (object);

	switch (prop_id) {
	case PROP_TITLE:
		g_value_set_string (value, gs_info_bar_get_title (infobar));
		break;
	case PROP_BODY:
		g_value_set_string (value, gs_info_bar_get_body (infobar));
		break;
	case PROP_WARNING:
		g_value_set_string (value, gs_info_bar_get_warning (infobar));
		break;
64 65 66
	case PROP_HAS_WINDOW:
		g_value_set_boolean (value, gs_info_bar_get_has_window (infobar));
		break;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gs_info_bar_set_property (GObject *object,
			  guint prop_id,
			  const GValue *value,
			  GParamSpec *pspec)
{
	GsInfoBar *infobar = GS_INFO_BAR (object);

	switch (prop_id) {
	case PROP_TITLE:
		gs_info_bar_set_title (infobar, g_value_get_string (value));
		break;
	case PROP_BODY:
		gs_info_bar_set_body (infobar, g_value_get_string (value));
		break;
	case PROP_WARNING:
		gs_info_bar_set_warning (infobar, g_value_get_string (value));
		break;
91 92 93
	case PROP_HAS_WINDOW:
		gs_info_bar_set_has_window (infobar, g_value_get_boolean (value));
		break;
94 95 96 97 98 99
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

100 101 102 103 104 105 106 107 108 109
static void
gs_info_bar_constructed (GObject *object)
{
	GsInfoBar *bar = GS_INFO_BAR (object);

	G_OBJECT_CLASS (gs_info_bar_parent_class)->constructed (object);

	gtk_widget_set_has_window (GTK_WIDGET (object), bar->has_window);
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
static void
gs_info_bar_init (GsInfoBar *infobar)
{
	gtk_widget_set_has_window (GTK_WIDGET (infobar), FALSE);
	gtk_widget_init_template (GTK_WIDGET (infobar));
}

static void
gs_info_bar_class_init (GsInfoBarClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

	object_class->get_property = gs_info_bar_get_property;
	object_class->set_property = gs_info_bar_set_property;
125
	object_class->constructed = gs_info_bar_constructed;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

	g_object_class_install_property (object_class, PROP_TITLE,
		g_param_spec_string ("title",
				     "Title Text",
				     "The title (header) text of the info bar",
				     "",
				     G_PARAM_READWRITE));

	g_object_class_install_property (object_class, PROP_BODY,
		g_param_spec_string ("body",
				     "Body Text",
				     "The body (main) text of the info bar",
				     NULL,
				     G_PARAM_READWRITE));

	g_object_class_install_property (object_class, PROP_WARNING,
		g_param_spec_string ("warning",
				     "Warning Text",
				     "The warning text of the info bar, below the body text",
				     NULL,
				     G_PARAM_READWRITE));

148 149 150 151 152 153 154 155
	g_object_class_install_property (object_class, PROP_HAS_WINDOW,
		g_param_spec_boolean ("has-window",
				      "Has Window",
				      "Private property, whether the info bar needs its own window",
				      FALSE,
				      G_PARAM_READWRITE |
				      G_PARAM_CONSTRUCT));

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
	gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Software/gs-info-bar.ui");

	gtk_widget_class_bind_template_child (widget_class,
					      GsInfoBar, label_title);
	gtk_widget_class_bind_template_child (widget_class,
					      GsInfoBar, label_body);
	gtk_widget_class_bind_template_child (widget_class,
					      GsInfoBar, label_warning);
}

GtkWidget *
gs_info_bar_new (void)
{
	GsInfoBar *infobar;

	infobar = g_object_new (GS_TYPE_INFO_BAR, NULL);

	return GTK_WIDGET (infobar);
}

static const gchar *
retrieve_from_label (GtkWidget *label)
{
	if (!gtk_widget_get_visible (label))
		return NULL;
	else
		return gtk_label_get_label (GTK_LABEL (label));
}

static void
update_label (GtkWidget *label, const gchar *text)
{
	gtk_label_set_label (GTK_LABEL (label), text);
	gtk_widget_set_visible (label,
				text != NULL && *text != '\0');
}

const gchar *
gs_info_bar_get_title (GsInfoBar *bar)
{
	g_return_val_if_fail (GS_IS_INFO_BAR (bar), NULL);

	return retrieve_from_label (bar->label_title);
}

void
gs_info_bar_set_title (GsInfoBar *bar, const gchar *text)
{
	g_return_if_fail (GS_IS_INFO_BAR (bar));

	update_label (bar->label_title, text);
}

const gchar *
gs_info_bar_get_body (GsInfoBar *bar)
{
	g_return_val_if_fail (GS_IS_INFO_BAR (bar), NULL);

	return retrieve_from_label (bar->label_body);
}

void
gs_info_bar_set_body (GsInfoBar *bar, const gchar *text)
{
	g_return_if_fail (GS_IS_INFO_BAR (bar));

	update_label (bar->label_body, text);
}

const gchar *
gs_info_bar_get_warning (GsInfoBar *bar)
{
	g_return_val_if_fail (GS_IS_INFO_BAR (bar), NULL);

	return retrieve_from_label (bar->label_warning);
}

void
gs_info_bar_set_warning (GsInfoBar *bar, const gchar *text)
{
	g_return_if_fail (GS_IS_INFO_BAR (bar));

	update_label (bar->label_warning, text);
}