File indexing completed on 2024-12-08 06:46:43

0001 //
0002 // C++ Implementation: cvartrigger
0003 //
0004 // Description: One variable trigger.
0005 //
0006 /*
0007 Copyright 2004-2011 Tomas Mecir <kmuddy@kmuddy.com>
0008 
0009 This program is free software; you can redistribute it and/or
0010 modify it under the terms of the GNU General Public License as
0011 published by the Free Software Foundation; either version 2 of 
0012 the License, or (at your option) any later version.
0013 
0014 This program is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 GNU General Public License for more details.
0018 
0019 You should have received a copy of the GNU General Public License
0020 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "cvartrigger.h"
0024 
0025 #include "cvartriggerlist.h"
0026 #include <QStringList>
0027 
0028 struct cVarTrigger::Private {
0029   QString varname;
0030   QStringList commands;
0031 };
0032 
0033 cVarTrigger::cVarTrigger (cList *list) : cListObject (list)
0034 {
0035   d = new Private;
0036 }
0037 
0038 cVarTrigger::~cVarTrigger()
0039 {
0040   delete d;
0041 }
0042 
0043 void cVarTrigger::updateVisibleName ()
0044 {
0045   QString variable = strVal ("variable");
0046   if (variable.isEmpty())
0047     cListObject::updateVisibleName();
0048   else
0049     setVisibleName (variable);
0050 }
0051 
0052 void cVarTrigger::attribChanged (const QString &name)
0053 {
0054   if (name == "variable") {
0055     QString v = strVal ("variable");
0056     d->varname = v;
0057     updateVisibleName ();
0058     // remove leading $
0059     // note that this will lead to this function being called again
0060     if ((!v.isEmpty()) && (v[0] == '$'))
0061       setStr ("variable", v.mid (1));
0062   }
0063 }
0064 
0065 cList::TraverseAction cVarTrigger::traverse (int traversalType)
0066 {
0067   if (traversalType == VARTRIGGER_MATCH) {
0068     QString variable = ((cVarTriggerList *) list())->variableName();
0069     if (variable == d->varname)
0070       executeCommands ();
0071     return cList::Continue;
0072   }
0073   return cList::Stop;
0074 }
0075 
0076 void cVarTrigger::executeCommands ()
0077 {
0078   d->commands.clear();
0079 
0080   int commands = strListCount ("command");
0081   // if we have only one empty command, we do nothing
0082   if ((commands == 1) && (strListValue ("command", 1).isEmpty()))
0083     return;
0084 
0085   // add commands to be sent
0086   for (int i = 1; i <= commands; ++i) {
0087     QString cmd = strListValue ("command", i);
0088     d->commands << cmd;
0089   }
0090   // send commands
0091   ((cVarTriggerList *) list())->processCommands (d->commands);
0092 }
0093