-
Notifications
You must be signed in to change notification settings - Fork 5
/
application.cpp
314 lines (251 loc) · 7.83 KB
/
application.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
Copyright 2016 Adam Reichold
This file is part of QMediathekView.
QMediathekView is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
QMediathekView is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QMediathekView. If not, see <http://www.gnu.org/licenses/>.
*/
#include "application.h"
#include <QDesktopServices>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QMessageBox>
#include <QProcess>
#include <QProxyStyle>
#include <QTimer>
#include <QUrl>
#include "settings.h"
#include "database.h"
#include "model.h"
#include "mainwindow.h"
#include "downloaddialog.h"
namespace QMediathekView
{
namespace
{
const auto projectName = QStringLiteral("QMediathekView");
class ProxyStyle : public QProxyStyle
{
public:
int styleHint(StyleHint hint, const QStyleOption* option, const QWidget* widget, QStyleHintReturn* returnData) const override
{
if (hint == QStyle::SH_ItemView_ActivateItemOnSingleClick)
{
return 0;
}
return QProxyStyle::styleHint(hint, option, widget, returnData);
}
};
bool startDetached(QString command)
{
#if QT_VERSION < QT_VERSION_CHECK(5,15,0)
return QProcess::startDetached(command);
#else
auto arguments = QProcess::splitCommand(command);
auto program = arguments.takeFirst();
return QProcess::startDetached(program, arguments);
#endif // QT_VERSION
}
} // anonymous
Application::Application(int& argc, char** argv, bool headless)
: QApplication(argc, argv)
, m_settings(new Settings(this))
, m_database(new Database(*m_settings, this))
, m_model(new Model(*m_database, this))
, m_networkManager(new QNetworkAccessManager(this))
, m_mainWindow(!headless ? new MainWindow(*m_settings, *m_model, *this) : nullptr)
{
setWindowIcon(QIcon::fromTheme(projectName));
setStyle(new ProxyStyle);
connect(m_database, &Database::updated, m_model, &Model::update);
connect(m_database, &Database::updated, this, &Application::completedDatabaseUpdate);
connect(m_database, &Database::failedToUpdate, this, &Application::failedToUpdateDatabase);
if (m_mainWindow != nullptr)
{
connect(this, &Application::startedDatabaseUpdate, m_mainWindow, &MainWindow::showStartedDatabaseUpdate);
connect(this, &Application::completedDatabaseUpdate, m_mainWindow, &MainWindow::showCompletedDatabaseUpdate);
connect(this, &Application::failedToUpdateDatabase, m_mainWindow, &MainWindow::showDatabaseUpdateFailure);
}
else
{
connect(this, &Application::startedDatabaseUpdate, this, &Application::logStartedDatabaseUpdate);
connect(this, &Application::completedDatabaseUpdate, this, &Application::logCompletedDatabaseUpdate);
connect(this, &Application::failedToUpdateDatabase, this, &Application::logDatabaseUpdateFailure);
}
}
Application::~Application() = default;
int Application::exec()
{
QTimer::singleShot(0, this, &Application::checkUpdateDatabase);
if (m_mainWindow != nullptr)
{
m_mainWindow->setAttribute(Qt::WA_DeleteOnClose);
m_mainWindow->show();
}
return QApplication::exec();
}
void Application::playPreferred(const QModelIndex& index) const
{
startPlay(preferredUrl(index));
}
void Application::playDefault(const QModelIndex& index) const
{
startPlay(m_model->url(index));
}
void Application::playSmall(const QModelIndex& index) const
{
startPlay(m_model->urlSmall(index));
}
void Application::playLarge(const QModelIndex& index) const
{
startPlay(m_model->urlLarge(index));
}
void Application::downloadPreferred(const QModelIndex& index) const
{
startDownload(m_model->title(index), preferredUrl(index));
}
void Application::downloadDefault(const QModelIndex& index) const
{
startDownload(m_model->title(index), m_model->url(index));
}
void Application::downloadSmall(const QModelIndex& index) const
{
startDownload(m_model->title(index), m_model->urlSmall(index));
}
void Application::downloadLarge(const QModelIndex& index) const
{
startDownload(m_model->title(index), m_model->urlLarge(index));
}
void Application::checkUpdateDatabase()
{
const auto updateAfter = m_settings->databaseUpdateAfterHours();
const auto updatedOn = m_settings->databaseUpdatedOn();
const auto updatedBefore = updatedOn.secsTo(QDateTime::currentDateTime()) / 60 / 60;
if (!updatedOn.isValid() || updateAfter < updatedBefore)
{
updateDatabase();
}
else if (m_mainWindow == nullptr)
{
quit();
}
}
void Application::updateDatabase()
{
emit startedDatabaseUpdate();
const auto updatedOn = m_settings->databaseUpdatedOn();
const auto fullUpdateOn = QDateTime(QDate::currentDate(), QTime(9, 0));
if (!updatedOn.isValid() || updatedOn < fullUpdateOn)
{
m_database->fullUpdate(m_settings->fullListUrl());
}
else
{
m_database->partialUpdate(m_settings->partialListUrl());
}
}
QString Application::preferredUrl(const QModelIndex& index) const
{
auto firstUrl = &Model::url;
auto secondUrl = &Model::urlSmall;
auto thirdUrl = &Model::urlLarge;
switch (m_settings->preferredUrl())
{
default:
case Url::Default:
break;
case Url::Small:
firstUrl = &Model::urlSmall;
secondUrl = &Model::url;
thirdUrl = &Model::urlLarge;
break;
case Url::Large:
firstUrl = &Model::urlLarge;
secondUrl = &Model::url;
thirdUrl = &Model::urlSmall;
break;
}
auto url = (m_model->*firstUrl)(index);
if (url.isEmpty())
{
url = (m_model->*secondUrl)(index);
}
if (url.isEmpty())
{
url = (m_model->*thirdUrl)(index);
}
return url;
}
void Application::startPlay(const QString& url) const
{
const auto command = m_settings->playCommand();
if (!command.isEmpty())
{
if (!startDetached(command.arg(url)))
{
QMessageBox::critical(m_mainWindow, tr("Critical"), tr("Failed to execute play command."));
}
}
else
{
QDesktopServices::openUrl(url);
}
}
void Application::startDownload(const QString& title, const QString& url) const
{
const auto command = m_settings->downloadCommand();
if (!command.isEmpty())
{
if (!startDetached(command.arg(url)))
{
QMessageBox::critical(m_mainWindow, tr("Critical"), tr("Failed to execute download command."));
}
}
else
{
const auto dialog = new DownloadDialog(
*m_settings, m_networkManager,
title, url);
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->show();
}
}
void Application::logStartedDatabaseUpdate()
{
qInfo() << tr("Started database update...");
}
void Application::logCompletedDatabaseUpdate()
{
qInfo() << tr("Successfully updated database.");
quit();
}
void Application::logDatabaseUpdateFailure(const QString& error)
{
qWarning() << tr("Failed to update database: %1").arg(error);
quit();
}
} // QMediathekView
int main(int argc, char** argv)
{
using namespace QMediathekView;
QApplication::setOrganizationName(projectName);
QApplication::setApplicationName(projectName);
bool headless = false;
for (int argi = 1; argi < argc; ++argi)
{
const char* const arg = argv[argi];
if (strcmp(arg, "--headless") == 0)
{
headless = true;
}
}
return Application(argc, argv, headless).exec();
}