summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/ui/jabberchooseserver.cpp
blob: 77356c5586da6d8c440a66bd6c31ffee8336783d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

/***************************************************************************
                   jabberchooseserver.cpp  -  Server list for Jabber
                             -------------------
    begin                : Mon Jul 12 2004
    copyright            : (C) 2004 by Till Gerken <till@tantalo.net>

		Kopete (C) 2001-2004 Kopete developers <kopete-devel@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "jabberchooseserver.h"

#include <kdebug.h>
#include <klocale.h>
#include <tdeio/global.h>
#include <tdeio/job.h>
#include <tdeio/jobclasses.h>
#include <tqtable.h>
#include <tqlabel.h>
#include "jabberprotocol.h"
#include "dlgjabberchooseserver.h"
#include "jabberregisteraccount.h"

JabberChooseServer::JabberChooseServer ( JabberRegisterAccount *parent, const char *name )
 : KDialogBase ( parent, name, true, i18n("Choose Jabber Server"),
 				 KDialogBase::Ok | KDialogBase::Cancel )
{

	mParentWidget = parent;
	mSelectedRow = -1;

	mMainWidget = new DlgJabberChooseServer ( this );
	setMainWidget ( mMainWidget );

	mMainWidget->lblStatus->setText ( i18n ( "Retrieving server list...") );

	mMainWidget->listServers->setLeftMargin ( 0 );

	// retrieve server list
	mTransferJob = TDEIO::get ( "http://www.jabber.org/servers.xml" );

	connect ( mTransferJob, TQT_SIGNAL ( result ( TDEIO::Job* ) ), this, TQT_SLOT ( slotTransferResult ( TDEIO::Job* ) ) );
	connect ( mTransferJob, TQT_SIGNAL ( data ( TDEIO::Job*, const TQByteArray& ) ), this, TQT_SLOT ( slotTransferData ( TDEIO::Job*, const TQByteArray& ) ) );

	connect ( mMainWidget->listServers, TQT_SIGNAL ( pressed ( int, int, int, const TQPoint & ) ), this, TQT_SLOT ( slotSetSelection ( int ) ) );
	connect ( mMainWidget->listServers, TQT_SIGNAL ( doubleClicked ( int, int, int, const TQPoint & ) ), this, TQT_SLOT ( slotOk () ) );

	enableButtonOK ( false );

}

JabberChooseServer::~JabberChooseServer()
{
}

void JabberChooseServer::slotOk ()
{

	if ( mSelectedRow != -1 )
	{
		mParentWidget->setServer ( mMainWidget->listServers->text ( mSelectedRow, 0 ) );
	}

	deleteLater ();

}

void JabberChooseServer::slotCancel ()
{

	deleteLater ();

}

void JabberChooseServer::slotSetSelection ( int row )
{

	mSelectedRow = row;
	mMainWidget->listServers->selectRow ( row );
	enableButtonOK ( true );

}

void JabberChooseServer::slotTransferData ( TDEIO::Job */*job*/, const TQByteArray &data )
{

	unsigned oldSize = xmlServerList.size ();

	xmlServerList.resize ( oldSize + data.size () );

	memcpy ( &xmlServerList.data()[oldSize], data.data (), data.size () );

	kdDebug ( JABBER_DEBUG_GLOBAL ) << k_funcinfo << "Server list now " << xmlServerList.size () << endl;

}

void JabberChooseServer::slotTransferResult ( TDEIO::Job *job )
{

	if ( job->error () || mTransferJob->isErrorPage () )
	{
		mMainWidget->lblStatus->setText ( i18n ( "Could not retrieve server list." ) );
		return;
	}
	else
	{
		kdDebug ( JABBER_DEBUG_GLOBAL ) << k_funcinfo << "Received server list ok!" << endl;

		// clear status message
		mMainWidget->lblStatus->setText ( "" );

		// parse XML list
		TQDomDocument doc;

		if ( !doc.setContent ( xmlServerList ) )
		{
			mMainWidget->lblStatus->setText ( i18n ( "Could not parse the server list.") );
			return;
		}

		TQDomElement docElement = doc.documentElement ();

		mMainWidget->listServers->setNumRows ( docElement.childNodes().count () );

		int listIndex = 0;
		for( TQDomNode node = docElement.firstChild (); !node.isNull (); node = node.nextSibling (), listIndex++ )
		{
			TQDomNamedNodeMap attributes = node.attributes ();
			mMainWidget->listServers->setText ( listIndex, 0, attributes.namedItem ( "jid" ).nodeValue () );
			mMainWidget->listServers->setText ( listIndex, 1, attributes.namedItem ( "name" ).nodeValue () );
		}

		mMainWidget->listServers->adjustColumn ( 0 );
		mMainWidget->listServers->adjustColumn ( 1 );
	}

}


#include "jabberchooseserver.moc"