File: battleshipgameplugin.cpp

package info (click to toggle)
psi-plugins 1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,368 kB
  • sloc: cpp: 42,063; xml: 714; ansic: 84; makefile: 61; sh: 12
file content (442 lines) | stat: -rw-r--r-- 14,140 bytes parent folder | download
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 * battleshipgameplugin.cpp - Battleship Game plugin
 * Copyright (C) 2014  Aleksey Andreev
 *
 * 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.
 *
 * You can also redistribute and/or modify this program under the
 * terms of the Psi License, specified in the accompanied COPYING
 * file, as published by the Psi Project; either dated January 1st,
 * 2005, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include "battleshipgameplugin.h"
#include "gamesessions.h"
#include "options.h"

#include <QFileDialog>

#define constVersion            "0.0.1"
#define constShortPluginName    "battleshipgameplugin"
#define constPluginName         "Battleship Game Plugin"

#ifndef HAVE_QT5
Q_EXPORT_PLUGIN2(battleshipgameplugin, BattleshipGamePlugin)
#endif

BattleshipGamePlugin::BattleshipGamePlugin(QObject *parent) :
		QObject(parent),
		enabled_(false),
		psiTab(NULL),
		psiIcon(NULL),
		psiAccInfo(NULL),
		psiContactInfo(NULL),
		psiSender(NULL),
		psiEvent(NULL),
		psiSound(NULL),
		psiPopup(NULL)
{
	Options::psiOptions = NULL;
}

QString BattleshipGamePlugin::name() const
{
	return constPluginName;
}

QString BattleshipGamePlugin::shortName() const
{
	return constShortPluginName;
}

QString BattleshipGamePlugin::version() const
{
	return constVersion;
}

QWidget *BattleshipGamePlugin::options()
{
	QWidget *options = new QWidget;
	ui_.setupUi(options);
	ui_.play_error->setIcon(psiIcon->getIcon("psi/play"));
	ui_.play_finish->setIcon(psiIcon->getIcon("psi/play"));
	ui_.play_move->setIcon(psiIcon->getIcon("psi/play"));;
	ui_.play_start->setIcon(psiIcon->getIcon("psi/play"));
	ui_.select_error->setIcon(psiIcon->getIcon("psi/browse"));
	ui_.select_finish->setIcon(psiIcon->getIcon("psi/browse"));
	ui_.select_move->setIcon(psiIcon->getIcon("psi/browse"));
	ui_.select_start->setIcon(psiIcon->getIcon("psi/browse"));
	restoreOptions();
	connect(ui_.play_error, SIGNAL(clicked()), this, SLOT(testSound()));
	connect(ui_.play_finish, SIGNAL(clicked()), this, SLOT(testSound()));
	connect(ui_.play_move, SIGNAL(clicked()), this, SLOT(testSound()));
	connect(ui_.play_start, SIGNAL(clicked()), this, SLOT(testSound()));
	connect(ui_.select_error, SIGNAL(clicked()), this, SLOT(getSound()));
	connect(ui_.select_finish, SIGNAL(clicked()), this, SLOT(getSound()));
	connect(ui_.select_start, SIGNAL(clicked()), this, SLOT(getSound()));
	connect(ui_.select_move, SIGNAL(clicked()), this, SLOT(getSound()));
	return options;
}

