~ubuntu-branches/ubuntu/wily/psi/wily-proposed

« back to all changes in this revision

Viewing changes to src/plugins/generic/noughtsandcrosses/noughtsandcrossesplugin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include <QDebug>
28
28
 
29
29
#include "psiplugin.h"
 
30
#include "eventfilter.h"
 
31
#include "stanzasender.h"
 
32
#include "stanzasendinghost.h"
 
33
 
30
34
#include "tictac.h"
31
35
 
32
 
class NoughtsAndCrossesPlugin : public PsiPlugin
 
36
class NoughtsAndCrossesPlugin : public QObject, public PsiPlugin, public EventFilter, public StanzaSender
33
37
{
34
38
        Q_OBJECT
35
 
        Q_INTERFACES(PsiPlugin)
 
39
        Q_INTERFACES(PsiPlugin EventFilter StanzaSender)
36
40
 
37
41
public:
38
42
        NoughtsAndCrossesPlugin();
39
 
        virtual QString name() const; 
40
 
        virtual void message( const PsiAccount* account, const QString& message, const QString& fromJid, const QString& fromDisplay); 
 
43
 
 
44
        virtual QString name() const;
41
45
        virtual QString shortName() const;
42
46
        virtual QString version() const;
 
47
        virtual QWidget* options() const;
 
48
        virtual bool enable();
 
49
        virtual bool disable();
 
50
 
 
51
        virtual void setStanzaSendingHost(StanzaSendingHost *host);
 
52
 
 
53
    virtual bool processEvent(int account, const QDomElement& e);
 
54
        virtual bool processMessage(int account, const QString& fromJid, const QString& body, const QString& subject);
43
55
 
44
56
private slots:
45
57
        void stopGame();
48
60
        void gameOver(TicTacGameBoard::State state);
49
61
        
50
62
private:
51
 
        void startGame(QString jid, int size, bool meFirst, const PsiAccount* account);
 
63
        void startGame(QString jid, int size, bool meFirst, int account);
52
64
        
53
65
 
54
66
        
55
67
        TicTacToe* game;
56
68
        QString playingWith;
57
 
        PsiAccount* account_;
 
69
        int account_;
 
70
        bool enabled_;
 
71
        StanzaSendingHost* stanzaSender_;
58
72
};
59
73
 
60
74
Q_EXPORT_PLUGIN(NoughtsAndCrossesPlugin);
61
75
 
62
 
NoughtsAndCrossesPlugin::NoughtsAndCrossesPlugin() : PsiPlugin()
 
76
NoughtsAndCrossesPlugin::NoughtsAndCrossesPlugin()
63
77
{
64
78
        game = NULL;
 
79
        enabled_ = false;
 
80
        stanzaSender_ = 0;
65
81
}
66
82
 
67
83
QString NoughtsAndCrossesPlugin::name() const
76
92
 
77
93
QString NoughtsAndCrossesPlugin::version() const
78
94
{
79
 
        return "0.0.1";
80
 
}
81
 
 
82
 
void NoughtsAndCrossesPlugin::message( const PsiAccount* account, const QString& message, const QString& fromJid, const QString& fromDisplay)
83
 
{
 
95
        return "0.1";
 
96
}
 
97
 
 
98
QWidget* NoughtsAndCrossesPlugin::options() const
 
99
{
 
100
        return 0;
 
101
}
 
102
 
 
103
bool NoughtsAndCrossesPlugin::enable()
 
104
{
 
105
        if (stanzaSender_) {
 
106
                enabled_ = true;
 
107
        }
 
108
        return enabled_;
 
109
}
 
110
 
 
111
bool NoughtsAndCrossesPlugin::disable()
 
112
{
 
113
        stopGame();
 
114
        enabled_ = false;
 
115
        return true;
 
116
}
 
117
 
 
118
void NoughtsAndCrossesPlugin::setStanzaSendingHost(StanzaSendingHost *host)
 
119
{
 
120
        stanzaSender_ = host;
 
121
}
 
122
 
 
123
 
 
124
bool NoughtsAndCrossesPlugin::processEvent(int account, const QDomElement& e)
 
125
{
 
126
        Q_UNUSED(account);
 
127
        Q_UNUSED(e);
 
128
        return false;
 
129
}
 
130
 
 
131
bool NoughtsAndCrossesPlugin::processMessage(int account, const QString& fromJid, const QString& message, const QString& subject)
 
