akregator/src/librss

enclosure.cpp

00001 /*
00002     This file is part of Akregator.
00003 
00004     Copyright (C) 2005 Frank Osterfeld <frank.osterfeld at kdemail.net>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "enclosure.h"
00026 #include "tools_p.h"
00027 
00028 #include <qdom.h>
00029 #include <qstring.h>
00030 
00031 #include <kdebug.h>
00032 
00033 namespace RSS
00034 {
00035 
00036 
00037 class Enclosure::EnclosurePrivate : public Shared
00038 {
00039     public:
00040 
00041     bool isNull;
00042     QString url;
00043     int length;
00044     QString type;
00045 
00046     bool operator==(const EnclosurePrivate &other) const
00047     {
00048         return ( isNull == other.isNull || (url == other.url &&
00049                 length == other.length &&
00050                 type == other.type));
00051     }
00052 };
00053 
00054 
00055 Enclosure Enclosure::fromXML(const QDomElement& e)
00056 {
00057     QString url, type;
00058     int length = -1;
00059 
00060     if (e.hasAttribute(QString::fromLatin1("url")))
00061         url = e.attribute(QString::fromLatin1("url"));
00062     
00063     if (e.hasAttribute(QString::fromLatin1("length")))
00064     {
00065         bool ok;
00066         int c = e.attribute(QString::fromLatin1("length")).toInt(&ok);
00067         length = ok ? c : -1;
00068     }
00069     if (e.hasAttribute(QString::fromLatin1("type")))
00070         type = e.attribute(QString::fromLatin1("type"));
00071 
00072     return Enclosure(url, length, type);
00073 }
00074 
00075 QDomElement Enclosure::toXML(QDomDocument document) const
00076 {
00077     QDomElement e = document.createElement(QString::fromLatin1("enclosure"));
00078     if (!d->url.isNull())
00079         e.setAttribute(QString::fromLatin1("url"), d->url);
00080     if (d->length != -1)
00081         e.setAttribute(QString::fromLatin1("length"), QString::number(d->length));
00082     if (!d->type.isNull())
00083         e.setAttribute(QString::fromLatin1("type"), d->type);
00084 
00085     return e;
00086 }
00087 
00088 Enclosure::Enclosure() : d(new EnclosurePrivate)
00089 {
00090     d->isNull = true;
00091     d->length = -1;
00092 }
00093 
00094 Enclosure::Enclosure(const Enclosure& other) : d(0)
00095 {
00096      *this = other;
00097 }
00098 
00099 Enclosure::Enclosure(const QString& url, int length, const QString& type) : d(new EnclosurePrivate)
00100 {
00101     d->isNull = false;
00102     d->url = url;
00103     d->length = length;
00104     d->type = type;
00105 }
00106 
00107 Enclosure::~Enclosure()
00108 {
00109     if (d->deref())
00110     {
00111         delete d;
00112         d = 0;
00113     }
00114 }
00115 
00116 Enclosure& Enclosure::operator=(const Enclosure& other)
00117 {
00118     if (d != other.d)
00119     {
00120         other.d->ref();
00121         if (d && d->deref())
00122             delete d;
00123         d = other.d;
00124     }
00125     return *this;
00126 }
00127 
00128 bool Enclosure::operator==(const Enclosure &other) const
00129 {
00130     return *d == *other.d;
00131 }
00132 
00133 bool Enclosure::isNull() const
00134 {
00135     return d->isNull;
00136 }
00137 
00138 QString Enclosure::url() const
00139 {
00140     return d->url;
00141 }
00142 
00143 int Enclosure::length() const
00144 {
00145     return d->length;
00146 }
00147 
00148 QString Enclosure::type() const
00149 {
00150     return d->type;
00151 }
00152 
00153 
00154 } // namespace RSS
KDE Home | KDE Accessibility Home | Description of Access Keys