~ubuntu-branches/ubuntu/lucid/kdebase/lucid

« back to all changes in this revision

Viewing changes to kcontrol/componentchooser/componentchooser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-05-27 12:09:48 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20080527120948-dottsyd5rcwhzd36
Tags: 4:4.0.80-1ubuntu1
* Merge with Debian
 - remove 97_fix_target_link_libraries.diff
 - Add replaces/conflicts on -kde4 packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
                          componentchooser.cpp  -  description
3
 
                             -------------------
4
 
    copyright            : (C) 2002 by Joseph Wenninger
5
 
    email                : jowenn@kde.org
6
 
 ***************************************************************************/
7
 
 
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
 
 ***************************************************************************/
15
 
 
16
 
#include <sys/types.h>
17
 
#include <sys/stat.h>
18
 
 
19
 
#include "componentchooser.h"
20
 
#include "componentchooser.moc"
21
 
 
22
 
#include <qcheckbox.h>
23
 
#include <qlabel.h>
24
 
#include <qlayout.h>
25
 
#include <qradiobutton.h>
26
 
#include <qwidgetstack.h>
27
 
 
28
 
#include <dcopclient.h>
29
 
 
30
 
#include <kapplication.h>
31
 
#include <kemailsettings.h>
32
 
#include <kipc.h>
33
 
#include <klocale.h>
34
 
#include <kmessagebox.h>
35
 
#include <kopenwith.h>
36
 
#include <ksimpleconfig.h>
37
 
#include <kstandarddirs.h>
38
 
#include <ktrader.h>
39
 
#include <kurlrequester.h>
40
 
 
41
 
class MyListBoxItem: public QListBoxText
42
 
{
43
 
public:
44
 
        MyListBoxItem(const QString& text, const QString &file):QListBoxText(text),File(file){}
45
 
        virtual ~MyListBoxItem(){;}
46
 
        QString File;
47
 
};
48
 
 
49
 
 
50
 
//BEGIN  General kpart based Component selection
51
 
 
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&)));
56
 
}
57
 
 
58
 
CfgComponent::~CfgComponent(){}
59
 
 
60
 
void CfgComponent::slotComponentChanged(const QString&) {
61
 
        emit changed(true);
62
 
}
63
 
 
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()])
67
 
                        return;
68
 
 
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()]);
73
 
                store->sync();
74
 
                delete store;
75
 
                emit changed(false);
76
 
}
77
 
 
78
 
void CfgComponent::load(KConfig *cfg) {
79
 
 
80
 
        ComponentSelector->clear();
81
 
        m_lookupDict.clear();
82
 
        m_revLookupDict.clear();
83
 
 
84
 
        QString ServiceTypeToConfigure=cfg->readEntry("ServiceTypeToConfigure");
85
 
 
86
 
        QString MimeTypeOfInterest=cfg->readEntry("MimeTypeOfInterest");
87
 
        KTrader::OfferList offers = KTrader::self()->query(MimeTypeOfInterest, "'"+ServiceTypeToConfigure+"' in ServiceTypes");
88
 
 
89
 
        for (KTrader::OfferList::Iterator tit = offers.begin(); tit != offers.end(); ++tit)
90
 
        {
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()));
94
 
        }
95
 
 
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"));
99
 
        delete store;
100
 
        if (setting.isEmpty()) setting=cfg->readEntry("defaultImplementation");
101
 
        QString *tmp=m_revLookupDict[setting];
102
 
        if (tmp)
103
 
                for (int i=0;i<ComponentSelector->count();i++)
104
 
                        if ((*tmp)==ComponentSelector->text(i))
105
 
                        {
106
 
                                ComponentSelector->setCurrentItem(i);
107
 
                                break;
108
 
                        }
109
 
        emit changed(false);
110
 
}
111
 
 
112
 
void CfgComponent::defaults()
113
 
{
114
 
    //todo
115
 
}
116
 
 
117
 
//END  General kpart based Component selection
118
 
 
119
 
 
120
 
 
121
 
 
122
 
 
123
 
 
124
 
//BEGIN Email client config
125
 
