File indexing completed on 2024-12-22 04:48:19

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2022 Louis Schul <schul9louis@gmail.com>
0003 
0004 #include "kleverUtility.h"
0005 #include "documentHandler.h"
0006 // #include <QDebug>
0007 #include <QDir>
0008 #include <QFontInfo>
0009 #include <QStandardPaths>
0010 #include <kio/global.h>
0011 #include <klocalizedstring.h>
0012 
0013 KleverUtility::KleverUtility(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 QString KleverUtility::getName(const QString &path) const
0019 {
0020     return QDir(path).dirName();
0021 }
0022 
0023 QString KleverUtility::getPath(const QUrl &url) const
0024 {
0025     return url.toLocalFile();
0026 }
0027 
0028 bool KleverUtility::exists(const QString &path)
0029 {
0030     return QFile().exists(path);
0031 }
0032 
0033 bool KleverUtility::create(const QString &path)
0034 {
0035     if (!exists(path)) {
0036         return QDir().mkpath(path);
0037     }
0038     return false;
0039 }
0040 
0041 QString KleverUtility::getImageStoragingPath(const QString &noteImagesStoringPath, const QString &wantedName, int iteration) const
0042 {
0043     create(noteImagesStoringPath);
0044 
0045     QString imagePath = noteImagesStoringPath + wantedName;
0046     if (iteration != 0)
0047         imagePath += QStringLiteral("(") + QString::number(iteration) + QStringLiteral(")");
0048     imagePath += QStringLiteral(".png");
0049 
0050     if (exists(imagePath)) {
0051         return getImageStoragingPath(noteImagesStoringPath, wantedName, iteration + 1);
0052     }
0053     return imagePath;
0054 }
0055 
0056 bool KleverUtility::isEmptyDir(const QString &path) const
0057 {
0058     return !exists(path) || QDir(path).isEmpty();
0059 }
0060 
0061 QString KleverUtility::isProperPath(const QString &parentPath, const QString &name) const
0062 {
0063     if (name.startsWith(QStringLiteral(".")))
0064         return QStringLiteral("dot");
0065 
0066     const QString properName = KIO::encodeFileName(name);
0067 
0068     const QString newPath = parentPath + QStringLiteral("/") + properName;
0069 
0070     return (exists(newPath)) ? QStringLiteral("exist") : QString();
0071 }
0072 
0073 QString KleverUtility::getParentPath(const QString &path) const
0074 {
0075     QDir dir(path);
0076     dir.cdUp();
0077     return dir.absolutePath();
0078 }
0079 
0080 bool KleverUtility::remove(const QString &path) const
0081 {
0082     QFile file(path);
0083     return file.remove();
0084 }
0085 
0086 QJsonObject KleverUtility::getCssStylesList() const
0087 {
0088     QJsonObject styleNameAndPath = {{QStringLiteral("KleverStyle"), QStringLiteral(":/KleverStyle.css")},
0089                                     {QStringLiteral("Avenir"), QStringLiteral(":/Avenir.css")},
0090                                     {QStringLiteral("Style7"), QStringLiteral(":/Style7.css")},
0091                                     {QStringLiteral("Style9"), QStringLiteral(":/Style9.css")}};
0092 
0093     static const QString externalStylesFolderPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + QStringLiteral("/Styles/");
0094     static const QDir externalStylesFolder(externalStylesFolderPath);
0095 
0096     if (!externalStylesFolder.exists()) {
0097         create(externalStylesFolderPath);
0098         static const QString message = i18n(
0099             "/*\
0100             \nThis file is a copy of the default CSS style for the note display.\
0101             \nFeel free to edit it or make your own.\
0102             \nNote: each style need to have a different name.\
0103             \n*/\n");
0104 
0105         static const QString defaultCss = DocumentHandler::getCssStyle(QStringLiteral(":/KleverStyle.css"));
0106         static const QString fileContent = message + defaultCss;
0107         static const QString filePath = externalStylesFolderPath + QStringLiteral("KleverStyle.css");
0108 
0109         DocumentHandler::writeFile(fileContent, filePath);
0110     }
0111 
0112     const QFileInfoList fileList = externalStylesFolder.entryInfoList(QDir::Filter::NoDotAndDotDot | QDir::Filter::Files);
0113 
0114     for (const QFileInfo &file : fileList) {
0115         QString name = file.fileName();
0116 
0117         if (!name.endsWith(QStringLiteral(".css")))
0118             continue;
0119 
0120         name.chop(4);
0121 
0122         if (styleNameAndPath.contains(name))
0123             continue;
0124 
0125         styleNameAndPath[name] = file.absoluteFilePath();
0126     }
0127 
0128     return styleNameAndPath;
0129 }
0130 
0131 QJsonObject KleverUtility::fontInfo(const QFont &font) const
0132 {
0133     QJsonObject fontInfo;
0134 
0135     const QFontInfo info(font);
0136 
0137     fontInfo[QStringLiteral("family")] = info.family();
0138     fontInfo[QStringLiteral("pointSize")] = info.pointSize();
0139     return fontInfo;
0140 }