[seahorse/refactor: 25/33] WIP



commit b32533465114a4334845c48d8788148974ea9ffd
Author: Stef Walter <stefw collabora co uk>
Date:   Thu Oct 13 15:29:06 2011 +0200

    WIP

 gkr/seahorse-gkr-item.c |  489 ++++++++++++++++++++++++++---------------------
 1 files changed, 274 insertions(+), 215 deletions(-)
---
diff --git a/gkr/seahorse-gkr-item.c b/gkr/seahorse-gkr-item.c
index b3c3596..9e15de0 100644
--- a/gkr/seahorse-gkr-item.c
+++ b/gkr/seahorse-gkr-item.c
@@ -36,41 +36,241 @@
 #include "seahorse-gkr-keyring.h"
 #include "seahorse-gkr-operation.h"
 
-/* For gnome-keyring secret type ids */
-#ifdef WITH_PGP
-#include "pgp/seahorse-pgp.h"
-#endif
-#ifdef WITH_SSH
-#include "ssh/seahorse-ssh.h"
-#endif 
+#define GENERIC_SECRET "org.freedesktop.Secret.Generic"
+#define NETWORK_PASSWORD "org.gnome.keyring.NetworkPassword"
+#define KEYRING_NOTE "org.gnome.keyring.Note"
+#define CHAINED_KEYRING "org.gnome.keyring.ChainedKeyring"
+#define ENCRYPTION_KEY "org.gnome.keyring.EncryptionKey"
+#define PK_STORAGE "org.gnome.keyring.PkStorage"
+
+typedef struct _DisplayEntry DisplayEntry;
+typedef struct _DisplayInfo DisplayInfo;
+
+typedef void      (*DisplayCustom) (DisplayEntry *entry,
+                                    GnomeKeyringItemInfo *item_info,
+                                    GnomeKeyringAttributeList *item_attrs,
+                                    DisplayInfo *info);
+
+struct _DisplayEntry {
+	const gchar *item_type;
+	const gchar *description;
+	DisplayCustom custom_func;
+};
+
+struct _DisplayInfo {
+	const gchar *item_type;
+	gchar *label;
+	gchar *details;
+	const gchar *description;
+};
+
+typedef struct {
+	const gchar *item_type;
+	const gchar *mapped_type;
+	const gchar *match_attribute;
+	const gchar *match_pattern;
+} MappingEntry;
+
+static guint32
+get_attribute_int (GnomeKeyringAttributeList *attrs,
+                   const gchar *name)
+{
+	guint i;
+
+	if (!attrs)
+		return 0;
+
+	for (i = 0; i < attrs->len; i++) {
+		GnomeKeyringAttribute *attr = &(gnome_keyring_attribute_list_index (attrs, i));
+		if (g_ascii_strcasecmp (name, attr->name) == 0 &&
+		    attr->type == GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32)
+			return attr->value.integer;
+	}
+
+	return 0;
+}
+
+static const gchar *
+get_attribute_string (GnomeKeyringAttributeList *attrs,
+                      const gchar *name)
+{
+	guint i;
+
+	if (!attrs)
+		return NULL;
+
+	for (i = 0; i < attrs->len; i++) {
+		GnomeKeyringAttribute *attr = &(gnome_keyring_attribute_list_index (attrs, i));
+		if (g_ascii_strcasecmp (name, attr->name) == 0 &&
+		    attr->type == GNOME_KEYRING_ATTRIBUTE_TYPE_STRING)
+			return attr->value.string;
+	}
+
+	return NULL;
+}
+
+static gboolean
+is_custom_display_name (const gchar *server,
+                        const gchar *user,
+                        const gchar *object,
+                        guint32 port,
+                        const gchar *display)
+{
+	GString *generated;
+	gboolean ret;
+
+	/*
+	 * For network passwords gnome-keyring generates in a funky looking display
+	 * name that's generated from login credentials. We simulate that generating
+	 * here and return FALSE if it matches. This allows us to display user
+	 * customized display names and ignore the generated ones.
+	 */
+
+	if (!server)
+		return TRUE;
+
+	generated = g_string_new (NULL);
+	if (user != NULL)
+		g_string_append_printf (generated, "%s@", user);
+	g_string_append (generated, server);
+	if (port != 0)
+		g_string_append_printf (generated, ":%d", port);
+	if (object != NULL)
+		g_string_append_printf (generated, "/%s", object);
+
+	ret = strcmp (display, generated->str) != 0;
+	g_string_free (generated, TRUE);
+
+	return ret;
+}
+
+static gchar *
+calc_network_label (GnomeKeyringItemInfo *info,
+                    GnomeKeyringAttributeList *attrs,
+                    gboolean always)
+{
+	const gchar *val;
+	gchar *display;
+
+	/* HTTP usually has a the realm as the "object" display that */
+	if (is_network_item (attrs, "http") && self->pv->item_attrs) {
+		val = seahorse_gkr_item_get_attribute (self, "object");
+		if (val && val[0]) {
+					g_free (display);
+					return g_strdup (val);
+				}
+			}
 
-/* XXX Copied from libgnomeui */
-#define GNOME_STOCK_AUTHENTICATION      "gnome-stock-authentication"
-#define GNOME_STOCK_BOOK_OPEN           "gnome-stock-book-open"
-#define GNOME_STOCK_BLANK               "gnome-stock-blank"
+			/* Display the server name as a last resort */
+			if (always) {
+				val = seahorse_gkr_item_get_attribute (self, "server");
+				if (val && val[0]) {
+					g_free (display);
+					return g_strdup (val);
+				}
+			}
+		}
+
+		return always ? display : NULL;
+}
+static void
+network_custom (DisplayEntry *entry,
+                GnomeKeyringItemInfo *item_info,
+                GnomeKeyringAttributeList *item_attrs,
+                DisplayInfo *info)
+{
+	const gchar *object;
+	const gchar *user;
+	const gchar *protocol;
+	const gchar *server;
+	guint32 port;
+	gchar *uri = NULL;
+	gchar *display = NULL;
+	gchar *ret;
+
+	server = get_attribute_string (self, "server");
+	protocol = get_attribute_string (self, "protocol");
+	object = get_attribute_string (self, "object");
+	user = get_attribute_string (self, "user");
+	port = get_attribute_int (self, "port");
+
+	if (!protocol)
+		return;
+
+	display = gnome_keyring_item_info_get_display_name (info);
+
+	/* If it's customized by the application or user then display that */
+	if (is_custom_network_label (server, user, object, port, display)) {
+		info->label = display;
+	} else {
+		g_free (display);
+		info->label = calc_network_label (info, attrs, TRUE);
+	}
+
+	if (server && protocol) {
+		uri = g_strdup_printf ("  %s://%s%s%s/%s",
+		                       protocol,
+		                       user ? user : "",
+		                       user ? "@" : "",
+		                       server,
+		                       object ? object : "");
+	}
+
+	ret = g_markup_printf_escaped ("%s<span foreground='#555555' size='small' rise='0'>%s</span>",
+	                               display, uri ? uri : "");
+	g_free (display);
+	g_free (uri);
+
+	return ret;
+
+}
+
+static const DisplayEntry DISPLAY_ENTRIES[] = {
+	{ GENERIC_SECRET, N_("Password or secret"), NULL },
+	{ NETWORK_PASSWORD, N_("Network password"), network_custom },
+	{ KEYRING_NOTE, N_("Stored note"), NULL },
+	{ CHAINED_KEYRING, N_("Keyring password"), NULL },
+	{ ENCRYPTION_KEY, N_("Encryption key password"), NULL },
+	{ PK_STORAGE, N_("Key storage password"), NULL },
+	{ CHROME_PASSWORD, N_("Google Chrome password"), chrome_custom },
+	{ GOA_PASSWORD, N_("Gnome Online Accounts password"), goa_custom },
+	{ TELEPATHY_PASSWORD, N_("Telepathy password"), telepathy_custom },
+	{ NETWORK_MANAGER_SECRET, N_("Network Manager secret"), network_manager_custom },
+};
+
+static const MappingEntry MAPPING_ENTRIES[] = {
+	{ GENERIC_SECRET, CHROME_PASSWORD, "application", "chrome" },
+	{ GENERIC_SECRET, GOA_PASSWORD, "goa-identity", NULL },
+	/* IM account password for stefwalter gmail com (gabble/jabber/stefwalter_40gmail_2ecom0) */
+	{ GENERIC_SECRET, TELEPATHY_PASSWORD, "account-id", "*/*/*" },
+	/* Network secret for Auto anna/802-11-wireless-security/psk */
+	{ GENERIC_SECRET, NETWORK_MANAGER_SECRET, "connection-uuid", NULL },
+};
 
 enum {
-    PROP_0,
-    PROP_KEYRING_NAME,
-    PROP_ITEM_ID,
-    PROP_ITEM_INFO,
-    PROP_ITEM_ATTRIBUTES,
-    PROP_HAS_SECRET,
-    PROP_USE
+	PROP_0,
+	PROP_KEYRING_NAME,
+	PROP_ITEM_ID,
+	PROP_ITEM_INFO,
+	PROP_ITEM_ATTRIBUTES,
+	PROP_HAS_SECRET,
+	PROP_USE
 };
 
 struct _SeahorseGkrItemPrivate {
 	gchar *keyring_name;
 	guint32 item_id;
-	
+
 	gpointer req_info;
 	GnomeKeyringItemInfo *item_info;
-	
+
 	gpointer req_attrs;
 	GnomeKeyringAttributeList *item_attrs;
 
 	gpointer req_secret;
 	gchar *item_secret;
+
+	DisplayInfo *display_info;
 };
 
 G_DEFINE_TYPE (SeahorseGkrItem, seahorse_gkr_item, SEAHORSE_TYPE_OBJECT);
