~ubuntu-branches/debian/stretch/psi-plus/stretch

« back to all changes in this revision

Viewing changes to src/plugins/generic/chessplugin/chessplugin.cpp

  • Committer: Package Import Robot
  • Author(s): Boris Pek
  • Date: 2013-10-23 02:42:20 UTC
  • mfrom: (1.4.7)
  • Revision ID: package-import@ubuntu.com-20131023024220-bk2hyoenqkwfhpgw
Tags: 0.16.242-1
* New upstream release:
  fixed the problem of initialization of private conversation when both
  sides use libotr 4.0.x. (Closes: #724880)
* Update debian/watch: sources were moved.
* Delete psi-plus-content-downloader package and update all related files.
  This plugin is in psi-plus-plugins package now.
* Update debian/control:
  - remove all currently unneeded Replaces and Breaks fields
  - add build dependency on libidn11-dev
* Update debian/rules: simplify get-orig-source section.
* Update debian/copyright:
  - update Source field due to changes in sources location
  - remove copyright holders whose code was deleted from source tree
    (bundled libidn library was removed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
#include "invitedialog.h"
55
55
#include "request.h"
56
56
 
57
 
#define cVer "0.2.6"
 
57
#define cVer "0.2.7"
58
58
 
59
59
#define soundStartConst "soundstart"
60
60
#define soundFinishConst "soundfinish"
65
65
 
66
66
 
67
67
class ChessPlugin: public QObject, public PsiPlugin, public OptionAccessor, public ActiveTabAccessor, public MenuAccessor, public ApplicationInfoAccessor,
68
 
public ToolbarIconAccessor, public IconFactoryAccessor, public StanzaSender, public AccountInfoAccessor, public StanzaFilter,
69
 
public PluginInfoProvider, public EventCreator, public ContactInfoAccessor, public PopupAccessor, public SoundAccessor
 
68
                                   public ToolbarIconAccessor, public IconFactoryAccessor, public StanzaSender, public AccountInfoAccessor, public StanzaFilter,
 
69
                                   public PluginInfoProvider, public EventCreator, public ContactInfoAccessor, public PopupAccessor, public SoundAccessor
70
70
{
71
71
        Q_OBJECT
 
72
#ifdef HAVE_QT5
 
73
        Q_PLUGIN_METADATA(IID "com.psi-plus.ChessPlugin")
 
74
#endif
72
75
        Q_INTERFACES(PsiPlugin AccountInfoAccessor OptionAccessor ActiveTabAccessor MenuAccessor StanzaFilter ContactInfoAccessor SoundAccessor
73
 
                     ToolbarIconAccessor IconFactoryAccessor StanzaSender ApplicationInfoAccessor PluginInfoProvider EventCreator PopupAccessor)
 
76
                                 ToolbarIconAccessor IconFactoryAccessor StanzaSender ApplicationInfoAccessor PluginInfoProvider EventCreator PopupAccessor)
74
77
 
75
78
public:
76
79
        ChessPlugin();
77
80
        virtual QString name() const;
78
81
        virtual QString shortName() const;
79
82
        virtual QString version() const;
80
 
        virtual QWidget* options();
 
83
        virtual QWidget* options();
81
84
        virtual bool enable();
82
 
        virtual bool disable();
83
 
        virtual void applyOptions();
84
 
        virtual void restoreOptions();
85
 
        virtual void optionChanged(const QString& /*option*/){};
86
 
        virtual void setAccountInfoAccessingHost(AccountInfoAccessingHost* host);
87
 
        virtual void setOptionAccessingHost(OptionAccessingHost* host);
88
 
        virtual void setActiveTabAccessingHost(ActiveTabAccessingHost* host);
89
 
        virtual void setIconFactoryAccessingHost(IconFactoryAccessingHost* host);
90
 
        virtual void setStanzaSendingHost(StanzaSendingHost *host);
 
85
        virtual bool disable();
 
86
        virtual void applyOptions();
 
87
        virtual void restoreOptions();
 
88
        virtual void optionChanged(const QString& /*option*/){};
 
89
        virtual void setAccountInfoAccessingHost(AccountInfoAccessingHost* host);
 
90
        virtual void setOptionAccessingHost(OptionAccessingHost* host);
 
91
        virtual void setActiveTabAccessingHost(ActiveTabAccessingHost* host);
 
92
        virtual void setIconFactoryAccessingHost(IconFactoryAccessingHost* host);
 
93
        virtual void setStanzaSendingHost(StanzaSendingHost *host);
91
94
        virtual QList < QVariantHash > getButtonParam();
92
95
        virtual QAction* getAction(QObject* , int , const QString& ) { return 0; };
93
96
        virtual QList < QVariantHash > getAccountMenuParam();
94
97
        virtual QList < QVariantHash > getContactMenuParam();
95
98
        virtual QAction* getContactAction(QObject* , int , const QString& ) { return 0; };
96
99
        virtual QAction* getAccountAction(QObject* , int ) { return 0; };
97
 
        virtual void setApplicationInfoAccessingHost(ApplicationInfoAccessingHost* host);
98
 
        virtual bool incomingStanza(int account, const QDomElement& xml);
 
100
        virtual void setApplicationInfoAccessingHost(ApplicationInfoAccessingHost* host);
 
101
        virtual bool incomingStanza(int account, const QDomElement& xml);
99
102
        virtual bool outgoingStanza(int account, QDomElement& xml);
100
103
        virtual void setEventCreatingHost(EventCreatingHost *host);
101
104
        virtual void setContactInfoAccessingHost(ContactInfoAccessingHost* host);
102
105
        virtual QString pluginInfo();
103
106
        virtual void setPopupAccessingHost(PopupAccessingHost* host);
104
107
        virtual void setSoundAccessingHost(SoundAccessingHost* host);
 
108
        virtual QPixmap icon() const;
105
109
 
106
110
public slots:
107
111
        void closeBoardEvent();
138
142
 
139
143
private:
140
144
        bool enabled;
141
 
        OptionAccessingHost* psiOptions;
 
145
        OptionAccessingHost* psiOptions;
142
146
        AccountInfoAccessingHost *accInfoHost;
143
 
        ActiveTabAccessingHost* activeTab;
 
147
        ActiveTabAccessingHost* activeTab;
144
148
        IconFactoryAccessingHost *icoHost;
145
 
        ApplicationInfoAccessingHost* appInfo;
146
 
        StanzaSendingHost *stanzaSender;
 
149
        ApplicationInfoAccessingHost* appInfo;
 
150
        StanzaSendingHost *stanzaSender;
147
151
        EventCreatingHost *psiEvent;
148
152
        ContactInfoAccessingHost* contactInfo;
149
153
        PopupAccessingHost* popup;
150
154
        SoundAccessingHost* sound_;
151
155
 
152
 
        ChessWindow *board;
153
 
        bool game_, theEnd_;
154
 
        bool waitFor;
155
 
        int id;
 
156
        ChessWindow *board;
 
157
        bool game_, theEnd_;
 
158
        bool waitFor;
 
159
        int id;
156
160
        QString tmpId;
157
161
 
158
 
        QString soundStart, soundFinish, soundMove, soundError;
159
 
        bool DndDisable, DefSoundSettings, enableSound;
 
162
        QString soundStart, soundFinish, soundMove, soundError;
 
163
        bool DndDisable, DefSoundSettings, enableSound;
160
164
 
161
 
        Ui::options ui_;
 
165
        Ui::options ui_;
162
166
 
163
167
        QList<Request> requests;
164
168
        QList<Request> invites;
165
169
        Request currentGame_;
166
170
};
167
171
 
 
172
#ifndef HAVE_QT5
168
173
Q_EXPORT_PLUGIN(ChessPlugin);
 
174
#endif
169
175
 
170
176
ChessPlugin::ChessPlugin()
171
177
        : enabled(false)
195
201
}
196
202
 
