File indexing completed on 2024-04-14 03:59:58
0001 /*************************************************************************** 0002 ctimer.cpp - description 0003 ------------------- 0004 begin : St maj 8 2003 0005 copyright : (C) 2003-2008 by Tomas Mecir 0006 email : kmuddy@kmuddy.com 0007 ***************************************************************************/ 0008 0009 /*************************************************************************** 0010 * * 0011 * This program is free software; you can redistribute it and/or modify * 0012 * it under the terms of the GNU General Public License as published by * 0013 * the Free Software Foundation; either version 2 of the License, or * 0014 * (at your option) any later version. * 0015 * * 0016 ***************************************************************************/ 0017 0018 #include "ctimer.h" 0019 0020 #include "cactionmanager.h" 0021 #include "cscripteval.h" 0022 #include "ctimerlist.h" 0023 0024 #include <KLocalizedString> 0025 0026 struct cTimer::Private { 0027 int nextLaunch; 0028 }; 0029 0030 cTimer::cTimer (cList *list) : cListObject (list) 0031 { 0032 d = new Private; 0033 d->nextLaunch = -1; 0034 } 0035 0036 cTimer::~cTimer () 0037 { 0038 delete d; 0039 } 0040 0041 cList::TraverseAction cTimer::traverse (int traversalType) 0042 { 0043 if (traversalType == TIMER_RESET) { 0044 resetTickCount(); 0045 return cList::Continue; 0046 } else if (traversalType == TIMER_TICK) { 0047 cTimerList *tl = (cTimerList *) list(); 0048 tick (tl->tickCount()); 0049 return cList::Continue; 0050 } else 0051 return cList::Stop; 0052 } 0053 0054 void cTimer::setEnabled (bool en) 0055 { 0056 // if we are enabling/disabling the timer, we need to reset the nextLaunch 0057 if (en != enabled()) 0058 d->nextLaunch = -1; 0059 cListObject::setEnabled (en); 0060 } 0061 0062 void cTimer::attribChanged (const QString &name) 0063 { 0064 if ((name == "command") || (name == "interval") || (name == "single-shot") || (name == "script")) 0065 updateVisibleName(); 0066 } 0067 0068 void cTimer::updateVisibleName() 0069 { 0070 //FIXME: fix word puzzle 0071 QString command = strVal ("command"); 0072 if (command.isEmpty() && (!strVal ("script").isEmpty())) command = "(script)"; 0073 if (command.isEmpty()) { 0074 cListObject::updateVisibleName(); 0075 return; 0076 } 0077 QString name = command; 0078 name += "(" + QString::number(intVal("interval")) + i18n(" sec"); 0079 if (boolVal ("single-shot")) 0080 name += i18n(", single-shot"); 0081 name += ")"; 0082 setVisibleName (name); 0083 } 0084 0085 void cTimer::tick (int tickTime) 0086 { 0087 int interval = intVal ("interval"); 0088 if (interval < 1) //no interval set 0089 return; 0090 0091 if (d->nextLaunch == -1) //no next-launch time set 0092 { 0093 //update next-launch time and return 0094 d->nextLaunch = tickTime + interval; 0095 return; 0096 } 0097 0098 // do we want to run a command in advance ? 0099 if (boolVal ("advance")) { 0100 int secs = intVal ("advance-seconds"); 0101 if (d->nextLaunch - tickTime == secs) 0102 cActionManager::self()->invokeEvent ("command", list()->session(), strVal ("command-advance")); 0103 } 0104 0105 //check if we should execute now 0106 if (d->nextLaunch == tickTime) 0107 execute (); 0108 } 0109 0110 void cTimer::execute () 0111 { 0112 cActionManager *am = cActionManager::self(); 0113 int sess = list()->session(); 0114 // send the command 0115 QString cmd = strVal ("command"); 0116 if (!cmd.isEmpty()) 0117 am->invokeEvent ("command", sess, cmd); 0118 // execute the script, if any 0119 QString script = strVal ("script"); 0120 if (!script.isEmpty()) { 0121 cScriptEval *eval = dynamic_cast<cScriptEval *>(am->object ("scripteval", sess)); 0122 if (eval) eval->eval (script); 0123 } 0124 0125 0126 int interval = intVal ("interval"); 0127 int tickCount = ((cTimerList *) list())->tickCount(); 0128 d->nextLaunch = tickCount + interval; 0129 //if we're a single-shot timer, we need to disable now 0130 if (boolVal ("single-shot")) 0131 setEnabled (false); 0132 } 0133 0134 int cTimer::tickTimeout () 0135 { 0136 if (d->nextLaunch == -1) return intVal ("interval"); 0137 0138 cTimerList *tl = (cTimerList *) list(); 0139 return d->nextLaunch - tl->tickCount(); 0140 } 0141 0142 void cTimer::resetTickCount () 0143 { 0144 d->nextLaunch = -1; 0145 } 0146