summaryrefslogtreecommitdiffstats
path: root/kcron/ktapp.cpp
blob: 6db4cb3232692822e28a3f7c47b16202c2802b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/***************************************************************************
 *   KT application implementation.                                        *
 *   --------------------------------------------------------------------  *
 *   Copyright (C) 1999, Gary Meyer <[email protected]>                       *
 *   --------------------------------------------------------------------  *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "ktapp.h"
#include <tdemenubar.h>
#include <tdestdaccel.h>
#include <tdemessagebox.h>
#include <tdeconfig.h>
#include <tdeapplication.h>
#include <tdelocale.h>       // i18n()
#include <kstdaction.h>
#include <tdeaction.h>
#include <kiconloader.h>
#include <tdepopupmenu.h>
#include <kstatusbar.h>

#include "cthost.h"
#include "ctcron.h"
#include "cttask.h"

#include "kticon.h"
#include "ktview.h"


const int KTApp::statusMessage            (1001);


KTApp::KTApp() : TDEMainWindow(0)
{
  config=kapp->config();

  setIcon(KTIcon::application(true));

  setCaption(i18n("Task Scheduler"));


  // Initialize document.
  cthost = new CTHost();

  // Initialize view.
  view = new KTView(this);
  setCentralWidget(view);
  
  // Call inits to invoke all other construction parts.
  setupActions();
  initStatusBar();
  createGUI();
  view->disableIcons();
  
  //Connections
  TDEPopupMenu *editMenu = static_cast<TDEPopupMenu*>(guiFactory()->container("edit", this)); 
  TDEPopupMenu *settingsMenu = static_cast<TDEPopupMenu*>(guiFactory()->container("settings", this)); 
  
  connect(editMenu,TQT_SIGNAL(highlighted(int)),this,TQT_SLOT(statusEditCallback(int)));
  connect(settingsMenu,TQT_SIGNAL(highlighted(int)),this,TQT_SLOT(statusSettingsCallback(int)));
  // Read options.
  readOptions();
}

bool KTApp::init()
{
  if (cthost->isError())
  {
    KMessageBox::error(this, i18n("The following error occurred while initializing KCron:"
                                  "\n\n%1\n\nKCron will now exit.\n").arg(cthost->errorMessage()));
    return false;
  }

  // Display greeting screen.
  // if there currently are no scheduled tasks...
  if (!cthost->root())
  {
    int taskCount(0);

    for (CTCronIterator i = (CTCronIterator)cthost->cron.begin();
      i != cthost->cron.end(); i++)
    {
      for (CTTaskIterator j = (CTTaskIterator)(*i)->task.begin();
        j != (*i)->task.end(); j++)
      {
        taskCount++;
      }
    }

    if (taskCount == 0)
    {
      show();
      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");
    }
  }
  return true;
}

KTApp::~KTApp()
{
 delete view;
 delete cthost;
}

const CTHost& KTApp::getCTHost() const
{
  return *cthost;
}


TQString KTApp::caption()
{
  TQString cap(kapp->caption());
  return cap;
}

void KTApp::setupActions()
{
  //File Menu
  KStdAction::save(TQT_TQOBJECT(this), TQT_SLOT(slotFileSave()), actionCollection());
  KStdAction::print(TQT_TQOBJECT(this), TQT_SLOT(slotFilePrint()), actionCollection());
  KStdAction::quit(TQT_TQOBJECT(this), TQT_SLOT(slotFileQuit()), actionCollection());
  
  //Edit menu
  KStdAction::cut(TQT_TQOBJECT(this), TQT_SLOT(slotEditCut()), actionCollection());
  KStdAction::copy(TQT_TQOBJECT(this), TQT_SLOT(slotEditCopy()), actionCollection());
  KStdAction::paste(TQT_TQOBJECT(this), TQT_SLOT(slotEditPaste()), actionCollection());
  (void)new TDEAction(i18n("&New..."), TDEStdAccel::openNew(), TQT_TQOBJECT(this),TQT_SLOT(slotEditNew()),actionCollection(),"edit_new");
  //I don't like this TDEStdAccel::open() for modifying, but I'm just porting this to xmlui
  (void)new TDEAction(i18n("M&odify..."), TDEStdAccel::open(), TQT_TQOBJECT(this),TQT_SLOT(slotEditModify()),actionCollection(),"edit_modify");
  (void)new TDEAction(i18n("&Delete"), 0, TQT_TQOBJECT(this),TQT_SLOT(slotEditDelete()),actionCollection(),"edit_delete");
  (void)new TDEAction(i18n("&Enabled"), 0, TQT_TQOBJECT(this),TQT_SLOT(slotEditEnable()),actionCollection(),"edit_enable");
  (void)new TDEAction(i18n("&Run Now"), 0, TQT_TQOBJECT(this),TQT_SLOT(slotEditRunNow()),actionCollection(),"edit_run");
  
  //Settings menu
  (void)new TDEAction(i18n("Show &Toolbar"), 0, TQT_TQOBJECT(this),TQT_SLOT(slotViewToolBar()),actionCollection(),"show_toolbar");
  (void)new TDEAction(i18n("Show &Statusbar"), 0, TQT_TQOBJECT(this),TQT_SLOT(slotViewStatusBar()),actionCollection(),"show_statusbar");
    
}

void KTApp::initStatusBar()
{
  statusBar()->insertItem(i18n("Ready."), statusMessage, 1);
  statusBar()->setItemAlignment(statusMessage, TQt::AlignLeft | TQt::AlignVCenter);
}

void KTApp::saveOptions()
{
  config->setGroup(TQString("General Options"));
  config->writeEntry(TQString("Geometry"), size());
  config->writeEntry(TQString("Show Toolbar"), toolBar()->isVisible());
  config->writeEntry(TQString("Show Statusbar"), statusBar()->isVisible());
  config->writeEntry(TQString("ToolBarPos"),  (int)toolBar()->barPos());
}


void KTApp::readOptions()
{
  config->setGroup(TQString("General Options"));
  TDEPopupMenu *settingsMenu = static_cast<TDEPopupMenu*>(guiFactory()->container("settings", this));

  // bar status settings
  bool bViewToolbar = config->readBoolEntry(TQString("Show Toolbar"), true);
  settingsMenu->setItemChecked(settingsMenu->idAt(0),bViewToolbar);
  if (!bViewToolbar)
    toolBar()->hide();

  bool bViewStatusbar = config->readBoolEntry(TQString("Show Statusbar"), true);
  settingsMenu->setItemChecked(settingsMenu->idAt(1),bViewStatusbar);
  if (!bViewStatusbar)
    statusBar()->hide();

  // bar position settings
  TDEToolBar::BarPosition tool_bar_pos;
  tool_bar_pos=(TDEToolBar::BarPosition)
    config->readNumEntry(TQString("ToolBarPos"),
    TDEToolBar::Top);

  toolBar()->setBarPos(tool_bar_pos);

  TQSize size=config->readSizeEntry(TQString("Geometry"));

  // Minimum size is 350 by 250

  if (size.isEmpty())
  {
    size.setWidth(350);
    size.setHeight(250);
  }

  if (size.width() < 350)
  {
    size.setWidth(350);
  }
  if (size.height() < 250)
  {
    size.setHeight(250);
  }

  resize(size);

}

bool KTApp::queryClose()
{
  if(cthost->dirty())
  {
    KTApp* win = (KTApp*)parent();

    int retVal = KMessageBox::warningYesNoCancel(win,
      i18n("Scheduled tasks have been modified.\nDo you want to save changes?"),
      TQString(),
      KStdGuiItem::save(), KStdGuiItem::discard()
      );

    switch (retVal)
    {
      case KMessageBox::Yes:
        cthost->apply();
        if (cthost->isError())
        {
           KMessageBox::error(win, cthost->errorMessage());
           return false;
        }
        return true;
        break;
      case KMessageBox::No:
        return true;
        break;
      case KMessageBox::Cancel:
        return false;
        break;
      default:
        return false;
        break;
    }
  }
  else
  {
    return true;
  }
}

bool KTApp::queryExit()
{
  saveOptions();
  return true;
}

void KTApp::slotFileSave()
{
  slotStatusMsg(i18n("Saving..."));
  cthost->apply();
  slotStatusMsg(i18n("Ready."));
  if (cthost->isError())
  {
     KMessageBox::error(this, cthost->errorMessage());
  }
}

void KTApp::slotFilePrint()
{
  slotStatusMsg(i18n("Printing..."));
  view->print();
  slotStatusMsg(i18n("Ready."));
}

void KTApp::slotFileQuit()
{
  saveOptions();
  close();
}

void KTApp::slotEdit(const TQPoint& qp)
{
  TDEPopupMenu *editMenu = static_cast<TDEPopupMenu*>(guiFactory()->container("edit", this));
  editMenu->exec(qp, 0);
}

void KTApp::slotEditCut()
{
  slotStatusMsg(i18n("Cutting to clipboard..."));
  view->copy();
  view->remove();
  slotStatusMsg(i18n("Ready."));
}

void KTApp::slotEditCopy()
{
  slotStatusMsg(i18n("Copying to clipboard..."));
  view->copy();
  slotStatusMsg(i18n("Ready."));
}

void KTApp::slotEditPaste()
{
  slotStatusMsg(i18n("Pasting from clipboard..."));
  view->paste();
  slotStatusMsg(i18n("Ready."));
}

void KTApp::slotEditNew()
{
  slotStatusMsg(i18n("Adding new entry..."));
  view->create();
  slotStatusMsg(i18n("Ready."));
}

void KTApp::slotEditModify()
{
  slotStatusMsg(i18n("Modifying entry..."));
  view->edit();
  slotStatusMsg(i18n("Ready."));
}

void KTApp::slotEditDelete()
{
  slotStatusMsg(i18n("Deleting entry..."));
  view->remove();
  slotStatusMsg(i18n("Ready."));
}

void KTApp::slotEditEnable()
{
  TDEPopupMenu *editMenu = static_cast<TDEPopupMenu*>(guiFactory()->container("edit", this)); 
  if (editMenu->isItemChecked(editMenu->idAt(8)))
  {
    slotStatusMsg(i18n("Disabling entry..."));
    view->enable(false);
    editMenu->setItemChecked(editMenu->idAt(8), false);
  }
  else
  {
    slotStatusMsg(i18n("Enabling entry..."));
    view->enable(true);
    editMenu->setItemChecked(editMenu->idAt(8), true);
  }
  slotStatusMsg(i18n("Ready."));
}

void KTApp::slotEditRunNow()
{
  slotStatusMsg(i18n("Running command..."));
  view->run();
  slotStatusMsg(i18n("Ready."));
}

void KTApp::slotViewToolBar()
{
  if(toolBar()->isVisible())
    toolBar()->hide();
  else
    toolBar()->show();
  
  TDEPopupMenu *settingsMenu = static_cast<TDEPopupMenu*>(guiFactory()->container("settings", this));
  settingsMenu->setItemChecked(settingsMenu->idAt(0),toolBar()->isVisible());
  
  slotStatusMsg(i18n("Ready."));
}

void KTApp::slotViewStatusBar()
{
  if (statusBar()->isVisible())
    statusBar()->hide();
  else
    statusBar()->show();
  
  TDEPopupMenu *settingsMenu = static_cast<TDEPopupMenu*>(guiFactory()->container("settings", this));
  settingsMenu->setItemChecked(settingsMenu->idAt(1),statusBar()->isVisible());
  
  slotStatusMsg(i18n("Ready."));
}

void KTApp::slotStatusMsg(const TQString & text)
{
  statusBar()->clear();
  statusBar()->changeItem(text, statusMessage);
  setCaption(i18n("Task Scheduler"), cthost->dirty());
}

void KTApp::slotStatusHelpMsg(const TQString & text)
{
  statusBar()->message(text, 2000);
}

void KTApp::statusEditCallback(int id_)
{
  TDEPopupMenu *editMenu = static_cast<TDEPopupMenu*>(guiFactory()->container("edit", this)); 
  int index = editMenu->indexOf(id_);
  switch (index) {
    case menuEditNew:
      slotStatusHelpMsg(i18n("Create a new task or variable."));
      break;
    case menuEditModify:
      slotStatusHelpMsg(i18n("Edit the selected task or variable."));
      break;
    case menuEditDelete:
      slotStatusHelpMsg(i18n("Delete the selected task or variable."));
      break;
    case menuEditEnabled:
      slotStatusHelpMsg(i18n("Enable/disable the selected task or variable."));
      break;
    case menuEditRunNow:
      slotStatusHelpMsg(i18n("Run the selected task now."));
      break;

    default:
      break;
  }
}

void KTApp::statusSettingsCallback(int id_)
{
  TDEPopupMenu *settingsMenu = static_cast<TDEPopupMenu*>(guiFactory()->container("settings", this)); 
  int index = settingsMenu->indexOf(id_);
  switch (index) {
    case menuSettingsShowToolBar:
      slotStatusHelpMsg(i18n("Enable/disable the tool bar."));
      break;
    case menuSettingsShowStatusBar:
      slotStatusHelpMsg(i18n("Enable/disable the status bar."));
      break;
    
    default:
      break;
  }
}

void KTApp::slotEnableModificationButtons(bool state)
{
  if (state)
    stateChanged("no_task_selected", StateReverse);
  else
    stateChanged("no_task_selected");
    
}

void KTApp::slotEnablePaste(bool state)
{
 if (state)
    stateChanged("paste_disabled", StateReverse);
 else
    stateChanged("paste_disabled");
}

void KTApp::slotEnableRunNow(bool state)
{
  if (state)
    stateChanged("runnow_enabled");
  else
    stateChanged("runnow_enabled", StateReverse);
}

void KTApp::slotEnableEnabled(bool state)
{
  TDEPopupMenu *editMenu = static_cast<TDEPopupMenu*>(guiFactory()->container("edit", this));
  editMenu->setItemChecked(editMenu->idAt(8),state);
}      

#include "ktapp.moc"