197
203
QString ChessPlugin::name() const {
198
 
        return "Chess Plugin";
 
204
        return "Chess Plugin";
199
205
}
200
206
 
201
207
QString ChessPlugin::shortName() const {
202
 
        return "chessplugin";
 
208
        return "chessplugin";
203
209
}
204
210
 
205
211
QString ChessPlugin::version() const {
206
 
        return cVer;
 
212
        return cVer;
207
213
}
208
214
 
209
215
bool ChessPlugin::enable() {
218
224
        requests.clear();
219
225
        invites.clear();
220
226
 
221
 
        enabled = true;
 
227
        enabled = true;
222
228
        QFile file(":/chessplugin/figures/Black queen 2d.png");
223
229
        if(file.open(QIODevice::ReadOnly)) {
224
230
                QByteArray ico = file.readAll();
225
231
                icoHost->addIcon("chessplugin/chess", ico);
226
232
                file.close();
227
 
        }
228
 
        soundStart = psiOptions->getPluginOption(soundStartConst, QVariant(soundStart)).toString();
229
 
        soundFinish = psiOptions->getPluginOption(soundFinishConst, QVariant(soundFinish)).toString();
230
 
        soundMove = psiOptions->getPluginOption(soundMoveConst, QVariant(soundMove)).toString();
231
 
        soundError = psiOptions->getPluginOption(soundErrorConst, QVariant(soundError)).toString();
232
 
        DndDisable = psiOptions->getPluginOption(constDndDisable, QVariant(DndDisable)).toBool();
233
 
        DefSoundSettings = psiOptions->getPluginOption(constDefSoundSettings, QVariant(DefSoundSettings)).toBool();
234
 
        return enabled;
 
233
        }
 
