1 /***************************************************************************
2 * Copyright (C) 2004 by Kita Developers *
3 * ikemo@users.sourceforge.jp *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
11 #include "favoritethreads.h"
13 #include <QtCore/QList>
14 #include <QtXml/QDomDocument>
16 #include "boarddatabase.h"
17 #include "datmanager.h"
18 #include "kita_misc.h"
20 #include "threadindex.h"
25 FavoriteThreadItem::FavoriteThreadItem()
28 FavoriteThreadItem::~ FavoriteThreadItem()
31 bool FavoriteThreadItem::operator==(const FavoriteThreadItem& item) const
33 return (m_datUrl == item.m_datUrl);
37 FavoriteThreads* FavoriteThreads::instance = 0;
39 FavoriteThreads::FavoriteThreads()
42 FavoriteThreads::~FavoriteThreads()
45 FavoriteThreads* FavoriteThreads::getInstance()
48 instance = new FavoriteThreads();
53 void FavoriteThreads::insert(const QString& datUrl)
55 if (! m_threadList.contains(datUrl)) {
56 m_threadList.append(datUrl);
60 void FavoriteThreads::remove(const QString& datUrl)
62 m_threadList.removeAll(datUrl);
65 bool FavoriteThreads::contains(const QString& datUrl)
67 if (m_threadList.contains(datUrl)) {
74 const QList<FavoriteThreadItem> FavoriteThreads::threadList() const
79 bool FavoriteThreads::readFromXML(const QString& xml)
81 FavoriteThreads * instance = FavoriteThreads::getInstance();
82 instance->m_threadList.clear();
84 QDomDocument document;
85 if (! document.setContent(xml, true)) {
89 QDomElement root = document.documentElement();
91 QDomNode node = root.firstChild();
92 while (!node.isNull()) {
93 if (node.isElement() &&
94 node.nodeName() == QString("thread") &&
95 node.namespaceURI() == QString("http://kita.sourceforge.jp/ns/thread")) {
96 processThreadNode(node);
98 node = node.nextSibling();
103 void FavoriteThreads::processThreadNode(QDomNode& node)
105 QDomNode datUrlNode = node.namedItem("daturl");
106 QDomNode nameNode = node.namedItem("name");
108 if (!datUrlNode.isElement() || !nameNode.isElement()) return ;
110 QString url = datUrlNode.toElement().text();
111 QString name = nameNode.toElement().text();
113 KUrl datUrl = Kita::getDatUrl(url);
114 Thread* thread = Thread::getByUrl(datUrl);
115 thread->setThreadName(name);
116 ThreadIndex threadIndex(datUrl);
117 threadIndex.loadIndex(thread);
119 FavoriteThreads::getInstance() ->insert(datUrl.prettyUrl());
122 const QString FavoriteThreads::toXML() const
124 QDomDocument document;
126 // QDomProcessingInstruction pi = document.createProcessingInstruction("xml", "version=\"1.0\"");
127 // document.appendChild(pi);
129 QDomElement root = document.createElementNS("http://kita.sourceforge.jp/ns/favorites", "favorites");
130 document.appendChild(root);
132 QList<FavoriteThreadItem>::const_iterator it;
133 for (it = threadList().begin(); it != threadList().end(); ++it) {
134 QString datUrl = (*it).m_datUrl;
135 QDomElement threadElement = document.createElementNS("http://kita.sourceforge.jp/ns/thread", "thread");
136 root.appendChild(threadElement);
138 QDomElement datUrlElement = document.createElement("daturl");
139 threadElement.appendChild(datUrlElement);
140 datUrlElement.appendChild(document.createTextNode(datUrl));
142 DatManager datManager(datUrl);
143 QString threadName = datManager.threadName();
144 QDomElement nameElement = document.createElement("name");
145 threadElement.appendChild(nameElement);
146 nameElement.appendChild(document.createTextNode(threadName));
148 QString resNum = QString::number(datManager.getResNum());
149 QDomElement resNumElement = document.createElement("resnum");
150 threadElement.appendChild(resNumElement);
151 resNumElement.appendChild(document.createTextNode(resNum));
154 QDomElement board = document.createElementNS("http://kita.sourceforge.jp/ns/board", "board");
155 threadElement.appendChild(board);
157 BoardDatabase db(datUrl);
158 QString boardUrl = db.boardUrl();
159 QDomElement boardUrlElement = document.createElement("url");
160 board.appendChild(boardUrlElement);
161 boardUrlElement.appendChild(document.createTextNode(boardUrl));
163 BoardDatabase db2(boardUrl);
164 QString boardName = db2.boardName();
165 QDomElement boardNameElement = document.createElement("name");
166 board.appendChild(boardNameElement);
167 boardNameElement.appendChild(document.createTextNode(boardName));
169 return document.toString(0);
172 void FavoriteThreads::replace(const QString& fromUrl, const QString& toUrl)
174 if (FavoriteThreads::getInstance() == 0) return ;
175 QList<FavoriteThreadItem>& threadList = FavoriteThreads::getInstance() ->m_threadList;
176 QList<FavoriteThreadItem>::iterator it;
177 for (it = threadList.begin(); it != threadList.end(); ++it) {
178 QString url = (*it).m_datUrl;
179 if (url.indexOf(fromUrl) == 0) {
180 url = url.replace(0, fromUrl.length(), toUrl);
181 threadList.erase(it);
182 threadList.prepend(url);
183 it = threadList.begin();
188 QString FavoriteThreads::getDatUrl(int i)
190 if (getInstance() ->m_threadList.count() > i) {
191 return getInstance() ->m_threadList[ i ].m_datUrl;