1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2016 - 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 parent program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 **************************************************************************/
19 
20 #include "WidgetFactory.h"
21 #include "MainWindow.h"
22 #include "TabBarWidget.h"
23 #include "ToolBarWidget.h"
24 #include "Window.h"
25 #include "../core/BookmarksManager.h"
26 #include "../modules/widgets/action/ActionWidget.h"
27 #include "../modules/widgets/address/AddressWidget.h"
28 #include "../modules/widgets/bookmark/BookmarkWidget.h"
29 #include "../modules/widgets/configurationOption/ConfigurationOptionWidget.h"
30 #include "../modules/widgets/contentBlockingInformation/ContentBlockingInformationWidget.h"
31 #include "../modules/widgets/errorConsole/ErrorConsoleWidget.h"
32 #include "../modules/widgets/menuButton/MenuButtonWidget.h"
33 #include "../modules/widgets/panelChooser/PanelChooserWidget.h"
34 #include "../modules/widgets/progressInformation/ProgressInformationWidget.h"
35 #include "../modules/widgets/privateWindowIndicator/PrivateWindowIndicatorWidget.h"
36 #include "../modules/widgets/search/SearchWidget.h"
37 #include "../modules/widgets/statusMessage/StatusMessageWidget.h"
38 #include "../modules/widgets/transfers/TransfersWidget.h"
39 #include "../modules/widgets/zoom/ZoomWidget.h"
40 #include "../modules/windows/addons/AddonsContentsWidget.h"
41 #include "../modules/windows/bookmarks/BookmarksContentsWidget.h"
42 #include "../modules/windows/cache/CacheContentsWidget.h"
43 #include "../modules/windows/configuration/ConfigurationContentsWidget.h"
44 #include "../modules/windows/cookies/CookiesContentsWidget.h"
45 #include "../modules/windows/feeds/FeedsContentsWidget.h"
46 #include "../modules/windows/history/HistoryContentsWidget.h"
47 #include "../modules/windows/links/LinksContentsWidget.h"
48 #include "../modules/windows/notes/NotesContentsWidget.h"
49 #include "../modules/windows/pageInformation/PageInformationContentsWidget.h"
50 #include "../modules/windows/passwords/PasswordsContentsWidget.h"
51 #include "../modules/windows/preferences/PreferencesContentsWidget.h"
52 #include "../modules/windows/tabHistory/TabHistoryContentsWidget.h"
53 #include "../modules/windows/transfers/TransfersContentsWidget.h"
54 #include "../modules/windows/web/WebContentsWidget.h"
55 #include "../modules/windows/windows/WindowsContentsWidget.h"
56 
57 #include <QtWidgets/QSizeGrip>
58 
59 namespace Otter
60 {
61 
62 namespace WidgetFactory
63 {
64 
createToolBar(int identifier,Window * window,QWidget * parent)65 ToolBarWidget* createToolBar(int identifier, Window *window, QWidget *parent)
66 {
67 	if (identifier == ToolBarsManager::TabBar)
68 	{
69 		return new TabBarToolBarWidget(identifier, window, parent);
70 	}
71 
72 	return new ToolBarWidget(identifier, window, parent);
73 }
74 
createToolBarItem(const ToolBarsManager::ToolBarDefinition::Entry & definition,Window * window,QWidget * parent)75 QWidget* createToolBarItem(const ToolBarsManager::ToolBarDefinition::Entry &definition, Window *window, QWidget *parent)
76 {
77 	if (definition.action == QLatin1String("spacer"))
78 	{
79 		QWidget *spacer(new QWidget(parent));
80 		spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
81 
82 		return spacer;
83 	}
84 
85 	if (!definition.entries.isEmpty())
86 	{
87 		return new ToolButtonWidget(definition, parent);
88 	}
89 
90 	if (definition.action == QLatin1String("AddressWidget"))
91 	{
92 		return new AddressWidget(window, parent);
93 	}
94 
95 	if (definition.action == QLatin1String("ConfigurationOptionWidget"))
96 	{
97 		return new ConfigurationOptionWidget(window, definition, parent);
98 	}
99 
100 	if (definition.action == QLatin1String("ErrorConsoleWidget"))
101 	{
102 		return new ErrorConsoleWidget(parent);
103 	}
104 
105 	if (definition.action == QLatin1String("ContentBlockingInformationWidget"))
106 	{
107 		return new ContentBlockingInformationWidget(window, definition, parent);
108 	}
109 
110 	if (definition.action == QLatin1String("MenuButtonWidget"))
111 	{
112 		return new MenuButtonWidget(definition, parent);
113 	}
114 
115 	if (definition.action == QLatin1String("PanelChooserWidget"))
116 	{
117 		return new PanelChooserWidget(definition, parent);
118 	}
119 
120 	if (definition.action == QLatin1String("PrivateWindowIndicatorWidget"))
121 	{
122 		return new PrivateWindowIndicatorWidget(definition, parent);
123 	}
124 
125 	if (definition.action == QLatin1String("ProgressInformationDocumentProgressWidget") || definition.action == QLatin1String("ProgressInformationTotalProgressWidget") || definition.action == QLatin1String("ProgressInformationTotalSizeWidget") || definition.action == QLatin1String("ProgressInformationElementsWidget") || definition.action == QLatin1String("ProgressInformationSpeedWidget") || definition.action == QLatin1String("ProgressInformationElapsedTimeWidget") || definition.action == QLatin1String("ProgressInformationMessageWidget"))
126 	{
127 		return new ProgressInformationWidget(window, definition, parent);
128 	}
129 
130 	if (definition.action == QLatin1String("SearchWidget"))
131 	{
132 		SearchWidget *searchWidget(new SearchWidget(window, parent));
133 		searchWidget->setOptions(definition.options);
134 
135 		return searchWidget;
136 	}
137 
138 	if (definition.action == QLatin1String("SizeGripWidget"))
139 	{
140 		return new QSizeGrip(parent);
141 	}
142 
143 	if (definition.action == QLatin1String("StatusMessageWidget"))
144 	{
145 		return new StatusMessageWidget(parent);
146 	}
147 
148 	if (definition.action == QLatin1String("TransfersWidget"))
149 	{
150 		return new TransfersWidget(definition, parent);
151 	}
152 
153 	if (definition.action == QLatin1String("TabBarWidget"))
154 	{
155 		return new TabBarWidget(parent);
156 	}
157 
158 	if (definition.action == QLatin1String("ZoomWidget"))
159 	{
160 		return new ZoomWidget(window, parent);
161 	}
162 
163 	if (definition.action.startsWith(QLatin1String("bookmarks:")))
164 	{
165 		BookmarksModel::Bookmark *bookmark(definition.action.startsWith(QLatin1String("bookmarks:/")) ? BookmarksManager::getModel()->getBookmarkByPath(definition.action.mid(11)) : BookmarksManager::getBookmark(definition.action.mid(10).toULongLong()));
166 
167 		if (bookmark)
168 		{
169 			return new BookmarkWidget(bookmark, definition, parent);
170 		}
171 	}
172 
173 	if (definition.action.endsWith(QLatin1String("Action")))
174 	{
175 		const int identifier(ActionsManager::getActionIdentifier(definition.action.left(definition.action.length() - 6)));
176 
177 		if (identifier >= 0)
178 		{
179 			ActionWidget *actionWidget(nullptr);
180 
181 			if (identifier == ActionsManager::GoBackAction || identifier == ActionsManager::GoForwardAction)
182 			{
183 				actionWidget = new NavigationActionWidget(window, definition, parent);
184 			}
185 			else
186 			{
187 				actionWidget = new ActionWidget(identifier, window, definition, parent);
188 			}
189 
190 			actionWidget->setOptions(definition.options);
191 
192 			return actionWidget;
193 		}
194 	}
195 
196 	if (definition.action.endsWith(QLatin1String("Menu")))
197 	{
198 		return new ToolButtonWidget(definition, parent);
199 	}
200 
201 	return nullptr;
202 }
203 
createContentsWidget(const QString & identifier,const QVariantMap & parameters,Window * window,QWidget * parent)204 ContentsWidget* createContentsWidget(const QString &identifier, const QVariantMap &parameters, Window *window, QWidget *parent)
205 {
206 	if (identifier == QLatin1String("addons"))
207 	{
208 		return new AddonsContentsWidget(parameters, window, parent);
209 	}
210 
211 	if (identifier == QLatin1String("bookmarks"))
212 	{
213 		return new BookmarksContentsWidget(parameters, window, parent);
214 	}
215 
216 	if (identifier == QLatin1String("cache"))
217 	{
218 		return new CacheContentsWidget(parameters, window, parent);
219 	}
220 
221 	if (identifier == QLatin1String("config"))
222 	{
223 		return new ConfigurationContentsWidget(parameters, window, parent);
224 	}
225 
226 	if (identifier == QLatin1String("cookies"))
227 	{
228 		return new CookiesContentsWidget(parameters, window, parent);
229 	}
230 
231 	if (identifier == QLatin1String("feeds"))
232 	{
233 		return new FeedsContentsWidget(parameters, parent);
234 	}
235 
236 	if (identifier == QLatin1String("history"))
237 	{
238 		return new HistoryContentsWidget(parameters, window, parent);
239 	}
240 
241 	if (identifier == QLatin1String("notes"))
242 	{
243 		return new NotesContentsWidget(parameters, window, parent);
244 	}
245 
246 	if (identifier == QLatin1String("passwords"))
247 	{
248 		return new PasswordsContentsWidget(parameters, window, parent);
249 	}
250 
251 	if (identifier == QLatin1String("preferences"))
252 	{
253 		return new PreferencesContentsWidget(parameters, window, parent);
254 	}
255 
256 	if (identifier == QLatin1String("transfers"))
257 	{
258 		return new TransfersContentsWidget(parameters, window, parent);
259 	}
260 
261 	if (identifier == QLatin1String("windows"))
262 	{
263 		return new WindowsContentsWidget(parameters, window, parent);
264 	}
265 
266 	return nullptr;
267 }
268 
createSidebarPanel(const QString & identifier,int sidebar,MainWindow * mainWindow,QWidget * parent)269 ContentsWidget* createSidebarPanel(const QString &identifier, int sidebar, MainWindow *mainWindow, QWidget *parent)
270 {
271 	QVariantMap parameters({{QLatin1String("sidebar"), sidebar}});
272 
273 	if (identifier == QLatin1String("links"))
274 	{
275 		return new LinksContentsWidget(parameters, parent);
276 	}
277 
278 	if (identifier == QLatin1String("pageInformation"))
279 	{
280 		return new PageInformationContentsWidget(parameters, parent);
281 	}
282 
283 	if (identifier == QLatin1String("tabHistory"))
284 	{
285 		return new TabHistoryContentsWidget(parameters, parent);
286 	}
287 
288 	if (identifier.startsWith(QLatin1String("web:")))
289 	{
290 		if (!mainWindow || mainWindow->isPrivate())
291 		{
292 			parameters[QLatin1String("hints")] = SessionsManager::PrivateOpen;
293 		}
294 
295 		WebContentsWidget *webWidget(new WebContentsWidget(parameters, {}, nullptr, nullptr, nullptr));
296 		webWidget->setUrl(identifier.section(QLatin1Char(':'), 1, -1), false);
297 
298 		return webWidget;
299 	}
300 
301 	if (!AddonsManager::getSpecialPage(identifier).types.testFlag(AddonsManager::SpecialPageInformation::SidebarPanelType))
302 	{
303 		return nullptr;
304 	}
305 
306 	return createContentsWidget(identifier, parameters, nullptr, parent);
307 }
308 
309 }
310 
311 }
312