234
        soundStart = psiOptions->getPluginOption(soundStartConst, QVariant(soundStart)).toString();
 
235
        soundFinish = psiOptions->getPluginOption(soundFinishConst, QVariant(soundFinish)).toString();
 
236
        soundMove = psiOptions->getPluginOption(soundMoveConst, QVariant(soundMove)).toString();
 
237
        soundError = psiOptions->getPluginOption(soundErrorConst, QVariant(soundError)).toString();
 
238
        DndDisable = psiOptions->getPluginOption(constDndDisable, QVariant(DndDisable)).toBool();
 
239
        DefSoundSettings = psiOptions->getPluginOption(constDefSoundSettings, QVariant(DefSoundSettings)).toBool();
 
240
        return enabled;
235
241
}
236
242
 
237
243
bool ChessPlugin::disable() {
450
456
        r.chessId = "ch_111";
451
457
        r.jid += "/" + stanzaSender->escape(resource);
452
458
        r.requestId = newId();
453
 
        stanzaSender->sendStanza(r.account, QString("<iq type=\"set\" to=\"%1\" id=\"%2\"><create xmlns=\"games:board\" id=\"%4\" type=\"chess\" color=\"%3\"></create></iq>")
454
 
                                 .arg(r.jid)
455
 
                                 .arg(r.requestId)
456
 
                                 .arg(color)
457
 
                                 .arg(r.chessId));
 
459
        stanzaSender->sendStanza(r.account,
 
460
                                                         QString("<iq type=\"set\" to=\"%1\" id=\"%2\"><create xmlns=\"games:board\" id=\"%4\" type=\"chess\" color=\"%3\"></create></iq>")
 
461
                                                         .arg(r.jid)
 
462
                                                         .arg(r.requestId)
 
463
                                                         .arg(color)
 
464
                                                         .arg(r.chessId));
458
465
        if(color == "white")
459
466
                r.type = Figure::WhitePlayer;
460
467
        else
466
473
 
467
474
//Этот метод вызывается, когда вы принимаете приглашение
468
475
void ChessPlugin::accept() {
469
 
        stanzaSender->sendStanza(currentGame_.account, QString("<iq type=\"result\" to=\"%1\" id=\"%2\"><create xmlns=\"games:board\" type=\"chess\" id=\"%3\"/></iq>")
470
 
                                 .arg(currentGame_.jid)
471
 
                                 .arg(currentGame_.requestId)
472
 
                                 .arg(currentGame_.chessId));
 
476
        stanzaSender->sendStanza(currentGame_.account,
 
477
                                                         QString("<iq type=\"result\" to=\"%1\" id=\"%2\"><create xmlns=\"games:board\" type=\"chess\" id=\"%3\"/></iq>")
 
478
                                                         .arg(currentGame_.jid)
 
479
                                                         .arg(currentGame_.requestId)
 
480
                                                         .arg(currentGame_.chessId));
473
481
        acceptGame();
474
482
}
475
483
 
476
484
//Этот метод вызывается, когда вы отказываетесь от игры
477
485
void ChessPlugin::reject() {
478
 
        stanzaSender->sendStanza(currentGame_.account, QString("<iq type=\"error\" to=\"%1\" id=\"%2\"></iq>")
479
 
                                .arg(currentGame_.jid)
480
 
                                .arg(currentGame_.requestId));
 
486
        stanzaSender->sendStanza(currentGame_.account,
 
487
                                                         QString("<iq type=\"error\" to=\"%1\" id=\"%2\"></iq>")
 
488
                                                         .arg(currentGame_.jid)
 
489
                                                         .arg(currentGame_.requestId));
481
490
        rejectGame();
482
491
}
483
492
 
484
493
// Этот метод вызывается, когда вы закрыли доску.
485
494
void ChessPlugin::closeBoardEvent() {
486
 
        stanzaSender->sendStanza(currentGame_.account, QString("<iq type=\"set\" to=\"%1\" id=\"%2\"><close xmlns=\"games:board\" id=\"%3\" type=\"chess\"></close></iq>")
487
 
                                 .arg(currentGame_.jid)
488
 
                                 .arg(newId())
489
 
                                 .arg(currentGame_.chessId));
 
495
        stanzaSender->sendStanza(currentGame_.account,
 
496
                                                         QString("<iq type=\"set\" to=\"%1\" id=\"%2\"><close xmlns=\"games:board\" id=\"%3\" type=\"chess\"></close></iq>")
 
497
                                                         .arg(currentGame_.jid)
 
498
                                                         .arg(newId())
 
499
                                                         .arg(currentGame_.chessId));
490
500
 
491
501
        if((DefSoundSettings || psiOptions->getGlobalOption("options.ui.notifications.sounds.enable").toBool()) && enableSound)
492
502
                playSound(soundFinish);
497
507
void ChessPlugin::boardClosed() {
498
508
        if(theEnd_) return;
499
509
        QMessageBox::warning(board, tr("Chess Plugin"),
500
 
                             tr("Your opponent has closed the board!\n You can still save the game."), QMessageBox::Ok);
 
510
                                                 tr("Your opponent has closed the board!\n You can still save the game."), QMessageBox::Ok);
501
511
}
502
512
 
503
513
// Окончание игры
540
550
 
541
551
// Вы отправляете вашему сопернику сохраненную партию
542
552
void ChessPlugin::load(const QString& settings) {
543
 
        stanzaSender->sendStanza(currentGame_.account, QString("<iq type=\"set\" to=\"%1\" id=\"%2\"><load xmlns=\"games:board\" id=\"%3\" type=\"chess\">%4</load></iq>")
544
 
                                 .arg(currentGame_.jid)
545
 
                                 .arg(newId())
546
 
                                 .arg(currentGame_.chessId)
547
 
                                 .arg(settings));
 
553
        stanzaSender->sendStanza(currentGame_.account,
 
554
                                                         QString("<iq type=\"set\" to=\"%1\" id=\"%2\"><load xmlns=\"games:board\" id=\"%3\" type=\"chess\">%4</load></iq>")
 
555
                                                         .arg(currentGame_.jid)
 
556
                                                         .arg(newId())
 
557
                                                         .arg(currentGame_.chessId)
 
558
                                                         .arg(settings));
548
559
}
549
560
 
550
561
// Вы походили
551
562
void ChessPlugin::move(int oldX, int oldY, int newX, int newY, const QString& figure) {
552
563
        QString stanza = QString("<iq type=\"set\" to=\"%1\" id=\"%2\"><turn xmlns=\"games:board\" type=\"chess\" id=\"%7\"><move pos=\"%3,%4;%5,%6\">")
553
 
                         .arg(currentGame_.jid)
554
 
                         .arg(newId())
555
 
                         .arg(QString::number(oldX))
556
 
                         .arg(QString::number(oldY))
557
 
                         .arg(QString::number(newX))
558
 
                         .arg(QString::number(newY))
559
 
                         .arg(currentGame_.chessId);
 
564
                                                         .arg(currentGame_.jid)
 
565
                                                         .arg(newId())
 
566
                                                         .arg(QString::number(oldX))
 
567
                                                         .arg(QString::number(oldY))
 
568
                                                         .arg(QString::number(newX))
 
569
                                                         .arg(QString::number(newY))
 
570
                                                         .arg(currentGame_.chessId);
560
571
        if(!figure.isEmpty())
561
572
                stanza += QString("<promotion>%1</promotion>").arg(figure);
562
573
        stanza += "</move></turn></iq>";
567
578
 
568
579
// Вы согласились с ходом
569
580
void ChessPlugin::moveAccepted() {
570
 
        stanzaSender->sendStanza(currentGame_.account, QString("<iq type=\"result\" to=\"%1\" id=\"%2\"><turn type=\"chess\" id=\"%3\" xmlns=\"games:board\"/></iq>")
571
 
                                 .arg(currentGame_.jid)
572
 
                                 .arg(tmpId)
573
 
                                 .arg(currentGame_.chessId));
 
581
        stanzaSender->sendStanza(currentGame_.account,
 
582
                                                         QString("<iq type=\"result\" to=\"%1\" id=\"%2\"><turn type=\"chess\" id=\"%3\" xmlns=\"games:board\"/></iq>")
 
583
                                                         .arg(currentGame_.jid)
 
584
                                                         .arg(tmpId)
 
585
                                                         .arg(currentGame_.chessId));
574
586
}
575
587
 
576
588
void ChessPlugin::youLose() {
577
589
        if(theEnd_) return;
578
 
        stanzaSender->sendStanza(currentGame_.account, QString("<iq type=\"set\" to=\"%1\" id=\"%2\"><turn xmlns=\"games:board\" type=\"chess\" id=\"%3\"><resign/></turn></iq>")
579
 
                                 .arg(currentGame_.jid)
580
 
                                 .arg(newId())
581
 
                                 .arg(currentGame_.chessId));
 
590
        stanzaSender->sendStanza(currentGame_.account,
 
591
                                                         QString("<iq type=\"set\" to=\"%1\" id=\"%2\"><turn xmlns=\"games:board\" type=\"chess\" id=\"%3\"><resign/></turn></iq>")
 
592
                                                         .arg(currentGame_.jid)
 
593
                                                         .arg(newId())
 
594
                                                         .arg(currentGame_.chessId));
582
595
        board->youLose();
583
596
        theEnd_ = true;
584
597
        QMessageBox::information(board, tr("Chess Plugin"),
585
 
                                 tr("You Lose."), QMessageBox::Ok);
 
598
                                                         tr("You Lose."), QMessageBox::Ok);
586
599
}
587
600
 
588
601
void ChessPlugin::youWin() {
592
605
        board->youWin();
593
606
        theEnd_ = true;
594
607
        QMessageBox::information(board, tr("Chess Plugin"),
595
 
                                 tr("You Win!"), QMessageBox::Ok);
 
608
                                                         tr("You Win!"), QMessageBox::Ok);
596
609
}
597
610
 
