KDEGames

kgameproperty.cpp
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2001 Andreas Beckermann ([email protected])
4  Copyright (C) 2001 Martin Heni (kde at heni-online.de)
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kgameproperty.h"
22 #include "kgamepropertyhandler.h"
23 #include "kgamemessage.h"
24 #include "kplayer.h"
25 #include "kgame.h"
26 
27 #define KPLAYERHANDLER_LOAD_COOKIE 6239
28 
30 {
31  init();
32  registerData(id, parent);
33 }
34 
36 {
37  init();
38  registerData(id, parent);
39 }
40 
42 {
43  init();
44  registerData(id, owner);
45 }
46 
48 {
49  init();
50 }
51 
52 KGamePropertyBase::~KGamePropertyBase()
53 {
54  unregisterData();
55 }
56 
57 void KGamePropertyBase::init()
58 {
59  mOwner = nullptr;
60  setDirty(false);
61 
62  // this is very useful and used by e.g. KGameDialog so
63  // it is activated by default. Big games may profit by deactivating it to get
64  // a better performance.
65  setEmittingSignal(true);
66 
67  setOptimized(false);
68 
69  //setReadOnly(false);
70  mFlags.bits.locked = false ; // setLocked(false); is NOT possible as it checks whether isLocked() allows to change the status
71 
72  // local is default
73  setPolicy(PolicyLocal);
74 }
75 
76 int KGamePropertyBase::registerData(int id, KGame* owner, const QString& name)
77 { return registerData(id, owner->dataHandler(), name); }
78 
79 int KGamePropertyBase::registerData(int id, KPlayer* owner, const QString& name)
80 { return registerData(id, owner->dataHandler(), name); }
81 
83 { return registerData(-1, owner,p, name); }
84 
86 { return registerData(id, owner,PolicyUndefined, name); }
87 
89 {
90 // we don't support changing the id
91  if (!owner) {
92  qCWarning(GAMES_PRIVATE_KGAME) << "Resetting owner=0. Sure you want to do this?";
93  mOwner=nullptr;
94  return -1;
95  }
96  if (!mOwner) {
97  if (id==-1) {
98  id=owner->uniquePropertyId();
99  }
100  mId = id;
101  mOwner = owner;
102  mOwner->addProperty(this, name);
103  if (p!=PolicyUndefined) {
104  setPolicy(p);
105  } else {
106  setPolicy(mOwner->policy());
107  }
108  }
109  return mId;
110 }
111 
112 void KGamePropertyBase::unregisterData()
113 {
114  if (!mOwner) {
115  return;
116  }
117  mOwner->removeProperty(this);
118  mOwner = nullptr;
119 }
120 
122 {
123  QByteArray b;
125  KGameMessage::createPropertyHeader(s, id());
126  save(s);
127  if (mOwner) {
128  return mOwner->sendProperty(s);
129  } else {
130  qCCritical(GAMES_PRIVATE_KGAME) << "Cannot send because there is no receiver defined";
131  return false;
132  }
133 }
134 
136 {
137  QByteArray b;
139  KGameMessage::createPropertyHeader(s, id());
140  s.writeRawData(data.data(), data.size());
141  if (mOwner) {
142  return mOwner->sendProperty(s);
143  } else {
144  qCCritical(GAMES_PRIVATE_KGAME) << ": Cannot send because there is no receiver defined";
145  return false;
146  }
147 }
148 
150 {
151  if (isLocked()) {
152  return false;
153  }
154  setLock(true);
155  return true;
156 }
157 
159 {
160  if (isLocked() && !force) {
161  return false;
162  }
163  setLock(false);
164  return true;
165 }
166 
168 {
169  QByteArray b;
171  KGameMessage::createPropertyCommand(s, IdCommand, id(), CmdLock);
172 
173  s << (qint8)l;
174  if (mOwner) {
175  mOwner->sendProperty(s);
176  } else {
177  qCCritical(GAMES_PRIVATE_KGAME) << ": Cannot send because there is no receiver defined";
178  return ;
179  }
180 }
181 
183 {
184  //qCDebug(GAMES_PRIVATE_KGAME) << ": mOwnerP="<< mOwner << "id=" << id();
185  if (mOwner ) {
186  mOwner->emitSignal(this);
187  } else {
188  qCCritical(GAMES_PRIVATE_KGAME) << ":id="<<id()<<" Cannot emitSignal because there is no handler set";
189  }
190 }
191 
192 void KGamePropertyBase::command(QDataStream& s, int cmd, bool isSender)
193 {
194  switch (cmd) {
195  case CmdLock:
196  {
197  if (!isSender) {
198  qint8 locked;
199  s >> locked;
200  mFlags.bits.locked = (bool)locked ;
201  break;
202  }
203  }
204  default: // probably in derived classes
205  break;
206  }
207 }
208 
void setEmittingSignal(bool p)
Sets this property to emit a signal on value changed.
int registerData(int id, KGamePropertyHandler *owner, PropertyPolicy p, const QString &name=QString())
You have to register a KGamePropertyBase before you can use it.
bool isLocked() const
A locked property can only be changed by the player who has set the lock.
bool sendProperty()
Forward the data to the owner of this property which then sends it over network.
A collection class for KGameProperty objects.
void setOptimized(bool p)
Sets this property to try to optimize signal and network handling by not sending it out when the prop...
void setDirty(bool d)
Sets the "dirty" flag of the property.
bool removeProperty(KGamePropertyBase *data)
Removes a property from the handler.
PropertyPolicy
The policy of the property.
void emitSignal()
Causes the parent object to emit a signal on value change.
void setLock(bool l)
A locked property can only be changed by the player who has set the lock.
KGamePropertyHandler * dataHandler() const
Returns a pointer to the KGame property handler.
Definition: kgame.cpp:697
void emitSignal(KGamePropertyBase *data)
called by a property to emit a signal This call is simply forwarded to the parent object ...
bool unlock(bool force=false)
A locked property can only be changed by the player who has set the lock.
void setPolicy(PropertyPolicy p)
Changes the consistency policy of a property.
int uniquePropertyId()
returns a unique property ID starting called usually with a base of KGamePropertyBase::IdAutomatic.
Base class for a game player.
Definition: kplayer.h:69
bool sendProperty(QDataStream &s)
called by a property to send itself into the datastream.
KGamePropertyBase()
Creates a KGamePropertyBase object without an owner.
bool lock()
A locked property can only be changed by the player who has set the lock.
virtual void save(QDataStream &s)=0
Write the value into a stream.
bool addProperty(KGamePropertyBase *data, const QString &name=QString())
Adds a KGameProperty property to the handler.
The main KDE game object.
Definition: kgame.h:64
KGamePropertyHandler * dataHandler()
Definition: kplayer.cpp:249
int writeRawData(const char *s, int len)
virtual void command(QDataStream &stream, int msgid, bool isSender=false)
send a command to advanced properties like arrays
char * data()
int size() const const
KGamePropertyBase::PropertyPolicy policy()
Returns the default policy for this property handler.
int id() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Nov 30 2020 22:37:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.