From 2034aa15c82570c989dfe5ab3647c55e8106e852 Mon Sep 17 00:00:00 2001 From: Arin Alex Date: Tue, 1 Nov 2016 20:42:45 +0300 Subject: [PATCH 1/6] QJSEngine has been added --- common.pri | 8 +- demo_r1/mainwindow.cpp | 4 +- include/lrglobal.h | 14 + include/lrreportengine.h | 1 + include/lrscriptenginemanagerintf.h | 11 +- limereport/items/lrtextitemeditor.cpp | 2 +- limereport/lrglobal.h | 14 + limereport/lritemdesignintf.cpp | 22 +- limereport/lrreportengine.cpp | 2 +- limereport/lrreportengine.h | 1 + limereport/lrreportrender.cpp | 2 +- limereport/lrscriptenginemanager.cpp | 463 +++++++++++++++++-- limereport/lrscriptenginemanager.h | 98 +++- limereport/lrscriptenginemanagerintf.h | 11 +- limereport/scriptbrowser/lrscriptbrowser.cpp | 2 +- 15 files changed, 595 insertions(+), 60 deletions(-) diff --git a/common.pri b/common.pri index 8eee2c5..78398e5 100644 --- a/common.pri +++ b/common.pri @@ -1,6 +1,6 @@ CONFIG += build_translations CONFIG += zint - +#CONFIG += qjsengine #greaterThan(QT_MAJOR_VERSION, 4) { # QT += uitools #} @@ -68,11 +68,15 @@ TRANSLATIONS_PATH = $$PWD/translations greaterThan(QT_MAJOR_VERSION, 4) { DEFINES+=HAVE_QT5 - QT+= printsupport widgets + QT+= printsupport widgets qml contains(QT,uitools){ message(uitools) DEFINES += HAVE_UI_LOADER } + contains(CONFIG, qjsengine){ + message(qjsengine) + DEFINES += USE_QJSENGINE + } } lessThan(QT_MAJOR_VERSION, 5){ diff --git a/demo_r1/mainwindow.cpp b/demo_r1/mainwindow.cpp index b5afab8..0634a15 100644 --- a/demo_r1/mainwindow.cpp +++ b/demo_r1/mainwindow.cpp @@ -93,8 +93,8 @@ MainWindow::MainWindow(QWidget *parent) : report->dataManager()->addModel("string_list",stringListModel,true); QStringList strList; strList<<"value1"<<"value2"; - QScriptValue value = qScriptValueFromSequence(report->scriptManager()->scriptEngine(),strList); - report->scriptManager()->scriptEngine()->globalObject().setProperty("test_list",value); + //QScriptValue value = qScriptValueFromSequence(report->scriptManager()->scriptEngine(),strList); + //report->scriptManager()->scriptEngine()->globalObject().setProperty("test_list",value); } diff --git a/include/lrglobal.h b/include/lrglobal.h index e64cd43..ea1d3ce 100644 --- a/include/lrglobal.h +++ b/include/lrglobal.h @@ -42,6 +42,12 @@ # define LIMEREPORT_EXPORT /**/ #endif +#ifdef USE_QJSENGINE +#include +#else +#include +#endif + namespace LimeReport { #ifdef __GNUC__ @@ -83,6 +89,7 @@ namespace Const{ const QString GROUP_FUNCTION_RX = "(%1\\s*"+GROUP_FUNCTION_PARAM_RX+")"; const QString GROUP_FUNCTION_NAME_RX = "%1\\s*\\((.*[^\\)])\\)"; const int SCENE_MARGIN = 50; + const QString FUNCTION_MANAGER_NAME = "LimeReport"; } QString extractClassName(QString className); enum RenderPass {FirstPass, SecondPass}; @@ -117,6 +124,13 @@ namespace Const{ typedef QStyleOptionViewItem StyleOptionViewItem; #endif +#ifdef USE_QJSENGINE + typedef QJSEngine ScriptEngineType; + typedef QJSValue ScriptValueType; +#else + typedef QScriptEngine ScriptEngineType; + typedef QScriptValue ScriptValueType; +#endif } // namespace LimeReport diff --git a/include/lrreportengine.h b/include/lrreportengine.h index 814d831..804e764 100644 --- a/include/lrreportengine.h +++ b/include/lrreportengine.h @@ -33,6 +33,7 @@ #include #include #include +//#include #include "lrglobal.h" #include "lrdatasourcemanagerintf.h" diff --git a/include/lrscriptenginemanagerintf.h b/include/lrscriptenginemanagerintf.h index d7662ff..4c2767a 100644 --- a/include/lrscriptenginemanagerintf.h +++ b/include/lrscriptenginemanagerintf.h @@ -30,15 +30,19 @@ #ifndef LRSCRIPTENGINEMANAGERINTF_H #define LRSCRIPTENGINEMANAGERINTF_H -#include +//#include +#include "lrglobal.h" namespace LimeReport{ + class IScriptEngineManager{ public: - virtual QScriptEngine* scriptEngine() = 0; - virtual bool addFunction(const QString& name, QScriptEngine::FunctionSignature function, + virtual ScriptEngineType* scriptEngine() = 0; +#ifndef USE_QJSENGINE + virtual bool addFunction(const QString& name, ScriptEngineType::FunctionSignature function, const QString& category="", const QString& description="") = 0; +#endif virtual bool addFunction(const QString &name, const QString& script, const QString &category="", const QString &description="") = 0; virtual const QString& lastError() const = 0; @@ -46,4 +50,5 @@ public: }; } //namespace LimeReport + #endif // LRSCRIPTENGINEMANAGERINTF_H diff --git a/limereport/items/lrtextitemeditor.cpp b/limereport/items/lrtextitemeditor.cpp index e2b6778..cba99ff 100644 --- a/limereport/items/lrtextitemeditor.cpp +++ b/limereport/items/lrtextitemeditor.cpp @@ -133,7 +133,7 @@ void TextItemEditor::initUI() ui->tabWidget->setVisible(false); } - foreach (LimeReport::ScriptFunctionDesc functionDesc, se.functionsDescriber()) { + foreach (LimeReport::ScriptFunctionDesc functionDesc, se.functionsDescribers()) { dataWords< +#else +#include +#endif + namespace LimeReport { #ifdef __GNUC__ @@ -83,6 +89,7 @@ namespace Const{ const QString GROUP_FUNCTION_RX = "(%1\\s*"+GROUP_FUNCTION_PARAM_RX+")"; const QString GROUP_FUNCTION_NAME_RX = "%1\\s*\\((.*[^\\)])\\)"; const int SCENE_MARGIN = 50; + const QString FUNCTION_MANAGER_NAME = "LimeReport"; } QString extractClassName(QString className); enum RenderPass {FirstPass, SecondPass}; @@ -117,6 +124,13 @@ namespace Const{ typedef QStyleOptionViewItem StyleOptionViewItem; #endif +#ifdef USE_QJSENGINE + typedef QJSEngine ScriptEngineType; + typedef QJSValue ScriptValueType; +#else + typedef QScriptEngine ScriptEngineType; + typedef QScriptValue ScriptValueType; +#endif } // namespace LimeReport diff --git a/limereport/lritemdesignintf.cpp b/limereport/lritemdesignintf.cpp index db18eb2..655c39b 100644 --- a/limereport/lritemdesignintf.cpp +++ b/limereport/lritemdesignintf.cpp @@ -215,27 +215,27 @@ QString ContentItemDesignIntf::expandScripts(QString context, DataSourceManager* if (context.contains(rx)){ ScriptEngineManager::instance().setDataManager(dataManager); - QScriptEngine* se = ScriptEngineManager::instance().scriptEngine(); + ScriptEngineType* se = ScriptEngineManager::instance().scriptEngine(); - QScriptValue svThis = se->globalObject().property("THIS"); - if (svThis.isValid()){ - se->newQObject(svThis, this); - } else { - svThis = se->newQObject(this); - se->globalObject().setProperty("THIS",svThis); - } + //QJSValue svThis = se->globalObject().property("THIS"); + //if (!svThis.isUndefined()){ + // se->newQObject(svThis, this); + //} else { + ScriptValueType svThis = se->newQObject(this); + se->globalObject().setProperty("THIS",svThis); + //} ScriptExtractor scriptExtractor(context); if (scriptExtractor.parse()){ for(int i=0; ievaluate(scriptBody); - if (!se->hasUncaughtException()) { + ScriptValueType value = se->evaluate(scriptBody); + if (!value.isError()) { m_varValue = value.toVariant(); context.replace(scriptExtractor.scriptAt(i),value.toString()); } else { - context.replace(scriptExtractor.scriptAt(i),se->uncaughtException().toString()); + context.replace(scriptExtractor.scriptAt(i),value.toString()); } } } diff --git a/limereport/lrreportengine.cpp b/limereport/lrreportengine.cpp index 7ff4095..2ce78ee 100644 --- a/limereport/lrreportengine.cpp +++ b/limereport/lrreportengine.cpp @@ -775,7 +775,7 @@ IDataSourceManager *ReportEngine::dataManager() return d->dataManagerIntf(); } -IScriptEngineManager* ReportEngine::scriptManager() +IScriptEngineManager *ReportEngine::scriptManager() { Q_D(ReportEngine); return d->scriptManagerIntf(); diff --git a/limereport/lrreportengine.h b/limereport/lrreportengine.h index 814d831..804e764 100644 --- a/limereport/lrreportengine.h +++ b/limereport/lrreportengine.h @@ -33,6 +33,7 @@ #include #include #include +//#include #include "lrglobal.h" #include "lrdatasourcemanagerintf.h" diff --git a/limereport/lrreportrender.cpp b/limereport/lrreportrender.cpp index 42dacef..1f4924c 100644 --- a/limereport/lrreportrender.cpp +++ b/limereport/lrreportrender.cpp @@ -167,7 +167,7 @@ void ReportRender::setScriptContext(ScriptEngineContext* scriptContext) bool ReportRender::runInitScript(){ if (m_scriptEngineContext){ - QScriptValue res = ScriptEngineManager::instance().scriptEngine()->evaluate(m_scriptEngineContext->initScript()); + ScriptValueType res = ScriptEngineManager::instance().scriptEngine()->evaluate(m_scriptEngineContext->initScript()); if (res.isBool()) return res.toBool(); } return true; diff --git a/limereport/lrscriptenginemanager.cpp b/limereport/lrscriptenginemanager.cpp index 976aa22..6aaedca 100644 --- a/limereport/lrscriptenginemanager.cpp +++ b/limereport/lrscriptenginemanager.cpp @@ -181,7 +181,7 @@ void ScriptEngineModel::updateModel() beginResetModel(); m_rootNode->clear(); QMap categories; - foreach(ScriptFunctionDesc funcDesc, m_scriptManager->functionsDescriber()){ + foreach(ScriptFunctionDesc funcDesc, m_scriptManager->functionsDescribers()){ ScriptEngineNode* categ; QString categoryName = (!funcDesc.category.isEmpty())?funcDesc.category:"NO CATEGORY"; if (categories.contains(categoryName)){ @@ -328,6 +328,44 @@ void ScriptEngineManager::deleteFunction(const QString &functionsName) } } +bool ScriptEngineManager::addFunction(const JSFunctionDesc &functionDescriber) +{ + ScriptValueType functionManager = scriptEngine()->globalObject().property(functionDescriber.managerName()); +#ifdef USE_QJSENGINE + if (functionManager.isUndefined()){ +#else + if (!functionManager.isValid()){ +#endif + functionManager = scriptEngine()->newQObject(functionDescriber.manager()); + scriptEngine()->globalObject().setProperty( + functionDescriber.managerName(), + functionManager + ); + } + + if (functionManager.toQObject() == functionDescriber.manager()){ + ScriptValueType checkWrapper = scriptEngine()->evaluate(functionDescriber.scriptWrapper()); + if (!checkWrapper.isError()){ + ScriptFunctionDesc funct; + funct.name = functionDescriber.name(); + funct.description = functionDescriber.description(); + funct.category = functionDescriber.category(); + funct.type = ScriptFunctionDesc::Native; + m_functions.append(funct); + if (m_model) + m_model->updateModel(); + return true; + } else { + m_lastError = checkWrapper.toString(); + return false; + } + } else { + m_lastError = tr("Function manger with name \"%1\" already exists !"); + return false; + } + +} + bool ScriptEngineManager::containsFunction(const QString& functionName){ foreach (ScriptFunctionDesc funct, m_functions) { if (funct.name.compare(functionName)== 0){ @@ -337,7 +375,8 @@ bool ScriptEngineManager::containsFunction(const QString& functionName){ return false; } -bool ScriptEngineManager::addFunction(const QString& name, +#ifndef USE_QJSENGINE +Q_DECL_DEPRECATED bool ScriptEngineManager::addFunction(const QString& name, QScriptEngine::FunctionSignature function, const QString& category, const QString& description) @@ -360,23 +399,23 @@ bool ScriptEngineManager::addFunction(const QString& name, return false; } } +#endif bool ScriptEngineManager::addFunction(const QString& name, const QString& script, const QString& category, const QString& description) { - QScriptSyntaxCheckResult cr = m_scriptEngine->checkSyntax(script); - if (cr.state() == QScriptSyntaxCheckResult::Valid){ + ScriptValueType functionValue = m_scriptEngine->evaluate(script); + if (!functionValue.isError()){ ScriptFunctionDesc funct; - funct.scriptValue = m_scriptEngine->evaluate(script); + funct.scriptValue = functionValue; funct.name = name; funct.category = category; funct.description = description; funct.type = ScriptFunctionDesc::Script; - funct.scriptValue.setData(m_scriptEngine->toScriptValue(this)); m_functions.append(funct); m_model->updateModel(); return true; } else { - m_lastError = cr.errorMessage(); + m_lastError = functionValue.toString(); return false; } } @@ -395,12 +434,18 @@ void ScriptEngineManager::setDataManager(DataSourceManager *dataManager){ m_dataManager = dataManager; if (m_dataManager){ foreach(QString func, m_dataManager->groupFunctionNames()){ - if (isFunctionExists(func)) deleteFunction(func); - addFunction(func, groupFunction,"GROUP FUNCTIONS", func+"(\""+tr("FieldName")+"\",\""+tr("BandName")+"\")"); - } - foreach(ScriptFunctionDesc func, m_functions){ - if (func.type==ScriptFunctionDesc::Native) - m_scriptEngine->globalObject().setProperty(func.name,func.scriptValue); + JSFunctionDesc describer( + func, + "GROUP FUNCTIONS", + func+"(\""+tr("FieldName")+"\",\""+tr("BandName")+"\")", + LimeReport::Const::FUNCTION_MANAGER_NAME, + m_functionManager, + QString("function %1(fieldName,bandName){\ + return %2.calcGroupFunction(\"%1\",fieldName,bandName);}" + ).arg(func) + .arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + addFunction(describer); } } } @@ -411,27 +456,211 @@ void ScriptEngineManager::updateModel() } +bool ScriptEngineManager::createLineFunction() +{ + JSFunctionDesc fd; + + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory("SYSTEM"); + fd.setName("line"); + fd.setDescription("line(\""+tr("BandName")+"\")"); + fd.setScriptWrapper(QString("function line(bandName){ return %1.line(bandName);}").arg(LimeReport::Const::FUNCTION_MANAGER_NAME)); + + return addFunction(fd); + +} + +bool ScriptEngineManager::createNumberFomatFunction() +{ + JSFunctionDesc fd; + + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory("NUMBER"); + fd.setName("numberFormat"); + fd.setDescription("numberFormat(\""+tr("Value")+"\",\""+tr("Format")+"\",\""+ + tr("Precision")+"\",\""+ + tr("Locale")+"\")" + ); + fd.setScriptWrapper(QString("function numberFormat(value, format, precision, locale){" + " if(typeof(format)==='undefined') format = \"f\"; " + " if(typeof(precision)==='undefined') precision=2; " + " if(typeof(locale)==='undefined') locale=\"\"; " + "return %1.numberFormat(value,format,precision,locale);}" + ).arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + return addFunction(fd); +} + +bool ScriptEngineManager::createDateFormatFunction(){ +// addFunction("dateFormat",dateFormat,"DATE&TIME", "dateFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); + JSFunctionDesc fd; + + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory("DATE&TIME"); + fd.setName("dateFormat"); + fd.setDescription("dateFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); + fd.setScriptWrapper(QString("function dateFormat(value, format){" + " if(typeof(format)==='undefined') format = \"dd.MM.yyyy\"; " + "return %1.dateFormat(value,format);}" + ).arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + return addFunction(fd); +} + +bool ScriptEngineManager::createTimeFormatFunction(){ +// addFunction("timeFormat",timeFormat,"DATE&TIME", "dateFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); + JSFunctionDesc fd; + + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory("DATE&TIME"); + fd.setName("timeFormat"); + fd.setDescription("timeFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); + fd.setScriptWrapper(QString("function timeFormat(value, format){" + " if(typeof(format)==='undefined') format = \"hh:mm\"; " + "return %1.timeFormat(value,format);}" + ).arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + return addFunction(fd); +} + +bool ScriptEngineManager::createDateTimeFormatFunction(){ +// addFunction("dateTimeFormat", dateTimeFormat, "DATE&TIME", "dateTimeFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); + JSFunctionDesc fd; + + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory("DATE&TIME"); + fd.setName("dateTimeFormat"); + fd.setDescription("dateTimeFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); + fd.setScriptWrapper(QString("function dateTimeFormat(value, format){" + " if(typeof(format)==='undefined') format = \"dd.MM.yyyy hh:mm\"; " + "return %1.dateTimeFormat(value,format);}" + ).arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + return addFunction(fd); +} + +bool ScriptEngineManager::createDateFunction(){ +// addFunction("date",date,"DATE&TIME","date()"); + JSFunctionDesc fd; + + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory("DATE&TIME"); + fd.setName("date"); + fd.setDescription("date()"); + fd.setScriptWrapper(QString("function date(){" + "return %1.date();}" + ).arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + return addFunction(fd); +} + + +bool ScriptEngineManager::createNowFunction(){ +// addFunction("now",now,"DATE&TIME","now()"); + JSFunctionDesc fd; + + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory("DATE&TIME"); + fd.setName("now"); + fd.setDescription("now()"); + fd.setScriptWrapper(QString("function now(){" + "return %1.now();}" + ).arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + return addFunction(fd); +} + +bool ScriptEngineManager::createCurrencyFormatFunction(){ +// addFunction("currencyFormat",currencyFormat,"NUMBER","currencyFormat(\""+tr("Value")+"\",\""+tr("Locale")+"\")"); + JSFunctionDesc fd; + + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory("NUMBER"); + fd.setName("currencyFormat"); + fd.setDescription("currencyFormat(\""+tr("Value")+"\",\""+tr("Locale")+"\")"); + fd.setScriptWrapper(QString("function currencyFormat(value, locale){" + " if(typeof(locale)==='undefined') locale = \"\"; " + "return %1.currencyFormat(value,locale);}" + ).arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + return addFunction(fd); +} + +bool ScriptEngineManager::createCurrencyUSBasedFormatFunction(){ +// addFunction("currencyUSBasedFormat",currencyUSBasedFormat,"NUMBER","currencyUSBasedFormat(\""+tr("Value")+",\""+tr("CurrencySymbol")+"\")"); + JSFunctionDesc fd; + + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory("NUMBER"); + fd.setName("currencyUSBasedFormat"); + fd.setDescription("currencyUSBasedFormat(\""+tr("Value")+",\""+tr("CurrencySymbol")+"\")"); + fd.setScriptWrapper(QString("function currencyUSBasedFormat(value, currencySymbol){" + " if(typeof(currencySymbol)==='undefined') currencySymbol = \"\"; " + "return %1.currencyFormat(value,currencySymbol);}" + ).arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + return addFunction(fd); +} + +bool ScriptEngineManager::createSetVariableFunction(){ +// addFunction("setVariable", setVariable, "GENERAL", "setVariable(\""+tr("Name")+"\",\""+tr("Value")+"\")"); + JSFunctionDesc fd; + + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory("GENERAL"); + fd.setName("setVariable"); + fd.setDescription("setVariable(\""+tr("Name")+"\",\""+tr("Value")+"\")"); + fd.setScriptWrapper(QString("function setVariable(name, value){" + "return %1.setVariable(name,value);}" + ).arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + return addFunction(fd); +} + ScriptEngineManager::ScriptEngineManager() :m_model(0), m_dataManager(0) { - m_scriptEngine = new QScriptEngine; + m_scriptEngine = new ScriptEngineType; + m_functionManager = new ScriptFunctionsManager(this); + m_functionManager->setScriptEngineManager(this); - //addFunction("dateToStr",dateToStr,"DATE", "dateToStr(\"value\",\"format\")"); - addFunction("line",line,"SYSTEM", "line(\""+tr("BandName")+"\")"); - addFunction("numberFormat",numberFormat,"NUMBER", "numberFormat(\""+tr("Value")+"\",\""+tr("Format")+"\",\""+ - tr("Precision")+"\",\""+ - tr("Locale")+"\")"); - addFunction("dateFormat",dateFormat,"DATE&TIME", "dateFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); - addFunction("timeFormat",timeFormat,"DATE&TIME", "dateFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); - addFunction("dateTimeFormat", dateTimeFormat, "DATE&TIME", "dateTimeFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); - addFunction("date",date,"DATE&TIME","date()"); - addFunction("now",now,"DATE&TIME","now()"); + createLineFunction(); + createNumberFomatFunction(); + createDateFormatFunction(); + createTimeFormatFunction(); + createDateTimeFormatFunction(); + createDateFunction(); + createNowFunction(); #if QT_VERSION>0x040800 - addFunction("currencyFormat",currencyFormat,"NUMBER","currencyFormat(\""+tr("Value")+"\",\""+tr("Locale")+"\")"); - addFunction("currencyUSBasedFormat",currencyUSBasedFormat,"NUMBER","currencyUSBasedFormat(\""+tr("Value")+",\""+tr("CurrencySymbol")+"\")"); + createCurrencyFormatFunction(); + createCurrencyUSBasedFormatFunction(); #endif - addFunction("setVariable", setVariable, "GENERAL", "setVariable(\""+tr("Name")+"\",\""+tr("Value")+"\")"); + createSetVariableFunction(); +// addFunction("line",line,"SYSTEM", "line(\""+tr("BandName")+"\")"); +// addFunction("numberFormat",numberFormat,"NUMBER", "numberFormat(\""+tr("Value")+"\",\""+tr("Format")+"\",\""+ +// tr("Precision")+"\",\""+ +// tr("Locale")+"\")"); + +// addFunction("dateFormat",dateFormat,"DATE&TIME", "dateFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); +// addFunction("timeFormat",timeFormat,"DATE&TIME", "dateFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); +// addFunction("dateTimeFormat", dateTimeFormat, "DATE&TIME", "dateTimeFormat(\""+tr("Value")+"\",\""+tr("Format")+"\")"); +// addFunction("date",date,"DATE&TIME","date()"); +// addFunction("now",now,"DATE&TIME","now()"); +// addFunction("currencyFormat",currencyFormat,"NUMBER","currencyFormat(\""+tr("Value")+"\",\""+tr("Locale")+"\")"); +// addFunction("currencyUSBasedFormat",currencyUSBasedFormat,"NUMBER","currencyUSBasedFormat(\""+tr("Value")+",\""+tr("CurrencySymbol")+"\")"); +// addFunction("setVariable", setVariable, "GENERAL", "setVariable(\""+tr("Name")+"\",\""+tr("Value")+"\")"); +#ifndef USE_QJSENGINE QScriptValue colorCtor = m_scriptEngine->newFunction(constructColor); m_scriptEngine->globalObject().setProperty("QColor", colorCtor); @@ -439,7 +668,7 @@ ScriptEngineManager::ScriptEngineManager() m_scriptEngine->setDefaultPrototype(qMetaTypeId(), fontProto); QScriptValue fontConstructor = m_scriptEngine->newFunction(QFontPrototype::constructorQFont, fontProto); m_scriptEngine->globalObject().setProperty("QFont", fontConstructor); - +#endif // foreach(QString func, dataManager()->groupFunctionNames()){ // addFunction(func, groupFunction,"GROUP FUNCTIONS", func+"(\""+tr("FieldName")+"\",\""+tr("BandName")+"\")"); // } @@ -753,5 +982,183 @@ DialogDescriber::Ptr DialogDescriber::create(const QString& name, const QByteArr return res; } +QString JSFunctionDesc::name() const +{ + return m_name; +} + +void JSFunctionDesc::setName(const QString &name) +{ + m_name = name; +} + +QString JSFunctionDesc::category() const +{ + return m_category; +} + +void JSFunctionDesc::setCategory(const QString &category) +{ + m_category = category; +} + +QString JSFunctionDesc::description() const +{ + return m_description; +} + +void JSFunctionDesc::setDescription(const QString &description) +{ + m_description = description; +} + +QString JSFunctionDesc::managerName() const +{ + return m_managerName; +} + +void JSFunctionDesc::setManagerName(const QString &managerName) +{ + m_managerName = managerName; +} + +QObject *JSFunctionDesc::manager() const +{ + return m_manager; +} + +void JSFunctionDesc::setManager(QObject *manager) +{ + m_manager = manager; +} + +QString JSFunctionDesc::scriptWrapper() const +{ + return m_scriptWrapper; +} + +void JSFunctionDesc::setScriptWrapper(const QString &scriptWrapper) +{ + m_scriptWrapper = scriptWrapper; +} + +QVariant ScriptFunctionsManager::calcGroupFunction(const QString &name, const QString &fieldName, const QString &bandName) +{ + if (m_scriptEngineManager->dataManager()){ + GroupFunction* gf = m_scriptEngineManager->dataManager()->groupFunction(name,fieldName,bandName); + if (gf){ + if (gf->isValid()){ + return gf->calculate(); + }else{ + return gf->error(); + } + } + else { + return QString(QObject::tr("Function %1 not found or have wrong arguments").arg(name)); + } + } else { + return QString(QObject::tr("Datasource manager not found")); + } +} + +QVariant ScriptFunctionsManager::line(const QString &bandName) +{ + QString varName = QLatin1String("line_")+bandName.toLower(); + QVariant res; + if (scriptEngineManager()->dataManager()->variable(varName).isValid()){ + res=scriptEngineManager()->dataManager()->variable(varName); + } else res=QString("Variable line for band %1 not found").arg(bandName); + return res; +} + +QVariant ScriptFunctionsManager::numberFormat(QVariant value, const char &format, int precision, const QString& locale) +{ + return (locale.isEmpty())?QString::number(value.toDouble(),format,precision): + QLocale(locale).toString(value.toDouble(),format,precision); +} + +QVariant ScriptFunctionsManager::dateFormat(QVariant value, const QString &format) +{ + return QLocale().toString(value.toDate(),format); +} + +QVariant ScriptFunctionsManager::timeFormat(QVariant value, const QString &format) +{ + return QLocale().toString(value.toTime(),format); +} + +QVariant ScriptFunctionsManager::dateTimeFormat(QVariant value, const QString &format) +{ + return QLocale().toString(value.toDateTime(),format); +} + +QVariant ScriptFunctionsManager::date() +{ + return QDate::currentDate(); +} + +QVariant ScriptFunctionsManager::now() +{ + return QDateTime::currentDateTime(); +} + +QVariant ScriptFunctionsManager::currencyFormat(QVariant value, const QString &locale) +{ + QString l = (!locale.isEmpty())?locale:QLocale::system().name(); + return QLocale(l).toCurrencyString(value.toDouble()); +} + +QVariant ScriptFunctionsManager::currencyUSBasedFormat(QVariant value, const QString ¤cySymbol) +{ + QString CurrencySymbol = (!currencySymbol.isEmpty())?currencySymbol:QLocale::system().currencySymbol(); + // Format it using USA locale + QString vTempStr=QLocale(QLocale::English, QLocale::UnitedStates).toCurrencyString(value.toDouble()); + // Replace currency symbol if necesarry + if (CurrencySymbol!="") vTempStr.replace("$", CurrencySymbol); + return vTempStr; +} + +void ScriptFunctionsManager::setVariable(const QString &name, QVariant value) +{ + DataSourceManager* dm = scriptEngineManager()->dataManager(); + dm->changeVariable(name,value); +} +#ifdef USE_QJSENGINE +QFont ScriptFunctionsManager::font(const QString &family, int pointSize, bool bold, bool italic, bool underLine) +{ + QFont result (family, pointSize); + result.setBold(bold); + result.setItalic(italic); + result.setUnderline(underLine); + return result; +} +#endif +QFont ScriptFunctionsManager::font(QVariantMap params){ + if (!params.contains("family")){ + return QFont(); + } else { + QFont result(params.value("family").toString()); + if (params.contains("pointSize")) + result.setPointSize(params.value("pointSize").toInt()); + if (params.contains("bold")) + result.setBold(params.value("bold").toBool()); + if (params.contains("italic")) + result.setItalic(params.value("italic").toBool()); + if (params.contains("underline")) + result.setUnderline(params.value("underline").toBool()); + return result; + } +} + +ScriptEngineManager *ScriptFunctionsManager::scriptEngineManager() const +{ + return m_scriptEngineManager; +} + +void ScriptFunctionsManager::setScriptEngineManager(ScriptEngineManager *scriptEngineManager) +{ + m_scriptEngineManager = scriptEngineManager; +} + } //namespace LimeReport diff --git a/limereport/lrscriptenginemanager.h b/limereport/lrscriptenginemanager.h index 0c738d9..63a2f45 100644 --- a/limereport/lrscriptenginemanager.h +++ b/limereport/lrscriptenginemanager.h @@ -39,11 +39,15 @@ #include #include + +//#include + #ifdef HAVE_UI_LOADER #include #endif #include "base/lrsingleton.h" +#include "lrglobal.h" #include "lrscriptenginemanagerintf.h" #include "lrcollection.h" @@ -53,7 +57,7 @@ class DataSourceManager; struct ScriptFunctionDesc{ enum FuncType {Native,Script}; - QScriptValue scriptValue; + ScriptValueType scriptValue; QString name; QString description; QString category; @@ -169,22 +173,91 @@ private: QString m_initScript; }; +class JSFunctionDesc{ +public: + JSFunctionDesc(){} + JSFunctionDesc(const QString& functionName, + const QString& functionCategory, + const QString& functionDescription, + const QString& functionManagerName, + QObject* functionManger, + const QString& functionScriptWrapper + ): m_name(functionName), m_category(functionCategory), m_description(functionDescription), + m_managerName(functionManagerName), m_manager(functionManger), m_scriptWrapper(functionScriptWrapper) + {} + QString name() const; + void setName(const QString &name); + + QString category() const; + void setCategory(const QString &category); + + QString description() const; + void setDescription(const QString &description); + + QString managerName() const; + void setManagerName(const QString &managerName); + + QObject *manager() const; + void setManager(QObject *manager); + + QString scriptWrapper() const; + void setScriptWrapper(const QString &scriptWrapper); + +private: + QString m_name; + QString m_category; + QString m_description; + QString m_managerName; + QObject* m_manager; + QString m_scriptWrapper; +}; + +class ScriptFunctionsManager : public QObject{ + Q_OBJECT +public: + explicit ScriptFunctionsManager(QObject* parent = 0):QObject(parent){} + Q_INVOKABLE QVariant calcGroupFunction(const QString& name, const QString& fieldName, const QString& bandName); + Q_INVOKABLE QVariant line(const QString& bandName); + Q_INVOKABLE QVariant numberFormat(QVariant value, const char &format, int precision, const QString &locale); + Q_INVOKABLE QVariant dateFormat(QVariant value, const QString& format); + Q_INVOKABLE QVariant timeFormat(QVariant value, const QString& format); + Q_INVOKABLE QVariant dateTimeFormat(QVariant value, const QString& format); + Q_INVOKABLE QVariant date(); + Q_INVOKABLE QVariant now(); + Q_INVOKABLE QVariant currencyFormat(QVariant value, const QString& locale); + Q_INVOKABLE QVariant currencyUSBasedFormat(QVariant value, const QString& currencySymbol); + Q_INVOKABLE void setVariable(const QString& name, QVariant value); + Q_INVOKABLE QVariant color(const QString& color){ return QColor(color);} +#ifdef USE_QJSENGINE + Q_INVOKABLE QFont font(const QString& family, int pointSize = -1, bool bold = false, bool italic = false, bool underLine = false); +#endif + Q_INVOKABLE QFont font(QVariantMap params); + ScriptEngineManager *scriptEngineManager() const; + void setScriptEngineManager(ScriptEngineManager *scriptEngineManager); + static QColor createQColor(const QString& color){ return QColor(color);} +private: + ScriptEngineManager* m_scriptEngineManager; +}; + class ScriptEngineManager : public QObject, public Singleton, public IScriptEngineManager { Q_OBJECT public: - QScriptEngine* scriptEngine(){return m_scriptEngine;} - ~ScriptEngineManager(); friend class Singleton; + ScriptEngineType* scriptEngine(){return m_scriptEngine;} + ~ScriptEngineManager(); bool isFunctionExists(const QString& functionName) const; void deleteFunction(const QString& functionsName); - bool addFunction(const QString& name, QScriptEngine::FunctionSignature function, - const QString& category="", const QString& description=""); + + bool addFunction(const JSFunctionDesc& functionsDescriber); +#ifndef USE_QJSENGINE + bool addFunction(const QString &name, QScriptEngine::FunctionSignature function, const QString &category, const QString &description); +#endif bool addFunction(const QString &name, const QString& script, const QString &category="", const QString &description=""); const QString& lastError() const {return m_lastError;} QStringList functionsNames(); - const QList& functionsDescriber(){return m_functions;} + const QList& functionsDescribers(){return m_functions;} ScriptEngineModel* model(){return m_model;} void setContext(ScriptEngineContext* context){m_context=context;} DataSourceManager* dataManager() const {return m_dataManager;} @@ -195,14 +268,25 @@ protected: bool containsFunction(const QString &functionName); private: Q_DISABLE_COPY(ScriptEngineManager) + bool createLineFunction(); + bool createNumberFomatFunction(); + bool createDateFormatFunction(); + bool createTimeFormatFunction(); + bool createDateTimeFormatFunction(); + bool createDateFunction(); + bool createNowFunction(); + bool createCurrencyFormatFunction(); + bool createCurrencyUSBasedFormatFunction(); + bool createSetVariableFunction(); private: ScriptEngineManager(); - QScriptEngine* m_scriptEngine; + ScriptEngineType* m_scriptEngine; QString m_lastError; QList m_functions; ScriptEngineModel* m_model; ScriptEngineContext* m_context; DataSourceManager* m_dataManager; + ScriptFunctionsManager* m_functionManager; }; class ScriptExtractor diff --git a/limereport/lrscriptenginemanagerintf.h b/limereport/lrscriptenginemanagerintf.h index d7662ff..4c2767a 100644 --- a/limereport/lrscriptenginemanagerintf.h +++ b/limereport/lrscriptenginemanagerintf.h @@ -30,15 +30,19 @@ #ifndef LRSCRIPTENGINEMANAGERINTF_H #define LRSCRIPTENGINEMANAGERINTF_H -#include +//#include +#include "lrglobal.h" namespace LimeReport{ + class IScriptEngineManager{ public: - virtual QScriptEngine* scriptEngine() = 0; - virtual bool addFunction(const QString& name, QScriptEngine::FunctionSignature function, + virtual ScriptEngineType* scriptEngine() = 0; +#ifndef USE_QJSENGINE + virtual bool addFunction(const QString& name, ScriptEngineType::FunctionSignature function, const QString& category="", const QString& description="") = 0; +#endif virtual bool addFunction(const QString &name, const QString& script, const QString &category="", const QString &description="") = 0; virtual const QString& lastError() const = 0; @@ -46,4 +50,5 @@ public: }; } //namespace LimeReport + #endif // LRSCRIPTENGINEMANAGERINTF_H diff --git a/limereport/scriptbrowser/lrscriptbrowser.cpp b/limereport/scriptbrowser/lrscriptbrowser.cpp index ea38798..8719229 100644 --- a/limereport/scriptbrowser/lrscriptbrowser.cpp +++ b/limereport/scriptbrowser/lrscriptbrowser.cpp @@ -66,7 +66,7 @@ void ScriptBrowser::updateFunctionTree() { ScriptEngineManager* sm = reportEditor()->scriptManager(); QMap categ; - foreach(ScriptFunctionDesc fd, sm->functionsDescriber()){ + foreach(ScriptFunctionDesc fd, sm->functionsDescribers()){ QString functionCategory = (fd.category!="") ? fd.category : tr("NO CATEGORY"); if (categ.contains(functionCategory)){ QTreeWidgetItem* item = new QTreeWidgetItem(categ.value(fd.category),QStringList(fd.name)); From db87e5dc9ec9689e5286a31f98f2ea11bfe52e69 Mon Sep 17 00:00:00 2001 From: Arin Alex Date: Wed, 2 Nov 2016 11:15:05 +0300 Subject: [PATCH 2/6] Build with uitools has been fixed --- limereport/lrreportrender.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/limereport/lrreportrender.cpp b/limereport/lrreportrender.cpp index 1f4924c..6791398 100644 --- a/limereport/lrreportrender.cpp +++ b/limereport/lrreportrender.cpp @@ -295,9 +295,9 @@ void ReportRender::initVariables() #ifdef HAVE_UI_LOADER void ReportRender::initDialogs(){ if (m_scriptEngineContext){ - QScriptEngine* se = ScriptEngineManager::instance().scriptEngine(); + ScriptEngineType* se = ScriptEngineManager::instance().scriptEngine(); foreach(DialogDescriber::Ptr dialog, m_scriptEngineContext->dialogsDescriber()){ - QScriptValue sv = se->newQObject(m_scriptEngineContext->getDialog(dialog->name())); + ScriptValueType sv = se->newQObject(m_scriptEngineContext->getDialog(dialog->name())); se->globalObject().setProperty(dialog->name(),sv); } } From cf1b2a4146181f4224c5a7fb3abfd9d2bfc91d8f Mon Sep 17 00:00:00 2001 From: Arin Alex Date: Wed, 2 Nov 2016 14:54:36 +0300 Subject: [PATCH 3/6] qjsengine and uitools are switched on --- common.pri | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/common.pri b/common.pri index 78398e5..fcefb93 100644 --- a/common.pri +++ b/common.pri @@ -1,12 +1,12 @@ CONFIG += build_translations CONFIG += zint -#CONFIG += qjsengine -#greaterThan(QT_MAJOR_VERSION, 4) { -# QT += uitools -#} -#lessThan(QT_MAJOR_VERSION, 5){ -# CONFIG += uitools -#} +CONFIG += qjsengine +greaterThan(QT_MAJOR_VERSION, 4) { + QT += uitools +} +lessThan(QT_MAJOR_VERSION, 5){ + CONFIG += uitools +} ZINT_PATH = $$PWD/3rdparty/zint-2.4.4 From 97c1eb3f771bfcf1a21b78217c38c07554631ae4 Mon Sep 17 00:00:00 2001 From: Arin Alexander Date: Fri, 4 Nov 2016 21:42:15 +0300 Subject: [PATCH 4/6] Dialogs for QJSEngine has been fixed. Child objects registration has been added --- limereport/lrreportrender.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/limereport/lrreportrender.cpp b/limereport/lrreportrender.cpp index 6791398..fc08cc9 100644 --- a/limereport/lrreportrender.cpp +++ b/limereport/lrreportrender.cpp @@ -293,11 +293,24 @@ void ReportRender::initVariables() } #ifdef HAVE_UI_LOADER + +#ifdef USE_QJSENGINE +void registerChildObjects(ScriptEngineType* se, ScriptValueType* sv){ + foreach(QObject* obj, sv->toQObject()->children()){ + ScriptValueType child = se->newQObject(obj); + sv->setProperty(obj->objectName(),child); + registerChildObjects(se, &child); + } +} +#endif void ReportRender::initDialogs(){ if (m_scriptEngineContext){ ScriptEngineType* se = ScriptEngineManager::instance().scriptEngine(); foreach(DialogDescriber::Ptr dialog, m_scriptEngineContext->dialogsDescriber()){ ScriptValueType sv = se->newQObject(m_scriptEngineContext->getDialog(dialog->name())); +#ifdef USE_QJSENGINE + registerChildObjects(se,&sv); +#endif se->globalObject().setProperty(dialog->name(),sv); } } From 7406789cbb7484da7e63de55fdf6b4a3c3278b5d Mon Sep 17 00:00:00 2001 From: Arin Alexander Date: Sat, 11 Feb 2017 00:21:03 +0300 Subject: [PATCH 5/6] QJSEngine -> QQmlEngine --- include/lrglobal.h | 12 +- limereport/lrglobal.h | 12 +- limereport/lrreportrender.cpp | 33 ++- limereport/lrscriptenginemanager.cpp | 314 +++++++++++++++++---------- limereport/lrscriptenginemanager.h | 8 +- 5 files changed, 247 insertions(+), 132 deletions(-) diff --git a/include/lrglobal.h b/include/lrglobal.h index 82a0364..421f8d7 100644 --- a/include/lrglobal.h +++ b/include/lrglobal.h @@ -43,7 +43,8 @@ #endif #ifdef USE_QJSENGINE -#include +//#include +#include #else #include #endif @@ -138,8 +139,15 @@ namespace Const{ #endif #ifdef USE_QJSENGINE - typedef QJSEngine ScriptEngineType; + typedef QQmlEngine ScriptEngineType; typedef QJSValue ScriptValueType; + template + static inline QJSValue getCppOwnedJSValue(QJSEngine &e, T *p) + { + QJSValue res = e.newQObject(p); + QQmlEngine::setObjectOwnership(p, QQmlEngine::CppOwnership); + return res; + } #else typedef QScriptEngine ScriptEngineType; typedef QScriptValue ScriptValueType; diff --git a/limereport/lrglobal.h b/limereport/lrglobal.h index 82a0364..421f8d7 100644 --- a/limereport/lrglobal.h +++ b/limereport/lrglobal.h @@ -43,7 +43,8 @@ #endif #ifdef USE_QJSENGINE -#include +//#include +#include #else #include #endif @@ -138,8 +139,15 @@ namespace Const{ #endif #ifdef USE_QJSENGINE - typedef QJSEngine ScriptEngineType; + typedef QQmlEngine ScriptEngineType; typedef QJSValue ScriptValueType; + template + static inline QJSValue getCppOwnedJSValue(QJSEngine &e, T *p) + { + QJSValue res = e.newQObject(p); + QQmlEngine::setObjectOwnership(p, QQmlEngine::CppOwnership); + return res; + } #else typedef QScriptEngine ScriptEngineType; typedef QScriptValue ScriptValueType; diff --git a/limereport/lrreportrender.cpp b/limereport/lrreportrender.cpp index 2d0701c..4092f82 100644 --- a/limereport/lrreportrender.cpp +++ b/limereport/lrreportrender.cpp @@ -167,17 +167,29 @@ void ReportRender::setScriptContext(ScriptEngineContext* scriptContext) bool ReportRender::runInitScript(){ if (m_scriptEngineContext){ - QScriptEngine* engine = ScriptEngineManager::instance().scriptEngine(); + ScriptEngineType* engine = ScriptEngineManager::instance().scriptEngine(); +#ifndef USE_QJSENGINE engine->pushContext(); - QScriptValue res = engine->evaluate(m_scriptEngineContext->initScript()); +#endif + ScriptValueType res = engine->evaluate(m_scriptEngineContext->initScript()); if (res.isBool()) return res.toBool(); +#ifdef USE_QJSENGINE + if (res.isError()){ + QMessageBox::critical(0,tr("Error"), + QString("Line %1: %2 ").arg(res.property("lineNumber").toString()) + .arg(res.toString()) + ); + return false; + } +#else if (engine->hasUncaughtException()) { QMessageBox::critical(0,tr("Error"), QString("Line %1: %2 ").arg(engine->uncaughtExceptionLineNumber()) .arg(engine->uncaughtException().toString()) ); return false; - } + } +#endif } return true; } @@ -269,8 +281,9 @@ void ReportRender::renderPage(PageDesignIntf* patternPage) renderBand(tearOffBand, 0, StartNewPageAsNeeded); savePage(true); - +#ifndef USE_QJSENGINE ScriptEngineManager::instance().scriptEngine()->popContext(); +#endif } } @@ -1262,18 +1275,26 @@ void ReportRender::baseDesignIntfToScript(BaseDesignIntf *item) if (item->metaObject()->indexOfSignal("afterRender()")!=-1) item->disconnect(SIGNAL(afterRender())); - QScriptEngine* engine = ScriptEngineManager::instance().scriptEngine(); - QScriptValue sItem = engine->globalObject().property(item->patternName()); + ScriptEngineType* engine = ScriptEngineManager::instance().scriptEngine(); + +#ifdef USE_QJSENGINE + //sItem = engine->newQObject(item); + ScriptValueType sItem = getCppOwnedJSValue(*engine, item); + engine->globalObject().setProperty(item->patternName(), sItem); +#else + ScriptValueType sItem = engine->globalObject().property(item->patternName()); if (sItem.isValid()){ engine->newQObject(sItem, item); } else { sItem = engine->newQObject(item); engine->globalObject().setProperty(item->patternName(),sItem); } +#endif foreach(BaseDesignIntf* child, item->childBaseItems()){ baseDesignIntfToScript(child); } } + } } diff --git a/limereport/lrscriptenginemanager.cpp b/limereport/lrscriptenginemanager.cpp index ad0d84d..dab7272 100644 --- a/limereport/lrscriptenginemanager.cpp +++ b/limereport/lrscriptenginemanager.cpp @@ -196,141 +196,141 @@ void ScriptEngineModel::updateModel() endResetModel(); } -QScriptValue line(QScriptContext* pcontext, QScriptEngine* pengine){ - ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); - DataSourceManager* dm = sm->dataManager(); - QString band = pcontext->argument(0).toString(); - QScriptValue res; - QString varName = QLatin1String("line_")+band.toLower(); - if (dm->variable(varName).isValid()){ - res=pengine->newVariant(dm->variable(varName)); - } else res=pengine->newVariant(QString("Variable line for band %1 not found").arg(band)); - return res; -} +//QScriptValue line(QScriptContext* pcontext, QScriptEngine* pengine){ +// ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); +// DataSourceManager* dm = sm->dataManager(); +// QString band = pcontext->argument(0).toString(); +// QScriptValue res; +// QString varName = QLatin1String("line_")+band.toLower(); +// if (dm->variable(varName).isValid()){ +// res=pengine->newVariant(dm->variable(varName)); +// } else res=pengine->newVariant(QString("Variable line for band %1 not found").arg(band)); +// return res; +//} -QScriptValue setVariable(QScriptContext* pcontext, QScriptEngine* /*pengine*/){ +//QScriptValue setVariable(QScriptContext* pcontext, QScriptEngine* /*pengine*/){ - QString name = pcontext->argument(0).toString(); - QVariant value = pcontext->argument(1).toVariant(); +// QString name = pcontext->argument(0).toString(); +// QVariant value = pcontext->argument(1).toVariant(); - ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); - DataSourceManager* dm = sm->dataManager(); +// ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); +// DataSourceManager* dm = sm->dataManager(); - dm->changeVariable(name,value); - return QScriptValue(); -} +// dm->changeVariable(name,value); +// return QScriptValue(); +//} -QScriptValue getVariable(QScriptContext* pcontext, QScriptEngine* pengine){ +//QScriptValue getVariable(QScriptContext* pcontext, QScriptEngine* pengine){ - QString name = pcontext->argument(0).toString(); +// QString name = pcontext->argument(0).toString(); - ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); - DataSourceManager* dm = sm->dataManager(); - QScriptValue res = pengine->newVariant(dm->variable(name)); +// ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); +// DataSourceManager* dm = sm->dataManager(); +// QScriptValue res = pengine->newVariant(dm->variable(name)); - return res; -} +// return res; +//} -QScriptValue getField(QScriptContext* pcontext, QScriptEngine* pengine){ +//QScriptValue getField(QScriptContext* pcontext, QScriptEngine* pengine){ - QString name = pcontext->argument(0).toString(); +// QString name = pcontext->argument(0).toString(); - ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); - DataSourceManager* dm = sm->dataManager(); - QScriptValue res = pengine->newVariant(dm->fieldData(name)); +// ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); +// DataSourceManager* dm = sm->dataManager(); +// QScriptValue res = pengine->newVariant(dm->fieldData(name)); - return res; -} +// return res; +//} -QScriptValue numberFormat(QScriptContext* pcontext, QScriptEngine* pengine){ - QVariant value = pcontext->argument(0).toVariant(); - char format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString()[0].toLatin1():'f'; - int precision = (pcontext->argumentCount()>2)?pcontext->argument(2).toInt32():2; - QString locale = (pcontext->argumentCount()>3)?pcontext->argument(3).toString():""; - QScriptValue res = (locale.isEmpty())?pengine->newVariant(QString::number(value.toDouble(),format,precision)): - pengine->newVariant(QLocale(locale).toString(value.toDouble(),format,precision)); - return res; -} -#if QT_VERSION>0x040800 -QScriptValue currencyFormat(QScriptContext* pcontext, QScriptEngine* pengine){ - QVariant value = pcontext->argument(0).toVariant(); - QString locale = (pcontext->argumentCount()>1)?pcontext->argument(1).toString():QLocale::system().name(); - return pengine->newVariant(QLocale(locale).toCurrencyString(value.toDouble())); -} +//QScriptValue numberFormat(QScriptContext* pcontext, QScriptEngine* pengine){ +// QVariant value = pcontext->argument(0).toVariant(); +// char format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString()[0].toLatin1():'f'; +// int precision = (pcontext->argumentCount()>2)?pcontext->argument(2).toInt32():2; +// QString locale = (pcontext->argumentCount()>3)?pcontext->argument(3).toString():""; +// QScriptValue res = (locale.isEmpty())?pengine->newVariant(QString::number(value.toDouble(),format,precision)): +// pengine->newVariant(QLocale(locale).toString(value.toDouble(),format,precision)); +// return res; +//} +//#if QT_VERSION>0x040800 +//QScriptValue currencyFormat(QScriptContext* pcontext, QScriptEngine* pengine){ +// QVariant value = pcontext->argument(0).toVariant(); +// QString locale = (pcontext->argumentCount()>1)?pcontext->argument(1).toString():QLocale::system().name(); +// return pengine->newVariant(QLocale(locale).toCurrencyString(value.toDouble())); +//} -QScriptValue currencyUSBasedFormat(QScriptContext* pcontext, QScriptEngine* pengine){ - QVariant value = pcontext->argument(0).toVariant(); - QString CurrencySymbol = (pcontext->argumentCount()>1)?pcontext->argument(1).toString():QLocale::system().currencySymbol(); - // Format it using USA locale - QString vTempStr=QLocale(QLocale::English, QLocale::UnitedStates).toCurrencyString(value.toDouble()); - // Replace currency symbol if necesarry - if (CurrencySymbol!="") vTempStr.replace("$", CurrencySymbol); - return pengine->newVariant(vTempStr); -} -#endif -QScriptValue dateFormat(QScriptContext* pcontext, QScriptEngine* pengine){ - QVariant value = pcontext->argument(0).toVariant(); - QString format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString().toLatin1():"dd.MM.yyyy"; - QScriptValue res = pengine->newVariant(QLocale().toString(value.toDate(),format)); - return res; -} +//QScriptValue currencyUSBasedFormat(QScriptContext* pcontext, QScriptEngine* pengine){ +// QVariant value = pcontext->argument(0).toVariant(); +// QString CurrencySymbol = (pcontext->argumentCount()>1)?pcontext->argument(1).toString():QLocale::system().currencySymbol(); +// // Format it using USA locale +// QString vTempStr=QLocale(QLocale::English, QLocale::UnitedStates).toCurrencyString(value.toDouble()); +// // Replace currency symbol if necesarry +// if (CurrencySymbol!="") vTempStr.replace("$", CurrencySymbol); +// return pengine->newVariant(vTempStr); +//} +//#endif +//QScriptValue dateFormat(QScriptContext* pcontext, QScriptEngine* pengine){ +// QVariant value = pcontext->argument(0).toVariant(); +// QString format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString().toLatin1():"dd.MM.yyyy"; +// QScriptValue res = pengine->newVariant(QLocale().toString(value.toDate(),format)); +// return res; +//} -QScriptValue timeFormat(QScriptContext* pcontext, QScriptEngine* pengine){ - QVariant value = pcontext->argument(0).toVariant(); - QString format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString().toLatin1():"hh:mm"; - QScriptValue res = pengine->newVariant(QLocale().toString(value.toTime(),format)); - return res; -} +//QScriptValue timeFormat(QScriptContext* pcontext, QScriptEngine* pengine){ +// QVariant value = pcontext->argument(0).toVariant(); +// QString format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString().toLatin1():"hh:mm"; +// QScriptValue res = pengine->newVariant(QLocale().toString(value.toTime(),format)); +// return res; +//} -QScriptValue dateTimeFormat(QScriptContext* pcontext, QScriptEngine* pengine){ - QVariant value = pcontext->argument(0).toVariant(); - QString format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString().toLatin1():"dd.MM.yyyy hh:mm"; - QScriptValue res = pengine->newVariant(QLocale().toString(value.toDateTime(),format)); - return res; -} +//QScriptValue dateTimeFormat(QScriptContext* pcontext, QScriptEngine* pengine){ +// QVariant value = pcontext->argument(0).toVariant(); +// QString format = (pcontext->argumentCount()>1)?pcontext->argument(1).toString().toLatin1():"dd.MM.yyyy hh:mm"; +// QScriptValue res = pengine->newVariant(QLocale().toString(value.toDateTime(),format)); +// return res; +//} -QScriptValue now(QScriptContext* /*pcontext*/, QScriptEngine* pengine){ - return pengine->newVariant(QDateTime::currentDateTime()); -} +//QScriptValue now(QScriptContext* /*pcontext*/, QScriptEngine* pengine){ +// return pengine->newVariant(QDateTime::currentDateTime()); +//} -QScriptValue date(QScriptContext* /*pcontext*/, QScriptEngine* pengine){ - return pengine->newVariant(QDate::currentDate()); -} +//QScriptValue date(QScriptContext* /*pcontext*/, QScriptEngine* pengine){ +// return pengine->newVariant(QDate::currentDate()); +//} -QScriptValue callGroupFunction(const QString& functionName, QScriptContext* pcontext, QScriptEngine* pengine){ +//QScriptValue callGroupFunction(const QString& functionName, QScriptContext* pcontext, QScriptEngine* pengine){ - ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); - DataSourceManager* dm = sm->dataManager(); +// ScriptEngineManager* sm = qscriptvalue_cast(pcontext->callee().data()); +// DataSourceManager* dm = sm->dataManager(); - QString expression; - QString band; +// QString expression; +// QString band; - if (functionName.compare("COUNT",Qt::CaseInsensitive) == 0 && pcontext->argumentCount()==1){ - expression = " "; - band = pcontext->argument(0).toString(); - } else { - expression = dm->getExpression(pcontext->argument(0).toString()); - band = pcontext->argument(1).toString(); - } +// if (functionName.compare("COUNT",Qt::CaseInsensitive) == 0 && pcontext->argumentCount()==1){ +// expression = " "; +// band = pcontext->argument(0).toString(); +// } else { +// expression = dm->getExpression(pcontext->argument(0).toString()); +// band = pcontext->argument(1).toString(); +// } - QScriptValue res; - GroupFunction* gf = dm->groupFunction(functionName,expression,band); - if (gf){ - if (gf->isValid()){ - res=pengine->newVariant(gf->calculate()); - }else{ - res=pengine->newVariant(gf->error()); - } - } - else { - res=pengine->newVariant(QString(QObject::tr("Function %1 not found or have wrong arguments").arg(functionName))); - } - return res; -} +// QScriptValue res; +// GroupFunction* gf = dm->groupFunction(functionName,expression,band); +// if (gf){ +// if (gf->isValid()){ +// res=pengine->newVariant(gf->calculate()); +// }else{ +// res=pengine->newVariant(gf->error()); +// } +// } +// else { +// res=pengine->newVariant(QString(QObject::tr("Function %1 not found or have wrong arguments").arg(functionName))); +// } +// return res; +//} -QScriptValue groupFunction(QScriptContext* pcontext, QScriptEngine* pengine){ - return callGroupFunction(pcontext->callee().property("functionName").toString(),pcontext,pengine); -} +//QScriptValue groupFunction(QScriptContext* pcontext, QScriptEngine* pengine){ +// return callGroupFunction(pcontext->callee().property("functionName").toString(),pcontext,pengine); +//} ScriptEngineManager::~ScriptEngineManager() { @@ -589,16 +589,24 @@ QString ScriptEngineManager::expandScripts(QString context, QVariant& varValue, if (ScriptEngineManager::instance().dataManager()!=dataManager()) ScriptEngineManager::instance().setDataManager(dataManager()); - QScriptEngine* se = ScriptEngineManager::instance().scriptEngine(); + ScriptEngineType* se = ScriptEngineManager::instance().scriptEngine(); if (reportItem){ - QScriptValue svThis = se->globalObject().property("THIS"); + + ScriptValueType svThis; + +#ifdef USE_QJSENGINE + svThis = getCppOwnedJSValue(*se, reportItem); + se->globalObject().setProperty("THIS",svThis); +#else + svThis = se->globalObject().property("THIS"); if (svThis.isValid()){ - se->newQObject(svThis, this); + se->newQObject(svThis, reportItem); } else { - svThis = se->newQObject(this); + svThis = se->newQObject(reportItem); se->globalObject().setProperty("THIS",svThis); } +#endif } ScriptExtractor scriptExtractor(context); @@ -606,16 +614,26 @@ QString ScriptEngineManager::expandScripts(QString context, QVariant& varValue, for(int i=0; ievaluate(scriptBody); + ScriptValueType value = se->evaluate(scriptBody); +#ifdef USE_QJSENGINE + if (!value.isError()){ + varValue = value.toVariant(); + context.replace(scriptExtractor.scriptAt(i),value.toString()); + } else { + context.replace(scriptExtractor.scriptAt(i),value.toString()); + } +#else if (!se->hasUncaughtException()) { varValue = value.toVariant(); context.replace(scriptExtractor.scriptAt(i),value.toString()); } else { context.replace(scriptExtractor.scriptAt(i),se->uncaughtException().toString()); } +#endif } } } + return context; } @@ -629,14 +647,18 @@ QVariant ScriptEngineManager::evaluateScript(const QString& script){ if (ScriptEngineManager::instance().dataManager()!=dataManager()) ScriptEngineManager::instance().setDataManager(dataManager()); - QScriptEngine* se = ScriptEngineManager::instance().scriptEngine(); + ScriptEngineType* se = ScriptEngineManager::instance().scriptEngine(); ScriptExtractor scriptExtractor(script); if (scriptExtractor.parse()){ QString scriptBody = expandDataFields(scriptExtractor.bodyAt(0),EscapeSymbols, varValue, 0); scriptBody = expandUserVariables(scriptBody, FirstPass, EscapeSymbols, varValue); - QScriptValue value = se->evaluate(scriptBody); + ScriptValueType value = se->evaluate(scriptBody); +#ifdef USE_QJSENGINE + if (!value.isError()){ +#else if (!se->hasUncaughtException()) { +#endif return value.toVariant(); } } @@ -820,6 +842,36 @@ bool ScriptEngineManager::createSetVariableFunction(){ return addFunction(fd); } +bool ScriptEngineManager::createGetVariableFunction() +{ + JSFunctionDesc fd; + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory("GENERAL"); + fd.setName("getVariable"); + fd.setDescription("getVariable(\""+tr("Name")+"\")"); + fd.setScriptWrapper(QString("function getVariable(name){" + "return %1.getVariable(name);}" + ).arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + return addFunction(fd); +} + +bool ScriptEngineManager::createGetFieldFunction() +{ + JSFunctionDesc fd; + fd.setManager(m_functionManager); + fd.setManagerName(LimeReport::Const::FUNCTION_MANAGER_NAME); + fd.setCategory("GENERAL"); + fd.setName("getField"); + fd.setDescription("getField(\""+tr("Name")+"\")"); + fd.setScriptWrapper(QString("function getField(name){" + "return %1.getField(name);}" + ).arg(LimeReport::Const::FUNCTION_MANAGER_NAME) + ); + return addFunction(fd); +} + ScriptEngineManager::ScriptEngineManager() :m_model(0), m_dataManager(0) { @@ -839,6 +891,8 @@ ScriptEngineManager::ScriptEngineManager() createCurrencyUSBasedFormatFunction(); #endif createSetVariableFunction(); + createGetFieldFunction(); + createGetVariableFunction(); // addFunction("line",line,"SYSTEM", "line(\""+tr("BandName")+"\")"); // addFunction("numberFormat",numberFormat,"NUMBER", "numberFormat(\""+tr("Value")+"\",\""+tr("Format")+"\",\""+ @@ -1235,10 +1289,17 @@ void JSFunctionDesc::setScriptWrapper(const QString &scriptWrapper) m_scriptWrapper = scriptWrapper; } -QVariant ScriptFunctionsManager::calcGroupFunction(const QString &name, const QString &fieldName, const QString &bandName) +QVariant ScriptFunctionsManager::calcGroupFunction(const QString &name, const QString &expressionID, const QString &bandName) { if (m_scriptEngineManager->dataManager()){ - GroupFunction* gf = m_scriptEngineManager->dataManager()->groupFunction(name,fieldName,bandName); + QString expression = ""; + if (name.compare("COUNT",Qt::CaseInsensitive) == 0){ + expression = " "; + } else { + expression = m_scriptEngineManager->dataManager()->getExpression(expressionID); + } + + GroupFunction* gf = m_scriptEngineManager->dataManager()->groupFunction(name,expression,bandName); if (gf){ if (gf->isValid()){ return gf->calculate(); @@ -1316,6 +1377,19 @@ void ScriptFunctionsManager::setVariable(const QString &name, QVariant value) DataSourceManager* dm = scriptEngineManager()->dataManager(); dm->changeVariable(name,value); } + +QVariant ScriptFunctionsManager::getVariable(const QString &name) +{ + DataSourceManager* dm = scriptEngineManager()->dataManager(); + return dm->variable(name); +} + +QVariant ScriptFunctionsManager::getField(const QString &field) +{ + DataSourceManager* dm = scriptEngineManager()->dataManager(); + return dm->fieldData(field); +} + #ifdef USE_QJSENGINE QFont ScriptFunctionsManager::font(const QString &family, int pointSize, bool bold, bool italic, bool underLine) { diff --git a/limereport/lrscriptenginemanager.h b/limereport/lrscriptenginemanager.h index b338bbb..726013b 100644 --- a/limereport/lrscriptenginemanager.h +++ b/limereport/lrscriptenginemanager.h @@ -216,7 +216,7 @@ class ScriptFunctionsManager : public QObject{ Q_OBJECT public: explicit ScriptFunctionsManager(QObject* parent = 0):QObject(parent){} - Q_INVOKABLE QVariant calcGroupFunction(const QString& name, const QString& fieldName, const QString& bandName); + Q_INVOKABLE QVariant calcGroupFunction(const QString& name, const QString& expressionID, const QString& bandName); Q_INVOKABLE QVariant line(const QString& bandName); Q_INVOKABLE QVariant numberFormat(QVariant value, const char &format, int precision, const QString &locale); Q_INVOKABLE QVariant dateFormat(QVariant value, const QString& format); @@ -226,7 +226,9 @@ public: Q_INVOKABLE QVariant now(); Q_INVOKABLE QVariant currencyFormat(QVariant value, const QString& locale); Q_INVOKABLE QVariant currencyUSBasedFormat(QVariant value, const QString& currencySymbol); - Q_INVOKABLE void setVariable(const QString& name, QVariant value); + Q_INVOKABLE void setVariable(const QString& name, QVariant value); + Q_INVOKABLE QVariant getVariable(const QString& name); + Q_INVOKABLE QVariant getField(const QString& field); Q_INVOKABLE QVariant color(const QString& color){ return QColor(color);} #ifdef USE_QJSENGINE Q_INVOKABLE QFont font(const QString& family, int pointSize = -1, bool bold = false, bool italic = false, bool underLine = false); @@ -283,6 +285,8 @@ private: bool createCurrencyFormatFunction(); bool createCurrencyUSBasedFormatFunction(); bool createSetVariableFunction(); + bool createGetVariableFunction(); + bool createGetFieldFunction(); private: ScriptEngineManager(); ScriptEngineType* m_scriptEngine; From d9285813ac2fd342879ce5f67b4cb106c4aeba23 Mon Sep 17 00:00:00 2001 From: Arin Alexander Date: Thu, 16 Feb 2017 04:11:39 +0300 Subject: [PATCH 6/6] Database connections have been refactored "defaultConnection" has been added to Designer keepDbCredentials property has been added to Connection setPassPharse(QString& passPharse) method has been added to ReportEngine IDbCredentialsProvider interface has been added registerDbCredentialsProvider(IDbCredentialsProvider *provider) has been added to IDataSourceManager --- include/lrdatasourcemanagerintf.h | 23 ++++--- include/lrreportengine.h | 1 + limereport/databrowser/lrconnectiondialog.cpp | 2 + limereport/databrowser/lrconnectiondialog.ui | 7 +++ limereport/lrdatadesignintf.cpp | 14 ++++- limereport/lrdatadesignintf.h | 10 ++- limereport/lrdatasourcemanager.cpp | 46 +++++++++++--- limereport/lrdatasourcemanager.h | 5 +- limereport/lrdatasourcemanagerintf.h | 23 ++++--- limereport/lrreportengine.cpp | 61 ++++++++++++++++++- limereport/lrreportengine.h | 1 + limereport/lrreportengine_p.h | 3 + limereport/lrsimplecrypt.cpp | 21 ++++++- limereport/lrsimplecrypt.h | 1 + limereport/serializators/lrserializatorintf.h | 10 ++- limereport/serializators/lrstorageintf.h | 26 ++++---- .../lrxmlbasetypesserializators.cpp | 4 +- .../lrxmlbasetypesserializators.h | 17 +++++- limereport/serializators/lrxmlreader.cpp | 9 +++ limereport/serializators/lrxmlreader.h | 24 +++++--- limereport/serializators/lrxmlwriter.cpp | 9 +++ limereport/serializators/lrxmlwriter.h | 4 ++ 22 files changed, 257 insertions(+), 64 deletions(-) diff --git a/include/lrdatasourcemanagerintf.h b/include/lrdatasourcemanagerintf.h index 85cf388..57bd0c1 100644 --- a/include/lrdatasourcemanagerintf.h +++ b/include/lrdatasourcemanagerintf.h @@ -37,18 +37,25 @@ class QString; class QAbstractItemModel; namespace LimeReport{ +class IDbCredentialsProvider{ +public: + virtual QString getUserName(const QString& connectionName) = 0; + virtual QString getPassword(const QString& connectionName) = 0; +}; + class IDataSourceManager{ public: - virtual void setReportVariable(const QString& name, const QVariant& value)=0; - virtual void setDefaultDatabasePath(const QString &defaultDatabasePath)=0; - virtual void deleteVariable(const QString& name)=0; - virtual bool containsVariable(const QString& variableName)=0; - virtual QVariant variable(const QString& variableName)=0; - virtual bool addModel(const QString& name, QAbstractItemModel *model, bool owned)=0; - virtual void removeModel(const QString& name)=0; - virtual bool containsDatasource(const QString& dataSourceName)=0; + virtual void setReportVariable(const QString& name, const QVariant& value) = 0; + virtual void setDefaultDatabasePath(const QString &defaultDatabasePath) = 0; + virtual void deleteVariable(const QString& name) = 0; + virtual bool containsVariable(const QString& variableName) = 0; + virtual QVariant variable(const QString& variableName) = 0; + virtual bool addModel(const QString& name, QAbstractItemModel *model, bool owned) = 0; + virtual void removeModel(const QString& name) = 0; + virtual bool containsDatasource(const QString& dataSourceName) = 0; virtual void clearUserVariables()=0; virtual ICallbackDatasource* createCallbackDatasouce(const QString& name) = 0; + virtual void registerDbCredentialsProvider(IDbCredentialsProvider* provider) = 0; //virtual void addCallbackDatasource(ICallbackDatasource* datasource, const QString& name) = 0; }; diff --git a/include/lrreportengine.h b/include/lrreportengine.h index 0d5b309..dbbb2a8 100644 --- a/include/lrreportengine.h +++ b/include/lrreportengine.h @@ -101,6 +101,7 @@ public: void setResultEditable(bool value); bool resultIsEditable(); bool isBusy(); + void setPassPharse(QString& passPharse); signals: void renderStarted(); void renderFinished(); diff --git a/limereport/databrowser/lrconnectiondialog.cpp b/limereport/databrowser/lrconnectiondialog.cpp index 60652cf..b90545e 100644 --- a/limereport/databrowser/lrconnectiondialog.cpp +++ b/limereport/databrowser/lrconnectiondialog.cpp @@ -122,6 +122,7 @@ ConnectionDesc *ConnectionDialog::uiToConnection(LimeReport::ConnectionDesc* con result ->setPassword(ui->lePassword->text()); result ->setDatabaseName(ui->leDataBase->text()); result ->setAutoconnect(ui->cbAutoConnect->isChecked()); + result->setKeepDBCredentials(!ui->cbbKeepCredentials->isChecked()); return result ; } @@ -137,6 +138,7 @@ void ConnectionDialog::connectionToUI() ui->lePassword->setText(m_connection->password()); ui->cbbDrivers->setCurrentIndex(ui->cbbDrivers->findText(m_connection->driver())); ui->cbAutoConnect->setChecked(m_connection->autoconnect()); + ui->cbbKeepCredentials->setChecked(!m_connection->keepDBCredentials()); } void ConnectionDialog::on_toolButton_clicked() diff --git a/limereport/databrowser/lrconnectiondialog.ui b/limereport/databrowser/lrconnectiondialog.ui index aa06393..60bf527 100644 --- a/limereport/databrowser/lrconnectiondialog.ui +++ b/limereport/databrowser/lrconnectiondialog.ui @@ -137,6 +137,13 @@ + + + + Dont keep credentals in lrxml + + + diff --git a/limereport/lrdatadesignintf.cpp b/limereport/lrdatadesignintf.cpp index ccff471..0d2d14b 100644 --- a/limereport/lrdatadesignintf.cpp +++ b/limereport/lrdatadesignintf.cpp @@ -351,13 +351,13 @@ void ModelToDataSource::slotModelDestroed() ConnectionDesc::ConnectionDesc(QSqlDatabase db, QObject *parent) : QObject(parent), m_connectionName(db.connectionName()), m_connectionHost(db.hostName()), m_connectionDriver(db.driverName()), m_databaseName(db.databaseName()), m_user(db.userName()), m_password(db.password()), m_autoconnect(false), - m_internal(false) + m_internal(false), m_keepDBCredentials(true) {} ConnectionDesc::ConnectionDesc(QObject *parent) :QObject(parent),m_connectionName(""),m_connectionHost(""),m_connectionDriver(""), m_databaseName(""), m_user(""), m_password(""), m_autoconnect(false), - m_internal(false) + m_internal(false), m_keepDBCredentials(true) {} ConnectionDesc::Ptr ConnectionDesc::create(QSqlDatabase db, QObject *parent) @@ -391,6 +391,16 @@ QString ConnectionDesc::connectionNameForReport(const QString &connectionName) return connectionName.compare(tr("defaultConnection")) == 0 ? QSqlDatabase::defaultConnection : connectionName; } +bool ConnectionDesc::keepDBCredentials() const +{ + return m_keepDBCredentials; +} + +void ConnectionDesc::setKeepDBCredentials(bool keepDBCredentals) +{ + m_keepDBCredentials = keepDBCredentals; +} + QueryDesc::QueryDesc(QString queryName, QString queryText, QString connection) :m_queryName(queryName), m_queryText(queryText), m_connectionName(connection) {} diff --git a/limereport/lrdatadesignintf.h b/limereport/lrdatadesignintf.h index d2c9685..e2f8134 100644 --- a/limereport/lrdatadesignintf.h +++ b/limereport/lrdatadesignintf.h @@ -110,6 +110,7 @@ class ConnectionDesc : public QObject{ Q_PROPERTY(QString password READ password WRITE setPassword) Q_PROPERTY(QString host READ host WRITE setHost) Q_PROPERTY(bool autoconnect READ autoconnect WRITE setAutoconnect) + Q_PROPERTY(bool keepDBCredentials READ keepDBCredentials WRITE setKeepDBCredentials) public: typedef QSharedPointer Ptr; ConnectionDesc(QSqlDatabase db, QObject* parent=0); @@ -124,17 +125,19 @@ public: void setDatabaseName(const QString &value){m_databaseName=value;} QString databaseName(){return m_databaseName;} void setUserName(const QString &value){m_user=value;} - QString userName(){return m_user;} + QString userName(){ return m_user; } void setPassword(const QString &value){m_password=value;} - QString password(){return m_password;} + QString password(){ return m_password; } void setAutoconnect(bool value){m_autoconnect=value;} bool autoconnect(){return m_autoconnect;} bool isEqual(const QSqlDatabase& db); bool isInternal(){ return m_internal; } void setInternal(bool value) {m_internal = value;} + bool keepDBCredentials() const; + void setKeepDBCredentials(bool keepDBCredentials); public: static QString connectionNameForUser(const QString& connectionName); - static QString connectionNameForReport(const QString& connectionName); + static QString connectionNameForReport(const QString& connectionName); signals: void nameChanged(const QString& oldName,const QString& newName); private: @@ -146,6 +149,7 @@ private: QString m_password; bool m_autoconnect; bool m_internal; + bool m_keepDBCredentials; }; class IConnectionController{ diff --git a/limereport/lrdatasourcemanager.cpp b/limereport/lrdatasourcemanager.cpp index 5607117..74dd69e 100644 --- a/limereport/lrdatasourcemanager.cpp +++ b/limereport/lrdatasourcemanager.cpp @@ -217,7 +217,7 @@ void DataSourceModel::updateModel() } DataSourceManager::DataSourceManager(QObject *parent) : - QObject(parent), m_lastError(""), m_designTime(true), m_needUpdate(false) + QObject(parent), m_lastError(""), m_designTime(true), m_needUpdate(false), m_dbCredentialsProvider(0) { m_groupFunctionFactory.registerFunctionCreator(QLatin1String("COUNT"),new ConstructorGroupFunctionCreator); m_groupFunctionFactory.registerFunctionCreator(QLatin1String("SUM"),new ConstructorGroupFunctionCreator); @@ -324,6 +324,11 @@ ICallbackDatasource *DataSourceManager::createCallbackDatasouce(const QString& n return ds; } +void DataSourceManager::registerDbCredentialsProvider(IDbCredentialsProvider *provider) +{ + m_dbCredentialsProvider = provider; +} + void DataSourceManager::addCallbackDatasource(ICallbackDatasource *datasource, const QString& name) { IDataSource* datasourceIntf = dynamic_cast(datasource); @@ -567,6 +572,11 @@ int DataSourceManager::connectionIndexByName(const QString &connectionName) return -1; } +QList& DataSourceManager::conections() +{ + return m_connections; +} + bool DataSourceManager::dataSourceIsValid(const QString &name) { if (m_datasources.value(name.toLower())) return !m_datasources.value(name.toLower())->isInvalid(); @@ -704,10 +714,19 @@ void DataSourceManager::putProxyDesc(ProxyDesc *proxyDesc) bool DataSourceManager::initAndOpenDB(QSqlDatabase& db, ConnectionDesc& connectionDesc){ bool connected = false; + + db.setHostName(replaceVariables(connectionDesc.host())); db.setUserName(replaceVariables(connectionDesc.userName())); db.setPassword(replaceVariables(connectionDesc.password())); + if (!connectionDesc.keepDBCredentials() && m_dbCredentialsProvider){ + if (!m_dbCredentialsProvider->getUserName(connectionDesc.name()).isEmpty()) + db.setUserName(m_dbCredentialsProvider->getUserName(connectionDesc.name())); + if (!m_dbCredentialsProvider->getPassword(connectionDesc.name()).isEmpty()) + db.setPassword(m_dbCredentialsProvider->getPassword(connectionDesc.name())); + } + QString dbName = replaceVariables(connectionDesc.databaseName()); if (connectionDesc.driver().compare("QSQLITE")==0){ if (!defaultDatabasePath().isEmpty()){ @@ -726,7 +745,6 @@ bool DataSourceManager::initAndOpenDB(QSqlDatabase& db, ConnectionDesc& connecti } connected=db.open(); - connectionDesc.setInternal(true); if (!connected) setLastError(db.lastError().text()); return connected; } @@ -758,10 +776,17 @@ bool DataSourceManager::connectConnection(ConnectionDesc *connectionDesc) } if (!QSqlDatabase::contains(connectionDesc->name())){ - QSqlDatabase db = QSqlDatabase::addDatabase(connectionDesc->driver(),connectionDesc->name()); - connected=initAndOpenDB(db, *connectionDesc); + QString dbError; + { + QSqlDatabase db = QSqlDatabase::addDatabase(connectionDesc->driver(),connectionDesc->name()); + connectionDesc->setInternal(true); + connected=initAndOpenDB(db, *connectionDesc); + dbError = db.lastError().text(); + } if (!connected){ - setLastError(db.lastError().text()); + if (!dbError.trimmed().isEmpty()) + setLastError(dbError); + QSqlDatabase::removeDatabase(connectionDesc->name()); return false; } } else { @@ -868,8 +893,9 @@ bool DataSourceManager::isConnection(const QString &connectionName) bool DataSourceManager::isConnectionConnected(const QString &connectionName) { - if (isConnection(connectionName)){ - return QSqlDatabase::database(connectionName).isOpen(); + if (isConnection(connectionName) && QSqlDatabase::contains(connectionName)){ + QSqlDatabase db = QSqlDatabase::database(connectionName); + return db.isValid() && QSqlDatabase::database(connectionName).isOpen(); } return false; } @@ -893,8 +919,10 @@ void DataSourceManager::disconnectConnection(const QString& connectionName) ConnectionDesc* connectionDesc = connectionByName(connectionName); if (connectionDesc->isInternal()){ - QSqlDatabase db = QSqlDatabase::database(connectionName); - if (db.isOpen()) db.close(); + { + QSqlDatabase db = QSqlDatabase::database(connectionName); + if (db.isOpen()) db.close(); + } if (QSqlDatabase::contains(connectionName)) QSqlDatabase::removeDatabase(connectionName); } diff --git a/limereport/lrdatasourcemanager.h b/limereport/lrdatasourcemanager.h index c60bfa5..77967c5 100644 --- a/limereport/lrdatasourcemanager.h +++ b/limereport/lrdatasourcemanager.h @@ -115,6 +115,7 @@ public: bool addModel(const QString& name, QAbstractItemModel *model, bool owned); void removeModel(const QString& name); ICallbackDatasource* createCallbackDatasouce(const QString &name); + void registerDbCredentialsProvider(IDbCredentialsProvider *provider); void addCallbackDatasource(ICallbackDatasource *datasource, const QString &name); void setReportVariable(const QString& name, const QVariant& value); void deleteVariable(const QString& name); @@ -150,6 +151,7 @@ public: int proxyIndexByName(const QString& dataSourceName); int connectionIndexByName(const QString& connectionName); + QList &conections(); bool dataSourceIsValid(const QString& name); IDataSource* dataSource(const QString& name); IDataSourceHolder* dataSourceHolder(const QString& name); @@ -250,8 +252,7 @@ private: ReportSettings* m_reportSettings; QHash m_groupFunctionsExpressionsMap; QVector m_groupFunctionsExpressions; - - + IDbCredentialsProvider* m_dbCredentialsProvider; }; } diff --git a/limereport/lrdatasourcemanagerintf.h b/limereport/lrdatasourcemanagerintf.h index 85cf388..57bd0c1 100644 --- a/limereport/lrdatasourcemanagerintf.h +++ b/limereport/lrdatasourcemanagerintf.h @@ -37,18 +37,25 @@ class QString; class QAbstractItemModel; namespace LimeReport{ +class IDbCredentialsProvider{ +public: + virtual QString getUserName(const QString& connectionName) = 0; + virtual QString getPassword(const QString& connectionName) = 0; +}; + class IDataSourceManager{ public: - virtual void setReportVariable(const QString& name, const QVariant& value)=0; - virtual void setDefaultDatabasePath(const QString &defaultDatabasePath)=0; - virtual void deleteVariable(const QString& name)=0; - virtual bool containsVariable(const QString& variableName)=0; - virtual QVariant variable(const QString& variableName)=0; - virtual bool addModel(const QString& name, QAbstractItemModel *model, bool owned)=0; - virtual void removeModel(const QString& name)=0; - virtual bool containsDatasource(const QString& dataSourceName)=0; + virtual void setReportVariable(const QString& name, const QVariant& value) = 0; + virtual void setDefaultDatabasePath(const QString &defaultDatabasePath) = 0; + virtual void deleteVariable(const QString& name) = 0; + virtual bool containsVariable(const QString& variableName) = 0; + virtual QVariant variable(const QString& variableName) = 0; + virtual bool addModel(const QString& name, QAbstractItemModel *model, bool owned) = 0; + virtual void removeModel(const QString& name) = 0; + virtual bool containsDatasource(const QString& dataSourceName) = 0; virtual void clearUserVariables()=0; virtual ICallbackDatasource* createCallbackDatasouce(const QString& name) = 0; + virtual void registerDbCredentialsProvider(IDbCredentialsProvider* provider) = 0; //virtual void addCallbackDatasource(ICallbackDatasource* datasource, const QString& name) = 0; }; diff --git a/limereport/lrreportengine.cpp b/limereport/lrreportengine.cpp index 2ea54d9..fb7ee7c 100644 --- a/limereport/lrreportengine.cpp +++ b/limereport/lrreportengine.cpp @@ -59,7 +59,7 @@ ReportEnginePrivate::ReportEnginePrivate(QObject *parent) : m_printer(new QPrinter(QPrinter::HighResolution)), m_printerSelected(false), m_showProgressDialog(true), m_reportName(""), m_activePreview(0), m_previewWindowIcon(":/report/images/logo32"), m_previewWindowTitle(tr("Preview")), - m_reportRendering(false), m_resultIsEditable(true) + m_reportRendering(false), m_resultIsEditable(true), m_passPhrase("HjccbzHjlbyfCkjy") { m_datasources = new DataSourceManager(this); m_datasources->setReportSettings(&m_reportSettings); @@ -513,14 +513,29 @@ bool ReportEnginePrivate::loadFromFile(const QString &fileName) { if (!QFile::exists(fileName)) return false; - clearReport(); + clearReport(); ItemsReaderIntf::Ptr reader = FileXMLReader::create(fileName); + reader->setPassPhrase(m_passPhrase); if (reader->first()){ if (reader->readItem(this)){ m_fileName=fileName; QFileInfo fi(fileName); m_reportName = fi.fileName(); + + QString dbSettingFileName = fi.absolutePath()+"/"+fi.baseName()+".db"; + if (QFile::exists(dbSettingFileName)){ + QSettings dbcredentals(dbSettingFileName, QSettings::IniFormat); + foreach (ConnectionDesc* connection, dataManager()->conections()) { + if (!connection->keepDBCredentials()){ + dbcredentals.beginGroup(connection->name()); + connection->setUserName(dbcredentals.value("user").toString()); + connection->setPassword(dbcredentals.value("password").toString()); + dbcredentals.endGroup(); + } + } + } + dataManager()->connectAutoConnections(); return true; }; @@ -533,6 +548,7 @@ bool ReportEnginePrivate::loadFromByteArray(QByteArray* data, const QString &nam clearReport(); ItemsReaderIntf::Ptr reader = ByteArrayXMLReader::create(data); + reader->setPassPhrase(m_passPhrase); if (reader->first()){ if (reader->readItem(this)){ m_fileName = ""; @@ -548,6 +564,7 @@ bool ReportEnginePrivate::loadFromString(const QString &report, const QString &n clearReport(); ItemsReaderIntf::Ptr reader = StringXMLreader::create(report); + reader->setPassPhrase(m_passPhrase); if (reader->first()){ if (reader->readItem(this)){ m_fileName = ""; @@ -566,10 +583,35 @@ bool ReportEnginePrivate::saveToFile(const QString &fileName) if (fi.suffix().isEmpty()) fn+=".lrxml"; + QString dbSettingFileName = fi.absolutePath()+"/"+fi.baseName()+".db"; + QSettings dbcredentals(dbSettingFileName, QSettings::IniFormat); + + foreach (ConnectionDesc* connection, dataManager()->conections()) { + if (!connection->keepDBCredentials()){ + dbcredentals.beginGroup(connection->name()); + dbcredentals.setValue("user",connection->userName()); + dbcredentals.setValue("password",connection->password()); + dbcredentals.endGroup(); + connection->setPassword(""); + connection->setUserName(""); + } + } + QScopedPointer< ItemsWriterIntf > writer(new XMLWriter()); + writer->setPassPhrase(m_passPhrase); writer->putItem(this); - m_fileName=fn; + m_fileName=fn; bool saved = writer->saveToFile(fn); + + foreach (ConnectionDesc* connection, dataManager()->conections()) { + if (!connection->keepDBCredentials()){ + dbcredentals.beginGroup(connection->name()); + connection->setUserName(dbcredentals.value("user").toString()); + connection->setPassword(dbcredentals.value("password").toString()); + dbcredentals.endGroup(); + } + } + if (saved){ foreach(PageDesignIntf* page, m_pages){ page->setToSaved(); @@ -581,6 +623,7 @@ bool ReportEnginePrivate::saveToFile(const QString &fileName) QByteArray ReportEnginePrivate::saveToByteArray() { QScopedPointer< ItemsWriterIntf > writer(new XMLWriter()); + writer->setPassPhrase(m_passPhrase); writer->putItem(this); QByteArray result = writer->saveToByteArray(); if (!result.isEmpty()){ @@ -593,6 +636,7 @@ QByteArray ReportEnginePrivate::saveToByteArray() QString ReportEnginePrivate::saveToString(){ QScopedPointer< ItemsWriterIntf > writer(new XMLWriter()); + writer->setPassPhrase(m_passPhrase); writer->putItem(this); QString result = writer->saveToString(); if (!result.isEmpty()){ @@ -629,6 +673,11 @@ QString ReportEnginePrivate::renderToString() }else return QString(); } +void ReportEnginePrivate::setPassPhrase(const QString &passPhrase) +{ + m_passPhrase = passPhrase; +} + bool ReportEnginePrivate::resultIsEditable() const { return m_resultIsEditable; @@ -807,6 +856,12 @@ bool ReportEngine::isBusy() return d->isBusy(); } +void ReportEngine::setPassPharse(QString &passPharse) +{ + Q_D(ReportEngine); + d->setPassPhrase(passPharse); +} + void ReportEngine::setShowProgressDialog(bool value) { Q_D(ReportEngine); diff --git a/limereport/lrreportengine.h b/limereport/lrreportengine.h index 0d5b309..dbbb2a8 100644 --- a/limereport/lrreportengine.h +++ b/limereport/lrreportengine.h @@ -101,6 +101,7 @@ public: void setResultEditable(bool value); bool resultIsEditable(); bool isBusy(); + void setPassPharse(QString& passPharse); signals: void renderStarted(); void renderFinished(); diff --git a/limereport/lrreportengine_p.h b/limereport/lrreportengine_p.h index 85aff24..4ec3508 100644 --- a/limereport/lrreportengine_p.h +++ b/limereport/lrreportengine_p.h @@ -125,6 +125,8 @@ public: bool resultIsEditable() const; void setResultEditable(bool value); + void setPassPhrase(const QString &passPhrase); + signals: void pagesLoadFinished(); void datasourceCollectionLoadFinished(const QString& collectionName); @@ -175,6 +177,7 @@ private: ReportSettings m_reportSettings; bool m_reportRendering; bool m_resultIsEditable; + QString m_passPhrase; }; } diff --git a/limereport/lrsimplecrypt.cpp b/limereport/lrsimplecrypt.cpp index 0b9aa06..d79273e 100644 --- a/limereport/lrsimplecrypt.cpp +++ b/limereport/lrsimplecrypt.cpp @@ -41,6 +41,8 @@ const int b = 16; /* number of bytes in key */ const int c = 4; /* number words in key = ceil(8*b/w)*/ const int t = 26; /* size of table S = 2*(r+1) words */ +const char* passPhrase = "HjccbzHjlbyfCkjy"; + WORD P = 0xb7e15163, Q = 0x9e3779b9; #define ROTL(x,y) (((x)<<(y&(w-1))) | ((x)>>(w-(y&(w-1))))) @@ -64,11 +66,15 @@ namespace LimeReport { class ChipperPrivate{ friend class Chipper; +public: + ChipperPrivate():m_prepared(false){} + bool isPrepared(){ return m_prepared;} private: void RC5_SETUP(const char *K); void RC5_ENCRYPT(WORD *pt, WORD *ct); void RC5_DECRYPT(WORD *ct, WORD *pt); WORD S[26]; + bool m_prepared; }; void ChipperPrivate::RC5_SETUP(const char *K) @@ -82,6 +88,7 @@ void ChipperPrivate::RC5_SETUP(const char *K) A = S[i] = ROTL(S[i]+(A+B),3); B = L[j] = ROTL(L[j]+(A+B),(A+B)); } + m_prepared = true; } void ChipperPrivate::RC5_ENCRYPT(WORD *pt, WORD *ct) @@ -111,7 +118,8 @@ QByteArray Chipper::cryptString(QString value) buff += value; WTB pt, ct, prior; - d->RC5_SETUP("HjccbzHjlbyfCkjy"); + if (!d->isPrepared()) + d->RC5_SETUP(passPhrase); prior.word[0]=0; prior.word[1]=0; @@ -144,7 +152,8 @@ QString Chipper::decryptByteArray(QByteArray value) QByteArray result; WTB pt, ct, prior; - d->RC5_SETUP("HjccbzHjlbyfCkjy"); + if (!d->isPrepared()) + d->RC5_SETUP(passPhrase); prior.word[0] = 0; prior.word[1] = 0; @@ -174,6 +183,14 @@ QString Chipper::decryptByteArray(QByteArray value) Chipper::Chipper(): d(new ChipperPrivate()){} +Chipper::Chipper(QString passphrase): d(new ChipperPrivate()) +{ + if (!passphrase.trimmed().isEmpty()){ + passphrase = passphrase.leftJustified(16,'#'); + d->RC5_SETUP(passphrase.toLatin1().data()); + } +} + Chipper::~Chipper() { delete d; diff --git a/limereport/lrsimplecrypt.h b/limereport/lrsimplecrypt.h index 68978b5..4088726 100644 --- a/limereport/lrsimplecrypt.h +++ b/limereport/lrsimplecrypt.h @@ -42,6 +42,7 @@ public: QByteArray cryptString(QString value); QString decryptByteArray(QByteArray value); Chipper(); + Chipper(QString passphrase); ~Chipper(); private: ChipperPrivate* d; diff --git a/limereport/serializators/lrserializatorintf.h b/limereport/serializators/lrserializatorintf.h index ce6a44b..b16263a 100644 --- a/limereport/serializators/lrserializatorintf.h +++ b/limereport/serializators/lrserializatorintf.h @@ -35,10 +35,16 @@ namespace LimeReport { class SerializatorIntf { public: - virtual QVariant loadValue()=0; - virtual void save(const QVariant& value,QString name)=0; + virtual QVariant loadValue() = 0; + virtual void save(const QVariant& value,QString name) = 0; virtual ~SerializatorIntf(){} }; +class CryptedSerializator : public virtual SerializatorIntf +{ +public: + virtual void setPassPhrase(const QString& passPhrase) = 0; +}; + } #endif // LRSERIALIZATORINTF_H diff --git a/limereport/serializators/lrstorageintf.h b/limereport/serializators/lrstorageintf.h index 85fb7ad..74b83ef 100644 --- a/limereport/serializators/lrstorageintf.h +++ b/limereport/serializators/lrstorageintf.h @@ -39,18 +39,19 @@ namespace LimeReport{ class ObjectLoadingStateIntf{ public: - virtual bool isLoading()=0; - virtual void objectLoadStarted()=0; - virtual void objectLoadFinished()=0; + virtual bool isLoading() = 0; + virtual void objectLoadStarted() = 0; + virtual void objectLoadFinished() = 0; }; class ItemsWriterIntf { public: - virtual void putItem(QObject* item)=0; + virtual void putItem(QObject* item) = 0; virtual bool saveToFile(QString fileName) = 0; virtual QString saveToString() = 0; virtual QByteArray saveToByteArray() = 0; + virtual void setPassPhrase(const QString& passPhrase) = 0; virtual ~ItemsWriterIntf(){} }; @@ -58,14 +59,15 @@ class ItemsReaderIntf { public: typedef QSharedPointer Ptr; - virtual bool first()=0; - virtual bool next()=0; - virtual bool prior()=0; - virtual QString itemType()=0; - virtual QString itemClassName()=0; - virtual bool readItem(QObject *item)=0; - virtual int firstLevelItemsCount()=0; - virtual QString lastError()=0; + virtual bool first() = 0; + virtual bool next() = 0; + virtual bool prior() = 0; + virtual QString itemType() = 0; + virtual QString itemClassName() = 0; + virtual bool readItem(QObject *item) = 0; + virtual int firstLevelItemsCount() = 0; + virtual QString lastError() = 0; + virtual void setPassPhrase(const QString& passPhrase) = 0; virtual ~ItemsReaderIntf(){} }; diff --git a/limereport/serializators/lrxmlbasetypesserializators.cpp b/limereport/serializators/lrxmlbasetypesserializators.cpp index ebc4260..c0c2ce7 100644 --- a/limereport/serializators/lrxmlbasetypesserializators.cpp +++ b/limereport/serializators/lrxmlbasetypesserializators.cpp @@ -109,7 +109,7 @@ void XmlQStringSerializator::save(const QVariant &value, QString name) QDomElement _node = doc()->createElement(name); _node.setAttribute("Type","QString"); if (name.compare("password")==0){ - Chipper chipper; + Chipper chipper(passPhrase()); QByteArray ba = chipper.cryptString(value.toString()); //ba.append(); _node.setAttribute("Value",QString(ba.toBase64())); @@ -123,7 +123,7 @@ QVariant XmlQStringSerializator::loadValue() { if (node()->tagName().compare("password")==0){ QByteArray ba; - Chipper chipper; + Chipper chipper(passPhrase()); ba.append(node()->attribute("Value").toLatin1()); return chipper.decryptByteArray(QByteArray::fromBase64(ba)); } else diff --git a/limereport/serializators/lrxmlbasetypesserializators.h b/limereport/serializators/lrxmlbasetypesserializators.h index 70bd40d..56f62c1 100644 --- a/limereport/serializators/lrxmlbasetypesserializators.h +++ b/limereport/serializators/lrxmlbasetypesserializators.h @@ -36,7 +36,7 @@ namespace LimeReport{ -class XmlBaseSerializator : public SerializatorIntf{ +class XmlBaseSerializator : public virtual SerializatorIntf{ public: XmlBaseSerializator(QDomDocument *doc, QDomElement *node):m_doc(doc),m_node(node){} protected: @@ -48,10 +48,21 @@ private: QDomElement *m_node; }; -class XmlQStringSerializator : public XmlBaseSerializator +class XmlCryptedSerializator : public XmlBaseSerializator, public virtual CryptedSerializator{ +public: + XmlCryptedSerializator(QDomDocument *doc, QDomElement *node) + :XmlBaseSerializator(doc, node){} + virtual void setPassPhrase(const QString& passPhrase) {m_passPhrase = passPhrase;} +protected: + QString passPhrase(){ return m_passPhrase;} +private: + QString m_passPhrase; +}; + +class XmlQStringSerializator : public XmlCryptedSerializator { public: - XmlQStringSerializator(QDomDocument *doc, QDomElement *node):XmlBaseSerializator(doc,node){} + XmlQStringSerializator(QDomDocument *doc, QDomElement *node):XmlCryptedSerializator(doc,node){} private: virtual void save(const QVariant &value,QString name); virtual QVariant loadValue(); diff --git a/limereport/serializators/lrxmlreader.cpp b/limereport/serializators/lrxmlreader.cpp index 545c4ca..bee2ff8 100644 --- a/limereport/serializators/lrxmlreader.cpp +++ b/limereport/serializators/lrxmlreader.cpp @@ -135,6 +135,11 @@ QString XMLReader::lastError() return m_error; } +void XMLReader::setPassPhrase(const QString &passPhrase) +{ + m_passPhrase = passPhrase; +} + bool XMLReader::extractFirstNode() { if (m_firstNode.isNull()){ @@ -166,6 +171,10 @@ QVariant XMLReader::getValue(QDomElement *node) if (creator) { QScopedPointerserializator(creator(m_doc.data(),node)); + CryptedSerializator* cs = dynamic_cast(serializator.data()); + if (cs){ + cs->setPassPhrase(m_passPhrase); + } return serializator->loadValue(); } return QVariant(); diff --git a/limereport/serializators/lrxmlreader.h b/limereport/serializators/lrxmlreader.h index f677c68..0bc17ee 100644 --- a/limereport/serializators/lrxmlreader.h +++ b/limereport/serializators/lrxmlreader.h @@ -44,14 +44,17 @@ public: XMLReader(); XMLReader(QSharedPointer doc); protected: +//ItemsReaderIntf interface + bool first(); + bool next(); + bool prior(); + QString itemType(); + QString itemClassName(); + bool readItem(QObject *item); + int firstLevelItemsCount(); + QString lastError(); + void setPassPhrase(const QString &passPhrase); - virtual bool first(); - virtual bool next(); - virtual bool prior(); - virtual QString itemType(); - virtual QString itemClassName(); - virtual bool readItem(QObject *item); - virtual int firstLevelItemsCount(); virtual bool prepareReader(QDomDocument *doc); void readItemFromNode(QObject *item, QDomElement *node); @@ -60,7 +63,7 @@ protected: void readCollection(QObject *item, QDomElement *node); QVariant getValue(QDomElement *node); - virtual QString lastError(); + protected: bool extractFirstNode(); QString m_error; @@ -68,6 +71,11 @@ private: QSharedPointer m_doc; QDomElement m_curNode; QDomElement m_firstNode; + QString m_passPhrase; + + + + }; class FileXMLReader : public XMLReader{ diff --git a/limereport/serializators/lrxmlwriter.cpp b/limereport/serializators/lrxmlwriter.cpp index 1a6e68a..3038373 100644 --- a/limereport/serializators/lrxmlwriter.cpp +++ b/limereport/serializators/lrxmlwriter.cpp @@ -113,6 +113,11 @@ QByteArray XMLWriter::saveToByteArray() return res; } +void XMLWriter::setPassPhrase(const QString &passPhrase) +{ + m_passPhrase = passPhrase; +} + QDomElement XMLWriter::putQObjectItem(QString name, QObject *item) { Q_UNUSED(name) @@ -158,6 +163,10 @@ void XMLWriter::saveProperty(QString name, QObject* item, QDomElement *node) if (creator) { QScopedPointer serializator(creator(m_doc.data(),node)); + CryptedSerializator* cs = dynamic_cast(serializator.data()); + if (cs){ + cs->setPassPhrase(m_passPhrase); + } serializator->save( item->property(name.toLatin1()), name diff --git a/limereport/serializators/lrxmlwriter.h b/limereport/serializators/lrxmlwriter.h index 399e2e2..0259a75 100644 --- a/limereport/serializators/lrxmlwriter.h +++ b/limereport/serializators/lrxmlwriter.h @@ -44,10 +44,13 @@ public: XMLWriter(QSharedPointer doc); ~XMLWriter() {} private: + // ItemsWriterIntf interface void putItem(QObject* item); bool saveToFile(QString fileName); QString saveToString(); QByteArray saveToByteArray(); + void setPassPhrase(const QString &passPhrase); + void init(); QDomElement putQObjectItem(QString name, QObject* item); void putChildQObjectItem(QString name, QObject* item, QDomElement* parentNode); @@ -66,6 +69,7 @@ private: QSharedPointer m_doc; QString m_fileName; QDomElement m_rootElement; + QString m_passPhrase; }; }