summaryrefslogtreecommitdiff
path: root/shell/ev-properties-license.c
blob: f9f11b04bd84307d60f672a1cb3dcb21ec4095f5 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
/* this file is part of atril, a mate document viewer
 *
 *  Copyright (C) 2009 Juanjo MarĂ­n <juanj.marin@juntadeandalucia.es>
 *  Copyright (C) 2005 Red Hat, Inc
 *
 * Atril 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.
 *
 * Atril 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "config.h"

#include <string.h>

#include <glib/gi18n.h>
#include <gtk/gtk.h>

#include "ev-properties-license.h"

struct _EvPropertiesLicense {
	GtkBox base_instance;
};

struct _EvPropertiesLicenseClass {
	GtkBoxClass base_class;
};

G_DEFINE_TYPE (EvPropertiesLicense, ev_properties_license, GTK_TYPE_BOX)

static void
ev_properties_license_class_init (EvPropertiesLicenseClass *properties_license_class)
{
}

static GtkWidget *
get_license_text_widget (EvDocumentLicense *license)
{
	GtkWidget *swindow, *textview;
	GtkTextBuffer *buffer;

	textview = gtk_text_view_new ();
	gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (textview), GTK_WRAP_WORD);
	gtk_text_view_set_left_margin (GTK_TEXT_VIEW (textview), 8);
	gtk_text_view_set_right_margin (GTK_TEXT_VIEW (textview), 8);

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
	gtk_text_buffer_set_text (buffer, ev_document_license_get_text (license), -1);

	swindow = gtk_scrolled_window_new (NULL, NULL);
	gtk_widget_set_hexpand (swindow, TRUE);
	gtk_widget_set_margin_start (swindow, 12);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
					GTK_POLICY_AUTOMATIC,
					GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
					     GTK_SHADOW_IN);
	gtk_container_add (GTK_CONTAINER (swindow), textview);
	gtk_widget_show (textview);

	return swindow;
}

static GtkWidget *
get_license_uri_widget (const gchar *uri)
{
	GtkWidget *label;
	gchar     *checked_uri;
	gchar     *markup;

	label = gtk_label_new (NULL);
	gtk_widget_set_margin_start (label, 12);
	g_object_set (G_OBJECT (label),
		      "xalign", 0.0,
		      "width_chars", 25,
		      "selectable", TRUE,
		      "ellipsize", PANGO_ELLIPSIZE_END,
		      NULL);

	checked_uri = g_uri_parse_scheme (uri);
	if (checked_uri) {
		markup = g_markup_printf_escaped ("<a href=\"%s\">%s</a>", uri, uri);
		gtk_label_set_markup (GTK_LABEL (label), markup);
		g_free (markup);
		g_free (checked_uri);
	} else {
		gtk_label_set_text (GTK_LABEL (label), uri);
	}

	return label;
}

static void
ev_properties_license_add_section (EvPropertiesLicense *properties,
				   const gchar         *title_text,
				   GtkWidget           *contents)
{
	GtkWidget *title;
	gchar     *markup;

	title = gtk_label_new (NULL);
#if GTK_CHECK_VERSION (3, 16, 0)
	gtk_label_set_xalign (GTK_LABEL (title), 0.0);
#else
	gtk_misc_set_alignment (GTK_MISC (title), 0.0, 0.5);
#endif
	gtk_label_set_use_markup (GTK_LABEL (title), TRUE);
	markup = g_strdup_printf ("<b>%s</b>", title_text);
	gtk_label_set_markup (GTK_LABEL (title), markup);
	g_free (markup);
	gtk_box_pack_start (GTK_BOX (properties), title, FALSE, FALSE, 0);
	gtk_widget_show (title);

	gtk_box_pack_start (GTK_BOX (properties), contents, FALSE, TRUE, 0);
	gtk_widget_show (contents);
}

void
ev_properties_license_set_license (EvPropertiesLicense *properties,
				   EvDocumentLicense   *license)
{
	const gchar *text = ev_document_license_get_text (license);
	const gchar *uri = ev_document_license_get_uri (license);
	const gchar *web_statement = ev_document_license_get_web_statement (license);

	if (text) {
		ev_properties_license_add_section (properties,
						   _("Usage terms"),
						   get_license_text_widget (license));
	}

	if (uri) {
		ev_properties_license_add_section (properties,
						   _("Text License"),
						   get_license_uri_widget (uri));
	}

	if (web_statement) {
		ev_properties_license_add_section (properties,
						   _("Further Information"),
						   get_license_uri_widget (web_statement));
	}
}

static void
ev_properties_license_init (EvPropertiesLicense *properties)
{
	gtk_orientable_set_orientation (GTK_ORIENTABLE (properties), GTK_ORIENTATION_VERTICAL);
	gtk_box_set_spacing (GTK_BOX (properties), 12);
	gtk_container_set_border_width (GTK_CONTAINER (properties), 12);
}

GtkWidget *
ev_properties_license_new (void)
{
	EvPropertiesLicense *properties_license;

	properties_license = g_object_new (EV_TYPE_PROPERTIES_LICENSE, NULL);

	return GTK_WIDGET (properties_license);
}