File indexing completed on 2024-04-21 05:42:39

0001 // clang-format off
0002 /*
0003  * KDiff3 - Text Diff And Merge Tool
0004  *
0005  * SPDX-FileCopyrightText: 2019-2020 Michael Reeves reeves.87@gmail.com
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 // clang-format on
0009 
0010 #include "options.h"
0011 
0012 #include "combiners.h"
0013 #include "ConfigValueMap.h"
0014 #include "diff.h"
0015 #include "OptionItems.h"
0016 
0017 #include <boost/signals2.hpp>
0018 #include <memory>
0019 
0020 #include <KSharedConfig>
0021 
0022 boost::signals2::signal<void ()> Options::apply;
0023 boost::signals2::signal<void ()> Options::resetToDefaults;
0024 boost::signals2::signal<void ()> Options::setToCurrent;
0025 boost::signals2::signal<void (ValueMap*)> Options::read;
0026 boost::signals2::signal<void (ValueMap*)> Options::write;
0027 
0028 boost::signals2::signal<void ()> Options::preserve;
0029 boost::signals2::signal<void ()> Options::unpreserve;
0030 
0031 boost::signals2::signal<bool (const QString&, const QString&), find> Options::accept;
0032 
0033 OptionItemBase::OptionItemBase(const QString& saveName)
0034 {
0035     m_saveName = saveName;
0036     m_bPreserved = false;
0037 
0038     connections.push_back(Options::apply.connect(boost::bind(&OptionItemBase::apply, this)));
0039     connections.push_back(Options::setToCurrent.connect(boost::bind(&OptionItemBase::setToCurrent, this)));
0040     connections.push_back(Options::resetToDefaults.connect(boost::bind(&OptionItemBase::setToDefault, this)));
0041 
0042     connections.push_back(Options::read.connect(boost::bind(&OptionItemBase::read, this, boost::placeholders::_1)));
0043     connections.push_back(Options::write.connect(boost::bind(&OptionItemBase::write, this, boost::placeholders::_1)));
0044 
0045     connections.push_back(Options::preserve.connect(boost::bind(&OptionItemBase::preserve, this)));
0046     connections.push_back(Options::unpreserve.connect(boost::bind(&OptionItemBase::unpreserve, this)));
0047 
0048     connections.push_back(Options::accept.connect(boost::bind(&OptionItemBase::accept, this, boost::placeholders::_1, boost::placeholders::_2)));
0049 }
0050 
0051 bool OptionItemBase::accept(const QString& key, const QString& val)
0052 {
0053     if(getSaveName() != key)
0054         return false;
0055 
0056     preserve();
0057 
0058     ValueMap config;
0059     config.writeEntry(key, val); // Write the value as a string and
0060     read(&config);               // use the internal conversion from string to the needed value.
0061 
0062     return true;
0063 }
0064 
0065 void Options::init()
0066 {
0067     //Settings not in Options dialog
0068     addOptionItem(std::make_unique<OptionSize>(QSize(600, 400), "Geometry", &m_geometry));
0069     addOptionItem(std::make_unique<OptionPoint>(QPoint(0, 22), "Position", &m_position));
0070     addOptionItem(std::make_unique<OptionToggleAction>(false, "WindowStateFullScreen", &m_bFullScreen));
0071     addOptionItem(std::make_unique<OptionToggleAction>(false, "WindowStateMaximised", &m_bMaximized));
0072 
0073     addOptionItem(std::make_unique<OptionToggleAction>(true, "Show Statusbar", &m_bShowStatusBar));
0074 
0075     //Options in Options dialog.
0076     addOptionItem(std::make_unique<OptionToggleAction>(false, "AutoAdvance", &m_bAutoAdvance));
0077     addOptionItem(std::make_unique<OptionToggleAction>(true, "ShowWhiteSpaceCharacters", &m_bShowWhiteSpaceCharacters));
0078     addOptionItem(std::make_unique<OptionToggleAction>(true, "ShowWhiteSpace", &m_bShowWhiteSpace));
0079     addOptionItem(std::make_unique<OptionToggleAction>(false, "ShowLineNumbers", &m_bShowLineNumbers));
0080     addOptionItem(std::make_unique<OptionToggleAction>(true, "HorizDiffWindowSplitting", &m_bHorizDiffWindowSplitting));
0081     addOptionItem(std::make_unique<OptionToggleAction>(false, "WordWrap", &m_bWordWrap));
0082 
0083     addOptionItem(std::make_unique<OptionToggleAction>(true, "ShowIdenticalFiles", &m_bDmShowIdenticalFiles));
0084 
0085     addOptionItem(std::make_unique<OptionStringList>(&m_recentAFiles, "RecentAFiles"));
0086     addOptionItem(std::make_unique<OptionStringList>(&m_recentBFiles, "RecentBFiles"));
0087     addOptionItem(std::make_unique<OptionStringList>(&m_recentCFiles, "RecentCFiles"));
0088     addOptionItem(std::make_unique<OptionStringList>(&m_recentOutputFiles, "RecentOutputFiles"));
0089     addOptionItem(std::make_unique<OptionStringList>(&m_recentEncodings, "RecentEncodings"));
0090 }
0091 
0092 void Options::saveOptions(const KSharedConfigPtr config)
0093 {
0094     // No i18n()-Translations here!
0095 
0096     ConfigValueMap cvm(config->group(KDIFF3_CONFIG_GROUP));
0097 
0098     unpreserve();
0099     write(&cvm);
0100 }
0101 
0102 void Options::readOptions(const KSharedConfigPtr config)
0103 {
0104     // No i18n()-Translations here!
0105 
0106     ConfigValueMap cvm(config->group(KDIFF3_CONFIG_GROUP));
0107 
0108     read(&cvm);
0109 
0110     if(m_whiteSpace2FileMergeDefault <= (qint32)e_SrcSelector::Min)
0111         m_whiteSpace2FileMergeDefault = (qint32)e_SrcSelector::None;
0112 
0113     if(m_whiteSpace2FileMergeDefault > (qint32)e_SrcSelector::Max)
0114         m_whiteSpace2FileMergeDefault = (qint32)e_SrcSelector::C;
0115 }
0116 
0117 const QString Options::parseOptions(const QStringList& optionList)
0118 {
0119     QString result;
0120 
0121     for(const QString& optionString: optionList)
0122     {
0123         QtSizeType pos = optionString.indexOf('=');
0124         if(pos > 0) // seems not to have a tag
0125         {
0126             const QString key = optionString.left(pos);
0127             const QString val = optionString.mid(pos + 1);
0128 
0129             bool bFound = accept(key, val);
0130 
0131             if(!bFound)
0132             {
0133                 result += "No config item named \"" + key + "\"\n";
0134             }
0135         }
0136         else
0137         {
0138             result += "No '=' found in \"" + optionString + "\"\n";
0139         }
0140     }
0141     return result;
0142 }
0143 
0144 QString Options::calcOptionHelp()
0145 {
0146     ValueMap config;
0147 
0148     write(&config);
0149 
0150     return config.getAsString();
0151 }
0152 
0153 void Options::addOptionItem(std::shared_ptr<OptionItemBase> inItem)
0154 {
0155     mOptionItemList.push_back(inItem);
0156 }