begin my work on the IRC-protocol, making the editaccount dialog and the network...
[kdenetwork.git] / kopete / protocols / irc / ircnetwork.cpp
blob6067ed7b910f96c04cc076b3856bf22b6e158ab8
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::setNetworks( const IRC::NetworkList& networks )
58 d->networks=networks;
61 void Networks::slotReadNetworks()
63 d->networks.clear();
65 QFile xmlFile( KStandardDirs::locate( "appdata", "ircnetworks.xml" ) );
66 xmlFile.open( QIODevice::ReadOnly );
69 // FIXME
70 QDomDocument doc;
71 doc.setContent( &xmlFile );
72 QDomElement networkNode = doc.documentElement().firstChild().toElement();
73 while( !networkNode.isNull () )
75 Network network;
77 QDomElement networkChild = networkNode.firstChild().toElement();
78 while( !networkChild.isNull() )
80 if( networkChild.tagName() == "name" )
81 network.name = networkChild.text();
82 else if( networkChild.tagName() == "description" )
83 network.description = networkChild.text();
84 else if( networkChild.tagName() == "servers" )
86 QDomElement server = networkChild.firstChild().toElement();
87 while( !server.isNull() )
89 Host host;
91 QDomElement serverChild = server.firstChild().toElement();
92 while( !serverChild.isNull() )
94 if( serverChild.tagName() == "host" )
95 host.host = serverChild.text();
96 else if( serverChild.tagName() == "port" )
97 host.port = serverChild.text().toInt();
98 else if( serverChild.tagName() == "useSSL" )
99 host.ssl = ( serverChild.text() == "true" );
101 serverChild = serverChild.nextSibling().toElement();
104 network.hosts.append( host );
105 // d->hosts.insert( host->host, host );
106 server = server.nextSibling().toElement();
109 networkChild = networkChild.nextSibling().toElement();
112 d->networks.append(network);
113 networkNode = networkNode.nextSibling().toElement();
117 xmlFile.close();
120 bool Networks::slotSaveNetworkConfig() const
123 // store any changes in the UI
124 storeCurrentNetwork();
125 kDebug( 14120 ) << m_uiCurrentHostSelection;
126 storeCurrentHost();
128 QDomDocument doc("irc-networks");
129 QDomNode root = doc.appendChild( doc.createElement("networks") );
131 foreach(const Network &network, d->networks)
133 QDomNode networkNode = root.appendChild( doc.createElement("network") );
134 QDomNode nameNode = networkNode.appendChild( doc.createElement("name") );
135 nameNode.appendChild( doc.createTextNode( net->name ) );
137 QDomNode descNode = networkNode.appendChild( doc.createElement("description") );
138 descNode.appendChild( doc.createTextNode( net->description ) );
140 QDomNode serversNode = networkNode.appendChild( doc.createElement("servers") );
142 foreach(const Host &host, network.host)
144 QDomNode serverNode = serversNode.appendChild( doc.createElement("server") );
146 QDomNode hostNode = serverNode.appendChild( doc.createElement("host") );
147 hostNode.appendChild( doc.createTextNode( (*it2)->host ) );
149 QDomNode portNode = serverNode.appendChild( doc.createElement("port" ) );
150 portNode.appendChild( doc.createTextNode( QString::number( (*it2)->port ) ) );
152 QDomNode sslNode = serverNode.appendChild( doc.createElement("useSSL") );
153 sslNode.appendChild( doc.createTextNode( (*it2)->ssl ? "true" : "false" ) );
157 // kDebug(14121) << doc.toString(4);
158 QFile xmlFile( KStandardDirs::locateLocal( "appdata", "ircnetworks.xml" ) );
160 if (xmlFile.open(QIODevice::WriteOnly))
162 QTextStream stream( &xmlFile );
163 stream << doc.toString(4);
164 xmlFile.close();
165 return true;
168 kDebug(14121) << "Failed to save the Networks definition file";
169 return false;
172 void Networks::addNetwork( IRCNetwork *network )
174 m_networks.insert( network->name, network );
175 // slotSaveNetworkConfig();
179 #include "ircnetwork.moc"