OSDN Git Service

1fa84e9e5f768edcb824cfed0b686a1fb2ec88cf
[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 #include "boardmanager.h"
12
13 #include <qdom.h>
14
15 using namespace Kita;
16
17 FavoriteBoards* FavoriteBoards::instance = 0;
18
19 FavoriteBoards::FavoriteBoards()
20 {}
21
22 FavoriteBoards::~FavoriteBoards()
23 {}
24
25 FavoriteBoards* FavoriteBoards::getInstance()
26 {
27     if ( ! instance ) {
28         instance = new FavoriteBoards();
29     }
30     return instance;
31 }
32
33 void FavoriteBoards::append( KURL& url )
34 {
35     if ( ! getInstance() ->m_list.contains( url ) ) {
36         getInstance() ->m_list.append( url );
37         getInstance()->notifyChange();
38     }
39 }
40
41 void FavoriteBoards::remove( KURL& url )
42 {
43     if ( getInstance() ->m_list.contains( url ) ) {
44         getInstance() ->m_list.remove( url );
45         getInstance()->notifyChange();
46     }
47 }
48
49 const QValueList<KURL>& FavoriteBoards::boards()
50 {
51     return getInstance() ->m_list;
52 }
53
54 bool FavoriteBoards::readFromXML( QString& xml )
55 {
56     FavoriteBoards * instance = FavoriteBoards::getInstance();
57     instance->m_list.clear();
58
59     QDomDocument document;
60     if ( ! document.setContent( xml, true ) ) {
61         return false;
62     }
63     QDomElement root = document.documentElement();
64
65     QDomNode node = root.firstChild();
66     while ( ! node.isNull() ) {
67         if ( node.isElement() &&
68                 node.nodeName() == QString( "board" ) &&
69                 node.namespaceURI() == QString( "http://kita.sourceforge.jp/ns/board" ) ) {
70             processChildNode( node );
71         }
72         node = node.nextSibling();
73     }
74     return true;
75 }
76
77 void FavoriteBoards::processChildNode( QDomNode& node )
78 {
79     QDomNode urlNode = node.namedItem( "url" );
80     if ( ! urlNode.isElement() ) return;
81
82     QString urlText = urlNode.toElement().text();
83
84     KURL url = KURL( urlText );
85     if ( url.isValid() ) {
86         //FavoriteBoards::append( url );
87         if ( !getInstance()->m_list.contains( url ) )
88             getInstance()->m_list.append( url );
89     }
90 }
91
92 QString FavoriteBoards::toXML()
93 {
94     QDomDocument document;
95     
96     QDomProcessingInstruction pi = document.createProcessingInstruction( "xml", "version=\"1.0\"" );
97     document.appendChild( pi );
98     
99     QDomElement root = document.createElementNS( "http://kita.sourceforge.jp/ns/boardlist", "boardlist" );
100     document.appendChild( root );
101     
102     QValueList<KURL> boards = FavoriteBoards::boards();
103     QValueList<KURL>::iterator it;
104     for( it = boards.begin(); it != boards.end(); ++it ) {
105         QDomElement board = document.createElementNS( "http://kita.sourceforge.jp/ns/board", "board" );
106         root.appendChild( board );
107         
108         QString boardURL = (*it).url();
109         QDomElement urlElement = document.createElement( "url" );
110         board.appendChild( urlElement );
111         urlElement.appendChild( document.createTextNode( boardURL ) );
112     
113         QString boardName = Kita::BoardManager::boardName( boardURL );
114         QDomElement nameElement = document.createElement( "name" );
115         board.appendChild( nameElement );
116         nameElement.appendChild( document.createTextNode( boardName ) );
117     }
118     return document.toString( 0 );
119 }
120
121 void FavoriteBoards::replace( QString fromURL, QString toURL )
122 {
123     if( FavoriteBoards::getInstance() == NULL ) return;
124     QValueList<KURL>& boardList = FavoriteBoards::getInstance()->m_list;
125     QValueList<KURL>::iterator it;
126     for ( it = boardList.begin(); it != boardList.end(); ++it ) {
127         QString url = (*it).url();
128         if ( url.find( fromURL ) == 0 ) {
129             url = url.replace( 0, fromURL.length(), toURL );
130             boardList.remove( it );
131             boardList.prepend( url );
132             it = boardList.begin();
133         }
134     }
135     FavoriteBoards::getInstance()->notifyChange();
136 }
137
138 void FavoriteBoards::notifyChange()
139 {
140     emit changed();
141 }
142
143 #include "favoriteboards.moc"