598
611
void ChessPlugin::draw() {
599
612
        if(!theEnd_) {
600
 
                stanzaSender->sendStanza(currentGame_.account, QString("<iq type=\"set\" to=\"%1\" id=\"%2\"><turn xmlns=\"games:board\" type=\"chess\" id=\"%3\"><draw/></turn></iq>")
601
 
                                         .arg(currentGame_.jid)
602
 
                                         .arg(newId())
603
 
                                         .arg(currentGame_.chessId));
 
613
                stanzaSender->sendStanza(currentGame_.account,
 
614
                                                                 QString("<iq type=\"set\" to=\"%1\" id=\"%2\"><turn xmlns=\"games:board\" type=\"chess\" id=\"%3\"><draw/></turn></iq>")
 
615
                                                                 .arg(currentGame_.jid)
 
616
                                                                 .arg(newId())
 
617
                                                                 .arg(currentGame_.chessId));
604
618
                if((DefSoundSettings || psiOptions->getGlobalOption("options.ui.notifications.sounds.enable").toBool()) && enableSound)
605
619
                        playSound(soundStart);
606
620
                board->youDraw();
607
621
                theEnd_ = true;
608
622
                QMessageBox::information(board, tr("Chess Plugin"),
609
 
                                         tr("Draw!"), QMessageBox::Ok);
 
623
                                                                 tr("Draw!"), QMessageBox::Ok);
610
624
        }
