/*
	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.
*/

#include <qcstring.h>
#include <qstrlist.h>
#include <qstringlist.h>
#include <qstring.h>
#include <qvaluelist.h>

#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_.setAutoDelete( TRUE );

  QPtrListIterator<ContentLine> it(x.contentLineList_);
  for (; it.current(); ++it) {
    ContentLine * c = new ContentLine(*it.current());
    contentLineList_.append(c);
  }

}

VCard::VCard(const QCString & s)
	:	Entity(s)
{
  contentLineList_.setAutoDelete( TRUE );
}

	VCard &
VCard::operator = (VCard & x)
{
	if (*this == x) return *this;

	group_				= x.group();
    QPtrListIterator<ContentLine> it(x.contentLineList_);
    for (; it.current(); ++it) {
      ContentLine * c = new ContentLine(*it.current());
      contentLineList_.append(c);
    }

	Entity::operator = (x);
	return *this;
}

	VCard &
VCard::operator = (const QCString & s)
{
	Entity::operator = (s);
	return *this;
}

	bool
VCard::operator == (VCard & x)
{
	x.parse();
	return false;
}

VCard::~VCard()
{
}

	void
VCard::_parse()
{
	
	QStringList l;
	QStrList sl;

	RTokenise(strRep_, "\r\n", sl);

	if (sl.count() < 3) { // Invalid VCARD !
        //qDebug("invalid vcard ");
		return;
	}
    l = QStringList::fromStrList( sl );
	// Get the first line
	QString beginLine = l[0].stripWhiteSpace();

	// Remove extra blank lines
	while (l.last().isEmpty())
		l.remove(l.last());

	// Now we know this is the last line
	QString endLine = l.last();

	// Trash the first and last lines as we have seen them.
	l.remove(l.begin());
	l.remove(l.last());

	///////////////////////////////////////////////////////////////
	// FIRST LINE

	int split = beginLine.find(':');

	if (split == -1) { // invalid, no BEGIN
		vDebug("No split");
		return;
	}

	QString firstPart(beginLine.left(split));
	QString valuePart(beginLine.mid(split + 1));

	split = firstPart.find('.');

	if (split != -1) {
		group_		= firstPart.left(split);
		firstPart	= firstPart.right(firstPart.length() - split - 1);
	}

	if (firstPart.left(5) != "BEGIN" ) { // No BEGIN !
		qDebug("no BEGIN in vcard ");
		return;
	}

	if (valuePart.left(5) != "VCARD") { // Not a vcard !
		qDebug("not a VCARD  ");
		return;
	}

	///////////////////////////////////////////////////////////////
	// CONTENT LINES
	//
	vDebug("Content lines");

	// Handle folded lines.

	QStringList refolded;


    QStringList::Iterator it =  l.begin(); 

	QString cur;

	for (; it != l.end(); ++it) {
		cur = (*it);
		++it;
		while ( it!= l.end() && (*it).at(0) == ' '	&& (*it).length()!= 1) {
			cur += (*it).mid(1) ;
			++it;
		}
		--it;
		refolded.append(cur);
	}
    QStringList::Iterator it2 = refolded.begin();
	for (; it2 != refolded.end(); ++it2) {
		ContentLine * cl = new ContentLine(QCString((*it2).latin1()));
		cl->parse();
		if (cl->value() == 0)
		{
		    qDebug("Content line could not be parsed. Discarded: %s", (*it2).latin1());
		    delete cl;
		}
		else
		  contentLineList_.append(cl);
	}

	///////////////////////////////////////////////////////////////
	// LAST LINE


        // LR: sorry, but the remaining code in this method makes no sense

#if 0
	split = endLine.find(':');

	if (split == -1) // invalid, no END
		return;

	firstPart = endLine.left(split);
	valuePart = endLine.right(firstPart.length() - split - 1);

	split = firstPart.find('.');

	if (split != -1) {
		group_		= firstPart.left(split);
		firstPart	= firstPart.right(firstPart.length() - split - 1);
	}

	if (qstricmp(firstPart, "END") != 0) // No END !
		return;

	if (qstricmp(valuePart, "VCARD") != 0) // Not a vcard !
		return;
#endif
}

	void
VCard::_assemble()
{
	vDebug("Assembling vcard");
	strRep_ = "BEGIN:VCARD\r\n";
	strRep_ += "VERSION:3.0\r\n";

	QPtrListIterator<ContentLine> it(contentLineList_);

	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
VCard::has(const QCString & s)
{
	parse();
	return contentLine(s) == 0 ? false : true;
}

	void
VCard::add(const ContentLine & cl)
{
	parse();
	ContentLine * c = new ContentLine(cl);
	contentLineList_.append(c);
}

	void
VCard::add(const QCString & s)
{
	parse();
	ContentLine * c = new ContentLine(s);
	contentLineList_.append(c);
}

	ContentLine *
VCard::contentLine(EntityType t)
{
	parse();
	QPtrListIterator<ContentLine> it(contentLineList_);

	for (; it.current(); ++it)
		if (it.current()->entityType() == t)
			return it.current();

	return 0;
}

	ContentLine *
VCard::contentLine(const QCString & s)
{
	parse();
	QPtrListIterator<ContentLine> it(contentLineList_);

	for (; it.current(); ++it)
		if (it.current()->entityType() == EntityNameToEntityType(s))
			return it.current();

	return 0;
}