bool BattleshipGamePlugin::enable()
{
	if (enabled_)
		return true;
	// Грузим иконку плагина
	QFile file(":/battleshipgameplugin/battleship");
	if(file.open(QIODevice::ReadOnly)) {
		QByteArray ico = file.readAll();
		psiIcon->addIcon("battleshipgameplugin/battleship", ico);
		file.close();
	}
	// Создаем соединения с менеджером игровых сессий
	GameSessionList *gsl = GameSessionList::instance();
	connect(gsl, SIGNAL(sendStanza(int, QString)), this, SLOT(sendGameStanza(int, QString)), Qt::QueuedConnection);
	connect(gsl, SIGNAL(doPopup(QString)), this, SLOT(doPopup(QString)), Qt::QueuedConnection);
	connect(gsl, SIGNAL(playSound(QString)), this, SLOT(playSound(QString)), Qt::QueuedConnection);
	connect(gsl, SIGNAL(doInviteEvent(int,QString,QString,QObject*,const char*)), this, SLOT(doPsiEvent(int,QString,QString,QObject*,const char*)), Qt::QueuedConnection);

	// Выставляем флаг и уходим
	enabled_ = true;
	return true;
}

bool BattleshipGamePlugin::disable()
{
	enabled_ = false;
	GameSessionList::reset();
	Options::reset();
	return true;
}

void BattleshipGamePlugin::applyOptions()
{
	Options *options = Options::instance();
	options->setOption(constDefSoundSettings, ui_.cb_sound_override->isChecked());
	options->setOption(constSoundStart, ui_.le_start->text());
	options->setOption(constSoundFinish, ui_.le_finish->text());
	options->setOption(constSoundMove, ui_.le_move->text());
	options->setOption(constSoundError, ui_.le_error->text());
	options->setOption(constDndDisable, ui_.cb_disable_dnd->isChecked());
	options->setOption(constConfDisable, ui_.cb_disable_conf->isChecked());
	options->setOption(constSaveWndPosition, ui_.cb_save_pos->isChecked());
	options->setOption(constSaveWndWidthHeight, ui_.cb_save_w_h->isChecked());
}

void BattleshipGamePlugin::restoreOptions()
{
	Options *options = Options::instance();
	ui_.cb_sound_override->setChecked(options->getOption(constDefSoundSettings).toBool());
	ui_.le_start->setText(options->getOption(constSoundStart).toString());
	ui_.le_finish->setText(options->getOption(constSoundFinish).toString());
	ui_.le_move->setText(options->getOption(constSoundMove).toString());
	ui_.le_error->setText(options->getOption(constSoundError).toString());
	ui_.cb_disable_dnd->setChecked(options->getOption(constDndDisable).toBool());
	ui_.cb_disable_conf->setChecked(options->getOption(constConfDisable).toBool());
	ui_.cb_save_pos->setChecked(options->getOption(constSaveWndPosition).toBool());
	ui_.cb_save_w_h->setChecked(options->getOption(constSaveWndWidthHeight).toBool());
}

QPixmap BattleshipGamePlugin::icon() const
{
	return QPixmap(":/battleshipgameplugin/battleship");
}

/**
 * Получение списка ресурсов и вызов формы для отправки приглашения
 */
void BattleshipGamePlugin::inviteDlg(int account, QString full_jid)
{
	QString bareJid = full_jid.section('/', 0, 0);
	//QStringList jid_parse = full_jid.split("/");
	//QString jid = jid_parse.takeFirst();
	if (bareJid.isEmpty())
		return;
	QStringList resList;
	if (psiContactInfo->isPrivate(account, full_jid))
	{
		// This is conference
		QString res = full_jid.section('/', 1);
		if (res.isEmpty())
			return;
		resList.append(res);
	}
	else
	{
		// Получаем список ресурсов оппонента
		resList = psiContactInfo->resources(account, bareJid);
	}
	// Отображение окна отправки приглашения
	GameSessionList::instance()->invite(account, bareJid, resList);
}

// ------------------------------------------ Slots ------------------------------------------

/**
 * Кто-то кликнул по кнопке тулбара в окне чата
 */
void BattleshipGamePlugin::toolButtonPressed()
{
	if (!enabled_)
		return;
	// Получаем наш account id
	QString jid = psiTab->getYourJid();
	int account = -1;
	for (int i = 0; ; i++) {
		QString str1 = psiAccInfo->getJid(i);
		if (str1 == jid) {
			account = i;
			break;
		}
		if (str1 == "-1")
			return;
	}
	// Проверяем статус аккаунта
	if (account == -1 || psiAccInfo->getStatus(account) == "offline")
		return;
	// --
	inviteDlg(account, psiTab->getJid());
}

