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

libplasma

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 "datacontainer_p.h"
00022 
00023 #include <QVariant>
00024 
00025 #include <KDebug>
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.count() < 1) {
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, Plasma::IntervalAlignment alignment)
00078 {
00079     //kDebug() << "connecting visualization" << visualization << "at interval of"
00080     //         << pollingInterval << "to" << objectName();
00081     QMap<QObject *, SignalRelay *>::iterator objIt = d->relayObjects.find(visualization);
00082     bool connected = objIt != d->relayObjects.end();
00083     if (connected) {
00084         // this visualization is already connected. just adjust the update
00085         // frequency if necessary
00086         SignalRelay *relay = objIt.value();
00087         if (relay) {
00088             // connected to a relay
00089             //kDebug() << "     already connected, but to a relay";
00090             if (relay->m_interval == pollingInterval) {
00091                 //kDebug() << "    already connected to a relay of the same interval of"
00092                 //          << pollingInterval << ", nothing to do";
00093                 return;
00094             }
00095 
00096             if (relay->receiverCount() == 1) {
00097                 //kDebug() << "    removing relay, as it is now unused";
00098                 d->relays.remove(relay->m_interval);
00099                 delete relay;
00100             } else {
00101                 disconnect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
00102                            visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
00103                 //relay->isUnused();
00104             }
00105         } else if (pollingInterval < 1) {
00106             // the visualization was connected already, but not to a relay
00107             // and it still doesn't want to connect to a relay, so we have
00108             // nothing to do!
00109             //kDebug() << "     already connected, nothing to do";
00110             return;
00111         } else {
00112             disconnect(this, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
00113                        visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
00114         }
00115     } else {
00116         connect(visualization, SIGNAL(destroyed(QObject*)),
00117                 this, SLOT(disconnectVisualization(QObject*)));//, Qt::QueuedConnection);
00118     }
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         SignalRelay *relay = d->signalRelay(this, visualization, pollingInterval, alignment);
00129         connect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
00130                 visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
00131     }
00132 }
00133 
00134 void DataContainer::disconnectVisualization(QObject* visualization)
00135 {
00136     QMap<QObject *, SignalRelay *>::iterator objIt = d->relayObjects.find(visualization);
00137 
00138     if (objIt == d->relayObjects.end() || !objIt.value()) {
00139         // it is connected directly to the DataContainer itself
00140         disconnect(this, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
00141                    visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
00142     } else {
00143         SignalRelay *relay = objIt.value();
00144 
00145         if (relay->receiverCount() == 1) {
00146             d->relays.remove(relay->m_interval);
00147             delete relay;
00148         } else {
00149             disconnect(relay, SIGNAL(dataUpdated(QString,Plasma::DataEngine::Data)),
00150                        visualization, SLOT(dataUpdated(QString,Plasma::DataEngine::Data)));
00151         }
00152     }
00153 
00154     d->relayObjects.erase(objIt);
00155     checkUsage();
00156 }
00157 
00158 void DataContainer::checkForUpdate()
00159 {
00160     if (d->dirty) {
00161         emit dataUpdated(objectName(), d->data);
00162 
00163         foreach (SignalRelay* relay, d->relays) {
00164             relay->checkQueueing();
00165         }
00166 
00167         d->dirty = false;
00168     }
00169 }
00170 
00171 uint DataContainer::timeSinceLastUpdate() const
00172 {
00173     //FIXME: we still assume it's been <24h
00174     //and ignore possible daylight savings changes
00175     return d->updateTs.elapsed();
00176 }
00177 
00178 void DataContainer::setNeedsUpdate(bool update)
00179 {
00180     d->cached = update;
00181 }
00182 
00183 void DataContainer::checkUsage()
00184 {
00185     if (d->relays.count() < 1 &&
00186         receivers(SIGNAL(dataUpdated(QString, Plasma::DataEngine::Data))) < 1) {
00187         // DO NOT CALL ANYTHING AFTER THIS LINE AS IT MAY GET DELETED!
00188         emit becameUnused(objectName());
00189     }
00190 }
00191 
00192 } // Plasma namespace
00193 
00194 #include "datacontainer.moc"
00195 #include "datacontainer_p.moc"
00196 

libplasma

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

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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