summaryrefslogtreecommitdiff
path: root/fetchconfigaction.cpp
blob: 787810cf791f78644f5e37763df2386dd5913e3f (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
#include <QtCore/QDateTime>
#include <QtCore/QDebug>
#include <QtSql/QSqlQuery>

#include "board.h"
#include "xmlrpcinterface.h"
#include "xmlrpcreply.h"
#include "fetchconfigaction.h"

FetchConfigAction::FetchConfigAction(Board *board) :
    Action(board)
{
}

bool FetchConfigAction::isSupersetOf(Action *action) const
{
	// If 'action' is also a fetch board config action then yes, this supersets 'action'.
	return qobject_cast<FetchConfigAction*>(action) != 0;
}

void FetchConfigAction::execute()
{
	_call = _board->service()->asyncCall("get_config");
	_call->setParent(this);
	connect(_call, SIGNAL(finished(XmlRpcPendingCall*)), SLOT(handleFinishedCall()));
}

void FetchConfigAction::handleFinishedCall()
{
	XmlRpcReply<QVariantMap> result(_call);
	if (result.isValid()) {
		QVariantMap map = result;
		QSqlDatabase db = _board->database();
		db.transaction();
		QSqlQuery query(db);

		// Let's add some of our config settings
		map["last_config_fetch"] = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
		map["tapasboard_db_version"] = Board::CURRENT_DB_VERSION;

		query.prepare("INSERT OR REPLACE INTO config (key, value) VALUES (:key, :value)");
		for (QVariantMap::iterator i = map.begin(); i != map.end(); i++) {
			query.bindValue(":key", i.key());
			query.bindValue(":value", i.value().toString());
			if (!query.exec()) {
				qWarning() << "Failed to set config key:" << i.key();
			}
		}
		db.commit();
		_board->notifyConfigChanged();
	} else {
		qWarning() << "Could not fetch board configuration";
		// TODO emit error ...
	}
	emit finished(this);
	_call->deleteLater();
}