]> git.sur5r.net Git - glabels/blobdiff - src/merge-vcard.c
Imported Upstream version 3.0.0
[glabels] / src / merge-vcard.c
index c0eb1449667bc9184f1c2c01f52c2d8ff207f3ca..da1deff300b7713b2a876fadf009eb8bcf3f53b1 100644 (file)
@@ -1,27 +1,23 @@
-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
-
 /*
- *  (GLABELS) Label and Business Card Creation program for GNOME
- *
- *  merge_vcard.c:  vcard merge backend module
- *
- *  Copyright (C) 2001  Jim Evins <evins@snaught.com>.
+ *  merge-vcard.c
+ *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
  *  and
  *  Copyright (C) 2005  Austin Henry <ahenry@users.sourceforge.net>
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  This file is part of gLabels.
+ *
+ *  gLabels is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
- *  This program is distributed in the hope that it will be useful,
+ *  gLabels is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <config.h>
 
 
 #include "merge-vcard.h"
-#include <libebook/e-contact.h>
 
+#include <libebook/e-contact.h>
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
 
 #include "debug.h"
 
+
 /*===========================================*/
 /* Private types                             */
 /*===========================================*/
@@ -51,9 +48,10 @@ enum {
 };
 
 enum {
-        ARG_0,
+        ARG_0
 };
 
+
 /*===========================================*/
 /* Private globals                           */
 /*===========================================*/
@@ -75,21 +73,21 @@ static void           gl_merge_vcard_get_property    (GObject          *object,
                                                       GValue           *value,
                                                       GParamSpec       *pspec);
 
-static GList         *gl_merge_vcard_get_key_list    (glMerge          *merge);
-static gchar         *gl_merge_vcard_get_primary_key (glMerge          *merge);
+static GList         *gl_merge_vcard_get_key_list    (const glMerge    *merge);
+static gchar         *gl_merge_vcard_get_primary_key (const glMerge    *merge);
 static void           gl_merge_vcard_open            (glMerge          *merge);
 static void           gl_merge_vcard_close           (glMerge          *merge);
 static glMergeRecord *gl_merge_vcard_get_record      (glMerge          *merge);
 static void           gl_merge_vcard_copy            (glMerge          *dst_merge,
-                                                      glMerge          *src_merge);
+                                                      const glMerge    *src_merge);
 static char *         parse_next_vcard               (FILE             *fp);
 
 
-\f
 /*****************************************************************************/
 /* Boilerplate object stuff.                                                 */
 /*****************************************************************************/
-G_DEFINE_TYPE (glMergeVCard, gl_merge_vcard, GL_TYPE_MERGE);
+G_DEFINE_TYPE (glMergeVCard, gl_merge_vcard, GL_TYPE_MERGE)
+
 
 static void
 gl_merge_vcard_class_init (glMergeVCardClass *class)
@@ -116,6 +114,7 @@ gl_merge_vcard_class_init (glMergeVCardClass *class)
         gl_debug (DEBUG_MERGE, "END");
 }
 
+
 static void
 gl_merge_vcard_init (glMergeVCard *merge_vcard)
 {
@@ -126,6 +125,7 @@ gl_merge_vcard_init (glMergeVCard *merge_vcard)
         gl_debug (DEBUG_MERGE, "END");
 }
 
+
 static void
 gl_merge_vcard_finalize (GObject *object)
 {
@@ -142,6 +142,7 @@ gl_merge_vcard_finalize (GObject *object)
         gl_debug (DEBUG_MERGE, "END");
 }
 
+
 /*--------------------------------------------------------------------------*/
 /* Set argument.                                                            */
 /*--------------------------------------------------------------------------*/
