26 #include <tqdatetime.h>
29 #include <tqvaluelist.h>
38 #include "treenodevisitor.h"
42 class FeedList::FeedListPrivate
46 TQMap<TQString, TQValueList<Feed*> > urlMap;
47 AddNodeVisitor* addNodeVisitor;
48 RemoveNodeVisitor* removeNodeVisitor;
51 class FeedList::AddNodeVisitor :
public TreeNodeVisitor
54 AddNodeVisitor(FeedList* list) : m_list(list) {}
57 virtual bool visitFeed(Feed* node)
59 m_list->idMap()->insert(node->id(), node);
60 m_list->flatList()->append(node);
68 class FeedList::RemoveNodeVisitor :
public TreeNodeVisitor
71 RemoveNodeVisitor(FeedList* list) : m_list(list) {}
73 virtual bool visitFeed(Feed* node)
75 m_list->d->urlMap[node->xmlUrl()].remove(node);
83 FeedList::FeedList(TQObject *parent,
const char *name)
84 : NodeList(parent, name), d(new FeedListPrivate)
86 d->addNodeVisitor =
new AddNodeVisitor(
this);
87 d->removeNodeVisitor =
new RemoveNodeVisitor(
this);
89 Folder* rootNode =
new Folder(i18n(
"All Feeds"));
91 setRootNode(rootNode);
92 addNode(rootNode,
true);
95 void FeedList::addNode(TreeNode* node,
bool preserveID)
97 NodeList::addNode(node, preserveID);
98 d->addNodeVisitor->visit(node);
101 void FeedList::removeNode(TreeNode* node)
103 NodeList::removeNode(node);
104 d->removeNodeVisitor->visit(node);
107 void FeedList::parseChildNodes(TQDomNode &node, Folder* parent)
109 TQDomElement e = node.toElement();
113 TQString title = e.hasAttribute(
"text") ? e.attribute(
"text") : e.attribute(
"title");
115 if (e.hasAttribute(
"xmlUrl") || e.hasAttribute(
"xmlurl") || e.hasAttribute(
"xmlURL") )
120 if (!d->urlMap[feed->xmlUrl()].contains(feed))
121 d->urlMap[feed->xmlUrl()].append(feed);
122 parent->appendChild(feed);
128 parent->appendChild(fg);
130 if (e.hasChildNodes())
132 TQDomNode child = e.firstChild();
133 while(!child.isNull())
135 parseChildNodes(child, fg);
136 child = child.nextSibling();
145 TQDomElement root = doc.documentElement();
147 kdDebug() <<
"loading OPML feed " << root.tagName().lower() << endl;
149 kdDebug() <<
"measuring startup time: START" << endl;
153 if (root.tagName().lower() !=
"opml")
157 TQDomNode bodyNode = root.firstChild();
159 while (!bodyNode.isNull() && bodyNode.toElement().tagName().lower() !=
"body")
160 bodyNode = bodyNode.nextSibling();
163 if (bodyNode.isNull())
165 kdDebug() <<
"Failed to acquire body node, markup broken?" << endl;
169 TQDomElement body = bodyNode.toElement();
171 TQDomNode i = body.firstChild();
175 parseChildNodes(i, rootNode());
179 for (
TreeNode* i = rootNode()->firstChild(); i && i != rootNode(); i = i->next() )
182 uint
id = generateID();
184 idMap()->insert(
id, i);
187 kdDebug() <<
"measuring startup time: STOP, " << spent.elapsed() <<
"ms" << endl;
188 kdDebug() <<
"Number of articles loaded: " << rootNode()->totalCount() << endl;
194 emit signalDestroyed(
this);
196 delete d->addNodeVisitor;
197 delete d->removeNodeVisitor;
204 if (d->urlMap[feedURL].isEmpty())
207 return *(d->urlMap[feedURL].begin());
210 Article FeedList::findArticle(
const TQString& feedURL,
const TQString& guid)
const
222 if ( !flatList()->contains(parent) )
225 TQValueList<TreeNode*> children = list->rootNode()->children();
227 TQValueList<TreeNode*>::ConstIterator end( children.end() );
228 for (TQValueList<TreeNode*>::ConstIterator it = children.begin(); it != end; ++it)
230 list->rootNode()->removeChild(*it);
239 doc.appendChild( doc.createProcessingInstruction(
"xml",
"version=\"1.0\" encoding=\"UTF-8\"" ) );
241 TQDomElement root = doc.createElement(
"opml" );
242 root.setAttribute(
"version",
"1.0" );
243 doc.appendChild( root );
245 TQDomElement head = doc.createElement(
"head" );
246 root.appendChild( head );
248 TQDomElement ti = doc.createElement(
"text" );
249 head.appendChild( ti );
251 TQDomText t = doc.createTextNode( title() );
254 TQDomElement body = doc.createElement(
"body" );
255 root.appendChild( body );
257 TQValueList<TreeNode*> children = rootNode()->children();
259 TQValueList<TreeNode*>::ConstIterator end( children.end() );
261 for (TQValueList<TreeNode*>::ConstIterator it = children.begin(); it != end; ++it)
262 body.appendChild( (*it)->toOPML(body, doc) );
268 #include "feedlist.moc"