|  | 
|  | 1 | +/**************************** agent.cpp ********************************  | 
|  | 2 | +
 | 
|  | 3 | +Code for the user agent registered on DBus.  When the connman daemon | 
|  | 4 | +needs to communicate with the user it does so through this agent. | 
|  | 5 | +
 | 
|  | 6 | +Copyright (C) 2013-2014 | 
|  | 7 | +by: Andrew J. Bibb | 
|  | 8 | +License: MIT  | 
|  | 9 | +
 | 
|  | 10 | +Permission is hereby granted, free of charge, to any person obtaining a copy  | 
|  | 11 | +of this software and associated documentation files (the "Software"),to deal  | 
|  | 12 | +in the Software without restriction, including without limitation the rights  | 
|  | 13 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  | 
|  | 14 | +copies of the Software, and to permit persons to whom the Software is  | 
|  | 15 | +furnished to do so, subject to the following conditions:  | 
|  | 16 | +
 | 
|  | 17 | +The above copyright notice and this permission notice shall be included  | 
|  | 18 | +in all copies or substantial portions of the Software. | 
|  | 19 | +
 | 
|  | 20 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | 
|  | 21 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
|  | 22 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  | 
|  | 23 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  | 
|  | 24 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING  | 
|  | 25 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER  | 
|  | 26 | +DEALINGS IN THE SOFTWARE. | 
|  | 27 | +***********************************************************************/   | 
|  | 28 | + | 
|  | 29 | +# include <QtCore/QDebug> | 
|  | 30 | +# include <QtDBus/QDBusConnection> | 
|  | 31 | +# include <QMessageBox> | 
|  | 32 | +# include <QDialog> | 
|  | 33 | +# include <QFile> | 
|  | 34 | +# include <QTextStream> | 
|  | 35 | + | 
|  | 36 | +#	include "./code/agent/agent.h" | 
|  | 37 | +# include "./code/resource.h"	 | 
|  | 38 | + | 
|  | 39 | +//	header files generated by qmake from the xml file created by qdbuscpp2xml | 
|  | 40 | +# include "agent_adaptor.h" | 
|  | 41 | +# include "agent_interface.h" | 
|  | 42 | + | 
|  | 43 | +//	defines | 
|  | 44 | +# define ERROR_RETRY "net.connman.Agent.Error.Retry" | 
|  | 45 | +# define ERROR_CANCELED "net.connman.Agent.Error.Canceled" | 
|  | 46 | +# define ERROR_LAUNCHBROWSER "net.connman.Agent.Error.LaunchBrowser" | 
|  | 47 | + | 
|  | 48 | +//	constructor | 
|  | 49 | +ConnmanAgent::ConnmanAgent(QObject* parent) | 
|  | 50 | +    : QObject(parent) | 
|  | 51 | +{	 | 
|  | 52 | +	// members | 
|  | 53 | +	uiDialog = new AgentDialog(qobject_cast<QWidget *> (this) ); | 
|  | 54 | +	input_map.clear(); | 
|  | 55 | +	b_loginputrequest = false; | 
|  | 56 | +	 | 
|  | 57 | +	//	Create Adaptor and register this Agent on the system bus.   | 
|  | 58 | +	new AgentAdaptor(this); | 
|  | 59 | +	QDBusConnection::systemBus().registerObject("/org/monkeybusiness/Agent", this); | 
|  | 60 | + | 
|  | 61 | +	//	Create an Interface for the Agent | 
|  | 62 | +	net::connman::Agent* iface; | 
|  | 63 | +	iface = new net::connman::Agent(QString("net.connman.Agent"), QString("/org/monkeybusiness/Agent"), QDBusConnection::systemBus(), this); | 
|  | 64 | +} | 
|  | 65 | + | 
|  | 66 | +/////////////////////////////////////// PUBLIC Q_SLOTS//////////////////////////////// | 
|  | 67 | +// | 
|  | 68 | +// Called when the service daemon unregisters the agent.  QT deals with cleanup | 
|  | 69 | +// tasks so don't need much here | 
|  | 70 | +void ConnmanAgent::Release() | 
|  | 71 | +{ | 
|  | 72 | +	return; | 
|  | 73 | +} | 
|  | 74 | + | 
|  | 75 | +// Called when an error has to be reported to the user.  Show the | 
|  | 76 | +// error in a QMessageBox | 
|  | 77 | +void ConnmanAgent::ReportError(QDBusObjectPath path, QString s_error) | 
|  | 78 | +{ | 
|  | 79 | +	if ( QMessageBox::warning(qobject_cast<QWidget *> (parent()), tr("Connman Error"), | 
|  | 80 | +		tr("Connman returned the following error:<b><center>%1</b><br>Would you like to retry?").arg(s_error), | 
|  | 81 | +		QMessageBox::Yes | QMessageBox::No, | 
|  | 82 | +		QMessageBox::No | 
|  | 83 | +		 ) == QMessageBox::Yes) this->sendErrorReply(ERROR_RETRY, "Going to retry the request");	 | 
|  | 84 | +	 | 
|  | 85 | +	else	return;	 | 
|  | 86 | +} | 
|  | 87 | + | 
|  | 88 | +//ff | 
|  | 89 | +// Called when it is required to ask the user to open qa website to proceed | 
|  | 90 | +// with login handling | 
|  | 91 | +void ConnmanAgent::RequestBrowser(QDBusObjectPath path, QString url) | 
|  | 92 | +{ | 
|  | 93 | +	// Send the url to the dialog to have the user the necessary information, return if canceled.	 | 
|  | 94 | +	if (this->uiDialog->showPage1(url) == QDialog::Rejected) this->sendErrorReply(ERROR_CANCELED,"User cancelled the dialog"); | 
|  | 95 | +	 | 
|  | 96 | +	return;	 | 
|  | 97 | +} | 
|  | 98 | + | 
|  | 99 | +// | 
|  | 100 | +// Called when trying to connect to a service and some extra input is required from the user | 
|  | 101 | +// A dialog is displayed with the required fields enabled (non-required fields are disabled). | 
|  | 102 | +QVariantMap ConnmanAgent::RequestInput(QDBusObjectPath path, QMap<QString,QVariant> dict) | 
|  | 103 | +{ | 
|  | 104 | +	// Take the dict returned by DBus and extract the information we are interested in and place in input_map. | 
|  | 105 | +	this->createInputMap(dict); | 
|  | 106 | +	 | 
|  | 107 | +	// Send our input_map to the dialog to have the user supply the necessary information | 
|  | 108 | +	// needed to continue.  Return if canceled.	 | 
|  | 109 | +	if (this->uiDialog->showPage0(input_map) == QDialog::Rejected) this->sendErrorReply(ERROR_CANCELED,"User cancelled the dialog");	 | 
|  | 110 | +	 | 
|  | 111 | +	// Create a return dict and send it back to connman on DBus	  | 
|  | 112 | +	QMap<QString,QVariant> rtn; | 
|  | 113 | +	rtn.clear(); | 
|  | 114 | +	uiDialog->createDict(rtn); | 
|  | 115 | +	return rtn; | 
|  | 116 | +} | 
|  | 117 | + | 
|  | 118 | +// | 
|  | 119 | +// Called when the agent request failed before a reply was returned. Show | 
|  | 120 | +// a QMessageBox | 
|  | 121 | +void ConnmanAgent::Cancel() | 
|  | 122 | +{ | 
|  | 123 | +	QMessageBox::information(qobject_cast<QWidget *> (parent()), tr("Agent Request Failed"), | 
|  | 124 | +		tr("The agent request failed before a reply was returned.") );		 | 
|  | 125 | +		 | 
|  | 126 | +	return;	 | 
|  | 127 | +} | 
|  | 128 | + | 
|  | 129 | +/////////////////////////////////////// PUBLIC FUNCTIONS //////////////////////////////// | 
|  | 130 | +// | 
|  | 131 | +//	Function to put all of input fields received via DBus:RequestInput into a  | 
|  | 132 | +//	QMap<QString,QString> where key is the input field received and value is | 
|  | 133 | +//	generally blank but can be used for informational text. | 
|  | 134 | +// | 
|  | 135 | +//	If we asked to log the input request create the log file in /tmp/cmst/input_request.log | 
|  | 136 | + | 
|  | 137 | +void ConnmanAgent::createInputMap(const QMap<QString,QVariant>& r_map) | 
|  | 138 | +{ | 
|  | 139 | +	// Initialize our data map | 
|  | 140 | +	input_map.clear(); | 
|  | 141 | +	 | 
|  | 142 | +	// QFile object for logging | 
|  | 143 | +	QTextStream log; | 
|  | 144 | +	QFile logfile("/tmp/cmst/input_request.log"); | 
|  | 145 | +	if (b_loginputrequest) { | 
|  | 146 | +		QDir d; | 
|  | 147 | +		d.mkpath("/tmp/cmst"); | 
|  | 148 | +		if (logfile.exists()) logfile.remove(); | 
|  | 149 | +		if (!logfile.open(QIODevice::WriteOnly | QIODevice::Text)) b_loginputrequest = false; | 
|  | 150 | +		else log.setDevice(&logfile); | 
|  | 151 | +	} | 
|  | 152 | +  | 
|  | 153 | +   | 
|  | 154 | +	// Run through the r_map getting the keys and the few values we are interested in. | 
|  | 155 | +	QMap<QString, QVariant>::const_iterator i = r_map.constBegin(); | 
|  | 156 | +	while (i != r_map.constEnd()) { | 
|  | 157 | +		     | 
|  | 158 | +    // Lets see what the values contain, but first make sure we can get to them. | 
|  | 159 | +    if (b_loginputrequest) log << "\nMap Key: " << i.key() << "\n"; | 
|  | 160 | +      | 
|  | 161 | +    if (! i.value().canConvert<QDBusArgument>() ) return; | 
|  | 162 | +    const QDBusArgument qdba =  i.value().value<QDBusArgument>(); | 
|  | 163 | +		if ( ! qdba.currentType() == QDBusArgument::MapType ) { | 
|  | 164 | +			if (b_loginputrequest) log << "Error - QDBusArgument as the value is not a MapType\n";  | 
|  | 165 | +			return; | 
|  | 166 | +		}	 | 
|  | 167 | +     | 
|  | 168 | +    // The r_map.value() is a QDBusArgument::MapType so extract this map into a new QMap called m. | 
|  | 169 | +    qdba.beginMap(); | 
|  | 170 | +    QMap<QString,QString> m; | 
|  | 171 | +		m.clear(); | 
|  | 172 | +		if (b_loginputrequest) log << "Extracting the DBusArgument Map...\n";  | 
|  | 173 | +		while ( ! qdba.atEnd() ) { | 
|  | 174 | +			QString k; | 
|  | 175 | +			QVariant v; | 
|  | 176 | +			qdba.beginMapEntry(); | 
|  | 177 | +			qdba >> k >> v; | 
|  | 178 | +			qdba.endMapEntry(); | 
|  | 179 | +			m.insert(k, v.toString()); | 
|  | 180 | +			if (b_loginputrequest) log << "{ " << k << " , " << v.toString() << "}\n";  | 
|  | 181 | +		}	// while | 
|  | 182 | +		qdba.endMap(); | 
|  | 183 | +		 | 
|  | 184 | +		// Browse through QMap m and get things we need to look at | 
|  | 185 | +		// Types we don' really care about.  We ignore "optional" and "alternate" requirements | 
|  | 186 | +    // and only extract the "mandatory" and "informational" requirements with values | 
|  | 187 | +		if (m.contains("Requirement") ) { | 
|  | 188 | +			QString val = QString(); | 
|  | 189 | +			if ( m.value("Requirement").contains("mandatory", Qt::CaseInsensitive) || m.value("Requirement").contains("informational", Qt::CaseInsensitive) ) { | 
|  | 190 | +				if (m.contains("Value") ) val = m.value("Value");  | 
|  | 191 | +			}	// if mandatory or informational | 
|  | 192 | +			//	create our input_map entry | 
|  | 193 | +			input_map[i.key()] = val; | 
|  | 194 | +		}	// if requirement | 
|  | 195 | + | 
|  | 196 | +    ++i; | 
|  | 197 | +	}	// while | 
|  | 198 | +	 | 
|  | 199 | +	logfile.close(); | 
|  | 200 | +	return;	 | 
|  | 201 | +} | 
|  | 202 | + | 
0 commit comments