~kubuntu-members/libkdegames/4.11

« back to all changes in this revision

Viewing changes to libkdegamesprivate/kchat.cpp

  • Committer: Stefan Majewsky
  • Date: 2012-05-01 15:34:35 UTC
  • Revision ID: git-v1:82376fb5ca6f29f862641b6ca68603cb76258831
Begin to move stuff into libkdegamesprivate.

The build is now broken because I'm moving stuff without adjusting the
CMake files. But I figured it's cleaner to have the move in one commit
and the various edits in CMake and source files in the next commits.

svn path=/trunk/KDE/kdegames/libkdegames/; revision=1292461

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This file is part of the KDE games library
 
3
    Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
 
4
 
 
5
    This library is free software; you can redistribute it and/or
 
6
    modify it under the terms of the GNU Library General Public
 
7
    License version 2 as published by the Free Software Foundation.
 
8
 
 
9
    This library is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
    Library General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Library General Public License
 
15
    along with this library; see the file COPYING.LIB.  If not, write to
 
16
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
    Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "kchat.h"
 
21
 
 
22
#include <QtCore/QMap>
 
23
 
 
24
#include <klocale.h>
 
25
#include <kdebug.h>
 
26
 
 
27
#include "kchatbasemodel.h"
 
28
#include "kchatbaseitemdelegate.h"
 
29
 
 
30
class KChatPrivate
 
31
{
 
32
public:
 
33
        KChatPrivate()
 
34
        {
 
35
        }
 
36
 
 
37
        bool mAutoAddMessages;
 
38
 
 
39
        QMap<int, QString> mPlayerMap;
 
40
        int mPlayerId;
 
41
        int mFromId;
 
42
};
 
43
 
 
44
KChat::KChat(QWidget* parent, bool twoPlayerGame) 
 
45
    : KChatBase(parent, 
 
46
                new KChatBaseModel(parent),
 
47
                new KChatBaseItemDelegate(parent),twoPlayerGame),
 
48
      d( new KChatPrivate )
 
49
{
 
50
 init(); 
 
51
}
 
52
 
 
53
KChat::KChat(QWidget* parent, KChatBaseModel* model, KChatBaseItemDelegate* delegate, bool noComboBox)
 
54
    : KChatBase(parent, model, delegate, noComboBox),
 
55
    d( new KChatPrivate )
 
56
{
 
57
    init();
 
58
}
 
59
 
 
60
KChat::~KChat()
 
61
{
 
62
 kDebug(11000) << "DESTRUCT KChat" << this;
 
63
 delete d;
 
64
}
 
65
 
 
66
void KChat::init()
 
67
{
 
68
 kDebug(11001) << "INIT KChat" << this;
 
69
 d->mAutoAddMessages = true;
 
70
 d->mPlayerId = 1;
 
71
 d->mFromId = 1;
 
72
}
 
73
 
 
74
void KChat::setFromNickname(const QString& n)
 
75
{ d->mFromId = addPlayer(n); }
 
76
QString KChat::fromName() const
 
77
{ return player(fromId()); }
 
78
void KChat::setAutoAddMessages(bool add) 
 
79
{ d->mAutoAddMessages = add; }
 
80
bool KChat::autoAddMessages() const 
 
81
{ return d->mAutoAddMessages; }
 
82
int KChat::uniqueId()
 
83
{ return d->mPlayerId++; }
 
84
int KChat::fromId() const
 
85
{ return d->mFromId; }
 
86
QString KChat::player(int id) const
 
87
{ return d->mPlayerMap[id]; }
 
88
 
 
89
void KChat::returnPressed(const QString& text)
 
90
{
 
91
 int id = fromId();
 
92
 if (id < 0) {
 
93
        // don't return - just display "unknown" as name
 
94
        kWarning(11000) << "KChat: no fromNickname has been set!";
 
95
 }
 
96
 emit signalSendMessage(id, text);
 
97
 if (autoAddMessages()) {
 
98
        QString p = player(id);
 
99
        if (p.isNull()) {
 
100
                p = i18nc("Unknown player", "Unknown");
 
101
        }
 
102
        kDebug(11000) << "auto adding message from player" << p << " ;id=" << id;
 
103
        addMessage(p, text);
 
104
 }
 
105
}
 
106
 
 
107
int KChat::addPlayer(const QString& nickname)
 
108
{
 
109
 int id = uniqueId();
 
110
 d->mPlayerMap.insert(id, nickname);
 
111
 return id;
 
112
}
 
113
 
 
114
void KChat::removePlayer(int id)
 
115
{
 
116
 d->mPlayerMap.remove(id);
 
117
}
 
118
 
 
119
void KChat::removePlayer(const QString& nickname)
 
120
{
 
121
 QMap<int, QString>::Iterator it;
 
122
 for (it = d->mPlayerMap.begin(); it != d->mPlayerMap.end(); ++it) {
 
123
        if (it.value() == nickname) {
 
124
                d->mPlayerMap.erase(it);
 
125
        }
 
126
 }
 
127
}
 
128
 
 
129
 
 
130
#include "kchat.moc"