summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/ui/dlgjabberservices.cpp
blob: 29b2b6171275e259902264f16662e394ad3f150d (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

/***************************************************************************
                          dlgjabberservices.cpp  -  Service browsing
                             -------------------
    begin                : Mon Dec 9 2002
    copyright            : (C) 2002-2003 by Till Gerken <till@tantalo.net>
    email                : 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 <kmessagebox.h>
#include <klocale.h>
#include <kdebug.h>

#include <tqpushbutton.h>
#include <tqlineedit.h>
#include <tqtable.h>

#include "jabberaccount.h"
#include "jabberclient.h"
#include "dlgjabberregister.h"
#include "dlgjabberbrowse.h"
#include "dlgjabberservices.h"

#include "dlgjabberservices.moc"

dlgJabberServices::dlgJabberServices (JabberAccount *account, TQWidget *parent, const char *name):dlgServices (parent, name)
{
	m_account = account;

	if(m_account->isConnected())
	{
		// pre-populate the server field
		leServer->setText(m_account->server());
	}

	// disable the left margin
	//tblServices->setLeftMargin (0);

	// no content for now
	//tblServices->setNumRows (0);

	// disable the buttons as long as nothing has been selected
	btnRegister->setDisabled (true);
	btnBrowse->setDisabled (true);

	// allow autostretching
	//tblServices->setColumnStretchable (0, true);
	//tblServices->setColumnStretchable (1, true);

	// disable user selections
	//tblServices->setSelectionMode (TQTable::NoSelection);

	// name table headers
	//tblServices->horizontalHeader ()->setLabel (0, i18n ("Name"));
	//tblServices->horizontalHeader ()->setLabel (1, i18n ("Address"));

	connect (btnQuery, TQT_SIGNAL (clicked ()), this, TQT_SLOT (slotDisco ()));
	//connect (tblServices, TQT_SIGNAL (clicked (int, int, int, const TQPoint &)), this, TQT_SLOT (slotSetSelection (int, int, int, const TQPoint &)));
	connect (lvServices, TQT_SIGNAL (selectionChanged (TQListViewItem *)), this, TQT_SLOT (slotSetSelection (TQListViewItem *)));

	connect (btnRegister, TQT_SIGNAL (clicked ()), this, TQT_SLOT (slotRegister ()));
	connect (btnBrowse, TQT_SIGNAL (clicked ()), this, TQT_SLOT (slotBrowse ()));

}

void dlgJabberServices::slotSetSelection (TQListViewItem *it)
{
	dlgJabberServies_item *item=dynamic_cast<dlgJabberServies_item*>(it);
	if(!item)
	{
		btnRegister->setDisabled (true);
		btnBrowse->setDisabled (true);
	}
	else
	{
		btnRegister->setDisabled (! item->can_register);
		btnBrowse->setDisabled (! item->can_browse);
		current_jid=item->jid;
	}

}

void dlgJabberServices::slotService ()
{

	if(!m_account->isConnected())
	{
		m_account->errorConnectFirst();
		return;
	}
	
	XMPP::JT_GetServices *serviceTask = new XMPP::JT_GetServices (m_account->client()->rootTask ());
	connect (serviceTask, TQT_SIGNAL (finished ()), this, TQT_SLOT (slotServiceFinished ()));

	/* populate server field if it is empty */
	if(leServer->text().isEmpty())
		leServer->setText(m_account->server());

	kdDebug (14130) << "[dlgJabberServices] Trying to fetch a list of services at " << leServer->text () << endl;

	serviceTask->get (leServer->text ());
	serviceTask->go (true);
}



void dlgJabberServices::slotServiceFinished ()
{
	kdDebug (14130) << "[dlgJabberServices] Query task finished" << endl;

	XMPP::JT_GetServices * task = (XMPP::JT_GetServices *) sender ();

	if (!task->success ())
	{
		TQString error = task->statusString();
		KMessageBox::queuedMessageBox (this, KMessageBox::Error, i18n ("Unable to retrieve the list of services.\nReason: %1").tqarg(error), i18n ("Jabber Error"));
		return;
	}

	lvServices->clear();

	for (XMPP::AgentList::const_iterator it = task->agents ().begin (); it != task->agents ().end (); ++it)
	{
		dlgJabberServies_item *item=new dlgJabberServies_item( lvServices , (*it).jid ().userHost () , (*it).name ());
		item->jid=(*it).jid();
		item->can_browse=(*it).features().canSearch();
		item->can_register=(*it).features().canRegister();
	}
}

void dlgJabberServices::slotDisco()
{
	lvServices->clear();

	if(!m_account->isConnected())
	{
		m_account->errorConnectFirst();
		return;
	}
	
	JT_DiscoItems *jt = new JT_DiscoItems(m_account->client()->rootTask());
	connect(jt, TQT_SIGNAL(finished()), this, TQT_SLOT(slotDiscoFinished()));
	
	/* populate server field if it is empty */
	if(leServer->text().isEmpty())
		leServer->setText(m_account->server());
	
	jt->get(leServer->text() , TQString());
	jt->go(true);
}





void dlgJabberServices::slotDiscoFinished( )
{
	XMPP::JT_DiscoItems *jt = (JT_DiscoItems *)sender();

	if ( jt->success() ) 
	{
		TQValueList<XMPP::DiscoItem> list = jt->items();
		
		lvServices->clear();

		for(TQValueList<XMPP::DiscoItem>::ConstIterator it = list.begin(); it != list.end(); ++it) 
		{
			const XMPP::DiscoItem a = *it;
			dlgJabberServies_item *item=new dlgJabberServies_item( lvServices , (*it).jid ().userHost () , (*it).name ());
			item->jid=a.jid();
			item->updateInfo(a.jid() , a.node(), m_account);
		}
	}
	else
	{
		slotService();
	}
}


void dlgJabberServices::slotRegister ()
{

	dlgJabberRegister *registerDialog = new dlgJabberRegister (m_account, current_jid);

	registerDialog->show ();
	registerDialog->raise ();

}

void dlgJabberServices::slotBrowse ()
{

	dlgJabberBrowse *browseDialog = new dlgJabberBrowse (m_account, current_jid);

	browseDialog->show ();
	browseDialog->raise ();

}

dlgJabberServices::~dlgJabberServices ()
{
}

void dlgJabberServies_item::updateInfo( const XMPP::Jid & jid , const TQString & node , JabberAccount *account )
{
	XMPP::JT_DiscoInfo *jt = new XMPP::JT_DiscoInfo(account->client()->rootTask());
	connect(jt, TQT_SIGNAL(finished()),this, TQT_SLOT(slotDiscoFinished()));
	jt->get(jid, node);
	jt->go(true);

}

void dlgJabberServies_item::slotDiscoFinished( )
{
	JT_DiscoInfo *jt = (JT_DiscoInfo *)sender();

	if ( jt->success() ) 
	{
		can_browse = jt->item().features().canSearch();
		can_register = jt->item().features().canRegister();
	}
	else
	{
		//TODO: error message  (it's a simple message box to show)
	}
}