akregator/src

tagnodelist.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 "feedlist.h"
00026 #include "tag.h"
00027 #include "tagnode.h"
00028 #include "tagnodelist.h"
00029 #include "tagset.h"
00030 #include "folder.h"
00031 #include "tagfolder.h"
00032 
00033 #include <qdom.h>
00034 #include <qmap.h>
00035 #include <qstring.h>
00036 #include <qvaluelist.h>
00037 
00038 #include <kapplication.h>
00039 #include <klocale.h>
00040 
00041 namespace Akregator {
00042 
00043 class TagNodeList::TagNodeListPrivate
00044 {
00045     public:
00046     FeedList* feedList;
00047     TagSet* tagSet;
00048     QMap<QString, TagNode*> tagIdToNodeMap;
00049 };
00050 
00051 FeedList* TagNodeList::feedList() const
00052 {
00053     return d->feedList;
00054 }
00055 
00056 TagNodeList::TagNodeList(FeedList* feedList, TagSet* tagSet) :  NodeList(), d(new TagNodeListPrivate)
00057 {
00058     d->feedList = feedList;
00059     d->tagSet = tagSet;
00060  
00061     connect(d->tagSet, SIGNAL(signalTagAdded(const Tag&)), this, SLOT(slotTagAdded(const Tag&)));
00062     connect(d->tagSet, SIGNAL(signalTagRemoved(const Tag&)), this, SLOT(slotTagRemoved(const Tag&)));
00063     connect(d->tagSet, SIGNAL(signalTagUpdated(const Tag&)), this, SLOT(slotTagUpdated(const Tag&)));
00064 
00065     setRootNode(new TagFolder(i18n("My Tags")));
00066 
00067     QValueList<Tag> list = tagSet->toMap().values();
00068     for (QValueList<Tag>::ConstIterator it = list.begin(); it != list.end(); ++it)
00069     {
00070        insert(new TagNode(*it, d->feedList->rootNode()));
00071     }
00072 }
00073 
00074 TagNodeList::~TagNodeList()
00075 {
00076     emit signalDestroyed(this);
00077     delete d;
00078     d = 0;
00079 }
00080 
00081 TagFolder* TagNodeList::rootNode() const
00082 {
00083     return static_cast<TagFolder*>(NodeList::rootNode());
00084 }
00085 
00086 TagNode* TagNodeList::findByTagID(const QString& tagID)
00087 {
00088     return d->tagIdToNodeMap[tagID];
00089 }
00090 
00091 bool TagNodeList::insert(TagNode* tagNode)
00092 {
00093     tagNode->setId(KApplication::random());
00094     QString id = tagNode->tag().id();
00095     if (!containsTagId(id))
00096     {
00097         rootNode()->appendChild(tagNode); // TODO: maintain sorting
00098         d->tagIdToNodeMap[id] = tagNode;
00099         emit signalTagNodeAdded(tagNode);
00100         return true;
00101     }
00102     return false;
00103 }
00104 
00105 bool TagNodeList::remove(TagNode* tagNode)
00106 {
00107     QString id = tagNode->tag().id();
00108     if (containsTagId(id))
00109     {
00110         rootNode()->removeChild(tagNode);
00111         d->tagIdToNodeMap.remove(id);
00112         emit signalTagNodeRemoved(tagNode);
00113         return true;
00114     }
00115     return false;
00116 }
00117 
00118 void TagNodeList::slotNodeDestroyed(TreeNode* node)
00119 {
00120     TagNode* tagNode = dynamic_cast<TagNode*>(node);
00121     QString id = tagNode ? tagNode->tag().id() : QString::null;
00122     
00123     if (tagNode != 0 && containsTagId(id))
00124     {
00125         rootNode()->removeChild(tagNode);
00126         d->tagIdToNodeMap.remove(id);
00127         emit signalTagNodeRemoved(tagNode);
00128     }
00129 }
00130 
00131 void TagNodeList::slotNodeAdded(TreeNode* node)
00132 {
00133     NodeList::slotNodeAdded(node);
00134 
00135     TagNode* tagNode = dynamic_cast<TagNode*>(node);
00136     QString id = tagNode ? tagNode->tag().id() : QString::null;
00137     
00138     if (tagNode != 0L && !containsTagId(id))
00139     {
00140        d->tagIdToNodeMap[id] = tagNode;
00141        emit signalTagNodeAdded(tagNode);
00142     }
00143 }
00144 
00145 void TagNodeList::slotNodeRemoved(Folder* parent, TreeNode* node)
00146 {
00147     NodeList::slotNodeRemoved(parent, node);
00148 
00149     TagNode* tagNode = dynamic_cast<TagNode*>(node);
00150     QString id = tagNode ? tagNode->tag().id() : QString::null;
00151     
00152     if (parent == rootNode() && tagNode != 0L && containsTagId(id))
00153     {
00154         d->tagIdToNodeMap.remove(id);
00155         emit signalTagNodeRemoved(tagNode);
00156     }
00157 }
00158 
00159 bool TagNodeList::containsTagId(const QString& tagId)
00160 {
00161     return d->tagIdToNodeMap.contains(tagId);
00162 }
00163 
00164 QValueList<TagNode*> TagNodeList::toList() const
00165 {
00166     return d->tagIdToNodeMap.values();
00167 }
00168 
00169 bool TagNodeList::readFromXML(const QDomDocument& doc)
00170 {
00171     return false; // TODO
00172 }
00173 
00174 QDomDocument TagNodeList::toXML() const
00175 {
00176     return QDomDocument();
00177 }
00178 
00179 void TagNodeList::slotTagAdded(const Tag& tag)
00180 {
00181     if (!containsTagId(tag.id()))
00182     {
00183         insert(new TagNode(tag, d->feedList->rootNode()));
00184     }
00185 }
00186 
00187 void TagNodeList::slotTagUpdated(const Tag& tag)
00188 {
00189     if (containsTagId(tag.id()))
00190     {
00191         d->tagIdToNodeMap[tag.id()]->tagChanged();
00192     }
00193 }
00194 void TagNodeList::slotTagRemoved(const Tag& tag)
00195 {
00196     if (containsTagId(tag.id()))
00197     {
00198         delete d->tagIdToNodeMap[tag.id()];
00199         d->tagIdToNodeMap[tag.id()] = 0;
00200     }
00201 }
00202 
00203      
00204 } // namespace Akregator
00205 
00206 #include "tagnodelist.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys