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     contentLineList_.setAutoDelete( true );
00040 }
00041 
00042 VCard::VCard(const VCard & x)
00043     :   Entity(x),
00044         group_(x.group_),
00045         contentLineList_(x.contentLineList_)
00046 {
00047 }
00048 
00049 VCard::VCard(const QCString & s)
00050     :   Entity(s)
00051 {
00052 }
00053 
00054     VCard &
00055 VCard::operator = (VCard & x)
00056 {
00057     if (*this == x) return *this;
00058 
00059     group_              = x.group();
00060     contentLineList_    = x.contentLineList_;
00061 
00062     Entity::operator = (x);
00063     return *this;
00064 }
00065 
00066     VCard &
00067 VCard::operator = (const QCString & s)
00068 {
00069     Entity::operator = (s);
00070     return *this;
00071 }
00072 
00073     bool
00074 VCard::operator == (VCard & x)
00075 {
00076     x.parse();
00077     return false;
00078 }
00079 
00080 VCard::~VCard()
00081 {
00082 }
00083 
00084     void
00085 VCard::_parse()
00086 {
00087     vDebug("parse() called");
00088     QStrList l;
00089 
00090     RTokenise(strRep_, "\r\n", l);
00091 
00092     if (l.count() < 3) { // Invalid VCARD !
00093         vDebug("Invalid vcard");
00094         return;
00095     }
00096 
00097     // Get the first line
00098     QCString beginLine = QCString(l.at(0)).stripWhiteSpace();
00099 
00100     vDebug("Begin line == \"" + beginLine + "\"");
00101 
00102     // Remove extra blank lines
00103     while (QCString(l.last()).isEmpty())
00104         l.remove(l.last());
00105 
00106     // Now we know this is the last line
00107     QCString endLine = l.last();
00108 
00109     // Trash the first and last lines as we have seen them.
00110     l.remove(0u);
00111     l.remove(l.last());
00112 
00114     // FIRST LINE
00115 
00116     int split = beginLine.find(':');
00117 
00118     if (split == -1) { // invalid, no BEGIN
00119         vDebug("No split");
00120         return;
00121     }
00122 
00123     QCString firstPart(beginLine.left(split));
00124     QCString valuePart(beginLine.mid(split + 1));
00125 
00126     split = firstPart.find('.');
00127 
00128     if (split != -1) {
00129         group_      = firstPart.left(split);
00130         firstPart   = firstPart.right(firstPart.length() - split - 1);
00131     }
00132 
00133     if (qstrnicmp(firstPart, "BEGIN", 5) != 0) { // No BEGIN !
00134         vDebug("No BEGIN");
00135         return;
00136     }
00137 
00138     if (qstrnicmp(valuePart, "VCARD", 5) != 0) { // Not a vcard !
00139         vDebug("No VCARD");
00140         return;
00141     }
00142 
00144     // CONTENT LINES
00145     //
00146     vDebug("Content lines");
00147 
00148     // Handle folded lines.
00149 
00150     QStrList refolded;
00151 
00152     QStrListIterator it(l);
00153 
00154     QCString cur;
00155 
00156     for (; it.current(); ++it) {
00157 
00158         cur = it.current();
00159 
00160         ++it;
00161 
00162         while (
00163             it.current()        &&
00164             it.current()[0] == ' '  &&
00165             strlen(it.current()) != 1)
00166         {
00167             cur += it.current() + 1;
00168             ++it;
00169         }
00170 
00171         --it;
00172 
00173         refolded.append(cur);
00174     }
00175 
00176     QStrListIterator it2(refolded);
00177 
00178     for (; it2.current(); ++it2) {
00179 
00180         vDebug("New contentline using \"" + QCString(it2.current()) + "\"");
00181         ContentLine * cl = new ContentLine(it2.current());
00182 
00183         cl->parse();
00184 
00185         contentLineList_.append(cl);
00186     }
00187 
00189     // LAST LINE
00190 
00191     split = endLine.find(':');
00192 
00193     if (split == -1) // invalid, no END
00194         return;
00195 
00196     firstPart = endLine.left(split);
00197     valuePart = endLine.right(firstPart.length() - split - 1);
00198 
00199     split = firstPart.find('.');
00200 
00201     if (split != -1) {
00202         group_      = firstPart.left(split);
00203         firstPart   = firstPart.right(firstPart.length() - split - 1);
00204     }
00205 
00206     if (qstricmp(firstPart, "END") != 0) // No END !
00207         return;
00208 
00209     if (qstricmp(valuePart, "VCARD") != 0) // Not a vcard !
00210         return;
00211 }
00212 
00213     void
00214 VCard::_assemble()
00215 {
00216     vDebug("Assembling vcard");
00217     strRep_ = "BEGIN:VCARD\r\n";
00218     strRep_ += "VERSION:3.0\r\n";
00219 
00220     QPtrListIterator<ContentLine> it(contentLineList_);
00221 
00222     for (; it.current(); ++it)
00223         strRep_ += it.current()->asString() + "\r\n";
00224 
00225     strRep_ += "END:VCARD\r\n";
00226 }
00227 
00228     bool
00229 VCard::has(EntityType t)
00230 {
00231     parse();
00232     return contentLine(t) == 0 ? false : true;
00233 }
00234 
00235     bool
00236 VCard::has(const QCString & s)
00237 {
00238     parse();
00239     return contentLine(s) == 0 ? false : true;
00240 }
00241 
00242     void
00243 VCard::add(const ContentLine & cl)
00244 {
00245     parse();
00246     ContentLine * c = new ContentLine(cl);
00247     contentLineList_.append(c);
00248 }
00249 
00250     void
00251 VCard::add(const QCString & s)
00252 {
00253     parse();
00254     ContentLine * c = new ContentLine(s);
00255     contentLineList_.append(c);
00256 }
00257 
00258     ContentLine *
00259 VCard::contentLine(EntityType t)
00260 {
00261     parse();
00262     QPtrListIterator<ContentLine> it(contentLineList_);
00263 
00264     for (; it.current(); ++it)
00265         if (it.current()->entityType() == t)
00266             return it.current();
00267 
00268     return 0;
00269 }
00270 
00271     ContentLine *
00272 VCard::contentLine(const QCString & s)
00273 {
00274     parse();
00275     QPtrListIterator<ContentLine> it(contentLineList_);
00276 
00277     for (; it.current(); ++it)
00278         if (it.current()->entityType() == EntityNameToEntityType(s))
00279             return it.current();
00280 
00281     return 0;
00282 }
00283 
KDE Home | KDE Accessibility Home | Description of Access Keys