]> git.datanom.net - vcard-parser.git/blob - example/vcard-example.c
All features added
[vcard-parser.git] / example / vcard-example.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include <stdio.h>
6 #include <glib.h>
7 #include <glib/gstdio.h>
8 #include "globals.h"
9 #include "vcard.h"
10 #include "curllib.h"
11 #include "image.h"
12
13 typedef enum {
14 GETDISPLAYNAME,
15 GETOPTIONS,
16 GETTYPE,
17 GETOBJECTS,
18 READFILE,
19 ADDOBJECT,
20 UPDATEOBJECT
21 } Actions;
22
23 typedef struct {
24 gchar* uid;
25 gchar* pwd;
26 gchar* url;
27 gboolean debug;
28 Actions action;
29 gchar* file;
30 gchar* vc;
31 } Arguments;
32
33 static void arguments_free(Arguments* a) {
34 if (a) {
35 g_free(a->pwd);
36 g_free(a->uid);
37 g_free(a->url);
38 g_free(a->file);
39 g_free(a->vc);
40 g_free(a);
41 }
42 }
43
44 static Arguments* parse_commandline(GOptionContext* opt_context, gint argc, gchar** argv) {
45 Arguments* args = g_new0(Arguments, 1);
46 static gchar* uid = NULL;
47 static gchar* pwd = NULL;
48 static gchar* url = NULL;
49 static gchar* action = NULL;
50 static gboolean debug = FALSE;
51 GError *error = NULL;
52
53 static GOptionEntry opt_entries[] = {
54 { "username", 'u', 0, G_OPTION_ARG_STRING, &uid, "Username for the account", "U"},
55 { "password", 'p', 0, G_OPTION_ARG_STRING, &pwd, "Password for the account", "P"},
56 { "url", 'l', 0, G_OPTION_ARG_STRING, &url, "URL to the server", "<http|https>://foo.bar[/collection]"},
57 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "Enable debug mode", NULL },
58 { "action", 'a', 0, G_OPTION_ARG_STRING, &action, "Request to make",
59 "GetOptions, GetDisplayname, GetType, GetObjects, ReadFile, AddObject, UpdateObject"},
60 { NULL }
61 };
62
63 g_option_context_add_main_entries (opt_context, opt_entries, NULL);
64 if (!g_option_context_parse(opt_context, &argc, &argv, &error)) {
65 g_printerr ("Error parsing options: %s\n", error->message);
66 g_error_free(error);
67 g_free(args);
68 args = NULL;
69 goto CLEAN;
70 }
71
72 if (!uid || !url) {
73 g_printerr("Missing username and/or url\n");
74 g_free(args);
75 args = NULL;
76 goto CLEAN;
77 } else {
78 args->pwd = pwd;
79 args->uid = uid;
80 args->url = url;
81 args->debug = debug;
82 if (action) {
83 if (g_ascii_strcasecmp(action, "GETDISPLAYNAME") == 0)
84 args->action = GETDISPLAYNAME;
85 else if (g_ascii_strcasecmp(action, "GETOPTIONS") == 0)
86 args->action = GETOPTIONS;
87 else if (g_ascii_strcasecmp(action, "GETTYPE") == 0)
88 args->action = GETTYPE;
89 else if (g_ascii_strcasecmp(action, "GETOBJECTS") == 0)
90 args->action = GETOBJECTS;
91 else if (g_ascii_strcasecmp(action, "READFILE") == 0)
92 args->action = READFILE;
93 else if (g_ascii_strcasecmp(action, "ADDOBJECT") == 0)
94 args->action = ADDOBJECT;
95 else if (g_ascii_strcasecmp(action, "UPDATEOBJECT") == 0)
96 args->action = UPDATEOBJECT;
97 else {
98 g_printerr("%s: Unknown action\n", action);
99 g_free(args);
100 args = NULL;
101 goto CLEAN;
102 }
103 g_free(action);
104 } else {
105 g_printerr("Missing action\n");
106 g_free(args);
107 args = NULL;
108 goto CLEAN;
109 }
110
111 if (args->action == READFILE) {
112 for (int c = argc - 1; c > 0; c--) {
113 g_free(args->file);
114 args->file = g_strdup(argv[c]);
115 }
116 }
117
118 if (args->action == ADDOBJECT || args->action == UPDATEOBJECT) {
119 GString* vc = g_string_new(NULL);
120 for (int c = 1; c < argc; c++) {
121 if (c > 1)
122 vc = g_string_append_c(vc, ' ');
123 vc = g_string_append(vc, g_strdup(argv[c]));
124 }
125 args->vc = g_string_free_and_steal(vc);
126 }
127
128 goto OK;
129 }
130
131 CLEAN:
132 if (uid)
133 g_free(uid);
134 if (pwd)
135 g_free(pwd);
136 if (url)
137 g_free(url);
138 if (action)
139 g_free(action);
140
141 OK:
142 return args;
143 }
144
145 int main(int argc, char *argv[]) {
146 GOptionContext* opt_context = g_option_context_new(NULL);
147
148 Arguments* args = parse_commandline(opt_context, argc, argv);
149 if (args) {
150
151 /*
152 if (argc > 1) {
153 for (args = 1; args < argc; args++) {
154 g_file_get_contents(argv[args], &buffer, NULL, &err);
155
156 if (err) {
157 fprintf(stderr, "%s\n", err->message);
158 g_error_free(err);
159 return 1;
160 }
161
162 GSList* list = VCard_new_from_text(buffer, VCARD_VERSION_DETECT, FALSE);
163 g_free(buffer);
164 buffer = NULL;
165 GSList* tmp = list;
166 while (tmp) {
167 VCard* vc = (VCard*) tmp->data;
168 gchar* state = VCard_get_state(vc);
169 if (g_strcmp0("OK", state) != 0)
170 fprintf(stderr, "%s\n", state);
171 g_free(state);
172
173 g_print("Search property ADR in vCard\n");
174 GSList* prop = VCard_get_property(vc, "ADR");
175 for (GSList* iter = prop; iter; iter = g_slist_next(iter)) {
176 gchar* s = (gchar*) iter->data;
177 g_print("%s\n", s);
178 g_free(s);
179 }
180 g_slist_free(prop);
181 tmp = tmp->next;
182 }
183 VCard_print_list(list);
184 g_print("\n");
185 VCard_list_free(list);
186 }
187 */
188
189 CarddavData* data = curl_carddavdata_new();
190 data->debug = args->debug;
191 data->url = g_strdup(args->url);
192 data->user = g_strdup(args->uid);
193 data->pwd = g_strdup(args->pwd);
194 data->mkcol = FALSE;
195
196 GString* buf = NULL;
197 switch (args->action) {
198 case GETOPTIONS:
199 buf = g_string_new(NULL);
200 if (curl_get_options(data, buf)) {
201 g_print("ERROR\n%s\n", buf->str);
202 } else {
203 g_print("OK\nDisplayname: %s\nMKCOL support: %s\n", data->displayname, data->mkcol ? "True" : "False");
204 }
205 g_string_free(buf, TRUE);
206 break;
207
208 case GETDISPLAYNAME:
209 buf = g_string_new(NULL);
210 if (curl_get_options(data, buf)) {
211 g_print("ERROR\n%s\n", buf->str);
212 } else {
213 g_print("OK\nDisplayname: %s\n", data->displayname);
214 }
215 g_string_free(buf, TRUE);
216 break;
217
218 case GETTYPE:
219 gboolean res = curl_get_resource_type(data);
220 if (res) {
221 g_printerr("Unautorized\n");
222 } else {
223 g_print("CollectionType: %s\n", get_resource_type_name(data->type));
224 }
225 gboolean dirty = curl_collection_dirty(data);
226 g_print("Etag: %s\nCollection dirty: %s\n", data->etag, dirty ? "YES" : "NO");
227 break;
228
229 case GETOBJECTS:
230 buf = g_string_new(NULL);
231 res = curl_get_objects(data, buf);
232 g_print("Objects:\n%s\n", res ? "ERROR": buf->str);
233 g_string_free(buf, TRUE);
234 break;
235
236 case READFILE:
237 // "/home/mir/git/vcard-parser/example/mir_book.vcf"
238 FILE* file = g_fopen(args->file, "r");
239 if (file) {
240 GSList* cards = VCard_new_from_file(file, VCARD_VERSION_DETECT, TRUE);
241 fclose(file);
242 VCard_print_list(cards);
243 g_print("\n");
244 vcard_slist_property_free(cards);
245 } else {
246 g_printerr("%s: Read error\n", args->file);
247 }
248 break;
249
250 case ADDOBJECT:
251 VCard* vcard = VCard_new();
252 VCard_set_email(vcard, EMAIL_HOME, "mir@miras.org");
253 VCard_set_email(vcard, EMAIL_WORK, "mr.it@cbs.dk");
254 VCard_set_name(vcard, "Michael", "Rasmussen");
255 VCard_set_company(vcard, "No Name");
256 VCard_set_telefon(vcard, TEL_DEFAULT, "40757616");
257 VCard_set_telefon(vcard, TEL_HOME, "40757616");
258 VCard_set_address(vcard, ADDR_HOME, "Helmer Søgårds Alle 7, st.th", NULL, "Kastrup", "2770", "Denmark");
259 VCard_set_photo_image(vcard, PHOTO_JPG, hacker_jpeg);
260
261 buf = g_string_new(NULL);
262 res = curl_set_object(data, vcard, buf);
263 gchar* output;
264 if (res) {
265 output = g_strconcat("ERROR: ", buf->str, NULL);
266 } else {
267 gchar* tmp = VCard_get_etag(vcard);
268 output = g_strconcat("ETag: ", tmp, "\n", NULL);
269 g_free(tmp);
270 }
271 g_print(output);
272 g_free(output);
273 g_string_free(buf, TRUE);
274
275 // Update
276 buf = g_string_new(NULL);;
277 VCard_set_company(vcard, "Some changed Name");
278 res = curl_update_object(data, vcard, buf);
279 if (res) {
280 output = VCard_get_etag(vcard);
281 g_print("Error updating (UID:%s", output);
282 g_free(output);
283 } else {
284 gchar* tmp = VCard_get_etag(vcard);
285 output = g_strconcat("ETag: ", tmp, "\n", NULL);
286 g_free(tmp);
287 g_print(output);
288 g_free(output);
289 }
290 g_string_free(buf, TRUE);
291
292 // Delete
293 buf = g_string_new(NULL);;
294 res = curl_delete_object(data, vcard, buf);
295 if (res) {
296 output = VCard_get_etag(vcard);
297 g_print("Error updating (UID:%s", output);
298 g_free(output);
299 } else {
300 g_print(buf->str);
301 }
302 g_string_free(buf, TRUE);
303
304 VCard_free(vcard);
305 break;
306 case UPDATEOBJECT:
307 g_print("%s\n", args->vc);
308 break;
309 }
310 arguments_free(args);
311 curl_carddavdata_free(data);
312 } else {
313 gchar* help = g_option_context_get_help(opt_context, TRUE, NULL);
314 g_print("%s\n", help);
315 g_free(help);
316 }
317 g_option_context_free(opt_context);
318
319 return 0;
320 }
This page took 0.770327 seconds and 6 git commands to generate.