akregator/src

tagnodelist.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 "feedlist.h"
26 #include "tag.h"
27 #include "tagnode.h"
28 #include "tagnodelist.h"
29 #include "tagset.h"
30 #include "folder.h"
31 #include "tagfolder.h"
32 
33 #include <tqdom.h>
34 #include <tqmap.h>
35 #include <tqstring.h>
36 #include <tqvaluelist.h>
37 
38 #include <tdeapplication.h>
39 #include <tdelocale.h>
40 
41 namespace Akregator {
42 
43 class TagNodeList::TagNodeListPrivate
44 {
45  public:
46  FeedList* feedList;
47  TagSet* tagSet;
48  TQMap<TQString, TagNode*> tagIdToNodeMap;
49 };
50 
51 FeedList* TagNodeList::feedList() const
52 {
53  return d->feedList;
54 }
55 
56 TagNodeList::TagNodeList(FeedList* feedList, TagSet* tagSet) : NodeList(), d(new TagNodeListPrivate)
57 {
58  d->feedList = feedList;
59  d->tagSet = tagSet;
60 
61  connect(d->tagSet, TQ_SIGNAL(signalTagAdded(const Tag&)), this, TQ_SLOT(slotTagAdded(const Tag&)));
62  connect(d->tagSet, TQ_SIGNAL(signalTagRemoved(const Tag&)), this, TQ_SLOT(slotTagRemoved(const Tag&)));
63  connect(d->tagSet, TQ_SIGNAL(signalTagUpdated(const Tag&)), this, TQ_SLOT(slotTagUpdated(const Tag&)));
64 
65  setRootNode(new TagFolder(i18n("My Tags")));
66 
67  TQValueList<Tag> list = tagSet->toMap().values();
68  for (TQValueList<Tag>::ConstIterator it = list.begin(); it != list.end(); ++it)
69  {
70  insert(new TagNode(*it, d->feedList->rootNode()));
71  }
72 }
73 
74 TagNodeList::~TagNodeList()
75 {
76  emit signalDestroyed(this);
77  delete d;
78  d = 0;
79 }
80 
81 TagFolder* TagNodeList::rootNode() const
82 {
83  return static_cast<TagFolder*>(NodeList::rootNode());
84 }
85 
86 TagNode* TagNodeList::findByTagID(const TQString& tagID)
87 {
88  return d->tagIdToNodeMap[tagID];
89 }
90 
91 bool TagNodeList::insert(TagNode* tagNode)
92 {
93  tagNode->setId(TDEApplication::random());
94  TQString id = tagNode->tag().id();
95  if (!containsTagId(id))
96  {
97  rootNode()->appendChild(tagNode); // TODO: maintain sorting
98  d->tagIdToNodeMap[id] = tagNode;
99  emit signalTagNodeAdded(tagNode);
100  return true;
101  }
102  return false;
103 }
104 
105 bool TagNodeList::remove(TagNode* tagNode)
106 {
107  TQString id = tagNode->tag().id();
108  if (containsTagId(id))
109  {
110  rootNode()->removeChild(tagNode);
111  d->tagIdToNodeMap.remove(id);
112  emit signalTagNodeRemoved(tagNode);
113  return true;
114  }
115  return false;
116 }
117 
118 void TagNodeList::slotNodeDestroyed(TreeNode* node)
119 {
120  TagNode* tagNode = dynamic_cast<TagNode*>(node);
121  TQString id = tagNode ? tagNode->tag().id() : TQString();
122 
123  if (tagNode != 0 && containsTagId(id))
124  {
125  rootNode()->removeChild(tagNode);
126  d->tagIdToNodeMap.remove(id);
127  emit signalTagNodeRemoved(tagNode);
128  }
129 }
130 
131 void TagNodeList::slotNodeAdded(TreeNode* node)
132 {
133  NodeList::slotNodeAdded(node);
134 
135  TagNode* tagNode = dynamic_cast<TagNode*>(node);
136  TQString id = tagNode ? tagNode->tag().id() : TQString();
137 
138  if (tagNode != 0L && !containsTagId(id))
139  {
140  d->tagIdToNodeMap[id] = tagNode;
141  emit signalTagNodeAdded(tagNode);
142  }
143 }
144 
145 void TagNodeList::slotNodeRemoved(Folder* parent, TreeNode* node)
146 {
147  NodeList::slotNodeRemoved(parent, node);
148 
149  TagNode* tagNode = dynamic_cast<TagNode*>(node);
150  TQString id = tagNode ? tagNode->tag().id() : TQString();
151 
152  if (parent == rootNode() && tagNode != 0L && containsTagId(id))
153  {
154  d->tagIdToNodeMap.remove(id);
155  emit signalTagNodeRemoved(tagNode);
156  }
157 }
158 
159 bool TagNodeList::containsTagId(const TQString& tagId)
160 {
161  return d->tagIdToNodeMap.contains(tagId);
162 }
163 
164 TQValueList<TagNode*> TagNodeList::toList() const
165 {
166  return d->tagIdToNodeMap.values();
167 }
168 
169 bool TagNodeList::readFromXML(const TQDomDocument& doc)
170 {
171  return false; // TODO
172 }
173 
174 TQDomDocument TagNodeList::toXML() const
175 {
176  return TQDomDocument();
177 }
178 
179 void TagNodeList::slotTagAdded(const Tag& tag)
180 {
181  if (!containsTagId(tag.id()))
182  {
183  insert(new TagNode(tag, d->feedList->rootNode()));
184  }
185 }
186 
187 void TagNodeList::slotTagUpdated(const Tag& tag)
188 {
189  if (containsTagId(tag.id()))
190  {
191  d->tagIdToNodeMap[tag.id()]->tagChanged();
192  }
193 }
194 void TagNodeList::slotTagRemoved(const Tag& tag)
195 {
196  if (containsTagId(tag.id()))
197  {
198  delete d->tagIdToNodeMap[tag.id()];
199  d->tagIdToNodeMap[tag.id()] = 0;
200  }
201 }
202 
203 
204 } // namespace Akregator
205 
206 #include "tagnodelist.moc"