KDEGames

kgameproperty.cpp
1 /*
2  This file is part of the KDE games library
3  SPDX-FileCopyrightText: 2001 Andreas Beckermann <[email protected]>
4  SPDX-FileCopyrightText: 2001 Martin Heni <kde at heni-online.de>
5 
6  SPDX-License-Identifier: LGPL-2.0-only
7 */
8 
9 #include "kgameproperty.h"
10 
11 // own
12 #include "kgame.h"
13 #include "kgamemessage.h"
14 #include "kgamepropertyhandler.h"
15 #include "kplayer.h"
16 
17 #define KPLAYERHANDLER_LOAD_COOKIE 6239
18 
20 {
21  init();
22  registerData(id, parent);
23 }
24 
26 {
27  init();
28  registerData(id, parent);
29 }
30 
32 {
33  init();
34  registerData(id, owner);
35 }
36 
38 {
39  init();
40 }
41 
42 KGamePropertyBase::~KGamePropertyBase()
43 {
44  unregisterData();
45 }
46 
47 void KGamePropertyBase::init()
48 {
49  mOwner = nullptr;
50  setDirty(false);
51 
52  // this is very useful and used by e.g. KGameDialog so
53  // it is activated by default. Big games may profit by deactivating it to get
54  // a better performance.
55  setEmittingSignal(true);
56 
57  setOptimized(false);
58 
59  // setReadOnly(false);
60  mFlags.bits.locked = false; // setLocked(false); is NOT possible as it checks whether isLocked() allows to change the status
61 
62  // local is default
63  setPolicy(PolicyLocal);
64 }
65 
66 int KGamePropertyBase::registerData(int id, KGame *owner, const QString &name)
67 {
68  return registerData(id, owner->dataHandler(), name);
69 }
70 
71 int KGamePropertyBase::registerData(int id, KPlayer *owner, const QString &name)
72 {
73  return registerData(id, owner->dataHandler(), name);
74 }
75 
77 {
78  return registerData(-1, owner, p, name);
79 }
80 
82 {
83  return registerData(id, owner, PolicyUndefined, name);
84 }
85 
87 {
88  // we don't support changing the id
89  if (!owner) {
90  qCWarning(GAMES_PRIVATE_KGAME) << "Resetting owner=0. Sure you want to do this?";
91  mOwner = nullptr;
92  return -1;
93  }
94  if (!mOwner) {
95  if (id == -1) {
96  id = owner->uniquePropertyId();
97  }
98  mId = id;
99  mOwner = owner;
100  mOwner->addProperty(this, name);
101  if (p != PolicyUndefined) {
102  setPolicy(p);
103  } else {
104  setPolicy(mOwner->policy());
105  }
106  }
107  return mId;
108 }
109 
110 void KGamePropertyBase::unregisterData()
111 {
112  if (!mOwner) {
113  return;
114  }
115  mOwner->removeProperty(this);
116  mOwner = nullptr;
117 }
118 
120 {
121  QByteArray b;
124  save(s);
125  if (mOwner) {
126  return mOwner->sendProperty(s);
127  } else {
128  qCCritical(GAMES_PRIVATE_KGAME) << "Cannot send because there is no receiver defined";
129  return false;
130  }
131 }
132 
134 {
135  QByteArray b;
138  s.writeRawData(data.data(), data.size());
139  if (mOwner) {
140  return mOwner->sendProperty(s);
141  } else {
142  qCCritical(GAMES_PRIVATE_KGAME) << ": Cannot send because there is no receiver defined";
143  return false;
144  }
145 }
146 
148 {
149  if (isLocked()) {
150  return false;
151  }
152  setLock(true);
153  return true;
154 }
155 
157 {
158  if (isLocked() && !force) {
159  return false;
160  }
161  setLock(false);
162  return true;
163 }
164 
166 {
167  QByteArray b;
169  KGameMessage::createPropertyCommand(s, IdCommand, id(), CmdLock);
170 
171  s << (qint8)l;
172  if (mOwner) {
173  mOwner->sendProperty(s);
174  } else {
175  qCCritical(GAMES_PRIVATE_KGAME) << ": Cannot send because there is no receiver defined";
176  return;
177  }
178 }
179 
181 {
182  // qCDebug(GAMES_PRIVATE_KGAME) << ": mOwnerP="<< mOwner << "id=" << id();
183  if (mOwner) {
184  mOwner->emitSignal(this);
185  } else {
186  qCCritical(GAMES_PRIVATE_KGAME) << ":id=" << id() << " Cannot emitSignal because there is no handler set";
187  }
188 }
189 
190 void KGamePropertyBase::command(QDataStream &s, int cmd, bool isSender)
191 {
192  switch (cmd) {
193  case CmdLock: {
194  if (!isSender) {
195  qint8 locked;
196  s >> locked;
197  mFlags.bits.locked = (bool)locked;
198  break;
199  }
200  }
201  default: // probably in derived classes
202  break;
203  }
204 }
static void createPropertyHeader(QDataStream &msg, int id)
Creates a property header given the property id.
int registerData(int id, KGamePropertyHandler *owner, PropertyPolicy p, const QString &name=QString())
You have to register a KGamePropertyBase before you can use it.
void emitSignal()
Causes the parent object to emit a signal on value change.
KGamePropertyHandler * dataHandler()
Definition: kplayer.cpp:238
void setLock(bool l)
A locked property can only be changed by the player who has set the lock.
The main KDE game object.
Definition: kgame.h:55
Base class for a game player.
Definition: kplayer.h:59
bool sendProperty()
Forward the data to the owner of this property which then sends it over network.
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.
PropertyPolicy
The policy of the property.
static void createPropertyCommand(QDataStream &msg, int cmdid, int pid, int cmd)
Creates a property header given the property id.
void emitSignal(KGamePropertyBase *data)
called by a property to emit a signal This call is simply forwarded to the parent object
bool isLocked() const
A locked property can only be changed by the player who has set the lock.
bool removeProperty(KGamePropertyBase *data)
Removes a property from the handler.
bool unlock(bool force=false)
A locked property can only be changed by the player who has set the lock.
bool addProperty(KGamePropertyBase *data, const QString &name=QString())
Adds a KGameProperty property to the handler.
int writeRawData(const char *s, int len)
A collection class for KGameProperty objects.
virtual void save(QDataStream &s)=0
Write the value into a stream.
int uniquePropertyId()
returns a unique property ID starting called usually with a base of KGamePropertyBase::IdAutomatic.
void setPolicy(PropertyPolicy p)
Changes the consistency policy of a property.
bool sendProperty(QDataStream &s)
called by a property to send itself into the datastream.
virtual void command(QDataStream &stream, int msgid, bool isSender=false)
send a command to advanced properties like arrays
KGamePropertyBase::PropertyPolicy policy()
Returns the default policy for this property handler.
KGamePropertyBase()
Creates a KGamePropertyBase object without an owner.
KGamePropertyHandler * dataHandler() const
Returns a pointer to the KGame property handler.
Definition: kgame.cpp:666
bool lock()
A locked property can only be changed by the player who has set the lock.
int size() const const
void setEmittingSignal(bool p)
Sets this property to emit a signal on value changed.
char * data()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 03:49:44 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.