// Copyright 2017 Alejandro Sirgo Rica // // This file is part of Flameshot. // // Flameshot 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 3 of the License, or // (at your option) any later version. // // Flameshot 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 Flameshot. If not, see . #include "confighandler.h" #include #include #include ConfigHandler::ConfigHandler(){ m_settings.setDefaultFormat(QSettings::IniFormat); } QList ConfigHandler::getButtons() { QList buttons = m_settings.value("buttons").value >(); bool modified = normalizeButtons(buttons); if (modified) { m_settings.setValue("buttons", QVariant::fromValue(buttons)); } return fromIntToButton(buttons); } void ConfigHandler::setButtons(const QList &buttons) { QList l = fromButtonToInt(buttons); normalizeButtons(l); m_settings.setValue("buttons", QVariant::fromValue(l)); } QString ConfigHandler::savePathValue() { return m_settings.value("savePath").toString(); } void ConfigHandler::setSavePath(const QString &savePath) { m_settings.setValue("savePath", savePath); } QColor ConfigHandler::uiMainColorValue() { return m_settings.value("uiColor").value(); } void ConfigHandler::setUIMainColor(const QColor &c) { m_settings.setValue("uiColor", c); } QColor ConfigHandler::uiContrastColorValue() { return m_settings.value("contastUiColor").value(); } void ConfigHandler::setUIContrastColor(const QColor &c) { m_settings.setValue("contastUiColor", c); } QColor ConfigHandler::drawColorValue() { return m_settings.value("drawColor").value(); } void ConfigHandler::setDrawColor(const QColor &c) { m_settings.setValue("drawColor", c); } bool ConfigHandler::showHelpValue() { return m_settings.value("showHelp").toBool(); } void ConfigHandler::setShowHelp(const bool showHelp) { m_settings.setValue("showHelp", showHelp); } bool ConfigHandler::desktopNotificationValue() { return m_settings.value("showDesktopNotification").toBool(); } void ConfigHandler::setDesktopNotification(const bool showDesktopNotification) { m_settings.setValue("showDesktopNotification", showDesktopNotification); } QString ConfigHandler::filenamePatternValue() { return m_settings.value("filenamePattern").toString(); } void ConfigHandler::setFilenamePattern(const QString &pattern) { return m_settings.setValue("filenamePattern", pattern); } bool ConfigHandler::disabledTrayIconValue() { return m_settings.value("disabledTrayIcon").toBool(); } void ConfigHandler::setDisabledTrayIcon(const bool disabledTrayIcon) { m_settings.setValue("disabledTrayIcon", disabledTrayIcon); } int ConfigHandler::drawThicknessValue() { return m_settings.value("drawThickness").toInt(); } void ConfigHandler::setdrawThickness(const int thickness) { m_settings.setValue("drawThickness", thickness); } bool ConfigHandler::keepOpenAppLauncherValue() { return m_settings.value("keepOpenAppLauncher").toBool(); } void ConfigHandler::setKeepOpenAppLauncher(const bool keepOpen) { m_settings.setValue("keepOpenAppLauncher", keepOpen); } bool ConfigHandler::startupLaunchValue() { bool res = false; #if defined(Q_OS_LINUX) QString path = QDir::homePath() + "/.config/autostart/Flameshot.desktop"; res = QFile(path).exists(); #elif defined(Q_OS_WIN) QSettings bootUpSettings( "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); res = !bootUpSettings.value("Flameshot").toString().isEmpty(); #endif return res; } void ConfigHandler::setStartupLaunch(const bool start) { #if defined(Q_OS_LINUX) QString path = QDir::homePath() + "/.config/autostart/Flameshot.desktop"; QFile file(path); if (start) { if (file.open(QIODevice::WriteOnly)) { file.write("[Desktop Entry]\nIcon=system-run\nExec=flameshot\nTerminal=false"); } } else { file.remove(); } #elif defined(Q_OS_WIN) QSettings bootUpSettings( "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); if (start) { QString app_path = QCoreApplication::applicationFilePath(); bootUpSettings.setValue("Flameshot", app_path); } else { bootUpSettings.remove("Flameshot"); } #endif } bool ConfigHandler::initiatedIsSet() { return m_settings.value("initiated").toBool(); } void ConfigHandler::setInitiated() { m_settings.setValue("initiated", true); } void ConfigHandler::setNotInitiated() { m_settings.setValue("initiated", false); } void ConfigHandler::setDefaults() { setShowHelp(true); setDesktopNotification(true); setDrawColor(QColor(Qt::red)); setUIMainColor(QColor(116, 0, 150)); setUIContrastColor(QColor(86, 0, 120)); setdrawThickness(0); setAllTheButtons(); } void ConfigHandler::setAllTheButtons() { QList buttons; auto listTypes = CaptureButton::getIterableButtonTypes(); for (const CaptureButton::ButtonType t: listTypes) { buttons << static_cast(t); } m_settings.setValue("buttons", QVariant::fromValue(buttons)); } QString ConfigHandler::configFilePath() const { return m_settings.fileName(); } bool ConfigHandler::normalizeButtons(QList &buttons) { auto listTypes = CaptureButton::getIterableButtonTypes(); QList listTypesInt; for(auto i: listTypes) listTypesInt << static_cast(i); bool hasChanged = false; QMutableListIterator i(buttons); while (i.hasNext()) { if (!listTypesInt.contains(i.next())) { i.remove(); hasChanged = true; } } std::sort(buttons.begin(), buttons.end(), [](int a, int b){ return CaptureButton::getPriorityByButton((CaptureButton::ButtonType)a) < CaptureButton::getPriorityByButton((CaptureButton::ButtonType)b); }); return hasChanged; } QList ConfigHandler::fromIntToButton( const QList &l) { QList buttons; for (auto const i: l) buttons << static_cast(i); return buttons; } QList ConfigHandler::fromButtonToInt( const QList &l) { QList buttons; for (auto const i: l) buttons << static_cast(i); return buttons; }