]> 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 43e5b2d22fae20abe850222308fc3522ec9f4a2b..f8f1770cd42d937a53124f5ed0b3b6d89054458a 100644 (file)
@@ -4,14 +4,83 @@
 
 #include <stdio.h>
 #include <glib.h>
-#include <globals.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[]) {
-       GError* err = NULL;
-    gchar* buffer = NULL;
-    int args;
+       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);
@@ -47,10 +116,36 @@ int main(int argc, char *argv[]) {
                        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 {
-               g_print("Mising file argument(s)\n");
-               g_print("Usage: %s file [file ..]\n", argv[0]);
+               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);
 
     return 0;
 }
This page took 0.278806 seconds and 5 git commands to generate.