| 1 | /* | 
| 2 |  * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) | 
| 3 |  * | 
| 4 |  * This program is free software: you can redistribute it and/or modify | 
| 5 |  * it under the terms of the GNU General Public License as published by | 
| 6 |  * the Free Software Foundation, either version 3 of the License, or | 
| 7 |  * (at your option) any later version. | 
| 8 |  * | 
| 9 |  * This program is distributed in the hope that it will be useful, | 
| 10 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 11 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 12 |  * GNU General Public License for more details. | 
| 13 |  * | 
| 14 |  * You should have received a copy of the GNU General Public License | 
| 15 |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
| 16 |  */ | 
| 17 | #include "autolinkmanager.h" | 
| 18 | #include "settings.h" | 
| 19 |  | 
| 20 | #include <QDir> | 
| 21 | #include <QJsonArray> | 
| 22 | #include <QJsonDocument> | 
| 23 | #include <QJsonObject> | 
| 24 | #include "systemconsts.h" | 
| 25 |  | 
| 26 | AutolinkManager* pAutolinkManager; | 
| 27 |  | 
| 28 | AutolinkManager::AutolinkManager() | 
| 29 | { | 
| 30 |  | 
| 31 | } | 
| 32 |  | 
| 33 | PAutolink AutolinkManager::getLink(const QString &) const | 
| 34 | { | 
| 35 |     PAutolink link = mLinks.value(header,PAutolink()); | 
| 36 |     if (link) | 
| 37 |         return link; | 
| 38 |     foreach (QString key, mLinks.keys()) { | 
| 39 |         if (header.endsWith("/" +key, PATH_SENSITIVITY)) { | 
| 40 |             return mLinks.value(key); | 
| 41 |         } | 
| 42 |     } | 
| 43 |     return PAutolink(); | 
| 44 | } | 
| 45 |  | 
| 46 | void AutolinkManager::load() | 
| 47 | { | 
| 48 |     QDir dir(pSettings->dirs().config()); | 
| 49 |     QString filename=dir.filePath(DEV_AUTOLINK_FILE); | 
| 50 |     QFile file(filename); | 
| 51 |     if (!file.exists()) { | 
| 52 | #ifdef Q_OS_WIN | 
| 53 |         QFile preFile(":/config/autolink.json" ); | 
| 54 |         if (!preFile.open(QFile::ReadOnly)) { | 
| 55 |             throw FileError(QObject::tr("Can't open file '%1' for read." ) | 
| 56 |                             .arg(":/config/autolink.json" )); | 
| 57 |         } | 
| 58 |         QByteArray content=preFile.readAll(); | 
| 59 |         if (!file.open(QFile::WriteOnly|QFile::Truncate)) { | 
| 60 |             throw FileError(QObject::tr("Can't open file '%1' for write." ) | 
| 61 |                             .arg(filename)); | 
| 62 |         } | 
| 63 |         file.write(content); | 
| 64 |         file.close(); | 
| 65 |         preFile.close(); | 
| 66 | #elif defined(Q_OS_LINUX) | 
| 67 |         QFile preFile(":/config/autolink-linux.json" ); | 
| 68 |         if (!preFile.open(QFile::ReadOnly)) { | 
| 69 |             throw FileError(QObject::tr("Can't open file '%1' for read." ) | 
| 70 |                             .arg(":/config/autolink-linux.json" )); | 
| 71 |         } | 
| 72 |         QByteArray content=preFile.readAll(); | 
| 73 |         if (!file.open(QFile::WriteOnly|QFile::Truncate)) { | 
| 74 |             throw FileError(QObject::tr("Can't open file '%1' for write." ) | 
| 75 |                             .arg(filename)); | 
| 76 |         } | 
| 77 |         file.write(content); | 
| 78 |         file.close(); | 
| 79 |         preFile.close(); | 
| 80 | #else | 
| 81 |         return; | 
| 82 | #endif | 
| 83 |     } | 
| 84 |     if (file.open(QFile::ReadOnly)) { | 
| 85 |         QByteArray content = file.readAll(); | 
| 86 |         QJsonDocument doc(QJsonDocument::fromJson(content)); | 
| 87 |         fromJson(doc.array()); | 
| 88 |         file.close(); | 
| 89 |     } else { | 
| 90 |         throw FileError(QObject::tr("Can't open file '%1' for read." ) | 
| 91 |                         .arg(filename)); | 
| 92 |     } | 
| 93 | } | 
| 94 |  | 
| 95 | void AutolinkManager::save() | 
| 96 | { | 
| 97 |     QDir dir(pSettings->dirs().config()); | 
| 98 |     QString filename=dir.filePath(DEV_AUTOLINK_FILE); | 
| 99 |     QFile file(filename); | 
| 100 |     if (file.open(QFile::WriteOnly|QFile::Truncate)) { | 
| 101 |         QJsonDocument doc(toJson()); | 
| 102 |         file.write(doc.toJson(QJsonDocument::Indented)); | 
| 103 |         file.close(); | 
| 104 |     } else { | 
| 105 |         throw FileError(QObject::tr("Can't open file '%1' for write." ) | 
| 106 |                         .arg(filename)); | 
| 107 |     } | 
| 108 | } | 
| 109 |  | 
| 110 | void AutolinkManager::setLink(const QString &, const QString &linkOption, bool execUseUTF8) | 
| 111 | { | 
| 112 |     PAutolink link = mLinks.value(header,PAutolink()); | 
| 113 |     if (link) { | 
| 114 |         link->linkOption = linkOption; | 
| 115 |         link->execUseUTF8 = execUseUTF8; | 
| 116 |     } else { | 
| 117 |         link = std::make_shared<Autolink>(); | 
| 118 |         link->header = header; | 
| 119 |         link->linkOption = linkOption; | 
| 120 |         link->execUseUTF8 = execUseUTF8; | 
| 121 |         mLinks.insert(header,link); | 
| 122 |     } | 
| 123 | } | 
| 124 |  | 
| 125 | const QMap<QString, PAutolink> &AutolinkManager::links() const | 
| 126 | { | 
| 127 |     return mLinks; | 
| 128 | } | 
| 129 |  | 
| 130 | void AutolinkManager::clear() | 
| 131 | { | 
| 132 |     mLinks.clear(); | 
| 133 | } | 
| 134 |  | 
| 135 | QJsonArray AutolinkManager::toJson() | 
| 136 | { | 
| 137 |     QJsonArray result; | 
| 138 |     foreach (const QString& , mLinks.keys()){ | 
| 139 |         QJsonObject autolink; | 
| 140 |         autolink["header" ]=header; | 
| 141 |         autolink["links" ]=mLinks[header]->linkOption; | 
| 142 |         autolink["execUseUTF8" ]=mLinks[header]->execUseUTF8; | 
| 143 |         result.append(autolink); | 
| 144 |     } | 
| 145 |     return result; | 
| 146 | } | 
| 147 |  | 
| 148 | void AutolinkManager::fromJson(QJsonArray json) | 
| 149 | { | 
| 150 |     clear(); | 
| 151 |     for (int i=0;i<json.size();i++) { | 
| 152 |         QJsonObject obj = json[i].toObject(); | 
| 153 |         setLink(obj["header" ].toString(),obj["links" ].toString(),obj["execUseUTF8" ].toBool()); | 
| 154 |     } | 
| 155 | } | 
| 156 |  |