]> git.datanom.net - vcard-parser.git/blob - example/vcard-example.c
Begin adding support for carddav server
[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 "globals.h"
8 #include "vcard.h"
9 #include "curllib.h"
10
11 typedef struct {
12 gchar* uid;
13 gchar* pwd;
14 gchar* url;
15 gboolean debug;
16 } Arguments;
17
18 static void arguments_free(Arguments* a) {
19 if (a) {
20 g_free(a->pwd);
21 g_free(a->uid);
22 g_free(a->url);
23 g_free(a);
24 }
25 }
26
27 static Arguments* parse_commandline(GOptionContext* opt_context, gint argc, gchar** argv) {
28 Arguments* args = g_new0(Arguments, 1);
29 static gchar* uid = NULL;
30 static gchar* pwd = NULL;
31 static gchar* url = NULL;
32 static gboolean debug = FALSE;
33 GError *error = NULL;
34
35 static GOptionEntry opt_entries[] = {
36 { "username", 'u', 0, G_OPTION_ARG_STRING, &uid, "Username for the account", "U"},
37 { "password", 'p', 0, G_OPTION_ARG_STRING, &pwd, "Password for the account", "P"},
38 { "url", 'l', 0, G_OPTION_ARG_STRING, &url, "URL to the server", "<http|https>://foo.bar[/collection]"},
39 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "Enable debug mode", NULL },
40 { NULL }
41 };
42
43 g_option_context_add_main_entries (opt_context, opt_entries, NULL);
44 if (!g_option_context_parse(opt_context, &argc, &argv, &error)) {
45 g_printerr ("Error parsing options: %s\n", error->message);
46 g_error_free(error);
47 g_free(args);
48 args = NULL;
49 goto CLEAN;
50 }
51
52 if (!uid || !url) {
53 g_printerr("Missing username and/or url\n");
54 g_free(args);
55 args = NULL;
56 goto CLEAN;
57 } else {
58 args->pwd = pwd;
59 args->uid = uid;
60 args->url = url;
61 args->debug = debug;
62 goto OK;
63 }
64
65 CLEAN:
66 if (uid)
67 g_free(uid);
68 if (pwd)
69 g_free(pwd);
70 if (url)
71 g_free(url);
72
73 OK:
74 return args;
75 }
76
77 int main(int argc, char *argv[]) {
78 GOptionContext* opt_context = g_option_context_new(NULL);
79
80 Arguments* args = parse_commandline(opt_context, argc, argv);
81 if (args) {
82
83 /*
84 if (argc > 1) {
85 for (args = 1; args < argc; args++) {
86 g_file_get_contents(argv[args], &buffer, NULL, &err);
87
88 if (err) {
89 fprintf(stderr, "%s\n", err->message);
90 g_error_free(err);
91 return 1;
92 }
93
94 GSList* list = VCard_new_from_text(buffer, VCARD_VERSION_DETECT, FALSE);
95 g_free(buffer);
96 buffer = NULL;
97 GSList* tmp = list;
98 while (tmp) {
99 VCard* vc = (VCard*) tmp->data;
100 gchar* state = VCard_get_state(vc);
101 if (g_strcmp0("OK", state) != 0)
102 fprintf(stderr, "%s\n", state);
103 g_free(state);
104
105 g_print("Search property ADR in vCard\n");
106 GSList* prop = VCard_get_property(vc, "ADR");
107 for (GSList* iter = prop; iter; iter = g_slist_next(iter)) {
108 gchar* s = (gchar*) iter->data;
109 g_print("%s\n", s);
110 g_free(s);
111 }
112 g_slist_free(prop);
113 tmp = tmp->next;
114 }
115 VCard_print_list(list);
116 g_print("\n");
117 VCard_list_free(list);
118 }
119 */
120
121 CarddavData* data = g_new0(CarddavData, 1);
122 data->debug = args->debug;
123 data->url = g_strdup(args->url);
124 data->user = g_strdup(args->uid);
125 data->pwd = g_strdup(args->pwd);
126 data->mkcol = FALSE;
127 arguments_free(args);
128
129 GString* buf = g_string_new(NULL);
130 if (curl_get_options(data, buf)) {
131 g_print("ERROR\n%s\n", buf->str);
132 } else {
133 g_print("OK\nDisplayname: %s\nMKCOL support: %s\n", data->displayname, data->mkcol ? "True" : "False");
134 }
135 g_string_free(buf, TRUE);
136 gboolean res = curl_get_resource_type(data);
137 if (res) {
138 g_printerr("Unautorized\n");
139 } else {
140 g_print("CollectionType: %s\n", get_resource_type_name(data->type));
141 }
142 curl_free_CarddavData(data);
143 } else {
144 gchar* help = g_option_context_get_help(opt_context, TRUE, NULL);
145 g_print("%s\n", help);
146 g_free(help);
147 }
148 g_option_context_free(opt_context);
149
150 return 0;
151 }
This page took 0.255811 seconds and 6 git commands to generate.