Browse Source

Move downloads folder detection code from preferences.h to misc.cpp

adaptive-webui-19844
Christophe Dumez 13 years ago
parent
commit
01ad2e9746
  1. 55
      src/misc.cpp
  2. 1
      src/misc.h
  3. 39
      src/preferences/preferences.h

55
src/misc.cpp

@ -37,6 +37,7 @@
#include <QByteArray> #include <QByteArray>
#include <QDebug> #include <QDebug>
#include <QProcess> #include <QProcess>
#include <QSettings>
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/operations.hpp>
#ifdef DISABLE_GUI #ifdef DISABLE_GUI
@ -59,6 +60,8 @@ const int UNLEN = 256;
#ifdef Q_WS_MAC #ifdef Q_WS_MAC
#include <CoreServices/CoreServices.h> #include <CoreServices/CoreServices.h>
#include <Carbon/Carbon.h> #include <Carbon/Carbon.h>
#include <Cocoa/Cocoa.h>
#include <NSPathUtilities.h>
#endif #endif
#ifdef Q_WS_WIN #ifdef Q_WS_WIN
@ -101,7 +104,9 @@ QString misc::QDesktopServicesDataLocation() {
return result; return result;
#else #else
#ifdef Q_WS_MAC #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; FSRef ref;
OSErr err = FSFindFolder(kUserDomain, kApplicationSupportFolderType, false, &ref); OSErr err = FSFindFolder(kUserDomain, kApplicationSupportFolderType, false, &ref);
if (err) if (err)
@ -149,6 +154,54 @@ QString misc::QDesktopServicesCacheLocation() {
#endif #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) { long long misc::freeDiskSpaceOnPath(QString path) {
if(path.isEmpty()) return -1; if(path.isEmpty()) return -1;
path.replace("\\", "/"); path.replace("\\", "/");

1
src/misc.h

@ -145,6 +145,7 @@ public:
/* Ported from Qt4 to drop dependency on QtGui */ /* Ported from Qt4 to drop dependency on QtGui */
static QString QDesktopServicesDataLocation(); static QString QDesktopServicesDataLocation();
static QString QDesktopServicesCacheLocation(); static QString QDesktopServicesCacheLocation();
static QString QDesktopServicesDownloadLocation();
/* End of Qt4 code */ /* End of Qt4 code */
#ifndef DISABLE_GUI #ifndef DISABLE_GUI

39
src/preferences/preferences.h

@ -70,7 +70,7 @@ enum Service { DYNDNS, NOIP, NONE = -1 };
} }
class Preferences : public QIniSettings { class Preferences : public QIniSettings {
Q_DISABLE_COPY(Preferences); Q_DISABLE_COPY(Preferences)
public: public:
Preferences() : QIniSettings("qBittorrent", "qBittorrent") { Preferences() : QIniSettings("qBittorrent", "qBittorrent") {
@ -186,45 +186,10 @@ public:
// Downloads // Downloads
QString getSavePath() const { 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(); QString save_path = value(QString::fromUtf8("Preferences/Downloads/SavePath")).toString();
if (!save_path.isEmpty()) if (!save_path.isEmpty())
return save_path; return save_path;
return misc::QDesktopServicesDownloadLocation();
// 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";
}
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
} }
void setSavePath(const QString &save_path) { void setSavePath(const QString &save_path) {

Loading…
Cancel
Save