@@ -151,17 +152,17 @@ gl_merge_vcard_set_property (GObject      *object,
                              const GValue *value,
                              GParamSpec   *pspec)
 {
-        glMergeVCard *merge_vcard;
+        switch (param_id) {
 
-        merge_vcard = GL_MERGE_VCARD (object);
+        /* Currently no vcard specific properties. */
 
-        switch (param_id) {
         default:
                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
                 break;
         }
 }
 
+
 /*--------------------------------------------------------------------------*/
 /* Get argument.                                                            */
 /*--------------------------------------------------------------------------*/
@@ -171,11 +172,10 @@ gl_merge_vcard_get_property (GObject     *object,
                              GValue      *value,
                              GParamSpec  *pspec)
 {
-        glMergeVCard *merge_vcard;
+        switch (param_id) {
 
-        merge_vcard = GL_MERGE_VCARD (object);
+        /* Currently no vcard specific properties. */
 
-        switch (param_id) {
         default:
                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
                 break;
@@ -183,43 +183,39 @@ gl_merge_vcard_get_property (GObject     *object,
 
 }
 
-/* TODO */
+
 /*--------------------------------------------------------------------------*/
 /* Get key list.                                                            */
 /*--------------------------------------------------------------------------*/
 static GList *
-gl_merge_vcard_get_key_list (glMerge *merge)
+gl_merge_vcard_get_key_list (const glMerge *merge)
 {
-        glMergeVCard   *merge_vcard;
-        GList          *key_list;
+        GList          *key_list = NULL;
+        EContactField   field_id;
         
         gl_debug (DEBUG_MERGE, "BEGIN");
 
-        merge_vcard = GL_MERGE_VCARD (merge);
-
-        /* extremely simple approach until I can list the available keys from the
-         * server, and return them. */
-        key_list = NULL;
-        key_list = g_list_append (key_list, g_strdup ("full_name"));
-        key_list = g_list_append (key_list, g_strdup ("home_address"));
-        key_list = g_list_append (key_list, g_strdup ("work_address"));
+        for ( field_id = E_CONTACT_FIELD_FIRST; field_id <= E_CONTACT_LAST_SIMPLE_STRING; field_id++ )
+        {
+                key_list = g_list_append (key_list, g_strdup (e_contact_pretty_name (field_id)));
+        }
 
         gl_debug (DEBUG_MERGE, "END");
 
         return key_list;
 }
 
-/* TODO? */
+
 /*--------------------------------------------------------------------------*/
 /* Get "primary" key.                                                       */
 /*--------------------------------------------------------------------------*/
 static gchar *
-gl_merge_vcard_get_primary_key (glMerge *merge)
+gl_merge_vcard_get_primary_key (const glMerge *merge)
 {
-        /* For now, let's always assume the full name is the primary key. */
-        return g_strdup ("full_name");
+        return g_strdup (e_contact_pretty_name(E_CONTACT_FILE_AS));
 }
 
+
 /*--------------------------------------------------------------------------*/
 /* Open merge source.                                                       */
 /*--------------------------------------------------------------------------*/
@@ -242,6 +238,7 @@ gl_merge_vcard_open (glMerge *merge)
         return;
 }
 
+
 /*--------------------------------------------------------------------------*/
 /* Close merge source.                                                      */
 /*--------------------------------------------------------------------------*/
@@ -258,6 +255,7 @@ gl_merge_vcard_close (glMerge *merge)
         }
 }
 
+
 /*--------------------------------------------------------------------------*/
 /* Get next record from merge source, NULL if no records left (i.e EOF)     */
 /*--------------------------------------------------------------------------*/
@@ -266,6 +264,7 @@ gl_merge_vcard_get_record (glMerge *merge)
 {
         glMergeVCard  *merge_vcard;
         glMergeRecord *record;
+        EContactField  field_id;
         glMergeField  *field;
 
         char *vcard;
@@ -289,26 +288,21 @@ gl_merge_vcard_get_record (glMerge *merge)
          * into the glMergeRecord structure. When done, free up the resources for
          * that contact */
 
-        /* get the full name */
-        field = g_new0 (glMergeField, 1);
-        field->key = g_strdup ("full_name");
-        field->value = g_strdup (e_contact_get_const(contact, E_CONTACT_FULL_NAME));
-
-        record->field_list = g_list_append (record->field_list, field);
+        for ( field_id = E_CONTACT_FIELD_FIRST; field_id <= E_CONTACT_LAST_SIMPLE_STRING; field_id++ )
+        {
+                gchar *value;
+                value = g_strdup (e_contact_get_const (contact, field_id));
 
-        /* get the home address */
-        field = g_new0 (glMergeField, 1);
-        field->key = g_strdup ("home_address");
-        field->value = g_strdup (e_contact_get_const(contact, E_CONTACT_ADDRESS_LABEL_HOME));
-
-        record->field_list = g_list_append (record->field_list, field);
+                if (value) {
+                        field = g_new0 (glMergeField, 1);
+                        field->key = g_strdup (e_contact_pretty_name (field_id));
+                        field->value = value;
+                        record->field_list = g_list_prepend (record->field_list, field);
+                }
+        }
 
-        /* get the work address */
-        field = g_new0 (glMergeField, 1);
-        field->key = g_strdup ("work_address");
-        field->value = g_strdup (e_contact_get_const(contact, E_CONTACT_ADDRESS_LABEL_WORK));
+        record->field_list = g_list_reverse (record->field_list);
 
-        record->field_list = g_list_append (record->field_list, field);
 
         /* free the contact */
         g_object_unref (contact);
@@ -317,20 +311,18 @@ gl_merge_vcard_get_record (glMerge *merge)
         return record;
 }
 
+
 /*---------------------------------------------------------------------------*/
 /* Copy merge_vcard specific fields.                                         */
 /*---------------------------------------------------------------------------*/
 static void
-gl_merge_vcard_copy (glMerge *dst_merge,
-                     glMerge *src_merge)
+gl_merge_vcard_copy (glMerge       *dst_merge,
+                     const glMerge *src_merge)
 {
-        glMergeVCard *dst_merge_vcard;
-        glMergeVCard *src_merge_vcard;
-
-        dst_merge_vcard = GL_MERGE_VCARD (dst_merge);
-        src_merge_vcard = GL_MERGE_VCARD (src_merge);
+        /* Currently nothing to copy. */
 }
 
+
 /*---------------------------------------------------------------------------*/
 /* PRIVATE: pull out a full VCard from the open file                         */
 /* Arguments:                                                                */
@@ -356,17 +348,31 @@ parse_next_vcard (FILE *fp)
 
         vcard = g_malloc0(size);
 
-        while (fgets(line, sizeof(line), fp) && found_end == FALSE) {
-                if (found_begin == TRUE) {
-                        if (g_str_has_prefix(line, "END:VCARD")) { found_end = TRUE; }
-                } else {
-                        if (g_str_has_prefix(line, "BEGIN:VCARD")) { found_begin = TRUE; } 
-                        else { continue; }/* skip lines not in a vcard */
+        while (fgets(line, sizeof(line), fp) && found_end == FALSE)
+        {
+                if (found_begin == TRUE)
+                {
+                        if (g_ascii_strncasecmp(line, "END:VCARD", strlen("END:VCARD")) == 0)
+                        {
+                                found_end = TRUE;
+                        }
+                }
+                else
+                {
+                        if (g_ascii_strncasecmp(line, "BEGIN:VCARD", strlen("BEGIN:VCARD")) == 0)
+                        {
+                                found_begin = TRUE;
+                        } 
+                        else
+                        {
+                                continue; /* skip lines not in a vcard */
+                        }
                 }
 
                 /* if the buffer passed us isn't big enough, reallocate it */
                 cursize += strlen(line);
-                if (cursize >= size) {
+                if (cursize >= size)
+                {
                         size *= 2;
                         vcard = (char *)g_realloc(vcard, size); /* aborts program on error */
                 }
@@ -381,3 +387,14 @@ parse_next_vcard (FILE *fp)
 
 
 #endif /* HAVE_LIBEBOOK */
+
+
+
+/*
+ * Local Variables:       -- emacs
+ * mode: C                -- emacs
+ * c-basic-offset: 8      -- emacs
+ * tab-width: 8           -- emacs
+ * indent-tabs-mode: nil  -- emacs
+ * End:                   -- emacs
+ */