summaryrefslogtreecommitdiff
path: root/newpostaction.cpp
blob: d2886a614c12555f30dd5ec0dcfaae88db608dfc (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
#include <QtCore/QDebug>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>

#include "global.h"
#include "board.h"
#include "xmlrpcinterface.h"
#include "xmlrpcreply.h"
#include "newpostaction.h"
#include "fetchpostsaction.h"

NewPostAction::NewPostAction(int topicId, const QString &text, Board *board)
	: Action(board), _topicId(topicId), _text(text)
{
}

bool NewPostAction::isSupersetOf(Action *action) const
{
	Q_UNUSED(action);
	return false;
}

void NewPostAction::execute()
{
	int forum_id = _board->getTopicForumId(_topicId);

	_call = _board->service()->asyncCall("reply_post",
	                                     QString::number(forum_id),
	                                     QString::number(_topicId),
	                                     QByteArray(), // Empty subject
	                                     _text.toUtf8()
	                                     );
	_call->setParent(this);
	connect(_call, SIGNAL(finished(XmlRpcPendingCall*)), SLOT(handleFinishedCall()));
}

void NewPostAction::handleFinishedCall()
{
	XmlRpcReply<QVariantMap> result(_call);
	if (result.isValid()) {
		QVariantMap map = result;
		bool post_ok = map["result"].toBool();
		if (post_ok) {
			int state = map["state"].toInt();
			if (state == 1) {
				// Awaiting moderation
				// TODO
			} else {
				// Refresh posts
				_board->enqueueAction(new FetchPostsAction(_topicId,
				                                           FetchPostsAction::FetchUnreadPosts,
				                                           TOPIC_PAGE_SIZE,
				                                           _board));
			}
		} else {
			qWarning() << "Could not submit post:" << map["result_text"].toString();
		}
	} else {
		qWarning() << "Could not submit post";
		// TODO emit error ...
	}
	emit finished(this);
	_call->deleteLater();
}