author | zecke <zecke> | 2004-09-14 22:50:22 (UTC) |
---|---|---|
committer | zecke <zecke> | 2004-09-14 22:50:22 (UTC) |
commit | d46a01de16c8ad65b00c76beda547970f7972b62 (patch) (side-by-side diff) | |
tree | 3ce4b630f5cd45c230c0a4f4ff92993a9c4f4f6c /library/backend/vobject.cpp | |
parent | 271a3753d1b71a85e5463b357abbfe46fce51732 (diff) | |
download | opie-d46a01de16c8ad65b00c76beda547970f7972b62.zip opie-d46a01de16c8ad65b00c76beda547970f7972b62.tar.gz opie-d46a01de16c8ad65b00c76beda547970f7972b62.tar.bz2 |
Be able to properly import vCard from Cell Phones
-rw-r--r-- | library/backend/vobject.cpp | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/library/backend/vobject.cpp b/library/backend/vobject.cpp index 9263c3a..592d116 100644 --- a/library/backend/vobject.cpp +++ b/library/backend/vobject.cpp @@ -1010,344 +1010,345 @@ static int writeBase64(OFile *fp, unsigned char *s, long len) } // now output 'quad' with appropriate whitespace and line ending appendsOFile(fp, (numQuads == 0 ? " " : "")); appendsOFile(fp, quad); appendsOFile(fp, ((cur >= len)?"\n" :(numQuads==MAXQUADS-1?"\n" : ""))); numQuads = (numQuads + 1) % MAXQUADS; } appendcOFile(fp,'\n'); return 1; } static const char *qpReplaceChar(unsigned char c) { if (c == '\n') { return "=0A=\n"; } else if ( // RFC 1521 (c >= 32 && c <= 60) // Note: " " not allowed at EOL || (c >= 62 && c <= 126) ) { return 0; } static char trans[4]; trans[0] = '='; trans[3] = '\0'; int rem = c % 16; int div = c / 16; if (div < 10) trans[1] = '0' + div; else trans[1] = 'A' + (div - 10); if (rem < 10) trans[2] = '0' + rem; else trans[2] = 'A' + (rem - 10); return trans; } static void writeEncString(OFile *fp, const char *s, bool nosemi) { /* only A-Z, 0-9 and "'" (ASCII code 39) "(" (ASCII code 40) ")" (ASCII code 41) "+" (ASCII code 43) "," (ASCII code 44) "-" (ASCII code 45) "/" (ASCII code 47) "?" (ASCII code 63) should remain un-encoded. '=' needs to be encoded as it is the escape character. ';' needs to be as it is a field separator. */ const char *p = s; switch ( vobj_enc ) { case EightBit: while (*p) { if ( *p == '\n' || nosemi && ( *p == '\\' || *p == ';' ) ) appendcOFile(fp, '\\'); appendcOFile(fp, *p); p++; } break; case QuotedPrintable: while (*p) { const char *rep = qpReplaceChar(*p); if (rep) appendsOFile(fp, rep); else if ( *p == ';' && nosemi ) appendsOFile(fp, "=3B"); else if ( *p == ' ' ) { if ( !p[1] || p[1] == '\n' ) // RFC 1521 appendsOFile(fp, "=20"); else appendcOFile(fp, *p); } else appendcOFile(fp, *p); p++; } break; case Base64: writeBase64(fp, (unsigned char*)p, strlen(p)); break; } } -static bool includesUnprintable(VObject *o) +static bool includesUnprintable(VObject *o, bool nosemi) { if (o) { if (VALUE_TYPE(o) == VCVT_STRINGZ) { const char *p = STRINGZ_VALUE_OF(o); if (p) { while (*p) { if (*p==' ' && (!p[1] || p[1]=='\n') // RFC 1521: spaces at ends need quoting - || qpReplaceChar(*p) ) + || qpReplaceChar(*p) + || *p==';' && nosemi ) return TRUE; p++; } } } } return FALSE; } static void writeVObject_(OFile *fp, VObject *o); static void writeValue(OFile *fp, VObject *o, unsigned long size, bool nosemi) { if (o == 0) return; switch (VALUE_TYPE(o)) { case VCVT_STRINGZ: { writeEncString(fp, STRINGZ_VALUE_OF(o), nosemi); break; } case VCVT_UINT: { char buf[16]; sprintf(buf,"%u", INTEGER_VALUE_OF(o)); appendsOFile(fp,buf); break; } case VCVT_ULONG: { char buf[16]; sprintf(buf,"%lu", LONG_VALUE_OF(o)); appendsOFile(fp,buf); break; } case VCVT_RAW: { appendcOFile(fp,'\n'); writeBase64(fp,(unsigned char*)(ANY_VALUE_OF(o)),size); break; } case VCVT_VOBJECT: appendcOFile(fp,'\n'); writeVObject_(fp,VOBJECT_VALUE_OF(o)); break; } } static void writeAttrValue(OFile *fp, VObject *o) { if (NAME_OF(o)) { struct PreDefProp *pi; pi = lookupPropInfo(NAME_OF(o)); if (pi && ((pi->flags & PD_INTERNAL) != 0)) return; - if ( includesUnprintable(o) ) + if ( includesUnprintable(o,TRUE) ) appendsOFileEncCs(fp); appendcOFile(fp,';'); appendsOFile(fp,NAME_OF(o)); } else { appendcOFile(fp,';'); } if (VALUE_TYPE(o)) { appendcOFile(fp,'='); writeValue(fp,o,0,TRUE); } } static void writeGroup(OFile *fp, VObject *o) { char buf1[256]; char buf2[256]; strcpy(buf1,NAME_OF(o)); while ((o=isAPropertyOf(o,VCGroupingProp)) != 0) { strcpy(buf2,STRINGZ_VALUE_OF(o)); strcat(buf2,"."); strcat(buf2,buf1); strcpy(buf1,buf2); } appendsOFile(fp,buf1); } static int inList(const char **list, const char *s) { if (list == 0) return 0; while (*list) { if (qstricmp(*list,s) == 0) return 1; list++; } return 0; } static void writeProp(OFile *fp, VObject *o) { if (NAME_OF(o)) { struct PreDefProp *pi; VObjectIterator t; const char **fields_ = 0; pi = lookupPropInfo(NAME_OF(o)); if (pi && ((pi->flags & PD_BEGIN) != 0)) { writeVObject_(fp,o); return; } if (isAPropertyOf(o,VCGroupingProp)) writeGroup(fp,o); else appendsOFile(fp,NAME_OF(o)); if (pi) fields_ = pi->fields; initPropIterator(&t,o); while (moreIteration(&t)) { const char *s; VObject *eachProp = nextVObject(&t); s = NAME_OF(eachProp); if (qstricmp(VCGroupingProp,s) && !inList(fields_,s)) writeAttrValue(fp,eachProp); } if (fields_) { int i = 0, n = 0; const char** fields = fields_; /* output prop as fields */ bool printable = TRUE; while (*fields && printable) { VObject *t = isAPropertyOf(o,*fields); - if (includesUnprintable(t)) + if (includesUnprintable(t,TRUE)) printable = FALSE; fields++; } fields = fields_; if (!printable) appendsOFileEncCs(fp); appendcOFile(fp,':'); while (*fields) { VObject *t = isAPropertyOf(o,*fields); i++; if (t) n = i; fields++; } fields = fields_; for (i=0;i<n;i++) { writeValue(fp,isAPropertyOf(o,*fields),0,TRUE); fields++; if (i<(n-1)) appendcOFile(fp,';'); } } } if (VALUE_TYPE(o)) { - if ( includesUnprintable(o) ) + if ( includesUnprintable(o,FALSE) ) appendsOFileEncCs(fp); unsigned long size = 0; VObject *p = isAPropertyOf(o,VCDataSizeProp); if (p) size = LONG_VALUE_OF(p); appendcOFile(fp,':'); writeValue(fp,o,size,FALSE); } appendcOFile(fp,'\n'); } static void writeVObject_(OFile *fp, VObject *o) { if (NAME_OF(o)) { struct PreDefProp *pi; pi = lookupPropInfo(NAME_OF(o)); if (pi && ((pi->flags & PD_BEGIN) != 0)) { VObjectIterator t; const char *begin = NAME_OF(o); appendsOFile(fp,"BEGIN:"); appendsOFile(fp,begin); appendcOFile(fp,'\n'); initPropIterator(&t,o); while (moreIteration(&t)) { VObject *eachProp = nextVObject(&t); writeProp(fp, eachProp); } appendsOFile(fp,"END:"); appendsOFile(fp,begin); appendsOFile(fp,"\n\n"); } } } static void initVObjectEncoding() { Config pimConfig( "Beam" ); pimConfig.setGroup("Send"); Config devcfg(pimConfig.readEntry("DeviceConfig"),Config::File); QString enc = "QP"; QString cs = "UTF-8"; if ( devcfg.isValid() ) { devcfg.setGroup("Send"); enc = devcfg.readEntry("Encoding","QP"); cs = devcfg.readEntry("CharSet","UTF-8"); } strncpy(vobj_cs,cs.latin1(),10); if ( enc == "QP" ) { vobj_enc = QuotedPrintable; vobj_enc_s = VCQuotedPrintableProp; } else if ( enc == "B64" ) { vobj_enc = Base64; vobj_enc_s = VCBase64Prop; } else { vobj_enc = EightBit; vobj_enc_s = 0; } } void writeVObject(FILE *fp, VObject *o) { initVObjectEncoding(); OFile ofp; // ##### //_setmode(_fileno(fp), _O_BINARY); initOFile(&ofp,fp); writeVObject_(&ofp,o); } DLLEXPORT(void) writeVObjectToFile(char *fname, VObject *o) { QFileDirect f( fname); if ( !f.open( IO_WriteOnly ) ) { qWarning("Unable to open vobject write %s", fname); return; } writeVObject( f.directHandle(),o ); } DLLEXPORT(void) writeVObjectsToFile(char *fname, VObject *list) { QFileDirect f( fname); if ( !f.open( IO_WriteOnly ) ) { qWarning("Unable to open vobject write %s", fname); return; } while (list) { writeVObject(f.directHandle(),list); list = nextVObjectInList(list); } } |