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 <sys/types.h>
19
#include "componentchooser.h"
20
#include "componentchooser.moc"
22
#include <qcheckbox.h>
25
#include <qradiobutton.h>
26
#include <qwidgetstack.h>
28
#include <dcopclient.h>
30
#include <kapplication.h>
31
#include <kemailsettings.h>
34
#include <kmessagebox.h>
35
#include <kopenwith.h>
36
#include <ksimpleconfig.h>
37
#include <kstandarddirs.h>
39
#include <kurlrequester.h>
41
class MyListBoxItem: public QListBoxText
44
MyListBoxItem(const QString& text, const QString &file):QListBoxText(text),File(file){}
45
virtual ~MyListBoxItem(){;}
50
//BEGIN General kpart based Component selection
52
CfgComponent::CfgComponent(QWidget *parent):ComponentConfig_UI(parent),CfgPlugin(){
53
m_lookupDict.setAutoDelete(true);
54
m_revLookupDict.setAutoDelete(true);
55
connect(ComponentSelector,SIGNAL(activated(const QString&)),this,SLOT(slotComponentChanged(const QString&)));
58
CfgComponent::~CfgComponent(){}
60
void CfgComponent::slotComponentChanged(const QString&) {
64
void CfgComponent::save(KConfig *cfg) {
65
// yes, this can happen if there are NO KTrader offers for this component
66
if (!m_lookupDict[ComponentSelector->currentText()])
69
QString ServiceTypeToConfigure=cfg->readEntry("ServiceTypeToConfigure");
70
KConfig *store = new KConfig(cfg->readPathEntry("storeInFile","null"));
71
store->setGroup(cfg->readEntry("valueSection"));
72
store->writePathEntry(cfg->readEntry("valueName","kcm_componenchooser_null"),*m_lookupDict[ComponentSelector->currentText()]);
78
void CfgComponent::load(KConfig *cfg) {
80
ComponentSelector->clear();
82
m_revLookupDict.clear();
84
QString ServiceTypeToConfigure=cfg->readEntry("ServiceTypeToConfigure");
86
QString MimeTypeOfInterest=cfg->readEntry("MimeTypeOfInterest");
87
KTrader::OfferList offers = KTrader::self()->query(MimeTypeOfInterest, "'"+ServiceTypeToConfigure+"' in ServiceTypes");
89
for (KTrader::OfferList::Iterator tit = offers.begin(); tit != offers.end(); ++tit)
91
ComponentSelector->insertItem((*tit)->name());
92
m_lookupDict.insert((*tit)->name(),new QString((*tit)->desktopEntryName()));
93
m_revLookupDict.insert((*tit)->desktopEntryName(),new QString((*tit)->name()));
96
KConfig *store = new KConfig(cfg->readPathEntry("storeInFile","null"));
97
store->setGroup(cfg->readEntry("valueSection"));
98
QString setting=store->readEntry(cfg->readEntry("valueName","kcm_componenchooser_null"));
100
if (setting.isEmpty()) setting=cfg->readEntry("defaultImplementation");
101
QString *tmp=m_revLookupDict[setting];
103
for (int i=0;i<ComponentSelector->count();i++)
104
if ((*tmp)==ComponentSelector->text(i))
106
ComponentSelector->setCurrentItem(i);
112
void CfgComponent::defaults()
117
//END General kpart based Component selection
124
//BEGIN Email client config
125
CfgEmailClient::CfgEmailClient(QWidget *parent):EmailClientConfig_UI(parent),CfgPlugin(){
126
pSettings = new KEMailSettings();
128
connect(kmailCB, SIGNAL(toggled(bool)), SLOT(configChanged()) );
129
connect(txtEMailClient, SIGNAL(textChanged(const QString&)), SLOT(configChanged()) );
130
connect(chkRunTerminal, SIGNAL(clicked()), SLOT(configChanged()) );
133
CfgEmailClient::~CfgEmailClient() {
137
void CfgEmailClient::defaults()
142
void CfgEmailClient::load(KConfig *)
144
QString emailClient = pSettings->getSetting(KEMailSettings::ClientProgram);
145
bool useKMail = (emailClient.isEmpty());
147
kmailCB->setChecked(useKMail);
148
otherCB->setChecked(!useKMail);
149
txtEMailClient->setText(emailClient);
150
txtEMailClient->setFixedHeight(txtEMailClient->sizeHint().height());
151
chkRunTerminal->setChecked((pSettings->getSetting(KEMailSettings::ClientTerminal) == "true"));
157
void CfgEmailClient::configChanged()
162
void CfgEmailClient::selectEmailClient()
165
KOpenWithDlg dlg(urlList, i18n("Select preferred email client:"), QString::null, this);
166
// hide "Do not &close when command exits" here, we don't need it for a mail client
167
dlg.hideNoCloseOnExit();
168
if (dlg.exec() != QDialog::Accepted) return;
169
QString client = dlg.text();
171
// get the preferred Terminal Application
172
KConfigGroup confGroup( KGlobal::config(), QString::fromLatin1("General") );
173
QString preferredTerminal = confGroup.readPathEntry("TerminalApplication", QString::fromLatin1("konsole"));
174
preferredTerminal += QString::fromLatin1(" -e ");
176
int len = preferredTerminal.length();
177
bool b = client.left(len) == preferredTerminal;
178
if (b) client = client.mid(len);
179
if (!client.isEmpty())
181
chkRunTerminal->setChecked(b);
182
txtEMailClient->setText(client);
187
void CfgEmailClient::save(KConfig *)
189
if (kmailCB->isChecked())
191
pSettings->setSetting(KEMailSettings::ClientProgram, QString::null);
192
pSettings->setSetting(KEMailSettings::ClientTerminal, "false");
196
pSettings->setSetting(KEMailSettings::ClientProgram, txtEMailClient->text());
197
pSettings->setSetting(KEMailSettings::ClientTerminal, (chkRunTerminal->isChecked()) ? "true" : "false");
200
// insure proper permissions -- contains sensitive data
201
QString cfgName(KGlobal::dirs()->findResource("config", "emails"));
202
if (!cfgName.isEmpty())
203
::chmod(QFile::encodeName(cfgName), 0600);
205
kapp->dcopClient()->emitDCOPSignal("KDE_emailSettingsChanged()", QByteArray());
211
//END Email client config
215
//BEGIN Terminal Emulator Configuration
217
CfgTerminalEmulator::CfgTerminalEmulator(QWidget *parent):TerminalEmulatorConfig_UI(parent),CfgPlugin(){
218
connect(terminalLE,SIGNAL(textChanged(const QString &)), this, SLOT(configChanged()));
219
connect(terminalCB,SIGNAL(toggled(bool)),this,SLOT(configChanged()));
220
connect(otherCB,SIGNAL(toggled(bool)),this,SLOT(configChanged()));
223
CfgTerminalEmulator::~CfgTerminalEmulator() {
226
void CfgTerminalEmulator::configChanged()
231
void CfgTerminalEmulator::defaults()
237
void CfgTerminalEmulator::load(KConfig *) {
238
KConfig *config = new KConfig("kdeglobals", true);
239
config->setGroup("General");
240
QString terminal = config->readPathEntry("TerminalApplication","konsole");
241
if (terminal == "konsole")
243
terminalLE->setText("xterm");
244
terminalCB->setChecked(true);
248
terminalLE->setText(terminal);
249
otherCB->setChecked(true);
256
void CfgTerminalEmulator::save(KConfig *) {
258
KConfig *config = new KConfig("kdeglobals");
259
config->setGroup("General");
260
config->writePathEntry("TerminalApplication",terminalCB->isChecked()?"konsole":terminalLE->text(), true, true);
264
KIPC::sendMessageAll(KIPC::SettingsChanged);
265
kapp->dcopClient()->send("klauncher", "klauncher","reparseConfiguration()", QString::null);
270
void CfgTerminalEmulator::selectTerminalApp()
273
KOpenWithDlg dlg(urlList, i18n("Select preferred terminal application:"), QString::null, this);
274
// hide "Run in &terminal" here, we don't need it for a Terminal Application
275
dlg.hideRunInTerminal();
276
if (dlg.exec() != QDialog::Accepted) return;
277
QString client = dlg.text();
279
if (!client.isEmpty())
281
terminalLE->setText(client);
285
//END Terminal Emulator Configuration
287
//BEGIN Browser Configuration
289
CfgBrowser::CfgBrowser(QWidget *parent) : BrowserConfig_UI(parent),CfgPlugin(){
290
connect(lineExec,SIGNAL(textChanged(const QString &)),this,SLOT(configChanged()));
291
connect(radioKIO,SIGNAL(toggled(bool)),this,SLOT(configChanged()));
292
connect(radioExec,SIGNAL(toggled(bool)),this,SLOT(configChanged()));
295
CfgBrowser::~CfgBrowser() {
298
void CfgBrowser::configChanged()
303
void CfgBrowser::defaults()
309
void CfgBrowser::load(KConfig *) {
310
KConfig *config = new KConfig("kdeglobals", true);
311
config->setGroup("General");
312
QString exec = config->readEntry("BrowserApplication");
315
radioKIO->setChecked(true);
316
m_browserExec = exec;
317
m_browserService = 0;
321
radioExec->setChecked(true);
322
if (exec.startsWith("!"))
324
m_browserExec = exec.mid(1);
325
m_browserService = 0;
329
m_browserService = KService::serviceByStorageId( exec );
330
if (m_browserService)
331
m_browserExec = m_browserService->desktopEntryName();
333
m_browserExec = QString::null;
337
lineExec->setText(m_browserExec);
343
void CfgBrowser::save(KConfig *) {
345
KConfig *config = new KConfig("kdeglobals");
346
config->setGroup("General");
348
if (radioExec->isChecked())
350
exec = lineExec->text();
351
if (m_browserService && (exec == m_browserExec))
352
exec = m_browserService->storageId(); // Use service
354
exec = "!" + exec; // Litteral command
356
config->writePathEntry("BrowserApplication", exec, true, true);
360
KIPC::sendMessageAll(KIPC::SettingsChanged);
365
void CfgBrowser::selectBrowser()
368
KOpenWithDlg dlg(urlList, i18n("Select preferred Web browser application:"), QString::null, this);
369
if (dlg.exec() != QDialog::Accepted) return;
370
m_browserService = dlg.service();
371
if (m_browserService)
372
m_browserExec = m_browserService->desktopEntryName();
374
m_browserExec = dlg.text();
376
lineExec->setText(m_browserExec);
379
//END Terminal Emulator Configuration
381
ComponentChooser::ComponentChooser(QWidget *parent, const char *name):
382
ComponentChooser_UI(parent,name), configWidget(0) {
384
ComponentChooser_UILayout->setRowStretch(1, 1);
385
somethingChanged=false;
386
latestEditedService="";
389
QStringList services=KGlobal::dirs()->findAllResources( "data","kcm_componentchooser/*.desktop",false,true,dummy);
390
for (QStringList::Iterator it=services.begin();it!=services.end();++it)
392
KSimpleConfig cfg(*it);
393
ServiceChooser->insertItem(new MyListBoxItem(cfg.readEntry("Name",i18n("Unknown")),(*it)));
396
ServiceChooser->setFixedWidth(ServiceChooser->sizeHint().width());
397
ServiceChooser->sort();
398
connect(ServiceChooser,SIGNAL(highlighted(QListBoxItem*)),this,SLOT(slotServiceSelected(QListBoxItem*)));
399
ServiceChooser->setSelected(0,true);
400
slotServiceSelected(ServiceChooser->item(0));
404
void ComponentChooser::slotServiceSelected(QListBoxItem* it) {
407
if (somethingChanged) {
408
if (KMessageBox::questionYesNo(this,i18n("<qt>You changed the default component of your choice. Do you want to save that change now?</qt>"),QString::null,KStdGuiItem::save(),KStdGuiItem::discard())==KMessageBox::Yes) save();
410
KSimpleConfig cfg(static_cast<MyListBoxItem*>(it)->File);
412
ComponentDescription->setText(cfg.readEntry("Comment",i18n("No description available")));
413
ComponentDescription->setMinimumSize(ComponentDescription->sizeHint());
416
QString cfgType=cfg.readEntry("configurationType");
417
QWidget *newConfigWidget = 0;
418
if (cfgType.isEmpty() || (cfgType=="component"))
420
if (!(configWidget && configWidget->qt_cast("CfgComponent")))
422
CfgComponent* cfgcomp = new CfgComponent(configContainer);
423
cfgcomp->ChooserDocu->setText(i18n("Choose from the list below which component should be used by default for the %1 service.").arg(it->text()));
424
newConfigWidget = cfgcomp;
428
static_cast<CfgComponent*>(configWidget)->ChooserDocu->setText(i18n("Choose from the list below which component should be used by default for the %1 service.").arg(it->text()));
431
else if (cfgType=="internal_email")
433
if (!(configWidget && configWidget->qt_cast("CfgEmailClient")))
435
newConfigWidget = new CfgEmailClient(configContainer);
439
else if (cfgType=="internal_terminal")
441
if (!(configWidget && configWidget->qt_cast("CfgTerminalEmulator")))
443
newConfigWidget = new CfgTerminalEmulator(configContainer);
447
else if (cfgType=="internal_browser")
449
if (!(configWidget && configWidget->qt_cast("CfgBrowser")))
451
newConfigWidget = new CfgBrowser(configContainer);
458
configContainer->addWidget(newConfigWidget);
459
configContainer->raiseWidget(newConfigWidget);
460
configContainer->removeWidget(configWidget);
462
configWidget=newConfigWidget;
463
connect(configWidget,SIGNAL(changed(bool)),this,SLOT(emitChanged(bool)));
464
configContainer->setMinimumSize(configWidget->sizeHint());
468
static_cast<CfgPlugin*>(configWidget->qt_cast("CfgPlugin"))->load(&cfg);
471
latestEditedService=static_cast<MyListBoxItem*>(it)->File;
475
void ComponentChooser::emitChanged(bool val) {
476
somethingChanged=val;
481
ComponentChooser::~ComponentChooser()
486
void ComponentChooser::load() {
489
CfgPlugin * plugin = static_cast<CfgPlugin*>(
490
configWidget->qt_cast( "CfgPlugin" ) );
493
KSimpleConfig cfg(latestEditedService);
494
plugin->load( &cfg );
499
void ComponentChooser::save() {
502
CfgPlugin * plugin = static_cast<CfgPlugin*>(
503
configWidget->qt_cast( "CfgPlugin" ) );
506
KSimpleConfig cfg(latestEditedService);
507
plugin->save( &cfg );
512
void ComponentChooser::restoreDefault() {
515
static_cast<CfgPlugin*>(configWidget->qt_cast("CfgPlugin"))->defaults();
520
txtEMailClient->setText("kmail");
521
chkRunTerminal->setChecked(false);
523
// Check if -e is needed, I do not think so
524
terminalLE->setText("xterm"); //No need for i18n
525
terminalCB->setChecked(true);
530
// vim: sw=4 ts=4 noet