CASA/c_adlib/ad_gk/native/ad_gk.c
2005-12-16 10:29:25 +00:00

388 lines
12 KiB
C

#include "ad_gk.h"
GMainLoop *loop = NULL;
void ListKeyringsCb (GnomeKeyringResult result,
GList *keyrings,
gpointer data)
{
GList *l = NULL;
char *name = NULL;
GList **retList = data;
GetKeyringsCbData *cbData = data;
GMainLoop *loop = cbData->loop;
retList = cbData->keyringList;
*retList = NULL;
if (result != GNOME_KEYRING_RESULT_OK)
{
g_print ("Unable to get keyring list - %d\n", result);
}
else
{
for (l = keyrings; l != NULL; l = l->next)
{
name = l->data;
*retList = g_list_append (*retList, g_strdup (name));
}
}
g_main_loop_quit (loop);
}
void KeyringGetInfoCb(GnomeKeyringResult result,
GnomeKeyringInfo *info,
gpointer data)
{
GetKeyringInfoCbData *cbData = data;
KeyringInfo *retInfo = cbData->info;
if (result != GNOME_KEYRING_RESULT_OK)
{
g_print ("Unable to get keyring info %d\n", result);
}
else
{
retInfo->lockOnIdle = gnome_keyring_info_get_lock_on_idle(info);
retInfo->lockTimeout = gnome_keyring_info_get_lock_timeout(info);
retInfo->mTime = gnome_keyring_info_get_mtime(info);
retInfo->cTime = gnome_keyring_info_get_ctime(info);
retInfo->isLocked = gnome_keyring_info_get_is_locked(info);
}
g_main_loop_quit (cbData->loop);
}
void ListItemCb( GnomeKeyringResult result,
GList *list,
gpointer data)
{
GList **retList;
GetItemsCbData *cbData = data;
retList = cbData->itemList;
*retList = NULL;
if (result != GNOME_KEYRING_RESULT_OK)
{
g_print ("Unable to get list of items : %d\n", result);
}
else
{
*retList = g_list_copy (list);
}
g_main_loop_quit (cbData->loop);
}
void ItemGetInfoCb( GnomeKeyringResult result,
GnomeKeyringItemInfo *info,
gpointer data)
{
GetItemInfoCbData *cbData = data;
ItemInfo *itemInfo = cbData->info;
if (result != GNOME_KEYRING_RESULT_OK)
{
g_print ("Unable to get Item info: %d\n", result);
}
else
{
itemInfo->itemType = gnome_keyring_item_info_get_type(info);
strcpy(itemInfo->displayName,gnome_keyring_item_info_get_display_name(info));
strcpy(itemInfo->secret,gnome_keyring_item_info_get_secret(info));
itemInfo->mTime = gnome_keyring_item_info_get_mtime(info);
itemInfo->cTime = gnome_keyring_item_info_get_ctime(info);
}
g_main_loop_quit (cbData->loop);
}
void ItemGetAttributesCb(GnomeKeyringResult result,
GnomeKeyringAttributeList *attributes,
gpointer data)
{
GnomeKeyringAttribute *attrList = NULL;
int i = 0;
GetAttributeListCbData *cbData = data;
Attribute *attr = NULL;
GList **retList;
retList = cbData->attrList;
*retList = NULL;
if( result != GNOME_KEYRING_RESULT_OK )
{
g_print("Unable to get the attributes of item\n");
}
else
{
attrList = (GnomeKeyringAttribute*)(attributes->data); //GArray has len and data
for(i = 0; i < attributes->len; i++ )
{
if(attrList[i].type == GNOME_KEYRING_ATTRIBUTE_TYPE_STRING)
{
attr = (Attribute*)malloc(sizeof(Attribute));
if( NULL != attr )
{
memset(attr,0,sizeof(Attribute));
attr->type = 0;
attr->key = (char*)malloc(KEY_SIZE);
if(attr->key != NULL)
strcpy(attr->key,attrList[i].name);
attr->value = (char*)malloc(VAL_SIZE);
if(attr->value != NULL)
strcpy(attr->value,attrList[i].value.string);
}
}
else if(attrList[i].type == GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32)
{
attr = (Attribute*)malloc(sizeof(Attribute));
if( NULL != attr )
{
memset(attr,0,sizeof(Attribute));
attr->type = 0;
attr->key = (char*)malloc(KEY_SIZE);
if(attr->key != NULL)
strcpy(attr->key,attrList[i].name);
attr->value = (char*)malloc(VAL_SIZE);
if(attr->value != NULL)
sprintf(attr->value,"%d",attrList[i].value.integer);
}
}
*retList = g_list_append (*retList, attr);
}
}
g_main_loop_quit (cbData->loop);
}
int GetKeyrings(GList **keyringList)
{
GList *l = NULL;
GetKeyringsCbData cbData;
cbData.loop = g_main_loop_new (NULL, FALSE);
cbData.keyringList = keyringList;
gnome_keyring_list_keyring_names(ListKeyringsCb,&cbData,NULL);
g_main_loop_run(cbData.loop);
return SUCCESS;
}
int GetKeyringInfo(char *keyring,KeyringInfo *info)
{
GetKeyringInfoCbData cbData;
cbData.loop = g_main_loop_new (NULL, FALSE);
cbData.info = info;
gnome_keyring_get_info(keyring, KeyringGetInfoCb,&cbData, NULL);
g_main_loop_run (cbData.loop);
return SUCCESS;
}
int GetItems(char *keyring, GList **itemList)
{
GList *l = NULL;
gint itemId;
GetItemsCbData cbData;
cbData.loop = g_main_loop_new (NULL, FALSE);
cbData.itemList = itemList;
gnome_keyring_list_item_ids(keyring, ListItemCb, &cbData, NULL);
g_main_loop_run (cbData.loop);
return SUCCESS;
}
int GetItemInfo(char *keyring, int itemId, ItemInfo *info)
{
GetItemInfoCbData cbData;
cbData.loop = g_main_loop_new (NULL, FALSE);
cbData.info = info;
gnome_keyring_item_get_info (keyring,itemId, ItemGetInfoCb,
&cbData, NULL);
g_main_loop_run (cbData.loop);
return SUCCESS;
}
int GetAttributeList(char *keyring, int itemId, GList **attrList)
{
int i = 0;
GetAttributeListCbData cbData;
cbData.loop = g_main_loop_new (NULL, FALSE);
cbData.attrList = attrList;
gnome_keyring_item_get_attributes (keyring, itemId,
ItemGetAttributesCb, &cbData,
NULL);
g_main_loop_run (cbData.loop);
return SUCCESS;
}
int FreeAttributeList(GList *attrList)
{
GList *l;
Attribute *attr = NULL;
for(l = attrList; l != NULL; l = l->next)
{
attr = (Attribute*)(l->data);
if(attr->key)
{
free(attr->key);
}
if(attr->value)
{
free(attr->value);
}
}
g_list_free(attrList);
return SUCCESS;
}
static void
OperationCompletedCb (GnomeKeyringResult result,
gpointer data)
{
OperationCompleted *cbData = (OperationCompleted *)data;
g_print ("ad_gk.c : Operation %s Completed %d\n", cbData->OperationName, result);
cbData->result = result;
g_main_loop_quit (loop);
}
static void
CreateItemCb (GnomeKeyringResult result,
guint32 id,
gpointer data)
{
OperationCompleted *cbData = (OperationCompleted *)data;
g_print ("ad_gk.c : CreateItemCb : created item: res: %d id: %d\n", result, id);
if (result != GNOME_KEYRING_RESULT_OK)
{
g_print ("ad_gk.c : CreateItemCb : Unable to create item : %d\n", result);
}
cbData->result = result;
g_main_loop_quit (loop);
}
int
SetItemAttributes (char *keyring, guint32 itemid, Attribute **attrs, int length)
{
GnomeKeyringAttributeList *attributes;
GnomeKeyringAttribute attribute;
OperationCompleted cbData;
int i;
printf("ad_gk.c : In SetItemAttributes\n");
printf("ad_gk.c : Keyring %s, itemid %d\n",keyring,itemid);
cbData.OperationName = "Set Item Attributes";
loop = g_main_loop_new (NULL, FALSE);
attributes = gnome_keyring_attribute_list_new ();
for (i=0; i< length; i++)
{
printf("ad_gk.c : In key %s \n", attrs[i]->key);
attribute.name = g_strdup (attrs[i]->key);
attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
attribute.value.string = g_strdup (attrs[i]->value);
g_array_append_val (attributes, attribute);
}
gnome_keyring_item_set_attributes (keyring, itemid, attributes,
OperationCompletedCb, &cbData, NULL);
gnome_keyring_attribute_list_free (attributes);
g_main_loop_run (loop);
return cbData.result;
}
int
RemoveItem (char *keyring, guint32 itemid)
{
OperationCompleted cbData;
loop = g_main_loop_new (NULL, FALSE);
cbData.OperationName = "Remove Item";
gnome_keyring_item_delete (keyring, itemid,
OperationCompletedCb, &cbData, NULL);
g_main_loop_run (loop);
return cbData.result;
}
int
SetPassword (char *keyring, guint32 itemid, char *secret)
{
GnomeKeyringItemInfo *info;
OperationCompleted cbData;
loop = g_main_loop_new (NULL, FALSE);
cbData.OperationName = "Set Item Secret";
info = gnome_keyring_item_info_new ();
gnome_keyring_item_info_set_secret (info, secret);
gnome_keyring_item_set_info (keyring, itemid, info,
OperationCompletedCb, &cbData, NULL);
gnome_keyring_item_info_free (info);
g_main_loop_run (loop);
return cbData.result;
}
int
CreateItem(char *keyring, int32_t itemType, char *display_name, char *secret, Attribute **attrs, int attrcnt)
{
int ret;
ret = CreateItemInKeyring(keyring,itemType,display_name,secret,attrs,attrcnt);
if (ret == 4)
{
ret = CreateKeyring(keyring);
if (ret != 0)
{
return GNOME_KEYRING_RESULT_CANNOT_CREATE_KEYRING ;
}
ret = CreateItemInKeyring(keyring,itemType,display_name ,secret,attrs,attrcnt);
return ret;
}
return ret;
}
static int
CreateItemInKeyring(char *keyring, int32_t itemType, char *display_name, char *secret, Attribute **attrs, int attrcnt)
{
GnomeKeyringAttributeList *attributes;
GnomeKeyringAttribute attribute;
OperationCompleted cbData;
int i;
printf("ad:gk.c :In CreateItemInKeyring\n");
printf("ad.gk.c : CreateItemInKeyring : Keyring %s, itemType %d displayname %s, secret %s \n",keyring,itemType, display_name,secret);
cbData.OperationName = "Create Item";
loop = g_main_loop_new (NULL, FALSE);
attributes = gnome_keyring_attribute_list_new ();
for (i=0; i< attrcnt; i++)
{
printf("as.gk.c : CreateItemInKeyring : In key %s \n", attrs[i]->key);
attribute.name = g_strdup (attrs[i]->key);
attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
attribute.value.string = g_strdup (attrs[i]->value);
g_array_append_val (attributes, attribute);
}
gnome_keyring_item_create(keyring,itemType,display_name,attributes,secret,FALSE,CreateItemCb,&cbData,NULL);
gnome_keyring_attribute_list_free (attributes);
g_main_loop_run (loop);
return cbData.result;
}
static int
CreateKeyring(char *keyring)
{
OperationCompleted cbData;
cbData.OperationName = "Create Keyring";
loop = g_main_loop_new (NULL, FALSE);
gnome_keyring_create(keyring,NULL,OperationCompletedCb,&cbData,NULL);
g_main_loop_run(loop);
return cbData.result;
}