akregator/src/librss

enclosure.cpp
1 /*
2  This file is part of Akregator.
3 
4  Copyright (C) 2005 Frank Osterfeld <frank.osterfeld at kdemail.net>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include "enclosure.h"
26 #include "tools_p.h"
27 
28 #include <tqdom.h>
29 #include <tqstring.h>
30 
31 #include <kdebug.h>
32 
33 namespace RSS
34 {
35 
36 
37 class Enclosure::EnclosurePrivate : public Shared
38 {
39  public:
40 
41  bool isNull;
42  TQString url;
43  int length;
44  TQString type;
45 
46  bool operator==(const EnclosurePrivate &other) const
47  {
48  return ( isNull == other.isNull || (url == other.url &&
49  length == other.length &&
50  type == other.type));
51  }
52 };
53 
54 
55 Enclosure Enclosure::fromXML(const TQDomElement& e)
56 {
57  TQString url, type;
58  int length = -1;
59 
60  if (e.hasAttribute(TQString::fromLatin1("url")))
61  url = e.attribute(TQString::fromLatin1("url"));
62 
63  if (e.hasAttribute(TQString::fromLatin1("length")))
64  {
65  bool ok;
66  int c = e.attribute(TQString::fromLatin1("length")).toInt(&ok);
67  length = ok ? c : -1;
68  }
69  if (e.hasAttribute(TQString::fromLatin1("type")))
70  type = e.attribute(TQString::fromLatin1("type"));
71 
72  return Enclosure(url, length, type);
73 }
74 
75 TQDomElement Enclosure::toXML(TQDomDocument document) const
76 {
77  TQDomElement e = document.createElement(TQString::fromLatin1("enclosure"));
78  if (!d->url.isNull())
79  e.setAttribute(TQString::fromLatin1("url"), d->url);
80  if (d->length != -1)
81  e.setAttribute(TQString::fromLatin1("length"), TQString::number(d->length));
82  if (!d->type.isNull())
83  e.setAttribute(TQString::fromLatin1("type"), d->type);
84 
85  return e;
86 }
87 
88 Enclosure::Enclosure() : d(new EnclosurePrivate)
89 {
90  d->isNull = true;
91  d->length = -1;
92 }
93 
94 Enclosure::Enclosure(const Enclosure& other) : d(0)
95 {
96  *this = other;
97 }
98 
99 Enclosure::Enclosure(const TQString& url, int length, const TQString& type) : d(new EnclosurePrivate)
100 {
101  d->isNull = false;
102  d->url = url;
103  d->length = length;
104  d->type = type;
105 }
106 
107 Enclosure::~Enclosure()
108 {
109  if (d->deref())
110  {
111  delete d;
112  d = 0;
113  }
114 }
115 
116 Enclosure& Enclosure::operator=(const Enclosure& other)
117 {
118  if (d != other.d)
119  {
120  other.d->ref();
121  if (d && d->deref())
122  delete d;
123  d = other.d;
124  }
125  return *this;
126 }
127 
128 bool Enclosure::operator==(const Enclosure &other) const
129 {
130  return *d == *other.d;
131 }
132 
133 bool Enclosure::isNull() const
134 {
135  return d->isNull;
136 }
137 
138 TQString Enclosure::url() const
139 {
140  return d->url;
141 }
142 
143 int Enclosure::length() const
144 {
145  return d->length;
146 }
147 
148 TQString Enclosure::type() const
149 {
150  return d->type;
151 }
152 
153 
154 } // namespace RSS