File indexing completed on 2024-07-21 13:06:26

0001 /**
0002  * SPDX-FileCopyrightText: 1999-2001 Lubos Lunak <l.lunak@kde.org>
0003  * SPDX-FileCopyrightText: 2009 Michael Jansen <kde@michael-jansen.biz>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  **/
0007 
0008 #include "settings_writer.h"
0009 
0010 #include "action_data/action_data.h"
0011 #include "action_data/action_data_group.h"
0012 #include "action_data/generic_action_data.h"
0013 #include "action_data/menuentry_shortcut_action_data.h"
0014 #include "action_data/simple_action_data.h"
0015 
0016 #include "conditions/conditions_list.h"
0017 
0018 #include "windows_helper/window_selection_list.h"
0019 
0020 #include <KConfig>
0021 #include <KConfigGroup>
0022 #include <QDebug>
0023 
0024 namespace KHotKeys
0025 {
0026 const int CurrentFileVersion = 2;
0027 
0028 SettingsWriter::SettingsWriter(const Settings *settings, ActionState state, const QString &id, bool allowMerging)
0029     : _settings(settings)
0030     , _state(state)
0031     , _importId(id)
0032     , _allowMerging(allowMerging)
0033     , _export(false)
0034 {
0035 }
0036 
0037 void SettingsWriter::exportTo(const ActionDataBase *element, KConfigBase &config)
0038 {
0039     _export = true;
0040 
0041     if (!element) {
0042         Q_ASSERT(element);
0043         return;
0044     }
0045 
0046     // Clean the file
0047     QStringList groups = config.groupList();
0048     Q_FOREACH (const QString &name, config.groupList()) {
0049         config.deleteGroup(name);
0050     }
0051 
0052     KConfigGroup mainGroup(&config, "Main");
0053     mainGroup.writeEntry("Version", CurrentFileVersion);
0054     mainGroup.writeEntry("AllowMerge", _allowMerging);
0055 
0056     if (!_importId.isEmpty())
0057         mainGroup.writeEntry("ImportId", _importId);
0058 
0059     // The root group contains nothing but the datacount!
0060     KConfigGroup dataGroup(&config, "Data");
0061     dataGroup.writeEntry("DataCount", 1);
0062 
0063     // The group for the element to export
0064     KConfigGroup data1Group(&config, "Data_1");
0065     _stack.push(&data1Group);
0066     element->accept(this);
0067     _stack.pop();
0068 
0069     _export = false;
0070 }
0071 
0072 void SettingsWriter::visitActionData(const ActionData *data)
0073 {
0074     visitActionDataBase(data);
0075 
0076     KConfigGroup *config = _stack.top();
0077 
0078     // Write triggers if available
0079     if (data->triggers()) {
0080         KConfigGroup triggersGroup(config->config(), config->name() + "Triggers");
0081         data->triggers()->cfg_write(triggersGroup);
0082     }
0083 
0084     // Write actions if available
0085     if (data->actions()) {
0086         KConfigGroup actionsGroup(config->config(), config->name() + "Actions");
0087         data->actions()->cfg_write(actionsGroup);
0088     }
0089 }
0090 
0091 void SettingsWriter::visitActionDataBase(const ActionDataBase *base)
0092 {
0093     KConfigGroup *config = _stack.top();
0094 
0095     config->writeEntry("Type", "ERROR"); // derived classes should call with their type
0096     config->writeEntry("Name", base->name());
0097     config->writeEntry("Comment", base->comment());
0098 
0099     switch (_state) {
0100     case KHotKeys::Retain:
0101         config->writeEntry("Enabled", base->isEnabled(ActionDataBase::Ignore));
0102         break;
0103 
0104     case KHotKeys::Enabled:
0105         config->writeEntry("Enabled", true);
0106         break;
0107 
0108     case KHotKeys::Disabled:
0109         config->writeEntry("Enabled", false);
0110         break;
0111 
0112     default:
0113         Q_ASSERT(false);
0114         config->writeEntry("Enabled", false);
0115     }
0116 
0117     if (base->conditions()) {
0118         KConfigGroup conditionsConfig(config->config(), config->name() + "Conditions");
0119         base->conditions()->cfg_write(conditionsConfig);
0120     } else {
0121         qDebug() << "No conditions";
0122     }
0123 }
0124 
0125 void SettingsWriter::visitActionDataGroup(const ActionDataGroup *group)
0126 {
0127     visitActionDataBase(group);
0128 
0129     KConfigGroup *config = _stack.top();
0130 
0131     config->writeEntry("SystemGroup", int(group->system_group()));
0132     config->writeEntry("Type", "ACTION_DATA_GROUP");
0133 
0134     int cnt = 0;
0135     Q_FOREACH (ActionDataBase *child, group->children()) {
0136         ++cnt;
0137         KConfigGroup childConfig(config->config(), config->name() + QString("_") + QString::number(cnt));
0138         _stack.push(&childConfig);
0139         child->accept(this);
0140         _stack.pop();
0141     }
0142     config->writeEntry("DataCount", cnt);
0143 
0144     // We only write those two back if we do not export the settings
0145     if (!_export) {
0146         // ImportId only if set
0147         if (!group->importId().isEmpty())
0148             config->writeEntry("ImportId", group->importId());
0149         if (group->allowMerging())
0150             config->writeEntry("AllowMerge", group->allowMerging());
0151     }
0152 }
0153 
0154 void SettingsWriter::visitGenericActionData(const Generic_action_data *data)
0155 {
0156     visitActionData(data);
0157 
0158     KConfigGroup *config = _stack.top();
0159     config->writeEntry("Type", "GENERIC_ACTION_DATA");
0160 }
0161 
0162 void SettingsWriter::visitMenuentryShortcutActionData(const MenuEntryShortcutActionData *data)
0163 {
0164     visitActionData(data);
0165 
0166     KConfigGroup *config = _stack.top();
0167     config->writeEntry("Type", "MENUENTRY_SHORTCUT_ACTION_DATA");
0168 }
0169 
0170 void SettingsWriter::visitSimpleActionData(const SimpleActionData *data)
0171 {
0172     visitActionData(data);
0173 
0174     KConfigGroup *config = _stack.top();
0175     config->writeEntry("Type", "SIMPLE_ACTION_DATA");
0176 }
0177 
0178 void SettingsWriter::writeTo(KConfigBase &config)
0179 {
0180     QStringList groups = config.groupList();
0181     for (QStringList::ConstIterator it = groups.constBegin(); it != groups.constEnd(); ++it)
0182         config.deleteGroup(*it);
0183 
0184     // Write the global settings
0185     KConfigGroup mainGroup(&config, "Main");
0186     mainGroup.writeEntry("Version", 2);
0187     mainGroup.writeEntry("AlreadyImported", _settings->already_imported);
0188     mainGroup.writeEntry("Disabled", _settings->isDaemonDisabled());
0189 
0190     // Write the actions
0191     KConfigGroup dataGroup(&config, "Data");
0192     _stack.push(&dataGroup);
0193 
0194     int cnt = 0;
0195     if (_settings->actions()) {
0196         Q_FOREACH (ActionDataBase *child, _settings->actions()->children()) {
0197             ++cnt;
0198             KConfigGroup childConfig(dataGroup.config(), QString("Data_") + QString::number(cnt));
0199             _stack.push(&childConfig);
0200             child->accept(this);
0201             _stack.pop();
0202         }
0203     }
0204     dataGroup.writeEntry("DataCount", cnt);
0205     _stack.pop();
0206 
0207     // CHECKME Count still needed????
0208     // int cnt = write_actions( dataGroup, _settings->actions(), true );
0209     // mainGroup.writeEntry( "Autostart", cnt != 0 && !_settings->isDaemonDisabled() );
0210 
0211     // Write the gestures configuration
0212     KConfigGroup gesturesConfig(&config, "Gestures");
0213     gesturesConfig.writeEntry("Disabled", _settings->areGesturesDisabled());
0214     gesturesConfig.writeEntry("MouseButton", _settings->gestureMouseButton());
0215     gesturesConfig.writeEntry("Timeout", _settings->gestureTimeOut());
0216     if (_settings->gesturesExclude() != nullptr) {
0217         KConfigGroup gesturesExcludeConfig(&config, "GesturesExclude");
0218         _settings->gesturesExclude()->cfg_write(gesturesExcludeConfig);
0219     } else
0220         config.deleteGroup("GesturesExclude");
0221     KConfigGroup voiceConfig(&config, "Voice");
0222     voiceConfig.writeEntry("Shortcut", _settings->voice_shortcut.toString());
0223 }
0224 
0225 } // namespace KHotKeys