diff --git a/kcal/attachment.cpp b/kcal/attachment.cpp index d09aaf612..13257d37e 100644 --- a/kcal/attachment.cpp +++ b/kcal/attachment.cpp @@ -1,212 +1,228 @@ /* This file is part of the kcal library. Copyright (c) 2002 Michael Brade This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** @file This file is part of the API for handling calendar data and defines the Attachment class. @brief Represents information related to an attachment for a Calendar Incidence. @author Michael Brade \ */ #include "attachment.h" #include using namespace KCal; /** Private class that helps to provide binary compatibility between releases. @internal */ //@cond PRIVATE class KCal::Attachment::Private { public: Private( const QString &mime, bool binary ) : mSize( 0 ), mMimeType( mime ), mData( 0 ), mBinary( binary ), mLocal( false ), mShowInline( false ) {} Private( const Private &other ) : mSize( other.mSize ), mMimeType( other.mMimeType ), mUri( other.mUri ), mData( qstrdup( other.mData ) ), mLabel( other.mLabel ), mBinary( other.mBinary ), mLocal( other.mLocal ), mShowInline( other.mShowInline ) {} ~Private() { delete[] mData; } QByteArray mDataCache; uint mSize; QString mMimeType; QString mUri; char *mData; QString mLabel; bool mBinary; bool mLocal; bool mShowInline; }; //@endcond Attachment::Attachment( const Attachment &attachment ) : d( new Attachment::Private( *attachment.d ) ) { } Attachment::Attachment( const QString &uri, const QString &mime ) : d( new Attachment::Private( mime, false ) ) { d->mUri = uri; } Attachment::Attachment( const char *base64, const QString &mime ) : d( new Attachment::Private( mime, true ) ) { d->mData = qstrdup( base64 ); } Attachment::~Attachment() { delete d; } bool Attachment::isUri() const { return !d->mBinary; } QString Attachment::uri() const { if ( !d->mBinary ) { return d->mUri; } else { return QString(); } } void Attachment::setUri( const QString &uri ) { d->mUri = uri; d->mBinary = false; } bool Attachment::isBinary() const { return d->mBinary; } char *Attachment::data() const { if ( d->mBinary ) { return d->mData; } else { return 0; } } QByteArray &Attachment::decodedData() const { if ( d->mDataCache.isNull() ) { d->mDataCache = QByteArray::fromBase64( d->mData ); } return d->mDataCache; } void Attachment::setDecodedData( const QByteArray &data ) { setData( data.toBase64().constData() ); d->mDataCache = data; d->mSize = d->mDataCache.size(); } void Attachment::setData( const char *base64 ) { delete[] d->mData; d->mData = qstrdup( base64 ); d->mBinary = true; d->mDataCache = QByteArray(); d->mSize = 0; } uint Attachment::size() const { if ( isUri() ) { return 0; } if ( !d->mSize ) { d->mSize = decodedData().size(); } return d->mSize; } QString Attachment::mimeType() const { return d->mMimeType; } void Attachment::setMimeType( const QString &mime ) { d->mMimeType = mime; } bool Attachment::showInline() const { return d->mShowInline; } void Attachment::setShowInline( bool showinline ) { d->mShowInline = showinline; } QString Attachment::label() const { return d->mLabel; } void Attachment::setLabel( const QString &label ) { d->mLabel = label; } bool Attachment::isLocal() const { return d->mLocal; } void Attachment::setLocal( bool local ) { d->mLocal = local; } + +bool Attachment::operator==( const Attachment &a2 ) const +{ + return uri() == a2.uri() && + d->mLabel == a2.label() && + d->mLocal == a2.isLocal() && + d->mBinary == a2.isBinary() && + d->mShowInline == a2.showInline() && + size() == a2.size() && + decodedData() == a2.decodedData(); +} + +bool Attachment::operator!=( const Attachment &a2 ) const +{ + return !( *this == a2 ); +} diff --git a/kcal/attachment.h b/kcal/attachment.h index f743b1bb6..82be110ac 100644 --- a/kcal/attachment.h +++ b/kcal/attachment.h @@ -1,239 +1,251 @@ /* This file is part of the kcal library. Copyright (c) 2002 Michael Brade This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** @file This file is part of the API for handling calendar data and defines the Attachment class. @author Michael Brade \ */ #ifndef KCAL_ATTACHMENT_H #define KCAL_ATTACHMENT_H #include "listbase.h" #include "kcal_export.h" #include namespace KCal { /** @brief Represents information related to an attachment for a Calendar Incidence. This is not an email message attachment. Calendar Incidence attachments consist of: - A Uniform Resource Identifier (URI) or a base64 encoded binary blob. - A Multipurpose Internet Mail Extensions (MIME) type. This class is used to associate files (local or remote) or other resources with a Calendar Incidence. */ class KCAL_EXPORT Attachment { public: /** List of attachments. */ typedef ListBase List; /** Constructs an attachment consisting of a @p uri and a @p mime type. @param uri is the @acronym URI referred to by this attachment. @param mime is the (optional) @acronym MIME type of the @p uri */ explicit Attachment( const QString &uri, const QString &mime = QString() ); /** Constructs an attachment consisting of a binary blob of data and a @p mime type. @param base64 is the binary data in base64 format for the attachment. @param mime is the (optional) @acronym MIME type of the attachment */ explicit Attachment( const char *base64, const QString &mime = QString() ); /** Constructs an attachment by copying another attachment. @param attachment is the attachment to be copied. */ Attachment( const Attachment &attachment ); /** Destroys the attachment. */ ~Attachment(); /** Sets the @acronym URI for this attachment to @p uri. @param uri is the @acronym URI to use for the attachment. @see uri(), isUri() */ void setUri( const QString &uri ); /** Returns the @acronym URI of the attachment. @see setUri(), isUri() */ QString uri() const; /** Returns true if the attachment has a @acronym URI; false otherwise. @see uri(), setUri(I), isBinary() */ bool isUri() const; /** Returns true if the attachment has a binary blob; false otherwise. @see isUri() */ bool isBinary() const; /** Sets the base64 encoded binary blob data of the attachment. @param base64 is a character string containing base64 encoded binary data. @see data(), decodedData() */ void setData( const char *base64 ); /** Returns a pointer to a character string containing the base64 encoded binary data of the attachment. @see setData(), setDecodedData() */ char *data() const; /** Sets the decoded attachment data. @param data is the decoded base64 binary data. @see decodedData(), data() */ void setDecodedData( const QByteArray &data ); /** Returns a #QByteArray containing the decoded base64 binary data of the attachment. @see setDecodedData(), setData() */ QByteArray &decodedData() const; /** Returns the size of the attachment, in bytes. If the attachment is binary (i.e, there is no @acronym URI associated with the attachment) then a value of 0 is returned. */ uint size() const; /** Sets the @acronym MIME-type of the attachment to @p mime. @param mime is the string to use for the attachment @acronym MIME-type. @see mimeType() */ void setMimeType( const QString &mime ); /** Returns the @acronym MIME-type of the attachment. @see setMimeType() */ QString mimeType() const; /** Sets the attachment "show in-line" option, which is derived from the Calendar Incidence @b X-CONTENT-DISPOSITION parameter. @param showinline is the flag to set (true) or unset (false) for the attachment "show in-line" option. @see showInline() */ void setShowInline( bool showinline ); /** Returns the attachment "show in-line" flag. @see setShowInline() */ bool showInline() const; /** Sets the attachment label to @p label, which is derived from the Calendar Incidence @b X-LABEL parameter. @param label is the string to use for the attachment label. @see label() */ void setLabel( const QString &label ); /** Returns the attachment label string. */ QString label() const; /** Sets the attachment "local" option, which is derived from the Calendar Incidence @b X-KONTACT-TYPE parameter. @param local is the flag to set (true) or unset (false) for the attachment "local" option. @see local() */ void setLocal( bool local ); /** Returns the attachment "local" flag. */ bool isLocal() const; + /** + Returns true if two attachments are equal + @since 4.3 + */ + bool operator==( const Attachment &a2 ) const; + + /** + Returns true if two attachments aren't equal + @since 4.3 + */ + bool operator!=( const Attachment &a2 ) const; + private: //@cond PRIVATE class Private; Private *const d; //@endcond }; } #endif diff --git a/kcal/listbase.h b/kcal/listbase.h index a2da34dd2..c246cc0ea 100644 --- a/kcal/listbase.h +++ b/kcal/listbase.h @@ -1,150 +1,166 @@ /* This file is part of the kcal library. Copyright (c) 2003 Cornelius Schumacher This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** @file This file is part of the API for handling calendar data and defines the ListBase class. @author Cornelius Schumacher \ */ #ifndef KCAL_LISTBASE_H #define KCAL_LISTBASE_H #include "kcal_export.h" #include namespace KCal { /** @brief This class provides a template for lists of pointers. It extends QList with an "auto-delete" functionality. */ template class ListBase : public QList { public: /** Constructor. */ ListBase() : QList(), mAutoDelete( false ) { } /** Copy constructor. @param other is the ListBase to copy. */ ListBase( const ListBase &other ) : QList( other ), mAutoDelete( false ) { } /** Destructor. */ ~ListBase() { if ( mAutoDelete ) { qDeleteAll( *this ); } } /** Assigns @p l to this listbase. @param l is the ListBase to copy. */ ListBase &operator=( const ListBase &l ) { if ( this == &l ) { return *this; } QList::operator=( l ); return *this; } /** Sets this list to operate in "auto-delete" mode. This mode deletes the memory pointed at by all members of the list in the destructor. @param autoDelete if true, puts the list into "auto-delete" mode. */ void setAutoDelete( bool autoDelete ) { mAutoDelete = autoDelete; } /** Clears the list. Memory is also freed if the list is set to "auto-delete" mode. */ void clearAll() { if ( mAutoDelete ) { qDeleteAll( *this ); } QList::clear(); } /** Removes all the members from the list with the specified address. Memory is also freed if the list is set to "auto-delete" mode. @param t is the pointer to remove from the list. @return true if successful; otherwise false (no such address @p t found). */ bool removeRef( T *t ) { if ( !contains( t ) ) { return false; } else { if ( mAutoDelete ) { delete t; } this->removeAll( t ); return true; } } /** Removes the specified member from the list. Memory is also freed if the list is set to "auto-delete" mode. @param it the iterator to remove from the list. */ void removeRef( typename QList::iterator it ) { if ( mAutoDelete ) { delete *it; } QList::erase( it ); } + bool operator==( const ListBase &l2 ) + { + int sz = QList::size(); + + if ( sz != l2.size() ) { + return false; + } else { + for ( int i=0; i::value( i ) == *l2.value( i ) ) ) { + return false; + } + } + } + return true; + } + private: //@cond PRIVATE bool mAutoDelete; //@endcond }; } #endif diff --git a/kcal/tests/testattachment.cpp b/kcal/tests/testattachment.cpp index 60c0affc5..24aaafd90 100644 --- a/kcal/tests/testattachment.cpp +++ b/kcal/tests/testattachment.cpp @@ -1,61 +1,72 @@ /* This file is part of the kcal library. Copyright (C) 2006 Allen Winter This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include "testattachment.h" #include "testattachment.moc" QTEST_KDEMAIN( AttachmentTest, NoGUI ) #include "kcal/event.h" #include "kcal/attachment.h" using namespace KCal; void AttachmentTest::testValidity() { Attachment attachment( QString( "http://www.kde.org" ) ); QCOMPARE( attachment.uri(), QString::fromLatin1("http://www.kde.org") ); QCOMPARE( attachment.data(), (char*)0 ); QVERIFY( attachment.decodedData().isEmpty() ); QVERIFY( !attachment.isBinary() ); attachment.setDecodedData( "foo" ); QVERIFY( attachment.isBinary() ); QCOMPARE( attachment.decodedData(), QByteArray("foo") ); QCOMPARE( attachment.data(), "Zm9v" ); QCOMPARE( attachment.size(), 3U ); Attachment attachment2 = Attachment( "Zm9v" ); QCOMPARE( attachment2.size(), 3U ); QCOMPARE( attachment2.decodedData(), QByteArray("foo") ); attachment2.setDecodedData( "123456" ); QCOMPARE( attachment2.size(), 6U ); Attachment attachment3( attachment2 ); QCOMPARE( attachment3.size(), attachment2.size() ); const char *fred = "jkajskldfasjfklasjfaskfaskfasfkasfjdasfkasjf"; Attachment attachment4( fred, "image/nonsense" ); QCOMPARE( fred, attachment4.data() ); QVERIFY( attachment4.isBinary() ); const char *ethel = "a9fafafjafkasmfasfasffksjklfjau"; attachment4.setData( ethel ); QCOMPARE( ethel, attachment4.data() ); + + Attachment attachment5( QString( "http://www.kde.org" ) ); + Attachment attachment6( QString( "http://www.kde.org" ) ); + QVERIFY( attachment5 == attachment6 ); + attachment5.setUri( "http://bugs.kde.org" ); + QVERIFY( attachment5 != attachment6 ); + attachment5.setDecodedData( "123456" ); + attachment6.setDecodedData( "123456" ); + QVERIFY( attachment5 == attachment6 ); + attachment6.setDecodedData( "12345" ); + QVERIFY( attachment5 != attachment6 ); }