]> git.datanom.net - vcard-parser.git/blobdiff - example/vcard-example.c
Begin adding support for carddav server
[vcard-parser.git] / example / vcard-example.c
index 254bca4cc31e62f4f1d2ddf2ce21a71669c9ad3b..f8f1770cd42d937a53124f5ed0b3b6d89054458a 100644 (file)
@@ -1,5 +1,151 @@
-# include <vcard.h>
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include <stdio.h>
+#include <glib.h>
+#include "globals.h"
+#include "vcard.h"
+#include "curllib.h"
+
+typedef struct {
+       gchar* uid;
+       gchar* pwd;
+       gchar* url;
+       gboolean debug;
+} Arguments;
+
+static void arguments_free(Arguments* a) {
+       if (a) {
+               g_free(a->pwd);
+               g_free(a->uid);
+               g_free(a->url);
+               g_free(a);
+       }
+}
+
+static Arguments* parse_commandline(GOptionContext* opt_context, gint argc, gchar** argv) {
+       Arguments* args = g_new0(Arguments, 1);
+       static gchar* uid = NULL;
+       static gchar* pwd = NULL;
+       static gchar* url = NULL;
+       static gboolean debug = FALSE;
+       GError *error = NULL;
+
+       static GOptionEntry opt_entries[] = {
+               { "username", 'u', 0, G_OPTION_ARG_STRING, &uid, "Username for the account", "U"},
+               { "password", 'p', 0, G_OPTION_ARG_STRING, &pwd, "Password for the account", "P"},
+               { "url", 'l', 0, G_OPTION_ARG_STRING, &url, "URL to the server", "<http|https>://foo.bar[/collection]"},
+               { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "Enable debug mode", NULL },
+               { NULL }
+       };
+
+       g_option_context_add_main_entries (opt_context, opt_entries, NULL);
+       if (!g_option_context_parse(opt_context, &argc, &argv, &error)) {
+               g_printerr ("Error parsing options: %s\n", error->message);
+               g_error_free(error);
+               g_free(args);
+               args = NULL;
+               goto CLEAN;
+    }
+
+    if (!uid || !url) {
+               g_printerr("Missing username and/or url\n");
+               g_free(args);
+               args = NULL;
+               goto CLEAN;
+       } else {
+               args->pwd = pwd;
+               args->uid = uid;
+               args->url = url;
+               args->debug = debug;
+               goto OK;
+       }
+
+       CLEAN:
+               if (uid)
+                       g_free(uid);
+               if (pwd)
+                       g_free(pwd);
+               if (url)
+                       g_free(url);
+
+       OK:
+               return args;
+}
+
+int main(int argc, char *argv[]) {
+       GOptionContext* opt_context = g_option_context_new(NULL);
+
+       Arguments* args = parse_commandline(opt_context, argc, argv);
+       if (args) {
+
+/*
+       if (argc > 1) {
+               for (args = 1; args < argc; args++) {
+                       g_file_get_contents(argv[args], &buffer, NULL, &err);
+
+                       if (err) {
+                               fprintf(stderr, "%s\n", err->message);
+                               g_error_free(err);
+                               return 1;
+                       }
+
+                       GSList* list = VCard_new_from_text(buffer, VCARD_VERSION_DETECT, FALSE);
+                       g_free(buffer);
+                       buffer = NULL;
+                       GSList* tmp = list;
+                       while (tmp) {
+                               VCard* vc = (VCard*) tmp->data;
+                               gchar* state = VCard_get_state(vc);
+                               if (g_strcmp0("OK", state) != 0)
+                                       fprintf(stderr, "%s\n", state);
+                               g_free(state);
+
+                               g_print("Search property ADR in vCard\n");
+                               GSList* prop = VCard_get_property(vc, "ADR");
+                               for (GSList* iter = prop; iter; iter = g_slist_next(iter)) {
+                                       gchar* s = (gchar*) iter->data;
+                                       g_print("%s\n", s);
+                                       g_free(s);
+                               }
+                               g_slist_free(prop);
+                               tmp = tmp->next;
+                       }
+                       VCard_print_list(list);
+                       g_print("\n");
+                       VCard_list_free(list);
+               }
+*/
+
+               CarddavData* data = g_new0(CarddavData, 1);
+               data->debug = args->debug;
+               data->url = g_strdup(args->url);
+               data->user = g_strdup(args->uid);
+               data->pwd = g_strdup(args->pwd);
+               data->mkcol = FALSE;
+               arguments_free(args);
+
+               GString* buf = g_string_new(NULL);
+               if (curl_get_options(data, buf)) {
+                       g_print("ERROR\n%s\n", buf->str);
+               } else {
+                       g_print("OK\nDisplayname: %s\nMKCOL support: %s\n", data->displayname, data->mkcol ? "True" : "False");
+               }
+               g_string_free(buf, TRUE);
+               gboolean res = curl_get_resource_type(data);
+               if (res) {
+                       g_printerr("Unautorized\n");
+               } else {
+                       g_print("CollectionType: %s\n", get_resource_type_name(data->type));
+               }
+               curl_free_CarddavData(data);
+       } else {
+               gchar* help = g_option_context_get_help(opt_context, TRUE, NULL);
+               g_print("%s\n", help);
+               g_free(help);
+       }
+       g_option_context_free(opt_context);
 
-int main() {
     return 0;
 }
This page took 0.519049 seconds and 5 git commands to generate.