From: dreamer.dead Date: Wed, 29 Apr 2015 14:14:02 +0000 (+0300) Subject: Add a set of paths helper functions and use it. X-Git-Tag: 2.5~82^2 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=abb37603f8eca9aa2dd04c1729d788c9dde2fe7a;p=minitube Add a set of paths helper functions and use it. --- diff --git a/minitube.pro b/minitube.pro index 2bcc62c..bc027d0 100644 --- a/minitube.pro +++ b/minitube.pro @@ -102,7 +102,8 @@ HEADERS += src/video.h \ src/ytchannel.h \ src/yt3.h \ src/paginatedvideosource.h \ - src/compatibility/qurlqueryhelper.h + src/compatibility/qurlqueryhelper.h \ + src/compatibility/pathsservice.h SOURCES += src/main.cpp \ src/searchlineedit.cpp \ src/urllineedit.cpp \ @@ -171,7 +172,8 @@ SOURCES += src/main.cpp \ src/yt3listparser.cpp \ src/ytchannel.cpp \ src/yt3.cpp \ - src/paginatedvideosource.cpp + src/paginatedvideosource.cpp \ + src/compatibility/pathsservice.cpp RESOURCES += resources.qrc DESTDIR = build/target/ OBJECTS_DIR = build/obj/ diff --git a/src/compatibility/pathsservice.cpp b/src/compatibility/pathsservice.cpp new file mode 100644 index 0000000..922c2ab --- /dev/null +++ b/src/compatibility/pathsservice.cpp @@ -0,0 +1,63 @@ +/* $BEGIN_LICENSE + +This file is part of Minitube. +Copyright 2015, Flavio Tordini + +Minitube 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. + +Minitube 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 Minitube. If not, see . + +$END_LICENSE */ + +#include "pathsservice.h" + +#include + +namespace { + +#if QT_VERSION >= 0x050000 +typedef QStandardPaths PathProvider; + +#define getLocation writableLocation +#else +typedef QDesktopServices PathProvider; + +#define getLocation storageLocation +#endif // QT_VERSION >= 0x050000 + +} // namespace + +namespace Paths { +QString getMoviesLocation() { + return PathProvider::getLocation(PathProvider::MoviesLocation); +} + +QString getDesktopLocation() { + return PathProvider::getLocation(PathProvider::DesktopLocation); +} + +QString getHomeLocation() { + return PathProvider::getLocation(PathProvider::HomeLocation); +} + +QString getDataLocation() { + return PathProvider::getLocation(PathProvider::DataLocation); +} + +QString getPicturesLocation() { + return PathProvider::getLocation(PathProvider::PicturesLocation); +} + +QString getTempLocation() { + return PathProvider::getLocation(PathProvider::TempLocation); +} +} diff --git a/src/compatibility/pathsservice.h b/src/compatibility/pathsservice.h new file mode 100644 index 0000000..4e9a2fe --- /dev/null +++ b/src/compatibility/pathsservice.h @@ -0,0 +1,35 @@ +/* $BEGIN_LICENSE + +This file is part of Minitube. +Copyright 2015, Flavio Tordini + +Minitube 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. + +Minitube 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 Minitube. If not, see . + +$END_LICENSE */ + +#ifndef PATHSSERVICE_H +#define PATHSSERVICE_H + +#include + +namespace Paths { +QString getMoviesLocation(); +QString getDesktopLocation(); +QString getHomeLocation(); +QString getDataLocation(); +QString getPicturesLocation(); +QString getTempLocation(); +} + +#endif // PATHSSERVICE_H diff --git a/src/database.cpp b/src/database.cpp index 4e089ff..e49133b 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -20,20 +20,19 @@ $END_LICENSE */ #include "database.h" #include "constants.h" -#include +#include "compatibility/pathsservice.h" +#include static const int DATABASE_VERSION = 1; static const QString dbName = QLatin1String(Constants::UNIX_NAME) + ".db"; static Database *databaseInstance = 0; Database::Database() { -#if QT_VERSION >= 0x050000 - QString dataLocation = QStandardPaths::writableLocation(QStandardPaths::DataLocation); -#else - QString dataLocation = QDesktopServices::storageLocation(QDesktopServices::DataLocation); -#endif + QString dataLocation = Paths::getDataLocation(); - QDir().mkpath(dataLocation); + if (!QDir().mkpath(dataLocation)) { + qCritical() << "Failed to create directory " << dataLocation; + } dbLocation = dataLocation + "/" + dbName; QMutexLocker locker(&lock); @@ -101,15 +100,12 @@ void Database::createDatabase() { + QString::number(DATABASE_VERSION) + ")", db); } +// static QString Database::getDbLocation() { -#if QT_VERSION >= 0x050000 - static const QString dataLocation = QStandardPaths::writableLocation(QStandardPaths::DataLocation); -#else - static const QString dataLocation = QDesktopServices::storageLocation(QDesktopServices::DataLocation); -#endif - return dataLocation + "/" + dbName; + return Paths::getDataLocation() + "/" + dbName; } +// static bool Database::exists() { static bool fileExists = false; if (!fileExists) @@ -117,6 +113,7 @@ bool Database::exists() { return fileExists; } +// static Database& Database::instance() { static QMutex mutex; QMutexLocker locker(&mutex); diff --git a/src/downloadmanager.cpp b/src/downloadmanager.cpp index 340b8d6..2a7ac2e 100644 --- a/src/downloadmanager.cpp +++ b/src/downloadmanager.cpp @@ -31,6 +31,7 @@ $END_LICENSE */ #include "extra.h" #endif #include "datautils.h" +#include "compatibility/pathsservice.h" static DownloadManager *downloadManagerInstance = 0; @@ -167,29 +168,17 @@ void DownloadManager::updateStatusMessage() { QString DownloadManager::defaultDownloadFolder() { // download in the Movies system folder -#if QT_VERSION >= 0x050000 - QString path = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation); -#else - QString path = QDesktopServices::storageLocation(QDesktopServices::MoviesLocation); -#endif + QString path = Paths::getMoviesLocation(); - QDir moviesDir(path); + const QDir moviesDir(path); if (!moviesDir.exists()) { // fallback to Desktop -#if QT_VERSION >= 0x050000 - path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); -#else - path = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation); -#endif + path = Paths::getDesktopLocation(); - QDir desktopDir(path); + const QDir desktopDir(path); if (!desktopDir.exists()) { // fallback to Home -#if QT_VERSION >= 0x050000 - path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); -#else - path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation); -#endif + path = Paths::getHomeLocation(); } } return path; diff --git a/src/downloadsettings.cpp b/src/downloadsettings.cpp index f04a3f2..e283867 100644 --- a/src/downloadsettings.cpp +++ b/src/downloadsettings.cpp @@ -21,6 +21,7 @@ $END_LICENSE */ #include "downloadsettings.h" #include "downloadmanager.h" #include "mainwindow.h" +#include "compatibility/pathsservice.h" DownloadSettings::DownloadSettings(QWidget *parent) : QWidget(parent) { @@ -56,25 +57,15 @@ void DownloadSettings::paintEvent(QPaintEvent * /*event*/) { } void DownloadSettings::changeFolder() { - QString path; + const QString path = Paths::getHomeLocation(); #ifdef APP_MAC QFileDialog* dialog = new QFileDialog(this); dialog->setFileMode(QFileDialog::Directory); dialog->setOptions(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly); -#if QT_VERSION >= 0x050000 - path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); -#else - path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation); -#endif 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"), path, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly); @@ -95,12 +86,8 @@ void DownloadSettings::folderChosen(const QString &dir) { } void DownloadSettings::updateMessage() { - QString path = DownloadManager::instance()->currentDownloadFolder(); -#if QT_VERSION >= 0x050000 - QString home = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); -#else - QString home = QDesktopServices::storageLocation(QDesktopServices::HomeLocation); -#endif + const QString path = DownloadManager::instance()->currentDownloadFolder(); + const QString home = Paths::getHomeLocation(); QString displayPath = path; displayPath = displayPath.remove(home + "/"); message->setText( diff --git a/src/jsfunctions.cpp b/src/jsfunctions.cpp index ede450e..a65c844 100644 --- a/src/jsfunctions.cpp +++ b/src/jsfunctions.cpp @@ -20,9 +20,9 @@ $END_LICENSE */ #include "jsfunctions.h" #include "networkaccess.h" -#include #include "constants.h" #include "compatibility/qurlqueryhelper.h" +#include "compatibility/pathsservice.h" namespace The { NetworkAccess* http(); @@ -65,13 +65,7 @@ QString JsFunctions::jsFilename() { } QString JsFunctions::jsPath() { - return QString( - #if QT_VERSION >= 0x050000 - QStandardPaths::writableLocation(QStandardPaths::DataLocation) - #else - QDesktopServices::storageLocation(QDesktopServices::DataLocation) - #endif - + "/" + jsFilename()); + return Paths::getDataLocation() + "/" + jsFilename(); } void JsFunctions::loadJs() { diff --git a/src/snapshotsettings.cpp b/src/snapshotsettings.cpp index 116286b..b65d4fa 100644 --- a/src/snapshotsettings.cpp +++ b/src/snapshotsettings.cpp @@ -20,11 +20,12 @@ $END_LICENSE */ #include "snapshotsettings.h" #include "mainwindow.h" -#include #ifdef APP_MAC #include "macutils.h" #endif #include "constants.h" +#include "compatibility/pathsservice.h" +#include SnapshotSettings::SnapshotSettings(QWidget *parent) : QWidget(parent) { QBoxLayout *layout = new QHBoxLayout(this); @@ -75,11 +76,7 @@ QString SnapshotSettings::getCurrentLocation() { QSettings settings; QString location = settings.value("snapshotsFolder").toString(); if (location.isEmpty() || !QFile::exists(location)) { -#if QT_VERSION >= 0x050000 - location = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); -#else - location = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation); -#endif + location = Paths::getPicturesLocation(); #ifdef APP_MAC_STORE location += "/MinitubeforYouTube"; #endif @@ -92,36 +89,21 @@ QString SnapshotSettings::displayPath(const QString &path) { return QDir(path).dirName(); #endif -#if QT_VERSION >= 0x050000 - QString home = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); -#else - QString home = QDesktopServices::storageLocation(QDesktopServices::HomeLocation); -#endif + const QString home = Paths::getHomeLocation(); QString displayPath = path; displayPath = displayPath.remove(home + "/"); return displayPath; } void SnapshotSettings::changeFolder() { - QString path; + const QString path = Paths::getHomeLocation(); #ifdef APP_MAC QFileDialog* dialog = new QFileDialog(this); dialog->setFileMode(QFileDialog::Directory); dialog->setOptions(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly); -#if QT_VERSION >= 0x050000 - path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); -#else - path = QDesktopServices::storageLocation(QDesktopServices::HomeLocation); -#endif 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(), QString(), path, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::ReadOnly); diff --git a/src/temporary.cpp b/src/temporary.cpp index 40bd542..7fd978c 100644 --- a/src/temporary.cpp +++ b/src/temporary.cpp @@ -20,6 +20,7 @@ $END_LICENSE */ #include "temporary.h" #include "constants.h" +#include "compatibility/pathsservice.h" static QList paths; #ifdef Q_OS_LINUX @@ -29,11 +30,7 @@ static QString userName; Temporary::Temporary() { } QString Temporary::filename() { -#if QT_VERSION >= 0x050000 - static const QString tempDir = QStandardPaths::writableLocation(QStandardPaths::TempLocation); -#else - static const QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation); -#endif + static const QString tempDir = Paths::getTempLocation(); QString tempFile = tempDir + "/" + Constants::UNIX_NAME + "-" + QString::number(qrand()); diff --git a/src/ytchannel.cpp b/src/ytchannel.cpp index d5fa795..cd5531c 100644 --- a/src/ytchannel.cpp +++ b/src/ytchannel.cpp @@ -28,6 +28,7 @@ $END_LICENSE */ #include #endif #include "compatibility/qurlqueryhelper.h" +#include "compatibility/pathsservice.h" namespace The { NetworkAccess* http(); @@ -190,13 +191,7 @@ void YTChannel::loadThumbnail() { } const QString & YTChannel::getThumbnailDir() { - static const QString thumbDir = - #if QT_VERSION >= 0x050000 - QStandardPaths::writableLocation(QStandardPaths::DataLocation) - #else - QDesktopServices::storageLocation(QDesktopServices::DataLocation) - #endif - + "/channels/"; + static const QString thumbDir = Paths::getDataLocation() + "/channels/"; return thumbDir; }