]> git.sur5r.net Git - minitube/commitdiff
Add a set of paths helper functions and use it.
authordreamer.dead <dreamer.dead@gmail.com>
Wed, 29 Apr 2015 14:14:02 +0000 (17:14 +0300)
committerdreamer.dead <dreamer.dead@gmail.com>
Wed, 29 Apr 2015 14:14:02 +0000 (17:14 +0300)
minitube.pro
src/compatibility/pathsservice.cpp [new file with mode: 0644]
src/compatibility/pathsservice.h [new file with mode: 0644]
src/database.cpp
src/downloadmanager.cpp
src/downloadsettings.cpp
src/jsfunctions.cpp
src/snapshotsettings.cpp
src/temporary.cpp
src/ytchannel.cpp

index 2bcc62cf5d90f27702a861dc35d2a3db3c837639..bc027d09b5dc34f81062e05c6cc269ecd12a676d 100644 (file)
@@ -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 (file)
index 0000000..922c2ab
--- /dev/null
@@ -0,0 +1,63 @@
+/* $BEGIN_LICENSE
+
+This file is part of Minitube.
+Copyright 2015, Flavio Tordini <flavio.tordini@gmail.com>
+
+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 <http://www.gnu.org/licenses/>.
+
+$END_LICENSE */
+
+#include "pathsservice.h"
+
+#include <QDesktopServices>
+
+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 (file)
index 0000000..4e9a2fe
--- /dev/null
@@ -0,0 +1,35 @@
+/* $BEGIN_LICENSE
+
+This file is part of Minitube.
+Copyright 2015, Flavio Tordini <flavio.tordini@gmail.com>
+
+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 <http://www.gnu.org/licenses/>.
+
+$END_LICENSE */
+
+#ifndef PATHSSERVICE_H
+#define PATHSSERVICE_H
+
+#include <QtGlobal>
+
+namespace Paths {
+QString getMoviesLocation();
+QString getDesktopLocation();
+QString getHomeLocation();
+QString getDataLocation();
+QString getPicturesLocation();
+QString getTempLocation();
+}
+
+#endif  // PATHSSERVICE_H
index 4e089ff9cdc895d812aabadf81be0470e0a93f05..e49133b1e829d698fe5076c9b2786992bbc7150e 100644 (file)
@@ -20,20 +20,19 @@ $END_LICENSE */
 
 #include "database.h"
 #include "constants.h"
-#include <QDesktopServices>
+#include "compatibility/pathsservice.h"
+#include <QtDebug>
 
 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);
index 340b8d6a9a159e48b2d2edbf86b8aa716f776ddf..2a7ac2e87620c8cdc0b67e3a3bf7462ce2763b54 100644 (file)
@@ -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;
index f04a3f215b8663f39253ef78e49581fa9f42578d..e283867586216fb7cc270e8b57c2a7fd24df897f 100644 (file)
@@ -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(
index ede450e6ca81f9e37202b865a4f436b159bba6e1..a65c844d7e2d8c7c3747978a3293533a7ff5f784 100644 (file)
@@ -20,9 +20,9 @@ $END_LICENSE */
 
 #include "jsfunctions.h"
 #include "networkaccess.h"
-#include <QDesktopServices>
 #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() {
index 116286b123aa3c9a662d70e8a65d0cfe898c4c52..b65d4fa1e3b3b0b31c6d9ed452b6a2b5ae0e8ac3 100644 (file)
@@ -20,11 +20,12 @@ $END_LICENSE */
 
 #include "snapshotsettings.h"
 #include "mainwindow.h"
-#include <QDesktopServices>
 #ifdef APP_MAC
 #include "macutils.h"
 #endif
 #include "constants.h"
+#include "compatibility/pathsservice.h"
+#include <QDesktopServices>
 
 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);
index 40bd5427efcd481659665ac4573f31fc4f35ff05..7fd978c14fca070f82f3565b1a75bb71c84f7761 100644 (file)
@@ -20,6 +20,7 @@ $END_LICENSE */
 
 #include "temporary.h"
 #include "constants.h"
+#include "compatibility/pathsservice.h"
 
 static QList<QString> 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());
 
index d5fa79508a31cc55c0671fb1ecbbad5b0c63d359..cd5531cb927f737022f7dacb89b5477915e132a4 100644 (file)
@@ -28,6 +28,7 @@ $END_LICENSE */
 #include <QtScript>
 #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;
 }