611
625
}
612
626
 
614
628
        if((DefSoundSettings || psiOptions->getGlobalOption("options.ui.notifications.sounds.enable").toBool()) && enableSound)
615
629
                playSound(soundError);
616
630
        QMessageBox::warning(board, tr("Chess Plugin"),
617
 
                             tr("Unknown error!"), QMessageBox::Ok);
 
631
                                                 tr("Unknown error!"), QMessageBox::Ok);
618
632
        board->close();
619
633
}
620
634
 
633
647
                                const QString xmlId = stanzaSender->escape(xml.attribute("id"));
634
648
                                const QString xmlFrom = stanzaSender->escape(xml.attribute("from"));
635
649
                                if((DndDisable && accInfoHost->getStatus(account) == "dnd") || game_) {
636
 
                                        stanzaSender->sendStanza(account, QString("<iq type=\"error\" to=\"%1\" id=\"%2\"></iq>")
637
 
                                                                 .arg(xmlFrom)
638
 
                                                                 .arg(xmlId));
 
650
                                        stanzaSender->sendStanza(account,
 
651
                                                                                         QString("<iq type=\"error\" to=\"%1\" id=\"%2\"></iq>")
 
652
                                                                                         .arg(xmlFrom)
 
653
                                                                                         .arg(xmlId));
639
654
                                        return true;
640
655
                                }
