-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
163 lines (115 loc) · 4.91 KB
/
Copy pathmain.cpp
File metadata and controls
163 lines (115 loc) · 4.91 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
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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QCommandLineParser>
#include <QDir>
#include <QMediaFormat>
#include <QMimeType>
#include <QGuiApplication>
#include <algorithm>
#include <QQmlContext>
#include "subtitleextractor.h"
#include "subtitlefinder.h"
//to set media.role make sure app get foucs and attention from os
#include <QProcessEnvironment>
#include <QGuiApplication>
#include "backend.h"
#include "settingsmanager.h"
#include "commandhandler.h"
//to pass session to backend
#include <QDBusConnection>
#include <QDBusError>
#include <QThread>
#include "logger.h"
#include "getwordtranslate.h"
using namespace Qt::Literals::StringLiterals;
struct NameFilters
{
QStringList filters;
int preferred = 0;
};
static NameFilters nameFilters()
{
QStringList result;
QString preferredFilter;
const auto formats = QMediaFormat().supportedFileFormats(QMediaFormat::Decode);
for (qsizetype m = 0, size = formats.size(); m < size; ++m) {
const auto format = formats.at(m);
QMediaFormat mediaFormat(format);
const QMimeType mimeType = mediaFormat.mimeType();
if (mimeType.isValid()) {
QString filter = QMediaFormat::fileFormatDescription(format) + " ("_L1;
const auto suffixes = mimeType.suffixes();
for (qsizetype i = 0, size = suffixes.size(); i < size; ++i) {
if (i)
filter += u' ';
filter += "*."_L1 + suffixes.at(i);
}
filter += u')';
result.append(filter);
if (mimeType.name() == "video/mp4"_L1)
preferredFilter = filter;
}
}
std::sort(result.begin(), result.end());
const int preferred = preferredFilter.isEmpty() ? 0 : int(result.indexOf(preferredFilter));
return { result, preferred };
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QThread::currentThread()->setObjectName("main thread");
// Set the media role to "video"
qputenv("PULSE_PROP", "media.role=video");
QCoreApplication::setApplicationName("moveV");
QCoreApplication::setOrganizationName("QtProject");
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main", "Qt Quick MediaPlayer Example"));
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("url", QCoreApplication::translate("main", "The URL(s) to open."));
parser.process(app);
//--------------------- logger ---------------------
QString logDir = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
QDir().mkpath(logDir);
QString logPath = logDir + "/app.log";
qInfo() << " logger filepath = " << logPath;
// Logger::install(logPath);
qmlRegisterType<SubtitleExtractor>("CustomMedia", 1, 0, "SubtitleExtractor");
qmlRegisterType<SubtitleFinder>("SubtitleFinder", 1, 0, "SubtitleFinder");
QQmlApplicationEngine engine;
// QThread* backendThread = new QThread;
// backendThread->setObjectName("backend thread");
SettingsManager settings;
Backend* backend = new Backend(&settings,&app,&app);
// backend->moveToThread(backendThread);
// QObject::connect(backendThread, &QThread::started, backend, &Backend::init);
GetWordTranslate getWordTranslate;
engine.rootContext()->setContextProperty("backend", backend);
engine.rootContext()->setContextProperty("settings", &settings);
engine.rootContext()->setContextProperty("translateWord", &getWordTranslate);
qmlRegisterUncreatableType<CommandHandler>("MyCommands", 1, 0, "Command", "Enums only");
QObject::connect(&engine, &QQmlApplicationEngine::quit, &app, &QGuiApplication::quit);
QUrl source;
if (!parser.positionalArguments().isEmpty())
source = QUrl::fromUserInput(parser.positionalArguments().at(0), QDir::currentPath());
const auto filters = nameFilters();
QVariantMap initialProperties{
{"source", source},
{"nameFilters", filters.filters},
{"selectedNameFilter", filters.preferred}
};
engine.setInitialProperties(initialProperties);
engine.loadFromModule("MediaPlayer", "Main");
if (engine.rootObjects().isEmpty())
return -1;
QObject *rootObject = engine.rootObjects().first();
//pass engine's root Object to backend because we need to call QML functions by c++
//and mpris needs this rootObject
backend->setRootObject(rootObject);
//also handle mpris (bluetooth/keyboard media buttons/os media buttons,os notification dialog)
backend->initMpris();
return app.exec();
}