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 ***************************************************************************/
10 #include "favoriteboards.h"
12 #include <QtXml/QDomDocument>
14 #include "boarddatabase.h"
18 FavoriteBoards* FavoriteBoards::instance = 0;
20 FavoriteBoards::FavoriteBoards()
23 FavoriteBoards::~FavoriteBoards()
26 FavoriteBoards* FavoriteBoards::getInstance()
29 instance = new FavoriteBoards();
34 void FavoriteBoards::append(KUrl& url)
36 if (! getInstance() ->m_list.contains(url)) {
37 getInstance() ->m_list.append(url);
38 getInstance() ->notifyChange();
42 void FavoriteBoards::remove(KUrl& url)
44 if (getInstance() ->m_list.contains(url)) {
45 getInstance() ->m_list.removeAll(url);
46 getInstance() ->notifyChange();
50 const QList<KUrl>& FavoriteBoards::boards()
52 return getInstance() ->m_list;
55 bool FavoriteBoards::readFromXML(QString& xml)
57 FavoriteBoards * instance = FavoriteBoards::getInstance();
58 instance->m_list.clear();
60 QDomDocument document;
61 if (! document.setContent(xml, true)) {
64 QDomElement root = document.documentElement();
66 QDomNode node = root.firstChild();
67 while (! node.isNull()) {
68 if (node.isElement() &&
69 node.nodeName() == QString("board") &&
70 node.namespaceURI() == QString("http://kita.sourceforge.jp/ns/board")) {
71 processChildNode(node);
73 node = node.nextSibling();
78 void FavoriteBoards::processChildNode(QDomNode& node)
80 QDomNode urlNode = node.namedItem("url");
81 if (! urlNode.isElement()) return ;
83 QString urlText = urlNode.toElement().text();
85 KUrl url = KUrl(urlText);
87 //FavoriteBoards::append(url);
88 if (!getInstance() ->m_list.contains(url))
89 getInstance() ->m_list.append(url);
93 QString FavoriteBoards::toXML()
95 QDomDocument document;
97 QDomProcessingInstruction pi = document.createProcessingInstruction("xml", "version=\"1.0\"");
98 document.appendChild(pi);
100 QDomElement root = document.createElementNS("http://kita.sourceforge.jp/ns/boardlist", "boardlist");
101 document.appendChild(root);
103 QList<KUrl> boards = FavoriteBoards::boards();
104 QList<KUrl>::iterator it;
105 for (it = boards.begin(); it != boards.end(); ++it) {
106 QDomElement board = document.createElementNS("http://kita.sourceforge.jp/ns/board", "board");
107 root.appendChild(board);
109 QString boardUrl = (*it).url();
110 QDomElement urlElement = document.createElement("url");
111 board.appendChild(urlElement);
112 urlElement.appendChild(document.createTextNode(boardUrl));
114 BoardDatabase db(boardUrl);
115 QString boardName = db.boardName();
116 QDomElement nameElement = document.createElement("name");
117 board.appendChild(nameElement);
118 nameElement.appendChild(document.createTextNode(boardName));
120 return document.toString(0);
123 void FavoriteBoards::replace(const QString& fromUrl, const QString& toUrl)
125 if (FavoriteBoards::getInstance() == 0) return ;
126 QList<KUrl>& boardList = FavoriteBoards::getInstance() ->m_list;
127 for (QList<KUrl>::iterator it = boardList.begin(); it != boardList.end(); ++it) {
128 QString url = (*it).url();
129 if (url.startsWith(fromUrl)) {
130 url = url.replace(0, fromUrl.length(), toUrl);
134 FavoriteBoards::getInstance() ->notifyChange();
137 void FavoriteBoards::notifyChange()