-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
443 lines (371 loc) · 14.9 KB
/
Copy pathMainWindow.cpp
File metadata and controls
443 lines (371 loc) · 14.9 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "MainWindow.h"
#include "DownloadItem.h"
#include "About.h"
#include "DownloadConfig.h"
#include "ServiceMaintainer.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
QWidget *centralWidget = new QWidget(this);
fullLayout = new QVBoxLayout(centralWidget);
layout = new QVBoxLayout();
linkLayout = new QHBoxLayout();
optionsLayout = new QHBoxLayout();
linkBox = new QLineEdit(centralWidget);
downloadButton = new QPushButton(centralWidget);
clearFinishedButton = new QPushButton(centralWidget);
getEngineButton = new QPushButton(centralWidget);
statusLabel = new QLabel(centralWidget);
titleLabel = new QLabel(centralWidget);
progressBar = new QProgressBar(centralWidget);
maintainer = new ServiceMaintainer(this);
chosenDirectory = QDir::homePath();
list = new QListWidget(centralWidget);
QAction *deleteAction = new QAction(list);
deleteAction->setShortcut(QKeySequence::Delete);
deleteAction->setShortcutContext(Qt::WidgetShortcut);
list->addAction(deleteAction);
// Supr key
connect(deleteAction, &QAction::triggered, this, [this]() {
QListWidgetItem *currentItem = list->currentItem();
if (!currentItem) return;
DownloadItem *di = qobject_cast<DownloadItem*>(list->itemWidget(currentItem));
if (di) {
// ¡Acá aprovechamos tu código!
// Esto frena la descarga (si está bajando)
// y hace el emit removeRequested() que la borra de la lista.
di->stopDownload();
}
});
bothButton = new QRadioButton(tr("Both"), centralWidget);
videoButton = new QRadioButton(tr("Video"), centralWidget);
audioButton = new QRadioButton(tr("Audio"), centralWidget);
/* MainWindow size */
this->resize(200, 200);
this->setMinimumWidth(462);
/* Persistent settings */
QSettings settings("MaximoPardo", "Phoca");
downloadLocation = settings.value("downloadLocation", QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)).toString();
savePlaylistInFolder = settings.value("savePlaylistInFolder", true).toBool();
saveThumbnail = settings.value("saveThumbnail", false).toBool();
firstLaunch = settings.value("firstLaunch", true).toBool();
lastEngineUpdate = settings.value("lastEngineUpdate", QDateTime::currentDateTime()).toDateTime();
QDateTime now = QDateTime::currentDateTime();
if (lastEngineUpdate.daysTo(now) >= 3) {
getServiceSlot();
}
/* Menu */
optionsMenu = new QMenu(tr("Options"), this);
aboutAction = new QAction(tr("About"), this);
buildMenu = new QMenu(tr("Choose yt-dlp version"), optionsMenu);
menuBar()->addMenu(optionsMenu);
optionsMenu->addMenu(buildMenu);
menuBar()->addAction(aboutAction);
chooseLocationAction = new QAction(tr("Change download location..."), this);
chooseNightlyAction = new QAction(tr("Use yt-dlp nightly (recommended)"), this);
chooseStableAction = new QAction(tr("Use yt-dlp stable"), this);
versionGroup = new QActionGroup(this);
savePlaylistInFolderAction = new QAction(tr("Save playlists in folder"), this);
saveThumbnailAction = new QAction(tr("Save thumbnail"), this);
savePlaylistInFolderAction->setCheckable(true);
savePlaylistInFolderAction->setChecked(savePlaylistInFolder);
saveThumbnailAction->setCheckable(true);
saveThumbnailAction->setChecked(saveThumbnail);
chooseNightlyAction->setCheckable(true);
chooseStableAction->setCheckable(true);
chooseNightlyAction->setChecked(true);
versionGroup->addAction(chooseNightlyAction);
versionGroup->addAction(chooseStableAction);
optionsMenu->addAction(chooseLocationAction);
optionsMenu->addAction(savePlaylistInFolderAction);
optionsMenu->addAction(saveThumbnailAction);
buildMenu->addAction(chooseNightlyAction);
buildMenu->addAction(chooseStableAction);
linkBox->setPlaceholderText(tr("Enter link..."));
downloadButton->setText(tr("Download"));
downloadButton->setEnabled(false);
clearFinishedButton->setText(tr("Clear finished"));
getEngineButton->setText(tr("Update yt-dlp"));
fullLayout->addLayout(layout);
fullLayout->addWidget(list);
layout->addLayout(linkLayout);
linkLayout->addWidget(linkBox);
layout->addLayout(optionsLayout);
linkLayout->addWidget(downloadButton);
layout->addWidget(titleLabel);
layout->addWidget(statusLabel);
layout->addWidget(progressBar);
QHBoxLayout *bottomLayout = new QHBoxLayout();
bottomLayout->addWidget(clearFinishedButton);
clearFinishedButton->setEnabled(false);
bottomLayout->addWidget(getEngineButton);
layout->addLayout(bottomLayout);
titleLabel->hide();
progressBar->hide();
optionsLayout->addWidget(bothButton);
optionsLayout->addWidget(videoButton);
optionsLayout->addWidget(audioButton);
qualityBox = new QComboBox(this);
qualityBox->setEditable(true);
qualityBox->setInsertPolicy(QComboBox::NoInsert);
qualityBox->addItems({tr("Best"), "2160p", "1440p", "1080p", "720p", "480p"});
optionsLayout->addWidget(qualityBox);
conversionBox = new QComboBox(this);
conversionBox->setEditable(true);
conversionBox->setInsertPolicy(QComboBox::NoInsert);
conversionBox->addItems({tr("Original"), ".mp4", ".mkv", ".webm"});
optionsLayout->addWidget(conversionBox);
QSpacerItem *spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
optionsLayout->addItem(spacer);
bothButton->setChecked(true);
this->setWindowTitle("Phoca");
setCentralWidget(centralWidget);
connect(linkBox, &QLineEdit::textChanged, this,
&MainWindow::setDownloadReadiness);
connect(qualityBox, &QComboBox::editTextChanged, this,
&MainWindow::setDownloadReadiness);
connect(conversionBox, &QComboBox::editTextChanged, this,
&MainWindow::setDownloadReadiness);
connect(getEngineButton, &QPushButton::clicked, this,
&MainWindow::getServiceSlot);
connect(maintainer, &ServiceMaintainer::started, this,
&MainWindow::engineDownloading);
connect(maintainer, &ServiceMaintainer::finished, this,
&MainWindow::engineDownloaded);
connect(chooseStableAction, &QAction::triggered, this,
&MainWindow::getServiceSlot);
connect(chooseNightlyAction, &QAction::triggered, this,
&MainWindow::getServiceSlot);
connect(chooseLocationAction, &QAction::triggered, this,
&MainWindow::changeLocation);
connect(aboutAction, &QAction::triggered, this,
&MainWindow::aboutPage);
connect(clearFinishedButton, &QPushButton::clicked, this,
&MainWindow::clearFinishedDownloads);
connect(linkBox, &QLineEdit::textChanged, this, [this](const QString &text) {
static int lastLength = 0;
int currentLength = text.length();
if (qAbs(currentLength - lastLength) > 1 && currentLength > 0) {
linkBox->setCursorPosition(0);
}
lastLength = currentLength;
});
connect(bothButton, &QPushButton::clicked, this,
&MainWindow::toggleQualityOptions);
connect(videoButton, &QPushButton::clicked, this,
&MainWindow::toggleQualityOptions);
connect(audioButton, &QPushButton::clicked, this,
&MainWindow::toggleQualityOptions);
connect(bothButton, &QRadioButton::clicked, this, [this]() {
conversionBox->clear();
conversionBox->addItems({tr("Original"), ".mp4", ".mkv", ".webm"});
});
connect(videoButton, &QRadioButton::clicked, this, [this]() {
conversionBox->clear();
conversionBox->addItems({tr("Original"), ".mp4", ".mkv", ".webm"});
});
connect(audioButton, &QRadioButton::clicked, this, [this]() {
conversionBox->clear();
conversionBox->addItems({tr("Original"), ".mp3", ".wav", ".flac", ".m4a"});
});
connect(savePlaylistInFolderAction, &QAction::triggered, this,
&MainWindow::changeSavePlaylistInFolder);
connect(saveThumbnailAction, &QAction::triggered, this,
&MainWindow::changeSaveThumbnail);
connect(downloadButton, &QPushButton::clicked, this,
&MainWindow::startDownload);
connect(linkBox, &QLineEdit::returnPressed,
downloadButton, &QPushButton::click);
//connect(list, &QListWidget::itemDoubleClicked, this, &MainWindow::openDirectory);
locationLabel = new QLabel(this);
locationLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
this->statusBar()->addWidget(locationLabel);
updateLocationLabel();
if (firstLaunch) {
QTimer::singleShot(500, [this]() {
getServiceSlot();
});
settings.setValue("firstLaunch", false);
}
}
void MainWindow::updateLocationLabel() {
const QString shown = QDir::toNativeSeparators(downloadLocation);
locationLabel->setText(tr("Download location: %1").arg(shown));
locationLabel->setToolTip(shown);
}
void MainWindow::engineDownloading() { this->statusBar()->showMessage(tr("[yt-dlp] Downloading...")); }
void MainWindow::engineDownloaded(int exit) {
switch (exit) {
case 0:
this->statusBar()->showMessage(tr("[yt-dlp] Downloaded successfully"), 5000);
break;
case 1:
this->statusBar()->showMessage(tr("[yt-dlp] Download failed: Network error"), 5000);
break;
case 2:
this->statusBar()->showMessage(tr("[yt-dlp] Download failed: Need permissions to write"), 5000);
break;
}
setDownloadReadiness();
}
void MainWindow::setDownloadReadiness() {
// search quality in qualityBox
bool validQuality = qualityBox->findText(qualityBox->currentText()) != -1;
bool validConversion = conversionBox->findText(conversionBox->currentText()) != -1;
if (!linkBox->text().isEmpty() && maintainer->exists() && validQuality && validConversion) {
downloadButton->setEnabled(true);
} else {
downloadButton->setEnabled(false);
}
}
void MainWindow::getServiceSlot() {
downloadButton->setEnabled(false);
bool nightly{chooseNightlyAction->isChecked()};
maintainer->getService(nightly);
QSettings settings("MaximoPardo", "Phoca");
QDateTime now = QDateTime::currentDateTime();
settings.setValue("lastEngineUpdate", now);
}
void MainWindow::changeLocation() {
const QString dir = QFileDialog::getExistingDirectory(
this, tr("Choose where to save files"), downloadLocation,
QFileDialog::ShowDirsOnly);
if (!dir.isEmpty()) {
downloadLocation = dir;
QSettings settings("MaximoPardo", "Phoca");
settings.setValue("downloadLocation", downloadLocation);
qDebug() << "Settings saved. Chosen folder:" << downloadLocation;
updateLocationLabel();
}
}
void MainWindow::startDownload() {
int format {0}; // both
if (videoButton->isChecked()) {
format = 1;
} else if (audioButton->isChecked()) {
format = 2;
}
QString link = linkBox->text();
bool playlist = false;
if (link.contains("list=") || link.contains("/playlist/") || link.contains("/album/")) {
QMessageBox msgBox(this);
msgBox.setIcon(QMessageBox::Question);
msgBox.setWindowTitle(tr("Playlist detected"));
if (saveThumbnail) {
msgBox.setText(tr("This link contains a playlist.\nDownload the whole list?\nThumbnails will not be saved"));
} else {
msgBox.setText(tr("This link contains a playlist.\nDownload the whole list?"));
}
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
msgBox.button(QMessageBox::Cancel)->hide();
int answer = msgBox.exec();
if (answer == QMessageBox::Yes) {
playlist = true;
} else if (answer == QMessageBox::No) {
playlist = false;
} else {
return;
}
}
bool parSaveThumbnail{saveThumbnail && !playlist};
QString quality;
QString conversion;
if (qualityBox->currentIndex() == 0) {
quality = "0";
} else {
quality = qualityBox->currentText();
}
if (conversionBox->currentIndex() == 0) {
conversion = "0";
} else {
conversion = conversionBox->currentText();
}
// Config struct
DownloadConfig config;
config.link = link;
config.downloadLocation = downloadLocation;
config.format = format;
config.quality = quality;
config.conversion = conversion;
config.playlist = playlist;
config.savePlaylistInFolder = savePlaylistInFolder;
config.saveThumbnail = parSaveThumbnail;
DownloadItem *newDownload = new DownloadItem(config, this);
QListWidgetItem *item = new QListWidgetItem();
connect(newDownload, &DownloadItem::removeRequested, [this, item]() {
delete item;
// Check for finished items
bool hasFinishedItems = false;
for (int i = 0; i < list->count(); ++i) {
DownloadItem *di = qobject_cast<DownloadItem*>(list->itemWidget(list->item(i)));
if (di && di->isFinished()) {
hasFinishedItems = true;
break;
}
}
clearFinishedButton->setEnabled(hasFinishedItems);
});
connect(newDownload, &DownloadItem::finishedSignal, this, &MainWindow::itemFinished);
item->setSizeHint(newDownload->sizeHint());
list->addItem(item);
list->setItemWidget(item, newDownload);
titleLabel->hide();
linkBox->clear();
}
void MainWindow::aboutPage() {
About aboutWindow(this);
aboutWindow.exec();
}
void MainWindow::toggleQualityOptions() {
if (!audioButton->isChecked()) {
qualityBox->setEnabled(true);
} else {
qualityBox->setCurrentIndex(0);
qualityBox->setEnabled(false);
}
}
void MainWindow::changeSavePlaylistInFolder() {
savePlaylistInFolder = savePlaylistInFolderAction->isChecked();
QSettings settings("MaximoPardo", "Phoca");
settings.setValue("savePlaylistInFolder", savePlaylistInFolderAction->isChecked());
}
void MainWindow::changeSaveThumbnail() {
saveThumbnail = saveThumbnailAction->isChecked();
QSettings settings("MaximoPardo", "Phoca");
settings.setValue("saveThumbnail", saveThumbnailAction->isChecked());
}
void MainWindow::closeEvent(QCloseEvent *event) {
// Check for active downloads
bool hasActiveDownloads = false;
for (int i = 0; i < list->count(); ++i) {
DownloadItem *di = qobject_cast<DownloadItem*>(list->itemWidget(list->item(i)));
if (di && !di->isFinished()) {
hasActiveDownloads = true;
break;
}
}
if (hasActiveDownloads) {
QMessageBox::StandardButton resBtn = QMessageBox::question(this, tr("Warning"),
tr("There is a download in progress.\nAre you sure you want to close Phoca?\nThe download will be cancelled."),
QMessageBox::No | QMessageBox::Yes,
QMessageBox::No);
if (resBtn != QMessageBox::Yes) {
event->ignore();
return;
}
}
event->accept();
}
void MainWindow::clearFinishedDownloads() {
for (int i = list->count() - 1; i >= 0; --i) {
QListWidgetItem *item = list->item(i);
QWidget *widget = list->itemWidget(item);
// Casting to class
DownloadItem *downloadItem = qobject_cast<DownloadItem*>(widget);
if (downloadItem && downloadItem->isFinished()) {
delete item;
}
}
clearFinishedButton->setEnabled(false);
}
void MainWindow::itemFinished() {
clearFinishedButton->setEnabled(true);
}