• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kopete/libkopete

  • sources
  • kde-4.14
  • kdenetwork
  • kopete
  • libkopete
kopeteproperties.h
Go to the documentation of this file.
1 /*
2  kopeteproperties.h - Kopete Properties
3 
4  Copyright (c) 2004 by Richard Smith <kde@metafoo.co.uk>
5  Kopete (c) 2002-2004 by the Kopete developers <kopete-devel@kde.org>
6 
7  *************************************************************************
8  * *
9  * This library is free software; you can redistribute it and/or *
10  * modify it under the terms of the GNU Lesser General Public *
11  * License as published by the Free Software Foundation; either *
12  * version 2 of the License, or (at your option) any later version. *
13  * *
14  *************************************************************************
15 */
16 
17 #ifndef KOPETEPROPERTIES_H
18 #define KOPETEPROPERTIES_H
19 
20 #include <QMultiHash>
21 #include <QVariant>
22 
23 #include <typeinfo>
24 
25 class QString;
26 class QDomElement;
27 
28 namespace Kopete
29 {
30 
38 namespace Properties
39 {
40 
41 //BEGIN core functionality
42 
54 template<class Parent>
55 class PropertyBase
56 {
57 public:
69  virtual const QByteArray &name() const = 0;
70 };
71 
87 template<class Parent, typename Type>
88 class Property : public PropertyBase<Parent>
89 {
90 public:
94  virtual Type get( const Parent *parent ) const = 0;
98  virtual void set( Parent *, const Type & ) const = 0;
99 };
100 
110 struct PropertyData
111 {
112  virtual ~PropertyData() {}
113 };
114 
122 class PropertyStorage
123 {
124  typedef QMultiHash<QByteArray, PropertyData*> PropertyDict;
125  // setCustomPropertyData can be called on a const object, allowing the
126  // guarantee that DataProperty::data() never returns 0.
127  mutable PropertyDict _storage;
128 
129 public:
130  PropertyStorage() {}
131  ~PropertyStorage()
132  {
133  qDeleteAll(_storage);
134  }
135 
144  void setCustomPropertyData( const QByteArray &name, PropertyData *data ) const { _storage.replace( name, data ); }
145 
150  PropertyData *getCustomPropertyData( const QByteArray &name ) const { return _storage.value(name); }
151 };
152 
166 template<class Parent>
167 class WithProperties : public PropertyStorage
168 {
169 public:
174  template<typename T>
175  T property( Property<Parent,T> const &prop ) { return prop.get( static_cast<Parent*>(this) ); }
181  template<typename T>
182  void setProperty( Property<Parent,T> const &prop, const T &value ) { prop.set( static_cast<Parent*>(this), value ); }
183 
190  static void propertyCreated( const PropertyBase<Parent> &property );
191 };
192 
193 //END core functionality
194 
195 //BEGIN interfaces
196 
201 template<class Parent>
202 struct UserVisible
203 {
204  virtual QString userText( Parent * ) = 0;
205  virtual QString label() = 0;
206  virtual QString icon() = 0;
207 };
208 
213 template<class Parent>
214 struct XMLSerializable
215 {
216  virtual void fromXML( Parent *, const QDomElement & ) = 0;
217  virtual void toXML( const Parent *, QDomElement & ) = 0;
218 };
219 
224 template<class Parent>
225 struct StringSerializable
226 {
227  virtual void fromString( Parent *, const QString & ) = 0;
228  virtual QString toString( const Parent * ) = 0;
229 };
230 
231 //END interfaces
232 
233 //BEGIN convenience classes
234 
238 void customPropertyDataIncorrectType( const char *name, const std::type_info &found, const std::type_info &expected );
239 
246 template<class Parent, typename Type, class Data>
247 class DataProperty : public Property<Parent,Type>
248 {
249 public:
250  Data *data( const Parent *c ) const
251  {
252  PropertyData *pd = c->getCustomPropertyData( this->name() );
253  Data *data = dynamic_cast<Data*>(pd);
254  if ( !data )
255  {
256  if ( pd )
257  customPropertyDataIncorrectType( this->name(), typeid(*pd), typeid(Data) );
258  data = new Data;
259  c->setCustomPropertyData( this->name(), data );
260  }
261  return data;
262  }
263 };
264 
273 template<typename T>
274 struct SimplePropertyData : public PropertyData
275 {
276  SimplePropertyData() : value() {}
277  T value;
278 };
279 
290 template<class Parent, typename Type>
291 class SimpleDataProperty : public DataProperty<Parent,Type,SimplePropertyData<Type> >
292 {
293 public:
294  Type get( const Parent *p ) const { return data(p)->value; }
295  void set( Parent *p, const Type &v ) const { data(p)->value = v; }
296 };
297 
306 template<class T> T variantTo(QVariant);
307 
308 QVariant variantFromXML(const QDomElement&);
309 void variantToXML(QVariant v, QDomElement &);
310 
334 template<class Derived, class Parent, typename Type>
335 class XMLProperty : public XMLSerializable<Parent>
336 {
337 public:
338  void fromXML( Parent *t, const QDomElement &e )
339  {
340  static_cast<Derived*>(this)->set(t, variantTo<Type>(variantFromXML(e)));
341  }
342  void toXML( const Parent *t, QDomElement &e )
343  {
344  variantToXML(QVariant(static_cast<Derived*>(this)->get(t)),e);
345  }
346 };
347 
348 //END convenience classes
349 
350 } // namespace Properties
351 
352 } // namespace Kopete
353 
354 #endif
Kopete::Properties::Property::set
virtual void set(Parent *, const Type &) const =0
Sets the value of this property in the object parent.
Kopete::Properties::WithProperties::setProperty
void setProperty(Property< Parent, T > const &prop, const T &value)
Set the value of property prop in this object.
Definition: kopeteproperties.h:182
Kopete::Properties::XMLSerializable::toXML
virtual void toXML(const Parent *, QDomElement &)=0
Kopete::Properties::PropertyStorage::setCustomPropertyData
void setCustomPropertyData(const QByteArray &name, PropertyData *data) const
Sets the stored property data with name name to be data.
Definition: kopeteproperties.h:144
Kopete::Properties::variantTo
T variantTo(QVariant)
Move somewhere else.
Kopete::Properties::variantFromXML
QVariant variantFromXML(const QDomElement &)
Definition: kopeteproperties.cpp:40
Kopete::Properties::WithProperties::propertyCreated
static void propertyCreated(const PropertyBase< Parent > &property)
Called when a property is created; loads the Parent object's data into the property.
QByteArray
Kopete::Properties::PropertyStorage
Storage object for PropertyData objects.
Definition: kopeteproperties.h:122
Kopete::Properties::DataProperty
Convenience implementation of a Property that stores PropertyData.
Definition: kopeteproperties.h:247
Kopete::Properties::StringSerializable::toString
virtual QString toString(const Parent *)=0
Kopete::Properties::UserVisible::label
virtual QString label()=0
QMultiHash::replace
QHash< Key, T >::iterator replace(const Key &key, const T &value)
Kopete::Properties::SimplePropertyData::value
T value
Definition: kopeteproperties.h:277
Kopete::Properties::PropertyStorage::getCustomPropertyData
PropertyData * getCustomPropertyData(const QByteArray &name) const
Gets the stored property data with name name.
Definition: kopeteproperties.h:150
Kopete::Properties::variantToXML
void variantToXML(QVariant v, QDomElement &)
Definition: kopeteproperties.cpp:45
Kopete::Properties::Property
Property-type-dependent base class for properties.
Definition: kopeteproperties.h:88
Kopete::Properties::StringSerializable
An interface for properties which can be serialized as strings.
Definition: kopeteproperties.h:225
Kopete::Properties::Property::get
virtual Type get(const Parent *parent) const =0
Returns the value of this property in the object parent.
Kopete::Properties::SimplePropertyData
Convenience implementation of a PropertyData subclass which stores a single datum.
Definition: kopeteproperties.h:274
Kopete::Properties::PropertyData::~PropertyData
virtual ~PropertyData()
Definition: kopeteproperties.h:112
Kopete::Properties::WithProperties::property
T property(Property< Parent, T > const &prop)
Get the value of property prop in this object.
Definition: kopeteproperties.h:175
Kopete::Properties::SimpleDataProperty
Convenience implementation of a Property which stores a single datum as PropertyData.
Definition: kopeteproperties.h:291
Kopete::Properties::StringSerializable::fromString
virtual void fromString(Parent *, const QString &)=0
QString
Kopete::Properties::WithProperties
Base class for classes to which properties can be applied.
Definition: kopeteproperties.h:167
Kopete::Properties::PropertyBase
Property-type-independent base class for properties.
Definition: kopeteproperties.h:55
Kopete::Properties::UserVisible::icon
virtual QString icon()=0
Kopete::Properties::customPropertyDataIncorrectType
void customPropertyDataIncorrectType(const char *name, const std::type_info &found, const std::type_info &expected)
Definition: kopeteproperties.cpp:30
Kopete::Properties::DataProperty::data
Data * data(const Parent *c) const
Definition: kopeteproperties.h:250
Kopete::Properties::PropertyData
Base class for property data objects.
Definition: kopeteproperties.h:110
Kopete::Properties::XMLProperty::toXML
void toXML(const Parent *t, QDomElement &e)
Definition: kopeteproperties.h:342
Kopete::Properties::UserVisible::userText
virtual QString userText(Parent *)=0
Kopete::Properties::UserVisible
An interface for user-visible properties.
Definition: kopeteproperties.h:202
Kopete::Properties::SimplePropertyData::SimplePropertyData
SimplePropertyData()
Definition: kopeteproperties.h:276
Kopete::Properties::XMLProperty
Convenience implementation of XMLSerializable in terms of QVariants.
Definition: kopeteproperties.h:335
QDomElement
Kopete::Properties::SimpleDataProperty::set
void set(Parent *p, const Type &v) const
Sets the value of this property in the object parent.
Definition: kopeteproperties.h:295
Kopete::Properties::XMLSerializable
An interface for properties which can be serialized as XML.
Definition: kopeteproperties.h:214
QMultiHash< QByteArray, PropertyData * >
Kopete::Properties::XMLProperty::fromXML
void fromXML(Parent *t, const QDomElement &e)
Definition: kopeteproperties.h:338
Kopete::Properties::PropertyStorage::~PropertyStorage
~PropertyStorage()
Definition: kopeteproperties.h:131
Kopete::Properties::PropertyBase::name
virtual const QByteArray & name() const =0
Returns the name of the property.
name
const char * name
Definition: kopeteonlinestatus.cpp:104
Kopete::Properties::PropertyStorage::PropertyStorage
PropertyStorage()
Definition: kopeteproperties.h:130
QVariant
Kopete::Properties::XMLSerializable::fromXML
virtual void fromXML(Parent *, const QDomElement &)=0
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/libkopete

Skip menu "kopete/libkopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal