-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsofgameplugin.cpp
More file actions
492 lines (454 loc) · 16.5 KB
/
sofgameplugin.cpp
File metadata and controls
492 lines (454 loc) · 16.5 KB
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* sof_game.cpp - Sof Game Psi plugin
* Copyright (C) 2010 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <QtGui>
//#include <QtCore>
#include "sofgameplugin.h"
#include "pluginhosts.h"
#include "sender.h"
#include "plugin_core.h"
Q_EXPORT_PLUGIN(SofGamePlugin);
SofGamePlugin::SofGamePlugin() :
enabled(false),
currentAccount(-1),
currAccActive(false),
shortCut("Ctrl+Alt+g"),
gameJidsWid(NULL),
chatJidsWid(NULL),
shortCutWid(NULL),
accountsWid(NULL),
psiShortcuts(NULL),
accInfoHost(NULL)
{
PluginHosts::psiOptions = 0;
sender_ = 0;
PluginHosts::myPopupHost = 0;
chatJids << tr("sofch@jabber.ru");
}
SofGamePlugin::~SofGamePlugin() {
disable();
}
QString SofGamePlugin::name() const
{
return "SofGame Plugin";
}
QString SofGamePlugin::shortName() const
{
return "sofgame";
}
QString SofGamePlugin::version() const
{
return cVer;
}
bool SofGamePlugin::enable()
{
if (PluginHosts::psiOptions == NULL || sender_ == NULL)
return false;
// Инициируем модуль приема/передачи плагина
Sender *senderObj = Sender::instance();
// Инициируем загрузку ядра плагина
PluginCore::instance();
//--
enabled = true;
// Получаем данные аккаунта
QVariant vGameAccount(accountJid);
vGameAccount = PluginHosts::psiOptions->getPluginOption(constGameAccount);
int newAccountId = -1;
QString newAccJid;
if (!vGameAccount.isNull()) {
newAccJid = vGameAccount.toString();
newAccountId = getAccountByJid(newAccJid);
}
currentAccount = newAccountId;
accountJid = newAccJid;
// Передаем модулю данные об выбранном аккаунте
senderObj->changeAccount(currentAccount, accountJid);
// Получаем список игровых джидов из настроек
QStringList game_jids = PluginHosts::psiOptions->getPluginOption(constGameJids).toStringList();
// Отправляем модулю
if (game_jids.isEmpty()) {
senderObj->insertGameJid("sof@jabbergames.ru", -1);
senderObj->insertGameJid("sofgm@jabber.ru", -1);
} else {
int cnt = game_jids.size();
for (int i = 0; i < cnt; i++) {
senderObj->insertGameJid(game_jids.at(i), -1);
}
}
// Данные о джиде чата
chatJids = PluginHosts::psiOptions->getPluginOption(constChatJids).toStringList();
// Горячая клавиша для вызова окна планина
shortCut = PluginHosts::psiOptions->getPluginOption(constShortCut).toString();
if (psiShortcuts)
psiShortcuts->connectShortcut(QKeySequence(shortCut), this, SLOT(doShortCut()));
// --
if (currentAccount != -1) {
// Проверяем статус аккаунта
QString jid_str = accInfoHost->getStatus(currentAccount);
if (jid_str != "offline") {
// Отсылаем запросы об активности зеркал
QStringList jids = senderObj->getGameJids();
while (!jids.isEmpty()) {
QString jid = jids.takeFirst();
sendLastActiveQuery(accountJid, jid);
}
}
}
return true;
}
bool SofGamePlugin::disable()
{
if (psiShortcuts) {
psiShortcuts->disconnectShortcut(QKeySequence(shortCut), this, SLOT(doShortCut()));
//psiShortcuts = 0;
}
PluginCore::reset();
Sender::reset();
enabled = false;
return true;
}
QWidget* SofGamePlugin::options()
{
if (!enabled) {
return 0;
}
//---
QWidget *optionsWid = new QWidget();
QVBoxLayout *layout = new QVBoxLayout(optionsWid);
// Текст с предупреждением
layout->addWidget(new QLabel(QString::fromUtf8("Внимание! Перед началом игры, необходимо добавить указанные ниже JID-ы в ростер."), optionsWid));
// Список Jid-ов игры
gameJidsWid = new QTextEdit(optionsWid);
QVBoxLayout *gameJidsVertLayout = new QVBoxLayout();
gameJidsVertLayout->addWidget(new QLabel(QString::fromUtf8("JID-ы игры:"), optionsWid));
gameJidsVertLayout->addWidget(gameJidsWid);
// Список Jid-ов чата
chatJidsWid = new QTextEdit(optionsWid);
QVBoxLayout *chatJidsVertLayout = new QVBoxLayout();
chatJidsVertLayout->addWidget(new QLabel(QString::fromUtf8("JID-ы чата:"), optionsWid));
chatJidsVertLayout->addWidget(chatJidsWid);
// --
QHBoxLayout *jidsHorLayout = new QHBoxLayout();
jidsHorLayout->addLayout(gameJidsVertLayout);
jidsHorLayout->addLayout(chatJidsVertLayout);
layout->addLayout(jidsHorLayout);
// Горячие клавиши
QHBoxLayout *hotkeyHorLayout = new QHBoxLayout();
hotkeyHorLayout->addWidget(new QLabel(QString::fromUtf8("Комбинация клавиш:"), optionsWid),0,Qt::AlignLeft);
shortCutWid = new QLineEdit(optionsWid);
shortCutWid->setFixedWidth(100);
shortCutWid->setDisabled(true);
hotkeyHorLayout->addWidget(shortCutWid,200,Qt::AlignLeft);
QPushButton *shortCutButton = new QPushButton(optionsWid);
shortCutButton->setText(QString::fromUtf8("Изменить"));
connect(shortCutButton, SIGNAL(released()), SLOT(selectShortCut()));
hotkeyHorLayout->addWidget(shortCutButton,400,Qt::AlignLeft);
//--
layout->addLayout(hotkeyHorLayout);
// Получаем список аккаунтов текущего профиля
QList< QPair<QString, QString> > accs = getAccounts();
// Формируем элементы для списка аккаунтов
QHBoxLayout *accountHorLayout = new QHBoxLayout;
accountHorLayout->addWidget(new QLabel(QString::fromUtf8("Игровой аккаунт:")), 0, Qt::AlignLeft);
accountsWid = new QComboBox(optionsWid);
accountsWid->clear();
for (int i = 0, cnt = accs.size(); i < cnt; i++) {
QString name = accs.at(i).first;
QString jid = accs.at(i).second;
accountsWid->addItem(QString("%1 (%2)").arg(name).arg(jid), jid);
}
//--
accountHorLayout->addWidget(accountsWid, 400, Qt::AlignLeft);
// Ссылка на сайт игры
QLabel *gameSiteLink = new QLabel(tr("<a href=\"http://www.jabbergames.ru/\">SoF game site (Online)</a>"), optionsWid);
gameSiteLink->setOpenExternalLinks(true);
layout->addLayout(accountHorLayout);
layout->addWidget(gameSiteLink, 400, Qt::AlignLeft);
//--
restoreOptions();
return optionsWid;
}
void SofGamePlugin::applyOptions() {
if (!gameJidsWid || !chatJidsWid || !shortCutWid || !accountsWid) {
return;
}
Sender *senderObj = Sender::instance();
// *** Аккаунт игры ***
int index = accountsWid->currentIndex();
QString newAccJid;
if (index != -1) {
newAccJid = accountsWid->itemData(index).toString();
}
// Сохраняем новый джид
PluginHosts::psiOptions->setPluginOption(constGameAccount, newAccJid);
// Если он изменился, отсылаем новые данные акка sender-у и обнавляем у себя
int newAccountId = getAccountByJid(newAccJid);
if (currentAccount != newAccountId || newAccJid != accountJid) {
currentAccount = newAccountId;
accountJid = newAccJid;
senderObj->changeAccount(currentAccount, accountJid);
}
// *** Jid-ы игры ***
QStringList game_jids = gameJidsWid->toPlainText().split(QRegExp("\\s+"), QString::SkipEmptyParts);
// Сохраняем в настройках
PluginHosts::psiOptions->setPluginOption(constGameJids, game_jids);
// Получаем старый набор джидов игры
QStringList old_game_jids = senderObj->getGameJids();
// Производим изменения списка зеркал в модуле отправки
bool fActive = false;
if (currentAccount != -1 && accInfoHost->getStatus(currentAccount) != "offline")
fActive = true;
while (!old_game_jids.isEmpty()) { // Проходимся по старому списку зеркал игры
QString jid = old_game_jids.takeFirst();
if (!game_jids.removeOne(jid)) {
// В новом списке нет такого зеркала, удаляем в sender-е
senderObj->removeGameJid(jid);
}
}
while (!game_jids.isEmpty()) { // Добавляем ранее отсутствующие зеркала в sender
QString jid = game_jids.takeFirst();
senderObj->insertGameJid(jid, -1);
if (fActive)
sendLastActiveQuery(accountJid, jid);
}
// *** Jid-ы чата ***
chatJids = chatJidsWid->toPlainText().split(QRegExp("\\s+"), QString::SkipEmptyParts);
PluginHosts::psiOptions->setPluginOption(constChatJids, chatJids);
// --
psiShortcuts->disconnectShortcut(QKeySequence(shortCut), this, SLOT(doShortCut()));
shortCut = shortCutWid->text();
PluginHosts::psiOptions->setPluginOption(constShortCut, shortCut);
psiShortcuts->connectShortcut(QKeySequence(shortCut), this, SLOT(doShortCut()));
}
void SofGamePlugin::restoreOptions() {
if (gameJidsWid == 0 || chatJidsWid == 0 || shortCutWid == 0 || accountsWid == 0) {
return;
}
gameJidsWid->setText(Sender::instance()->getGameJids().join("\n"));
// --
chatJidsWid->setText(chatJids.join("\n"));
// --
shortCutWid->setText(shortCut);
// Выбранный аккаунт
int index = accountsWid->findData(accountJid);
accountsWid->setCurrentIndex(index);
}
QString SofGamePlugin::pluginInfo() {
return QString::fromUtf8("Автор: liuch\nEmail: liuch@mail.ru\n\n"
"Этот плагин позволяет играть в jabber игру \"Камень судьбы\" используя графический интерфейс.\n"
"Официальный сайт игры находится по адресу: http://www.jabbergames.ru/\n"
"Для начала игры добавьте следующие джиды себе в ростер: sofgm@jabber.ru (джид игры), sof@jabbergames.ru (зеркало игры), sofch@jabber.ru(джид чата игры). "
"Вызовите окно плагина горячей клавишей и отправьте 0 в игру через плагин. Приятной игры!");
}
void SofGamePlugin::selectShortCut()
{
if (psiShortcuts) {
psiShortcuts->requestNewShortcut(this, SLOT(onNewShortcutKey(QKeySequence)));
}
}
void SofGamePlugin::onNewShortcutKey(QKeySequence ks)
{
if (shortCutWid) {
shortCutWid->setText(ks.toString(QKeySequence::NativeText));
}
}
QList< QPair<QString, QString> > SofGamePlugin::getAccounts() const
{
QList< QPair<QString, QString> > res;
for (int i = 0; ; i++) {
QString jid = accInfoHost->getJid(i);
if (jid == "-1")
break;
if (!jid.isEmpty()) {
QPair<QString, QString> p;
QString name = accInfoHost->getName(i);
if (name.isEmpty()) {
name = QString("acc %1").arg(i + 1);
}
p.first = name;
p.second = jid;
res.append(p);
}
}
return res;
}
int SofGamePlugin::getAccountByJid(const QString &jid) const
{
if (!jid.isEmpty()) {
// Перебираем аккаунты и находим свой
for (int i = 0; ; i++) {
// Проверяем аккаунт
QString jid_str = accInfoHost->getJid(i);
if (jid_str == "-1")
break;
if (jid == jid_str)
return i;
}
}
return -1;
}
void SofGamePlugin::sendLastActiveQuery(const QString &from_jid, const QString &to_jid)
{
if (currentAccount != -1) {
QString query = QString("<iq from=\"%1\" id=\"sofgame_plugin\" to=\"%2\" type=\"get\"><query xmlns=\"jabber:iq:last\"/></iq>")
.arg(sender_->escape(from_jid)).arg(sender_->escape(to_jid));
sender_->sendStanza(currentAccount, query);
}
}
//-- OptionAccessor -------------------------------------------------
void SofGamePlugin::setOptionAccessingHost(OptionAccessingHost* host)
{
PluginHosts::psiOptions = host;
}
void SofGamePlugin::optionChanged(const QString& /*option*/)
{
}
//-- StanzaSender ---------------------------------------------------
void SofGamePlugin::setStanzaSendingHost(StanzaSendingHost *host)
{
sender_ = host;
}
//-- ShortcutAccessor -----------------------------------------------
void SofGamePlugin::setShortcutAccessingHost(ShortcutAccessingHost* host)
{
psiShortcuts = host;
}
void SofGamePlugin::setShortcuts()
{
if (enabled && psiShortcuts) {
psiShortcuts->connectShortcut(QKeySequence(shortCut), this, SLOT(doShortCut()));
}
}
// ----------------- ApplicationInfoAccessor -------------------
void SofGamePlugin::setApplicationInfoAccessingHost(ApplicationInfoAccessingHost* host) {
PluginHosts::appInfoHost = host;
}
// ----------------- AccountInfoAccessor -------------------
void SofGamePlugin::setAccountInfoAccessingHost(AccountInfoAccessingHost* host) {
accInfoHost = host;
}
// ----------------- PopupAccessor -------------------
void SofGamePlugin::setPopupAccessingHost(PopupAccessingHost* host) {
//qDebug() << "SofGamePlugin::setPopupAccessingHost";
PluginHosts::myPopupHost = host;
}
// ----------------- IconFactoryAccessor --------------------------
void SofGamePlugin::setIconFactoryAccessingHost(IconFactoryAccessingHost *host)
{
PluginHosts::psiIcon = host;
}
// ----------------------- StanzaFilter ------------------------------
bool SofGamePlugin::incomingStanza(int account, const QDomElement &stanza)
{
if (!enabled)
return false;
Sender *senderObj = Sender::instance();
if (currentAccount == -1) {
// Проверяем JID, от которого пришло сообщение
if (accountJid != accInfoHost->getJid(account))
return false;
// Сохраняем id аккаунта
currentAccount = account;
senderObj->changeAccount(account, accountJid);
} else if (currentAccount != account) {
return false;
}
// Проверяем JID от которого пришло сообщение
QString jid = stanza.attribute("from");
jid = jid.left(jid.indexOf("/")).toLower();
int i = senderObj->getGameJidIndex(jid);
if (i == -1) { // JID не найден
return false;
}
// Просматриваем сообщение
QString sTagName = stanza.tagName();
if (sTagName == "message") {
// Интересуют только сообщения в чате
if (stanza.attribute("type") != "chat") {
return false;
}
// Получаем текст сообщения
QDomElement eBody = stanza.firstChildElement("body");
if (eBody.isNull()) {
return false;
}
QString message = eBody.text();
// Передаем jid отправителя и текст обработчику
bool res = senderObj->doGameAsk(jid, message);
return res;
} else if (sTagName == "presence") {
if (stanza.attribute("type") == "unavailable") {
PluginCore::instance()->setGameJidStatus(i, 0);
} else {
currAccActive = true;
PluginCore::instance()->setGameJidStatus(i, 1);
}
} else if (sTagName == "iq") {
if (stanza.attribute("type") == "result" && stanza.attribute("id") == "sofgame_plugin") {
// Это ответ на запрос который посылался при загрузке плагина
QDomElement queryElement = stanza.firstChildElement("query");
if (!queryElement.isNull() && queryElement.attribute("xmlns") == "jabber:iq:last") {
bool fOk = false;
int secs = queryElement.attribute("seconds").toInt(&fOk);
if (fOk) {
if (secs == 0) {
currAccActive = true;
PluginCore::instance()->setGameJidStatus(i, 1);
} else {
PluginCore::instance()->setGameJidStatus(i, 0);
}
}
}
}
}
return false;
}
bool SofGamePlugin::outgoingStanza(int account, QDomElement& stanza)
{
if (enabled && account == currentAccount) {
if (stanza.tagName() == "presence" && stanza.attribute("to").isEmpty()) {
if (stanza.attribute("type") == "unavailable") {
if (currAccActive) {
// Произошло отключение аккаунта пользователем
currAccActive = false;
PluginCore::instance()->setAccountStatus(0);
}
} else {
if (!currAccActive) {
// Аккаунт стал активен
currAccActive = true;
PluginCore::instance()->setAccountStatus(1);
}
}
}
}
return false;
}
//-- private slots --------------------------------------------------
void SofGamePlugin::doShortCut()
{
if (enabled) {
PluginCore::instance()->doShortCut();
}
}