Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / kinfocenter / toplevel.cpp
blobb7f566c1f2b548e22290edb52f17fb931036a9ec
1 /*
2 Copyright (c) 1999 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
3 Copyright (c) 2000 Matthias Elter <elter@kde.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 MA 02110-1301, USA.
21 #include "toplevel.h"
22 #include "indexwidget.h"
23 #include "aboutwidget.h"
24 #include "proxywidget.h"
25 #include "moduletreeview.h"
26 #include "modules.h"
28 #include <kaboutapplicationdialog.h>
29 #include <kactioncollection.h>
30 #include <kbugreport.h>
31 #include <kaboutdata.h>
32 #include <kconfig.h>
33 #include <kcomponentdata.h>
34 #include <kicon.h>
35 #include <kiconloader.h>
36 #include <klocale.h>
37 #include <kmessagebox.h>
38 #include <kstandarddirs.h>
39 #include <kstandardaction.h>
40 #include <ktoggleaction.h>
41 #include <kwindowsystem.h>
42 #include <kxmlguifactory.h>
44 #include <kdebug.h>
46 #include <QTabWidget>
47 #include <QSplitter>
49 #include <stdio.h>
51 #include "toplevel.moc"
53 TopLevel::TopLevel() :
54 KXmlGuiWindow( 0, Qt::WindowContextHelpButtonHint), _active(NULL), dummyAbout(NULL) {
55 setCaption(QString());
57 report_bug = NULL;
59 // initialize the entries
60 _modules = new ConfigModuleList();
61 _modules->readDesktopEntries();
63 // create the layout box
64 _splitter = new QSplitter( Qt::Horizontal, this );
65 _splitter->setContentsMargins(0, 0, 0, 0);
67 // index tab
68 _indextab = new IndexWidget(_modules, this);
69 connect(_indextab, SIGNAL(moduleActivated(ConfigModule*)), this, SLOT(activateModule(ConfigModule*)));
70 connect(_indextab, SIGNAL(generalActivated()), this, SLOT(activateGeneral()));
71 _splitter->addWidget(_indextab);
73 _indextab->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred) );
75 // Restore sizes
76 KConfigGroup config = KConfigGroup(KGlobal::config(), "General");
77 QList<int> sizes = config.readEntry("SplitterSizes", QList<int>() );
78 if (!sizes.isEmpty())
79 _splitter->setSizes(sizes);
81 AboutWidget* aboutWidget = new AboutWidget( this, _modules);
82 connect(aboutWidget, SIGNAL( moduleSelected( ConfigModule * ) ), _indextab, SLOT(selectModule(ConfigModule*)));
84 // set up the right hand side (the docking area)
85 _dock = new DockContainer(aboutWidget, _splitter);
87 // That one does the trick ...
88 _splitter->setStretchFactor(_splitter->indexOf(_indextab), 0);
89 _splitter->setStretchFactor(_splitter->indexOf(_dock), 1);
91 connect(_indextab, SIGNAL(generalActivated()), _dock, SLOT(showAboutWidget()));
93 // set the main view
94 setCentralWidget(_splitter);
96 // initialize the GUI actions
97 setupActions();
99 KWindowSystem::setIcons(winId(), KIconLoader::global()->loadIcon(KINFOCENTER_ICON_NAME, KIconLoader::NoGroup, 32), KIconLoader::global()->loadIcon("hwinfo", KIconLoader::NoGroup, 16) );
101 _indextab->selectGeneral();
104 TopLevel::~TopLevel() {
106 KConfigGroup config = KConfigGroup(KGlobal::config(), "General");
107 config.writeEntry("SplitterSizes", _splitter->sizes());
109 config.sync();
111 delete _modules;
114 void TopLevel::setupActions() {
115 KStandardAction::quit(this, SLOT(close()), actionCollection());
116 KStandardAction::keyBindings(guiFactory(), SLOT(configureShortcuts()), actionCollection());
118 about_module = actionCollection()->addAction("help_about_module");
119 about_module->setText(i18n("About Current Module"));
120 about_module->setVisible(false);
121 connect(about_module, SIGNAL(triggered(bool) ), SLOT(aboutModule()));
123 createGUI("kinfocenterui.rc");
125 report_bug = actionCollection()->action("help_report_bug");
126 report_bug->setText(i18n("&Report Bug..."));
127 report_bug->disconnect();
128 connect(report_bug, SIGNAL(triggered()), SLOT(reportBug()));
131 void TopLevel::activateModule(ConfigModule *configModule) {
132 kDebug() << "Activating module..." << endl;
134 _active=configModule;
136 // dock it
137 if (!_dock->dockModule(configModule)) {
138 kDebug() << "Activating module by docking it." << endl;
140 return;
143 kDebug() << "Modifying About Module." << endl;
145 if (configModule->aboutData()) {
146 about_module->setText(i18nc("Help menu->about <modulename>", "About %1", handleAmpersand(configModule->moduleName())));
147 about_module->setIcon(configModule->realIcon(KIconLoader::SizeSmall));
148 about_module->setVisible(true);
149 } else {
150 about_module->setText(i18n("About Current Module"));
151 about_module->setIcon(KIcon());
152 about_module->setVisible(false);
155 setCaption(configModule->moduleName(), false);
157 if (configModule->moduleName().isEmpty())
158 report_bug->setText(i18n("&Report Bug..."));
159 else
160 report_bug->setText(i18n("Report Bug on Module %1...", handleAmpersand(configModule->moduleName())));
163 kDebug() << "Activating module done." << endl;
166 void TopLevel::deleteDummyAbout() {
167 delete dummyAbout;
168 dummyAbout = 0;
171 void TopLevel::reportBug() {
172 dummyAbout = NULL;
173 bool deleteit = false;
175 if (!_active) // report against kinfocenter
176 dummyAbout = const_cast<KAboutData*>(KGlobal::mainComponent().aboutData());
177 else {
178 if (_active->aboutData())
179 dummyAbout = const_cast<KAboutData*>(_active->aboutData());
180 else {
181 QString kcmLibrary = "kcm" + _active->library();
182 dummyAbout = new KAboutData(kcmLibrary.toLatin1(), 0, ki18n(_active->moduleName().toUtf8()), "2.0");
183 deleteit = true;
186 KBugReport *br = new KBugReport(this, false, dummyAbout);
187 if (deleteit)
188 connect(br, SIGNAL(finished()), SLOT(deleteDummyAbout()));
189 else
190 dummyAbout = NULL;
192 br->show();
195 void TopLevel::aboutModule() {
196 kDebug() << "About " << _active->moduleName() << endl;
198 KAboutApplicationDialog dlg(_active->aboutData());
199 dlg.exec();
202 QString TopLevel::handleAmpersand(const QString& modName) const {
203 QString modulename = modName;
204 // double it
205 if (modulename.contains( '&')) {
206 for (int i = modulename.length(); i >= 0; --i)
207 if (modulename[ i ] == '&')
208 modulename.insert(i, "&");
211 return modulename;
214 void TopLevel::activateGeneral() {
215 kDebug() << "Activating General..." << endl;
217 about_module->setText(i18n("About Current Module"));
218 about_module->setIcon(KIcon());
219 about_module->setVisible(false);
221 setCaption(i18n("General Information"), false);
223 report_bug->setText(i18n("&Report Bug..."));