1 /*
2 * Copyright (C) 2010 Stefan Walter
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include "gcr/gcr-icons.h"
21 #include "gcr/gcr-parser.h"
22
23 #include "gcr-display-view.h"
24 #include "gcr-secure-entry-buffer.h"
25 #include "gcr-unlock-renderer.h"
26
27 #include <gdk/gdk.h>
28 #include <glib/gi18n-lib.h>
29
30 enum {
31 PROP_0,
32 PROP_LABEL,
33 PROP_ATTRIBUTES
34 };
35
36 struct _GcrUnlockRendererPrivate {
37 GtkEntry *entry;
38 GtkLabel *warning;
39
40 GBytes *locked_data;
41 gchar *label;
42 gboolean unlocked;
43 GList *renderers;
44 guint unlock_tries;
45
46 /* block widget destroys during render */
47 gint no_destroy;
48 };
49
50 enum {
51 UNLOCK_CLICKED,
52 LAST_SIGNAL,
53 };
54
55 static guint signals[LAST_SIGNAL] = { 0 };
56
57 static void gcr_renderer_iface_init (GcrRendererIface *iface);
58
59 G_DEFINE_TYPE_WITH_CODE (GcrUnlockRenderer, _gcr_unlock_renderer, GTK_TYPE_BIN,
60 G_ADD_PRIVATE (GcrUnlockRenderer);
61 G_IMPLEMENT_INTERFACE (GCR_TYPE_RENDERER, gcr_renderer_iface_init);
62 );
63
64 static gchar*
calculate_label(GcrUnlockRenderer * self)65 calculate_label (GcrUnlockRenderer *self)
66 {
67 if (self->pv->label)
68 return g_strdup_printf (_("Unlock: %s"), self->pv->label);
69
70 return g_strdup (_("Unlock"));
71 }
72
73 void
_gcr_unlock_renderer_show_warning(GcrUnlockRenderer * self,const gchar * message)74 _gcr_unlock_renderer_show_warning (GcrUnlockRenderer *self,
75 const gchar *message)
76 {
77 gchar *text;
78
79 g_return_if_fail (GCR_UNLOCK_RENDERER (self));
80 g_return_if_fail (message != NULL);
81
82 text = g_strdup_printf ("<i>%s</i>", message);
83 gtk_label_set_markup (self->pv->warning, text);
84 g_free (text);
85
86 gtk_widget_show (GTK_WIDGET (self->pv->warning));
87 }
88
89 static void
on_unlock_button_clicked(GtkButton * button,gpointer user_data)90 on_unlock_button_clicked (GtkButton *button,
91 gpointer user_data)
92 {
93 GcrUnlockRenderer *self = GCR_UNLOCK_RENDERER (user_data);
94 g_signal_emit (self, signals[UNLOCK_CLICKED], 0);
95 }
96
97 static void
on_entry_activated(GtkEntry * entry,gpointer user_data)98 on_entry_activated (GtkEntry *entry,
99 gpointer user_data)
100 {
101 GtkButton *button = GTK_BUTTON (user_data);
102 gtk_button_clicked (button);
103 }
104
105 static void
_gcr_unlock_renderer_init(GcrUnlockRenderer * self)106 _gcr_unlock_renderer_init (GcrUnlockRenderer *self)
107 {
108 GtkWidget *box, *vbox;
109 GtkWidget *button;
110 GtkEntryBuffer *buffer;
111
112 self->pv = _gcr_unlock_renderer_get_instance_private (self);
113
114 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
115
116 buffer = gcr_secure_entry_buffer_new ();
117 self->pv->entry = GTK_ENTRY (gtk_entry_new_with_buffer (buffer));
118 gtk_entry_set_visibility (self->pv->entry, FALSE);
119 gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (self->pv->entry), TRUE, FALSE, 0);
120 gtk_widget_show (GTK_WIDGET (self->pv->entry));
121 g_object_unref (buffer);
122 gtk_entry_set_placeholder_text (self->pv->entry, _("Password"));
123
124 button = gtk_button_new_with_label (_("Unlock"));
125 gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
126 g_signal_connect (button, "clicked", G_CALLBACK (on_unlock_button_clicked), self);
127 g_signal_connect (self->pv->entry, "activate", G_CALLBACK (on_entry_activated), button);
128 gtk_widget_show (button);
129
130 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
131 gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, FALSE, 0);
132 gtk_widget_show (box);
133
134 self->pv->warning = GTK_LABEL (gtk_label_new (""));
135 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (self->pv->warning), FALSE, FALSE, 0);
136 gtk_widget_hide (GTK_WIDGET (self->pv->warning));
137
138 gtk_container_add (GTK_CONTAINER (self), vbox);
139 gtk_widget_show (vbox);
140 }
141
142 static void
_gcr_unlock_renderer_finalize(GObject * obj)143 _gcr_unlock_renderer_finalize (GObject *obj)
144 {
145 GcrUnlockRenderer *self = GCR_UNLOCK_RENDERER (obj);
146
147 g_bytes_unref (self->pv->locked_data);
148 g_free (self->pv->label);
149 g_list_free_full (self->pv->renderers, g_object_unref);
150
151 G_OBJECT_CLASS (_gcr_unlock_renderer_parent_class)->finalize (obj);
152 }
153
154 static void
_gcr_unlock_renderer_set_property(GObject * obj,guint prop_id,const GValue * value,GParamSpec * pspec)155 _gcr_unlock_renderer_set_property (GObject *obj,
156 guint prop_id,
157 const GValue *value,
158 GParamSpec *pspec)
159 {
160 GcrUnlockRenderer *self = GCR_UNLOCK_RENDERER (obj);
161
162 switch (prop_id) {
163 case PROP_LABEL:
164 g_free (self->pv->label);
165 self->pv->label = g_value_dup_string (value);
166 g_object_notify (obj, "label");
167 gcr_renderer_emit_data_changed (GCR_RENDERER (self));
168 break;
169 case PROP_ATTRIBUTES:
170 break;
171 default:
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
173 break;
174 }
175 }
176
177 static void
_gcr_unlock_renderer_get_property(GObject * obj,guint prop_id,GValue * value,GParamSpec * pspec)178 _gcr_unlock_renderer_get_property (GObject *obj,
179 guint prop_id,
180 GValue *value,
181 GParamSpec *pspec)
182 {
183 GcrUnlockRenderer *self = GCR_UNLOCK_RENDERER (obj);
184
185 switch (prop_id) {
186 case PROP_LABEL:
187 g_value_take_string (value, calculate_label (self));
188 break;
189 case PROP_ATTRIBUTES:
190 g_value_set_boxed (value, NULL);
191 break;
192 default:
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
194 break;
195 }
196 }
197
198 static void
_gcr_unlock_renderer_class_init(GcrUnlockRendererClass * klass)199 _gcr_unlock_renderer_class_init (GcrUnlockRendererClass *klass)
200 {
201 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
202
203 gobject_class->finalize = _gcr_unlock_renderer_finalize;
204 gobject_class->set_property = _gcr_unlock_renderer_set_property;
205 gobject_class->get_property = _gcr_unlock_renderer_get_property;
206
207 g_object_class_install_property (gobject_class, PROP_LABEL,
208 g_param_spec_string ("label", "Label", "Unlock Label",
209 "",
210 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
211
212 g_object_class_install_property (gobject_class, PROP_ATTRIBUTES,
213 g_param_spec_boxed ("attributes", "Attributes", "Certificate pkcs11 attributes",
214 GCK_TYPE_ATTRIBUTES,
215 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216
217 signals[UNLOCK_CLICKED] = g_signal_new ("unlock-clicked", GCR_TYPE_UNLOCK_RENDERER, G_SIGNAL_RUN_LAST,
218 G_STRUCT_OFFSET (GcrUnlockRendererClass, unlock_clicked),
219 NULL, NULL, NULL, G_TYPE_NONE, 0);
220 }
221
222 static void
gcr_unlock_renderer_render(GcrRenderer * renderer,GcrViewer * viewer)223 gcr_unlock_renderer_render (GcrRenderer *renderer,
224 GcrViewer *viewer)
225 {
226 GcrUnlockRenderer *self = GCR_UNLOCK_RENDERER (renderer);
227 GcrDisplayView *view;
228 gchar *display;
229 GList *renderers;
230 GIcon *icon;
231 GList *l;
232
233 if (GCR_IS_DISPLAY_VIEW (viewer)) {
234 view = GCR_DISPLAY_VIEW (viewer);
235
236 } else {
237 g_warning ("GcrUnlockRenderer only works with internal specific "
238 "GcrViewer returned by gcr_viewer_new().");
239 return;
240 }
241
242 /*
243 * If we were successfully unlocked, then this will contain a list of
244 * renderers to add to the viewer.
245 */
246 if (self->pv->unlocked) {
247
248 /* We used prepend above, so list is backwards */
249 renderers = g_list_reverse (self->pv->renderers);
250 self->pv->renderers = NULL;
251
252 for (l = renderers; l != NULL; l = g_list_next (l))
253 gcr_viewer_insert_renderer (viewer, l->data, renderer);
254 g_list_free_full (renderers, g_object_unref);
255
256 /* And finally remove ourselves from the viewer */
257 gcr_viewer_remove_renderer (viewer, GCR_RENDERER (self));
258 /*
259 * Not yet unlocked, display the unlock dialog.
260 */
261 } else {
262
263 _gcr_display_view_begin (view, renderer);
264
265 icon = g_themed_icon_new ("emblem-readonly");
266 _gcr_display_view_set_icon (view, renderer, icon);
267 g_object_unref (icon);
268
269 display = calculate_label (self);
270 _gcr_display_view_append_title (view, renderer, display);
271 g_free (display);
272
273 if (self->pv->label)
274 display = g_strdup_printf (_("The contents of “%s” are locked. In order to view the contents, enter the correct password."),
275 self->pv->label);
276 else
277 display = g_strdup (_("The contents are locked. In order to view the contents, enter the correct password."));
278 _gcr_display_view_append_content (view, renderer, display, NULL);
279 g_free (display);
280
281 _gcr_display_view_add_widget_area (view, renderer, GTK_WIDGET (self));
282 gtk_widget_show (GTK_WIDGET (self));
283
284 _gcr_display_view_end (view, renderer);
285 }
286 }
287
288 static void
gcr_renderer_iface_init(GcrRendererIface * iface)289 gcr_renderer_iface_init (GcrRendererIface *iface)
290 {
291 iface->render_view = gcr_unlock_renderer_render;
292 }
293
294 GcrUnlockRenderer*
_gcr_unlock_renderer_new(const gchar * label,GBytes * locked_data)295 _gcr_unlock_renderer_new (const gchar *label,
296 GBytes *locked_data)
297 {
298 GcrUnlockRenderer *renderer;
299
300 renderer = g_object_new (GCR_TYPE_UNLOCK_RENDERER,
301 "label", label,
302 NULL);
303 g_object_ref_sink (renderer);
304
305 renderer->pv->locked_data = g_bytes_ref (locked_data);
306 return renderer;
307 }
308
309 GcrUnlockRenderer *
_gcr_unlock_renderer_new_for_parsed(GcrParser * parser)310 _gcr_unlock_renderer_new_for_parsed (GcrParser *parser)
311 {
312 g_return_val_if_fail (GCR_IS_PARSER (parser), NULL);
313 return _gcr_unlock_renderer_new (gcr_parser_get_parsed_label (parser),
314 gcr_parser_get_parsed_bytes (parser));
315 }
316
317 const gchar *
_gcr_unlock_renderer_get_password(GcrUnlockRenderer * self)318 _gcr_unlock_renderer_get_password (GcrUnlockRenderer *self)
319 {
320 g_return_val_if_fail (GCR_IS_UNLOCK_RENDERER (self), NULL);
321 return gtk_entry_get_text (self->pv->entry);
322 }
323
324 void
_gcr_unlock_renderer_set_password(GcrUnlockRenderer * self,const gchar * text)325 _gcr_unlock_renderer_set_password (GcrUnlockRenderer *self,
326 const gchar *text)
327 {
328 g_return_if_fail (GCR_IS_UNLOCK_RENDERER (self));
329 g_return_if_fail (text != NULL);
330 gtk_entry_set_text (self->pv->entry, text);
331 }
332
333 void
_gcr_unlock_renderer_focus_password(GcrUnlockRenderer * self)334 _gcr_unlock_renderer_focus_password (GcrUnlockRenderer *self)
335 {
336 g_return_if_fail (GCR_IS_UNLOCK_RENDERER (self));
337 gtk_widget_grab_focus (GTK_WIDGET (self->pv->entry));
338 }
339
340 GBytes *
_gcr_unlock_renderer_get_locked_data(GcrUnlockRenderer * self)341 _gcr_unlock_renderer_get_locked_data (GcrUnlockRenderer *self)
342 {
343 g_return_val_if_fail (GCR_IS_UNLOCK_RENDERER (self), NULL);
344 return self->pv->locked_data;
345 }
346