]> git.datanom.net - vcard-parser.git/blobdiff - example/vcard-example.c
All features added
[vcard-parser.git] / example / vcard-example.c
index 254bca4cc31e62f4f1d2ddf2ce21a71669c9ad3b..68962bacd8cb016cbf09a8ef6c80d463e4f1b492 100644 (file)
@@ -1,5 +1,320 @@
-# include <vcard.h>
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include <stdio.h>
+#include <glib.h>
+#include <glib/gstdio.h>
+#include "globals.h"
+#include "vcard.h"
+#include "curllib.h"
+#include "image.h"
+
+typedef enum {
+       GETDISPLAYNAME,
+       GETOPTIONS,
+       GETTYPE,
+       GETOBJECTS,
+       READFILE,
+       ADDOBJECT,
+       UPDATEOBJECT
+} Actions;
+
+typedef struct {
+       gchar* uid;
+       gchar* pwd;
+       gchar* url;
+       gboolean debug;
+       Actions action;
+       gchar* file;
+       gchar* vc;
+} Arguments;
+
+static void arguments_free(Arguments* a) {
+       if (a) {
+               g_free(a->pwd);
+               g_free(a->uid);
+               g_free(a->url);
+               g_free(a->file);
+               g_free(a->vc);
+               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 gchar* action = 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 },
+               { "action", 'a', 0, G_OPTION_ARG_STRING, &action, "Request to make",
+                       "GetOptions, GetDisplayname, GetType, GetObjects, ReadFile, AddObject, UpdateObject"},
+               { 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;
+               if (action) {
+                       if (g_ascii_strcasecmp(action, "GETDISPLAYNAME") == 0)
+                               args->action = GETDISPLAYNAME;
+                       else if (g_ascii_strcasecmp(action, "GETOPTIONS") == 0)
+                               args->action = GETOPTIONS;
+                       else if (g_ascii_strcasecmp(action, "GETTYPE") == 0)
+                               args->action = GETTYPE;
+                       else if (g_ascii_strcasecmp(action, "GETOBJECTS") == 0)
+                               args->action = GETOBJECTS;
+                       else if (g_ascii_strcasecmp(action, "READFILE") == 0)
+                               args->action = READFILE;
+                       else if (g_ascii_strcasecmp(action, "ADDOBJECT") == 0)
+                               args->action = ADDOBJECT;
+                       else if (g_ascii_strcasecmp(action, "UPDATEOBJECT") == 0)
+                               args->action = UPDATEOBJECT;
+                       else {
+                               g_printerr("%s: Unknown action\n", action);
+                               g_free(args);
+                               args = NULL;
+                               goto CLEAN;
+                       }
+                       g_free(action);
+               } else {
+                       g_printerr("Missing action\n");
+                       g_free(args);
+                       args = NULL;
+                       goto CLEAN;
+               }
+
+               if (args->action == READFILE) {
+                       for (int c = argc - 1; c > 0; c--) {
+                               g_free(args->file);
+                               args->file = g_strdup(argv[c]);
+                       }
+               }
+
+               if (args->action == ADDOBJECT || args->action == UPDATEOBJECT) {
+                       GString* vc = g_string_new(NULL);
+                       for (int c = 1; c < argc; c++) {
+                               if (c > 1)
+                                       vc = g_string_append_c(vc, ' ');
+                               vc = g_string_append(vc, g_strdup(argv[c]));
+                       }
+                       args->vc = g_string_free_and_steal(vc);
+               }
+
+               goto OK;
+       }
+
+       CLEAN:
+               if (uid)
+                       g_free(uid);
+               if (pwd)
+                       g_free(pwd);
+               if (url)
+                       g_free(url);
+               if (action)
+                       g_free(action);
+
+       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 = curl_carddavdata_new();
+               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;
+
+               GString* buf = NULL;
+               switch (args->action) {
+                       case GETOPTIONS:
+                               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);
+                               break;
+
+                       case GETDISPLAYNAME:
+                               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\n", data->displayname);
+                               }
+                               g_string_free(buf, TRUE);
+                               break;
+
+                       case GETTYPE:
+                               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));
+                               }
+                               gboolean dirty = curl_collection_dirty(data);
+                               g_print("Etag: %s\nCollection dirty: %s\n", data->etag, dirty ? "YES" : "NO");
+                               break;
+
+                       case GETOBJECTS:
+                               buf = g_string_new(NULL);
+                               res = curl_get_objects(data, buf);
+                               g_print("Objects:\n%s\n", res ? "ERROR": buf->str);
+                               g_string_free(buf, TRUE);
+                               break;
+
+                       case READFILE:
+                               // "/home/mir/git/vcard-parser/example/mir_book.vcf"
+                               FILE* file = g_fopen(args->file, "r");
+                               if (file) {
+                                       GSList* cards = VCard_new_from_file(file, VCARD_VERSION_DETECT, TRUE);
+                                       fclose(file);
+                                       VCard_print_list(cards);
+                                       g_print("\n");
+                                       vcard_slist_property_free(cards);
+                               } else {
+                                       g_printerr("%s: Read error\n", args->file);
+                               }
+                               break;
+
+                       case ADDOBJECT:
+                               VCard* vcard = VCard_new();
+                               VCard_set_email(vcard, EMAIL_HOME, "mir@miras.org");
+                               VCard_set_email(vcard, EMAIL_WORK, "mr.it@cbs.dk");
+                               VCard_set_name(vcard, "Michael", "Rasmussen");
+                               VCard_set_company(vcard, "No Name");
+                               VCard_set_telefon(vcard, TEL_DEFAULT, "40757616");
+                               VCard_set_telefon(vcard, TEL_HOME, "40757616");
+                               VCard_set_address(vcard, ADDR_HOME, "Helmer Søgårds Alle 7, st.th", NULL, "Kastrup", "2770", "Denmark");
+                               VCard_set_photo_image(vcard, PHOTO_JPG, hacker_jpeg);
+
+                               buf = g_string_new(NULL);
+                               res = curl_set_object(data, vcard,  buf);
+                               gchar* output;
+                               if (res) {
+                                       output = g_strconcat("ERROR: ", buf->str, NULL);
+                               } else {
+                                       gchar* tmp = VCard_get_etag(vcard);
+                                       output = g_strconcat("ETag: ", tmp, "\n", NULL);
+                                       g_free(tmp);
+                               }
+                               g_print(output);
+                               g_free(output);
+                               g_string_free(buf, TRUE);
+
+                               // Update
+                               buf = g_string_new(NULL);;
+                               VCard_set_company(vcard, "Some changed Name");
+                               res = curl_update_object(data, vcard,  buf);
+                               if (res) {
+                                       output = VCard_get_etag(vcard);
+                                       g_print("Error updating (UID:%s", output);
+                                       g_free(output);
+                               } else {
+                                       gchar* tmp = VCard_get_etag(vcard);
+                                       output = g_strconcat("ETag: ", tmp, "\n", NULL);
+                                       g_free(tmp);
+                                       g_print(output);
+                                       g_free(output);
+                               }
+                               g_string_free(buf, TRUE);
+
+                               // Delete
+                               buf = g_string_new(NULL);;
+                               res = curl_delete_object(data, vcard,  buf);
+                               if (res) {
+                                       output = VCard_get_etag(vcard);
+                                       g_print("Error updating (UID:%s", output);
+                                       g_free(output);
+                               } else {
+                                       g_print(buf->str);
+                               }
+                               g_string_free(buf, TRUE);
+
+                               VCard_free(vcard);
+                               break;
+                       case UPDATEOBJECT:
+                               g_print("%s\n", args->vc);
+                               break;
+               }
+               arguments_free(args);
+               curl_carddavdata_free(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.582579 seconds and 5 git commands to generate.