/
Manitou.cpp
234 lines (196 loc) · 6.74 KB
/
Manitou.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "Manitou.h"
#include "ui/BrowserWindow.h"
#include "ui/BrowserTab.h"
#include <QtCore/QDir>
#include <QtCore/QSettings>
#include <QtCore/QTimer>
#include <QtGui/QAction>
#include <QtGui/QDesktopServices>
#include <QtWebKit/QWebSettings>
#ifdef Q_WS_MAC
void qt_mac_set_dock_menu(QMenu *menu);
#endif
Manitou::Manitou(int &argc, char **argv)
: QApplication(argc, argv), windowsSequence(1), tabsSequence(1), dockMenu(0)
{
initialize();
#ifdef Q_WS_MAC
setQuitOnLastWindowClosed(false);
#endif
connect(this, SIGNAL(aboutToQuit()), SLOT(destroy()));
QTimer::singleShot(0, this, SLOT(postInitialize()));
}
Manitou::~Manitou()
{
if (dockMenu)
delete dockMenu;
}
void Manitou::initialize()
{
QCoreApplication::setApplicationName("Manitou");
QCoreApplication::setApplicationVersion("0.1.0");
QCoreApplication::setOrganizationName("Internet3D");
QCoreApplication::setOrganizationDomain("internet3d.rdc.cz");
// init database
database.initialize();
history = new History(this);
searchHistory = new SearchHistory(this);
// actions initialization
createActions();
// initialize Web icon ("favicon") cache database
QString directory = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
QDir dir;
if (!directory.isEmpty()) {
if (!dir.exists(directory)) {
if (!dir.mkpath(directory)) {
directory = "";
}
}
}
else {
directory = dir.homePath() + "/." + QCoreApplication::applicationName();
if (!dir.exists(directory)) {
if (!dir.mkpath(directory)) {
directory = "";
}
}
}
if (!directory.isEmpty()) {
QWebSettings::setIconDatabasePath(directory);
}
#ifdef Linux
wiiRemote = new WiiRemote();
#endif
}
void Manitou::createActions()
{
QAction *action = new QAction(tr("New Window"), this);
action->setShortcut(QKeySequence::New);
actionsMap.insert(Manitou::ActionNewWindow, action);
connect(action, SIGNAL(triggered()), SLOT(onOpenWindow()));
action = new QAction(tr("New Tab"), this);
action->setShortcut(QKeySequence::AddTab/*(Qt::CTRL + Qt::Key_T)*/);
actionsMap.insert(Manitou::ActionNewTab, action);
connect(action, SIGNAL(triggered()), SLOT(onOpenTab()));
action = new QAction(tr("Close Window"), this);
action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));
actionsMap.insert(Manitou::ActionCloseWindow, action);
connect(action, SIGNAL(triggered()), SLOT(closeWindow()));
action = new QAction(tr("Close Tab"), this);
actionsMap.insert(Manitou::ActionCloseTab, action);
connect(action, SIGNAL(triggered()), SLOT(closeTab()));
#ifdef Q_WS_MAC
// initialize dock menu (specific for Mac OS X)
dockMenu = new QMenu();
dockMenu->addAction(getAction(Manitou::ActionNewWindow));
qt_mac_set_dock_menu(dockMenu);
#endif
}
void Manitou::postInitialize()
{
// load WebKit settings
QSettings settings;
QWebSettings *globalSettings = QWebSettings::globalSettings();
settings.beginGroup("WebKit");
globalSettings->setAttribute(QWebSettings::AutoLoadImages, settings.value("AutoLoadImages", true).toBool());
globalSettings->setAttribute(QWebSettings::DeveloperExtrasEnabled, settings.value("DevelopersExtras", true).toBool());
globalSettings->setAttribute(QWebSettings::PluginsEnabled, settings.value("Plugins", true).toBool());
globalSettings->setAttribute(QWebSettings::LocalStorageEnabled, settings.value("LocalStorage", true).toBool());
globalSettings->setAttribute(QWebSettings::LocalStorageDatabaseEnabled, settings.value("LocalDatabase", true).toBool());
settings.endGroup();
}
void Manitou::destroy()
{
QSettings settings;
QWebSettings *globalSettings = QWebSettings::globalSettings();
settings.beginGroup("WebKit");
settings.setValue("AutoLoadImages", globalSettings->testAttribute(QWebSettings::AutoLoadImages));
settings.setValue("DevelopersExtras", globalSettings->testAttribute(QWebSettings::DeveloperExtrasEnabled));
settings.setValue("Plugins", globalSettings->testAttribute(QWebSettings::PluginsEnabled));
settings.setValue("LocalStorage", globalSettings->testAttribute(QWebSettings::LocalStorageEnabled));
settings.setValue("LocalDatabase", globalSettings->testAttribute(QWebSettings::LocalStorageDatabaseEnabled));
settings.endGroup();
delete searchHistory;
database.destroy();
delete wiiRemote;
}
bool Manitou::event(QEvent *event)
{
#ifdef Q_WS_MAC
// TODO: nejspise se bude resit pomoci odchytavani eventu primo od Cocoa frameworku...
// ostatnich platforem se to netyka.
// if (event->type() == QEvent::ApplicationActivate) {
// if (windowsMap.empty()) {
// BrowserWindow *window = createNewWindow();
// window->showMaximized();
// }
// }
#endif
return QApplication::event(event);
}
BrowserWindow* Manitou::createNewWindow(const QUrl &url)
{
BrowserWindow *window = new BrowserWindow(windowsSequence);
windowsMap.insert(windowsSequence++, window);
connect(window, SIGNAL(windowClosed(unsigned long)), SLOT(onWindowClosed(unsigned long)));
BrowserTab *tab = createNewTab(url);
window->addTab(tab);
BrowserWindow *activeWindow = (BrowserWindow*) Manitou::activeWindow();
if (activeWindow) {
window->setWindowState(activeWindow->windowState());
}
return window;
}
BrowserTab* Manitou::createNewTab(const QUrl &url)
{
BrowserTab *tab = new BrowserTab(tabsSequence);
tabsMap.insert(tabsSequence++, tab);
connect(tab, SIGNAL(tabClosed(unsigned long)), SLOT(onTabClosed(unsigned long)));
if (!url.isEmpty()) {
tab->openUrl(url);
}
else {
tab->openUrl(QUrl("qrc:/html/index.html", QUrl::TolerantMode));
}
return tab;
}
void Manitou::onOpenWindow()
{
BrowserWindow *window = createNewWindow();
window->show();
}
void Manitou::onOpenTab()
{
BrowserTab *tab = createNewTab();
BrowserWindow *window = (BrowserWindow*) activeWindow();
window->addTab(tab);
window->setCurrentTab(tab);
}
void Manitou::onWindowClosed(unsigned long id)
{
BrowserWindow *window = windowsMap.value(id, 0);
if (window) {
windowsMap.remove(id);
window->deleteLater();
}
}
void Manitou::onTabClosed(unsigned long id)
{
BrowserTab *tab = tabsMap.value(id, 0);
if (tab) {
tabsMap.remove(id);
tab->deleteLater();
}
}
void Manitou::closeWindow()
{
BrowserWindow *window = (BrowserWindow*) Manitou::activeWindow();
if (window)
window->close();
}
void Manitou::closeTab()
{
BrowserWindow *window = (BrowserWindow*) Manitou::activeWindow();
if (window)
window->closeCurrentTab();
}