1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2015 - 2018 Michal Dutkiewicz aka Emdek <michal@emdek.pl>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 **************************************************************************/
19 
20 #ifndef OTTER_TOOLBARSMANAGER_H
21 #define OTTER_TOOLBARSMANAGER_H
22 
23 #include <QtCore/QCoreApplication>
24 #include <QtCore/QVariantMap>
25 #include <QtCore/QVector>
26 
27 namespace Otter
28 {
29 
30 class ToolBarsManager final : public QObject
31 {
32 	Q_OBJECT
33 	Q_ENUMS(ToolBarIdentifier)
34 
35 public:
36 	enum ToolBarIdentifier
37 	{
38 		MenuBar = 0,
39 		BookmarksBar,
40 		TabBar,
41 		AddressBar,
42 		ProgressBar,
43 		SideBar,
44 		StatusBar,
45 		ErrorConsoleBar,
46 		OtherToolBar
47 	};
48 
49 	enum ToolBarType
50 	{
51 		ActionsBarType = 0,
52 		BookmarksBarType,
53 		SideBarType
54 	};
55 
56 	enum ToolBarVisibility
57 	{
58 		AlwaysVisibleToolBar = 0,
59 		OnHoverVisibleToolBar,
60 		AutoVisibilityToolBar,
61 		AlwaysHiddenToolBar
62 	};
63 
64 	enum ToolBarsMode
65 	{
66 		NormalMode = 0,
67 		FullScreenMode
68 	};
69 
70 	struct ToolBarDefinition final
71 	{
72 		struct Entry final
73 		{
74 			QString action;
75 			QVariantMap options;
76 			QVariantMap parameters;
77 			QVector<Entry> entries;
78 
79 			bool operator ==(const Entry &other) const
80 			{
81 				return (action == other.action && options == other.options && parameters == other.parameters && entries == other.entries);
82 			}
83 		};
84 
85 		QString title;
86 		QString bookmarksPath;
87 		QString currentPanel;
88 		QStringList panels;
89 		QVector<Entry> entries;
90 		ToolBarType type = ActionsBarType;
91 		ToolBarVisibility normalVisibility = AlwaysVisibleToolBar;
92 		ToolBarVisibility fullScreenVisibility = AlwaysHiddenToolBar;
93 		Qt::ToolBarArea location = Qt::NoToolBarArea;
94 		Qt::ToolButtonStyle buttonStyle = Qt::ToolButtonIconOnly;
95 		int identifier = -1;
96 		int iconSize = -1;
97 		int maximumButtonSize = -1;
98 		int panelSize = -1;
99 		int row = -1;
100 		bool hasToggle = false;
101 		bool isDefault = false;
102 		bool wasRemoved = false;
103 
setVisibilityfinal104 		void setVisibility(ToolBarsMode mode, ToolBarVisibility visibility)
105 		{
106 			if (mode == FullScreenMode)
107 			{
108 				fullScreenVisibility = visibility;
109 			}
110 			else
111 			{
112 				normalVisibility = visibility;
113 			}
114 		}
115 
getTitlefinal116 		QString getTitle() const
117 		{
118 			return (isDefault ? QCoreApplication::translate("actions", title.toUtf8()) : title);
119 		}
120 
getVisibilityfinal121 		ToolBarVisibility getVisibility(ToolBarsMode mode) const
122 		{
123 			return ((mode == FullScreenMode) ? fullScreenVisibility : normalVisibility);
124 		}
125 
126 		bool operator ==(const ToolBarDefinition &other) const
127 		{
128 			return (title == other.title && bookmarksPath == other.bookmarksPath && currentPanel == other.currentPanel && panels == other.panels && entries == other.entries && type == other.type && normalVisibility == other.normalVisibility && fullScreenVisibility == other.fullScreenVisibility && location == other.location && buttonStyle == other.buttonStyle && identifier == other.identifier && iconSize == other.iconSize && maximumButtonSize == other.maximumButtonSize && panelSize == other.panelSize && row == other.row && hasToggle == other.hasToggle);
129 		}
130 
canResetfinal131 		bool canReset() const
132 		{
133 			return (!isDefault && identifier < OtherToolBar);
134 		}
135 
isGlobalfinal136 		bool isGlobal() const
137 		{
138 			return (identifier != AddressBar && identifier != ProgressBar);
139 		}
140 
isValidfinal141 		bool isValid() const
142 		{
143 			return (identifier >= 0);
144 		}
145 	};
146 
147 	static void createInstance();
148 	static void addToolBar(ToolBarType type);
149 	static void configureToolBar(int identifier);
150 	static void resetToolBar(int identifier);
151 	static void removeToolBar(int identifier);
152 	static void resetToolBars();
153 	static void setToolBar(ToolBarsManager::ToolBarDefinition definition);
154 	static ToolBarsManager* getInstance();
155 	static QString getToolBarName(int identifier);
156 	static QVector<ToolBarsManager::ToolBarDefinition> getToolBarDefinitions(Qt::ToolBarAreas areas = Qt::AllToolBarAreas);
157 	static ToolBarsManager::ToolBarDefinition getToolBarDefinition(int identifier);
158 	static int getToolBarIdentifier(const QString &name);
159 	static bool areToolBarsLocked();
160 
161 protected:
162 	explicit ToolBarsManager(QObject *parent);
163 
164 	void timerEvent(QTimerEvent *event) override;
165 	static void ensureInitialized();
166 	static QJsonValue encodeEntry(const ToolBarDefinition::Entry &definition);
167 	static ToolBarDefinition::Entry decodeEntry(const QJsonValue &value);
168 	static QHash<QString, ToolBarDefinition> loadToolBars(const QString &path, bool isDefault);
169 
170 protected slots:
171 	void scheduleSave();
172 	void handleOptionChanged(int identifier, const QVariant &value);
173 
174 private:
175 	int m_saveTimer;
176 
177 	static ToolBarsManager *m_instance;
178 	static QMap<int, QString> m_identifiers;
179 	static QVector<ToolBarDefinition> m_definitions;
180 	static int m_toolBarIdentifierEnumerator;
181 	static bool m_areToolBarsLocked;
182 	static bool m_isLoading;
183 
184 signals:
185 	void toolBarAdded(int identifier);
186 	void toolBarModified(int identifier);
187 	void toolBarMoved(int identifier);
188 	void toolBarRemoved(int identifier);
189 	void toolBarsLockedChanged(bool areLocked);
190 };
191 
192 }
193 
194 #endif
195