videodownloader/cli/mainfeatures.cpp

120 lines
4.5 KiB
C++
Raw Normal View History

2015-09-08 17:05:59 +02:00
#include "./mainfeatures.h"
2015-09-08 17:05:59 +02:00
#include "../network/download.h"
2017-05-01 03:22:50 +02:00
#include "../network/groovesharkdownload.h"
2015-09-08 17:05:59 +02:00
#include "../network/httpdownload.h"
#include "../network/youtubedownload.h"
2015-06-24 00:56:12 +02:00
#include <c++utilities/application/argumentparser.h>
#include <c++utilities/application/commandlineutils.h>
2017-05-01 03:22:50 +02:00
#include <c++utilities/conversion/stringconversion.h>
2015-06-24 00:56:12 +02:00
#include <QCoreApplication>
#include <iostream>
using namespace std;
using namespace std::placeholders;
2019-06-10 22:50:15 +02:00
using namespace CppUtilities;
2015-06-24 00:56:12 +02:00
using namespace Network;
namespace Cli {
2016-08-14 22:51:58 +02:00
void download(int argc, char *argv[], const ArgumentOccurrence &, const Argument &urlsArg, const Argument &noConfirmArg)
{
2015-09-08 17:05:59 +02:00
CMD_UTILS_START_CONSOLE;
2015-06-24 00:56:12 +02:00
// init Qt
QCoreApplication app(argc, argv);
QObject rootObj;
// instantiate downloads
QList<Download *> downloads;
2016-06-14 00:54:13 +02:00
downloads.reserve(urlsArg.values().size());
2015-06-24 00:56:12 +02:00
QVariant currentTargetDirectory;
QVariant currentTargetName;
2017-05-01 03:22:50 +02:00
enum { Auto, HttpUrl, YoutubeUrl, YoutubeId, GroovesharkId } currentDownloadType = Auto;
2015-06-24 00:56:12 +02:00
size_t specifiedDownloads = 0;
2017-05-01 03:22:50 +02:00
for (const auto &val : urlsArg.values()) {
2015-06-24 00:56:12 +02:00
// check whether value denotes target directory or download type
2019-07-20 20:20:58 +02:00
auto parts = splitString<vector<string>>(val, "=", EmptyPartsTreat::Keep, 2);
2017-05-01 03:22:50 +02:00
if (parts.size() >= 2) {
if (parts.front() == "type") {
2015-06-24 00:56:12 +02:00
// value denotes download type
2017-05-01 03:22:50 +02:00
if (parts.back() == "http") {
2015-06-24 00:56:12 +02:00
currentDownloadType = HttpUrl;
2017-05-01 03:22:50 +02:00
} else if (parts.back() == "youtubeurl") {
2015-06-24 00:56:12 +02:00
currentDownloadType = YoutubeUrl;
2017-05-01 03:22:50 +02:00
} else if (parts.back() == "youtubeid") {
2015-06-24 00:56:12 +02:00
currentDownloadType = YoutubeId;
2017-05-01 03:22:50 +02:00
} else if (parts.back() == "groovesharkid") {
2015-06-24 00:56:12 +02:00
currentDownloadType = GroovesharkId;
} else {
currentDownloadType = Auto;
2017-05-01 03:22:50 +02:00
if (parts.back() != "auto") {
2015-06-24 00:56:12 +02:00
cout << "Specified type \"" << parts.back() << "\" is invalid; using auto-detection." << endl;
}
}
continue;
2017-05-01 03:22:50 +02:00
} else if (parts.front() == "targetdir") {
2015-06-24 00:56:12 +02:00
currentTargetDirectory = QString::fromStdString(parts.back());
continue;
2017-05-01 03:22:50 +02:00
} else if (parts.front() == "targetname") {
2015-06-24 00:56:12 +02:00
currentTargetName = QString::fromStdString(parts.back());
continue;
}
}
// do actual instantiation of new download object
Download *download;
QString qstrVal = QString::fromStdString(val);
2017-05-01 03:22:50 +02:00
switch (currentDownloadType) {
2015-06-24 00:56:12 +02:00
case HttpUrl:
download = new HttpDownload(QUrl(qstrVal));
break;
case YoutubeUrl:
download = new YoutubeDownload(QUrl(qstrVal));
break;
case YoutubeId:
download = new YoutubeDownload(qstrVal);
break;
case GroovesharkId:
download = new GroovesharkDownload(qstrVal);
break;
default:
download = Download::fromUrl(qstrVal);
break;
}
// set properties of download accordingly current configuration
2017-05-01 03:22:50 +02:00
if (download) {
2015-06-24 00:56:12 +02:00
download->setParent(&rootObj);
2017-05-01 03:22:50 +02:00
if (!currentTargetDirectory.isNull()) {
2015-06-24 00:56:12 +02:00
download->setProperty("targetdir", currentTargetDirectory);
}
2017-05-01 03:22:50 +02:00
if (!currentTargetName.isNull()) {
2015-06-24 00:56:12 +02:00
download->setProperty("targetname", currentTargetName);
currentTargetName.clear();
}
downloads << download;
cout << "Specified URL \"" << val << "\" has been added as " << download->typeName().toStdString() << " download." << endl;
} else {
cout << "Specified URL \"" << val << "\" can not be added." << endl;
}
++specifiedDownloads;
}
// check whether downloads could be instantiated, print appropriate error messages if not
2017-05-01 03:22:50 +02:00
if (!specifiedDownloads) {
2015-06-24 00:56:12 +02:00
cout << "No downloads have been specified." << endl;
2017-05-01 03:22:50 +02:00
} else if (downloads.isEmpty()) {
2015-06-24 00:56:12 +02:00
cout << "None of the specified downloads could be added." << endl;
} else {
// ask the user to continue
2017-05-01 03:22:50 +02:00
if (!noConfirmArg.isPresent() && !confirmPrompt("Do you want to start these downloads?", Response::Yes)) {
2015-06-24 00:56:12 +02:00
return;
}
cout << "Starting downloads ..." << endl;
//for(Download *download : downloads) {
// download->init();
//}
cout << "TODO" << endl;
}
}
2019-07-20 20:20:58 +02:00
} // namespace Cli