1 /* Copyright (c) 2013-2015 Jeffrey Pfau
2  *
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #pragma once
7 
8 #include "Override.h"
9 
10 #include <QHash>
11 #include <QObject>
12 #include <QSettings>
13 #include <QVariant>
14 
15 #include <functional>
16 #include <memory>
17 
18 #include <mgba/core/config.h>
19 #include <mgba-util/configuration.h>
20 #include <mgba/feature/commandline.h>
21 
22 class QMenu;
23 
24 struct mArguments;
25 struct GBACartridgeOverride;
26 
27 namespace QGBA {
28 
29 class Action;
30 class ActionMapper;
31 
32 class ConfigOption : public QObject {
33 Q_OBJECT
34 
35 public:
36 	ConfigOption(const QString& name, QObject* parent = nullptr);
37 
38 	void connect(std::function<void(const QVariant&)>, QObject* parent = nullptr);
39 
40 	Action* addValue(const QString& text, const QVariant& value, ActionMapper* actions = nullptr, const QString& menu = {});
41 	Action* addValue(const QString& text, const char* value, ActionMapper* actions = nullptr, const QString& menu = {});
42 	Action* addBoolean(const QString& text, ActionMapper* actions = nullptr, const QString& menu = {});
43 
name()44 	QString name() const { return m_name; }
45 
46 public slots:
47 	void setValue(bool value);
48 	void setValue(int value);
49 	void setValue(unsigned value);
50 	void setValue(const char* value);
51 	void setValue(const QVariant& value);
52 
53 signals:
54 	void valueChanged(const QVariant& value);
55 
56 private:
57 	QHash<QObject*, std::function<void(const QVariant&)>> m_slots;
58 	QList<std::pair<Action*, QVariant>> m_actions;
59 	QString m_name;
60 };
61 
62 class ConfigController : public QObject {
63 Q_OBJECT
64 
65 public:
66 	constexpr static const char* const PORT = "qt";
67 	static const int MRU_LIST_SIZE = 10;
68 
69 	ConfigController(QObject* parent = nullptr);
70 	~ConfigController();
71 
options()72 	const mCoreOptions* options() const { return &m_opts; }
73 	bool parseArguments(mArguments* args, int argc, char* argv[], mSubParser* subparser = nullptr);
74 
75 	ConfigOption* addOption(const char* key);
76 	void updateOption(const char* key);
77 
78 	QString getOption(const char* key, const QVariant& defaultVal = QVariant()) const;
79 	QString getOption(const QString& key, const QVariant& defaultVal = QVariant()) const;
80 
81 	QVariant getQtOption(const QString& key, const QString& group = QString()) const;
82 
83 	QList<QString> getMRU() const;
84 	void setMRU(const QList<QString>& mru);
85 
overrides()86 	Configuration* overrides() { return mCoreConfigGetOverrides(&m_config); }
87 	void saveOverride(const Override&);
88 
input()89 	Configuration* input() { return mCoreConfigGetInput(&m_config); }
90 
config()91 	const mCoreConfig* config() const { return &m_config; }
config()92 	mCoreConfig* config() { return &m_config; }
93 
94 	static const QString& configDir();
95 	static bool isPortable();
96 
97 public slots:
98 	void setOption(const char* key, bool value);
99 	void setOption(const char* key, int value);
100 	void setOption(const char* key, unsigned value);
101 	void setOption(const char* key, const char* value);
102 	void setOption(const char* key, const QVariant& value);
103 	void setQtOption(const QString& key, const QVariant& value, const QString& group = QString());
104 
105 	void makePortable();
106 	void write();
107 
108 private:
defaults()109 	Configuration* defaults() { return &m_config.defaultsTable; }
110 
111 	mCoreConfig m_config;
112 	mCoreOptions m_opts{};
113 
114 	QHash<QString, ConfigOption*> m_optionSet;
115 	std::unique_ptr<QSettings> m_settings;
116 	static QString s_configDir;
117 };
118 
119 }
120