You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
284 lines
5.4 KiB
284 lines
5.4 KiB
15 years ago
|
/*
|
||
|
libvcard - vCard parsing library for vCard version 3.0
|
||
|
|
||
|
Copyright (C) 1998 Rik Hemsley rik@kde.org
|
||
|
|
||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
of this software and associated documentation files (the "Software"), to
|
||
|
deal in the Software without restriction, including without limitation the
|
||
|
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||
|
sell copies of the Software, and to permit persons to whom the Software is
|
||
|
furnished to do so, subject to the following conditions:
|
||
|
|
||
|
The above copyright notice and this permission notice shall be included in
|
||
|
all copies or substantial portions of the Software.
|
||
|
|
||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||
|
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||
|
*/
|
||
|
|
||
15 years ago
|
#include <tqcstring.h>
|
||
|
#include <tqstrlist.h>
|
||
15 years ago
|
|
||
|
#include <VCardEntity.h>
|
||
|
#include <VCardVCard.h>
|
||
|
#include <VCardContentLine.h>
|
||
|
#include <VCardRToken.h>
|
||
|
|
||
|
#include <VCardDefines.h>
|
||
|
|
||
|
using namespace VCARD;
|
||
|
|
||
|
VCard::VCard()
|
||
|
: Entity()
|
||
|
{
|
||
|
contentLineList_.setAutoDelete( true );
|
||
|
}
|
||
|
|
||
|
VCard::VCard(const VCard & x)
|
||
|
: Entity(x),
|
||
|
group_(x.group_),
|
||
|
contentLineList_(x.contentLineList_)
|
||
|
{
|
||
|
}
|
||
|
|
||
15 years ago
|
VCard::VCard(const TQCString & s)
|
||
15 years ago
|
: Entity(s)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
VCard &
|
||
|
VCard::operator = (VCard & x)
|
||
|
{
|
||
|
if (*this == x) return *this;
|
||
|
|
||
|
group_ = x.group();
|
||
|
contentLineList_ = x.contentLineList_;
|
||
|
|
||
|
Entity::operator = (x);
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
VCard &
|
||
15 years ago
|
VCard::operator = (const TQCString & s)
|
||
15 years ago
|
{
|
||
|
Entity::operator = (s);
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
bool
|
||
|
VCard::operator == (VCard & x)
|
||
|
{
|
||
|
x.parse();
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
VCard::~VCard()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void
|
||
|
VCard::_parse()
|
||
|
{
|
||
|
vDebug("parse() called");
|
||
15 years ago
|
TQStrList l;
|
||
15 years ago
|
|
||
|
RTokenise(strRep_, "\r\n", l);
|
||
|
|
||
|
if (l.count() < 3) { // Invalid VCARD !
|
||
|
vDebug("Invalid vcard");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Get the first line
|
||
13 years ago
|
TQCString beginLine = TQCString(l.at(0)).stripWhiteSpace();
|
||
15 years ago
|
|
||
|
vDebug("Begin line == \"" + beginLine + "\"");
|
||
|
|
||
|
// Remove extra blank lines
|
||
15 years ago
|
while (TQCString(l.last()).isEmpty())
|
||
15 years ago
|
l.remove(l.last());
|
||
|
|
||
|
// Now we know this is the last line
|
||
15 years ago
|
TQCString endLine = l.last();
|
||
15 years ago
|
|
||
|
// Trash the first and last lines as we have seen them.
|
||
|
l.remove(0u);
|
||
|
l.remove(l.last());
|
||
|
|
||
|
///////////////////////////////////////////////////////////////
|
||
|
// FIRST LINE
|
||
|
|
||
13 years ago
|
int split = beginLine.find(':');
|
||
15 years ago
|
|
||
|
if (split == -1) { // invalid, no BEGIN
|
||
|
vDebug("No split");
|
||
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQCString firstPart(beginLine.left(split));
|
||
|
TQCString valuePart(beginLine.mid(split + 1));
|
||
15 years ago
|
|
||
13 years ago
|
split = firstPart.find('.');
|
||
15 years ago
|
|
||
|
if (split != -1) {
|
||
|
group_ = firstPart.left(split);
|
||
|
firstPart = firstPart.right(firstPart.length() - split - 1);
|
||
|
}
|
||
|
|
||
13 years ago
|
if (tqstrnicmp(firstPart, "BEGIN", 5) != 0) { // No BEGIN !
|
||
15 years ago
|
vDebug("No BEGIN");
|
||
|
return;
|
||
|
}
|
||
|
|
||
13 years ago
|
if (tqstrnicmp(valuePart, "VCARD", 5) != 0) { // Not a vcard !
|
||
15 years ago
|
vDebug("No VCARD");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
///////////////////////////////////////////////////////////////
|
||
|
// CONTENT LINES
|
||
|
//
|
||
|
vDebug("Content lines");
|
||
|
|
||
|
// Handle folded lines.
|
||
|
|
||
15 years ago
|
TQStrList refolded;
|
||
15 years ago
|
|
||
15 years ago
|
TQStrListIterator it(l);
|
||
15 years ago
|
|
||
15 years ago
|
TQCString cur;
|
||
15 years ago
|
|
||
|
for (; it.current(); ++it) {
|
||
|
|
||
|
cur = it.current();
|
||
|
|
||
|
++it;
|
||
|
|
||
|
while (
|
||
|
it.current() &&
|
||
|
it.current()[0] == ' ' &&
|
||
|
strlen(it.current()) != 1)
|
||
|
{
|
||
|
cur += it.current() + 1;
|
||
|
++it;
|
||
|
}
|
||
|
|
||
|
--it;
|
||
|
|
||
|
refolded.append(cur);
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStrListIterator it2(refolded);
|
||
15 years ago
|
|
||
|
for (; it2.current(); ++it2) {
|
||
|
|
||
15 years ago
|
vDebug("New contentline using \"" + TQCString(it2.current()) + "\"");
|
||
15 years ago
|
ContentLine * cl = new ContentLine(it2.current());
|
||
|
|
||
|
cl->parse();
|
||
|
|
||
|
contentLineList_.append(cl);
|
||
|
}
|
||
|
|
||
|
///////////////////////////////////////////////////////////////
|
||
|
// LAST LINE
|
||
|
|
||
13 years ago
|
split = endLine.find(':');
|
||
15 years ago
|
|
||
|
if (split == -1) // invalid, no END
|
||
|
return;
|
||
|
|
||
|
firstPart = endLine.left(split);
|
||
|
valuePart = endLine.right(firstPart.length() - split - 1);
|
||
|
|
||
13 years ago
|
split = firstPart.find('.');
|
||
15 years ago
|
|
||
|
if (split != -1) {
|
||
|
group_ = firstPart.left(split);
|
||
|
firstPart = firstPart.right(firstPart.length() - split - 1);
|
||
|
}
|
||
|
|
||
13 years ago
|
if (tqstricmp(firstPart, "END") != 0) // No END !
|
||
15 years ago
|
return;
|
||
|
|
||
13 years ago
|
if (tqstricmp(valuePart, "VCARD") != 0) // Not a vcard !
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
VCard::_assemble()
|
||
|
{
|
||
|
vDebug("Assembling vcard");
|
||
|
strRep_ = "BEGIN:VCARD\r\n";
|
||
|
strRep_ += "VERSION:3.0\r\n";
|
||
|
|
||
15 years ago
|
TQPtrListIterator<ContentLine> it(contentLineList_);
|
||
15 years ago
|
|
||
|
for (; it.current(); ++it)
|
||
|
strRep_ += it.current()->asString() + "\r\n";
|
||
|
|
||
|
strRep_ += "END:VCARD\r\n";
|
||
|
}
|
||
|
|
||
|
bool
|
||
|
VCard::has(EntityType t)
|
||
|
{
|
||
|
parse();
|
||
|
return contentLine(t) == 0 ? false : true;
|
||
|
}
|
||
|
|
||
|
bool
|
||
15 years ago
|
VCard::has(const TQCString & s)
|
||
15 years ago
|
{
|
||
|
parse();
|
||
|
return contentLine(s) == 0 ? false : true;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
VCard::add(const ContentLine & cl)
|
||
|
{
|
||
|
parse();
|
||
|
ContentLine * c = new ContentLine(cl);
|
||
|
contentLineList_.append(c);
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
VCard::add(const TQCString & s)
|
||
15 years ago
|
{
|
||
|
parse();
|
||
|
ContentLine * c = new ContentLine(s);
|
||
|
contentLineList_.append(c);
|
||
|
}
|
||
|
|
||
|
ContentLine *
|
||
|
VCard::contentLine(EntityType t)
|
||
|
{
|
||
|
parse();
|
||
15 years ago
|
TQPtrListIterator<ContentLine> it(contentLineList_);
|
||
15 years ago
|
|
||
|
for (; it.current(); ++it)
|
||
|
if (it.current()->entityType() == t)
|
||
|
return it.current();
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
ContentLine *
|
||
15 years ago
|
VCard::contentLine(const TQCString & s)
|
||
15 years ago
|
{
|
||
|
parse();
|
||
15 years ago
|
TQPtrListIterator<ContentLine> it(contentLineList_);
|
||
15 years ago
|
|
||
|
for (; it.current(); ++it)
|
||
|
if (it.current()->entityType() == EntityNameToEntityType(s))
|
||
|
return it.current();
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|