@@ -212,200 +412,77 @@ require_item_attrs (SeahorseGkrItem *self)
 	return self->pv->item_attrs != NULL;
 }
 
-static guint32
-find_attribute_int (GnomeKeyringAttributeList *attrs, const gchar *name)
-{
-    guint i;
-    
-    if (!attrs)
-        return 0;
-    
-    for (i = 0; i < attrs->len; i++) {
-        GnomeKeyringAttribute *attr = &(gnome_keyring_attribute_list_index (attrs, i));
-        if (g_ascii_strcasecmp (name, attr->name) == 0 && 
-            attr->type == GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32)
-            return attr->value.integer;
-    }
-    
-    return 0;
-}
-
-
-static gboolean
-is_network_item (SeahorseGkrItem *self, const gchar *match)
-{
-	const gchar *protocol;
-	
-	if (!require_item_info (self))
-		return FALSE;
-	
-	if (gnome_keyring_item_info_get_type (self->pv->item_info) != GNOME_KEYRING_ITEM_NETWORK_PASSWORD)
-		return FALSE;
-    
-	if (!match)
-		return TRUE;
-    
-	protocol = seahorse_gkr_item_get_attribute (self, "protocol");
-	return protocol && g_ascii_strncasecmp (protocol, match, strlen (match)) == 0;
-}
-
-static gboolean 
-is_custom_display_name (SeahorseGkrItem *self, const gchar *display)
-{
-	const gchar *user;
-	const gchar *server;
-	const gchar *object;
-	guint32 port;
-	GString *generated;
-	gboolean ret;
-	
-	if (require_item_info (self) && gnome_keyring_item_info_get_type (self->pv->item_info) != 
-					    GNOME_KEYRING_ITEM_NETWORK_PASSWORD)
-		return TRUE;
-    
-	if (!require_item_attrs (self) || !display)
-		return TRUE;
-    
-	/* 
-	 * For network passwords gnome-keyring generates in a funky looking display 
-	 * name that's generated from login credentials. We simulate that generating 
-	 * here and return FALSE if it matches. This allows us to display user 
-	 * customized display names and ignore the generated ones.
-	 */
-    
-	user = seahorse_gkr_item_get_attribute (self, "user");
-	server = seahorse_gkr_item_get_attribute (self, "server");
-	object = seahorse_gkr_item_get_attribute (self, "object");
-	port = find_attribute_int (self->pv->item_attrs, "port");
-    
-	if (!server)
-		return TRUE;
-    
-	generated = g_string_new (NULL);
-	if (user != NULL)
-		g_string_append_printf (generated, "%s@", user);
-	g_string_append (generated, server);
-	if (port != 0)
-		g_string_append_printf (generated, ":%d", port);
-	if (object != NULL)
-		g_string_append_printf (generated, "/%s", object);
-
-	ret = strcmp (display, generated->str) != 0;
-	g_string_free (generated, TRUE);
-    
-	return ret;
-}
-
-static gchar*
-calc_display_name (SeahorseGkrItem *self, gboolean always)
-{
-	const gchar *val;
-	gchar *display;
-    
-	if (!require_item_info (self))
-		return NULL;
-	
-	display = gnome_keyring_item_info_get_display_name (self->pv->item_info);
-    
-	/* If it's customized by the application or user then display that */
-	if (is_custom_display_name (self, display))
-		return display;
-    
-	/* If it's a network item ... */
-	if (gnome_keyring_item_info_get_type (self->pv->item_info) == GNOME_KEYRING_ITEM_NETWORK_PASSWORD) {
-        
-		/* HTTP usually has a the realm as the "object" display that */
-		if (is_network_item (self, "http") && self->pv->item_attrs) {
-			val = seahorse_gkr_item_get_attribute (self, "object");
-			if (val && val[0]) {
-				g_free (display);
-				return g_strdup (val);
-			}
-		}
-        
-		/* Display the server name as a last resort */
-		if (always) {
-			val = seahorse_gkr_item_get_attribute (self, "server");
-			if (val && val[0]) {
-				g_free (display);
-				return g_strdup (val);
-			}
+static void
+free_display_info (DisplayInfo *info)
+{
+	g_free (info->label);
+	g_free (info->details);
+	g_free (info);
+}
+
+static DisplayInfo *
+ensure_display_info (SeahorseGkrItem *self)
+{
+	const gchar *item_type;
+	DisplayInfo *info;
+
+	if (self->pv->display_info)
+		return self->display_info;
+
+	info = g_new0 (DisplayInfo, 1);
+
+	item_type = map_item_type_to_string (self->pv->item_info);
+	item_type = map_item_type_to_specific (item_type);
+	g_assert (item_type != NULL);
+	info->item_type = item_type;
+
+	for (i = 0; i < G_N_ELEMENTS (DISPLAY_ENTRIES); i++) {
+		if (g_str_equal (DISPLAY_ENTRIES[i].item_type, item_type)) {
+			if (DISPLAY_ENTRIES[i].custom_func != NULL)
+				(DISPLAY_ENTRIES[i].custom_func) (&DISPLAY_ENTRIES[i],
+				                                  self->pv->item_info,
+				                                  self->pv->item_attrs,
+				                                  info);
+			if (!info->label && self->pv->item_info)
+				info->label = gnome_keyring_item_info_get_display_name (self->pv->item_info);
+			if (!info->description)
+				info->description = DISPLAY_ENTRIES[i].description;
+			if (!info->details)
+				info->details = g_strdup ("");
+			break;
 		}
 	}
-    
-	return always ? display : NULL;
+
+	self->pv->display_info = info;
+	return info;
 }
 
-static gchar*
+#if 0
+static gchar *
 calc_network_item_markup (SeahorseGkrItem *self)
 {
-	const gchar *object;
-	const gchar *user;
-	const gchar *protocol;
-	const gchar *server;
-    
-	gchar *uri = NULL;
-	gchar *display = NULL;
-	gchar *ret;
-    
-	server = seahorse_gkr_item_get_attribute (self, "server");
-	protocol = seahorse_gkr_item_get_attribute (self, "protocol");
-	object = seahorse_gkr_item_get_attribute (self, "object");
-	user = seahorse_gkr_item_get_attribute (self, "user");
-
-	if (!protocol)
-		return NULL;
-    
-	/* The object in HTTP often isn't a path at all */
-	if (is_network_item (self, "http"))
-		object = NULL;
-    
-	display = calc_display_name (self, TRUE);
-    
-	if (server && protocol) {
-		uri = g_strdup_printf ("  %s://%s%s%s/%s", 
-		                       protocol, 
-		                       user ? user : "",
-		                       user ? "@" : "",
-		                       server,
-		                       object ? object : "");
-	}
-    
-	ret = g_markup_printf_escaped ("%s<span foreground='#555555' size='small' rise='0'>%s</span>",
-	                               display, uri ? uri : "");
-	g_free (display);
-	g_free (uri);
-    
-	return ret;
 }
+#endif
 
-static gchar* 
+static gchar*
 calc_name_markup (SeahorseGkrItem *self)
 {
-	gchar *name, *markup = NULL;
-	
+	GString *result;
+	DisplayInfo *info;
+	gchar *value;
+
 	if (!require_item_info (self))
 		return NULL;
-    
-	/* Only do our special markup for network passwords */
-	if (is_network_item (self, NULL))
-        markup = calc_network_item_markup (self);
-    
-	if (!markup) {
-		name = calc_display_name (self, TRUE);
-		markup = g_markup_escape_text (name, -1);
-		g_free (name);
-	}
 
-	return markup;
-}
+	info = ensure_display_info (self);
+	result = g_string_new ("");
+	value = g_markup_escape_text (info->label, -1);
+	g_string_append (result, value);
+	g_string_append (result, "\n<span size='small' rise='0'>");
+	g_string_append (result, info->details);
+	g_string_append (result, "</span>");
 
-static gint
-calc_item_type (SeahorseGkrItem *self)
-{
-	if (!require_item_info (self))
-		return -1;
-	return gnome_keyring_item_info_get_type (self->pv->item_info);
+	return g_string_free (result, FALSE);
 }
 
 /* -----------------------------------------------------------------------------
@@ -418,31 +495,13 @@ seahorse_gkr_item_realize (SeahorseGkrItem *self)
 	gchar *display;
 	gchar *markup;
 	gchar *identifier;
-	const gchar *icon_name;
 	GIcon *icon;
 
 	display = calc_display_name (self, TRUE);
 	markup = calc_name_markup(self);
 	identifier = g_strdup_printf ("%u", self->pv->item_id);
 
-	/* We use a pointer so we don't copy the string every time */
-	switch (calc_item_type (self))
-	{
-	case GNOME_KEYRING_ITEM_GENERIC_SECRET:
-		icon_name = GNOME_STOCK_AUTHENTICATION;
-		break;
-	case GNOME_KEYRING_ITEM_NETWORK_PASSWORD:
-		icon_name = is_network_item (self, "http") ? SEAHORSE_ICON_WEBBROWSER : GTK_STOCK_NETWORK;
-		break;
-	case GNOME_KEYRING_ITEM_NOTE:
-		icon_name = GNOME_STOCK_BOOK_OPEN;
-		break;
-	default:
-		icon_name = GNOME_STOCK_BLANK;
-		break;
-	}
-
-	icon = g_themed_icon_new (icon_name);
+	icon = g_themed_icon_new (GCR_ICON_PASSWORD);
 	g_object_set (self,
 		      "label", display,
 		      "icon", icon,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]