-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
181 lines (152 loc) · 5.85 KB
/
mainwindow.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
#include <QtGui>
#include <QtWebKit>
#include <QSslConfiguration>
#include <QSslSocket>
#include <QWebView>
#include <QWebFrame>
#include <QMessageBox>
#include <QApplication>
#include "mainwindow.h"
MainWindow::MainWindow(const QUrl& url, QApplication& _app) {
progress = 0;
app = &_app;
createTrayIcon();
trayIcon->show();
setUnreadCount(-1);
QSettings settings;
QNetworkProxyFactory::setUseSystemConfiguration(true);
QWebSettings::enablePersistentStorage();
QWebSettings* s = QWebSettings::globalSettings();
// https://developer.qt.nokia.com/doc/qt-4.8/qwebsettings.html#WebAttribute-enum
s->setAttribute(QWebSettings::LocalStorageEnabled,true);
s->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,true);
s->setAttribute(QWebSettings::DeveloperExtrasEnabled,true);
s->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled,true);
s->setAttribute(QWebSettings::JavascriptCanAccessClipboard, true);
//s->setAttribute(QWebSettings::TiledBackingStoreEnabled, true);
s->setAttribute(QWebSettings::SiteSpecificQuirksEnabled, false);
qDebug("%s",qVersion());
webView = new QWebView(this);
bridge = new BGBridge(this);
webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
//webView->setContextMenuPolicy(Qt::NoContextMenu);
webView->load(url);
connect(webView, SIGNAL(loadFinished(bool)), SLOT(adjustLocation()));
connect(webView, SIGNAL(urlChanged(QUrl)), this, SLOT(adjustLocation(QUrl)));
connect(webView, SIGNAL(titleChanged(QString)), SLOT(adjustTitle()));
connect(webView, SIGNAL(loadProgress(int)), SLOT(setProgress(int)));
connect(webView, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool)));
connect(webView->page(), SIGNAL(databaseQuotaExceeded(QWebFrame*,QString)),this,SLOT(increaseDatabaseQuota(QWebFrame*,QString)));
connect(webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(javaScriptWindowObjectCleared()));
connect(webView->page()->mainFrame(), SIGNAL(urlChanged(QUrl)), this, SLOT(adjustLocation(QUrl)));
connect(webView, SIGNAL(linkClicked(QUrl)), this, SLOT(linkClicked(QUrl)));
setCentralWidget(webView);
setUnifiedTitleAndToolBarOnMac(true);
javaScriptWindowObjectCleared();
QPoint pos = settings.value("pos", QPoint(100, 100)).toPoint();
QSize size = settings.value("size", QSize(800, 600)).toSize();
resize(size);
move(pos);
if (!QSslSocket::supportsSsl()) {
qDebug() << "ERROR: NO SSL SUPPORT!";
QMessageBox::critical(this, "No SSL Support",
"You don't have the SSL run-time libraries installed - "
"you must install these before you can use Floim"
);
}
}
void MainWindow::createTrayIcon() {
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(QIcon(":/floim.png"));
trayIcon->setToolTip("Floim");
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason) {
raise();
}
void MainWindow::adjustLocation(QUrl url) {
QWebFrame *frame = webView->page()->mainFrame();
QString href = frame->evaluateJavaScript("window.location.href").toString();
qDebug() << href;
}
void MainWindow::linkClicked(QUrl url) {
QDesktopServices::openUrl(url);
}
void MainWindow::adjustLocation() {
QUrl url = webView->url();
adjustLocation(url);
}
void MainWindow::adjustTitle() {
adjustLocation();
if (progress <= 0 || progress >= 100) {
setWindowTitle(webView->title());
} else {
setWindowTitle(QString("%1 (%2%)").arg(webView->title()).arg(progress));
}
}
void MainWindow::setProgress(int p) {
progress = p;
adjustTitle();
}
void MainWindow::finishLoading(bool) {
progress = 100;
adjustTitle();
}
void MainWindow::increaseDatabaseQuota(QWebFrame* frame,QString) {
QWebSecurityOrigin origin = frame->securityOrigin();
origin.setDatabaseQuota(100 * 1024 * 1024);
}
QIcon MainWindow::iconWithUnread(QString unread, double scale) {
QImage base(QString(":/floim.png"));
qDebug()<<unread;
if (unread != "") {
QPainter painter(&base);
painter.setPen(Qt::white);
painter.setBrush(QBrush(Qt::red));
int r = base.rect().width()*scale;
QRect unreadBox(base.rect().width()-r,0,r,r);
painter.drawEllipse(unreadBox);
painter.setFont(QFont("Arial Bold", (int)ceil(r*0.7)));
painter.drawText(unreadBox, Qt::AlignCenter, unread);
}
QIcon icon = QIcon(QPixmap::fromImage(base));
return icon;
}
void MainWindow::setUnreadCount(int count) {
QString unread="";
if (count < 0) {
unread = QString("?");
} else if (count > 0) {
unread = QString("%1").arg(count);
}
QIcon icon = this->iconWithUnread(unread,1/3.0);
setWindowIcon(icon);
app->setWindowIcon(icon);
icon = this->iconWithUnread(unread,3/4.0);
trayIcon->setIcon(icon);
}
void MainWindow::showMessage(QString icon,QString title,QString msg) {
trayIcon->showMessage(title,msg);
Q_UNUSED(icon);
}
void MainWindow::openUrl(QString url) {
qDebug() << url;
if (url.startsWith("/")) {
url = webView->page()->mainFrame()->evaluateJavaScript("window.location.protocol+'//'+window.location.host").toString()+url;
}
if (url.startsWith("http://") || url.startsWith("https://")) {
QDesktopServices::openUrl(QUrl(url));
}
}
void MainWindow::javaScriptWindowObjectCleared() {
QWebFrame *frame = webView->page()->mainFrame();
frame->addToJavaScriptWindowObject("QTApp", bridge);
frame->evaluateJavaScript("window.open = QTApp.open;");
}
void MainWindow::closeEvent(QCloseEvent *event)
{
QSettings settings;
settings.setValue("pos", pos());
settings.setValue("size", size());
QWidget::closeEvent(event);
}