implement update interval of list; can be disabled / changed by user
[kdenetwork.git] / kopete / protocols / irc / ircnetwork.cpp
blobb09842cfaa9b29442f261d05ca22699ec350a0e4
1 /*
2 ircaccount.cpp - IRC Account
4 Copyright (c) 2002 by Nick Betcher <nbetcher@kde.org>
5 Copyright (c) 2003-2004 by Jason Keirstead <jason@keirstead.org>
6 Copyright (c) 2003-2007 by Michel Hermier <michel.hermier@gmail.com>
8 Kopete (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
10 *************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 *************************************************************************
20 #include "ircnetwork.h"
22 #include <kdebug.h>
23 #include <kstandarddirs.h> // for locate
25 #include <qdom.h>
26 #include <qfile.h>
28 struct IRC::Networks::Private {
29 IRC::NetworkList networks;
32 using namespace IRC;
34 Networks::Networks()
35 : d(new Private)
37 slotReadNetworks();
40 Networks::~Networks()
42 delete d;
45 Networks *Networks::self()
47 static Networks *self = new IRC::Networks;
48 return self;
51 const NetworkList &Networks::networks() const
53 return d->networks;
56 void Networks::slotReadNetworks()
58 d->networks.clear();
60 QFile xmlFile( KStandardDirs::locate( "appdata", "ircnetworks.xml" ) );
61 xmlFile.open( QIODevice::ReadOnly );
63 #if 0
64 // FIXME
65 QDomDocument doc;
66 doc.setContent( &xmlFile );
67 QDomElement networkNode = doc.documentElement().firstChild().toElement();
68 while( !networkNode.isNull () )
70 Network network;
72 QDomElement networkChild = networkNode.firstChild().toElement();
73 while( !networkChild.isNull() )
75 if( networkChild.tagName() == "name" )
76 network.name = networkChild.text();
77 else if( networkChild.tagName() == "description" )
78 network.description = networkChild.text();
79 else if( networkChild.tagName() == "servers" )
81 QDomElement server = networkChild.firstChild().toElement();
82 while( !server.isNull() )
84 Host host;
86 QDomElement serverChild = server.firstChild().toElement();
87 while( !serverChild.isNull() )
89 if( serverChild.tagName() == "host" )
90 host.host = serverChild.text();
91 else if( serverChild.tagName() == "port" )
92 host.port = serverChild.text().toInt();
93 else if( serverChild.tagName() == "useSSL" )
94 host.ssl = ( serverChild.text() == "true" );
96 serverChild = serverChild.nextSibling().toElement();
99 network.hosts.append( host );
100 // d->hosts.insert( host->host, host );
101 server = server.nextSibling().toElement();
104 networkChild = networkChild.nextSibling().toElement();
107 d->networks.append(network);
108 networkNode = networkNode.nextSibling().toElement();
110 #endif
112 xmlFile.close();
115 bool Networks::slotSaveNetworkConfig() const
118 // store any changes in the UI
119 storeCurrentNetwork();
120 kDebug( 14120 ) << m_uiCurrentHostSelection;
121 storeCurrentHost();
123 QDomDocument doc("irc-networks");
124 QDomNode root = doc.appendChild( doc.createElement("networks") );
126 foreach(const Network &network, d->networks)
128 QDomNode networkNode = root.appendChild( doc.createElement("network") );
129 QDomNode nameNode = networkNode.appendChild( doc.createElement("name") );
130 nameNode.appendChild( doc.createTextNode( net->name ) );
132 QDomNode descNode = networkNode.appendChild( doc.createElement("description") );
133 descNode.appendChild( doc.createTextNode( net->description ) );
135 QDomNode serversNode = networkNode.appendChild( doc.createElement("servers") );
137 foreach(const Host &host, network.host)
139 QDomNode serverNode = serversNode.appendChild( doc.createElement("server") );
141 QDomNode hostNode = serverNode.appendChild( doc.createElement("host") );
142 hostNode.appendChild( doc.createTextNode( (*it2)->host ) );
144 QDomNode portNode = serverNode.appendChild( doc.createElement("port" ) );
145 portNode.appendChild( doc.createTextNode( QString::number( (*it2)->port ) ) );
147 QDomNode sslNode = serverNode.appendChild( doc.createElement("useSSL") );
148 sslNode.appendChild( doc.createTextNode( (*it2)->ssl ? "true" : "false" ) );
152 // kDebug(14121) << doc.toString(4);
153 QFile xmlFile( KStandardDirs::locateLocal( "appdata", "ircnetworks.xml" ) );
155 if (xmlFile.open(QIODevice::WriteOnly))
157 QTextStream stream( &xmlFile );
158 stream << doc.toString(4);
159 xmlFile.close();
160 return true;
163 kDebug(14121) << "Failed to save the Networks definition file";
164 return false;
167 void Networks::addNetwork( IRCNetwork *network )
169 m_networks.insert( network->name, network );
170 // slotSaveNetworkConfig();
174 #include "ircnetwork.moc"