/* * This file is part of the DSView project. * DSView is based on PulseView. * * Copyright (C) 2021 DreamSourceLab * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "appconfig.h" #include #include #include #include #include #include #include "../log.h" #define MAX_PROTOCOL_FORMAT_LIST 15 StringPair::StringPair(const std::string &key, const std::string &value) { m_key = key; m_value = value; } //------------function static QString FormatArrayToString(std::vector &protocolFormats) { QString str; for (StringPair &o : protocolFormats){ if (!str.isEmpty()){ str += ";"; } str += o.m_key.c_str(); str += "="; str += o.m_value.c_str(); } return str; } static void StringToFormatArray(const QString &str, std::vector &protocolFormats) { QStringList arr = str.split(";"); for (int i=0; i MAX_PROTOCOL_FORMAT_LIST) { while (appOptions.m_protocolFormats.size() < MAX_PROTOCOL_FORMAT_LIST) { appOptions.m_protocolFormats.erase(appOptions.m_protocolFormats.begin()); } } appOptions.m_protocolFormats.push_back(StringPair(protocolName, value)); bChange = true; } if (bChange){ SaveApp(); } } std::string AppConfig::GetProtocolFormat(const std::string &protocolName) { for (StringPair &o : appOptions.m_protocolFormats){ if (o.m_key == protocolName){ return o.m_value; } } return ""; } //-------------api QString GetIconPath() { QString style = AppConfig::Instance().frameOptions.style; if (style == ""){ style = THEME_STYLE_DARK; } return ":/icons/" + style; } QString GetAppDataDir() { //applicationDirPath not end with '/' #ifdef Q_OS_LINUX QDir dir(QCoreApplication::applicationDirPath()); if (dir.cd("..") && dir.cd("share") && dir.cd("DSView")) { return dir.absolutePath(); } QDir dir1("/usr/local/share/DSView"); if (dir1.exists()){ return dir1.absolutePath(); } dsv_err("Data directory is not exists: ../share/DSView"); assert(false); #else #ifdef Q_OS_DARWIN QDir dir1(QCoreApplication::applicationDirPath()); //"./res" is not exists if (dir1.cd("res") == false){ QDir dir(QCoreApplication::applicationDirPath()); // ../share/DSView if (dir.cd("..") && dir.cd("share") && dir.cd("DSView")) { return dir.absolutePath(); } } #endif // The bin location return QCoreApplication::applicationDirPath(); #endif } QString GetFirmwareDir() { QDir dir1 = GetAppDataDir() + "/res"; // ./res if (dir1.exists()){ return dir1.absolutePath(); } QDir dir(QCoreApplication::applicationDirPath()); // ../share/DSView/res if (dir.cd("..") && dir.cd("share") && dir.cd("DSView") && dir.cd("res")) { return dir.absolutePath(); } dsv_err("%s%s", "Resource directory is not exists:", dir1.absolutePath().toUtf8().data()); return dir1.absolutePath(); } QString GetUserDataDir() { #if QT_VERSION >= 0x050400 return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); #else return QStandardPaths::writableLocation(QStandardPaths::DataLocation); #endif } QString GetDecodeScriptDir() { QString path = GetAppDataDir() + "/decoders"; QDir dir1; // ./decoders if (dir1.exists(path)) { return path; } QDir dir(QCoreApplication::applicationDirPath()); // ../share/libsigrokdecode4DSL/decoders if (dir.cd("..") && dir.cd("share") && dir.cd("libsigrokdecode4DSL") && dir.cd("decoders")) { return dir.absolutePath(); } return ""; } QString GetProfileDir() { #if QT_VERSION >= 0x050400 return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); #else return QStandardPaths::writableLocation(QStandardPaths::DataLocation); #endif }