641
656
                                const QString color = stanzaSender->escape(createElem.attribute("color"));
753
768
 
754
769
        if(game_) {
755
770
                QMessageBox::information(0, tr("Chess Plugin"), tr("You are allready playing!"));
756
 
                stanzaSender->sendStanza(rec.account, QString("<iq type=\"error\" to=\"%1\" id=\"%2\"></iq>")
757
 
                                         .arg(rec.jid)
758
 
                                         .arg(rec.requestId));
 
771
                stanzaSender->sendStanza(rec.account,
 
772
                                                                 QString("<iq type=\"error\" to=\"%1\" id=\"%2\"></iq>")
 
773
                                                                 .arg(rec.jid)
 
774
                                                                 .arg(rec.requestId));
759
775
                return;
760
776
        }
761
777
 
840
856
}
841
857
 
842
858
QString ChessPlugin::pluginInfo() {
843
 
        return tr("Author: ") +  "Dealer_WeARE\n"
844
 
                        + tr("Email: ") + "wadealer@gmail.com\n\n"
845
 
                        + trUtf8("This plugin allows you to play chess with your friends.\n"
846
 
                                 "The plugin is compatible with a similar plugin for Tkabber.\n"
847
 
                                 "For sending commands, normal messages are used, so this plugin will always work wherever you are able to log in."
848
 
                                 "To invite a friend for a game, you can use contact menu item or the button on the toolbar in a chat window.");
 
859
        return tr("Author: ") +  "Dealer_WeARE\n"
 
860
                 + tr("Email: ") + "wadealer@gmail.com\n\n"
 
861
                 + trUtf8("This plugin allows you to play chess with your friends.\n"
 
862
                                  "The plugin is compatible with a similar plugin for Tkabber.\n"
 
863
                                  "For sending commands, normal messages are used, so this plugin will always work wherever you are able to log in."
 
864
                                  "To invite a friend for a game, you can use contact menu item or the button on the toolbar in a chat window.");
849
865
}
850
866
 
 
867
QPixmap ChessPlugin::icon() const
 
868
{
 
869
        return QPixmap(":/chessplugin/chess.png");
 
870
}
851
871
 
852
872
#include "chessplugin.moc"