/*
* PSIP - A lightweight GTK GUI for pjsip
* (C) James Budiono 2011
* License: GNU GPL Version 3
*
* All the config-related functions are in this file.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "cJSON.h"
#include "psip.h"
/* Parse text to JSON, then render back to text, and print! */
void doit(char *text)
{
char *out;cJSON *json;
json=cJSON_Parse(text);
if (!json) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}
else
{
out=cJSON_Print(json);
cJSON_Delete(json);
printf("%s\n",out);
free(out);
}
}
//stub:
void load_config() {
FILE *f=fopen(CONFIG_FILE,"rb");
if (!f) return;
fseek(f,0,SEEK_END);long len=ftell(f);fseek(f,0,SEEK_SET);
char *data=malloc(len+1);fread(data,1,len,f);fclose(f);
//doit(data);
cJSON *root = cJSON_Parse (data);
//read accounts
cJSON *account = cJSON_GetObjectItem (root, "account");
gtk_entry_set_text (psip_state->account_sip_url_field, cJSON_GetObjectItem(account, "sip-url")->valuestring);
gtk_entry_set_text (psip_state->account_registrar_field, cJSON_GetObjectItem(account, "registrar")->valuestring);
gtk_entry_set_text (psip_state->account_realm_field, cJSON_GetObjectItem(account, "realm")->valuestring);
gtk_entry_set_text (psip_state->account_user_field, cJSON_GetObjectItem(account, "user")->valuestring);
gtk_entry_set_text (psip_state->account_password_field, cJSON_GetObjectItem(account, "password")->valuestring);
//read buddies
cJSON *buddies = cJSON_GetObjectItem (root, "buddies");
int i, no_of_buddies = cJSON_GetArraySize (buddies);
for (i=0; i<no_of_buddies; i++) {
cJSON *buddy = cJSON_GetArrayItem (buddies, i);
buddylist_add (
cJSON_GetObjectItem(buddy, "nick")->valuestring,
cJSON_GetObjectItem(buddy, "address")->valuestring
);
}
g_object_ref(psip_state->buddylist); //make sure these objects are not destroyed until we say so
cJSON_Delete(root);
free(data);
}
//stub:
void save_config() {
cJSON *root, *account, *buddies;
FILE *f=fopen(CONFIG_FILE,"wb");
if (!f) return;
//create root objects
root = cJSON_CreateObject();
cJSON_AddItemToObject (root, "account", account = cJSON_CreateObject());
cJSON_AddItemToObject (root, "buddies", buddies = cJSON_CreateArray());
//save account
cJSON_AddStringToObject (account, "sip-url", gtk_entry_get_text(psip_state->account_sip_url_field));
cJSON_AddStringToObject (account, "registrar", gtk_entry_get_text(psip_state->account_registrar_field));
cJSON_AddStringToObject (account, "realm", gtk_entry_get_text(psip_state->account_realm_field));
cJSON_AddStringToObject (account, "user", gtk_entry_get_text(psip_state->account_user_field));
cJSON_AddStringToObject (account, "password", gtk_entry_get_text(psip_state->account_password_field));
//save buddies
GtkTreeIter i;
gchararray nick, address;
cJSON *buddy;
gboolean ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (psip_state->buddylist), &i);
while (ok) {
gtk_tree_model_get (GTK_TREE_MODEL (psip_state->buddylist), &i, 0, &nick, 1, &address, -1);
//g_print("%s %s\n", nick, address);
cJSON_AddItemToArray (buddies, buddy = cJSON_CreateObject() );
cJSON_AddStringToObject (buddy,"nick", nick);
cJSON_AddStringToObject (buddy,"address", address);
g_free(nick);
g_free(address);
ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (psip_state->buddylist), &i);
}
g_object_unref (psip_state->buddylist);
/*
int i;
cJSON *buddy;
for (i=0; i<10; i++) {
cJSON_AddItemToArray (buddies, buddy = cJSON_CreateObject() );
cJSON_AddStringToObject (buddy,"nick", g_strdup_printf("user%d", i));
cJSON_AddStringToObject (buddy,"address", g_strdup_printf("sip:user%d@iptel.org", i));
}
*/
//save file
char *out = cJSON_Print (root);
//g_print(out);
fwrite (out,1,strlen(out),f);
free(out);
cJSON_Delete (root);
fclose(f);
}