summaryrefslogtreecommitdiffstats
path: root/akregator/src/tagset.cpp
blob: fd1cd42f02c11048a41856a5d90215b1ad1af00c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
    This file is part of Akregator.

    Copyright (C) 2005 Frank Osterfeld <frank.osterfeld at kdemail.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

    As a special exception, permission is given to link this program
    with any edition of TQt, and distribute the resulting executable,
    without including the source code for TQt in the source distribution.
*/

#include "tag.h"
#include "tagset.h"

#include <tqdom.h>
#include <tqmap.h>
#include <tqstring.h>
#include <tqstringlist.h>

namespace Akregator {

class TagSet::TagSetPrivate 
{
    public:
    TQMap<TQString,Tag> map;
};

TagSet::TagSet(TQObject* parent) : TQObject(parent), d(new TagSetPrivate)
{
}

TagSet::~TagSet()
{
    TQValueList<Tag> tags = d->map.values();
    for (TQValueList<Tag>::Iterator it = tags.begin(); it != tags.end(); ++it)
        (*it).removedFromTagSet(this);
    
    delete d;
    d = 0;
}

void TagSet::insert(const Tag& tag)
{
    if (!d->map.contains(tag.id()))
    {
        d->map.insert(tag.id(), tag);
        tag.addedToTagSet(this);
        emit signalTagAdded(tag);
    }
}

void TagSet::remove(const Tag& tag)
{
    if (d->map.contains(tag.id()))
    {
        d->map.remove(tag.id());
        tag.removedFromTagSet(this);
        emit signalTagRemoved(tag);
    }
}

bool TagSet::containsID(const TQString& id) const
{
    return d->map.contains(id);
}

bool TagSet::contains(const Tag& tag) const
{
    return d->map.contains(tag.id());
}

Tag TagSet::findByID(const TQString& id) const
{
    return d->map.contains(id) ? d->map[id] : Tag();
}

TQMap<TQString,Tag> TagSet::toMap() const
{
    return d->map;
}

void TagSet::readFromXML(const TQDomDocument& doc)
{
    TQDomElement root = doc.documentElement();

    if (root.isNull())
        return;

    TQDomNodeList list = root.elementsByTagName(TQString::tqfromLatin1("tag"));

    for (uint i = 0; i < list.length(); ++i)
    {
        TQDomElement e = list.item(i).toElement();
        if (!e.isNull())
        {
            if (e.hasAttribute(TQString::tqfromLatin1("id")))
            {
                TQString id = e.attribute(TQString::tqfromLatin1("id"));
                TQString name = e.text();
                TQString scheme = e.attribute(TQString::tqfromLatin1("scheme"));
                Tag tag(id, name, scheme);
                
                TQString icon = e.attribute(TQString::tqfromLatin1("icon"));
                if (!icon.isEmpty())
                    tag.setIcon(icon);

                insert(tag);
                
            }
        }
    }

}
void TagSet::tagUpdated(const Tag& tag)
{
    emit signalTagUpdated(tag);
}
        
TQDomDocument TagSet::toXML() const
{
    TQDomDocument doc;
    doc.appendChild( doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );

    TQDomElement root = doc.createElement("tagSet");
    root.setAttribute( "version", "0.1" );
    doc.appendChild(root);

    TQValueList<Tag> list = d->map.values();
    for (TQValueList<Tag>::ConstIterator it = list.begin(); it != list.end(); ++it)
    {    
    
        TQDomElement tn = doc.createElement("tag");
        
        TQDomText text = doc.createTextNode((*it).name());
        tn.setAttribute(TQString::tqfromLatin1("id"),(*it).id());
        if (!(*it).scheme().isEmpty())
            tn.setAttribute(TQString::tqfromLatin1("scheme"),(*it).scheme());
        if (!(*it).icon().isEmpty())
            tn.setAttribute(TQString::tqfromLatin1("icon"),(*it).icon());
        tn.appendChild(text);
        root.appendChild(tn);
    }
    return doc;
}

} // namespace Akregator

#include "tagset.moc"