File indexing completed on 2024-04-21 15:08:20

0001 //
0002 // C++ Implementation: cexpresolver
0003 //
0004 // Description: Variable/function resolver for the expression parser.
0005 //
0006 /*
0007 Copyright 2006-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 "cexpresolver.h"
0024 
0025 #include "cactionmanager.h"
0026 #include "cmacromanager.h"
0027 #include "cvariablelist.h"
0028 
0029 #include <KLocalizedString>
0030 
0031 cExpResolver::cExpResolver (int _sess)
0032 {
0033   sess = _sess;
0034 }
0035 
0036 cValue cExpResolver::get (QString varName)
0037 {
0038   // we have to request the list on every call, because we aren't notified
0039   // about disconnects/reconnects, so if we were to keep a pointer to
0040   // the variable list, we could easily end up with a wrong one
0041   cVariableList *vars = dynamic_cast<cVariableList *>(cActionManager::self()->object("variables", sess));
0042 
0043   cValue *val = nullptr;
0044   if (vars) val = vars->value(varName, queue);
0045   if (val) return *val;
0046   // can't retrieve value for some reason - return empty one
0047   return cValue::empty();
0048 }
0049 
0050 cValue cExpResolver::function_call (const QString &functionName, list<cValue> &arguments)
0051 {
0052   if (!cMacroManager::self()->functionExists (functionName)) {
0053     cActionManager::self()->invokeEvent ("message", sess,
0054         i18n ("Function %1 does not exist - assuming empty return value.", functionName));
0055     return cValue::empty();
0056   }
0057   return cMacroManager::self()->callFunction (functionName, arguments, sess, queue);
0058 }
0059