From 9476a8954eb308f1c64aac9943c6c2c28615174c Mon Sep 17 00:00:00 2001 From: Flavio Tordini Date: Sun, 21 Sep 2014 18:43:50 +0200 Subject: [PATCH] Moved filename chars function to DataUtils --- src/datautils.cpp | 22 ++++++++++++++++++++++ src/datautils.h | 16 ++++++++++++++++ src/downloadmanager.cpp | 19 ++----------------- src/downloadsettings.cpp | 11 ++++++++--- 4 files changed, 48 insertions(+), 20 deletions(-) create mode 100644 src/datautils.cpp create mode 100644 src/datautils.h diff --git a/src/datautils.cpp b/src/datautils.cpp new file mode 100644 index 0000000..3490448 --- /dev/null +++ b/src/datautils.cpp @@ -0,0 +1,22 @@ +#include "datautils.h" + +QString DataUtils::stringToFilename(const QString &s) { + QString f = s; + f.replace('(', '['); + f.replace(')', ']'); + f.replace('/', ' '); + f.replace('\\', ' '); + f.replace('<', ' '); + f.replace('>', ' '); + f.replace(':', ' '); + f.replace('"', ' '); + f.replace('|', ' '); + f.replace('?', ' '); + f.replace('*', ' '); + f = f.simplified(); + + if (!f.isEmpty() && f.at(0) == '.') + f = f.mid(1).trimmed(); + + return f; +} diff --git a/src/datautils.h b/src/datautils.h new file mode 100644 index 0000000..eb0a3c0 --- /dev/null +++ b/src/datautils.h @@ -0,0 +1,16 @@ +#ifndef DATAUTILS_H +#define DATAUTILS_H + +#include + +class DataUtils { + +public: + static QString stringToFilename(const QString &s); + +private: + DataUtils() { } + +}; + +#endif // DATAUTILS_H diff --git a/src/downloadmanager.cpp b/src/downloadmanager.cpp index c42b074..340b8d6 100644 --- a/src/downloadmanager.cpp +++ b/src/downloadmanager.cpp @@ -30,6 +30,7 @@ $END_LICENSE */ #ifdef APP_EXTRA #include "extra.h" #endif +#include "datautils.h" static DownloadManager *downloadManagerInstance = 0; @@ -123,23 +124,7 @@ void DownloadManager::gotStreamUrl(QUrl url) { video->disconnect(this); - QString basename = video->title(); - basename.replace('(', '['); - basename.replace(')', ']'); - basename.replace('/', ' '); - basename.replace('\\', ' '); - basename.replace('<', ' '); - basename.replace('>', ' '); - basename.replace(':', ' '); - basename.replace('"', ' '); - basename.replace('|', ' '); - basename.replace('?', ' '); - basename.replace('*', ' '); - basename = basename.simplified(); - - if (!basename.isEmpty() && basename.at(0) == '.') - basename = basename.mid(1).trimmed(); - + QString basename = DataUtils::stringToFilename(video->title()); if (basename.isEmpty()) basename = video->id(); QString filename = currentDownloadFolder() + "/" + basename + ".mp4"; diff --git a/src/downloadsettings.cpp b/src/downloadsettings.cpp index 333f8e1..f04a3f2 100644 --- a/src/downloadsettings.cpp +++ b/src/downloadsettings.cpp @@ -56,12 +56,11 @@ void DownloadSettings::paintEvent(QPaintEvent * /*event*/) { } void DownloadSettings::changeFolder() { - + QString path; #ifdef APP_MAC QFileDialog* dialog = new QFileDialog(this); dialog->setFileMode(QFileDialog::Directory); dialog->setOptions(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly); - QString path; #if QT_VERSION >= 0x050000 path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); #else @@ -70,8 +69,14 @@ void DownloadSettings::changeFolder() { dialog->setDirectory(path); dialog->open(this, SLOT(folderChosen(const QString &))); #else + +#if QT_VERSION >= 0x050000 + path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); +#else + path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation); +#endif QString folder = QFileDialog::getExistingDirectory(window(), tr("Choose the download location"), - QDesktopServices::storageLocation(QDesktopServices::HomeLocation), + path, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly); folderChosen(folder); #endif -- 2.39.5