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 "kgamepropertyhandler.h"
13 #include "kgamemessage.h"
14 #include "kplayer.h"
15 #include "kgame.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 { return registerData(id, owner->dataHandler(), name); }
68 
69 int KGamePropertyBase::registerData(int id, KPlayer* owner, const QString& name)
70 { return registerData(id, owner->dataHandler(), name); }
71 
73 { return registerData(-1, owner,p, name); }
74 
76 { return registerData(id, owner,PolicyUndefined, name); }
77 
79 {
80 // we don't support changing the id
81  if (!owner) {
82  qCWarning(GAMES_PRIVATE_KGAME) << "Resetting owner=0. Sure you want to do this?";
83  mOwner=nullptr;
84  return -1;
85  }
86  if (!mOwner) {
87  if (id==-1) {
88  id=owner->uniquePropertyId();
89  }
90  mId = id;
91  mOwner = owner;
92  mOwner->addProperty(this, name);
93  if (p!=PolicyUndefined) {
94  setPolicy(p);
95  } else {
96  setPolicy(mOwner->policy());
97  }
98  }
99  return mId;
100 }
101 
102 void KGamePropertyBase::unregisterData()
103 {
104  if (!mOwner) {
105  return;
106  }
107  mOwner->removeProperty(this);
108  mOwner = nullptr;
109 }
110 
112 {
113  QByteArray b;
115  KGameMessage::createPropertyHeader(s, id());
116  save(s);
117  if (mOwner) {
118  return mOwner->sendProperty(s);
119  } else {
120  qCCritical(GAMES_PRIVATE_KGAME) << "Cannot send because there is no receiver defined";
121  return false;
122  }
123 }
124 
126 {
127  QByteArray b;
129  KGameMessage::createPropertyHeader(s, id());
130  s.writeRawData(data.data(), data.size());
131  if (mOwner) {
132  return mOwner->sendProperty(s);
133  } else {
134  qCCritical(GAMES_PRIVATE_KGAME) << ": Cannot send because there is no receiver defined";
135  return false;
136  }
137 }
138 
140 {
141  if (isLocked()) {
142  return false;
143  }
144  setLock(true);
145  return true;
146 }
147 
149 {
150  if (isLocked() && !force) {
151  return false;
152  }
153  setLock(false);
154  return true;
155 }
156 
158 {
159  QByteArray b;
161  KGameMessage::createPropertyCommand(s, IdCommand, id(), CmdLock);
162 
163  s << (qint8)l;
164  if (mOwner) {
165  mOwner->sendProperty(s);
166  } else {
167  qCCritical(GAMES_PRIVATE_KGAME) << ": Cannot send because there is no receiver defined";
168  return ;
169  }
170 }
171 
173 {
174  //qCDebug(GAMES_PRIVATE_KGAME) << ": mOwnerP="<< mOwner << "id=" << id();
175  if (mOwner ) {
176  mOwner->emitSignal(this);
177  } else {
178  qCCritical(GAMES_PRIVATE_KGAME) << ":id="<<id()<<" Cannot emitSignal because there is no handler set";
179  }
180 }
181 
182 void KGamePropertyBase::command(QDataStream& s, int cmd, bool isSender)
183 {
184  switch (cmd) {
185  case CmdLock:
186  {
187  if (!isSender) {
188  qint8 locked;
189  s >> locked;
190  mFlags.bits.locked = (bool)locked ;
191  break;
192  }
193  }
194  default: // probably in derived classes
195  break;
196  }
197 }
198 
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:700
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:60
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:57
KGamePropertyHandler * dataHandler()
Definition: kplayer.cpp:238
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-2021 The KDE developers.
Generated on Tue Dec 7 2021 22:34:14 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.