kabc Library API Documentation

VCard.cpp

00001 /*
00002         libvcard - vCard parsing library for vCard version 3.0
00003 
00004         Copyright (C) 1998 Rik Hemsley rik@kde.org
00005 
00006   Permission is hereby granted, free of charge, to any person obtaining a copy
00007   of this software and associated documentation files (the "Software"), to
00008   deal in the Software without restriction, including without limitation the
00009   rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00010   sell copies of the Software, and to permit persons to whom the Software is
00011   furnished to do so, subject to the following conditions:
00012 
00013   The above copyright notice and this permission notice shall be included in
00014   all copies or substantial portions of the Software.
00015 
00016   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019   AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00020   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00021   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 */
00023 
00024 #include <qcstring.h>
00025 #include <qstrlist.h>
00026 
00027 #include <VCardEntity.h>
00028 #include <VCardVCard.h>
00029 #include <VCardContentLine.h>
00030 #include <VCardRToken.h>
00031 
00032 #include <VCardDefines.h>
00033 
00034 using namespace VCARD;
00035 
00036 VCard::VCard()
00037         :       Entity()
00038 {
00039 }
00040 
00041 VCard::VCard(const VCard & x)
00042         :       Entity(x),
00043                 group_(x.group_),
00044                 contentLineList_(x.contentLineList_)
00045 {
00046 }
00047 
00048 VCard::VCard(const QCString & s)
00049         :       Entity(s)
00050 {
00051 }
00052 
00053         VCard &
00054 VCard::operator = (VCard & x)
00055 {
00056         if (*this == x) return *this;
00057 
00058         group_                          = x.group();
00059         contentLineList_        = x.contentLineList_;
00060 
00061         Entity::operator = (x);
00062         return *this;
00063 }
00064 
00065         VCard &
00066 VCard::operator = (const QCString & s)
00067 {
00068         Entity::operator = (s);
00069         return *this;
00070 }
00071 
00072         bool
00073 VCard::operator == (VCard & x)
00074 {
00075         x.parse();
00076         return false;
00077 }
00078 
00079 VCard::~VCard()
00080 {
00081 }
00082 
00083         void
00084 VCard::_parse()
00085 {
00086         vDebug("parse() called");
00087         QStrList l;
00088 
00089         RTokenise(strRep_, "\r\n", l);
00090 
00091         if (l.count() < 3) { // Invalid VCARD !
00092                 vDebug("Invalid vcard");
00093                 return;
00094         }
00095 
00096         // Get the first line
00097         QCString beginLine = QCString(l.at(0)).stripWhiteSpace();
00098 
00099         vDebug("Begin line == \"" + beginLine + "\"");
00100 
00101         // Remove extra blank lines
00102         while (QCString(l.last()).isEmpty())
00103                 l.remove(l.last());
00104 
00105         // Now we know this is the last line
00106         QCString endLine = l.last();
00107 
00108         // Trash the first and last lines as we have seen them.
00109         l.remove(0u);
00110         l.remove(l.last());
00111 
00113         // FIRST LINE
00114 
00115         int split = beginLine.find(':');
00116 
00117         if (split == -1) { // invalid, no BEGIN
00118                 vDebug("No split");
00119                 return;
00120         }
00121 
00122         QCString firstPart(beginLine.left(split));
00123         QCString valuePart(beginLine.mid(split + 1));
00124 
00125         split = firstPart.find('.');
00126 
00127         if (split != -1) {
00128                 group_          = firstPart.left(split);
00129                 firstPart       = firstPart.right(firstPart.length() - split - 1);
00130         }
00131 
00132         if (qstrnicmp(firstPart, "BEGIN", 5) != 0) { // No BEGIN !
00133                 vDebug("No BEGIN");
00134                 return;
00135         }
00136 
00137         if (qstrnicmp(valuePart, "VCARD", 5) != 0) { // Not a vcard !
00138                 vDebug("No VCARD");
00139                 return;
00140         }
00141 
00143         // CONTENT LINES
00144         //
00145         vDebug("Content lines");
00146 
00147         // Handle folded lines.
00148 
00149         QStrList refolded;
00150 
00151         QStrListIterator it(l);
00152 
00153         QCString cur;
00154 
00155         for (; it.current(); ++it) {
00156 
00157                 cur = it.current();
00158 
00159                 ++it;
00160 
00161                 while (
00162                         it.current()            &&
00163                         it.current()[0] == ' '  &&
00164                         strlen(it.current()) != 1)
00165                 {
00166                         cur += it.current() + 1;
00167                         ++it;
00168                 }
00169 
00170                 --it;
00171 
00172                 refolded.append(cur);
00173         }
00174 
00175         QStrListIterator it2(refolded);
00176 
00177         for (; it2.current(); ++it2) {
00178 
00179                 vDebug("New contentline using \"" + QCString(it2.current()) + "\"");
00180                 ContentLine * cl = new ContentLine(it2.current());
00181 
00182                 cl->parse();
00183 
00184                 contentLineList_.append(cl);
00185         }
00186 
00188         // LAST LINE
00189 
00190         split = endLine.find(':');
00191 
00192         if (split == -1) // invalid, no END
00193                 return;
00194 
00195         firstPart = endLine.left(split);
00196         valuePart = endLine.right(firstPart.length() - split - 1);
00197 
00198         split = firstPart.find('.');
00199 
00200         if (split != -1) {
00201                 group_          = firstPart.left(split);
00202                 firstPart       = firstPart.right(firstPart.length() - split - 1);
00203         }
00204 
00205         if (qstricmp(firstPart, "END") != 0) // No END !
00206                 return;
00207 
00208         if (qstricmp(valuePart, "VCARD") != 0) // Not a vcard !
00209                 return;
00210 }
00211 
00212         void
00213 VCard::_assemble()
00214 {
00215         vDebug("Assembling vcard");
00216         strRep_ = "BEGIN:VCARD\r\n";
00217         strRep_ += "VERSION:3.0\r\n";
00218 
00219         QPtrListIterator<ContentLine> it(contentLineList_);
00220 
00221         for (; it.current(); ++it)
00222                 strRep_ += it.current()->asString() + "\r\n";
00223 
00224         strRep_ += "END:VCARD\r\n";
00225 }
00226 
00227         bool
00228 VCard::has(EntityType t)
00229 {
00230         parse();
00231         return contentLine(t) == 0 ? false : true;
00232 }
00233 
00234         bool
00235 VCard::has(const QCString & s)
00236 {
00237         parse();
00238         return contentLine(s) == 0 ? false : true;
00239 }
00240 
00241         void
00242 VCard::add(const ContentLine & cl)
00243 {
00244         parse();
00245         ContentLine * c = new ContentLine(cl);
00246         contentLineList_.append(c);
00247 }
00248 
00249         void
00250 VCard::add(const QCString & s)
00251 {
00252         parse();
00253         ContentLine * c = new ContentLine(s);
00254         contentLineList_.append(c);
00255 }
00256 
00257         ContentLine *
00258 VCard::contentLine(EntityType t)
00259 {
00260         parse();
00261         QPtrListIterator<ContentLine> it(contentLineList_);
00262 
00263         for (; it.current(); ++it)
00264                 if (it.current()->entityType() == t)
00265                         return it.current();
00266 
00267         return 0;
00268 }
00269 
00270         ContentLine *
00271 VCard::contentLine(const QCString & s)
00272 {
00273         parse();
00274         QPtrListIterator<ContentLine> it(contentLineList_);
00275 
00276         for (; it.current(); ++it)
00277                 if (it.current()->entityType() == EntityNameToEntityType(s))
00278                         return it.current();
00279 
00280         return 0;
00281 }
00282 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:29:23 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001