1 /***************************************************************************
2 componentchooser.cpp - description
4 copyright : (C) 2002 by Joseph Wenninger
6 ***************************************************************************/
8 /***************************************************************************
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 *
14 ***************************************************************************/
16 #include "componentchooser.h"
17 #include "componentchooser.moc"
19 #include "componentchooserbrowser.h"
20 #include "componentchooseremail.h"
21 #include "componentchooserfilemanager.h"
23 #include "componentchooserterminal.h"
26 #include "componentchooserwm.h"
32 #include <QRadioButton>
35 #include <kmessagebox.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
48 MyListBoxItem(const QString
& text
, const QString
&file
):QListWidgetItem(text
),mFile(file
){}
53 //BEGIN General kpart based Component selection
55 CfgComponent::CfgComponent(QWidget
*parent
)
56 : QWidget(parent
), Ui::ComponentConfig_UI(), CfgPlugin()
59 connect(ComponentSelector
,SIGNAL(activated(const QString
&)),this,SLOT(slotComponentChanged(const QString
&)));
62 CfgComponent::~CfgComponent()
66 void CfgComponent::slotComponentChanged(const QString
&) {
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()))
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()));
85 void CfgComponent::load(KConfig
*cfg
) {
87 ComponentSelector
->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
);
120 void CfgComponent::defaults()
125 //END General kpart based Component selection
132 ComponentChooser::ComponentChooser(QWidget
*parent
):
133 QWidget(parent
), Ui::ComponentChooser_UI(), configWidget(0)
136 static_cast<QGridLayout
*>(layout())->setRowStretch(1, 1);
137 somethingChanged
=false;
138 latestEditedService
="";
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
) {
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
;
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
);
193 else if (cfgType
=="internal_terminal")
195 if (!(configWidget
&& qobject_cast
<CfgTerminalEmulator
*>(configWidget
)))
197 newConfigWidget
= new CfgTerminalEmulator(configContainer
);
202 else if (cfgType
=="internal_wm")
204 if (!(configWidget
&& qobject_cast
<CfgWm
*>(configWidget
)))
206 newConfigWidget
= new CfgWm(configContainer
);
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
);
231 configContainer
->addWidget(newConfigWidget
);
232 configContainer
->setCurrentWidget (newConfigWidget
);
233 configContainer
->removeWidget(configWidget
);
235 configWidget
=newConfigWidget
;
236 connect(configWidget
,SIGNAL(changed(bool)),this,SLOT(emitChanged(bool)));
237 configContainer
->setMinimumSize(configWidget
->sizeHint());
241 dynamic_cast<CfgPlugin
*>(configWidget
)->load(&cfg
);
244 latestEditedService
=static_cast<MyListBoxItem
*>(it
)->mFile
;
248 void ComponentChooser::emitChanged(bool val
) {
249 somethingChanged
=val
;
254 ComponentChooser::~ComponentChooser()
259 void ComponentChooser::load() {
262 CfgPlugin
* plugin
= dynamic_cast<CfgPlugin
*>( configWidget
);
265 KConfig
cfg(latestEditedService
, KConfig::SimpleConfig
);
266 plugin
->load( &cfg
);
271 void ComponentChooser::save() {
274 CfgPlugin
* plugin
= dynamic_cast<CfgPlugin
*>( configWidget
);
277 KConfig
cfg(latestEditedService
, KConfig::SimpleConfig
);
278 plugin
->save( &cfg
);
283 void ComponentChooser::restoreDefault() {
286 dynamic_cast<CfgPlugin
*>(configWidget
)->defaults();
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);
301 // vim: sw=4 ts=4 noet