1 /*
2  * gnome-keyring
3  *
4  * Copyright (C) 2008 Stefan Walter
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 
23 #include "gkm-ssh-public-key.h"
24 
25 #include "gkm/gkm-attributes.h"
26 #include "gkm/gkm-module.h"
27 #include "gkm/gkm-object.h"
28 #include "gkm/gkm-util.h"
29 
30 #include <glib/gi18n.h>
31 
32 enum {
33 	PROP_0,
34 	PROP_LABEL
35 };
36 
37 struct _GkmSshPublicKey {
38 	GkmPublicXsaKey parent;
39 	gchar *label;
40 };
41 
42 G_DEFINE_TYPE (GkmSshPublicKey, gkm_ssh_public_key, GKM_TYPE_PUBLIC_XSA_KEY);
43 
44 /* -----------------------------------------------------------------------------
45  * OBJECT
46  */
47 
48 static CK_RV
gkm_ssh_public_key_get_attribute(GkmObject * base,GkmSession * session,CK_ATTRIBUTE_PTR attr)49 gkm_ssh_public_key_get_attribute (GkmObject *base, GkmSession *session, CK_ATTRIBUTE_PTR attr)
50 {
51 	GkmSshPublicKey *self = GKM_SSH_PUBLIC_KEY (base);
52 
53 	switch (attr->type) {
54 	case CKA_LABEL:
55 		return gkm_attribute_set_string (attr, self->label ? self->label : "");
56 	}
57 
58 	return GKM_OBJECT_CLASS (gkm_ssh_public_key_parent_class)->get_attribute (base, session, attr);
59 }
60 
61 static void
gkm_ssh_public_key_init(GkmSshPublicKey * self)62 gkm_ssh_public_key_init (GkmSshPublicKey *self)
63 {
64 
65 }
66 
67 static void
gkm_ssh_public_key_finalize(GObject * obj)68 gkm_ssh_public_key_finalize (GObject *obj)
69 {
70 	GkmSshPublicKey *self = GKM_SSH_PUBLIC_KEY (obj);
71 
72 	g_free (self->label);
73 	self->label = NULL;
74 
75 	G_OBJECT_CLASS (gkm_ssh_public_key_parent_class)->finalize (obj);
76 }
77 
78 static void
gkm_ssh_public_key_set_property(GObject * obj,guint prop_id,const GValue * value,GParamSpec * pspec)79 gkm_ssh_public_key_set_property (GObject *obj, guint prop_id, const GValue *value,
80                            GParamSpec *pspec)
81 {
82 	GkmSshPublicKey *self = GKM_SSH_PUBLIC_KEY (obj);
83 
84 	switch (prop_id) {
85 	case PROP_LABEL:
86 		gkm_ssh_public_key_set_label (self, g_value_get_string (value));
87 		break;
88 	default:
89 		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
90 		break;
91 	}
92 }
93 
94 static void
gkm_ssh_public_key_get_property(GObject * obj,guint prop_id,GValue * value,GParamSpec * pspec)95 gkm_ssh_public_key_get_property (GObject *obj, guint prop_id, GValue *value,
96                            GParamSpec *pspec)
97 {
98 	GkmSshPublicKey *self = GKM_SSH_PUBLIC_KEY (obj);
99 
100 	switch (prop_id) {
101 	case PROP_LABEL:
102 		g_value_set_string (value, gkm_ssh_public_key_get_label (self));
103 		break;
104 	default:
105 		G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
106 		break;
107 	}
108 }
109 
110 static void
gkm_ssh_public_key_class_init(GkmSshPublicKeyClass * klass)111 gkm_ssh_public_key_class_init (GkmSshPublicKeyClass *klass)
112 {
113 	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114 	GkmObjectClass *gkm_class = GKM_OBJECT_CLASS (klass);
115 
116 	gobject_class->finalize = gkm_ssh_public_key_finalize;
117 	gobject_class->set_property = gkm_ssh_public_key_set_property;
118 	gobject_class->get_property = gkm_ssh_public_key_get_property;
119 
120 	gkm_class->get_attribute = gkm_ssh_public_key_get_attribute;
121 
122 	g_object_class_install_property (gobject_class, PROP_LABEL,
123 	           g_param_spec_string ("label", "Label", "Object Label",
124 	                                "", G_PARAM_READWRITE));
125 }
126 
127 /* -----------------------------------------------------------------------------
128  * PUBLIC
129  */
130 
131 GkmSshPublicKey*
gkm_ssh_public_key_new(GkmModule * module,const gchar * unique)132 gkm_ssh_public_key_new (GkmModule *module, const gchar *unique)
133 {
134 	return g_object_new (GKM_TYPE_SSH_PUBLIC_KEY, "unique", unique,
135 	                     "module", module, "manager", gkm_module_get_manager (module), NULL);
136 }
137 
138 const gchar*
gkm_ssh_public_key_get_label(GkmSshPublicKey * self)139 gkm_ssh_public_key_get_label (GkmSshPublicKey *self)
140 {
141 	g_return_val_if_fail (GKM_IS_SSH_PUBLIC_KEY (self), NULL);
142 	return self->label;
143 }
144 
145 void
gkm_ssh_public_key_set_label(GkmSshPublicKey * self,const gchar * label)146 gkm_ssh_public_key_set_label (GkmSshPublicKey *self, const gchar *label)
147 {
148 	g_return_if_fail (GKM_IS_SSH_PUBLIC_KEY (self));
149 	g_free (self->label);
150 	self->label = g_strdup (label);
151 	g_object_notify (G_OBJECT (self), "label");
152 }
153