• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Plasma

datacontainer.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "datacontainer.h"
00021 #include "private/datacontainer_p.h"
00022 
00023 #include <QVariant>
00024 
00025 #include <kdebug.h>
00026 
00027 #include "plasma.h"
00028 
00029 namespace Plasma
00030 {
00031 
00032 DataContainer::DataContainer(QObject *parent)
00033     : QObject(parent),
00034       d(new DataContainerPrivate)
00035 {
00036 }
00037 
00038 DataContainer::~DataContainer()
00039 {
00040     delete d;
00041 }
00042 
00043 const DataEngine::Data DataContainer::data() const
00044 {
00045     return d->data;
00046 }
00047 
00048 void DataContainer::setData(const QString &key, const QVariant &value)
00049 {
00050     if (value.isNull() || !value.isValid()) {
00051         d->data.remove(key);
00052     } else {
00053         d->data[key] = value;
00054     }
00055 
00056     d->dirty = true;
00057     d->updateTs.start();
00058 }
00059 
00060 void DataContainer::removeAllData()
00061 {
00062     if (d->data.isEmpty()) {
00063         // avoid an update if we don't have any data anyways
00064         return;
00065     }
00066 
00067     d->data.clear();
00068     d->dirty = true;
00069     d->updateTs.start();
00070 }
00071 
00072 bool DataContainer::visualizationIsConnected(QObject *visualization) const
00073 {
00074     return d->relayObjects.contains(visualization);
00075 }
00076 
00077 void DataContainer::connectVisualization(QObject *visualization, uint pollingInterval,
00078                                          Plasma::IntervalAlignment alignment)
00079 {
00080     //kDebug() << "connecting visualization" << visualization << "at interval of"
00081     //         << pollingInterval << "to" << objectName();
00082     QMap<QObject *, SignalRelay *>::iterator objIt = d->relayObjects.find(visualization);
00083     bool connected = objIt != d->relayObjects.end();
00084     if (connected) {
00085         // this visualization is already connected. just adjust the update
00086         // frequency if necessary
00087         SignalRelay *relay = objIt.value();
00088         if (relay) {
00089             // connected to a relay
00090             //kDebug() << "     already connected, but to a relay";
00091             if (relay->m_interval == pollingInterval) {
00092                 //kDebug() << "    already connected to a relay of the same interval of"
00093                 //          << pollingInterval << ", nothing to do";
00094                 return;
00095             }
00096 
00097             if (relay->receiverCount() == 1) {
00098                 //kDebug() << "    removing relay, as it is now unused";
00099                 d->relays.remove(relay->m_interval);
00100                 delete relay;
00101             } else {
00102                 disconnect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
00103                            visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
00104                 //relay->isUnused();
00105             }
00106         } else if (pollingInterval < 1) {
00107             // the visualization was connected already, but not to a relay
00108             // and it still doesn't want to connect to a relay, so we have
00109             // nothing to do!
00110             //kDebug() << "     already connected, nothing to do";
00111             return;
00112         } else {
00113             disconnect(this, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
00114                        visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
00115         }
00116     } else {
00117         connect(visualization, SIGNAL(destroyed(QObject*)),
00118                 this, SLOT(disconnectVisualization(QObject*)));//, Qt::QueuedConnection);
00119     }
00120 
00121     if (pollingInterval < 1) {
00122         //kDebug() << "    connecting directly";
00123         d->relayObjects[visualization] = 0;
00124         connect(this, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
00125                 visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
00126     } else {
00127         //kDebug() << "    connecting to a relay";
00128         // we only want to do an imediate update if this is not the first object to connect to us
00129         // if it is the first visualization, then the source will already have been populated
00130         // engine's sourceRequested method
00131         bool immediateUpdate = connected || d->relayObjects.count() > 1;
00132         SignalRelay *relay = d->signalRelay(this, visualization, pollingInterval,
00133                                             alignment, immediateUpdate);
00134         connect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
00135                 visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
00136     }
00137 }
00138 
00139 void DataContainer::disconnectVisualization(QObject *visualization)
00140 {
00141     QMap<QObject *, SignalRelay *>::iterator objIt = d->relayObjects.find(visualization);
00142 
00143     if (objIt == d->relayObjects.end() || !objIt.value()) {
00144         // it is connected directly to the DataContainer itself
00145         disconnect(this, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
00146                    visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
00147     } else {
00148         SignalRelay *relay = objIt.value();
00149 
00150         if (relay->receiverCount() == 1) {
00151             d->relays.remove(relay->m_interval);
00152             delete relay;
00153         } else {
00154             disconnect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
00155                        visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
00156         }
00157     }
00158 
00159     d->relayObjects.erase(objIt);
00160     checkUsage();
00161 }
00162 
00163 void DataContainer::checkForUpdate()
00164 {
00165     //kDebug() << objectName() << d->dirty;
00166     if (d->dirty) {
00167         emit dataUpdated(objectName(), d->data);
00168 
00169         foreach (SignalRelay *relay, d->relays) {
00170             relay->checkQueueing();
00171         }
00172 
00173         d->dirty = false;
00174     }
00175 }
00176 
00177 uint DataContainer::timeSinceLastUpdate() const
00178 {
00179     //FIXME: we still assume it's been <24h
00180     //and ignore possible daylight savings changes
00181     return d->updateTs.elapsed();
00182 }
00183 
00184 void DataContainer::setNeedsUpdate(bool update)
00185 {
00186     d->cached = update;
00187 }
00188 
00189 void DataContainer::checkUsage()
00190 {
00191     if (d->relays.count() < 1 &&
00192         receivers(SIGNAL(dataUpdated(QString, Plasma::DataEngine::Data))) < 1) {
00193         // DO NOT CALL ANYTHING AFTER THIS LINE AS IT MAY GET DELETED!
00194         emit becameUnused(objectName());
00195     }
00196 }
00197 
00198 } // Plasma namespace
00199 
00200 #include "datacontainer.moc"
00201 

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal