SVN_SILENT made messages (.desktop file)
[kdeadmin.git] / kcron / ktapp.cpp
blob61312020d02b1c31fed57f8da4e3cb68b0e8dcde
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 ***************************************************************************/
12 #include "ktapp.h"
13 #include <kmenubar.h>
14 #include <kstandardshortcut.h>
15 #include <kmessagebox.h>
16 #include <kconfig.h>
17 #include <klocale.h> // i18n()
18 #include <kstandardaction.h>
19 #include <kaction.h>
20 #include <kiconloader.h>
21 #include <kmenu.h>
22 #include <kstatusbar.h>
23 #include <kactioncollection.h>
24 #include <ktoggleaction.h>
25 #include "cthost.h"
26 #include "ctcron.h"
27 #include "cttask.h"
28 #include <kxmlguifactory.h>
29 #include <kglobal.h>
30 #include "kticon.h"
31 #include "ktview.h"
32 #include <ktoolbar.h>
33 #include <kicon.h>
34 #include <QLabel>
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.
48 setupActions();
49 initStatusBar();
51 // Read options.
52 readOptions();
54 // Initialize document.
55 cthost = new CTHost(crontab);
57 setupGUI(QSize(600, 320));
59 // Initialize view.
60 view = new KTView(this);
61 setCentralWidget(view);
63 //Connections
64 KMenu *editMenu = static_cast<KMenu*>(guiFactory()->container("edit", this));
65 connect(editMenu,SIGNAL(hovered(QAction*)),this,SLOT(statusEditCallback(QAction*)));
68 bool KTApp::init()
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()));
74 return false;
77 // Display greeting screen.
78 // if there currently are no scheduled tasks...
79 if (!cthost->root())
81 int taskCount(0);
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++)
89 taskCount++;
93 if (taskCount == 0)
95 show();
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");
99 return true;
102 KTApp::~KTApp()
104 delete view;
105 delete cthost;
108 const CTHost& KTApp::getCTHost() const
110 return *cthost;
114 QString KTApp::caption()
116 QString cap(KGlobal::caption());
117 return cap;
120 void KTApp::setupActions()
122 //File Menu
123 KStandardAction::save(this, SLOT(slotFileSave()), actionCollection());
124 KStandardAction::print(this, SLOT(slotFilePrint()), actionCollection());
125 KStandardAction::quit(this, SLOT(slotFileQuit()), actionCollection());
127 //Edit menu
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()
190 if(cthost->dirty())
192 KTApp* win = (KTApp*)parent();
194 int retVal = KMessageBox::warningYesNoCancel(win,
195 i18n("Scheduled tasks have been modified.\nDo you want to save changes?"),
196 QString::null,
197 KStandardGuiItem::save(), KStandardGuiItem::discard()
200 switch (retVal)
202 case KMessageBox::Yes:
203 cthost->apply();
204 if (cthost->isError())
206 KMessageBox::error(win, cthost->errorMessage());
207 return false;
209 return true;
210 break;
211 case KMessageBox::No:
212 return true;
213 break;
214 case KMessageBox::Cancel:
215 return false;
216 break;
217 default:
218 return false;
219 break;
222 else
224 return true;
228 bool KTApp::queryExit()
230 saveOptions();
231 return true;
234 void KTApp::slotFileSave()
236 slotStatusMsg(i18n("Saving..."));
237 cthost->apply();
238 slotStatusMsg(i18n("Ready."));
239 if (cthost->isError())
241 KMessageBox::error(this, cthost->errorMessage());
245 void KTApp::slotFilePrint()
247 slotStatusMsg(i18n("Printing..."));
248 view->print();
249 slotStatusMsg(i18n("Ready."));
252 void KTApp::slotFileQuit()
254 saveOptions();
255 close();
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..."));
267 view->copy();
268 view->remove();
269 slotStatusMsg(i18n("Ready."));
272 void KTApp::slotEditCopy()
274 slotStatusMsg(i18n("Copying to clipboard..."));
275 view->copy();
276 slotStatusMsg(i18n("Ready."));
279 void KTApp::slotEditPaste()
281 slotStatusMsg(i18n("Pasting from clipboard..."));
282 view->paste();
283 slotStatusMsg(i18n("Ready."));
286 void KTApp::slotEditNew()
288 slotStatusMsg(i18n("Adding new entry..."));
289 view->create();
290 slotStatusMsg(i18n("Ready."));
293 void KTApp::slotEditModify()
295 slotStatusMsg(i18n("Modifying entry..."));
296 view->edit();
297 slotStatusMsg(i18n("Ready."));
300 void KTApp::slotEditDelete()
302 slotStatusMsg(i18n("Deleting entry..."));
303 view->remove();
304 slotStatusMsg(i18n("Ready."));
307 void KTApp::slotEditEnable()
309 QAction* editEnable = actionCollection()->action("edit_enable");
310 if (editEnable->isChecked())
312 slotStatusMsg(i18n("Disabling entry..."));
313 view->enable(false);
314 editEnable->setChecked(false);
316 else
318 slotStatusMsg(i18n("Enabling entry..."));
319 view->enable(true);
320 editEnable->setChecked(true);
322 slotStatusMsg(i18n("Ready."));
325 void KTApp::slotEditRunNow()
327 slotStatusMsg(i18n("Running command..."));
328 view->run();
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)
361 if (state)
362 stateChanged("no_task_selected", StateReverse);
363 else
364 stateChanged("no_task_selected");
368 void KTApp::slotEnablePaste(bool state)
370 if (state)
371 stateChanged("paste_disabled", StateReverse);
372 else
373 stateChanged("paste_disabled");
376 void KTApp::slotEnableRunNow(bool state)
378 if (state)
379 stateChanged("runnow_enabled");
380 else
381 stateChanged("runnow_enabled", StateReverse);
384 void KTApp::slotEnableEnabled(bool state)
386 actionCollection()->action("edit_enable")->setChecked(state);
389 #include "ktapp.moc"