PSIP
Check-in [0f225404ba]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:working save config
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0f225404ba3f3f85aecf88b7acda66edc028e97d
User & Date: james 2011-09-13 14:48:39.237
Context
2011-09-13
14:54
Makefile now has a clean target check-in: 47aad44d5c user: james tags: trunk
14:48
working save config check-in: 0f225404ba user: james tags: trunk
13:18
working load config check-in: 9cac6f87fd user: james tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to config.c.
1
2
3
4
5
6
7
8
9

10
11
12
13
14
15
16
/*
 * 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 <stdio.h>
#include "cJSON.h"
#include "psip.h"

/* Parse text to JSON, then render back to text, and print! */
void doit(char *text)
{









>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * 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)
{
51
52
53
54
55
56
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82




83




84











85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
	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
		);
	}

	
	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", "sip:xxx@iptel.org");
	cJSON_AddStringToObject (account, "registrar", "sip:iptel.org");
	cJSON_AddStringToObject (account, "realm", "*");
	cJSON_AddStringToObject (account, "user", "xxx");
	cJSON_AddStringToObject (account, "password", "xxxxxx");










	//save buddies











	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);
}








>



















|
|
|
|
|

>
>
>
>

>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>







|










52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
	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);
}

Changes to psip.c.
261
262
263
264
265
266
267
268

269
270
271
272
273
274
275
276
277
278

279

280
281
282
283
284
285
 
    /* Connect signals */
    gtk_builder_connect_signals( builder, NULL);
 
    /* Destroy builder, since we don't need it anymore */
    g_object_unref( G_OBJECT( builder ) );

	/* === test === */

	clear_buddies();
	load_config();
  
    /* Show window. All other widgets are automatically shown by GtkBuilder */
    gtk_widget_show( window );
 
    /* Start main loop */
    gdk_threads_enter();
    gtk_main();
    gdk_threads_leave();

    save_config();

 
	g_slice_free(psip_state_struct, psip_state);
	g_print( "\nDone.\n");
    return( 0 );
}








|
>







<

|
>

>
|





261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

277
278
279
280
281
282
283
284
285
286
287
 
    /* Connect signals */
    gtk_builder_connect_signals( builder, NULL);
 
    /* Destroy builder, since we don't need it anymore */
    g_object_unref( G_OBJECT( builder ) );

	/* init system */
    gdk_threads_enter();	
	clear_buddies();
	load_config();
  
    /* Show window. All other widgets are automatically shown by GtkBuilder */
    gtk_widget_show( window );
 
    /* Start main loop */

    gtk_main();
    
    /* done */
    save_config();
    gdk_threads_leave();
     
	g_slice_free(psip_state_struct, psip_state);
	g_print( "\nDone.\n");
    return( 0 );
}

Changes to psip.conf.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
	"account":	{
		"sip-url":	"sip:xxx@iptel.org",
		"registrar":	"sip:iptel.org",
		"realm":	"*",
		"user":	"xxx",
		"password":	"xxxxxx"
	},
	"buddies":	[{
			"nick":	"user0",
			"address":	"sip:user0@iptel.org"
		}, {
			"nick":	"user1",
			"address":	"sip:user1@iptel.org"
		}, {
			"nick":	"user2",
			"address":	"sip:user2@iptel.org"
		}, {
			"nick":	"user3",
			"address":	"sip:user3@iptel.org"
		}, {
			"nick":	"user4",
			"address":	"sip:user4@iptel.org"
		}, {
			"nick":	"user5",
			"address":	"sip:user5@iptel.org"
		}, {
			"nick":	"user6",
			"address":	"sip:user6@iptel.org"
		}, {
			"nick":	"user7",
			"address":	"sip:user7@iptel.org"
		}, {
			"nick":	"user8",
			"address":	"sip:user8@iptel.org"
		}, {
			"nick":	"user9",
			"address":	"sip:user9@iptel.org"
		}]
}


|

|
|
|


|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


1
2
3
4
5
6
7
8
9
10
11



























12
13
{
	"account":	{
		"sip-url":	"sip:yourname@iptel.org",
		"registrar":	"sip:iptel.org",
		"realm":	"iptel.org",
		"user":	"yourname",
		"password":	"yourpassword"
	},
	"buddies":	[{
			"nick":	"aaa",
			"address":	"bbb"



























		}]
}