CfgEmailClient::CfgEmailClient(QWidget *parent):EmailClientConfig_UI(parent),CfgPlugin(){
126
 
        pSettings = new KEMailSettings();
127
 
 
128
 
        connect(kmailCB, SIGNAL(toggled(bool)), SLOT(configChanged()) );
129
 
        connect(txtEMailClient, SIGNAL(textChanged(const QString&)), SLOT(configChanged()) );
130
 
        connect(chkRunTerminal, SIGNAL(clicked()), SLOT(configChanged()) );
131
 
}
132
 
 
133
 
CfgEmailClient::~CfgEmailClient() {
134
 
        delete pSettings;
135
 
}
136
 
 
137
 
void CfgEmailClient::defaults()
138
 
{
139
 
    load(0L);
140
 
}
141
 
 
142
 
void CfgEmailClient::load(KConfig *)
143
 
{
144
 
        QString emailClient = pSettings->getSetting(KEMailSettings::ClientProgram);
145
 
        bool useKMail = (emailClient.isEmpty());
146
 
 
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"));
152
 
 
153
 
        emit changed(false);
154
 
 
155
 
}
156
 
 
157
 
void CfgEmailClient::configChanged()
158
 
{
159
 
        emit changed(true);
160
 
}
161
 
 
162
 
void CfgEmailClient::selectEmailClient()
163
 
{
164
 
        KURL::List urlList;
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();
170
 
 
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 ");
175
 
        
176
 
        int len = preferredTerminal.length();
177
 
        bool b = client.left(len) == preferredTerminal;
178
 
        if (b) client = client.mid(len);
179
 
        if (!client.isEmpty())
180
 
        {
181
 
                chkRunTerminal->setChecked(b);
182
 
                txtEMailClient->setText(client);
183
 
        }
184
 
}
185
 
 
186
 
 
187
 
void CfgEmailClient::save(KConfig *)
188
 
{
189
 
        if (kmailCB->isChecked())
190
 
        {
191
 
                pSettings->setSetting(KEMailSettings::ClientProgram, QString::null);
192
 
                pSettings->setSetting(KEMailSettings::ClientTerminal, "false");
193
 
        }
194
 
        else
195
 
        {
196
 
                pSettings->setSetting(KEMailSettings::ClientProgram, txtEMailClient->text());
197
 
                pSettings->setSetting(KEMailSettings::ClientTerminal, (chkRunTerminal->isChecked()) ? "true" : "false");
198
 
        }
199
 
 
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);
204
 
 
205
 
        kapp->dcopClient()->emitDCOPSignal("KDE_emailSettingsChanged()", QByteArray());
206
 
 
207
 
        emit changed(false);
208
 
}
209
 
 
210
 
 
211
 
//END Email client config
212
 
 
213
 
 
214
 
 
215
 
//BEGIN Terminal Emulator Configuration
216
 
 
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()));
221
 
}
222
 
 
223
 
CfgTerminalEmulator::~CfgTerminalEmulator() {
224
 
}
225
 
 
226
 
void CfgTerminalEmulator::configChanged()
227
 
{
228
 
        emit changed(true);
229
 
}
230
 
 
231
 
void CfgTerminalEmulator::defaults()
232
 
{
233
 
    load(0L);
234
 
}
235
 
 
236
 
 
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")
242
 
        {
243
 
           terminalLE->setText("xterm");
244
 
           terminalCB->setChecked(true);
245
 
        }
246
 
        else
247
 
        {
248
 
          terminalLE->setText(terminal);
249
 
          otherCB->setChecked(true);
250
 
        }
251
 
        delete config;
252
 
 
253
 
        emit changed(false);
254
 
}
255
 
 
256
 
void CfgTerminalEmulator::save(KConfig *) {
257
 
 
258
 
        KConfig *config = new KConfig("kdeglobals");
259
 
        config->setGroup("General");
260
 
        config->writePathEntry("TerminalApplication",terminalCB->isChecked()?"konsole":terminalLE->text(), true, true);
261
 
        config->sync();
262
 
        delete config;
263
 
 
264
 
        KIPC::sendMessageAll(KIPC::SettingsChanged);
265
 
        kapp->dcopClient()->send("klauncher", "klauncher","reparseConfiguration()", QString::null);
266
 
 
267
 
        emit changed(false);
268
 
}
269
 
 
270
 
void CfgTerminalEmulator::selectTerminalApp()
271
 
