00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef WITHOUT_ARTS
00022
00023 #include "kalarm.h"
00024
00025 #include <qlabel.h>
00026 #include <qhbox.h>
00027 #include <qgroupbox.h>
00028 #include <qlayout.h>
00029 #include <qfile.h>
00030 #include <qdir.h>
00031 #include <qwhatsthis.h>
00032 #include <qtooltip.h>
00033
00034 #include <klocale.h>
00035 #include <kstandarddirs.h>
00036 #include <kiconloader.h>
00037 #ifdef WITHOUT_ARTS
00038 #include <kaudioplayer.h>
00039 #else
00040 #include <qtimer.h>
00041 #include <arts/kartsdispatcher.h>
00042 #include <arts/kartsserver.h>
00043 #include <arts/kplayobjectfactory.h>
00044 #include <arts/kplayobject.h>
00045 #endif
00046 #include <kmessagebox.h>
00047 #include <kio/netaccess.h>
00048 #include <kdebug.h>
00049
00050 #include "checkbox.h"
00051 #include "functions.h"
00052 #include "lineedit.h"
00053 #include "mainwindow.h"
00054 #include "pushbutton.h"
00055 #include "slider.h"
00056 #include "soundpicker.h"
00057 #include "spinbox.h"
00058 #include "sounddlg.moc"
00059
00060
00061
00062
00063 QString SoundDlg::i18n_SetVolume() { return i18n("Set volume"); }
00064 QString SoundDlg::i18n_v_SetVolume() { return i18n("Set &volume"); }
00065 QString SoundDlg::i18n_Repeat() { return i18n("Repeat"); }
00066 QString SoundDlg::i18n_p_Repeat() { return i18n("Re&peat"); }
00067
00068 static const char SOUND_DIALOG_NAME[] = "SoundDialog";
00069
00070
00071 SoundDlg::SoundDlg(const QString& file, float volume, float fadeVolume, int fadeSeconds, bool repeat,
00072 const QString& caption, QWidget* parent, const char* name)
00073 : KDialogBase(parent, name, true, caption, Ok|Cancel, Ok, false),
00074 mReadOnly(false),
00075 mArtsDispatcher(0),
00076 mPlayObject(0),
00077 mPlayTimer(0)
00078 {
00079 QWidget* page = new QWidget(this);
00080 setMainWidget(page);
00081 QVBoxLayout* layout = new QVBoxLayout(page, 0, spacingHint());
00082
00083
00084 QHBox* box = new QHBox(page);
00085 layout->addWidget(box);
00086 mFilePlay = new QPushButton(box);
00087 mFilePlay->setPixmap(SmallIcon("player_play"));
00088 mFilePlay->setFixedSize(mFilePlay->sizeHint());
00089 connect(mFilePlay, SIGNAL(clicked()), SLOT(playSound()));
00090 QToolTip::add(mFilePlay, i18n("Test the sound"));
00091 QWhatsThis::add(mFilePlay, i18n("Play the selected sound file."));
00092
00093
00094 mFileEdit = new LineEdit(LineEdit::Url, box);
00095 mFileEdit->setAcceptDrops(true);
00096 QWhatsThis::add(mFileEdit, i18n("Enter the name or URL of a sound file to play."));
00097
00098
00099 mFileBrowseButton = new PushButton(box);
00100 mFileBrowseButton->setPixmap(SmallIcon("fileopen"));
00101 mFileBrowseButton->setFixedSize(mFileBrowseButton->sizeHint());
00102 connect(mFileBrowseButton, SIGNAL(clicked()), SLOT(slotPickFile()));
00103 QToolTip::add(mFileBrowseButton, i18n("Choose a file"));
00104 QWhatsThis::add(mFileBrowseButton, i18n("Select a sound file to play."));
00105
00106
00107 mRepeatCheckbox = new CheckBox(i18n_p_Repeat(), page);
00108 mRepeatCheckbox->setFixedSize(mRepeatCheckbox->sizeHint());
00109 QWhatsThis::add(mRepeatCheckbox,
00110 i18n("If checked, the sound file will be played repeatedly for as long as the message is displayed."));
00111 layout->addWidget(mRepeatCheckbox);
00112
00113
00114 QGroupBox* group = new QGroupBox(i18n("Volume"), page);
00115 layout->addWidget(group);
00116 QGridLayout* grid = new QGridLayout(group, 4, 3, marginHint(), spacingHint());
00117 grid->addRowSpacing(0, fontMetrics().lineSpacing()/2);
00118 grid->setColStretch(2, 1);
00119 int indentWidth = 3 * KDialog::spacingHint();
00120 grid->addColSpacing(0, indentWidth);
00121 grid->addColSpacing(1, indentWidth);
00122
00123 int alignment = QApplication::reverseLayout() ? Qt::AlignRight : Qt::AlignLeft;
00124
00125
00126 box = new QHBox(group);
00127 box->setSpacing(spacingHint());
00128 grid->addMultiCellWidget(box, 1, 1, 0, 2);
00129 mVolumeCheckbox = new CheckBox(i18n_v_SetVolume(), box);
00130 mVolumeCheckbox->setFixedSize(mVolumeCheckbox->sizeHint());
00131 connect(mVolumeCheckbox, SIGNAL(toggled(bool)), SLOT(slotVolumeToggled(bool)));
00132 QWhatsThis::add(mVolumeCheckbox,
00133 i18n("Select to choose the volume for playing the sound file."));
00134
00135
00136 mVolumeSlider = new Slider(0, 100, 10, 0, Qt::Horizontal, box);
00137 mVolumeSlider->setTickmarks(QSlider::Below);
00138 mVolumeSlider->setTickInterval(10);
00139 mVolumeSlider->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
00140 QWhatsThis::add(mVolumeSlider, i18n("Choose the volume for playing the sound file."));
00141 mVolumeCheckbox->setFocusWidget(mVolumeSlider);
00142
00143
00144 mFadeCheckbox = new CheckBox(i18n("Fade"), group);
00145 mFadeCheckbox->setFixedSize(mFadeCheckbox->sizeHint());
00146 connect(mFadeCheckbox, SIGNAL(toggled(bool)), SLOT(slotFadeToggled(bool)));
00147 QWhatsThis::add(mFadeCheckbox,
00148 i18n("Select to fade the volume when the sound file first starts to play."));
00149 grid->addMultiCellWidget(mFadeCheckbox, 2, 2, 1, 2, alignment);
00150
00151
00152 mFadeBox = new QHBox(group);
00153 mFadeBox->setSpacing(spacingHint());
00154 grid->addWidget(mFadeBox, 3, 2, alignment);
00155 QLabel* label = new QLabel(i18n("Time period over which to fade the sound", "Fade time:"), mFadeBox);
00156 label->setFixedSize(label->sizeHint());
00157 mFadeTime = new SpinBox(1, 999, 1, mFadeBox);
00158 mFadeTime->setLineShiftStep(10);
00159 mFadeTime->setFixedSize(mFadeTime->sizeHint());
00160 label->setBuddy(mFadeTime);
00161 label = new QLabel(i18n("seconds"), mFadeBox);
00162 label->setFixedSize(label->sizeHint());
00163 QWhatsThis::add(mFadeBox, i18n("Enter how many seconds to fade the sound before reaching the set volume."));
00164
00165
00166 mFadeVolumeBox = new QHBox(group);
00167 mFadeVolumeBox->setSpacing(spacingHint());
00168 grid->addWidget(mFadeVolumeBox, 4, 2);
00169 label = new QLabel(i18n("Initial volume:"), mFadeVolumeBox);
00170 label->setFixedSize(label->sizeHint());
00171 mFadeSlider = new Slider(0, 100, 10, 0, Qt::Horizontal, mFadeVolumeBox);
00172 mFadeSlider->setTickmarks(QSlider::Below);
00173 mFadeSlider->setTickInterval(10);
00174 mFadeSlider->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
00175 label->setBuddy(mFadeSlider);
00176 QWhatsThis::add(mFadeVolumeBox, i18n("Choose the initial volume for playing the sound file."));
00177
00178
00179 QSize s;
00180 if (KAlarm::readConfigWindowSize(SOUND_DIALOG_NAME, s))
00181 resize(s);
00182
00183
00184 mFileEdit->setText(file);
00185 mRepeatCheckbox->setChecked(repeat);
00186 mVolumeCheckbox->setChecked(volume >= 0);
00187 mVolumeSlider->setValue(volume >= 0 ? static_cast<int>(volume*100) : 100);
00188 mFadeCheckbox->setChecked(fadeVolume >= 0);
00189 mFadeSlider->setValue(fadeVolume >= 0 ? static_cast<int>(fadeVolume*100) : 100);
00190 mFadeTime->setValue(fadeSeconds);
00191 slotVolumeToggled(volume >= 0);
00192 }
00193
00194 SoundDlg::~SoundDlg()
00195 {
00196 stopPlay();
00197 }
00198
00199
00200
00201
00202 void SoundDlg::setReadOnly(bool readOnly)
00203 {
00204 if (readOnly != mReadOnly)
00205 {
00206 mFileEdit->setReadOnly(readOnly);
00207 mFileBrowseButton->setReadOnly(readOnly);
00208 mRepeatCheckbox->setReadOnly(readOnly);
00209 mVolumeCheckbox->setReadOnly(readOnly);
00210 mVolumeSlider->setReadOnly(readOnly);
00211 mFadeCheckbox->setReadOnly(readOnly);
00212 mFadeTime->setReadOnly(readOnly);
00213 mFadeSlider->setReadOnly(readOnly);
00214 mReadOnly = readOnly;
00215 }
00216 }
00217
00218
00219
00220
00221
00222
00223
00224 bool SoundDlg::getSettings(float& volume, float& fadeVolume, int& fadeSeconds) const
00225 {
00226 volume = mVolumeCheckbox->isChecked() ? (float)mVolumeSlider->value() / 100 : -1;
00227 if (mFadeCheckbox->isChecked())
00228 {
00229 fadeVolume = (float)mFadeSlider->value() / 100;
00230 fadeSeconds = mFadeTime->value();
00231 }
00232 else
00233 {
00234 fadeVolume = -1;
00235 fadeSeconds = 0;
00236 }
00237 return mRepeatCheckbox->isChecked();
00238 }
00239
00240
00241
00242
00243
00244 void SoundDlg::resizeEvent(QResizeEvent* re)
00245 {
00246 if (isVisible())
00247 KAlarm::writeConfigWindowSize(SOUND_DIALOG_NAME, re->size());
00248 mVolumeSlider->resize(mFadeSlider->size());
00249 KDialog::resizeEvent(re);
00250 }
00251
00252 void SoundDlg::showEvent(QShowEvent* se)
00253 {
00254 mVolumeSlider->resize(mFadeSlider->size());
00255 KDialog::showEvent(se);
00256 }
00257
00258
00259
00260
00261 void SoundDlg::slotOk()
00262 {
00263 if (mReadOnly)
00264 reject();
00265 if (!checkFile())
00266 return;
00267 accept();
00268 }
00269
00270
00271
00272
00273 void SoundDlg::slotPickFile()
00274 {
00275 QString url = SoundPicker::browseFile(mDefaultDir, mFileEdit->text());
00276 if (!url.isEmpty())
00277 mFileEdit->setText(url);
00278 }
00279
00280
00281
00282
00283 void SoundDlg::playSound()
00284 {
00285 #ifdef WITHOUT_ARTS
00286 if (checkFile())
00287 KAudioPlayer::play(QFile::encodeName(mFileName));
00288 #else
00289 if (mPlayObject)
00290 {
00291 stopPlay();
00292 return;
00293 }
00294 if (!checkFile())
00295 return;
00296 KURL url(mFileName);
00297 MainWindow* mmw = MainWindow::mainMainWindow();
00298 if (!url.isValid() || !KIO::NetAccess::exists(url, true, mmw)
00299 || !KIO::NetAccess::download(url, mLocalAudioFile, mmw))
00300 {
00301 kdError(5950) << "SoundDlg::playAudio(): Open failure: " << mFileName << endl;
00302 KMessageBox::error(this, i18n("Cannot open audio file:\n%1").arg(mFileName));
00303 return;
00304 }
00305 mPlayTimer = new QTimer(this);
00306 connect(mPlayTimer, SIGNAL(timeout()), SLOT(checkAudioPlay()));
00307 mArtsDispatcher = new KArtsDispatcher;
00308 mPlayStarted = false;
00309 mAudioFileStart = QTime::currentTime();
00310 KArtsServer aserver;
00311 Arts::SoundServerV2 sserver = aserver.server();
00312 KDE::PlayObjectFactory factory(sserver);
00313 mPlayObject = factory.createPlayObject(mLocalAudioFile, true);
00314 mFilePlay->setPixmap(SmallIcon("player_stop"));
00315 QToolTip::add(mFilePlay, i18n("Stop sound"));
00316 QWhatsThis::add(mFilePlay, i18n("Stop playing the sound"));
00317 connect(mPlayObject, SIGNAL(playObjectCreated()), SLOT(checkAudioPlay()));
00318 if (!mPlayObject->object().isNull())
00319 checkAudioPlay();
00320 #endif
00321 }
00322
00323
00324
00325
00326
00327
00328
00329 void SoundDlg::checkAudioPlay()
00330 {
00331 #ifndef WITHOUT_ARTS
00332 if (!mPlayObject)
00333 return;
00334 if (mPlayObject->state() == Arts::posIdle)
00335 {
00336
00337 if (mPlayStarted)
00338 {
00339
00340 stopPlay();
00341 return;
00342 }
00343
00344
00345 kdDebug(5950) << "SoundDlg::checkAudioPlay(): start\n";
00346 mPlayStarted = true;
00347 mPlayObject->play();
00348 }
00349
00350
00351 Arts::poTime overall = mPlayObject->overallTime();
00352 Arts::poTime current = mPlayObject->currentTime();
00353 int time = 1000*(overall.seconds - current.seconds) + overall.ms - current.ms;
00354 if (time < 0)
00355 time = 0;
00356 kdDebug(5950) << "SoundDlg::checkAudioPlay(): wait for " << (time+100) << "ms\n";
00357 mPlayTimer->start(time + 100, true);
00358 #endif
00359 }
00360
00361
00362
00363
00364
00365 void SoundDlg::stopPlay()
00366 {
00367 #ifndef WITHOUT_ARTS
00368 delete mPlayObject; mPlayObject = 0;
00369 delete mArtsDispatcher; mArtsDispatcher = 0;
00370 delete mPlayTimer; mPlayTimer = 0;
00371 if (!mLocalAudioFile.isEmpty())
00372 {
00373 KIO::NetAccess::removeTempFile(mLocalAudioFile);
00374 mLocalAudioFile = QString::null;
00375 }
00376 mFilePlay->setPixmap(SmallIcon("player_play"));
00377 QToolTip::add(mFilePlay, i18n("Test the sound"));
00378 QWhatsThis::add(mFilePlay, i18n("Play the selected sound file."));
00379 #endif
00380 }
00381
00382
00383
00384
00385
00386 bool SoundDlg::checkFile()
00387 {
00388 mFileName = mFileEdit->text();
00389 KURL url;
00390 if (KURL::isRelativeURL(mFileName))
00391 {
00392
00393 QFileInfo f(mFileName);
00394 if (!f.isRelative())
00395 url.setPath(mFileName);
00396 }
00397 else
00398 url = KURL::fromPathOrURL(mFileName);
00399 #ifdef WITHOUT_ARTS
00400 if (!url.isEmpty())
00401 {
00402
00403
00404 if (url.isLocalFile() && KIO::NetAccess::exists(url, true, this))
00405 {
00406 mFileName = url.path();
00407 return true;
00408 }
00409 }
00410 else
00411 #else
00412 if (url.isEmpty())
00413 #endif
00414 {
00415
00416
00417 QStringList soundDirs = KGlobal::dirs()->resourceDirs("sound");
00418 if (!soundDirs.isEmpty())
00419 {
00420 QDir dir;
00421 dir.setFilter(QDir::Files | QDir::Readable);
00422 for (QStringList::ConstIterator it = soundDirs.begin(); it != soundDirs.end(); ++it)
00423 {
00424 dir = *it;
00425 if (dir.isReadable() && dir.count() > 2)
00426 {
00427 url.setPath(*it);
00428 url.addPath(mFileName);
00429 if (KIO::NetAccess::exists(url, true, this))
00430 {
00431 mFileName = url.path();
00432 return true;
00433 }
00434 }
00435 }
00436 }
00437 url.setPath(QDir::homeDirPath());
00438 url.addPath(mFileName);
00439 if (KIO::NetAccess::exists(url, true, this))
00440 {
00441 mFileName = url.path();
00442 return true;
00443 }
00444 }
00445 #ifdef WITHOUT_ARTS
00446 KMessageBox::sorry(this, i18n("File not found"));
00447 mFileName = QString::null;
00448 return false;
00449 #else
00450 return true;
00451 #endif
00452 }
00453
00454
00455
00456
00457 void SoundDlg::slotVolumeToggled(bool on)
00458 {
00459 mVolumeSlider->setEnabled(on);
00460 mFadeCheckbox->setEnabled(on);
00461 slotFadeToggled(on && mFadeCheckbox->isChecked());
00462 }
00463
00464
00465
00466
00467 void SoundDlg::slotFadeToggled(bool on)
00468 {
00469 mFadeBox->setEnabled(on);
00470 mFadeVolumeBox->setEnabled(on);
00471 }
00472
00473 #endif // #ifndef WITHOUT_ARTS