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"
11 #include "boardmanager.h"
17 FavoriteBoards* FavoriteBoards::instance = 0;
19 FavoriteBoards::FavoriteBoards()
22 FavoriteBoards::~FavoriteBoards()
25 FavoriteBoards* FavoriteBoards::getInstance()
28 instance = new FavoriteBoards();
33 void FavoriteBoards::append( KURL& url )
35 if ( ! getInstance() ->m_list.contains( url ) ) {
36 getInstance() ->m_list.append( url );
37 getInstance()->notifyChange();
41 void FavoriteBoards::remove( KURL& url )
43 if ( getInstance() ->m_list.contains( url ) ) {
44 getInstance() ->m_list.remove( url );
45 getInstance()->notifyChange();
49 const QValueList<KURL>& FavoriteBoards::boards()
51 return getInstance() ->m_list;
54 bool FavoriteBoards::readFromXML( QString& xml )
56 FavoriteBoards * instance = FavoriteBoards::getInstance();
57 instance->m_list.clear();
59 QDomDocument document;
60 if ( ! document.setContent( xml, true ) ) {
63 QDomElement root = document.documentElement();
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 );
72 node = node.nextSibling();
77 void FavoriteBoards::processChildNode( QDomNode& node )
79 QDomNode urlNode = node.namedItem( "url" );
80 if ( ! urlNode.isElement() ) return;
82 QString urlText = urlNode.toElement().text();
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 );
92 QString FavoriteBoards::toXML()
94 QDomDocument document;
96 QDomProcessingInstruction pi = document.createProcessingInstruction( "xml", "version=\"1.0\"" );
97 document.appendChild( pi );
99 QDomElement root = document.createElementNS( "http://kita.sourceforge.jp/ns/boardlist", "boardlist" );
100 document.appendChild( root );
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 );
108 QString boardURL = (*it).url();
109 QDomElement urlElement = document.createElement( "url" );
110 board.appendChild( urlElement );
111 urlElement.appendChild( document.createTextNode( boardURL ) );
113 QString boardName = Kita::BoardManager::boardName( boardURL );
114 QDomElement nameElement = document.createElement( "name" );
115 board.appendChild( nameElement );
116 nameElement.appendChild( document.createTextNode( boardName ) );
118 return document.toString( 0 );
121 void FavoriteBoards::replace( QString fromURL, QString toURL )
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();
135 FavoriteBoards::getInstance()->notifyChange();
138 void FavoriteBoards::notifyChange()
143 #include "favoriteboards.moc"