not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / libs / solid / control / managerbase.cpp
blob4a6af269528dd2ba5a4da67e62a1e35c9169be56
1 /* This file is part of the KDE project
2 Copyright (C) 2006-2007 Kevin Ottens <ervin@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
20 #include "managerbase_p.h"
22 #include <kservicetypetrader.h>
23 #include <kservice.h>
24 #include <klibloader.h>
26 #include <klocale.h>
27 #include <kdebug.h>
29 static QMap<QString, QObject *> _k_preloadedBackends;
31 Solid::Control::ManagerBasePrivate::ManagerBasePrivate()
32 : m_backend(0)
36 Solid::Control::ManagerBasePrivate::~ManagerBasePrivate()
40 void Solid::Control::ManagerBasePrivate::loadBackend(const QString &description, const char *serviceName,
41 const char *backendClassName)
43 if (_k_preloadedBackends.contains(backendClassName)) {
44 m_backend = _k_preloadedBackends[backendClassName];
45 return;
48 QStringList error_msg;
50 KService::List offers = KServiceTypeTrader::self()->query(serviceName, "(Type == 'Service')");
52 foreach (const KService::Ptr &ptr, offers)
54 QString error_string;
55 m_backend = ptr->createInstance<QObject>(0, QVariantList(), &error_string);
57 if(m_backend!=0) {
58 if (m_backend->inherits(backendClassName)) {
59 kDebug() << "Backend loaded: " << ptr->name();
60 break;
61 } else {
62 QString error_string = i18n("Backend loaded but wrong type obtained, expected %1",
63 backendClassName);
65 kDebug() << "Error loading '" << ptr->name() << "': " << error_string;
66 error_msg.append(error_string);
68 delete m_backend;
69 m_backend = 0;
71 } else {
72 kDebug() << "Error loading '" << ptr->name() << "', KService said: " << error_string;
73 error_msg.append(error_string);
77 if (m_backend==0) {
78 if (offers.size() == 0)
80 m_errorText = i18n("No %1 Backend found", description);
82 else
84 m_errorText = "<qt>";
85 m_errorText+= i18n("Unable to use any of the %1 Backends", description);
86 m_errorText+= "<table>";
88 QString line = "<tr><td><b>%1</b></td><td>%2</td></tr>";
90 for (int i = 0; i< offers.size(); i++)
92 m_errorText+= line.arg(offers[i]->name()).arg(error_msg[i]);
95 m_errorText+= "</table></qt>";
100 QString Solid::Control::ManagerBasePrivate::errorText() const
102 return m_errorText;
105 QObject *Solid::Control::ManagerBasePrivate::managerBackend() const
107 return m_backend;
110 void Solid::Control::ManagerBasePrivate::_k_forcePreloadedBackend(const char *backendClassName, QObject *backend)
112 _k_preloadedBackends[backendClassName] = backend;