File indexing completed on 2024-03-24 15:18:12

0001 /*
0002     SPDX-FileCopyrightText: 2003 Jason Harris <kstars@30doradus.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "scriptfunction.h"
0008 
0009 #include <QDebug>
0010 #include <QStringList>
0011 
0012 ScriptFunction::ScriptFunction(const QString &name, const QString &desc, bool clockfcn, const QString &at1,
0013                                const QString &an1, const QString &at2, const QString &an2, const QString &at3,
0014                                const QString &an3, const QString &at4, const QString &an4, const QString &at5,
0015                                const QString &an5, const QString &at6, const QString &an6)
0016     : INDIProp(QString())
0017 {
0018     Name          = name;
0019     ClockFunction = clockfcn;
0020 
0021     ArgType[0]     = at1;
0022     ArgDBusType[0] = DBusType(at1);
0023     ArgName[0]     = an1;
0024     ArgType[1]     = at2;
0025     ArgDBusType[1] = DBusType(at2);
0026     ArgName[1]     = an2;
0027     ArgType[2]     = at3;
0028     ArgDBusType[2] = DBusType(at3);
0029     ArgName[2]     = an3;
0030     ArgType[3]     = at4;
0031     ArgDBusType[3] = DBusType(at4);
0032     ArgName[3]     = an4;
0033     ArgType[4]     = at5;
0034     ArgDBusType[4] = DBusType(at5);
0035     ArgName[4]     = an5;
0036     ArgType[5]     = at6;
0037     ArgDBusType[5] = DBusType(at6);
0038     ArgName[5]     = an6;
0039 
0040     //Construct a richtext description of the function
0041     QString nameStyle  = "<span style=\"font-family:monospace;font-weight:600\">%1</span>"; //bold
0042     QString typeStyle  = "<span style=\"font-family:monospace;color:#009d00\">%1</span>";   //green
0043     QString paramStyle = "<span style=\"font-family:monospace;color:#00007f\">%1</span>";   //blue
0044 
0045     Description = "<html><head><meta name=\"qrichtext\" content=\"1\" /></head>";
0046     Description += "<body style=\"font-size:11pt;font-family:sans\">";
0047     Description += "<p>" + nameStyle.arg(Name + '(');
0048 
0049     NumArgs = 0;
0050     if (!at1.isEmpty() && !an1.isEmpty())
0051     {
0052         Description += ' ' + typeStyle.arg(at1);
0053         Description += ' ' + paramStyle.arg(an1);
0054         NumArgs++;
0055     }
0056 
0057     if (!at2.isEmpty() && !an2.isEmpty())
0058     {
0059         Description += ", " + typeStyle.arg(at2);
0060         Description += ' ' + paramStyle.arg(an2);
0061         NumArgs++;
0062     }
0063 
0064     if (!at3.isEmpty() && !an3.isEmpty())
0065     {
0066         Description += ", " + typeStyle.arg(at3);
0067         Description += ' ' + paramStyle.arg(an3);
0068         NumArgs++;
0069     }
0070 
0071     if (!at4.isEmpty() && !an4.isEmpty())
0072     {
0073         Description += ", " + typeStyle.arg(at4);
0074         Description += ' ' + paramStyle.arg(an4);
0075         NumArgs++;
0076     }
0077 
0078     if (!at5.isEmpty() && !an5.isEmpty())
0079     {
0080         Description += ", " + typeStyle.arg(at5);
0081         Description += ' ' + paramStyle.arg(an5);
0082         NumArgs++;
0083     }
0084 
0085     if (!at6.isEmpty() && !an6.isEmpty())
0086     {
0087         Description += ", " + typeStyle.arg(at6);
0088         Description += ' ' + paramStyle.arg(an6);
0089         NumArgs++;
0090     }
0091 
0092     //Set Valid=false if there are arguments (indicates that this fcn's args must be filled in)
0093     Valid = true;
0094     if (NumArgs)
0095         Valid = false;
0096 
0097     //Finish writing function prototype
0098     if (NumArgs)
0099         Description += ' ';
0100     Description += nameStyle.arg(")") + "</p><p>";
0101 
0102     //Add description
0103     Description += desc;
0104 
0105     //Finish up
0106     Description += "</p></body></html>";
0107 }
0108 
0109 //Copy constructor
0110 ScriptFunction::ScriptFunction(ScriptFunction *sf)
0111 {
0112     Name          = sf->name();
0113     Description   = sf->description();
0114     ClockFunction = sf->isClockFunction();
0115     NumArgs       = sf->numArgs();
0116     INDIProp      = sf->INDIProperty();
0117     Valid         = sf->valid();
0118 
0119     for (unsigned int i = 0; i < 6; i++)
0120     {
0121         ArgType[i]     = sf->argType(i);
0122         ArgName[i]     = sf->argName(i);
0123         ArgDBusType[i] = sf->argDBusType(i);
0124         //ArgVal[i] .clear();
0125         // JM: Some default argument values might be passed from another object as well
0126         ArgVal[i] = sf->argVal(i);
0127     }
0128 }
0129 
0130 QString ScriptFunction::DBusType(const QString &type)
0131 {
0132     if (type == QString("int"))
0133         return QString("int32");
0134     else if (type == QString("uint"))
0135         return QString("uint32");
0136     else if (type == QString("double"))
0137         return type;
0138     else if (type == QString("QString"))
0139         return QString("string");
0140     else if (type == QString("bool"))
0141         return QString("boolean");
0142 
0143     return nullptr;
0144 }
0145 
0146 QString ScriptFunction::prototype() const
0147 {
0148     QString p = Name + '(';
0149 
0150     bool args(false);
0151     if (!ArgType[0].isEmpty() && !ArgName[0].isEmpty())
0152     {
0153         p += ' ' + ArgType[0];
0154         p += ' ' + ArgName[0];
0155         args = true; //assume that if any args are present, 1st arg is present
0156     }
0157 
0158     if (!ArgType[1].isEmpty() && !ArgName[1].isEmpty())
0159     {
0160         p += ", " + ArgType[1];
0161         p += ' ' + ArgName[1];
0162     }
0163 
0164     if (!ArgType[2].isEmpty() && !ArgName[2].isEmpty())
0165     {
0166         p += ", " + ArgType[2];
0167         p += ' ' + ArgName[2];
0168     }
0169 
0170     if (!ArgType[3].isEmpty() && !ArgName[3].isEmpty())
0171     {
0172         p += ", " + ArgType[3];
0173         p += ' ' + ArgName[3];
0174     }
0175 
0176     if (!ArgType[4].isEmpty() && !ArgName[4].isEmpty())
0177     {
0178         p += ", " + ArgType[4];
0179         p += ' ' + ArgName[4];
0180     }
0181 
0182     if (!ArgType[5].isEmpty() && !ArgName[5].isEmpty())
0183     {
0184         p += ", " + ArgType[5];
0185         p += ' ' + ArgName[5];
0186     }
0187 
0188     if (args)
0189         p += ' ';
0190     p += ')';
0191 
0192     return p;
0193 }
0194 
0195 QString ScriptFunction::scriptLine() const
0196 {
0197     QString out(Name);
0198     unsigned int i = 0;
0199 
0200     while (i < 6 && !ArgName[i].isEmpty())
0201     {
0202         //Make sure strings are quoted
0203         QString value = ArgVal[i];
0204         if (ArgDBusType[i] == "string")
0205         {
0206             if (value.isEmpty())
0207             {
0208                 value = "\"\"";
0209             }
0210             else
0211             {
0212                 if (value.at(0) != '\"' && value.at(0) != '\'')
0213                 {
0214                     value = '\"' + value;
0215                 }
0216                 if (value.right(1) != "\"" && value.right(1) != "\'")
0217                 {
0218                     value = value + '\"';
0219                 }
0220             }
0221         }
0222 
0223         // Write DBus style prototype compatible with dbus-send format
0224         out += ' ' + ArgDBusType[i] + ':' + value;
0225         ++i;
0226     }
0227 
0228     return out;
0229 }