1 /***************************************************************************
2 * KT application implementation. *
3 * -------------------------------------------------------------------- *
4 * Copyright (C) 1999, Gary Meyer <gary@meyer.net> *
5 * -------------------------------------------------------------------- *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 ***************************************************************************/
14 #include <kstandardshortcut.h>
15 #include <kmessagebox.h>
17 #include <klocale.h> // i18n()
18 #include <kstandardaction.h>
20 #include <kiconloader.h>
22 #include <kstatusbar.h>
23 #include <kactioncollection.h>
24 #include <ktoggleaction.h>
28 #include <kxmlguifactory.h>
37 const int KTApp::statusMessage (1001);
40 KTApp::KTApp() : KXmlGuiWindow(0),
41 config(KGlobal::config())
43 setWindowIcon(KTIcon::application(true));
45 setCaption(i18n("Task Scheduler"));
47 // Call inits to invoke all other construction parts.
54 // Initialize document.
55 cthost
= new CTHost(crontab
);
57 setupGUI(QSize(600, 320));
60 view
= new KTView(this);
61 setCentralWidget(view
);
64 KMenu
*editMenu
= static_cast<KMenu
*>(guiFactory()->container("edit", this));
65 connect(editMenu
,SIGNAL(hovered(QAction
*)),this,SLOT(statusEditCallback(QAction
*)));
70 if (cthost
->isError())
72 KMessageBox::error(this, i18n("The following error occurred while initializing KCron:"
73 "\n\n%1\n\nKCron will now exit.\n", cthost
->errorMessage()));
77 // Display greeting screen.
78 // if there currently are no scheduled tasks...
83 for (CTCronIterator i
= (CTCronIterator
)cthost
->cron
.begin();
84 i
!= cthost
->cron
.end(); i
++)
86 for (CTTaskIterator j
= (CTTaskIterator
)(*i
)->task
.begin();
87 j
!= (*i
)->task
.end(); j
++)
96 KMessageBox::information(this, i18n("You can use this application to schedule programs to run in the background.\nTo schedule a new task now, click on the Tasks folder and select Edit/New from the menu."), i18n("Welcome to the Task Scheduler"), "welcome");
108 const CTHost
& KTApp::getCTHost() const
114 QString
KTApp::caption()
116 QString
cap(KGlobal::caption());
120 void KTApp::setupActions()
123 KStandardAction::save(this, SLOT(slotFileSave()), actionCollection());
124 KStandardAction::print(this, SLOT(slotFilePrint()), actionCollection());
125 KStandardAction::quit(this, SLOT(slotFileQuit()), actionCollection());
128 QAction
*a
= KStandardAction::cut(this, SLOT(slotEditCut()), actionCollection());
129 actionCollection()->addAction( "edit_cut", a
);
130 KStandardAction::copy(this, SLOT(slotEditCopy()), actionCollection());
131 KStandardAction::paste(this, SLOT(slotEditPaste()), actionCollection());
133 QAction
* newAct
= actionCollection()->addAction( "edit_new" );
134 newAct
->setObjectName("edit_new");
135 newAct
->setText( i18n("&New...") );
136 newAct
->setIcon( KIcon("document-new") );
137 qobject_cast
<KAction
*>( newAct
)->setShortcut(KStandardShortcut::shortcut(KStandardShortcut::New
));
138 connect(newAct
, SIGNAL(triggered(bool)), SLOT(slotEditNew()));
140 //I don't like this KStandardShortcut::open() for modifying, but I'm just porting this to xmlui
141 QAction
*modifyAct
= actionCollection()->addAction( "edit_modify" );
142 modifyAct
->setObjectName("edit_modify");
143 modifyAct
->setText( i18n("M&odify...") );
144 modifyAct
->setIcon( KIcon("document-open") );
145 qobject_cast
<KAction
*>( modifyAct
)->setShortcut(KStandardShortcut::shortcut(KStandardShortcut::Open
));
146 connect(modifyAct
, SIGNAL(triggered(bool)), SLOT(slotEditModify()));
148 QAction
*deleteAct
= actionCollection()->addAction( "edit_delete" );
149 deleteAct
->setObjectName("edit_delete");
150 deleteAct
->setText( i18n("&Delete") );
151 deleteAct
->setIcon( KIcon("edit-delete") );
152 connect(deleteAct
, SIGNAL(triggered(bool)), SLOT(slotEditDelete()));
154 QAction
*enableAct
= actionCollection()->addAction( "edit_enable" );
155 enableAct
->setObjectName("edit_enable");
156 enableAct
->setText( i18n("&Enabled") );
157 connect(enableAct
, SIGNAL(triggered(bool)), SLOT(slotEditEnable()));
159 QAction
*runAct
= actionCollection()->addAction( "edit_run" );
160 runAct
->setObjectName("edit_run");
161 runAct
->setText( i18n("&Run Now") );
162 connect(runAct
, SIGNAL(triggered(bool)), SLOT(slotEditRunNow()));
165 void KTApp::initStatusBar()
167 QLabel
*defaultmsg
= new QLabel(i18n(" Ready."));
168 defaultmsg
->setFixedHeight( fontMetrics().height() + 2 );
169 defaultmsg
->setAlignment( Qt::AlignLeft
| Qt::AlignVCenter
);
170 statusBar()->addWidget(defaultmsg
);
173 void KTApp::saveOptions()
175 KConfigGroup
group(config
, "General Options");
176 group
.writeEntry(QString("Path to crontab"), crontab
);
180 void KTApp::readOptions()
182 KConfigGroup
group(config
, "General Options");
184 // get the path to the crontab binary
185 crontab
= group
.readEntry(QString("Path to crontab"), QString("crontab"));
188 bool KTApp::queryClose()
192 KTApp
* win
= (KTApp
*)parent();
194 int retVal
= KMessageBox::warningYesNoCancel(win
,
195 i18n("Scheduled tasks have been modified.\nDo you want to save changes?"),
197 KStandardGuiItem::save(), KStandardGuiItem::discard()
202 case KMessageBox::Yes
:
204 if (cthost
->isError())
206 KMessageBox::error(win
, cthost
->errorMessage());
211 case KMessageBox::No
:
214 case KMessageBox::Cancel
:
228 bool KTApp::queryExit()
234 void KTApp::slotFileSave()
236 slotStatusMsg(i18n("Saving..."));
238 slotStatusMsg(i18n("Ready."));
239 if (cthost
->isError())
241 KMessageBox::error(this, cthost
->errorMessage());
245 void KTApp::slotFilePrint()
247 slotStatusMsg(i18n("Printing..."));
249 slotStatusMsg(i18n("Ready."));
252 void KTApp::slotFileQuit()
258 void KTApp::slotEdit(const QPoint
& qp
)
260 KMenu
*editMenu
= static_cast<KMenu
*>(guiFactory()->container("edit", this));
261 editMenu
->exec(qp
, 0);
264 void KTApp::slotEditCut()
266 slotStatusMsg(i18n("Cutting to clipboard..."));
269 slotStatusMsg(i18n("Ready."));
272 void KTApp::slotEditCopy()
274 slotStatusMsg(i18n("Copying to clipboard..."));
276 slotStatusMsg(i18n("Ready."));
279 void KTApp::slotEditPaste()
281 slotStatusMsg(i18n("Pasting from clipboard..."));
283 slotStatusMsg(i18n("Ready."));
286 void KTApp::slotEditNew()
288 slotStatusMsg(i18n("Adding new entry..."));
290 slotStatusMsg(i18n("Ready."));
293 void KTApp::slotEditModify()
295 slotStatusMsg(i18n("Modifying entry..."));
297 slotStatusMsg(i18n("Ready."));
300 void KTApp::slotEditDelete()
302 slotStatusMsg(i18n("Deleting entry..."));
304 slotStatusMsg(i18n("Ready."));
307 void KTApp::slotEditEnable()
309 QAction
* editEnable
= actionCollection()->action("edit_enable");
310 if (editEnable
->isChecked())
312 slotStatusMsg(i18n("Disabling entry..."));
314 editEnable
->setChecked(false);
318 slotStatusMsg(i18n("Enabling entry..."));
320 editEnable
->setChecked(true);
322 slotStatusMsg(i18n("Ready."));
325 void KTApp::slotEditRunNow()
327 slotStatusMsg(i18n("Running command..."));
329 slotStatusMsg(i18n("Ready."));
332 void KTApp::slotStatusMsg(const QString
& text
)
334 statusBar()->showMessage(text
);
335 setCaption(i18n("Task Scheduler"), cthost
->dirty());
338 void KTApp::slotStatusHelpMsg(const QString
& text
)
340 statusBar()->showMessage(text
, 2000);
343 void KTApp::statusEditCallback(QAction
* action
)
345 QString text
= action
->objectName();
346 if (text
== "edit_new") {
347 slotStatusHelpMsg(i18n("Create a new task or variable."));
348 } else if (text
== "edit_modify") {
349 slotStatusHelpMsg(i18n("Edit the selected task or variable."));
350 } else if (text
== "edit_delete") {
351 slotStatusHelpMsg(i18n("Delete the selected task or variable."));
352 } else if (text
== "edit_enable") {
353 slotStatusHelpMsg(i18n("Enable/disable the selected task or variable."));
354 } else if (text
== "edit_run") {
355 slotStatusHelpMsg(i18n("Run the selected task now."));
359 void KTApp::slotEnableModificationButtons(bool state
)
362 stateChanged("no_task_selected", StateReverse
);
364 stateChanged("no_task_selected");
368 void KTApp::slotEnablePaste(bool state
)
371 stateChanged("paste_disabled", StateReverse
);
373 stateChanged("paste_disabled");
376 void KTApp::slotEnableRunNow(bool state
)
379 stateChanged("runnow_enabled");
381 stateChanged("runnow_enabled", StateReverse
);
384 void KTApp::slotEnableEnabled(bool state
)
386 actionCollection()->action("edit_enable")->setChecked(state
);