kalarm

specialactions.cpp

00001 /*
00002  *  specialactions.cpp  -  widget to specify special alarm actions
00003  *  Program:  kalarm
00004  *  Copyright © 2004,2005,2007 by David Jarvie <software@astrojar.org.uk>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "kalarm.h"
00022 
00023 #include <qlabel.h>
00024 #include <qlayout.h>
00025 #include <qwhatsthis.h>
00026 
00027 #include <klineedit.h>
00028 #include <kapplication.h>
00029 #include <kaboutdata.h>
00030 #include <klocale.h>
00031 #include <kdebug.h>
00032 
00033 #include "functions.h"
00034 #include "shellprocess.h"
00035 #include "specialactions.moc"
00036 
00037 
00038 /*=============================================================================
00039 = Class SpecialActionsButton
00040 = Button to display the Special Alarm Actions dialogue.
00041 =============================================================================*/
00042 
00043 SpecialActionsButton::SpecialActionsButton(const QString& caption, QWidget* parent, const char* name)
00044     : QPushButton(caption, parent, name),
00045       mReadOnly(false)
00046 {
00047     setToggleButton(true);
00048     setOn(false);
00049     connect(this, SIGNAL(clicked()), SLOT(slotButtonPressed()));
00050     QWhatsThis::add(this,
00051           i18n("Specify actions to execute before and after the alarm is displayed."));
00052 }
00053 
00054 /******************************************************************************
00055 *  Set the pre- and post-alarm actions.
00056 *  The button's pressed state is set to reflect whether any actions are set.
00057 */
00058 void SpecialActionsButton::setActions(const QString& pre, const QString& post)
00059 {
00060     mPreAction  = pre;
00061     mPostAction = post;
00062     setOn(!mPreAction.isEmpty() || !mPostAction.isEmpty());
00063 }
00064 
00065 /******************************************************************************
00066 *  Called when the OK button is clicked.
00067 *  Display a font and colour selection dialog and get the selections.
00068 */
00069 void SpecialActionsButton::slotButtonPressed()
00070 {
00071     SpecialActionsDlg dlg(mPreAction, mPostAction,
00072                       i18n("Special Alarm Actions"), this, "actionsDlg");
00073     dlg.setReadOnly(mReadOnly);
00074     if (dlg.exec() == QDialog::Accepted)
00075     {
00076         mPreAction  = dlg.preAction();
00077         mPostAction = dlg.postAction();
00078         emit selected();
00079     }
00080     setOn(!mPreAction.isEmpty() || !mPostAction.isEmpty());
00081 }
00082 
00083 
00084 /*=============================================================================
00085 = Class SpecialActionsDlg
00086 = Pre- and post-alarm actions dialogue.
00087 =============================================================================*/
00088 
00089 static const char SPEC_ACT_DIALOG_NAME[] = "SpecialActionsDialog";
00090 
00091 
00092 SpecialActionsDlg::SpecialActionsDlg(const QString& preAction, const QString& postAction,
00093                                      const QString& caption, QWidget* parent, const char* name)
00094     : KDialogBase(parent, name, true, caption, Ok|Cancel, Ok, false)
00095 {
00096     QWidget* page = new QWidget(this);
00097     setMainWidget(page);
00098     QVBoxLayout* layout = new QVBoxLayout(page, 0, spacingHint());
00099 
00100     mActions = new SpecialActions(page);
00101     mActions->setActions(preAction, postAction);
00102     layout->addWidget(mActions);
00103     layout->addSpacing(KDialog::spacingHint());
00104 
00105     QSize s;
00106     if (KAlarm::readConfigWindowSize(SPEC_ACT_DIALOG_NAME, s))
00107         resize(s);
00108 }
00109 
00110 /******************************************************************************
00111 *  Called when the OK button is clicked.
00112 */
00113 void SpecialActionsDlg::slotOk()
00114 {
00115     if (mActions->isReadOnly())
00116         reject();
00117     accept();
00118 }
00119 
00120 /******************************************************************************
00121 *  Called when the dialog's size has changed.
00122 *  Records the new size in the config file.
00123 */
00124 void SpecialActionsDlg::resizeEvent(QResizeEvent* re)
00125 {
00126     if (isVisible())
00127         KAlarm::writeConfigWindowSize(SPEC_ACT_DIALOG_NAME, re->size());
00128     KDialog::resizeEvent(re);
00129 }
00130 
00131 
00132 /*=============================================================================
00133 = Class SpecialActions
00134 = Pre- and post-alarm actions widget.
00135 =============================================================================*/
00136 
00137 SpecialActions::SpecialActions(QWidget* parent, const char* name)
00138     : QWidget(parent, name),
00139       mReadOnly(false)
00140 {
00141     QBoxLayout* topLayout = new QVBoxLayout(this, 0, KDialog::spacingHint());
00142 
00143     // Pre-alarm action
00144     QLabel* label = new QLabel(i18n("Pre-a&larm action:"), this);
00145     label->setFixedSize(label->sizeHint());
00146     topLayout->addWidget(label, 0, Qt::AlignAuto);
00147 
00148     mPreAction = new KLineEdit(this);
00149     label->setBuddy(mPreAction);
00150     QWhatsThis::add(mPreAction,
00151           i18n("Enter a shell command to execute before the alarm is displayed.\n"
00152                    "Note that it is executed only when the alarm proper is displayed, not when a reminder or deferred alarm is displayed.\n"
00153                "N.B. KAlarm will wait for the command to complete before displaying the alarm."));
00154     topLayout->addWidget(mPreAction);
00155     topLayout->addSpacing(KDialog::spacingHint());
00156 
00157     // Post-alarm action
00158     label = new QLabel(i18n("Post-alar&m action:"), this);
00159     label->setFixedSize(label->sizeHint());
00160     topLayout->addWidget(label, 0, Qt::AlignAuto);
00161 
00162     mPostAction = new KLineEdit(this);
00163     label->setBuddy(mPostAction);
00164     QWhatsThis::add(mPostAction,
00165           i18n("Enter a shell command to execute after the alarm window is closed.\n"
00166                "Note that it is not executed after closing a reminder window. If you defer "
00167                "the alarm, it is not executed until the alarm is finally acknowledged or closed."));
00168     topLayout->addWidget(mPostAction);
00169 }
00170 
00171 void SpecialActions::setActions(const QString& pre, const QString& post)
00172 {
00173     mPreAction->setText(pre);
00174     mPostAction->setText(post);
00175 }
00176 
00177 QString SpecialActions::preAction() const
00178 {
00179     return mPreAction->text();
00180 }
00181 
00182 QString SpecialActions::postAction() const
00183 {
00184     return mPostAction->text();
00185 }
00186 
00187 void SpecialActions::setReadOnly(bool ro)
00188 {
00189     mReadOnly = ro;
00190     mPreAction->setReadOnly(mReadOnly);
00191     mPostAction->setReadOnly(mReadOnly);
00192 }