~ubuntu-branches/ubuntu/karmic/psi/karmic

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * chessplugin.cpp - Psi plugin to play chess
 
3
 * Copyright (C) 2006  Kevin Smith
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * You can also redistribute and/or modify this program under the
 
11
 * terms of the Psi License, specified in the accompanied COPYING
 
12
 * file, as published by the Psi Project; either dated January 1st,
 
13
 * 2005, or (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this library; if not, write to the Free Software
 
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
23
 *
 
24
 */
 
25
 
 
26
#include <QtCore>
 
27
#include <QDebug>
 
28
 
 
29
#include "psiplugin.h"
 
30
#include "gameboard.h"
 
31
 
 
32
extern QColor   cw, cb;
 
33
 
 
34
class ChessPlugin : public PsiPlugin
 
35
{
 
36
        Q_OBJECT
 
37
        Q_INTERFACES(PsiPlugin)
 
38
 
 
39
public:
 
40
        ChessPlugin();
 
41
        virtual QString name() const; 
 
42
        virtual bool processEvent( const PsiAccount* account, QDomNode &event );
 
43
        virtual void message( const PsiAccount* account, const QString& message, const QString& fromJid, const QString& fromDisplay); 
 
44
        virtual QString shortName() const;
 
45
        virtual QString version() const;
 
46
 
 
47
private slots:
 
48
        void sendData(const QString& data);
 
49
        void receiveData(const QString& data);
 
50
 
 
51
        
 
52
private:
 
53
        void startGame(const QString& jid, bool meWhite, const PsiAccount* account);
 
54
        void stopGame();
 
55
        
 
56
 
 
57
        
 
58
        GameBoard* game_;
 
59
        QString playingWith_;
 
60
        PsiAccount* account_;
 
61
};
 
62
 
 
63
Q_EXPORT_PLUGIN(ChessPlugin);
 
64
 
 
65
ChessPlugin::ChessPlugin() : PsiPlugin()
 
66
{
 
67
        game_ = NULL;
 
68
}
 
69
 
 
70
QString ChessPlugin::name() const
 
71
{
 
72
        return "Chess Plugin";
 
73
}
 
74
 
 
75
QString ChessPlugin::shortName() const
 
76
{
 
77
        return "chess";
 
78
}
 
79
 
 
80
QString ChessPlugin::version() const
 
81
{
 
82
        return "0.0.1";
 
83
}
 
84
 
 
85
bool ChessPlugin::processEvent( const PsiAccount* account, QDomNode &event )
 
86
{
 
87
        return true;
 
88
}
 
89
 
 
90
 
 
91
void ChessPlugin::message( const PsiAccount* account, const QString& message, const QString& fromJid, const QString& fromDisplay)
 
92
{
 
93
        QString reply;
 
94
        qDebug("chess message");
 
95
        if (!message.startsWith("chess"))
 
96
                return;
 
97
        qDebug("message for us in chess");
 
98
        if (game_ && fromJid != playingWith_)
 
99
        {
 
100
                reply=QString("<message to=\"%1\" type=\"chat\"><body>already playing chess with %2, sorry</body></message>").arg(fromJid).arg(playingWith_);
 
101
                emit sendStanza(account, reply);
 
102
                return;
 
103
        }
 
104
        QString command = QString(message);
 
105
        command.remove(0,6);
 
106
        qDebug() << (qPrintable(QString("chess command string %1").arg(command)));
 
107
        if (command == QString("start"))
 
108
        {
 
109
                if (game_)
 
110
                        return;
 
111
                qWarning(qPrintable(QString("Received message '%1', launching chess with %2").arg(message).arg(fromDisplay)));
 
112
                QString reply;
 
113
                reply=QString("<message to=\"%1\" type=\"chat\"><body>chess starting</body></message>").arg(fromJid);
 
114
                emit sendStanza(account, reply);
 
115
                startGame(fromJid, false, account);
 
116
        }
 
117
        else if (command == QString("starting"))
 
118
        {
 
119
                if (game_)
 
120
                        return;
 
121
                qWarning(qPrintable(QString("Received message '%1', launching chess with %2").arg(message).arg(fromDisplay)));
 
122
                QString reply;
 
123
                reply=QString("<message to=\"%1\" type=\"chat\"><body>starting chess, I go first :)</body></message>").arg(fromJid);
 
124
                emit sendStanza(account, reply);
 
125
                startGame(fromJid, true, account);
 
126
        }
 
127
        else if (!game_)
 
128
        {
 
129
                return;
 
130
        }
 
131
        else if (command.startsWith("command"))
 
132
        {
 
133
                command.remove(0,8);
 
134
                
 
135
                qDebug() << (qPrintable(QString("chess command %1").arg(command)));
 
136
                receiveData(command);
 
137
        }
 
138
}
 
139
 
 
140
void ChessPlugin::startGame(const QString& jid, bool meFirst, const PsiAccount* account)
 
141
{
 
142
/*      game = new TicTacToe( meFirst, size );
 
143
        game->setCaption(QString("Noughts and Crosses with %1").arg(jid));
 
144
        playingWith=jid;
 
145
    game->show();
 
146
        account_=(PsiAccount*)account;
 
147
        connect(game, SIGNAL(closing()), this, SLOT(stopGame()));
 
148
        connect(game, SIGNAL(myMove(int)), this, SLOT(myTurn(int)));
 
149
        connect(game, SIGNAL(gameOverSignal(TicTacGameBoard::State)), this, SLOT(gameOver(TicTacGameBoard::State)));
 
150
*/
 
151
        playingWith_=jid;
 
152
        account_=(PsiAccount*)account;
 
153
        cw = QColor(0x8F, 0xDF, 0xF0);
 
154
        cb = QColor(0x70, 0x9F, 0xDF);
 
155
        if (meFirst)
 
156
        {
 
157
                GameBoard::GameType type=GameBoard::WHITE;
 
158
                game_ = new GameBoard(type, jid, NULL);
 
159
        } else {
 
160
                game_ = new GameBoard(1);
 
161
        }
 
162
        
 
163
        //showStatus(game_->status());
 
164
        //QObject::connect(brd, SIGNAL(showStatus(const QString&)), this, SLOT(showStatus(const QString&)));
 
165
        connect(game_, SIGNAL(sendData(const QString&)), this, SLOT(sendData(const QString &)));
 
166
        game_->show();
 
167
 }
 
168
 
 
169
void ChessPlugin::stopGame()
 
170
{
 
171
        delete game_;
 
172
        game_=NULL;
 
173
}
 
174
 
 
175
/*void ChessPlugin::gameOver(TicTacGameBoard::State state)
 
176
{
 
177
        QString reply;
 
178
        QString winner;
 
179
        switch (state)
 
180
        {
 
181
                case TicTacGameBoard::HumanWon:
 
182
                        winner="I";
 
183
                        break;
 
184
                case TicTacGameBoard::ComputerWon:
 
185
                        winner="You";
 
186
                        break;
 
187
                case TicTacGameBoard::NobodyWon:
 
188
                        winner="It was a draw, no-one";
 
189
                        break;
 
190
                default:
 
191
                        winner="ERROR!!!";
 
192
        }
 
193
        reply=QString("<message to=\"%1\" type=\"chat\"><body>%2 won. Good game.</body></message>").arg(playingWith).arg(winner);
 
194
        emit sendStanza(account_, reply);
 
195
}*/
 
196
 
 
197
void ChessPlugin::sendData(const QString& data)
 
198
{
 
199
        qDebug() << (qPrintable(QString("sendingData turn: %1").arg(data)));
 
200
        if (!game_)
 
201
                return;
 
202
        QString reply;
 
203
        QString stanzaId="aaaa";
 
204
        reply=QString("<message to=\"%1\" type=\"chat\" id=\"%2\"><body>chess command %3</body></message>").arg(playingWith_).arg(stanzaId).arg(data);
 
205
        qDebug() << (qPrintable(QString("sendingData stanza: %1").arg(reply)));
 
206
        emit sendStanza(account_, reply);
 
207
}
 
208
 
 
209
void ChessPlugin::receiveData(const QString& data)
 
210
{
 
211
        qDebug() << (qPrintable(QString("received data: %1").arg(data)));
 
212
        if (!game_)
 
213
                return;
 
214
        //game->theirMove(space);
 
215
        game_->receiveData(data);
 
216
}
 
217
 
 
218
 
 
219
#include "chessplugin.moc"