132
{
 
133
        // FIXME(mck)
 
134
        QString fromDisplay = fromJid;
 
135
 
 
136
        Q_UNUSED(subject);
 
137
 
 
138
        if (!enabled_) {
 
139
                return false;
 
140
        }
 
141
 
84
142
        QString reply;
85
143
        qDebug("naughtsandcrosses message");
86
144
        if (!message.startsWith("noughtsandcrosses"))
87
 
                return;
 
145
                return false;
88
146
        qDebug("message for us in noughtsandcrosses");
89
147
        if (game && fromJid != playingWith)
90
148
        {
91
149
                reply=QString("<message to=\"%1\" type=\"chat\"><body>already playing with %2, sorry</body></message>").arg(fromJid).arg(playingWith);
92
 
                emit sendStanza(account, reply);
93
 
                return;
 
150
                stanzaSender_->sendStanza(account, reply);
 
151
                return true;
94
152
        }
95
153
        QString command = QString(message);
96
154
        command.remove(0,18);
98
156
        if (command == QString("start"))
99
157
        {
100
158
                if (game)
101
 
                        return;
 
159
                        return true;
102
160
                qWarning(qPrintable(QString("Received message '%1', launching nac with %2").arg(message).arg(fromDisplay)));
103
161
                QString reply;
104
162
                reply=QString("<message to=\"%1\" type=\"chat\"><body>noughtsandcrosses starting</body></message>").arg(fromJid);
105
 
                emit sendStanza(account, reply);
 
163
                stanzaSender_->sendStanza(account, reply);
106
164
                startGame(fromJid, 3, false, account);
107
165
        }
108
166
        else if (command == QString("starting"))
109
167
        {
110
168
                if (game)
111
 
                        return;
 
169
                        return true;
112
170
                qWarning(qPrintable(QString("Received message '%1', launching nac with %2").arg(message).arg(fromDisplay)));
113
171
                QString reply;
114
172
                reply=QString("<message to=\"%1\" type=\"chat\"><body>starting noughts and crosses, I go first :)</body></message>").arg(fromJid);
115
 
                emit sendStanza(account, reply);
 
173
                stanzaSender_->sendStanza(account, reply);
116
174
                startGame(fromJid, 3, true, account);
117
175
        }
118
176
        else if (!game)
119
177
        {
120
 
                return;
 
178
                return true;
121
179
        }
122
180
        else if (command.startsWith("move"))
123
181
        {
127
185
                qDebug() << (qPrintable(QString("noughtsandcrosses move to space %1").arg(space)));
128
186
                theirTurn(space);
129
187
        }
 
188
        return true;
130
189
}       
131
190
 
132
 
void NoughtsAndCrossesPlugin::startGame(QString jid, int size, bool meFirst, const PsiAccount* account)
 
191
void NoughtsAndCrossesPlugin::startGame(QString jid, int size, bool meFirst, int account)
133
192
{
134
193
        game = new TicTacToe( meFirst, size );
135
194
        game->setCaption(QString("Noughts and Crosses with %1").arg(jid));
136
195
        playingWith=jid;
137
196
    game->show();
138
 
        account_=(PsiAccount*)account;
 
197
        account_=account;
139
198
        connect(game, SIGNAL(closing()), this, SLOT(stopGame()));
140
199
        connect(game, SIGNAL(myMove(int)), this, SLOT(myTurn(int)));
141
200
        connect(game, SIGNAL(gameOverSignal(TicTacGameBoard::State)), this, SLOT(gameOver(TicTacGameBoard::State)));
166
225
                        winner="ERROR!!!";
167
226
        }
168
227
        reply=QString("<message to=\"%1\" type=\"chat\"><body>%2 won. Good game.</body></message>").arg(playingWith).arg(winner);
169
 
        emit sendStanza(account_, reply);
 
228
        stanzaSender_->sendStanza(account_, reply);
170
229
}
171
230
 
172
231
void NoughtsAndCrossesPlugin::myTurn(int space)
176
235
                return;
177
236
        QString reply;
178
237
        reply=QString("<message to=\"%1\" type=\"chat\"><body>noughtsandcrosses move %2</body></message>").arg(playingWith).arg(space);
179
 
        emit sendStanza(account_, reply);
 
238
        stanzaSender_->sendStanza(account_, reply);
180
239
}
181
240
 
182
241
void NoughtsAndCrossesPlugin::theirTurn(int space)