{
272
 
        KURL::List urlList;
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();
278
 
 
279
 
        if (!client.isEmpty())
280
 
        {
281
 
                terminalLE->setText(client);
282
 
        }
283
 
}
284
 
 
285
 
//END Terminal Emulator Configuration
286
 
 
287
 
//BEGIN Browser Configuration
288
 
 
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()));
293
 
}
294
 
 
295
 
CfgBrowser::~CfgBrowser() {
296
 
}
297
 
 
298
 
void CfgBrowser::configChanged()
299
 
{
300
 
        emit changed(true);
301
 
}
302
 
 
303
 
void CfgBrowser::defaults()
304
 
{
305
 
        load(0L);
306
 
}
307
 
 
308
 
 
309
 
void CfgBrowser::load(KConfig *) {
310
 
        KConfig *config = new KConfig("kdeglobals", true);
311
 
        config->setGroup("General");
312
 
        QString exec = config->readEntry("BrowserApplication");
313
 
        if (exec.isEmpty())
314
 
        {
315
 
           radioKIO->setChecked(true);
316
 
           m_browserExec = exec;
317
 
           m_browserService = 0;
318
 
        }
319
 
        else
320
 
        {
321
 
           radioExec->setChecked(true);
322
 
           if (exec.startsWith("!"))
323
 
           {
324
 
              m_browserExec = exec.mid(1);
325
 
              m_browserService = 0;
326
 
           }
327
 
           else
328
 
           {
329
 
              m_browserService = KService::serviceByStorageId( exec );
330
 
              if (m_browserService)
331
 
                 m_browserExec = m_browserService->desktopEntryName();
332
 
              else
333
 
                 m_browserExec = QString::null;
334
 
           }
335
 
        }
336
 
        
337
 
        lineExec->setText(m_browserExec);
338
 
        delete config;
339
 
 
340
 
        emit changed(false);
341
 
}
342
 
 
343
 
void CfgBrowser::save(KConfig *) {
344
 
 
345
 
        KConfig *config = new KConfig("kdeglobals");
346
 
        config->setGroup("General");
347
 
        QString exec;
348
 
        if (radioExec->isChecked())
349
 
        {
350
 
           exec = lineExec->text();
351
 
           if (m_browserService && (exec == m_browserExec))
352
 
              exec = m_browserService->storageId(); // Use service
353
 
           else
354
 
              exec = "!" + exec; // Litteral command
355
 
        }
356
 
        config->writePathEntry("BrowserApplication", exec, true, true);
357
 
        config->sync();
358
 
        delete config;
359
 
 
360
 
        KIPC::sendMessageAll(KIPC::SettingsChanged);
361
 
 
362
 
        emit changed(false);
363
 
}
364
 
 
365
 
void CfgBrowser::selectBrowser()
366
 
{
367
 
        KURL::List urlList;
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();
373
 
        else
374
 
           m_browserExec = dlg.text();
375
 
 
376
 
        lineExec->setText(m_browserExec);
377
 
}
378
 
 
379
 
//END Terminal Emulator Configuration
380
 
 
381
 
ComponentChooser::ComponentChooser(QWidget *parent, const char *name):
382
 
        ComponentChooser_UI(parent,name), configWidget(0) {
383
 
 
384
 
        ComponentChooser_UILayout->setRowStretch(1, 1);
385
 
        somethingChanged=false;
386
 
        latestEditedService="";
387
 
 
388
 
        QStringList dummy;
389
 
        QStringList services=KGlobal::dirs()->findAllResources( "data","kcm_componentchooser/*.desktop",false,true,dummy);
390
 
        for (QStringList::Iterator it=services.begin();it!=services.end();++it)
391
 
        {
392
 
                KSimpleConfig cfg(*it);
393
 
                ServiceChooser->insertItem(new MyListBoxItem(cfg.readEntry("Name",i18n("Unknown")),(*it)));
394
 
 
395
 
        }
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));
401
 
 
402
 
}
403
 
 
404
 
void ComponentChooser::slotServiceSelected(QListBoxItem* it) {
405
 
        if (!it) return;
406
 
 
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();
409
 
        }
410
 
        KSimpleConfig cfg(static_cast<MyListBoxItem*>(it)->File);
411
 
 
412
 
        ComponentDescription->setText(cfg.readEntry("Comment",i18n("No description available")));
413
 
        ComponentDescription->setMinimumSize(ComponentDescription->sizeHint());