/**
 * Кто-то выбрал плагин в меню ростера
 */
void BattleshipGamePlugin::menuActivated()
{
	if(!enabled_)
		return;
	int account = sender()->property("account").toInt();
	if (psiAccInfo->getStatus(account) == "offline")
		return;
	QString jid = sender()->property("jid").toString();
	inviteDlg(account, jid);
}

/**
 * Создания события для приглашения
 */
void BattleshipGamePlugin::doPsiEvent(int account, QString from, QString text, QObject *receiver, const char *method)
{
	psiEvent->createNewEvent(account, from, text, receiver, method);
}

/**
 * Отсылка станзы по запросу игры
 */
void BattleshipGamePlugin::sendGameStanza(int account, QString stanza)
{
	if (enabled_ && psiAccInfo->getStatus(account) != "offline")
		psiSender->sendStanza(account, stanza);
}

void BattleshipGamePlugin::testSound() {
	QObject *sender_ = sender();
	if (sender_ == (ui_.play_error)) {
		psiSound->playSound(ui_.le_error->text());
	} else if (sender_ == ui_.play_finish) {
		psiSound->playSound(ui_.le_finish->text());
	} else if (sender_ == ui_.play_move) {
		psiSound->playSound(ui_.le_move->text());
	} else if (sender_ == ui_.play_start) {
		psiSound->playSound(ui_.le_start->text());
	}
}

void BattleshipGamePlugin::getSound() {
	QObject *sender_ = sender();
	QLineEdit *le = 0;
	if (sender_ == ui_.select_error) {
		le = ui_.le_error;
	} else if (sender_ == ui_.select_finish) {
		le = ui_.le_finish;
	} else if (sender_ == ui_.select_move) {
		le = ui_.le_move;
	} else if (sender_ == ui_.select_start) {
		le = ui_.le_start;
	}
	if (!le)
		return;
	QString file_name = QFileDialog::getOpenFileName(0, tr("Choose a sound file"), "", tr("Sound (*.wav)"));
	if (file_name.isEmpty())
		return;
	le->setText(file_name);
}

void BattleshipGamePlugin::doPopup(QString text)
{
	psiPopup->initPopup(text, tr(constPluginName), "battleshipgameplugin/battleship");
}

void BattleshipGamePlugin::playSound(QString sound_id)
{
	Options *options = Options::instance();
	if (options->getOption(constDefSoundSettings).toBool() || Options::psiOptions->getGlobalOption("options.ui.notifications.sounds.enable").toBool())
	{
		if (sound_id == constSoundMove)
			psiSound->playSound(options->getOption(constSoundMove).toString());
		else if (sound_id == constSoundStart)
			psiSound->playSound(options->getOption(constSoundStart).toString());
		else if (sound_id == constSoundFinish)
			psiSound->playSound(options->getOption(constSoundFinish).toString());
		else if (sound_id == constSoundError)
			psiSound->playSound(options->getOption(constSoundError).toString());
	}
}

// --------------------- Plugin info provider ---------------------------

QString BattleshipGamePlugin::pluginInfo()
{
	return tr("Author: ") +  "Liuch\n"
		+ tr("Email: ") + "liuch@mail.ru\n\n"
		+ trUtf8("This plugin allows you to play battleship with your friends.\n"
			 "For sending commands, normal messages are used, so this plugin will always work wherever you are able to log in."
			 "To invite a friend for a game, you can use contact menu item or the button on the toolbar in a chat window.");
}

// --------------------- Option accessor ---------------------------

void BattleshipGamePlugin::setOptionAccessingHost(OptionAccessingHost *host)
{
	Options::psiOptions = host;
}

