OSDN Git Service

Add BoardDatabase class
[kita/kita.git] / kita / src / libkita / favoriteboards.cpp
1 /***************************************************************************
2 *   Copyright (C) 2004 by Kita Developers                                 *
3 *   ikemo@users.sourceforge.jp                                            *
4 *                                                                         *
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"
11
12 #include <QtXml/QDomDocument>
13
14 #include "boarddatabase.h"
15
16 using namespace Kita;
17
18 FavoriteBoards* FavoriteBoards::instance = 0;
19
20 FavoriteBoards::FavoriteBoards()
21 {}
22
23 FavoriteBoards::~FavoriteBoards()
24 {}
25
26 FavoriteBoards* FavoriteBoards::getInstance()
27 {
28     if (! instance) {
29         instance = new FavoriteBoards();
30     }
31     return instance;
32 }
33
34 void FavoriteBoards::append(KUrl& url)
35 {
36     if (! getInstance() ->m_list.contains(url)) {
37         getInstance() ->m_list.append(url);
38         getInstance() ->notifyChange();
39     }
40 }
41
42 void FavoriteBoards::remove(KUrl& url)
43 {
44     if (getInstance() ->m_list.contains(url)) {
45         getInstance() ->m_list.removeAll(url);
46         getInstance() ->notifyChange();
47     }
48 }
49
50 const QList<KUrl>& FavoriteBoards::boards()
51 {
52     return getInstance() ->m_list;
53 }
54
55 bool FavoriteBoards::readFromXML(QString& xml)
56 {
57     FavoriteBoards * instance = FavoriteBoards::getInstance();
58     instance->m_list.clear();
59
60     QDomDocument document;
61     if (! document.setContent(xml, true)) {
62         return false;
63     }
64     QDomElement root = document.documentElement();
65
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);
72         }
73         node = node.nextSibling();
74     }
75     return true;
76 }
77
78 void FavoriteBoards::processChildNode(QDomNode& node)
79 {
80     QDomNode urlNode = node.namedItem("url");
81     if (! urlNode.isElement()) return ;
82
83     QString urlText = urlNode.toElement().text();
84
85     KUrl url = KUrl(urlText);
86     if (url.isValid()) {
87         //FavoriteBoards::append(url);
88         if (!getInstance() ->m_list.contains(url))
89             getInstance() ->m_list.append(url);
90     }
91 }
92
93 QString FavoriteBoards::toXML()
94 {
95     QDomDocument document;
96
97     QDomProcessingInstruction pi = document.createProcessingInstruction("xml", "version=\"1.0\"");
98     document.appendChild(pi);
99
100     QDomElement root = document.createElementNS("http://kita.sourceforge.jp/ns/boardlist", "boardlist");
101     document.appendChild(root);
102
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);
108
109         QString boardUrl = (*it).url();
110         QDomElement urlElement = document.createElement("url");
111         board.appendChild(urlElement);
112         urlElement.appendChild(document.createTextNode(boardUrl));
113
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));
119     }
120     return document.toString(0);
121 }
122
123 void FavoriteBoards::replace(const QString& fromUrl, const QString& toUrl)
124 {
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);
131             *it = url;
132         }
133     }
134     FavoriteBoards::getInstance() ->notifyChange();
135 }
136
137 void FavoriteBoards::notifyChange()
138 {
139     emit changed();
140 }