diff --git a/src/misc.cpp b/src/misc.cpp index c24252b72..14acbaede 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #ifdef DISABLE_GUI @@ -59,6 +60,8 @@ const int UNLEN = 256; #ifdef Q_WS_MAC #include #include +#include +#include #endif #ifdef Q_WS_WIN @@ -101,7 +104,9 @@ QString misc::QDesktopServicesDataLocation() { return result; #else #ifdef Q_WS_MAC - // http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/Reference/reference.html + // http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); + NSString *dataDirectory = FSRef ref; OSErr err = FSFindFolder(kUserDomain, kApplicationSupportFolderType, false, &ref); if (err) @@ -149,6 +154,54 @@ QString misc::QDesktopServicesCacheLocation() { #endif } +QString misc::QDesktopServicesDownloadLocation() { +#ifdef Q_WS_WIN + // TODO: Use IKnownFolderManager to get path of FOLDERID_Downloads + // instead of hardcoding "Downloads" + return QDir(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation)).absoluteFilePath(tr("Downloads")).toString(); +#endif + +#ifdef Q_WS_X11 + QString save_path; + // Default save path on Linux + QString config_path = QString::fromLocal8Bit(qgetenv("XDG_CONFIG_HOME").constData()); + if (config_path.isEmpty()) + config_path = QDir::home().absoluteFilePath(".config"); + + QString user_dirs_file = config_path + "/user-dirs.dirs"; + if (QFile::exists(user_dirs_file)) { + QSettings settings(user_dirs_file, QSettings::IniFormat); + QString xdg_download_dir = settings.value("XDG_DOWNLOAD_DIR").toString(); + if (!xdg_download_dir.isEmpty()) { + // Resolve $HOME environment variables + xdg_download_dir.replace("$HOME", QDir::homePath()); + save_path = xdg_download_dir; + qDebug() << Q_FUNC_INFO << "SUCCESS: Using XDG path for downloads: " << save_path; + } + } + + // Fallback + if (!save_path.isEmpty() && !QFile::exists(save_path)) { + QDir().mkpath(save_path); + } + + if (save_path.isEmpty() || !QFile::exists(save_path)) { + save_path = QDir::home().absoluteFilePath(tr("Downloads")); + qDebug() << Q_FUNC_INFO << "using" << save_path << "as fallback since the XDG detection did not work"; + } + + return save_path; +#endif + +#ifdef Q_WS_MAC + // TODO: Use NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES) + // See http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html +#endif + + // Fallback + return QDir::home().absoluteFilePath(tr("Downloads")); +} + long long misc::freeDiskSpaceOnPath(QString path) { if(path.isEmpty()) return -1; path.replace("\\", "/"); diff --git a/src/misc.h b/src/misc.h index 015b91696..4cce42d8f 100644 --- a/src/misc.h +++ b/src/misc.h @@ -145,6 +145,7 @@ public: /* Ported from Qt4 to drop dependency on QtGui */ static QString QDesktopServicesDataLocation(); static QString QDesktopServicesCacheLocation(); + static QString QDesktopServicesDownloadLocation(); /* End of Qt4 code */ #ifndef DISABLE_GUI diff --git a/src/preferences/preferences.h b/src/preferences/preferences.h index 5c8daddfa..1e082b2e4 100644 --- a/src/preferences/preferences.h +++ b/src/preferences/preferences.h @@ -70,7 +70,7 @@ enum Service { DYNDNS, NOIP, NONE = -1 }; } class Preferences : public QIniSettings { - Q_DISABLE_COPY(Preferences); + Q_DISABLE_COPY(Preferences) public: Preferences() : QIniSettings("qBittorrent", "qBittorrent") { @@ -186,45 +186,10 @@ public: // Downloads QString getSavePath() const { -#if defined(Q_WS_WIN) - // TODO: Use IKnownFolderManager to get path of FOLDERID_Downloads - // instead of hardcoding "Downloads" - return value(QString::fromUtf8("Preferences/Downloads/SavePath"), - QDir(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation)).absoluteFilePath("Downloads")).toString(); -#elif defined(Q_WS_X11) - QString save_path = value(QString::fromUtf8("Preferences/Downloads/SavePath")).toString(); - if (!save_path.isEmpty()) - return save_path; - - // Default save path on Linux - QString config_path = QString::fromLocal8Bit(qgetenv("XDG_CONFIG_HOME").constData()); - if (config_path.isEmpty()) - config_path = QDir::home().absoluteFilePath(".config"); - - QString user_dirs_file = config_path + "/user-dirs.dirs"; - if (QFile::exists(user_dirs_file)) { - QSettings settings(user_dirs_file, QSettings::IniFormat); - QString xdg_download_dir = settings.value("XDG_DOWNLOAD_DIR").toString(); - if (!xdg_download_dir.isEmpty()) { - // Resolve $HOME environment variables - xdg_download_dir.replace("$HOME", QDir::homePath()); - save_path = xdg_download_dir; - qDebug() << Q_FUNC_INFO << "SUCCESS: Using XDG path for downloads: " << save_path; - } - } - - // Fallback - if (save_path.isEmpty() || !QFile::exists(save_path)) { - save_path = QDir::home().absoluteFilePath("Downloads"); - qDebug() << Q_FUNC_INFO << "using" << save_path << "as fallback since the XDG detection did not work"; - } - + QString save_path = value(QString::fromUtf8("Preferences/Downloads/SavePath")).toString(); + if (!save_path.isEmpty()) return save_path; - -#else - // TODO: On MAC OS X there is probably a way to detect the Downloads folder path - return value(QString::fromUtf8("Preferences/Downloads/SavePath"), QDir::home().absoluteFilePath("Downloads")).toString(); -#endif + return misc::QDesktopServicesDownloadLocation(); } void setSavePath(const QString &save_path) {