akregator/src

tagset.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 "tag.h"
26 #include "tagset.h"
27 
28 #include <tqdom.h>
29 #include <tqmap.h>
30 #include <tqstring.h>
31 #include <tqstringlist.h>
32 
33 namespace Akregator {
34 
35 class TagSet::TagSetPrivate
36 {
37  public:
38  TQMap<TQString,Tag> map;
39 };
40 
41 TagSet::TagSet(TQObject* parent) : TQObject(parent), d(new TagSetPrivate)
42 {
43 }
44 
45 TagSet::~TagSet()
46 {
47  TQValueList<Tag> tags = d->map.values();
48  for (TQValueList<Tag>::Iterator it = tags.begin(); it != tags.end(); ++it)
49  (*it).removedFromTagSet(this);
50 
51  delete d;
52  d = 0;
53 }
54 
55 void TagSet::insert(const Tag& tag)
56 {
57  if (!d->map.contains(tag.id()))
58  {
59  d->map.insert(tag.id(), tag);
60  tag.addedToTagSet(this);
61  emit signalTagAdded(tag);
62  }
63 }
64 
65 void TagSet::remove(const Tag& tag)
66 {
67  if (d->map.contains(tag.id()))
68  {
69  d->map.remove(tag.id());
70  tag.removedFromTagSet(this);
71  emit signalTagRemoved(tag);
72  }
73 }
74 
75 bool TagSet::containsID(const TQString& id) const
76 {
77  return d->map.contains(id);
78 }
79 
80 bool TagSet::contains(const Tag& tag) const
81 {
82  return d->map.contains(tag.id());
83 }
84 
85 Tag TagSet::findByID(const TQString& id) const
86 {
87  return d->map.contains(id) ? d->map[id] : Tag();
88 }
89 
90 TQMap<TQString,Tag> TagSet::toMap() const
91 {
92  return d->map;
93 }
94 
95 void TagSet::readFromXML(const TQDomDocument& doc)
96 {
97  TQDomElement root = doc.documentElement();
98 
99  if (root.isNull())
100  return;
101 
102  TQDomNodeList list = root.elementsByTagName(TQString::fromLatin1("tag"));
103 
104  for (uint i = 0; i < list.length(); ++i)
105  {
106  TQDomElement e = list.item(i).toElement();
107  if (!e.isNull())
108  {
109  if (e.hasAttribute(TQString::fromLatin1("id")))
110  {
111  TQString id = e.attribute(TQString::fromLatin1("id"));
112  TQString name = e.text();
113  TQString scheme = e.attribute(TQString::fromLatin1("scheme"));
114  Tag tag(id, name, scheme);
115 
116  TQString icon = e.attribute(TQString::fromLatin1("icon"));
117  if (!icon.isEmpty())
118  tag.setIcon(icon);
119 
120  insert(tag);
121 
122  }
123  }
124  }
125 
126 }
127 void TagSet::tagUpdated(const Tag& tag)
128 {
129  emit signalTagUpdated(tag);
130 }
131 
132 TQDomDocument TagSet::toXML() const
133 {
134  TQDomDocument doc;
135  doc.appendChild( doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
136 
137  TQDomElement root = doc.createElement("tagSet");
138  root.setAttribute( "version", "0.1" );
139  doc.appendChild(root);
140 
141  TQValueList<Tag> list = d->map.values();
142  for (TQValueList<Tag>::ConstIterator it = list.begin(); it != list.end(); ++it)
143  {
144 
145  TQDomElement tn = doc.createElement("tag");
146 
147  TQDomText text = doc.createTextNode((*it).name());
148  tn.setAttribute(TQString::fromLatin1("id"),(*it).id());
149  if (!(*it).scheme().isEmpty())
150  tn.setAttribute(TQString::fromLatin1("scheme"),(*it).scheme());
151  if (!(*it).icon().isEmpty())
152  tn.setAttribute(TQString::fromLatin1("icon"),(*it).icon());
153  tn.appendChild(text);
154  root.appendChild(tn);
155  }
156  return doc;
157 }
158 
159 } // namespace Akregator
160 
161 #include "tagset.moc"
bool containsID(const TQString &id) const
returns true if this set contains a tag with a given ID
Definition: tagset.cpp:75
void readFromXML(const TQDomDocument &doc)
reads tag set from XML see toXML() for an explanation of the format
Definition: tagset.cpp:95
TQMap< TQString, Tag > toMap() const
returns the tag set as map ((id, Tag) pairs)
Definition: tagset.cpp:90
void signalTagUpdated(const Tag &)
emitted when a tag in this set was changed (e.g.
void remove(const Tag &tag)
removes a tag from the tag set.
Definition: tagset.cpp:65
void signalTagAdded(const Tag &)
emitted when a tag was added to this tag set
void signalTagRemoved(const Tag &)
emitted when a tag was removed from this set
void insert(const Tag &tag)
adds a tag to the tag set.
Definition: tagset.cpp:55
bool contains(const Tag &tag) const
returns true if this set contains tag
Definition: tagset.cpp:80
TQDomDocument toXML() const
returns an XML representation of the tag set.
Definition: tagset.cpp:132
Tag findByID(const TQString &id) const
returns the tag with the given ID if the tag is element of the set, or a null tag if not
Definition: tagset.cpp:85
void tagUpdated(const Tag &tag)
called by the tag (Tag is friend class) after a change
Definition: tagset.cpp:127