gnokii-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Cleanups & proper vcard support


From: Pavel Machek
Subject: Cleanups & proper vcard support
Date: Sun, 6 Oct 2002 20:59:32 +0200
User-agent: Mutt/1.4i

Hi!

I'd like to commit this: it moves vcard support to central place, and
hopes to clean up... Okay to commit?
                                                                Pavel

Index: common/Makefile
===================================================================
RCS file: /cvsroot/gnokii/gnokii/common/Makefile,v
retrieving revision 1.46
diff -u -u -r1.46 Makefile
--- common/Makefile     18 Sep 2002 12:53:38 -0000      1.46
+++ common/Makefile     6 Oct 2002 18:57:23 -0000
@@ -34,7 +34,8 @@
        compat.o \
        snprintf.o \
        nokia-decoding.o \
-       gsm-call.o
+       gsm-call.o \
+       vcard.o
 
 ifdef NO_SHARED
 GNOKII_LIB=libgnokii.a
Index: gnokii/gnokii.c
===================================================================
RCS file: /cvsroot/gnokii/gnokii/gnokii/gnokii.c,v
retrieving revision 1.303
diff -u -u -r1.303 gnokii.c
--- gnokii/gnokii.c     28 Sep 2002 23:51:37 -0000      1.303
+++ gnokii/gnokii.c     6 Oct 2002 18:58:14 -0000
@@ -264,7 +264,7 @@
        fprintf(f, _("   usage: gnokii [--help|--monitor|--version]\n"
                     "          gnokii --getphonebook memory_type start_number 
[end_number|end]\n"
                     "                 [-r|--raw]\n"
-                    "          gnokii --writephonebook [-i]\n"
+                    "          gnokii --writephonebook [-iv]\n"
                     "          gnokii --getwapbookmark number\n"
                     "          gnokii --writewapbookmark name URL\n"
                     "          gnokii --deletewapbookmark number\n"
@@ -2967,7 +2967,7 @@
        int count, start_entry, end_entry = 0;
        gn_error error;
        char *memory_type_string;
-       bool all = false, raw = false;
+       bool all = false, raw = false, vcard = false;
 
        /* Handle command line args that set type, start and end locations. */
        memory_type_string = argv[0];
@@ -2983,7 +2983,9 @@
                break;
        case 4:
                if (!strcmp(argv[3], "-r") || !strcmp(argv[3], "--raw")) raw = 
true;
-               else usage(stderr);
+               else 
+                       if (!strcmp(argv[3], "-v") || !strcmp(argv[3], 
"--vcard")) vcard = true;
+                       else usage(stderr);
        case 3:
                if (!strcmp(argv[2], "end")) all = true;
                else if (!strcmp(argv[2], "-r") || !strcmp(argv[2], "--raw")) 
raw = true;
@@ -3014,6 +3016,10 @@
                                fprintf(stdout, "\n");
                                if (entry.MemoryType == GMT_MC || 
entry.MemoryType == GMT_DC || entry.MemoryType == GMT_RC)
                                        fprintf(stdout, "%02u.%02u.%04u 
%02u:%02u:%02u\n", entry.Date.Day, entry.Date.Month, entry.Date.Year, 
entry.Date.Hour, entry.Date.Minute, entry.Date.Second);
+                       } else if (vcard) {
+                               char buf[10240];
+                               sprintf(buf, "X_GSM_STORE_AT:%s%d\n", 
memory_type_string, entry.Location);
+                               phonebook2vcard(stdout, &entry, buf);
                        } else {
                                fprintf(stdout, _("%d. Name: %s\nNumber: 
%s\nGroup id: %d\n"), entry.Location, entry.Name, entry.Number, entry.Location);
                                for (i = 0; i < entry.SubEntriesCount; i++) {
@@ -3083,120 +3089,138 @@
        return 0;
 }
 
-/* Read data from stdin, parse and write to phone.  The parsing is relatively
-   crude and doesn't allow for much variation from the stipulated format. */
-/* FIXME: I guess there's *very* similar code in xgnokii */
-static int writephonebook(int argc, char *args[])
+char * decodephonebook(GSM_PhonebookEntry *entry, char *OLine)
 {
-       GSM_PhonebookEntry entry;
-       gn_error error = GN_ERR_NOTSUPPORTED;
+       char *Line = OLine;
+       char *ptr;
        char *memory_type_string;
-       int line_count = 0;
+       char BackLine[MAX_INPUT_LINE_LEN];
        int subentry;
-       char *Line, OLine[MAX_INPUT_LINE_LEN], BackLine[MAX_INPUT_LINE_LEN];
-       char *ptr;
 
-       /* Check argument */
-       if (argc && (strcmp("-i", args[0])))
-               usage(stderr);
+       strcpy(BackLine, Line);
+       memset(entry, 0, sizeof(GSM_PhonebookEntry));
 
-       Line = OLine;
+       ptr = strsep(&Line, ";");
+       if (ptr) strncpy(entry->Name, ptr, sizeof(entry->Name) - 1);
 
-       memset(&entry, 0, sizeof(GSM_PhonebookEntry));
+       ptr = strsep(&Line, ";");
+       if (ptr) strncpy(entry->Number, ptr, sizeof(entry->Number) - 1);
 
-       /* Go through data from stdin. */
-       while (GetLine(stdin, Line, MAX_INPUT_LINE_LEN)) {
-               strcpy(BackLine, Line);
-               line_count++;
+       ptr = strsep(&Line, ";");
 
-               ptr = strsep(&Line, ";");
-               if (ptr) strncpy(entry.Name, ptr, sizeof(entry.Name) - 1);
+       if (!ptr) {
+               fprintf(stderr, _("Format problem on line [%s]\n"), BackLine);
+               Line = OLine;
+               return NULL;
+       }
 
-               ptr = strsep(&Line, ";");
-               if (ptr) strncpy(entry.Number, ptr, sizeof(entry.Number) - 1);
+       if (!strncmp(ptr, "ME", 2)) {
+               memory_type_string = "int";
+               entry->MemoryType = GMT_ME;
+       } else {
+               if (!strncmp(ptr, "SM", 2)) {
+                       memory_type_string = "sim";
+                       entry->MemoryType = GMT_SM;
+               } else {
+                       fprintf(stderr, _("Format problem on line [%s]\n"), 
BackLine);
+                       return NULL;
+               }
+       }
 
-               ptr = strsep(&Line, ";");
+       ptr = strsep(&Line, ";");
+       if (ptr) entry->Location = atoi(ptr);
+       else entry->Location = 0;
 
-               if (!ptr) {
-                       fprintf(stderr, _("Format problem on line %d [%s]\n"), 
line_count, BackLine);
-                       Line = OLine;
-                       continue;
-               }
+       ptr = strsep(&Line, ";");
+       if (ptr) entry->Group = atoi(ptr);
+       else entry->Group = 0;
 
-               if (!strncmp(ptr, "ME", 2)) {
-                       memory_type_string = "int";
-                       entry.MemoryType = GMT_ME;
-               } else {
-                       if (!strncmp(ptr, "SM", 2)) {
-                               memory_type_string = "sim";
-                               entry.MemoryType = GMT_SM;
-                       } else {
-                               fprintf(stderr, _("Format problem on line %d 
[%s]\n"), line_count, BackLine);
-                               break;
-                       }
-               }
+       if (!ptr) {
+               fprintf(stderr, _("Format problem on line [%s]\n"), BackLine);
+               return NULL;
+       }
 
+       for (subentry = 0; ; subentry++) {
                ptr = strsep(&Line, ";");
-               if (ptr) entry.Location = atoi(ptr);
-               else entry.Location = 0;
+
+               if (ptr && *ptr != 0)
+                       entry->SubEntries[subentry].EntryType = atoi(ptr);
+               else
+                       break;
 
                ptr = strsep(&Line, ";");
-               if (ptr) entry.Group = atoi(ptr);
-               else entry.Group = 0;
+               if (ptr) entry->SubEntries[subentry].NumberType = atoi(ptr);
 
-               if (!ptr) {
-                       fprintf(stderr, _("Format problem on line %d [%s]\n"), 
line_count, BackLine);
-                       continue;
+               /* Phone Numbers need to have a number type. */
+               if (!ptr && entry->SubEntries[subentry].EntryType == 
GSM_Number) {
+                       fprintf(stderr, _("Missing phone number type on line %d"
+                                         " entry [%s]\n"), subentry, BackLine);
+                       subentry--;
+                       break;
                }
 
-               for (subentry = 0; ; subentry++) {
-                       ptr = strsep(&Line, ";");
+               ptr = strsep(&Line, ";");
+               if (ptr) entry->SubEntries[subentry].BlockNumber = atoi(ptr);
 
-                       if (ptr && *ptr != 0)
-                               entry.SubEntries[subentry].EntryType = 
atoi(ptr);
-                       else
-                               break;
+               ptr = strsep(&Line, ";");
 
-                       ptr = strsep(&Line, ";");
-                       if (ptr) entry.SubEntries[subentry].NumberType = 
atoi(ptr);
+               /* 0x13 Date Type; it is only for Dailed Numbers, etc.
+                  we don't store to this memories so it's an error to use it. 
*/
+               if (!ptr || entry->SubEntries[subentry].EntryType == GSM_Date) {
+                       fprintf(stderr, _("There is no phone number on line 
[%s] entry %d\n"),
+                               BackLine, subentry);
+                       subentry--;
+                       break;
+               } else
+                       strncpy(entry->SubEntries[subentry].data.Number, ptr, 
sizeof(entry->SubEntries[subentry].data.Number) - 1);
+       }
+
+       entry->SubEntriesCount = subentry;
+
+       /* This is to send other exports (like from 6110) to 7110 */
+       if (!entry->SubEntriesCount) {
+               entry->SubEntriesCount = 1;
+               entry->SubEntries[subentry].EntryType   = GSM_Number;
+               entry->SubEntries[subentry].NumberType  = GSM_General;
+               entry->SubEntries[subentry].BlockNumber = 2;
+               strcpy(entry->SubEntries[subentry].data.Number, entry->Number);
+       }
+       return memory_type_string;
+}
 
-                       /* Phone Numbers need to have a number type. */
-                       if (!ptr && entry.SubEntries[subentry].EntryType == 
GSM_Number) {
-                               fprintf(stderr, _("Missing phone number type on 
line %d"
-                                                 " entry %d [%s]\n"), 
line_count, subentry, BackLine);
-                               subentry--;
-                               break;
-                       }
 
-                       ptr = strsep(&Line, ";");
-                       if (ptr) entry.SubEntries[subentry].BlockNumber = 
atoi(ptr);
+/* Read data from stdin, parse and write to phone.  The parsing is relatively
+   crude and doesn't allow for much variation from the stipulated format. */
+/* FIXME: I guess there's *very* similar code in xgnokii */
+static int writephonebook(int argc, char *args[])
+{
+       GSM_PhonebookEntry entry;
+       gn_error error = GN_ERR_NOTSUPPORTED;
+       char *memory_type_string;
+       char *Line, OLine[MAX_INPUT_LINE_LEN];
+       int vcard = 0;
 
-                       ptr = strsep(&Line, ";");
+       /* Check argument */
+       if (argc && (strcmp("-i", args[0])) && (strcmp("-v", args[0])))
+               usage(stderr);
 
-                       /* 0x13 Date Type; it is only for Dailed Numbers, etc.
-                          we don't store to this memories so it's an error to 
use it. */
-                       if (!ptr || entry.SubEntries[subentry].EntryType == 
GSM_Date) {
-                               fprintf(stderr, _("There is no phone number on 
line %d entry %d [%s]\n"),
-                                       line_count, subentry, BackLine);
-                               subentry--;
-                               break;
-                       } else
-                               strncpy(entry.SubEntries[subentry].data.Number, 
ptr, sizeof(entry.SubEntries[subentry].data.Number) - 1);
-               }
+       if (!strcmp("-v", args[0]))
+               vcard = 1;
 
-               entry.SubEntriesCount = subentry;
+       Line = OLine;
 
-               /* This is to send other exports (like from 6110) to 7110 */
-               if (!entry.SubEntriesCount) {
-                       entry.SubEntriesCount = 1;
-                       entry.SubEntries[subentry].EntryType   = GSM_Number;
-                       entry.SubEntries[subentry].NumberType  = GSM_General;
-                       entry.SubEntries[subentry].BlockNumber = 2;
-                       strcpy(entry.SubEntries[subentry].data.Number, 
entry.Number);
+       /* Go through data from stdin. */
+       while (1) {
+               if (!vcard) {
+                       if (!GetLine(stdin, Line, MAX_INPUT_LINE_LEN))
+                               break;
+                       if (decodephonebook(&entry, OLine))
+                               continue;
+               } else {
+                       if (vcard2phonebook(stdin, &entry))
+                               break;
                }
 
-               Line = OLine;
-
                if (argc) {
                        GSM_PhonebookEntry aux;
 
@@ -3216,7 +3240,7 @@
                                                if (!strcmp(ans, _("yes"))) 
confirm = 1;
                                                else if (!strcmp(ans, _("no"))) 
confirm = 0;
                                        }
-                                       if (!confirm) continue;
+                                       if (!confirm) return -1;
                                }
                        } else {
                                fprintf(stderr, _("Error (%s)\n"), 
gn_error_print(error));
Index: xgnokii/xgnokii_contacts.c
===================================================================
RCS file: /cvsroot/gnokii/gnokii/xgnokii/xgnokii_contacts.c,v
retrieving revision 1.43
diff -u -u -r1.43 xgnokii_contacts.c
--- xgnokii/xgnokii_contacts.c  28 Sep 2002 23:51:38 -0000      1.43
+++ xgnokii/xgnokii_contacts.c  6 Oct 2002 18:58:51 -0000
@@ -2610,7 +2610,7 @@
 
 static void ExportVCARD(FILE * f)
 {
-       gchar buf2[10];
+       gchar buf2[1024];
        register gint i, j;
        PhonebookEntry *pbEntry;
 
@@ -2620,29 +2620,15 @@
                if (pbEntry->status == E_Deleted || pbEntry->status == E_Empty)
                        continue;
 
-               fprintf(f, "BEGIN:VCARD\n");
-               fprintf(f, "VERSION:3.0\n");
-               fprintf(f, "FN:%s\n", pbEntry->entry.Name);
-               fprintf(f, "TEL;PREF:%s\n", pbEntry->entry.Number);
-
                if (pbEntry->entry.MemoryType == GMT_ME)
-                       sprintf(buf2, "ME%d", i + 1);
+                       sprintf(buf2, "X_GSM_STORE_AT:ME%d\n", i + 1);
                else
-                       sprintf(buf2, "SM%d", i - memoryStatus.MaxME + 1);
+                       sprintf(buf2, "X_GSM_STORE_AT:SM%d\n", i - 
memoryStatus.MaxME + 1);
 
                fprintf(f, "X_GSM_STORE_AT:%s\n", buf2);
                fprintf(f, "X_GSM_CALLERGROUP:%d\n", pbEntry->entry.Group);
 
-               /* Add ext. pbk info if required */
-               if (phoneMonitor.supported & PM_EXTPBK)
-                       for (j = 0; j < pbEntry->entry.SubEntriesCount; j++) {
-                               if (pbEntry->entry.SubEntries[j].EntryType == 
GSM_Number)
-                                       fprintf(f, "TEL;UNKNOWN_%d:%s\n",
-                                               
pbEntry->entry.SubEntries[j].NumberType,
-                                               
pbEntry->entry.SubEntries[j].data.Number);
-                       }
-
-               fprintf(f, "END:VCARD\n\n");
+               phonebook2vcard(f, &pbEntry->entry, buf2);
        }
 
        fclose(f);
@@ -2790,7 +2776,7 @@
        }
 }
 
-
+/* FIXME: This is very similar to gnokii.c: decodephonebook */
 static bool ParseLine(GSM_PhonebookEntry * entry, gint * num, gchar * buf)
 {
        register gint i = 0;


<vcard.c:>

/*

  $Id: xgnokii_contacts.c,v 1.42 2002/08/27 11:57:33 plail Exp $
  
  X G N O K I I

  A Linux/Unix GUI for Nokia mobile phones.

  This file is part of gnokii.

  Gnokii 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
  (at your option) any later version.

  Gnokii 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 gnokii; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  Copyright (C) 2002 Pavel Machek <address@hidden>

 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "gsm-common.h"

int phonebook2vcard(FILE * f, GSM_PhonebookEntry *entry, char *addon)
{
        char buf2[1024];
        int i;

        fprintf(f, "BEGIN:VCARD\n");
        fprintf(f, "VERSION:3.0\n");
        fprintf(f, "FN:%s\n", entry->Name);
        fprintf(f, "TEL;VOICE:%s\n", entry->Number);

        fprintf(f, "X_GSM_STORE_AT:%s\n", buf2);
        fprintf(f, "X_GSM_CALLERGROUP:%d\n", entry->Group);
        fprintf(f, "%s", addon);

        /* Add ext. pbk info if required */
        for (i = 0; i < entry->SubEntriesCount; i++) {
                switch (entry->SubEntries[i].EntryType) {
                case 0x08:
                        fprintf(f, "EMAIL;INTERNET:%s\n", 
entry->SubEntries[i].data.Number);
                        break;
                case 0x09:
                        fprintf(f, "ADR;HOME:%s\n", 
entry->SubEntries[i].data.Number);
                        break;
                case 0x0a:
                        fprintf(f, "NOTE:%s\n", 
entry->SubEntries[i].data.Number);
                        break;
                case 0x0b:
                        switch (entry->SubEntries[i].NumberType) {
                        case 0x02:
                                fprintf(f, "TEL;HOME:%s\n", 
entry->SubEntries[i].data.Number);
                                break;
                        case 0x03:
                                fprintf(f, "TEL;CELL:%s\n", 
entry->SubEntries[i].data.Number);
                                break;
                        case 0x04:
                                fprintf(f, "TEL;FAX:%s\n", 
entry->SubEntries[i].data.Number);
                                break;
                        case 0x06:
                                fprintf(f, "TEL;WORK:%s\n", 
entry->SubEntries[i].data.Number);
                                break;
                        case 0x0a:
                                fprintf(f, "TEL;PREF:%s\n", 
entry->SubEntries[i].data.Number);
                                break;
                        default:
                                fprintf(f, "TEL;X_UNKNOWN_%d: %s\n", 
entry->SubEntries[i].NumberType, entry->SubEntries[i].data.Number);
                                break;
                        }
                        break;
                case 0x2c:
                        fprintf(f, "URL:%s\n", 
entry->SubEntries[i].data.Number);
                        break;
                default:
                        fprintf(f, "X_GNOKII_%d: %s\n", 
entry->SubEntries[i].EntryType, entry->SubEntries[i].data.Number);
                        break;
                }
        }

        fprintf(f, "END:VCARD\n\n");
        return 0;
}

#define BEGINS(a) ( !strncmp(buf, a, strlen(a)) )
#define STORE2(a, b, c) if (BEGINS(a)) { c; strcpy(b, buf+strlen(a)+1); 
continue; }
#define STORE(a, b) STORE2(a, b, (void) 0)

#define STORESUB(a, c) STORE2(a, 
entry->SubEntries[entry->SubEntriesCount++].data.Number, 
entry->SubEntries[entry->SubEntriesCount].EntryType = c);
#define STORENUM(a, c) STORE2(a, 
entry->SubEntries[entry->SubEntriesCount++].data.Number, 
entry->SubEntries[entry->SubEntriesCount].EntryType = 0x0b; 
entry->SubEntries[entry->SubEntriesCount].NumberType = c);


#define ERROR(a) fprintf(stderr, "%s\n", a)

int vcard2phonebook(FILE *f, GSM_PhonebookEntry *entry)
{
        char buf[10240];

        memset(entry, 0, sizeof(*entry));

        while (1) {
                if (!fgets(buf, 1024, f))
                        return -1;
                if (BEGINS("BEGIN:VCARD"))
                        break;
        }

        while (1) {
                if (!fgets(buf, 1024, f)) {
                        ERROR("Vcard began but not ended?");
                        return -1;
                }
                STORE("FN:", entry->Name);
                STORE("TEL;VOICE:", entry->Number);

                STORESUB("URL:", 0x2c);
                STORESUB("EMAIL;INTERNET:", 0x08);
                STORESUB("ADR;HOME:", 0x09);
                STORESUB("NOTE:", 0x0a);

                STORENUM("TEL;HOME:", 0x02);
                STORENUM("TEL;CELL:", 0x03);
                STORENUM("TEL;FAX:", 0x04);
                STORENUM("TEL;WORK:", 0x06);
                STORENUM("TEL;PREF:", 0x0a);

                if (BEGINS("END:VCARD"))
                        break;
        }
        return 0;
}

-- 
Worst form of spam? Adding advertisment signatures ala sourceforge.net.
What goes next? Inserting advertisment *into* email?




reply via email to

[Prev in Thread] Current Thread [Next in Thread]