akregator/src

tagset.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 "tag.h"
00026 #include "tagset.h"
00027 
00028 #include <qdom.h>
00029 #include <qmap.h>
00030 #include <qstring.h>
00031 #include <qstringlist.h>
00032 
00033 namespace Akregator {
00034 
00035 class TagSet::TagSetPrivate 
00036 {
00037     public:
00038     QMap<QString,Tag> map;
00039 };
00040 
00041 TagSet::TagSet(QObject* parent) : QObject(parent), d(new TagSetPrivate)
00042 {
00043 }
00044 
00045 TagSet::~TagSet()
00046 {
00047     QValueList<Tag> tags = d->map.values();
00048     for (QValueList<Tag>::Iterator it = tags.begin(); it != tags.end(); ++it)
00049         (*it).removedFromTagSet(this);
00050     
00051     delete d;
00052     d = 0;
00053 }
00054 
00055 void TagSet::insert(const Tag& tag)
00056 {
00057     if (!d->map.contains(tag.id()))
00058     {
00059         d->map.insert(tag.id(), tag);
00060         tag.addedToTagSet(this);
00061         emit signalTagAdded(tag);
00062     }
00063 }
00064 
00065 void TagSet::remove(const Tag& tag)
00066 {
00067     if (d->map.contains(tag.id()))
00068     {
00069         d->map.remove(tag.id());
00070         tag.removedFromTagSet(this);
00071         emit signalTagRemoved(tag);
00072     }
00073 }
00074 
00075 bool TagSet::containsID(const QString& id) const
00076 {
00077     return d->map.contains(id);
00078 }
00079 
00080 bool TagSet::contains(const Tag& tag) const
00081 {
00082     return d->map.contains(tag.id());
00083 }
00084 
00085 Tag TagSet::findByID(const QString& id) const
00086 {
00087     return d->map.contains(id) ? d->map[id] : Tag();
00088 }
00089 
00090 QMap<QString,Tag> TagSet::toMap() const
00091 {
00092     return d->map;
00093 }
00094 
00095 void TagSet::readFromXML(const QDomDocument& doc)
00096 {
00097     QDomElement root = doc.documentElement();
00098 
00099     if (root.isNull())
00100         return;
00101 
00102     QDomNodeList list = root.elementsByTagName(QString::fromLatin1("tag"));
00103 
00104     for (uint i = 0; i < list.length(); ++i)
00105     {
00106         QDomElement e = list.item(i).toElement();
00107         if (!e.isNull())
00108         {
00109             if (e.hasAttribute(QString::fromLatin1("id")))
00110             {
00111                 QString id = e.attribute(QString::fromLatin1("id"));
00112                 QString name = e.text();
00113                 QString scheme = e.attribute(QString::fromLatin1("scheme"));
00114                 Tag tag(id, name, scheme);
00115                 
00116                 QString icon = e.attribute(QString::fromLatin1("icon"));
00117                 if (!icon.isEmpty())
00118                     tag.setIcon(icon);
00119 
00120                 insert(tag);
00121                 
00122             }
00123         }
00124     }
00125 
00126 }
00127 void TagSet::tagUpdated(const Tag& tag)
00128 {
00129     emit signalTagUpdated(tag);
00130 }
00131         
00132 QDomDocument TagSet::toXML() const
00133 {
00134     QDomDocument doc;
00135     doc.appendChild( doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
00136 
00137     QDomElement root = doc.createElement("tagSet");
00138     root.setAttribute( "version", "0.1" );
00139     doc.appendChild(root);
00140 
00141     QValueList<Tag> list = d->map.values();
00142     for (QValueList<Tag>::ConstIterator it = list.begin(); it != list.end(); ++it)
00143     {    
00144     
00145         QDomElement tn = doc.createElement("tag");
00146         
00147         QDomText text = doc.createTextNode((*it).name());
00148         tn.setAttribute(QString::fromLatin1("id"),(*it).id());
00149         if (!(*it).scheme().isEmpty())
00150             tn.setAttribute(QString::fromLatin1("scheme"),(*it).scheme());
00151         if (!(*it).icon().isEmpty())
00152             tn.setAttribute(QString::fromLatin1("icon"),(*it).icon());
00153         tn.appendChild(text);
00154         root.appendChild(tn);
00155     }
00156     return doc;
00157 }
00158 
00159 } // namespace Akregator
00160 
00161 #include "tagset.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys