File indexing completed on 2024-05-19 04:55:58

0001 /**
0002  * \file useractionsconfig.h
0003  * User actions configuration.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 29 Jun 2013
0008  *
0009  * Copyright (C) 2013-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #pragma once
0028 
0029 #include <QVariantList>
0030 #include "generalconfig.h"
0031 #include "kid3api.h"
0032 
0033 /**
0034  * User actions configuration.
0035  */
0036 class KID3_CORE_EXPORT UserActionsConfig : public StoredConfig<UserActionsConfig>
0037 {
0038   Q_OBJECT
0039   /** list of context menu commands */
0040   Q_PROPERTY(QVariantList contextMenuCommands
0041              READ contextMenuCommandVariantList
0042              WRITE setContextMenuCommandVariantList
0043              NOTIFY contextMenuCommandsChanged)
0044 
0045 public:
0046   /**
0047    * External command in context menu.
0048    */
0049   class MenuCommand {
0050   public:
0051     /**
0052      * Constructor.
0053      *
0054      * @param name display name
0055      * @param cmd  command string with argument codes
0056      * @param confirm true if confirmation required
0057      * @param showOutput true if output of command shall be shown
0058      */
0059     explicit MenuCommand(const QString& name = QString(),
0060                          const QString& cmd = QString(),
0061                          bool confirm = false, bool showOutput = false);
0062 
0063     /**
0064      * Constructor.
0065      *
0066      * @param strList string list with encoded command
0067      */
0068     explicit MenuCommand(const QStringList& strList);
0069 
0070     /**
0071      * Encode into string list.
0072      *
0073      * @return string list with encoded command.
0074      */
0075     QStringList toStringList() const;
0076 
0077     /**
0078      * Get the display name.
0079      * @return name.
0080      */
0081     const QString& getName() const { return m_name; }
0082 
0083     /**
0084      * Set the display name.
0085      * @param name display name
0086      */
0087     void setName(const QString& name) { m_name = name; }
0088 
0089     /**
0090      * Get the command string.
0091      * @return command string.
0092      */
0093     const QString& getCommand() const { return m_cmd; }
0094 
0095     /**
0096      * Set the command string.
0097      * @param cmd command string.
0098      */
0099     void setCommand(const QString& cmd) { m_cmd = cmd; }
0100 
0101     /**
0102      * Check if command must be confirmed.
0103      * @return true if command has to be confirmed.
0104      */
0105     bool mustBeConfirmed() const { return m_confirm; }
0106 
0107     /**
0108      * Set if command must be confirmed.
0109      * @param confirm true if command has to be confirmed
0110      */
0111     void setMustBeConfirmed(bool confirm) { m_confirm = confirm; }
0112 
0113     /**
0114      * Check if command output has to be shown.
0115      * @return true if command output has to be shown.
0116      */
0117     bool outputShown() const { return m_showOutput; }
0118 
0119     /**
0120      * Set if command output has to be shown.
0121      * @param showOutput true if command output has to be shown
0122      */
0123     void setOutputShown(bool showOutput) { m_showOutput = showOutput; }
0124 
0125     /**
0126      * Test for equality.
0127      * @param rhs right hand side
0128      * @return true if equal.
0129      */
0130     bool operator==(const MenuCommand& rhs) const {
0131       return m_name == rhs.m_name && m_cmd == rhs.m_cmd &&
0132           m_confirm == rhs.m_confirm && m_showOutput == rhs.m_showOutput;
0133     }
0134 
0135   private:
0136     QString m_name;
0137     QString m_cmd;
0138     bool m_confirm;
0139     bool m_showOutput;
0140   };
0141 
0142   /**
0143    * Constructor.
0144    */
0145   UserActionsConfig();
0146 
0147   /**
0148    * Destructor.
0149    */
0150   ~UserActionsConfig() override = default;
0151 
0152   /**
0153    * Persist configuration.
0154    *
0155    * @param config configuration
0156    */
0157   void writeToConfig(ISettings* config) const override;
0158 
0159   /**
0160    * Read persisted configuration.
0161    *
0162    * @param config configuration
0163    */
0164   void readFromConfig(ISettings* config) override;
0165 
0166   /** Get list of context menu commands. */
0167   QList<MenuCommand> contextMenuCommands() const {
0168     return m_contextMenuCommands;
0169   }
0170 
0171   /** Set list of context menu commands. */
0172   void setContextMenuCommands(const QList<MenuCommand>& contextMenuCommands);
0173 
0174   /** Get list of context menu commands as variant list. */
0175   QVariantList contextMenuCommandVariantList() const;
0176 
0177   /** Set list of context menu commands from variant list. */
0178   void setContextMenuCommandVariantList(const QVariantList& lst);
0179 
0180   /**
0181    * Set default user actions.
0182    * @param upgradeOnly if true only upgrade configuration with new actions
0183    */
0184   void setDefaultUserActions(bool upgradeOnly = false);
0185 
0186 signals:
0187   /** Emitted when commands changed. */
0188   void contextMenuCommandsChanged();
0189 
0190 private:
0191   friend UserActionsConfig& StoredConfig<UserActionsConfig>::instance();
0192 
0193   /** commands available in context menu */
0194   QList<MenuCommand> m_contextMenuCommands;
0195 
0196   /** Index in configuration storage */
0197   static int s_index;
0198 };