1  /*
2     Copyright (c) 2008 by Igor Janssen  <alaves17@gmail.com>
3 
4     Kopete    (c) 2008 by the Kopete developers <kopete-devel@kde.org>
5 
6     *************************************************************************
7     *                                                                       *
8     * This program is free software; you can redistribute it and/or modify  *
9     * it under the terms of the GNU General Public License as published by  *
10     * the Free Software Foundation; either version 2 of the License, or     *
11     * (at your option) any later version.                                   *
12     *                                                                       *
13     *************************************************************************
14  */
15 
16 #include "jt_ahcommand.h"
17 
18 #include "xmpp_xmlcommon.h"
19 #include "xmpp_client.h"
20 
21 #include "dlgahcommand.h"
22 
23 #define AHC_NS "http://jabber.org/protocol/commands"
24 
25 using namespace XMPP;
26 
AHCommand(const QString & node,const QString & sessionId,Action action)27 AHCommand::AHCommand(const QString &node, const QString &sessionId, Action action)
28 {
29 	mNode = node;
30 	mHasData = false;
31 	mStatus = NoStatus;
32 	mDefaultAction = NoAction;
33 	mAction = action;
34 	mSessionId = sessionId;
35 }
36 
AHCommand(const QString & node,XMPP::XData data,const QString & sessionId,Action action)37 AHCommand::AHCommand(const QString &node, XMPP::XData data, const QString &sessionId, Action action)
38 {
39 	mNode = node;
40 	mHasData = true;
41 	mData = data;
42 	mStatus = NoStatus;
43 	mDefaultAction = NoAction;
44 	mAction = action;
45 	mSessionId = sessionId;
46 }
47 
AHCommand(const QDomElement & e)48 AHCommand::AHCommand(const QDomElement &e)
49 {
50 	mHasData = false;
51 	mDefaultAction = NoAction;
52 	mStatus = string2status(e.attribute(QStringLiteral("status")));
53 	mNode = e.attribute(QStringLiteral("node"));
54 	mAction = string2action(e.attribute(QStringLiteral("action")));
55 	mSessionId = e.attribute(QStringLiteral("sessionid"));
56 
57 	for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling())
58 	{
59 		QDomElement ee = n.toElement();
60 		if(ee.isNull())
61 			continue;
62 		if(ee.tagName() == QLatin1String("x") && ee.attribute(QStringLiteral("xmlns")) == QLatin1String("jabber:x:data"))
63 		{
64 			// Data form
65 			mData.fromXml(ee);
66 			mHasData = true;
67 		}
68 		else if(ee.tagName() == QLatin1String("actions"))
69 		{
70 			// Actions
71 			QString execute = ee.attribute(QStringLiteral("execute"));
72 			if(!execute.isEmpty())
73 				mDefaultAction = string2action(execute);
74 			for(QDomNode m = ee.firstChild(); !m.isNull(); m = m.nextSibling())
75 			{
76 				Action a = string2action(m.toElement().tagName());
77 				if(a == Prev || a == Next || a == Complete)
78 					mActions += a;
79 			}
80 		}
81 	}
82 }
83 
toXml(QDomDocument * doc,bool submit)84 QDomElement AHCommand::toXml(QDomDocument *doc, bool submit)
85 {
86 	QDomElement command = doc->createElement(QStringLiteral("command"));
87 	command.setAttribute(QStringLiteral("xmlns"), AHC_NS);
88 	if(mStatus != NoStatus)
89 		command.setAttribute(QStringLiteral("status"), status2string(mStatus));
90 	if(mHasData)
91 		command.appendChild(mData.toXml(doc, submit));
92 	if(mAction != Execute)
93 		command.setAttribute(QStringLiteral("action"), action2string(mAction));
94 	command.setAttribute(QStringLiteral("node"), mNode);
95 	if(!mSessionId.isEmpty())
96 		command.setAttribute(QStringLiteral("sessionid"), mSessionId);
97 	return command;
98 }
99 
status2string(Status status)100 QString AHCommand::status2string(Status status)
101 {
102 	switch(status)
103 	{
104 	case Executing:
105 		return QStringLiteral("executing");
106 	case Completed:
107 		return QStringLiteral("completed");
108 	case Canceled:
109 		return QStringLiteral("canceled");
110 	default:
111 		return QLatin1String("");
112 	}
113 }
114 
action2string(Action action)115 QString AHCommand::action2string(Action action)
116 {
117 	switch(action)
118 	{
119 	case Prev:
120 		return QStringLiteral("prev");
121 	case Next:
122 		return QStringLiteral("next");
123 	case Cancel:
124 		return QStringLiteral("cancel");
125 	case Complete:
126 		return QStringLiteral("complete");
127 	default:
128 		return QLatin1String("");
129 	}
130 }
131 
string2action(const QString & s)132 AHCommand::Action AHCommand::string2action(const QString &s)
133 {
134 	if(s == QLatin1String("prev"))
135 		return Prev;
136 	else if(s == QLatin1String("next"))
137 		return Next;
138 	else if(s == QLatin1String("complete"))
139 		return Complete;
140 	else if(s == QLatin1String("cancel"))
141 		return Cancel;
142 	else
143 		return Execute;
144 }
145 
string2status(const QString & s)146 AHCommand::Status AHCommand::string2status(const QString &s)
147 {
148 	if(s == QLatin1String("canceled"))
149 		return Canceled;
150 	else if(s == QLatin1String("completed"))
151 		return Completed;
152 	else if(s == QLatin1String("executing"))
153 		return Executing;
154 	else
155 		return NoStatus;
156 }
157 
158 //----------------------------------------------------------------------------------------------
159 
JT_AHCommand(const XMPP::Jid & jid,const AHCommand & command,XMPP::Task * parent)160 JT_AHCommand::JT_AHCommand(const XMPP::Jid &jid, const AHCommand &command, XMPP::Task *parent):
161 Task(parent), mCommand(command)
162 {
163 	mJid = jid;
164 }
165 
~JT_AHCommand()166 JT_AHCommand::~JT_AHCommand()
167 {
168 }
169 
onGo()170 void JT_AHCommand::onGo()
171 {
172 	QDomElement e = createIQ(doc(), QStringLiteral("set"), mJid.full(), id());
173 	e.appendChild(mCommand.toXml(doc(), true));
174 	send(e);
175 }
176 
take(const QDomElement & e)177 bool JT_AHCommand::take(const QDomElement &e)
178 {
179 	if(!iqVerify(e, mJid, id()))
180 		return false;
181 
182 	// Result of a command
183 	if(e.attribute(QStringLiteral("type")) == QLatin1String("result"))
184 	{
185 		QDomElement i = e.firstChildElement(QStringLiteral("command"));
186 		if(!i.isNull())
187 		{
188 			AHCommand c(i);
189 			if(c.status() == AHCommand::Executing)
190 			{
191 				dlgAHCommand *w = new dlgAHCommand(c, mJid, client());
192 				w->show();
193 			}
194 			else if(c.status() == AHCommand::Completed && i.childNodes().count() > 0)
195 			{
196 				dlgAHCommand *w = new dlgAHCommand(c, mJid, client(), true);
197 				w->show();
198 			}
199 			setSuccess();
200 			return true;
201 		}
202 	}
203 	setError(e);
204 	return false;
205 }
206 
207 //----------------------------------------------------------------------------------------------
208 
JT_AHCGetList(Task * t,const Jid & j)209 JT_AHCGetList::JT_AHCGetList(Task *t, const Jid &j):
210 Task(t)
211 {
212 	mJid = j;
213 }
214 
onGo()215 void JT_AHCGetList::onGo()
216 {
217 	QDomElement e = createIQ(doc(), QStringLiteral("get"), mJid.full(), id());
218 	QDomElement q = doc()->createElement(QStringLiteral("query"));
219 	q.setAttribute(QStringLiteral("xmlns"), QStringLiteral("http://jabber.org/protocol/disco#items"));
220 	q.setAttribute(QStringLiteral("node"), AHC_NS);
221 	e.appendChild(q);
222 	send(e);
223 }
224 
take(const QDomElement & e)225 bool JT_AHCGetList::take(const QDomElement &e)
226 {
227 	if(!iqVerify(e, mJid, id()))
228 		return false;
229 
230 	if(e.attribute(QStringLiteral("type")) == QLatin1String("result"))
231 	{
232 		mCommands.clear();
233 		QDomElement commands = e.firstChildElement(QStringLiteral("query"));
234 		if(!commands.isNull())
235 		{
236 			for(QDomNode n = commands.firstChild(); !n.isNull(); n = n.nextSibling())
237 			{
238 				QDomElement i = n.toElement();
239 				if(i.isNull())
240 					continue;
241 				if(i.tagName() == QLatin1String("item"))
242 				{
243 					JT_AHCGetList::Item ci;
244 					ci.jid = i.attribute(QStringLiteral("jid"));
245 					ci.node = i.attribute(QStringLiteral("node"));
246 					ci.name = i.attribute(QStringLiteral("name"));
247 					mCommands.append(ci);
248 				}
249 			}
250 		}
251 		setSuccess();
252 		return true;
253 	}
254 	else
255 	{
256 		setError(e);
257 		return false;
258 	}
259 }
260 
261