delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kcontrol / componentchooser / componentchooser.cpp
blobe7e140bde412aa0de55e1ed8507da8ea6c4e8b84
1 /***************************************************************************
2 componentchooser.cpp - description
3 -------------------
4 copyright : (C) 2002 by Joseph Wenninger
5 email : jowenn@kde.org
6 ***************************************************************************/
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License verstion 2 as *
12 * published by the Free Software Foundation *
13 * *
14 ***************************************************************************/
16 #include "componentchooser.h"
17 #include "componentchooser.moc"
19 #include "componentchooserbrowser.h"
20 #include "componentchooseremail.h"
21 #include "componentchooserfilemanager.h"
22 #ifdef Q_OS_UNIX
23 #include "componentchooserterminal.h"
24 #endif
25 #ifdef Q_WS_X11
26 #include "componentchooserwm.h"
27 #endif
29 #include <QCheckBox>
30 #include <QLabel>
31 #include <QLayout>
32 #include <QRadioButton>
34 #include <klocale.h>
35 #include <kmessagebox.h>
36 #include <kconfig.h>
37 #include <kstandarddirs.h>
38 #include <kmimetypetrader.h>
39 #include <kurlrequester.h>
40 #include <ktoolinvocation.h>
41 #include <klauncher_iface.h>
42 #include <kconfiggroup.h>
43 #include <KServiceTypeTrader>
45 class MyListBoxItem: public QListWidgetItem
47 public:
48 MyListBoxItem(const QString& text, const QString &file):QListWidgetItem(text),mFile(file){}
49 QString mFile;
53 //BEGIN General kpart based Component selection
55 CfgComponent::CfgComponent(QWidget *parent)
56 : QWidget(parent), Ui::ComponentConfig_UI(), CfgPlugin()
58 setupUi( this );
59 connect(ComponentSelector,SIGNAL(activated(const QString&)),this,SLOT(slotComponentChanged(const QString&)));
62 CfgComponent::~CfgComponent()
66 void CfgComponent::slotComponentChanged(const QString&) {
67 emit changed(true);
70 void CfgComponent::save(KConfig *cfg) {
71 // yes, this can happen if there are NO KTrader offers for this component
72 if (!m_lookupDict.contains(ComponentSelector->currentText()))
73 return;
75 KConfigGroup mainGroup = cfg->group("");
76 QString serviceTypeToConfigure=mainGroup.readEntry("ServiceTypeToConfigure");
77 KConfig store(mainGroup.readPathEntry("storeInFile", "null"));
78 KConfigGroup cg(&store, mainGroup.readEntry("valueSection"));
79 cg.writePathEntry(mainGroup.readEntry("valueName", "kcm_componenchooser_null"),
80 m_lookupDict.value(ComponentSelector->currentText()));
81 store.sync();
82 emit changed(false);
85 void CfgComponent::load(KConfig *cfg) {
87 ComponentSelector->clear();
88 m_lookupDict.clear();
89 m_revLookupDict.clear();
91 const KConfigGroup mainGroup = cfg->group("");
92 const QString serviceTypeToConfigure = mainGroup.readEntry("ServiceTypeToConfigure");
94 const KService::List offers = KServiceTypeTrader::self()->query(serviceTypeToConfigure);
96 for (KService::List::const_iterator tit = offers.begin(); tit != offers.end(); ++tit) {
97 ComponentSelector->addItem((*tit)->name());
98 m_lookupDict.insert((*tit)->name(), (*tit)->desktopEntryName());
99 m_revLookupDict.insert((*tit)->desktopEntryName(), (*tit)->name());
102 KConfig store(mainGroup.readPathEntry("storeInFile","null"));
103 const KConfigGroup group(&store, mainGroup.readEntry("valueSection"));
104 QString setting = group.readEntry(mainGroup.readEntry("valueName","kcm_componenchooser_null"), QString());
106 if (setting.isEmpty())
107 setting = mainGroup.readEntry("defaultImplementation", QString());
108 QString tmp = m_revLookupDict.value(setting);
109 if (!tmp.isEmpty()) {
110 for (int i=0;i<ComponentSelector->count();i++)
111 if (tmp==ComponentSelector->itemText(i))
113 ComponentSelector->setCurrentIndex(i);
114 break;
117 emit changed(false);
120 void CfgComponent::defaults()
122 //todo
125 //END General kpart based Component selection
132 ComponentChooser::ComponentChooser(QWidget *parent):
133 QWidget(parent), Ui::ComponentChooser_UI(), configWidget(0)
135 setupUi(this);
136 static_cast<QGridLayout*>(layout())->setRowStretch(1, 1);
137 somethingChanged=false;
138 latestEditedService="";
140 QStringList dummy;
141 const QStringList services=KGlobal::dirs()->findAllResources( "data","kcm_componentchooser/*.desktop",
142 KStandardDirs::NoDuplicates, dummy );
143 for (QStringList::const_iterator it=services.constBegin(); it!=services.constEnd(); ++it)
145 KConfig cfg(*it, KConfig::SimpleConfig);
146 ServiceChooser->addItem(new MyListBoxItem(cfg.group("").readEntry("Name",i18n("Unknown")),(*it)));
149 ServiceChooser->setFixedWidth(ServiceChooser->sizeHintForColumn(0) + 20);
150 ServiceChooser->model()->sort(0);
151 connect(ServiceChooser,SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),this,SLOT(slotServiceSelected(QListWidgetItem*)));
152 ServiceChooser->item(0)->setSelected(true);
153 slotServiceSelected(ServiceChooser->item(0));
157 void ComponentChooser::slotServiceSelected(QListWidgetItem* it) {
158 if (!it) return;
160 if (somethingChanged) {
161 if (KMessageBox::questionYesNo(this,i18n("<qt>You changed the default component of your choice, do want to save that change now ?</qt>"),QString(),KStandardGuiItem::save(),KStandardGuiItem::discard())==KMessageBox::Yes) save();
163 KConfig cfg(static_cast<MyListBoxItem*>(it)->mFile, KConfig::SimpleConfig);
165 ComponentDescription->setText(cfg.group("").readEntry("Comment",i18n("No description available")));
166 ComponentDescription->setMinimumSize(ComponentDescription->sizeHint());
169 QString cfgType=cfg.group("").readEntry("configurationType");
170 QWidget *newConfigWidget = 0;
171 if (cfgType.isEmpty() || (cfgType=="component"))
173 if (!(configWidget && qobject_cast<CfgComponent*>(configWidget)))
175 CfgComponent* cfgcomp = new CfgComponent(configContainer);
176 cfgcomp->ChooserDocu->setText(i18n("Choose from the list below which component should be used by default for the %1 service.", it->text()));
177 newConfigWidget = cfgcomp;
179 else
181 static_cast<CfgComponent*>(configWidget)->ChooserDocu->setText(i18n("Choose from the list below which component should be used by default for the %1 service.", it->text()));
184 else if (cfgType=="internal_email")
186 if (!(configWidget && qobject_cast<CfgEmailClient*>(configWidget)))
188 newConfigWidget = new CfgEmailClient(configContainer);
192 #ifdef Q_OS_UNIX
193 else if (cfgType=="internal_terminal")
195 if (!(configWidget && qobject_cast<CfgTerminalEmulator*>(configWidget)))
197 newConfigWidget = new CfgTerminalEmulator(configContainer);
201 #ifdef Q_WS_X11
202 else if (cfgType=="internal_wm")
204 if (!(configWidget && qobject_cast<CfgWm*>(configWidget)))
206 newConfigWidget = new CfgWm(configContainer);
210 #endif
211 #endif
212 else if (cfgType=="internal_filemanager")
214 if (!(configWidget && qobject_cast<CfgFileManager*>(configWidget)))
216 newConfigWidget = new CfgFileManager(configContainer);
220 else if (cfgType=="internal_browser")
222 if (!(configWidget && qobject_cast<CfgBrowser*>(configWidget)))
224 newConfigWidget = new CfgBrowser(configContainer);
229 if (newConfigWidget)
231 configContainer->addWidget(newConfigWidget);
232 configContainer->setCurrentWidget (newConfigWidget);
233 configContainer->removeWidget(configWidget);
234 delete configWidget;
235 configWidget=newConfigWidget;
236 connect(configWidget,SIGNAL(changed(bool)),this,SLOT(emitChanged(bool)));
237 configContainer->setMinimumSize(configWidget->sizeHint());
240 if (configWidget)
241 dynamic_cast<CfgPlugin*>(configWidget)->load(&cfg);
243 emitChanged(false);
244 latestEditedService=static_cast<MyListBoxItem*>(it)->mFile;
248 void ComponentChooser::emitChanged(bool val) {
249 somethingChanged=val;
250 emit changed(val);
254 ComponentChooser::~ComponentChooser()
256 delete configWidget;
259 void ComponentChooser::load() {
260 if( configWidget )
262 CfgPlugin * plugin = dynamic_cast<CfgPlugin*>( configWidget );
263 if( plugin )
265 KConfig cfg(latestEditedService, KConfig::SimpleConfig);
266 plugin->load( &cfg );
271 void ComponentChooser::save() {
272 if( configWidget )
274 CfgPlugin* plugin = dynamic_cast<CfgPlugin*>( configWidget );
275 if( plugin )
277 KConfig cfg(latestEditedService, KConfig::SimpleConfig);
278 plugin->save( &cfg );
283 void ComponentChooser::restoreDefault() {
284 if (configWidget)
286 dynamic_cast<CfgPlugin*>(configWidget)->defaults();
287 emitChanged(true);
291 txtEMailClient->setText("kmail");
292 chkRunTerminal->setChecked(false);
294 // Check if -e is needed, I do not think so
295 terminalLE->setText("xterm"); //No need for i18n
296 terminalCB->setChecked(true);
297 emitChanged(false);
301 // vim: sw=4 ts=4 noet