dtor first
[personal-kdebase.git] / workspace / powerdevil / daemon / PollSystemLoader.cpp
blobdf2e237f5a4cbf4e12bbe1ce5dad43f085299969
1 /***************************************************************************
2 * Copyright (C) 2008 by Dario Freddi <drf@kdemod.ath.cx> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 **************************************************************************/
20 #include "PollSystemLoader.h"
22 #include "WidgetBasedPoller.h"
23 #include "XSyncBasedPoller.h"
24 #include "TimerBasedPoller.h"
26 PollSystemLoader::PollSystemLoader(QObject *parent)
27 : QObject(parent),
28 m_poller(0)
30 reloadAvailableCache();
33 PollSystemLoader::~PollSystemLoader()
35 unloadCurrentSystem();
38 void PollSystemLoader::reloadAvailableCache()
40 m_availableSystems.clear();
42 // Test each polling system
43 WidgetBasedPoller *wpl = new WidgetBasedPoller(this);
44 XSyncBasedPoller *xpl = XSyncBasedPoller::instance();
45 TimerBasedPoller *tpl = new TimerBasedPoller(this);
47 if (wpl->isAvailable()) {
48 m_availableSystems[AbstractSystemPoller::WidgetBased] = wpl->name();
50 if (xpl->isAvailable()) {
51 m_availableSystems[AbstractSystemPoller::XSyncBased] = xpl->name();
53 if (tpl->isAvailable()) {
54 m_availableSystems[AbstractSystemPoller::TimerBased] = tpl->name();
57 wpl->deleteLater();
58 tpl->deleteLater();
61 QMap<AbstractSystemPoller::PollingType, QString> PollSystemLoader::getAvailableSystems()
63 // Cached
64 return m_availableSystems;
67 QMap<int, QString> PollSystemLoader::getAvailableSystemsAsInt()
69 // Cached
71 QMap<int, QString> retlist;
73 foreach(const AbstractSystemPoller::PollingType &ent, m_availableSystems.keys()) {
74 retlist[(int) ent] = m_availableSystems[ent];
77 return retlist;
80 bool PollSystemLoader::loadSystem(AbstractSystemPoller::PollingType type)
82 if (m_poller) {
83 if (m_poller->getPollingType() == type) {
84 return true;
85 } else {
86 unloadCurrentSystem();
90 switch (type) {
91 case AbstractSystemPoller::WidgetBased:
92 m_poller = new WidgetBasedPoller(this);
93 break;
94 case AbstractSystemPoller::TimerBased:
95 m_poller = new TimerBasedPoller(this);
96 break;
97 case AbstractSystemPoller::XSyncBased:
98 m_poller = XSyncBasedPoller::instance();
99 break;
100 default:
101 return false;
102 break;
105 if (!m_poller->isAvailable()) {
106 return false;
109 if (!m_poller->setUpPoller()) {
110 return false;
113 return true;
116 bool PollSystemLoader::unloadCurrentSystem()
118 if (m_poller) {
119 m_poller->unloadPoller();
121 if (m_poller->getPollingType() != AbstractSystemPoller::XSyncBased) {
122 m_poller->deleteLater();
126 return true;
129 #include "PollSystemLoader.moc"