File indexing completed on 2025-01-19 03:55:42
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2014-09-12 0007 * Description : Web Service tool utils methods 0008 * 0009 * SPDX-FileCopyrightText: 2014-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> 0010 * 0011 * SPDX-License-Identifier: GPL-2.0-or-later 0012 * 0013 * ============================================================ */ 0014 0015 #include "wstoolutils.h" 0016 0017 // Qt includes 0018 0019 #include <QRandomGenerator> 0020 #include <QStandardPaths> 0021 #include <QApplication> 0022 #include <QByteArray> 0023 #include <QBuffer> 0024 #include <QTime> 0025 0026 namespace Digikam 0027 { 0028 0029 QDir WSToolUtils::makeTemporaryDir(const char* prefix) 0030 { 0031 QString subDir = QString::fromLatin1("digikam-%1-%2").arg(QString::fromUtf8(prefix)).arg(qApp->applicationPid()); 0032 QString path = QDir(QDir::tempPath()).filePath(subDir); 0033 0034 if (!QDir().exists(path)) 0035 { 0036 QDir().mkpath(path); 0037 } 0038 0039 return QDir(path); 0040 } 0041 0042 // ------------------------------------------------------------------------------------ 0043 0044 void WSToolUtils::removeTemporaryDir(const char* prefix) 0045 { 0046 QString subDir = QString::fromLatin1("digikam-%1-%2").arg(QString::fromUtf8(prefix)).arg(qApp->applicationPid()); 0047 QString path = QDir(QDir::tempPath()).filePath(subDir); 0048 0049 if (QDir().exists(path)) 0050 { 0051 QDir(path).removeRecursively(); 0052 } 0053 } 0054 0055 // ------------------------------------------------------------------------------------ 0056 0057 QString WSToolUtils::randomString(const int& length) 0058 { 0059 const QString possibleCharacters(QLatin1String("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")); 0060 0061 QString randomString; 0062 QRandomGenerator* const generator = QRandomGenerator::global(); 0063 0064 for (int i = 0 ; i < length ; ++i) 0065 { 0066 const int index = generator->bounded(possibleCharacters.length()); 0067 QChar nextChar = possibleCharacters.at(index); 0068 randomString.append(nextChar); 0069 } 0070 0071 return randomString; 0072 } 0073 0074 // ------------------------------------------------------------------------------------ 0075 0076 QSettings* WSToolUtils::getOauthSettings(QObject* const parent) 0077 { 0078 QString dkoauth = oauthConfigFile(); 0079 0080 return (new QSettings(dkoauth, QSettings::IniFormat, parent)); 0081 } 0082 0083 void WSToolUtils::saveToken(const QString& name, const QString& token) 0084 { 0085 QString dkoauth = oauthConfigFile(); 0086 0087 QSettings settings(dkoauth, QSettings::IniFormat); 0088 QByteArray code = token.toLatin1().toBase64(); 0089 0090 settings.beginGroup(name); 0091 settings.setValue(QLatin1String("refreshToken"), code); 0092 settings.endGroup(); 0093 } 0094 0095 QString WSToolUtils::readToken(const QString& name) 0096 { 0097 QString dkoauth = oauthConfigFile(); 0098 0099 QSettings settings(dkoauth, QSettings::IniFormat); 0100 0101 settings.beginGroup(name); 0102 QByteArray code = settings.value(QLatin1String("refreshToken")).toByteArray(); 0103 settings.endGroup(); 0104 0105 return QString::fromLatin1(QByteArray::fromBase64(code)); 0106 } 0107 0108 void WSToolUtils::clearToken(const QString& name) 0109 { 0110 QString dkoauth = oauthConfigFile(); 0111 0112 QSettings settings(dkoauth, QSettings::IniFormat); 0113 0114 settings.beginGroup(name); 0115 settings.remove(QString()); 0116 settings.endGroup(); 0117 } 0118 0119 QString WSToolUtils::oauthConfigFile() 0120 { 0121 QString dkoauth = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + 0122 QLatin1String("/digikam_oauthrc"); 0123 0124 return dkoauth; 0125 } 0126 0127 } // namespace Digikam