-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
86 lines (76 loc) · 3.29 KB
/
main.cpp
File metadata and controls
86 lines (76 loc) · 3.29 KB
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
#include <QtCore/QUrl>
#include <QtCore/QCommandLineOption>
#include <QtCore/QCommandLineParser>
#include <QGuiApplication>
#include <QStyleHints>
#include <QScreen>
#include <QQmlApplicationEngine>
#include <QtQml/QQmlContext>
#include <QtWebView/QtWebView>
#include <QtWidgets/QApplication>
#include <QQmlApplicationEngine>
#include <QIcon>
#include <QQuickWidget>
#include <QtWidgets/QSystemTrayIcon>
#include <QQmlContext>
Q_DECLARE_METATYPE(QSystemTrayIcon::ActivationReason)
// Workaround: As of Qt 5.4 QtQuick does not expose QUrl::fromUserInput.
class Utils : public QObject {
Q_OBJECT
public:
Utils(QObject* parent = 0) : QObject(parent) { }
Q_INVOKABLE static QUrl fromUserInput(const QString& userInput);
};
QUrl Utils::fromUserInput(const QString& userInput)
{
if (userInput.isEmpty())
return QUrl::fromUserInput("about:blank");
const QUrl result = QUrl::fromUserInput(userInput);
return result.isValid() ? result : QUrl::fromUserInput("about:blank");
}
#include "main.moc"
int main(int argc, char *argv[])
{
//! [0]
QGuiApplication app(argc, argv);
QtWebView::initialize();
//! [0]
QGuiApplication::setApplicationDisplayName(QCoreApplication::translate("main",
"WhatsApp"));
QCommandLineParser parser;
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
parser.setApplicationDescription(QGuiApplication::applicationDisplayName());
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("url", "The initial URL to open.");
QStringList arguments = app.arguments();
#ifdef Q_OS_WINRT
arguments.removeAt(1); // The launcher always passes in the -ServerName parameter, breaking the command line parser
#endif
parser.process(arguments);
const QString initialUrl = QStringLiteral("https://web.whatsapp.com/");
QQmlApplicationEngine engine;
QQmlContext *context = engine.rootContext();
qmlRegisterType<QSystemTrayIcon>("QSystemTrayIcon", 1, 0, "QSystemTrayIcon");
qRegisterMetaType<QSystemTrayIcon::ActivationReason>("ActivationReason");
context->setContextProperty(QStringLiteral("utils"), new Utils(&engine));
context->setContextProperty(QStringLiteral("initialUrl"),
Utils::fromUserInput(initialUrl));
QRect geometry = QGuiApplication::primaryScreen()->availableGeometry();
if (!QGuiApplication::styleHints()->showIsFullScreen()) {
const QSize size = geometry.size() * 4 / 5;
const QSize offset = (geometry.size() - size) / 2;
const QPoint pos = geometry.topLeft() + QPoint(offset.width(), offset.height());
geometry = QRect(pos, size);
}
context->setContextProperty(QStringLiteral("initialX"), geometry.x());
context->setContextProperty(QStringLiteral("initialY"), geometry.y());
context->setContextProperty(QStringLiteral("initialWidth"), geometry.width());
context->setContextProperty(QStringLiteral("initialHeight"), geometry.height());
engine.rootContext()->setContextProperty("iconTray", QIcon(":/images/whatsapp-logo-icone.png"));
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
app.setWindowIcon(QIcon(":/images/whatsapp-logo-icone.png"));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}