• 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
kopetepropertycontainer.cpp
Go to the documentation of this file.
1 /*
2  kopetepropertycontainer.cpp - Kopete Property Container
3 
4  Copyright (c) 2007 by Gustavo Pichorim Boiko <gustavo.boiko@kemail.net>
5  Copyright (c) 2002-2004 by Duncan Mac-Vicar Prett <duncan@kde.org>
6  Copyright (c) 2002-2003 by Martijn Klingens <klingens@kde.org>
7  Copyright (c) 2002-2004 by Olivier Goffart <ogoffart @tiscalinet.be>
8 
9  Kopete (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
10 
11  *************************************************************************
12  * *
13  * This library is free software; you can redistribute it and/or *
14  * modify it under the terms of the GNU Lesser General Public *
15  * License as published by the Free Software Foundation; either *
16  * version 2 of the License, or (at your option) any later version. *
17  * *
18  *************************************************************************
19 */
20 
21 #include "kopetepropertycontainer.h"
22 
23 #include <QStringList>
24 #include <KDebug>
25 
26 #include <kdeversion.h>
27 
28 namespace Kopete {
29 
30 class PropertyContainer::Private
31 {
32 public:
33  Kopete::Property::Map properties;
34 };
35 
36 PropertyContainer::PropertyContainer(QObject *parent)
37 : QObject(parent), d(new Private())
38 {
39 }
40 
41 PropertyContainer::~PropertyContainer()
42 {
43  delete d;
44 }
45 
46 void PropertyContainer::serializeProperties(QMap<QString, QString> &serializedData) const
47 {
48 
49  Kopete::Property::Map::ConstIterator it;// = d->properties.ConstIterator;
50  for (it=d->properties.constBegin(); it != d->properties.constEnd(); ++it)
51  {
52  if (!it.value().tmpl().persistent())
53  continue;
54 
55  QVariant val = it.value().value();
56  QString key = QString::fromLatin1("prop_%1_%2").arg(QString::fromLatin1(val.typeName()), it.key());
57 
58  serializedData[key] = val.toString();
59 
60  } // end for()
61 } // end serializeProperties()
62 
63 void PropertyContainer::deserializeProperties( const QMap<QString, QString> &serializedData )
64 {
65  QMap<QString, QString>::ConstIterator it;
66  for ( it=serializedData.constBegin(); it != serializedData.constEnd(); ++it )
67  {
68  QString key = it.key();
69 
70  if ( !key.startsWith( QString::fromLatin1("prop_") ) ) // avoid parsing other serialized data
71  continue;
72 
73  QStringList keyList = key.split( QChar('_'), QString::SkipEmptyParts );
74  if( keyList.count() < 3 ) // invalid key, not enough parts in string "prop_X_Y"
75  continue;
76 
77  key = keyList[2]; // overwrite key var with the real key name this property has
78  QString type( keyList[1] ); // needed for QVariant casting
79 
80  QVariant variant( it.value() );
81  if( !variant.convert(QVariant::nameToType(type.toLatin1())) )
82  {
83  kDebug(14010) <<
84  "Casting QVariant to needed type FAILED" <<
85  "key=" << key << ", type=" << type << endl;
86  continue;
87  }
88 
89  Kopete::PropertyTmpl tmpl = Kopete::Global::Properties::self()->tmpl(key);
90  if( tmpl.isNull() )
91  {
92  kDebug( 14010 ) << "no PropertyTmpl defined for" \
93  " key " << key << ", cannot restore persistent property" << endl;
94  continue;
95  }
96 
97  setProperty(tmpl, variant);
98  } // end for()
99 }
100 
101 QStringList PropertyContainer::properties() const
102 {
103  return d->properties.keys();
104 }
105 
106 bool PropertyContainer::hasProperty(const QString &key) const
107 {
108  return d->properties.contains(key);
109 }
110 
111 const Property &PropertyContainer::property(const QString &key) const
112 {
113  if(hasProperty(key))
114  return d->properties[key];
115  else
116  return Kopete::Property::null;
117 }
118 
119 const Kopete::Property &PropertyContainer::property(
120  const Kopete::PropertyTmpl &tmpl) const
121 {
122  if(hasProperty(tmpl.key()))
123  return d->properties[tmpl.key()];
124  else
125  return Kopete::Property::null;
126 }
127 
128 
129 void PropertyContainer::setProperty(const Kopete::PropertyTmpl &tmpl,
130  const QVariant &value)
131 {
132  if(tmpl.isNull() || tmpl.key().isEmpty())
133  {
134  kDebug(14000) <<
135  "No valid template for property passed!" << endl;
136  return;
137  }
138 
139  if(value.isNull() || (value.canConvert(QVariant::String) && value.toString().isEmpty()))
140  {
141  removeProperty(tmpl);
142  }
143  else
144  {
145  QVariant oldValue = property(tmpl.key()).value();
146 
147  if(oldValue != value)
148  {
149  Kopete::Property prop(tmpl, value);
150  d->properties.remove(tmpl.key());
151  d->properties.insert(tmpl.key(), prop);
152 
153  emit propertyChanged(this, tmpl.key(), oldValue, value);
154  }
155  }
156 }
157 
158 void PropertyContainer::removeProperty(const Kopete::PropertyTmpl &tmpl)
159 {
160  if(!tmpl.isNull() && !tmpl.key().isEmpty() && hasProperty(tmpl.key()))
161  {
162  QVariant oldValue = property(tmpl.key()).value();
163  d->properties.remove(tmpl.key());
164  emit propertyChanged(this, tmpl.key(), oldValue, QVariant());
165  }
166 }
167 
168 } //END namespace Kopete
169 
170 #include "kopetepropertycontainer.moc"
171 
QVariant::canConvert
bool canConvert(Type t) const
Kopete::PropertyContainer::propertyChanged
void propertyChanged(Kopete::PropertyContainer *container, const QString &key, const QVariant &oldValue, const QVariant &newValue)
Kopete::Global::Properties::self
static Properties * self()
Singleton accessor for this class.
Definition: kopeteglobal.cpp:49
QChar
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QMap::constBegin
const_iterator constBegin() const
QMap< QString, Property >
Kopete::PropertyContainer::property
const Kopete::Property & property(const QString &key) const
Get the value of a property with key "key".
Definition: kopetepropertycontainer.cpp:111
QVariant::value
T value() const
kopetepropertycontainer.h
Kopete::Global::Properties::tmpl
const PropertyTmpl & tmpl(const QString &key) const
Return a template with defined by key, if no such template has been registered PropertyTmpl::null wil...
Definition: kopeteglobal.cpp:89
Kopete::PropertyTmpl
Definition: kopeteproperty.h:41
QList::count
int count(const T &value) const
QVariant::isNull
bool isNull() const
QObject
QVariant::nameToType
Type nameToType(const char *name)
QString::isEmpty
bool isEmpty() const
QMap::constEnd
const_iterator constEnd() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
Kopete::PropertyContainer::hasProperty
bool hasProperty(const QString &key) const
Check for existence of a certain property stored using "key".
Definition: kopetepropertycontainer.cpp:106
QString
Kopete::PropertyContainer::deserializeProperties
void deserializeProperties(const QMap< QString, QString > &serializedData)
Deserialize the contacts persistent properties.
Definition: kopetepropertycontainer.cpp:63
Kopete::PropertyTmpl::isNull
bool isNull() const
Returns true if this object is an empty template.
Definition: kopeteproperty.cpp:172
QStringList
Kopete::PropertyContainer::serializeProperties
void serializeProperties(QMap< QString, QString > &serializedData) const
Serialize the persistent properties for storage in the contact list.
Definition: kopetepropertycontainer.cpp:46
Kopete::PropertyContainer::removeProperty
void removeProperty(const Kopete::PropertyTmpl &tmpl)
Remove a property if it exists.
Definition: kopetepropertycontainer.cpp:158
Kopete::PropertyContainer::properties
QStringList properties() const
Definition: kopetepropertycontainer.cpp:101
QMap::key
const Key key(const T &value) const
Kopete::PropertyContainer::PropertyContainer
PropertyContainer(QObject *parent=0)
A container for properties.
Definition: kopetepropertycontainer.cpp:36
QString::toLatin1
QByteArray toLatin1() const
Kopete::Property
Definition: kopeteproperty.h:150
Kopete::Property::null
static Property null
The null, i.e.
Definition: kopeteproperty.h:189
QVariant::typeName
const char * typeName() const
Kopete::PropertyContainer::setProperty
void setProperty(const Kopete::PropertyTmpl &tmpl, const QVariant &value)
Add or Set a property for this contact.
Definition: kopetepropertycontainer.cpp:129
QString::fromLatin1
QString fromLatin1(const char *str, int size)
Kopete::PropertyContainer::~PropertyContainer
virtual ~PropertyContainer()
Definition: kopetepropertycontainer.cpp:41
Kopete::PropertyTmpl::key
const QString & key() const
Getter for the unique key.
Definition: kopeteproperty.cpp:137
QMap< QString, Property >::ConstIterator
typedef ConstIterator
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QVariant::toString
QString toString() const
QMap::value
const T value(const Key &key) const
QVariant
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