Okular

js_util.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Pino Toscano <pino@kde.org>
3 SPDX-FileCopyrightText: 2008 Harri Porten <porten@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#include "js_util_p.h"
9
10#include <QDateTime>
11#include <QDebug>
12#include <QJSEngine>
13#include <QLocale>
14#include <QRegularExpression>
15#include <QUrl>
16
17#include <cmath>
18
19using namespace Okular;
20
21QJSValue JSUtil::crackURL(const QString &cURL) const
22{
23 QUrl url(QUrl::fromLocalFile(cURL));
24 if (!url.isValid()) {
25 return qjsEngine(this)->newErrorObject(QJSValue::URIError, QStringLiteral("Invalid URL"));
26 }
27 if (url.scheme() != QLatin1String("file") || url.scheme() != QLatin1String("http") || url.scheme() != QLatin1String("https")) {
28 return qjsEngine(this)->newErrorObject(QJSValue::URIError, QStringLiteral("Protocol not valid: '") + url.scheme() + QLatin1Char('\''));
29 }
30
31 QJSValue obj;
32 obj.setProperty(QStringLiteral("cScheme"), url.scheme());
33 if (!url.userName().isEmpty()) {
34 obj.setProperty(QStringLiteral("cUser"), url.userName());
35 }
36 if (!url.password().isEmpty()) {
37 obj.setProperty(QStringLiteral("cPassword"), url.password());
38 }
39 obj.setProperty(QStringLiteral("cHost"), url.host());
40 obj.setProperty(QStringLiteral("nPort"), url.port(80));
41 // TODO cPath (Optional) The path portion of the URL.
42 // TODO cParameters (Optional) The parameter string portion of the URL.
43 if (url.hasFragment()) {
44 obj.setProperty(QStringLiteral("cFragments"), url.fragment(QUrl::FullyDecoded));
45 }
46
47 return obj;
48}
49
50QJSValue JSUtil::printd(const QJSValue &oFormat, const QDateTime &oDate) const
51{
52 QString format;
53 QLocale defaultLocale;
54
55 if (oFormat.isNumber()) {
56 int formatType = oFormat.toInt();
57 switch (formatType) {
58 case 0:
59 format = QStringLiteral("D:yyyyMMddHHmmss");
60 break;
61 case 1:
62 format = QStringLiteral("yyyy.MM.dd HH:mm:ss");
63 break;
64 case 2:
65 format = defaultLocale.dateTimeFormat(QLocale::ShortFormat);
66 if (!format.contains(QStringLiteral("ss"))) {
67 format.insert(format.indexOf(QStringLiteral("mm")) + 2, QStringLiteral(":ss"));
68 }
69 break;
70 }
71 } else {
72 format = oFormat.toString().replace(QLatin1String("tt"), QLatin1String("ap"));
73 format.replace(QLatin1Char('t'), QLatin1Char('a'));
74 for (QChar &formatChar : format) {
75 if (formatChar == QLatin1Char('M')) {
76 formatChar = QLatin1Char('m');
77 } else if (formatChar == QLatin1Char('m')) {
78 formatChar = QLatin1Char('M');
79 }
80 }
81 }
82
83 return defaultLocale.toString(oDate, format);
84}
85
86/** Converts a Number to a String using l10n
87 *
88 * String numberToString( Number number, String format = 'g', int precision = 6,
89 * String LocaleName = system )
90 */
91QString JSUtil::numberToString(double number, const QString &fmt, int precision, const QString &localeName) const
92{
93 if (std::isnan(number)) {
94 return QStringLiteral("NaN");
95 }
96
97 QChar format = QLatin1Char('g');
98 if (!fmt.isEmpty()) {
99 format = fmt[0];
100 }
101
102 QLocale locale;
103 if (!localeName.isEmpty()) {
104 locale = QLocale(localeName);
105 }
106
107 return locale.toString(number, format.toLatin1(), precision);
108}
109
110/** Converts a String to a Number trying with the current locale first and
111 * if that fails trying with the reverse locale for the decimal separator
112 *
113 * Number stringToNumber( String number ) */
114double JSUtil::stringToNumber(const QString &number) const
115{
116 if (number.isEmpty()) {
117 return 0;
118 }
119
120 const QLocale locale;
121 bool ok;
122 double converted = locale.toDouble(number, &ok);
123
124 if (!ok) {
125 const QLocale locale2(locale.decimalPoint() == QLatin1Char('.') ? QStringLiteral("de") : QStringLiteral("en"));
126 converted = locale2.toDouble(number, &ok);
127 if (!ok) {
128 return NAN;
129 }
130 }
131
132 return converted;
133}
134
135/*
136 * Get locale dependent month names
137 *
138 * A method not present in the Adobe Javascript APIs reference. We add it here for our convenience. Not to be used externally.
139 */
140QStringList JSUtil::getMonths() const
141{
142 QLocale defaultLocale;
143 QStringList monthNames;
144 // add both long and short format dates for processing in parseDate method.
145 for (int i = 1; i <= 12; i++) {
146 monthNames << defaultLocale.monthName(i, QLocale::LongFormat).toUpper();
147 monthNames << defaultLocale.monthName(i, QLocale::ShortFormat).toUpper();
148 }
149 return monthNames;
150}
KIOCORE_EXPORT QString number(KIO::filesize_t size)
global.h
Definition action.h:17
char toLatin1() const const
bool isNumber() const const
void setProperty(const QString &name, const QJSValue &value)
qint32 toInt() const const
QString toString() const const
QString dateTimeFormat(FormatType format) const const
QString decimalPoint() const const
QString monthName(int month, FormatType type) const const
double toDouble(QStringView s, bool *ok) const const
QString toString(QDate date, FormatType format) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
QString & insert(qsizetype position, QChar ch)
bool isEmpty() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QString toUpper() const const
FullyDecoded
QUrl fromLocalFile(const QString &localFile)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 11:49:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.