KTextEditor

katescripthelpers.cpp
1 /*
2  SPDX-FileCopyrightText: 2010-2018 Dominik Haumann <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 // Undefine this because we don't want our i18n*() method names to be turned
8 // into i18nd*(). This means any translated string in this file should use
9 // i18nd("ktexteditor5", "foo") instead of i18n("foo")
10 #undef TRANSLATION_DOMAIN
11 
12 #include "katescripthelpers.h"
13 #include "katedocument.h"
14 #include "katepartdebug.h"
15 #include "katescript.h"
16 #include "katescriptdocument.h"
17 #include "katescriptview.h"
18 #include "kateview.h"
19 
20 #include <iostream>
21 
22 #include <QFile>
23 #include <QJSEngine>
24 #include <QStandardPaths>
25 
26 #include <QDebug>
27 
28 #include <KLocalizedString>
29 
30 namespace Kate
31 {
32 namespace Script
33 {
34 bool readFile(const QString &sourceUrl, QString &sourceCode)
35 {
36  sourceCode = QString();
37 
38  QFile file(sourceUrl);
39  if (!file.open(QIODevice::ReadOnly)) {
40  qCDebug(LOG_KTE) << QStringLiteral("Unable to find '%1'").arg(sourceUrl);
41  return false;
42  } else {
43  QTextStream stream(&file);
44 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
45  stream.setCodec("UTF-8");
46 #endif
47  sourceCode = stream.readAll();
48  file.close();
49  }
50  return true;
51 }
52 
53 } // namespace Script
54 
55 QString ScriptHelper::read(const QString &name)
56 {
57  // get full name of file
58  // skip on errors
59  QString content;
60  QString fullName = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("katepart5/script/files/") + name);
61  if (fullName.isEmpty()) {
62  // retry with resource
63  fullName = QLatin1String(":/ktexteditor/script/files/") + name;
64  if (!QFile::exists(fullName)) {
65  return content;
66  }
67  }
68 
69  // try to read complete file
70  // skip non-existing files
71  Script::readFile(fullName, content);
72  return content;
73 }
74 
75 void ScriptHelper::require(const QString &name)
76 {
77  // get full name of file
78  // skip on errors
80  if (fullName.isEmpty()) {
81  // retry with resource
82  fullName = QLatin1String(":/ktexteditor/script/libraries/") + name;
83  if (!QFile::exists(fullName)) {
84  return;
85  }
86  }
87 
88  // check include guard
89  QJSValue require_guard = m_engine->globalObject().property(QStringLiteral("require_guard"));
90  if (require_guard.property(fullName).toBool()) {
91  return;
92  }
93 
94  // try to read complete file
95  // skip non-existing files
96  QString code;
97  if (!Script::readFile(fullName, code)) {
98  return;
99  }
100 
101  // eval in current script engine
102  const QJSValue val = m_engine->evaluate(code, fullName);
103  if (val.isError()) {
104  qCWarning(LOG_KTE) << "error evaluating" << fullName << val.toString() << ", at line" << val.property(QStringLiteral("lineNumber")).toInt();
105  }
106 
107  // set include guard
108  require_guard.setProperty(fullName, QJSValue(true));
109 }
110 
111 void ScriptHelper::debug(const QString &message)
112 {
113  // debug in blue to distance from other debug output if necessary
114  std::cerr << "\033[31m" << qPrintable(message) << "\033[0m\n";
115 }
116 
117 // BEGIN code adapted from kdelibs/kross/modules/translation.cpp
118 /// helper function to do the substitution from QtScript > QVariant > real values
119 // KLocalizedString substituteArguments(const KLocalizedString &kls, const QVariantList &arguments, int max = 99)
120 //{
121 // KLocalizedString ls = kls;
122 // int cnt = qMin(arguments.count(), max); // QString supports max 99
123 // for (int i = 0; i < cnt; ++i) {
124 // QVariant arg = arguments[i];
125 // switch (arg.type()) {
126 // case QVariant::Int: ls = ls.subs(arg.toInt()); break;
127 // case QVariant::UInt: ls = ls.subs(arg.toUInt()); break;
128 // case QVariant::LongLong: ls = ls.subs(arg.toLongLong()); break;
129 // case QVariant::ULongLong: ls = ls.subs(arg.toULongLong()); break;
130 // case QVariant::Double: ls = ls.subs(arg.toDouble()); break;
131 // default: ls = ls.subs(arg.toString()); break;
132 // }
133 // }
134 // return ls;
135 //}
136 
137 /// i18n("text", arguments [optional])
138 QString ScriptHelper::_i18n(const QString &text)
139 {
140  KLocalizedString ls = ki18n(text.toUtf8().constData());
141  return ls.toString(); // substituteArguments(ls, args).toString();
142 }
143 
144 /// i18nc("context", "text", arguments [optional])
145 QString ScriptHelper::_i18nc(const QString &textContext, const QString &text)
146 {
147  KLocalizedString ls = ki18nc(textContext.toUtf8().constData(), text.toUtf8().constData());
148  return ls.toString(); // substituteArguments(ls, args).toString();
149 }
150 
151 /// i18np("singular", "plural", number, arguments [optional])
152 QString ScriptHelper::_i18np(const QString &trSingular, const QString &trPlural, int number)
153 {
154  KLocalizedString ls = ki18np(trSingular.toUtf8().constData(), trPlural.toUtf8().constData()).subs(number);
155  return ls.toString(); // substituteArguments(ls, args, 98).toString();
156 }
157 
158 /// i18ncp("context", "singular", "plural", number, arguments [optional])
159 QString ScriptHelper::_i18ncp(const QString &trContext, const QString &trSingular, const QString &trPlural, int number)
160 {
161  KLocalizedString ls = ki18ncp(trContext.toUtf8().data(), trSingular.toUtf8().data(), trPlural.toUtf8().data()).subs(number);
162  return ls.toString(); // substituteArguments(ls, args, 98).toString();
163 }
164 
165 // END code adapted from kdelibs/kross/modules/translation.cpp
166 
167 } // namespace kate
QString readAll()
KLocalizedString KI18N_EXPORT ki18nc(const char *context, const char *text)
virtual bool open(QIODevice::OpenMode mode) override
QString toString() const
QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
bool exists() const const
bool readFile(const QString &sourceUrl, QString &sourceCode)
read complete file contents, helper
bool toBool() const const
KLocalizedString KI18N_EXPORT ki18ncp(const char *context, const char *singular, const char *plural)
bool isError() const const
void setProperty(const QString &name, const QJSValue &value)
bool isEmpty() const const
QByteArray toUtf8() const const
KLocalizedString KI18N_EXPORT ki18n(const char *text)
KLocalizedString subs(const KLocalizedString &a, int fieldWidth=0, QChar fillChar=QLatin1Char(' ')) const
virtual void close() override
const char * constData() const const
QString name(StandardShortcut id)
KLocalizedString KI18N_EXPORT ki18np(const char *singular, const char *plural)
QJSValue property(const QString &name) const const
void setCodec(QTextCodec *codec)
QString fullName(const PartType &type)
QString message
QString toString() const const
char * data()
qint32 toInt() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 03:50:22 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.