void BattleshipGamePlugin::optionChanged(const QString &/*option*/)
{
}

// --------------------- Iconfactory accessor ---------------------------
void BattleshipGamePlugin::setIconFactoryAccessingHost(IconFactoryAccessingHost *host)
{
	psiIcon = host;
}

// --------------------- Toolbar icon accessor ---------------------------
QList<QVariantHash> BattleshipGamePlugin::getButtonParam()
{
	QList<QVariantHash> list;
	QVariantHash hash;
	hash["tooltip"] = QVariant(tr("Battleship game"));
	hash["icon"] = QVariant(QString("battleshipgameplugin/battleship"));
	hash["reciver"] = qVariantFromValue(qobject_cast<QObject *>(this));
	hash["slot"] = QVariant(SLOT(toolButtonPressed()));
	list.push_back(hash);
	return list;
}

QAction* BattleshipGamePlugin::getAction(QObject* /*parent*/, int /*account*/, const QString& /*contact*/)
{
	return NULL;
}

// --------------------- Activetab accessor ---------------------------

void BattleshipGamePlugin::setActiveTabAccessingHost(ActiveTabAccessingHost *host)
{
	psiTab = host;
}

// --------------------- Account info accessor ---------------------------

void BattleshipGamePlugin::setAccountInfoAccessingHost(AccountInfoAccessingHost * host)
{
	psiAccInfo = host;
}

// --------------------- Contact info accessor ---------------------------

void BattleshipGamePlugin::setContactInfoAccessingHost(ContactInfoAccessingHost * host)
{
	psiContactInfo = host;
}

// --------------------- Stanza sender ---------------------------

void BattleshipGamePlugin::setStanzaSendingHost(StanzaSendingHost *host)
{
	psiSender = host;
}

// --------------------- Stanza filter ---------------------------

bool BattleshipGamePlugin::incomingStanza(int account, const QDomElement& xml)
{
	if(xml.tagName() == "iq") {
		QString acc_status = "";
		bool confPriv = false;
		if (xml.attribute("type") == "set") {
			acc_status = psiAccInfo->getStatus(account);
			confPriv = psiContactInfo->isPrivate(account, xml.attribute("from"));
		}
		return GameSessionList::instance()->processIncomingIqStanza(account, xml, acc_status, confPriv);
	}
	return false;
}

bool BattleshipGamePlugin::outgoingStanza(int /*account*/, QDomElement& /*xml*/)
{
	return false;
}

// --------------------- Event creator ---------------------------

void BattleshipGamePlugin::setEventCreatingHost(EventCreatingHost *host)
{
	psiEvent = host;
}

// --------------------- Sound accessor ---------------------------

void BattleshipGamePlugin::setSoundAccessingHost(SoundAccessingHost *host)
{
	psiSound = host;
}

// --------------------- Menu accessor ---------------------------

QList<QVariantHash> BattleshipGamePlugin::getAccountMenuParam()
{
	return QList<QVariantHash>();
}

QList<QVariantHash> BattleshipGamePlugin::getContactMenuParam()
{
	QList<QVariantHash> menu_list;
	QVariantHash hash;
	hash["name"] = QVariant(tr("Battleship game!"));
	hash["icon"] = QVariant(QString("battleshipgameplugin/battleship"));
	hash["reciver"] = qVariantFromValue(qobject_cast<QObject *>(this));
	hash["slot"] = QVariant(SLOT(menuActivated()));
	menu_list.push_back(hash);
	return menu_list;
}

QAction* BattleshipGamePlugin::getContactAction(QObject*, int, const QString&)
{
	return NULL;
}

QAction* BattleshipGamePlugin::getAccountAction(QObject*, int)
{
	return NULL;
}

// --------------------- Popup accessor ---------------------------

void BattleshipGamePlugin::setPopupAccessingHost(PopupAccessingHost *host)
{
	psiPopup = host;
}