00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <dcopclient.h>
00020
00021 #include <kaboutdata.h>
00022 #include <kapplication.h>
00023 #include <kaudioplayer.h>
00024 #include <kcombobox.h>
00025 #include <kconfig.h>
00026 #include <kcursor.h>
00027 #include <kdebug.h>
00028 #include <kfiledialog.h>
00029 #include <kiconloader.h>
00030 #include <kicontheme.h>
00031 #include <klineedit.h>
00032 #include <klocale.h>
00033 #include <kmessagebox.h>
00034 #include <knotifyclient.h>
00035 #include <knotifydialog.h>
00036 #include <kstandarddirs.h>
00037 #include <kurlrequester.h>
00038
00039 #include <qcheckbox.h>
00040 #include <qgroupbox.h>
00041 #include <qheader.h>
00042 #include <qlabel.h>
00043 #include <qlistview.h>
00044 #include <qlayout.h>
00045 #include <qptrlist.h>
00046 #include <qpushbutton.h>
00047 #include <qstring.h>
00048 #include <qtooltip.h>
00049 #include <qtimer.h>
00050 #include <qvbox.h>
00051
00052 using namespace KNotify;
00053
00054
00055
00056
00057
00058 namespace KNotify
00059 {
00060 class SelectionCombo
00061 {
00062 public:
00063
00064
00065
00066 static void fill( KComboBox *combo )
00067 {
00068 combo->insertItem( i18n("Sounds") );
00069 combo->insertItem( i18n("Logging") );
00070 combo->insertItem( i18n("Program Execution") );
00071 combo->insertItem( i18n("Message Windows") );
00072 combo->insertItem( i18n("Passive Windows") );
00073 combo->insertItem( i18n("Standard Error Output") );
00074 }
00075
00076 static int type( KComboBox *combo )
00077 {
00078 switch( combo->currentItem() )
00079 {
00080 case 0:
00081 return KNotifyClient::Sound;
00082 case 1:
00083 return KNotifyClient::Logfile;
00084 case 2:
00085 return KNotifyClient::Execute;
00086 case 3:
00087 return KNotifyClient::Messagebox;
00088 case 4:
00089 return KNotifyClient::PassivePopup;
00090 case 5:
00091 return KNotifyClient::Stderr;
00092 }
00093
00094 return KNotifyClient::None;
00095 }
00096 };
00097 };
00098
00099
00100 int KNotifyDialog::configure( QWidget *parent, const char *name,
00101 const KAboutData *aboutData )
00102 {
00103 KNotifyDialog dialog( parent, name, true, aboutData );
00104 return dialog.exec();
00105 }
00106
00107 KNotifyDialog::KNotifyDialog( QWidget *parent, const char *name, bool modal,
00108 const KAboutData *aboutData )
00109 : KDialogBase(parent, name, modal, i18n("Notification Settings"),
00110 Ok | Apply | Cancel | Default, Ok, true )
00111 {
00112 QVBox *box = makeVBoxMainWidget();
00113
00114 m_notifyWidget = new KNotifyWidget( box, "knotify widget" );
00115
00116 if ( aboutData )
00117 addApplicationEvents( aboutData->appName() );
00118
00119 connect( this, SIGNAL( okClicked() ), m_notifyWidget, SLOT( save() ));
00120 connect( this, SIGNAL( applyClicked() ), m_notifyWidget, SLOT( save() ));
00121 };
00122
00123 KNotifyDialog::~KNotifyDialog()
00124 {
00125 }
00126
00127 void KNotifyDialog::addApplicationEvents( const char *appName )
00128 {
00129 addApplicationEvents( QString::fromUtf8( appName ) +
00130 QString::fromLatin1( "/eventsrc" ) );
00131 }
00132
00133 void KNotifyDialog::addApplicationEvents( const QString& path )
00134 {
00135 Application *app = m_notifyWidget->addApplicationEvents( path );
00136 if ( app )
00137 {
00138 m_notifyWidget->addVisibleApp( app );
00139 m_notifyWidget->sort();
00140 }
00141 }
00142
00143 void KNotifyDialog::clearApplicationEvents()
00144 {
00145 m_notifyWidget->clear();
00146 }
00147
00148 void KNotifyDialog::slotDefault()
00149 {
00150 m_notifyWidget->resetDefaults( true );
00151 }
00152
00153
00156
00157
00158 #define COL_EXECUTE 0
00159 #define COL_STDERR 1
00160 #define COL_MESSAGE 2
00161 #define COL_LOGFILE 3
00162 #define COL_SOUND 4
00163 #define COL_EVENT 5
00164
00165 class KNotifyWidget::Private
00166 {
00167 public:
00168 QPixmap pixmaps[5];
00169 };
00170
00171
00172 KNotifyWidget::KNotifyWidget( QWidget *parent, const char *name,
00173 bool handleAllApps )
00174 : KNotifyWidgetBase( parent, name ? name : "KNotifyWidget" )
00175 {
00176 d = new Private;
00177
00178 m_allApps.setAutoDelete( true );
00179
00180 layout()->setMargin( 0 );
00181 layout()->setSpacing( KDialogBase::spacingHint() );
00182
00183 if ( !handleAllApps )
00184 {
00185 m_affectAllApps->hide();
00186 m_playerButton->hide();
00187 }
00188
00189 SelectionCombo::fill( m_comboEnable );
00190 SelectionCombo::fill( m_comboDisable );
00191
00192 m_listview->setFullWidth( true );
00193 m_listview->setAllColumnsShowFocus( true );
00194
00195 QPixmap pexec = SmallIcon("exec");
00196 QPixmap pstderr = SmallIcon("terminal");
00197 QPixmap pmessage = SmallIcon("info");
00198 QPixmap plogfile = SmallIcon("log");
00199 QPixmap psound = SmallIcon("sound");
00200
00201 d->pixmaps[COL_EXECUTE] = pexec;
00202 d->pixmaps[COL_STDERR] = pstderr;
00203 d->pixmaps[COL_MESSAGE] = pmessage;
00204 d->pixmaps[COL_LOGFILE] = plogfile;
00205 d->pixmaps[COL_SOUND] = psound;
00206
00207 int w = KIcon::SizeSmall + 6;
00208
00209 QHeader *header = m_listview->header();
00210 header->setLabel( COL_EXECUTE, pexec, QString::null, w );
00211 header->setLabel( COL_STDERR, pstderr, QString::null, w );
00212 header->setLabel( COL_MESSAGE, pmessage, QString::null, w );
00213 header->setLabel( COL_LOGFILE, plogfile, QString::null, w );
00214 header->setLabel( COL_SOUND, psound, QString::null, w );
00215
00216 m_playButton->setPixmap( SmallIcon( "1rightarrow" ) );
00217 connect( m_playButton, SIGNAL( clicked() ), SLOT( playSound() ));
00218
00219 connect( m_listview, SIGNAL( currentChanged( QListViewItem * ) ),
00220 SLOT( slotEventChanged( QListViewItem * ) ));
00221
00222 connect( m_playSound, SIGNAL( toggled( bool )),
00223 SLOT( soundToggled( bool )) );
00224 connect( m_logToFile, SIGNAL( toggled( bool )),
00225 SLOT( loggingToggled( bool )) );
00226 connect( m_execute, SIGNAL( toggled( bool )),
00227 SLOT( executeToggled( bool )) );
00228 connect( m_messageBox, SIGNAL( toggled( bool )),
00229 SLOT( messageBoxChanged() ) );
00230 connect( m_passivePopup, SIGNAL( toggled( bool )),
00231 SLOT( messageBoxChanged() ) );
00232 connect( m_stderr, SIGNAL( toggled( bool )),
00233 SLOT( stderrToggled( bool ) ) );
00234
00235 connect( m_soundPath, SIGNAL( textChanged( const QString& )),
00236 SLOT( soundFileChanged( const QString& )));
00237 connect( m_logfilePath, SIGNAL( textChanged( const QString& )),
00238 SLOT( logfileChanged( const QString& ) ));
00239 connect( m_executePath, SIGNAL( textChanged( const QString& )),
00240 SLOT( commandlineChanged( const QString& ) ));
00241
00242 connect( m_soundPath, SIGNAL( openFileDialog( KURLRequester * )),
00243 SLOT( openSoundDialog( KURLRequester * )));
00244 connect( m_logfilePath, SIGNAL( openFileDialog( KURLRequester * )),
00245 SLOT( openLogDialog( KURLRequester * )));
00246 connect( m_executePath, SIGNAL( openFileDialog( KURLRequester * )),
00247 SLOT( openExecDialog( KURLRequester * )));
00248
00249 connect( m_extension, SIGNAL( clicked() ),
00250 SLOT( toggleAdvanced()) );
00251
00252 connect( m_buttonEnable, SIGNAL( clicked() ), SLOT( enableAll() ));
00253 connect( m_buttonDisable, SIGNAL( clicked() ), SLOT( enableAll() ));
00254
00255 showAdvanced( false );
00256
00257 slotEventChanged( 0L );
00258 }
00259
00260 KNotifyWidget::~KNotifyWidget()
00261 {
00262 delete d;
00263 }
00264
00265 void KNotifyWidget::toggleAdvanced()
00266 {
00267 showAdvanced( m_logToFile->isHidden() );
00268 }
00269
00270 void KNotifyWidget::showAdvanced( bool show )
00271 {
00272 if ( show )
00273 {
00274 m_extension->setText( i18n("Fewer Op&tions") );
00275 QToolTip::add( m_extension, i18n("Hide advanced options") );
00276
00277 m_logToFile->show();
00278 m_logfilePath->show();
00279 m_execute->show();
00280 m_executePath->show();
00281 m_messageBox->show();
00282 m_passivePopup->show();
00283 m_stderr->show();
00284 m_controlsBox->show();
00285
00286 m_actionsBoxLayout->setSpacing( KDialog::spacingHint() );
00287 }
00288 else
00289 {
00290 m_extension->setText( i18n("More Op&tions") );
00291 QToolTip::add( m_extension, i18n("Show advanced options") );
00292
00293 m_logToFile->hide();
00294 m_logfilePath->hide();
00295 m_execute->hide();
00296 m_executePath->hide();
00297 m_messageBox->hide();
00298 m_passivePopup->hide();
00299 m_stderr->hide();
00300 m_controlsBox->hide();
00301
00302 m_actionsBoxLayout->setSpacing( 0 );
00303 }
00304 }
00305
00306 Application * KNotifyWidget::addApplicationEvents( const QString& path )
00307 {
00308 kdDebug() << "**** knotify: adding path: " << path << endl;
00309 QString relativePath = path;
00310
00311 if ( path.at(0) == '/' && KStandardDirs::exists( path ) )
00312 relativePath = makeRelative( path );
00313
00314 if ( !relativePath.isEmpty() )
00315 {
00316 Application *app = new Application( relativePath );
00317 m_allApps.append( app );
00318 return app;
00319 }
00320
00321 return 0L;
00322 }
00323
00324 void KNotifyWidget::clear()
00325 {
00326 clearVisible();
00327 m_allApps.clear();
00328 }
00329
00330 void KNotifyWidget::clearVisible()
00331 {
00332 m_visibleApps.clear();
00333 m_listview->clear();
00334 slotEventChanged( 0L );
00335 }
00336
00337 void KNotifyWidget::showEvent( QShowEvent *e )
00338 {
00339 selectItem( m_listview->firstChild() );
00340 KNotifyWidgetBase::showEvent( e );
00341 }
00342
00343 void KNotifyWidget::slotEventChanged( QListViewItem *item )
00344 {
00345 bool on = (item != 0L);
00346
00347 m_actionsBox->setEnabled( on );
00348 m_controlsBox->setEnabled( on );
00349
00350 if ( !on )
00351 return;
00352
00353 ListViewItem *lit = static_cast<ListViewItem*>( item );
00354 updateWidgets( lit );
00355 }
00356
00357 void KNotifyWidget::updateWidgets( ListViewItem *item )
00358 {
00359 bool enable;
00360 bool checked;
00361
00362 blockSignals( true );
00363
00364 const Event& event = item->event();
00365
00366
00367 m_playButton->setEnabled( !event.soundfile.isEmpty() );
00368 m_soundPath->setURL( event.soundfile );
00369 enable = (event.dontShow & KNotifyClient::Sound) == 0;
00370 checked = enable && !event.soundfile.isEmpty() &&
00371 (event.presentation & KNotifyClient::Sound);
00372 m_playSound->setEnabled( enable );
00373 m_playSound->setChecked( checked );
00374 m_soundPath->setEnabled( checked );
00375
00376
00377
00378 m_logfilePath->setURL( event.logfile );
00379 enable = (event.dontShow & KNotifyClient::Logfile) == 0;
00380 checked = enable && !event.logfile.isEmpty() &&
00381 (event.presentation & KNotifyClient::Logfile);
00382 m_logToFile->setEnabled( enable );
00383 m_logToFile->setChecked( checked );
00384 m_logfilePath->setEnabled( checked );
00385
00386
00387
00388 m_executePath->setURL( event.commandline );
00389 enable = (event.dontShow & KNotifyClient::Execute) == 0;
00390 checked = enable && !event.commandline.isEmpty() &&
00391 (event.presentation & KNotifyClient::Execute);
00392 m_execute->setEnabled( enable );
00393 m_execute->setChecked( checked );
00394 m_executePath->setEnabled( checked );
00395
00396
00397
00398 m_messageBox->setChecked(event.presentation & (KNotifyClient::Messagebox | KNotifyClient::PassivePopup));
00399 m_passivePopup->setChecked(event.presentation & KNotifyClient::PassivePopup);
00400 m_stderr->setChecked( event.presentation & KNotifyClient::Stderr );
00401
00402 updatePixmaps( item );
00403
00404 blockSignals( false );
00405 }
00406
00407 void KNotifyWidget::updatePixmaps( ListViewItem *item )
00408 {
00409 QPixmap emptyPix;
00410 Event &event = item->event();
00411
00412 bool doIt = (event.presentation & KNotifyClient::Execute) &&
00413 !event.commandline.isEmpty();
00414 item->setPixmap( COL_EXECUTE, doIt ? d->pixmaps[COL_EXECUTE] : emptyPix );
00415
00416 doIt = (event.presentation & KNotifyClient::Sound) &&
00417 !event.soundfile.isEmpty();
00418 item->setPixmap( COL_SOUND, doIt ? d->pixmaps[COL_SOUND] : emptyPix );
00419
00420 doIt = (event.presentation & KNotifyClient::Logfile) &&
00421 !event.logfile.isEmpty();
00422 item->setPixmap( COL_LOGFILE, doIt ? d->pixmaps[COL_LOGFILE] : emptyPix );
00423
00424 item->setPixmap( COL_MESSAGE,
00425 (event.presentation &
00426 (KNotifyClient::Messagebox | KNotifyClient::PassivePopup)) ?
00427 d->pixmaps[COL_MESSAGE] : emptyPix );
00428
00429 item->setPixmap( COL_STDERR,
00430 (event.presentation & KNotifyClient::Stderr) ?
00431 d->pixmaps[COL_STDERR] : emptyPix );
00432 }
00433
00434 void KNotifyWidget::addVisibleApp( Application *app )
00435 {
00436 if ( !app || (m_visibleApps.findRef( app ) != -1) )
00437 return;
00438
00439 m_visibleApps.append( app );
00440 addToView( app->eventList() );
00441
00442 QListViewItem *item = m_listview->selectedItem();
00443 if ( !item )
00444 item = m_listview->firstChild();
00445
00446 selectItem( item );
00447 }
00448
00449 void KNotifyWidget::addToView( const EventList& events )
00450 {
00451 ListViewItem *item = 0L;
00452
00453 EventListIterator it( events );
00454
00455 for ( ; it.current(); ++it )
00456 {
00457 Event *event = it.current();
00458 item = new ListViewItem( m_listview, event );
00459
00460 if ( (event->presentation & KNotifyClient::Execute) &&
00461 !event->commandline.isEmpty() )
00462 item->setPixmap( COL_EXECUTE, d->pixmaps[COL_EXECUTE] );
00463 if ( (event->presentation & KNotifyClient::Sound) &&
00464 !event->soundfile.isEmpty() )
00465 item->setPixmap( COL_SOUND, d->pixmaps[COL_SOUND] );
00466 if ( (event->presentation & KNotifyClient::Logfile) &&
00467 !event->logfile.isEmpty() )
00468 item->setPixmap( COL_LOGFILE, d->pixmaps[COL_LOGFILE] );
00469 if ( event->presentation & (KNotifyClient::Messagebox|KNotifyClient::PassivePopup) )
00470 item->setPixmap( COL_MESSAGE, d->pixmaps[COL_MESSAGE] );
00471 if ( event->presentation & KNotifyClient::Stderr )
00472 item->setPixmap( COL_STDERR, d->pixmaps[COL_STDERR] );
00473 }
00474 }
00475
00476 void KNotifyWidget::widgetChanged( QListViewItem *item,
00477 int what, bool on, QWidget *buddy )
00478 {
00479 if ( signalsBlocked() )
00480 return;
00481
00482 if ( buddy )
00483 buddy->setEnabled( on );
00484
00485 Event &e = static_cast<ListViewItem*>( item )->event();
00486 if ( on )
00487 {
00488 e.presentation |= what;
00489 if ( buddy )
00490 buddy->setFocus();
00491 }
00492 else
00493 e.presentation &= ~what;
00494
00495 emit changed( true );
00496 }
00497
00498 void KNotifyWidget::soundToggled( bool on )
00499 {
00500 QListViewItem *item = m_listview->currentItem();
00501 if ( !item )
00502 return;
00503 bool doIcon = on && !m_soundPath->url().isEmpty();
00504 item->setPixmap( COL_SOUND, doIcon ? d->pixmaps[COL_SOUND] : QPixmap() );
00505 widgetChanged( item, KNotifyClient::Sound, on, m_soundPath );
00506 }
00507
00508 void KNotifyWidget::loggingToggled( bool on )
00509 {
00510 QListViewItem *item = m_listview->currentItem();
00511 if ( !item )
00512 return;
00513 bool doIcon = on && !m_logfilePath->url().isEmpty();
00514 item->setPixmap(COL_LOGFILE, doIcon ? d->pixmaps[COL_LOGFILE] : QPixmap());
00515 widgetChanged( item, KNotifyClient::Logfile, on, m_logfilePath );
00516 }
00517
00518 void KNotifyWidget::executeToggled( bool on )
00519 {
00520 QListViewItem *item = m_listview->currentItem();
00521 if ( !item )
00522 return;
00523 bool doIcon = on && !m_executePath->url().isEmpty();
00524 item->setPixmap(COL_EXECUTE, doIcon ? d->pixmaps[COL_EXECUTE] : QPixmap());
00525 widgetChanged( item, KNotifyClient::Execute, on, m_executePath );
00526 }
00527
00528 void KNotifyWidget::messageBoxChanged()
00529 {
00530 if ( signalsBlocked() )
00531 return;
00532
00533 m_passivePopup->setEnabled( m_messageBox->isChecked() );
00534
00535 QListViewItem *item = m_listview->currentItem();
00536 if ( !item )
00537 return;
00538
00539 bool on = m_passivePopup->isEnabled();
00540 item->setPixmap( COL_MESSAGE, on ? d->pixmaps[COL_MESSAGE] : QPixmap() );
00541
00542 Event &e = static_cast<ListViewItem*>( item )->event();
00543
00544 if ( m_messageBox->isChecked() ) {
00545 if ( m_passivePopup->isChecked() ) {
00546 e.presentation |= KNotifyClient::PassivePopup;
00547 e.presentation &= ~KNotifyClient::Messagebox;
00548 }
00549 else {
00550 e.presentation &= ~KNotifyClient::PassivePopup;
00551 e.presentation |= KNotifyClient::Messagebox;
00552 }
00553 }
00554 else {
00555 e.presentation &= ~KNotifyClient::Messagebox;
00556 e.presentation &= ~KNotifyClient::PassivePopup;
00557 }
00558
00559 emit changed( true );
00560 }
00561
00562 void KNotifyWidget::stderrToggled( bool on )
00563 {
00564 QListViewItem *item = m_listview->currentItem();
00565 if ( !item )
00566 return;
00567 item->setPixmap( COL_STDERR, on ? d->pixmaps[COL_STDERR] : QPixmap() );
00568 widgetChanged( item, KNotifyClient::Stderr, on );
00569 }
00570
00571 void KNotifyWidget::soundFileChanged( const QString& text )
00572 {
00573 if ( signalsBlocked() )
00574 return;
00575
00576 QListViewItem *item = m_listview->currentItem();
00577 if ( !item )
00578 return;
00579
00580 m_playButton->setEnabled( !text.isEmpty() );
00581
00582 currentEvent()->soundfile = text;
00583 bool ok = !text.isEmpty() && m_playSound->isChecked();
00584 item->setPixmap( COL_SOUND, ok ? d->pixmaps[COL_SOUND] : QPixmap() );
00585
00586 emit changed( true );
00587 }
00588
00589 void KNotifyWidget::logfileChanged( const QString& text )
00590 {
00591 if ( signalsBlocked() )
00592 return;
00593
00594 QListViewItem *item = m_listview->currentItem();
00595 if ( !item )
00596 return;
00597
00598 currentEvent()->logfile = text;
00599 bool ok = !text.isEmpty() && m_logToFile->isChecked();
00600 item->setPixmap( COL_LOGFILE, ok ? d->pixmaps[COL_LOGFILE] : QPixmap() );
00601
00602 emit changed( true );
00603 }
00604
00605 void KNotifyWidget::commandlineChanged( const QString& text )
00606 {
00607 if ( signalsBlocked() )
00608 return;
00609
00610 QListViewItem *item = m_listview->currentItem();
00611 if ( !item )
00612 return;
00613
00614 currentEvent()->commandline = text;
00615 bool ok = !text.isEmpty() && m_execute->isChecked();
00616 item->setPixmap( COL_EXECUTE, ok ? d->pixmaps[COL_EXECUTE] : QPixmap() );
00617
00618 emit changed( true );
00619 }
00620
00621 void KNotifyWidget::sort( bool ascending )
00622 {
00623 m_listview->setSorting( COL_EVENT, ascending );
00624 m_listview->sort();
00625 }
00626
00627 void KNotifyWidget::selectItem( QListViewItem *item )
00628 {
00629 if ( item )
00630 {
00631 m_listview->setCurrentItem( item );
00632 item->setSelected( true );
00633 slotEventChanged( item );
00634 }
00635 }
00636
00637 void KNotifyWidget::resetDefaults( bool ask )
00638 {
00639 if ( ask )
00640 {
00641 if ( KMessageBox::warningContinueCancel(this,
00642 i18n("This will cause the notifications "
00643 "to be reset to their defaults!"),
00644 i18n("Are you sure?"),
00645 i18n("Continue"))
00646 != KMessageBox::Continue)
00647 return;
00648 }
00649
00650 reload( true );
00651 emit changed( true );
00652 }
00653
00654 void KNotifyWidget::reload( bool revertToDefaults )
00655 {
00656 m_listview->clear();
00657 ApplicationListIterator it( m_visibleApps );
00658 for ( ; it.current(); ++it )
00659 {
00660 it.current()->reloadEvents( revertToDefaults );
00661 addToView( it.current()->eventList() );
00662 }
00663
00664 m_listview->sort();
00665 selectItem( m_listview->firstChild() );
00666 }
00667
00668 void KNotifyWidget::save()
00669 {
00670 kdDebug() << "save\n";
00671
00672 ApplicationListIterator it( m_allApps );
00673 while ( it.current() )
00674 {
00675 (*it)->save();
00676 ++it;
00677 }
00678
00679 if ( kapp )
00680 {
00681 if ( !kapp->dcopClient()->isAttached() )
00682 kapp->dcopClient()->attach();
00683 kapp->dcopClient()->send("knotify", "", "reconfigure()", "");
00684 }
00685
00686 emit changed( false );
00687 }
00688
00689
00690
00691 QString KNotifyWidget::makeRelative( const QString& fullPath )
00692 {
00693 int slash = fullPath.findRev( '/' ) - 1;
00694 slash = fullPath.findRev( '/', slash );
00695
00696 if ( slash < 0 )
00697 return QString::null;
00698
00699 return fullPath.mid( slash+1 );
00700 }
00701
00702 Event * KNotifyWidget::currentEvent()
00703 {
00704 QListViewItem *current = m_listview->currentItem();
00705 if ( !current )
00706 return 0L;
00707
00708 return &static_cast<ListViewItem*>( current )->event();
00709 }
00710
00711 void KNotifyWidget::openSoundDialog( KURLRequester *requester )
00712 {
00713 static bool init = true;
00714 if ( !init )
00715 return;
00716
00717 init = false;
00718
00719 KFileDialog *fileDialog = requester->fileDialog();
00720 fileDialog->setCaption( i18n("Select Sound File") );
00721 QStringList filters;
00722 filters << "audio/x-wav" << "audio/x-mp3" << "application/x-ogg"
00723 << "audio/x-adpcm";
00724 fileDialog->setMimeFilter( filters );
00725
00726
00727 const Application *app = currentEvent()->application();
00728 QStringList soundDirs =
00729 KGlobal::dirs()->findDirs("data", app->appName() + "/sounds");
00730 soundDirs += KGlobal::dirs()->resourceDirs( "sound" );
00731
00732 if ( !soundDirs.isEmpty() ) {
00733 KURL soundURL;
00734 QDir dir;
00735 dir.setFilter( QDir::Files | QDir::Readable );
00736 QStringList::ConstIterator it = soundDirs.begin();
00737 while ( it != soundDirs.end() ) {
00738 dir = *it;
00739 if ( dir.isReadable() && dir.count() > 2 ) {
00740 soundURL.setPath( *it );
00741 fileDialog->setURL( soundURL );
00742 break;
00743 }
00744 ++it;
00745 }
00746 }
00747 }
00748
00749 void KNotifyWidget::openLogDialog( KURLRequester *requester )
00750 {
00751 static bool init = true;
00752 if ( !init )
00753 return;
00754
00755 init = false;
00756
00757 KFileDialog *fileDialog = requester->fileDialog();
00758 fileDialog->setCaption( i18n("Select Log File") );
00759 QStringList filters;
00760 filters << "text/x-log" << "text/plain";
00761 fileDialog->setMimeFilter( filters );
00762 }
00763
00764 void KNotifyWidget::openExecDialog( KURLRequester *requester )
00765 {
00766 static bool init = true;
00767 if ( !init )
00768 return;
00769
00770 init = false;
00771
00772 KFileDialog *fileDialog = requester->fileDialog();
00773 fileDialog->setCaption( i18n("Select File to Execute") );
00774 QStringList filters;
00775 filters << "application/x-executable" << "application/x-shellscript"
00776 << "application/x-perl" << "application/x-python";
00777 fileDialog->setMimeFilter( filters );
00778 }
00779
00780 void KNotifyWidget::playSound()
00781 {
00782 KAudioPlayer::play( m_soundPath->url() );
00783 }
00784
00785 void KNotifyWidget::enableAll()
00786 {
00787 bool enable = (sender() == m_buttonEnable);
00788 enableAll( SelectionCombo::type(enable ? m_comboEnable : m_comboDisable),
00789 enable );
00790 }
00791
00792 void KNotifyWidget::enableAll( int what, bool enable )
00793 {
00794 if ( m_listview->childCount() == 0 )
00795 return;
00796
00797 bool affectAll = m_affectAllApps->isChecked();
00798
00799 ApplicationListIterator appIt( affectAll ? m_allApps : m_visibleApps );
00800 for ( ; appIt.current(); ++appIt )
00801 {
00802 const EventList& events = appIt.current()->eventList();
00803 EventListIterator it( events );
00804 for ( ; it.current(); ++it )
00805 {
00806 if ( enable )
00807 it.current()->presentation |= what;
00808 else
00809 it.current()->presentation &= ~what;
00810 }
00811 }
00812
00813
00814 QListViewItemIterator it( m_listview->firstChild() );
00815 for ( ; it.current(); ++it )
00816 {
00817 ListViewItem *item = static_cast<ListViewItem*>( it.current() );
00818 updatePixmaps( item );
00819 }
00820
00821 QListViewItem *item = m_listview->currentItem();
00822 if ( !item )
00823 item = m_listview->firstChild();
00824 selectItem( item );
00825
00826 emit changed( true );
00827 }
00828
00829
00832
00833
00834
00835
00836
00837 Application::Application( const QString &path )
00838 {
00839 QString config_file = path;
00840 config_file[config_file.find('/')] = '.';
00841 m_events = 0L;
00842 config = new KConfig(config_file, false, false);
00843 kc = new KConfig(path, true, false, "data");
00844 kc->setGroup( QString::fromLatin1("!Global!") );
00845 m_icon = kc->readEntry(QString::fromLatin1("IconName"),
00846 QString::fromLatin1("misc"));
00847 m_description = kc->readEntry( QString::fromLatin1("Comment"),
00848 i18n("No description available") );
00849
00850 int index = path.find( '/' );
00851 if ( index >= 0 )
00852 m_appname = path.left( index );
00853 else
00854 kdDebug() << "Cannot determine application name from path: " << path << endl;
00855 }
00856
00857 Application::~Application()
00858 {
00859 delete config;
00860 delete kc;
00861 delete m_events;
00862 }
00863
00864
00865 const EventList& Application::eventList()
00866 {
00867 if ( !m_events ) {
00868 m_events = new EventList;
00869 m_events->setAutoDelete( true );
00870 reloadEvents();
00871 }
00872
00873 return *m_events;
00874 }
00875
00876
00877 void Application::save()
00878 {
00879 if ( !m_events )
00880 return;
00881
00882 EventListIterator it( *m_events );
00883 Event *e;
00884 while ( (e = it.current()) ) {
00885 config->setGroup( e->configGroup );
00886 config->writeEntry( "presentation", e->presentation );
00887 config->writeEntry( "soundfile", e->soundfile );
00888 config->writeEntry( "logfile", e->logfile );
00889 config->writeEntry( "commandline", e->commandline );
00890
00891 ++it;
00892 }
00893 config->sync();
00894 }
00895
00896
00897 void Application::reloadEvents( bool revertToDefaults )
00898 {
00899 if ( m_events )
00900 m_events->clear();
00901 else
00902 {
00903 m_events = new EventList;
00904 m_events->setAutoDelete( true );
00905 }
00906
00907 Event *e = 0L;
00908
00909 QString global = QString::fromLatin1("!Global!");
00910 QString default_group = QString::fromLatin1("<default>");
00911 QString name = QString::fromLatin1("Name");
00912 QString comment = QString::fromLatin1("Comment");
00913
00914 QStringList conflist = kc->groupList();
00915 QStringList::ConstIterator it = conflist.begin();
00916
00917 while ( it != conflist.end() ) {
00918 if ( (*it) != global && (*it) != default_group ) {
00919 kc->setGroup( *it );
00920
00921 e = new Event( this );
00922 e->name = kc->readEntry( name );
00923 e->description = kc->readEntry( comment );
00924 e->configGroup = *it;
00925
00926 if ( e->name.isEmpty() || e->description.isEmpty() )
00927 delete e;
00928
00929 else {
00930 int default_rep = kc->readNumEntry("default_presentation", 0);
00931 QString default_logfile = kc->readEntry("default_logfile");
00932 QString default_soundfile = kc->readEntry("default_sound");
00933 QString default_commandline = kc->readEntry("default_commandline");
00934 config->setGroup(*it);
00935 e->dontShow = config->readNumEntry("nopresentation", 0 );
00936
00937 if ( revertToDefaults )
00938 {
00939 e->presentation = default_rep;
00940 e->logfile = default_logfile;
00941 e->soundfile = default_soundfile;
00942 e->commandline = default_commandline;
00943 }
00944
00945 else
00946 {
00947 e->presentation = config->readNumEntry("presentation",
00948 default_rep);
00949 e->logfile = config->readEntry("logfile",
00950 default_logfile);
00951 e->soundfile = config->readEntry("soundfile",
00952 default_soundfile);
00953 e->commandline = config->readEntry("commandline",
00954 default_commandline);
00955 }
00956
00957 m_events->append( e );
00958 }
00959 }
00960
00961 ++it;
00962 }
00963
00964 return;
00965 }
00966
00969
00970 ListViewItem::ListViewItem( QListView *view, Event *event )
00971 : QListViewItem( view ),
00972 m_event( event )
00973 {
00974 setText( COL_EVENT, event->text() );
00975 }
00976
00977 int ListViewItem::compare ( QListViewItem * i, int col, bool ascending ) const
00978 {
00979 ListViewItem *item = static_cast<ListViewItem*>( i );
00980 int myPres = m_event->presentation;
00981 int otherPres = item->event().presentation;
00982
00983 int action = 0;
00984
00985 switch ( col )
00986 {
00987 case COL_EVENT:
00988 return QListViewItem::compare( i, col, ascending );
00989
00990 case COL_EXECUTE:
00991 action = KNotifyClient::Execute;
00992 break;
00993 case COL_LOGFILE:
00994 action = KNotifyClient::Logfile;
00995 break;
00996 case COL_MESSAGE:
00997 action = (KNotifyClient::Messagebox | KNotifyClient::PassivePopup);
00998 break;
00999 case COL_SOUND:
01000 action = KNotifyClient::Sound;
01001 break;
01002 case COL_STDERR:
01003 action = KNotifyClient::Stderr;
01004 break;
01005 }
01006
01007 if ( (myPres & action) == (otherPres & action) )
01008 {
01009
01010 return QListViewItem::compare( i, COL_EVENT, true );
01011 }
01012
01013 if ( myPres & action )
01014 return -1;
01015 if ( otherPres & action )
01016 return 1;
01017
01018 return 0;
01019 }
01020
01021 #include "knotifydialog.moc"