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

Solid

devicemanager.cpp

Go to the documentation of this file.
00001 /*  This file is part of the KDE project
00002     Copyright (C) 2005-2007 Kevin Ottens <ervin@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License version 2 as published by the Free Software Foundation.
00007 
00008     This library is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011     Library General Public License for more details.
00012 
00013     You should have received a copy of the GNU Library General Public License
00014     along with this library; see the file COPYING.LIB.  If not, write to
00015     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016     Boston, MA 02110-1301, USA.
00017 
00018 */
00019 
00020 #include "devicenotifier.h"
00021 #include "devicemanager_p.h" //krazy:exclude=includes (devicenotifier.h is the header file for this class)
00022 
00023 #include "device.h"
00024 #include "device_p.h"
00025 #include "predicate.h"
00026 
00027 #include "ifaces/devicemanager.h"
00028 #include "ifaces/device.h"
00029 
00030 #include "soliddefs_p.h"
00031 
00032 SOLID_GLOBAL_STATIC(Solid::DeviceManagerPrivate, globalDeviceManager)
00033 
00034 Solid::DeviceManagerPrivate::DeviceManagerPrivate()
00035     : m_nullDevice(new DevicePrivate(QString()))
00036 {
00037     loadBackend();
00038 
00039     if (managerBackend()!=0) {
00040         connect(managerBackend(), SIGNAL(deviceAdded(QString)),
00041                 this, SLOT(_k_deviceAdded(QString)));
00042         connect(managerBackend(), SIGNAL(deviceRemoved(QString)),
00043                 this, SLOT(_k_deviceRemoved(QString)));
00044     }
00045 }
00046 
00047 Solid::DeviceManagerPrivate::~DeviceManagerPrivate()
00048 {
00049     foreach (DevicePrivate *dev, m_devicesMap) {
00050         delete dev;
00051     }
00052 
00053     m_devicesMap.clear();
00054 }
00055 
00056 QList<Solid::Device> Solid::Device::allDevices()
00057 {
00058     QList<Device> list;
00059     Ifaces::DeviceManager *backend
00060         = qobject_cast<Ifaces::DeviceManager *>(globalDeviceManager->managerBackend());
00061 
00062     if (backend == 0) return list;
00063 
00064     QStringList udis = backend->allDevices();
00065 
00066     foreach (const QString &udi, udis)
00067     {
00068         list.append(Device(udi));
00069     }
00070 
00071     return list;
00072 }
00073 
00074 QList<Solid::Device> Solid::Device::listFromQuery(const QString &predicate,
00075                                                   const QString &parentUdi)
00076 {
00077     Predicate p = Predicate::fromString(predicate);
00078 
00079     if (p.isValid())
00080     {
00081         return listFromQuery(p, parentUdi);
00082     }
00083     else
00084     {
00085         return QList<Device>();
00086     }
00087 }
00088 
00089 QList<Solid::Device> Solid::Device::listFromType(const DeviceInterface::Type &type,
00090                                                  const QString &parentUdi)
00091 {
00092     QList<Device> list;
00093 
00094     Ifaces::DeviceManager *backend
00095         = qobject_cast<Ifaces::DeviceManager *>(globalDeviceManager->managerBackend());
00096 
00097     if (backend == 0) return list;
00098 
00099     QStringList udis = backend->devicesFromQuery(parentUdi, type);
00100 
00101     foreach (const QString &udi, udis)
00102     {
00103         list.append(Device(udi));
00104     }
00105 
00106     return list;
00107 }
00108 
00109 QList<Solid::Device> Solid::Device::listFromQuery(const Predicate &predicate,
00110                                                   const QString &parentUdi)
00111 {
00112     QList<Device> list;
00113 
00114     Ifaces::DeviceManager *backend
00115         = qobject_cast<Ifaces::DeviceManager *>(globalDeviceManager->managerBackend());
00116 
00117     if (backend == 0) return list;
00118 
00119     QSet<QString> udis;
00120     if (predicate.isValid()) {
00121         QSet<DeviceInterface::Type> types = predicate.usedTypes();
00122 
00123         foreach (DeviceInterface::Type type, types) {
00124             udis+= QSet<QString>::fromList(backend->devicesFromQuery(parentUdi, type));
00125         }
00126     } else {
00127         udis+= QSet<QString>::fromList(backend->allDevices());
00128     }
00129 
00130     foreach (const QString &udi, udis)
00131     {
00132         Device dev(udi);
00133 
00134         bool matches = false;
00135 
00136         if(!predicate.isValid()) {
00137             matches = true;
00138         } else {
00139             matches = predicate.matches(dev);
00140         }
00141 
00142         if (matches)
00143         {
00144             list.append(dev);
00145         }
00146     }
00147 
00148     return list;
00149 }
00150 
00151 Solid::DeviceNotifier *Solid::DeviceNotifier::instance()
00152 {
00153     return globalDeviceManager;
00154 }
00155 
00156 void Solid::DeviceManagerPrivate::_k_deviceAdded(const QString &udi)
00157 {
00158     if (m_devicesMap.contains(udi)) {
00159         DevicePrivate *dev = m_devicesMap[udi];
00160 
00161         // Ok, this one was requested somewhere was invalid
00162         // and now becomes magically valid!
00163 
00164         Q_ASSERT(dev->backendObject()==0);
00165         dev->setBackendObject(createBackendObject(udi));
00166         Q_ASSERT(dev->backendObject()!=0);
00167     }
00168 
00169     emit deviceAdded(udi);
00170 }
00171 
00172 void Solid::DeviceManagerPrivate::_k_deviceRemoved(const QString &udi)
00173 {
00174     if (m_devicesMap.contains(udi)) {
00175         DevicePrivate *dev = m_devicesMap[udi];
00176 
00177         // Ok, this one was requested somewhere was valid
00178         // and now becomes magically invalid!
00179 
00180         Q_ASSERT(dev->backendObject()!=0);
00181         delete dev->backendObject();
00182         Q_ASSERT(dev->backendObject()==0);
00183     }
00184 
00185     emit deviceRemoved(udi);
00186 }
00187 
00188 void Solid::DeviceManagerPrivate::_k_destroyed(QObject *object)
00189 {
00190     QString udi = m_reverseMap.take(object);
00191 
00192     if (!udi.isEmpty()) {
00193         m_devicesMap.remove(udi);
00194     }
00195 }
00196 
00197 Solid::DevicePrivate *Solid::DeviceManagerPrivate::findRegisteredDevice(const QString &udi)
00198 {
00199     if (udi.isEmpty()) {
00200         return m_nullDevice.data();
00201     } else if (m_devicesMap.contains(udi)) {
00202         return m_devicesMap[udi];
00203     } else {
00204         Ifaces::Device *iface = createBackendObject(udi);
00205 
00206         DevicePrivate *devData = new DevicePrivate(udi);
00207         devData->setBackendObject(iface);
00208 
00209         m_devicesMap[udi] = devData;
00210         m_reverseMap[devData] = udi;
00211 
00212         connect(devData, SIGNAL(destroyed(QObject *)),
00213                 this, SLOT(_k_destroyed(QObject *)));
00214 
00215         return devData;
00216     }
00217 }
00218 
00219 Solid::Ifaces::Device *Solid::DeviceManagerPrivate::createBackendObject(const QString &udi)
00220 {
00221     Ifaces::DeviceManager *backend = qobject_cast<Ifaces::DeviceManager *>(managerBackend());
00222     Ifaces::Device *iface = 0;
00223 
00224     if (backend!= 0) {
00225         QObject *object = backend->createDevice(udi);
00226         iface = qobject_cast<Ifaces::Device *>(object);
00227 
00228         if (iface==0) {
00229             delete object;
00230         }
00231     }
00232 
00233     return iface;
00234 }
00235 
00236 #include "devicenotifier.moc"
00237 #include "devicemanager_p.moc"
00238 

Solid

Skip menu "Solid"
  • 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
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs 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