414
 
 
415
 
 
416
 
        QString cfgType=cfg.readEntry("configurationType");
417
 
        QWidget *newConfigWidget = 0;
418
 
        if (cfgType.isEmpty() || (cfgType=="component"))
419
 
        {
420
 
                if (!(configWidget && configWidget->qt_cast("CfgComponent")))
421
 
                {
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;
425
 
                }
426
 
                else
427
 
                {
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()));
429
 
                }
430
 
        }
431
 
        else if (cfgType=="internal_email")
432
 
        {
433
 
                if (!(configWidget && configWidget->qt_cast("CfgEmailClient")))
434
 
                {
435
 
                        newConfigWidget = new CfgEmailClient(configContainer);
436
 
                }
437
 
 
438
 
        }
439
 
        else if (cfgType=="internal_terminal")
440
 
        {
441
 
                if (!(configWidget && configWidget->qt_cast("CfgTerminalEmulator")))
442
 
                {
443
 
                        newConfigWidget = new CfgTerminalEmulator(configContainer);
444
 
                }
445
 
 
446
 
        }
447
 
        else if (cfgType=="internal_browser")
448
 
        {
449
 
                if (!(configWidget && configWidget->qt_cast("CfgBrowser")))
450
 
                {
451
 
                        newConfigWidget = new CfgBrowser(configContainer);
452
 
                }
453
 
 
454
 
        }
455
 
 
456
 
        if (newConfigWidget)
457
 
        {
458
 
                configContainer->addWidget(newConfigWidget);
459
 
                configContainer->raiseWidget(newConfigWidget);
460
 
                configContainer->removeWidget(configWidget);
461
 
                delete configWidget;
462
 
                configWidget=newConfigWidget;
463
 
                connect(configWidget,SIGNAL(changed(bool)),this,SLOT(emitChanged(bool)));
464
 
                configContainer->setMinimumSize(configWidget->sizeHint());
465
 
        }
466
 
        
467
 
        if (configWidget)
468
 
                static_cast<CfgPlugin*>(configWidget->qt_cast("CfgPlugin"))->load(&cfg);
469
 
        
470
 
        emitChanged(false);
471
 
        latestEditedService=static_cast<MyListBoxItem*>(it)->File;
472
 
}
473
 
 
474
 
 
475
 
void ComponentChooser::emitChanged(bool val) {
476
 
        somethingChanged=val;
477
 
        emit changed(val);
478
 
}
479
 
 
480
 
 
481
 
ComponentChooser::~ComponentChooser()
482
 
{
483
 
        delete configWidget;
484
 
}
485
 
 
486
 
void ComponentChooser::load() {
487
 
        if( configWidget )
488
 
        {
489
 
                CfgPlugin * plugin = static_cast<CfgPlugin*>(
490
 
                                configWidget->qt_cast( "CfgPlugin" ) );
491
 
                if( plugin )
492
 
                {
493
 
                        KSimpleConfig cfg(latestEditedService);
494
 
                        plugin->load( &cfg );
495
 
                }
496
 
        }
497
 
}
498
 
 
499
 
void ComponentChooser::save() {
500
 
        if( configWidget )
501
 
        {
502
 
                CfgPlugin * plugin = static_cast<CfgPlugin*>(
503
 
                                configWidget->qt_cast( "CfgPlugin" ) );
504
 
                if( plugin )
505
 
                {
506
 
                        KSimpleConfig cfg(latestEditedService);
507
 
                        plugin->save( &cfg );
508
 
                }
509
 
        }
510
 
}
511
 
 
512
 
void ComponentChooser::restoreDefault() {
513
 
    if (configWidget)
514
 
    {
515
 
        static_cast<CfgPlugin*>(configWidget->qt_cast("CfgPlugin"))->defaults();
516
 
        emitChanged(true);
517
 
    }
518
 
 
519
 
/*
520
 
        txtEMailClient->setText("kmail");
521
 
        chkRunTerminal->setChecked(false);
522
 
 
523
 
        // Check if -e is needed, I do not think so
524
 
        terminalLE->setText("xterm");  //No need for i18n
525
 
        terminalCB->setChecked(true);
526
 
        emitChanged(false);
527
 
*/
528
 
}
529
 
 
530
 
// vim: sw=4 ts=4 noet