File indexing completed on 2024-05-12 04:33:33
0001 /* 0002 SPDX-FileCopyrightText: 2008 Pino Toscano <pino@kde.org> 0003 SPDX-FileCopyrightText: 2008 Harri Porten <porten@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "js_util_p.h" 0009 0010 #include <QDateTime> 0011 #include <QDebug> 0012 #include <QJSEngine> 0013 #include <QLocale> 0014 #include <QRegularExpression> 0015 #include <QUrl> 0016 0017 #include <cmath> 0018 0019 using namespace Okular; 0020 0021 QJSValue JSUtil::crackURL(const QString &cURL) const 0022 { 0023 QUrl url(QUrl::fromLocalFile(cURL)); 0024 if (!url.isValid()) { 0025 return qjsEngine(this)->newErrorObject(QJSValue::URIError, QStringLiteral("Invalid URL")); 0026 } 0027 if (url.scheme() != QLatin1String("file") || url.scheme() != QLatin1String("http") || url.scheme() != QLatin1String("https")) { 0028 return qjsEngine(this)->newErrorObject(QJSValue::URIError, QStringLiteral("Protocol not valid: '") + url.scheme() + QLatin1Char('\'')); 0029 } 0030 0031 QJSValue obj; 0032 obj.setProperty(QStringLiteral("cScheme"), url.scheme()); 0033 if (!url.userName().isEmpty()) { 0034 obj.setProperty(QStringLiteral("cUser"), url.userName()); 0035 } 0036 if (!url.password().isEmpty()) { 0037 obj.setProperty(QStringLiteral("cPassword"), url.password()); 0038 } 0039 obj.setProperty(QStringLiteral("cHost"), url.host()); 0040 obj.setProperty(QStringLiteral("nPort"), url.port(80)); 0041 // TODO cPath (Optional) The path portion of the URL. 0042 // TODO cParameters (Optional) The parameter string portion of the URL. 0043 if (url.hasFragment()) { 0044 obj.setProperty(QStringLiteral("cFragments"), url.fragment(QUrl::FullyDecoded)); 0045 } 0046 0047 return obj; 0048 } 0049 0050 QJSValue JSUtil::printd(const QJSValue &oFormat, const QDateTime &oDate) const 0051 { 0052 QString format; 0053 QLocale defaultLocale; 0054 0055 if (oFormat.isNumber()) { 0056 int formatType = oFormat.toInt(); 0057 switch (formatType) { 0058 case 0: 0059 format = QStringLiteral("D:yyyyMMddHHmmss"); 0060 break; 0061 case 1: 0062 format = QStringLiteral("yyyy.MM.dd HH:mm:ss"); 0063 break; 0064 case 2: 0065 format = defaultLocale.dateTimeFormat(QLocale::ShortFormat); 0066 if (!format.contains(QStringLiteral("ss"))) { 0067 format.insert(format.indexOf(QStringLiteral("mm")) + 2, QStringLiteral(":ss")); 0068 } 0069 break; 0070 } 0071 } else { 0072 format = oFormat.toString().replace(QLatin1String("tt"), QLatin1String("ap")); 0073 format.replace(QLatin1Char('t'), QLatin1Char('a')); 0074 for (QChar &formatChar : format) { 0075 if (formatChar == QLatin1Char('M')) { 0076 formatChar = QLatin1Char('m'); 0077 } else if (formatChar == QLatin1Char('m')) { 0078 formatChar = QLatin1Char('M'); 0079 } 0080 } 0081 } 0082 0083 return defaultLocale.toString(oDate, format); 0084 } 0085 0086 /** Converts a Number to a String using l10n 0087 * 0088 * String numberToString( Number number, String format = 'g', int precision = 6, 0089 * String LocaleName = system ) 0090 */ 0091 QString JSUtil::numberToString(double number, const QString &fmt, int precision, const QString &localeName) const 0092 { 0093 if (std::isnan(number)) { 0094 return QStringLiteral("NaN"); 0095 } 0096 0097 QChar format = QLatin1Char('g'); 0098 if (!fmt.isEmpty()) { 0099 format = fmt[0]; 0100 } 0101 0102 QLocale locale; 0103 if (!localeName.isEmpty()) { 0104 locale = QLocale(localeName); 0105 } 0106 0107 return locale.toString(number, format.toLatin1(), precision); 0108 } 0109 0110 /** Converts a String to a Number trying with the current locale first and 0111 * if that fails trying with the reverse locale for the decimal separator 0112 * 0113 * Number stringToNumber( String number ) */ 0114 double JSUtil::stringToNumber(const QString &number) const 0115 { 0116 if (number.isEmpty()) { 0117 return 0; 0118 } 0119 0120 const QLocale locale; 0121 bool ok; 0122 double converted = locale.toDouble(number, &ok); 0123 0124 if (!ok) { 0125 const QLocale locale2(locale.decimalPoint() == QLatin1Char('.') ? QStringLiteral("de") : QStringLiteral("en")); 0126 converted = locale2.toDouble(number, &ok); 0127 if (!ok) { 0128 return NAN; 0129 } 0130 } 0131 0132 return converted; 0133 }