not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / screensaver / scrnsave.cpp
blobd4db3b17a1a48f9c91fcb0c960fdfa93d1d175fb
1 //-----------------------------------------------------------------------------
2 //
3 // KDE Display screen saver setup module
4 //
5 // Copyright (c) Martin R. Jones 1996,1999,2002
6 //
7 // Converted to a kcc module by Matthias Hoelzer 1997
8 //
11 #include <config-workspace.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <signal.h>
15 #include <unistd.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
20 #include <kservicetypetrader.h>
21 #include <kstandarddirs.h>
22 #include <QCheckBox>
23 #include <Qt3Support/Q3Header>
24 #include <QLabel>
25 #include <QLayout>
26 #include <Qt3Support/Q3CheckListItem>
27 #include <QPushButton>
28 #include <QSlider>
29 #include <QTimer>
30 #include <kmacroexpander.h>
31 #include <kshell.h>
33 //Added by qt3to4:
34 #include <QPixmap>
35 #include <QTextStream>
36 #include <QKeyEvent>
37 #include <QHBoxLayout>
38 #include <QBoxLayout>
39 #include <QVBoxLayout>
40 #include <QResizeEvent>
41 #include <QMouseEvent>
44 #include <QtDBus/QtDBus>
45 #include <kaboutdata.h>
46 #include <kapplication.h>
47 #include <kdebug.h>
48 #include <kdialog.h>
49 #include <kiconloader.h>
50 #include <knuminput.h>
51 #include <k3process.h>
52 #include <kservicegroup.h>
54 #include <X11/Xlib.h>
55 #include <fixx11h.h>
57 #include "scrnsave.h"
58 #include <QX11Info>
59 #include <QDesktopWidget>
60 #include <kscreensaver_interface.h>
61 #include <KPluginFactory>
62 #include <KPluginLoader>
63 #include <KMessageBox>
65 template class QList<SaverConfig*>;
67 const uint widgetEventMask = // X event mask
68 (uint)(
69 ExposureMask |
70 PropertyChangeMask |
71 StructureNotifyMask
74 //===========================================================================
75 // DLL Interface for kcontrol
76 K_PLUGIN_FACTORY(KSSFactory,
77 registerPlugin<KScreenSaver>(); // K_EXPORT_COMPONENT_FACTORY (screensaver
79 K_EXPORT_PLUGIN(KSSFactory("kcmscreensaver"))
82 static QString findExe(const QString &exe) {
83 QString result = KStandardDirs::locate("exe", exe);
84 if (result.isEmpty())
85 result = KStandardDirs::findExe(exe);
86 return result;
89 KScreenSaver::KScreenSaver(QWidget *parent, const QVariantList&)
90 : KCModule(KSSFactory::componentData(), parent)
92 mSetupProc = 0;
93 mPreviewProc = 0;
94 mTestWin = 0;
95 mTestProc = 0;
96 mPrevSelected = -2;
97 mMonitor = 0;
98 mTesting = false;
100 setQuickHelp( i18n("<h1>Screen Saver</h1> <p>This module allows you to enable and"
101 " configure a screen saver. Note that you can enable a screen saver"
102 " even if you have power saving features enabled for your display.</p>"
103 " <p>Besides providing an endless variety of entertainment and"
104 " preventing monitor burn-in, a screen saver also gives you a simple"
105 " way to lock your display if you are going to leave it unattended"
106 " for a while. If you want the screen saver to lock the session, make sure you enable"
107 " the \"Require password\" feature of the screen saver; if you do not, you can still"
108 " explicitly lock the session using the desktop's \"Lock Session\" action.</p>"));
110 setButtons( KCModule::Help | KCModule::Default | KCModule::Apply );
114 readSettings();
116 mSetupProc = new K3Process;
117 connect(mSetupProc, SIGNAL(processExited(K3Process *)),
118 this, SLOT(slotSetupDone(K3Process *)));
120 mPreviewProc = new K3Process;
121 connect(mPreviewProc, SIGNAL(processExited(K3Process *)),
122 this, SLOT(slotPreviewExited(K3Process *)));
124 QBoxLayout *topLayout = new QHBoxLayout(this);
125 topLayout->setMargin(0);
126 topLayout->setSpacing(KDialog::spacingHint());
128 // left column
129 QVBoxLayout *leftColumnLayout = new QVBoxLayout( );
130 topLayout->addItem( leftColumnLayout );
131 leftColumnLayout->setSpacing( KDialog::spacingHint() );
133 mSaverGroup = new QGroupBox(i18n("Screen Saver"), this );
134 QVBoxLayout *groupLayout = new QVBoxLayout( mSaverGroup );
135 leftColumnLayout->addWidget(mSaverGroup);
136 leftColumnLayout->setStretchFactor( mSaverGroup, 10 );
138 mSaverListView = new Q3ListView( mSaverGroup );
139 mSaverListView->setMinimumHeight( 120 );
140 mSaverListView->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
141 mSaverListView->addColumn("");
142 mSaverListView->header()->hide();
143 mSelected = -1;
144 groupLayout->addWidget( mSaverListView, 10 );
145 connect( mSaverListView, SIGNAL(doubleClicked ( Q3ListViewItem *)), this, SLOT( slotSetup()));
146 mSaverListView->setWhatsThis( i18n("Select the screen saver to use.") );
148 QBoxLayout* hlay = new QHBoxLayout();
149 groupLayout->addLayout(hlay);
150 mSetupBt = new QPushButton( i18n("&Setup..."), mSaverGroup );
151 connect( mSetupBt, SIGNAL( clicked() ), SLOT( slotSetup() ) );
152 mSetupBt->setEnabled(false);
153 hlay->addWidget( mSetupBt );
154 mSetupBt->setWhatsThis( i18n("Configure the screen saver's options, if any.") );
156 mTestBt = new QPushButton( i18n("&Test"), mSaverGroup );
157 connect( mTestBt, SIGNAL( clicked() ), SLOT( slotTest() ) );
158 mTestBt->setEnabled(false);
159 hlay->addWidget( mTestBt );
160 mTestBt->setWhatsThis( i18n("Show a full screen preview of the screen saver.") );
162 mSettingsGroup = new QGroupBox( i18n("Settings"), this );
163 groupLayout = new QVBoxLayout( mSettingsGroup );
164 leftColumnLayout->addWidget( mSettingsGroup );
166 mEnabledCheckBox = new QCheckBox(i18n(
167 "Start a&utomatically"), mSettingsGroup);
168 mEnabledCheckBox->setChecked(mEnabled);
169 mEnabledCheckBox->setWhatsThis( i18n(
170 "Automatically start the screen saver after a period of inactivity.") );
171 connect(mEnabledCheckBox, SIGNAL(toggled(bool)),
172 this, SLOT(slotEnable(bool)));
173 groupLayout->addWidget(mEnabledCheckBox);
175 QBoxLayout *hbox = new QHBoxLayout();
176 groupLayout->addLayout(hbox);
177 hbox->addSpacing(30);
178 mActivateLbl = new QLabel(i18n("After:"), mSettingsGroup);
179 mActivateLbl->setEnabled(mEnabled);
180 hbox->addWidget(mActivateLbl);
181 mWaitEdit = new QSpinBox(mSettingsGroup);
182 //mWaitEdit->setSteps(1, 10);
183 mWaitEdit->setRange(1, INT_MAX);
184 mWaitEdit->setSuffix(i18n(" min"));
185 mWaitEdit->setValue(mTimeout/60);
186 mWaitEdit->setEnabled(mEnabled);
187 connect(mWaitEdit, SIGNAL(valueChanged(int)),
188 this, SLOT(slotTimeoutChanged(int)));
189 mActivateLbl->setBuddy(mWaitEdit);
190 hbox->addWidget(mWaitEdit);
191 hbox->addStretch(1);
192 QString wtstr = i18n(
193 "The period of inactivity "
194 "after which the screen saver should start.");
195 mActivateLbl->setWhatsThis( wtstr );
196 mWaitEdit->setWhatsThis( wtstr );
198 mLockCheckBox = new QCheckBox( i18n(
199 "&Require password to stop"), mSettingsGroup );
200 mLockCheckBox->setEnabled( mEnabled );
201 mLockCheckBox->setChecked( mLock );
202 connect( mLockCheckBox, SIGNAL( toggled( bool ) ),
203 this, SLOT( slotLock( bool ) ) );
204 groupLayout->addWidget(mLockCheckBox);
205 mLockCheckBox->setWhatsThis( i18n(
206 "Prevent potential unauthorized use by requiring a password"
207 " to stop the screen saver.") );
208 hbox = new QHBoxLayout();
209 groupLayout->addLayout(hbox);
210 hbox->addSpacing(30);
211 mLockLbl = new QLabel(i18n("After:"), mSettingsGroup);
212 mLockLbl->setEnabled(mEnabled && mLock);
213 mLockLbl->setWhatsThis( i18n(
214 "The amount of time, after the screen saver has started, to ask for the unlock password.") );
215 hbox->addWidget(mLockLbl);
216 mWaitLockEdit = new QSpinBox(mSettingsGroup);
217 //mWaitLockEdit->setSteps(1, 10);
218 mWaitLockEdit->setRange(1, 300);
219 mWaitLockEdit->setSuffix(i18n(" sec"));
220 mWaitLockEdit->setValue(mLockTimeout/1000);
221 mWaitLockEdit->setEnabled(mEnabled && mLock);
222 if ( mWaitLockEdit->sizeHint().width() <
223 mWaitEdit->sizeHint().width() ) {
224 mWaitLockEdit->setFixedWidth( mWaitEdit->sizeHint().width() );
225 mWaitEdit->setFixedWidth( mWaitEdit->sizeHint().width() );
227 else {
228 mWaitEdit->setFixedWidth( mWaitLockEdit->sizeHint().width() );
229 mWaitLockEdit->setFixedWidth( mWaitLockEdit->sizeHint().width() );
231 connect(mWaitLockEdit, SIGNAL(valueChanged(int)),
232 this, SLOT(slotLockTimeoutChanged(int)));
233 mLockLbl->setBuddy(mWaitLockEdit);
234 hbox->addWidget(mWaitLockEdit);
235 hbox->addStretch(1);
236 QString wltstr = i18n(
237 "Choose the period "
238 "after which the display will be locked. ");
239 mLockLbl->setWhatsThis( wltstr );
240 mWaitLockEdit->setWhatsThis( wltstr );
242 mPlasmaCheckBox = new QCheckBox(i18n("Allow widgets on screen saver"), mSaverGroup);
243 mPlasmaCheckBox->setChecked(mPlasmaEnabled);
244 mPlasmaCheckBox->setWhatsThis(i18n("Add widgets to your screensaver"));
245 connect(mPlasmaCheckBox, SIGNAL(toggled(bool)), this, SLOT(slotEnablePlasma(bool)));
246 groupLayout->addWidget(mPlasmaCheckBox);
248 hbox = new QHBoxLayout();
249 groupLayout->addLayout(hbox);
250 hbox->addSpacing(30);
251 mPlasmaSetup = new QPushButton(i18n("Setup..."), mSaverGroup);
252 mPlasmaSetup->setEnabled(mPlasmaEnabled);
253 connect(mPlasmaSetup, SIGNAL(clicked()), this, SLOT(slotPlasmaSetup()));
254 hbox->addWidget(mPlasmaSetup);
255 hbox->addItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
257 // right column
258 QBoxLayout* rightColumnLayout = new QVBoxLayout();
259 topLayout->addItem( rightColumnLayout );
260 rightColumnLayout->setSpacing( KDialog::spacingHint() );
262 mMonitorLabel = new QLabel( this );
263 mMonitorLabel->setAlignment( Qt::AlignCenter );
264 mMonitorLabel->setPixmap( QPixmap(KStandardDirs::locate("data",
265 "kcontrol/pics/monitor.png")));
266 rightColumnLayout->addWidget(mMonitorLabel, 0);
267 mMonitorLabel->setWhatsThis( i18n("A preview of the selected screen saver.") );
269 QBoxLayout* advancedLayout = new QHBoxLayout();
270 rightColumnLayout->addItem( advancedLayout );
271 advancedLayout->setSpacing( 3 );
272 advancedLayout->addWidget( new QWidget( this ) );
273 QPushButton* advancedBt = new QPushButton(
274 i18n( "Advanced &Options" ), this );
275 advancedBt->setObjectName("advancedBtn");
276 advancedBt->setSizePolicy( QSizePolicy(
277 QSizePolicy::Fixed, QSizePolicy::Fixed) );
278 connect( advancedBt, SIGNAL( clicked() ),
279 this, SLOT( slotAdvanced() ) );
280 advancedLayout->addWidget( advancedBt );
281 advancedLayout->addWidget( new QWidget( this ) );
283 rightColumnLayout->addStretch();
285 if (mImmutable)
287 setButtons(buttons() & ~Default);
288 mSettingsGroup->setEnabled(false);
289 mSaverGroup->setEnabled(false);
292 // finding the savers can take some time, so defer loading until
293 // we've started up.
294 mNumLoaded = 0;
295 mLoadTimer = new QTimer( this );
296 connect( mLoadTimer, SIGNAL(timeout()), SLOT(findSavers()) );
297 mLoadTimer->start( 100 );
298 mChanged = false;
299 emit changed(false);
301 KAboutData *about =
302 new KAboutData(I18N_NOOP("kcmscreensaver"), 0, ki18n("KDE Screen Saver Control Module"),
303 0, KLocalizedString(), KAboutData::License_GPL,
304 ki18n("(c) 1997-2002 Martin R. Jones\n"
305 "(c) 2003-2004 Chris Howells"));
306 about->addAuthor(ki18n("Chris Howells"), KLocalizedString(), "howells@kde.org");
307 about->addAuthor(ki18n("Martin R. Jones"), KLocalizedString(), "jones@kde.org");
309 setAboutData( about );
313 //---------------------------------------------------------------------------
315 void KScreenSaver::resizeEvent( QResizeEvent * )
318 if (mMonitor)
320 mMonitor->setGeometry( (mMonitorLabel->width()-200)/2+23,
321 (mMonitorLabel->height()-186)/2+14, 151, 115 );
325 //---------------------------------------------------------------------------
327 void KScreenSaver::mousePressEvent( QMouseEvent *)
329 if ( mTesting )
330 slotStopTest();
333 //---------------------------------------------------------------------------
335 void KScreenSaver::keyPressEvent( QKeyEvent *)
337 if ( mTesting )
338 slotStopTest();
340 //---------------------------------------------------------------------------
342 KScreenSaver::~KScreenSaver()
344 if (mPreviewProc)
346 if (mPreviewProc->isRunning())
348 mPreviewProc->kill( );
349 mPreviewProc->wait( );
351 delete mPreviewProc;
354 delete mTestProc;
355 delete mSetupProc;
356 delete mTestWin;
358 qDeleteAll(mSaverList);
361 //---------------------------------------------------------------------------
363 void KScreenSaver::load()
365 readSettings();
367 //with the following line, the Test and Setup buttons are not enabled correctly
368 //if no saver was selected, the "Reset" and the "Enable screensaver", it is only called when starting and when pressing reset, aleXXX
369 // mSelected = -1;
370 int i = 0;
371 Q3ListViewItem *selectedItem = 0;
372 Q_FOREACH( SaverConfig* saver, mSaverList ){
373 if (saver->file() == mSaver)
375 selectedItem = mSaverListView->findItem ( saver->name(), 0 );
376 if (selectedItem) {
377 mSelected = i;
378 break;
381 i++;
383 if ( selectedItem )
385 mSaverListView->setSelected( selectedItem, true );
386 mSaverListView->setCurrentItem( selectedItem );
387 slotScreenSaver( selectedItem );
390 updateValues();
391 mChanged = false;
392 emit changed(false);
395 //------------------------------------------------------------After---------------
397 void KScreenSaver::readSettings()
399 KConfigGroup config( KSharedConfig::openConfig( "kscreensaverrc"), "ScreenSaver" );
401 mImmutable = config.isImmutable();
403 mEnabled = config.readEntry("Enabled", false);
404 mTimeout = config.readEntry("Timeout", 300);
405 mLockTimeout = config.readEntry("LockGrace", 60000);
406 mLock = config.readEntry("Lock", false);
407 mSaver = config.readEntry("Saver");
408 mPlasmaEnabled = config.readEntry("PlasmaEnabled", false);
410 if (mTimeout < 60) mTimeout = 60;
411 if (mLockTimeout < 0) mLockTimeout = 0;
412 if (mLockTimeout > 300000) mLockTimeout = 300000;
414 mChanged = false;
417 //---------------------------------------------------------------------------
419 void KScreenSaver::updateValues()
421 if (mEnabled)
423 mWaitEdit->setValue(mTimeout/60);
425 else
427 mWaitEdit->setValue(0);
430 mWaitLockEdit->setValue(mLockTimeout/1000);
431 mLockCheckBox->setChecked(mLock);
434 //---------------------------------------------------------------------------
436 void KScreenSaver::defaults()
438 if (mImmutable) return;
440 slotScreenSaver( 0 );
442 Q3ListViewItem *item = mSaverListView->firstChild();
443 if (item) {
444 mSaverListView->setSelected( item, true );
445 mSaverListView->setCurrentItem( item );
446 mSaverListView->ensureItemVisible( item );
448 slotTimeoutChanged( 5 );
449 slotLockTimeoutChanged( 60 );
450 slotLock( false );
451 mEnabledCheckBox->setChecked(false);
452 mPlasmaCheckBox->setChecked(false);
453 mPlasmaSetup->setEnabled(false);
455 updateValues();
457 emit changed(true);
460 //---------------------------------------------------------------------------
462 void KScreenSaver::save()
464 if ( !mChanged )
465 return;
467 KConfigGroup config(KSharedConfig::openConfig( "kscreensaverrc"), "ScreenSaver" );
469 config.writeEntry("Enabled", mEnabled);
470 config.writeEntry("Timeout", mTimeout);
471 config.writeEntry("LockGrace", mLockTimeout);
472 config.writeEntry("Lock", mLock);
473 config.writeEntry("PlasmaEnabled", mPlasmaEnabled);
475 if ( !mSaver.isEmpty() )
476 config.writeEntry("Saver", mSaver);
477 config.sync();
479 org::kde::screensaver kscreensaver("org.kde.screensaver", "/ScreenSaver", QDBusConnection::sessionBus());
480 kscreensaver.configure();
482 mChanged = false;
483 emit changed(false);
486 //---------------------------------------------------------------------------
488 void KScreenSaver::findSavers()
490 if ( !mNumLoaded ) {
491 mSaverServices = KServiceTypeTrader::self()->query( "ScreenSaver");
492 new Q3ListViewItem ( mSaverListView, i18n("Loading...") );
493 if ( mSaverServices.isEmpty() )
494 mLoadTimer->stop();
495 else
496 mLoadTimer->start( 50 );
498 for( KService::List::const_iterator it = mSaverServices.constBegin();
499 it != mSaverServices.constEnd(); it++,mNumLoaded++)
501 SaverConfig *saver = new SaverConfig;
502 QString file = KStandardDirs::locate("services", (*it)->entryPath());
503 if (saver->read(file)) {
504 mSaverList.append(saver);
505 } else
506 delete saver;
509 if ( mNumLoaded == mSaverServices.count() ) {
510 Q3ListViewItem *selectedItem = 0;
511 int categoryCount = 0;
512 int indx = 0;
514 mLoadTimer->stop();
515 delete mLoadTimer;
516 qSort(mSaverList.begin(), mSaverList.end());
518 mSelected = -1;
519 mSaverListView->clear();
520 Q_FOREACH( SaverConfig *s, mSaverList )
522 Q3ListViewItem *item;
523 if (s->category().isEmpty())
524 item = new Q3ListViewItem ( mSaverListView, s->name(), '2' + s->name() );
525 else
527 Q3ListViewItem *categoryItem = mSaverListView->findItem( s->category(), 0 );
528 if ( !categoryItem ) {
529 categoryItem = new Q3ListViewItem ( mSaverListView, s->category(), '1' + s->category() );
530 categoryItem->setPixmap ( 0, SmallIcon ( "preferences-desktop-screensaver" ) );
532 item = new Q3ListViewItem ( categoryItem, s->name(), s->name() );
533 categoryCount++;
535 if (s->file() == mSaver) {
536 mSelected = indx;
537 selectedItem = item;
539 indx++;
542 // Delete categories with only one item
543 Q3ListViewItemIterator it ( mSaverListView );
544 for ( ; it.current(); it++ )
545 if ( it.current()->childCount() == 1 ) {
546 Q3ListViewItem *item = it.current()->firstChild();
547 it.current()->takeItem( item );
548 mSaverListView->insertItem ( item );
549 delete it.current();
550 categoryCount--;
553 mSaverListView->setRootIsDecorated ( categoryCount > 0 );
554 mSaverListView->setSorting ( 1 );
556 if ( mSelected > -1 )
558 mSaverListView->setSelected(selectedItem, true);
559 mSaverListView->setCurrentItem(selectedItem);
560 mSaverListView->ensureItemVisible(selectedItem);
561 mSetupBt->setEnabled(!mSaverList.at(mSelected)->setup().isEmpty());
562 mTestBt->setEnabled(true);
565 connect( mSaverListView, SIGNAL( currentChanged( Q3ListViewItem * ) ),
566 this, SLOT( slotScreenSaver( Q3ListViewItem * ) ) );
568 setMonitor();
572 //---------------------------------------------------------------------------
574 void KScreenSaver::setMonitor()
576 if (mPreviewProc->isRunning())
577 // CC: this will automatically cause a "slotPreviewExited"
578 // when the viewer exits
579 mPreviewProc->kill();
580 else
581 slotPreviewExited(mPreviewProc);
584 //---------------------------------------------------------------------------
586 void KScreenSaver::slotPreviewExited(K3Process *)
588 // Ugly hack to prevent continual respawning of savers that crash
589 if (mSelected == mPrevSelected)
590 return;
592 if ( mSaverList.isEmpty() ) // safety check
593 return;
595 // Some xscreensaver hacks do something nasty to the window that
596 // requires a new one to be created (or proper investigation of the
597 // problem).
598 delete mMonitor;
600 mMonitor = new KSSMonitor(mMonitorLabel);
601 QPalette palette;
602 palette.setColor(mMonitor->backgroundRole(), Qt::black);
603 mMonitor->setPalette(palette);
604 mMonitor->setGeometry((mMonitorLabel->width()-200)/2+23,
605 (mMonitorLabel->height()-186)/2+14, 151, 115);
606 mMonitor->show();
607 // So that hacks can XSelectInput ButtonPressMask
608 XSelectInput(QX11Info::display(), mMonitor->winId(), widgetEventMask );
610 if (mSelected >= 0) {
611 mPreviewProc->clearArguments();
613 QString saver = mSaverList.at(mSelected)->saver();
614 QHash<QChar, QString> keyMap;
615 keyMap.insert('w', QString::number(mMonitor->winId()));
616 *mPreviewProc << KShell::splitArgs(KMacroExpander::expandMacrosShellQuote(saver, keyMap));
618 mPreviewProc->start();
621 mPrevSelected = mSelected;
624 //---------------------------------------------------------------------------
626 void KScreenSaver::slotEnable(bool e)
628 mEnabled = e;
629 mActivateLbl->setEnabled( e );
630 mWaitEdit->setEnabled( e );
631 mLockCheckBox->setEnabled( e );
632 mLockLbl->setEnabled( e && mLock );
633 mWaitLockEdit->setEnabled( e && mLock );
634 mChanged = true;
635 emit changed(true);
638 void KScreenSaver::slotEnablePlasma(bool enable)
640 mPlasmaEnabled = enable;
641 //FIXME even though the button's enabled, plasma isn't until the user hits apply
642 //so the button will just show the screensaver, no plasma.
643 //what should I do about this?
644 mPlasmaSetup->setEnabled(mPlasmaEnabled);
645 mChanged = true;
646 emit changed(true);
649 void KScreenSaver::slotPlasmaSetup()
651 org::kde::screensaver kscreensaver("org.kde.screensaver", "/ScreenSaver", QDBusConnection::sessionBus());
652 kscreensaver.setupPlasma();
656 //---------------------------------------------------------------------------
658 void KScreenSaver::slotScreenSaver(Q3ListViewItem *item)
660 if (!item)
661 return;
663 int i = 0, indx = -1;
664 Q_FOREACH( SaverConfig* saver , mSaverList ){
665 if ( item->parent() )
667 if ( item->parent()->text( 0 ) == saver->category() && saver->name() == item->text (0))
669 indx = i;
670 break;
673 else
675 if ( saver->name() == item->text (0) )
677 indx = i;
678 break;
681 i++;
683 if (indx == -1) {
684 mSelected = -1;
685 return;
688 bool bChanged = (indx != mSelected);
690 if (!mSetupProc->isRunning())
691 mSetupBt->setEnabled(!mSaverList.at(indx)->setup().isEmpty());
692 mTestBt->setEnabled(true);
693 mSaver = mSaverList.at(indx)->file();
695 mSelected = indx;
696 setMonitor();
697 if (bChanged)
699 mChanged = true;
700 emit changed(true);
704 //---------------------------------------------------------------------------
706 void KScreenSaver::slotSetup()
708 if ( mSelected < 0 )
709 return;
711 if (mSetupProc->isRunning())
712 return;
714 mSetupProc->clearArguments();
716 QString saver = mSaverList.at(mSelected)->setup();
717 if( saver.isEmpty())
718 return;
719 QTextStream ts(&saver, QIODevice::ReadOnly);
721 QString word;
722 ts >> word;
723 bool kxsconfig = word == "kxsconfig";
724 QString path = findExe(word);
726 if (!path.isEmpty())
728 (*mSetupProc) << path;
730 // Add caption and icon to about dialog
731 if (!kxsconfig) {
732 word = "-caption";
733 (*mSetupProc) << word;
734 word = mSaverList.at(mSelected)->name();
735 (*mSetupProc) << word;
736 word = "-icon";
737 (*mSetupProc) << word;
738 word = "kscreensaver";
739 (*mSetupProc) << word;
742 while (!ts.atEnd())
744 ts >> word;
745 (*mSetupProc) << word;
748 // Pass translated name to kxsconfig
749 if (kxsconfig) {
750 word = mSaverList.at(mSelected)->name();
751 (*mSetupProc) << word;
754 mSetupBt->setEnabled( false );
755 kapp->flush();
757 mSetupProc->start();
761 //---------------------------------------------------------------------------
763 void KScreenSaver::slotAdvanced()
765 KScreenSaverAdvancedDialog dlg( window() );
766 if ( dlg.exec() ) {
767 mChanged = true;
768 emit changed(true);
772 //---------------------------------------------------------------------------
774 void KScreenSaver::slotTest()
776 if ( mSelected == -1 )
777 return;
779 if (!mTestProc) {
780 mTestProc = new K3Process;
781 } else {
782 mPreviewProc->kill();
783 mPreviewProc->wait();
784 mTestProc->clearArguments();
787 if (!mTestWin)
789 mTestWin = new TestWin();
790 mTestWin->setAttribute(Qt::WA_NoSystemBackground, true);
791 mTestWin->setAttribute(Qt::WA_PaintOnScreen, true);
792 mTestWin->setGeometry(qApp->desktop()->geometry());
795 mTestWin->show();
796 mTestWin->raise();
797 mTestWin->setFocus();
798 // So that hacks can XSelectInput ButtonPressMask
799 XSelectInput(QX11Info::display(), mTestWin->winId(), widgetEventMask );
801 grabMouse();
802 grabKeyboard();
804 mTestBt->setEnabled( false );
806 QString saver = mSaverList.at(mSelected)->saver();
807 QHash<QChar, QString> keyMap;
808 keyMap.insert('w', QString::number(mTestWin->winId()));
809 *mTestProc << KShell::splitArgs(KMacroExpander::expandMacrosShellQuote(saver, keyMap));
811 mTestProc->start(K3Process::NotifyOnExit);
813 mTesting = true;
816 //---------------------------------------------------------------------------
818 void KScreenSaver::slotStopTest()
820 if (mTestProc->isRunning()) {
821 mTestProc->kill();
822 mTestProc->wait();
824 releaseMouse();
825 releaseKeyboard();
826 mTestWin->hide();
827 mTestBt->setEnabled(true);
828 mPrevSelected = -1;
829 setMonitor();
830 mTesting = false;
833 //---------------------------------------------------------------------------
835 void KScreenSaver::slotTimeoutChanged(int to )
837 mTimeout = to * 60;
838 mChanged = true;
839 emit changed(true);
842 //-----------------------------------------------------------------------
844 void KScreenSaver::slotLockTimeoutChanged(int to )
846 mLockTimeout = to * 1000;
847 mChanged = true;
848 emit changed(true);
852 //---------------------------------------------------------------------------
854 void KScreenSaver::slotLock( bool l )
856 mLock = l;
857 mLockLbl->setEnabled( l );
858 mWaitLockEdit->setEnabled( l );
859 mChanged = true;
860 emit changed(true);
863 //---------------------------------------------------------------------------
865 void KScreenSaver::slotSetupDone(K3Process *)
867 mPrevSelected = -1; // see ugly hack in slotPreviewExited()
868 setMonitor();
869 mSetupBt->setEnabled( true );
870 emit changed(true);
873 #include "scrnsave.moc"