Skip to content

Commit

Permalink
[browserpaths] Move directory functions to dedicated module. Fixes JB…
Browse files Browse the repository at this point in the history
…#32547
  • Loading branch information
rojkov committed Oct 19, 2015
1 parent 0ccf371 commit 7796948
Show file tree
Hide file tree
Showing 34 changed files with 148 additions and 71 deletions.
53 changes: 53 additions & 0 deletions common/browserpaths.cpp
@@ -0,0 +1,53 @@
/****************************************************************************
**
** Copyright (C) 2015 Jolla Ltd.
** Contact: Dmitry Rozhkov <dmitry.rozhkov@jolla.com>
**
****************************************************************************/

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <QString>
#include <QDir>
#include <QStandardPaths>
#include "browserpaths.h"

static QString getLocation(QStandardPaths::StandardLocation locationType) {
QString location(QStandardPaths::writableLocation(locationType));
QDir dir(location);
if (!dir.exists()) {
if (!dir.mkpath(location)) {
qWarning(QString("Can't create directory ").append(location).toLatin1().data()) ;
return QString();
}
}

return location;
}

QString BrowserPaths::downloadLocation()
{
return getLocation(QStandardPaths::DownloadLocation);
}

QString BrowserPaths::picturesLocation()
{
return getLocation(QStandardPaths::PicturesLocation);
}

QString BrowserPaths::dataLocation()
{
return getLocation(QStandardPaths::DataLocation);
}

QString BrowserPaths::applicationsLocation()
{
return getLocation(QStandardPaths::ApplicationsLocation);
}

QString BrowserPaths::cacheLocation()
{
return getLocation(QStandardPaths::CacheLocation);
}
26 changes: 26 additions & 0 deletions common/browserpaths.h
@@ -0,0 +1,26 @@
/****************************************************************************
**
** Copyright (C) 2015 Jolla Ltd.
** Contact: Dmitry Rozhkov <dmitry.rozhkov@jolla.com>
**
****************************************************************************/

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BROWSERPATHS_H
#define BROWSERPATHS_H

class QString;

struct BrowserPaths
{
static QString downloadLocation();
static QString picturesLocation();
static QString dataLocation();
static QString applicationsLocation();
static QString cacheLocation();
};

#endif // BROWSERPATHS_H
1 change: 0 additions & 1 deletion common/opensearchconfigs.cpp
Expand Up @@ -12,7 +12,6 @@

#include <QDir>
#include <QFile>
#include <QStandardPaths>
#include <QXmlStreamReader>
#include "opensearchconfigs.h"

Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions common/paths.pri
@@ -0,0 +1,9 @@
INCLUDEPATH += $$PWD

# C++ sources
SOURCES += \
$$PWD/browserpaths.cpp \

# C++ headers
HEADERS += \
$$PWD/browserpaths.h \
2 changes: 1 addition & 1 deletion settings/settings.pro
Expand Up @@ -12,7 +12,7 @@ import.files = qmldir
import.path = $$TARGETPATH
target.path = $$TARGETPATH

include(../common/common.pri)
include(../common/opensearchconfigs.pri)

SOURCES += \
declarative_plugin.cpp \
Expand Down
15 changes: 5 additions & 10 deletions src/bookmarks/bookmarkmanager.cpp
Expand Up @@ -11,8 +11,6 @@

#include "bookmarkmanager.h"
#include <QDebug>
#include <QStandardPaths>
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QJsonObject>
Expand All @@ -22,6 +20,7 @@
#include <MGConfItem>

#include "bookmark.h"
#include "browserpaths.h"

BookmarkManager::BookmarkManager()
: QObject(nullptr)
Expand All @@ -46,13 +45,9 @@ BookmarkManager* BookmarkManager::instance()

void BookmarkManager::save(const QList<Bookmark*> & bookmarks)
{
QString dataLocation = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
QDir dir(dataLocation);
if (!dir.exists()) {
if (!dir.mkpath(dataLocation)) {
qWarning() << "Can't create directory " << dataLocation;
return;
}
QString dataLocation = BrowserPaths::dataLocation();
if (dataLocation.isNull()) {
return;
}
QString path = dataLocation + "/bookmarks.json";
QFile file(path);
Expand Down Expand Up @@ -85,7 +80,7 @@ void BookmarkManager::clear()

QList<Bookmark*> BookmarkManager::load() {
QList<Bookmark*> bookmarks;
QString bookmarkFile = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/bookmarks.json";
QString bookmarkFile = BrowserPaths::dataLocation() + "/bookmarks.json";
QScopedPointer<QFile> file(new QFile(bookmarkFile));

if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
Expand Down
10 changes: 5 additions & 5 deletions src/bookmarks/desktopbookmarkwriter.cpp
Expand Up @@ -9,12 +9,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "desktopbookmarkwriter.h"

#include <QDir>
#include <QStandardPaths>
#include <QtConcurrent>

#include "desktopbookmarkwriter.h"
#include "browserpaths.h"

static bool dbw_testMode = false;

DesktopBookmarkWriter::DesktopBookmarkWriter(QObject *parent)
Expand Down Expand Up @@ -64,9 +64,9 @@ QString DesktopBookmarkWriter::uniqueDesktopFileName(QString title)
{
QString filePath;
if (!isTestModeEnabled()) {
filePath = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
filePath = BrowserPaths::applicationsLocation();
} else {
filePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
filePath = BrowserPaths::dataLocation();
}
title = title.simplified().replace(QString(" "), QString("-"));

Expand Down
2 changes: 0 additions & 2 deletions src/core/core.pri
@@ -1,7 +1,5 @@
INCLUDEPATH += $$PWD

include(../../common/common.pri)

# C++ sources
SOURCES += \
$$PWD/declarativewebcontainer.cpp \
Expand Down
9 changes: 3 additions & 6 deletions src/core/declarativewebcontainer.cpp
Expand Up @@ -17,13 +17,12 @@
#include "downloadmanager.h"
#include "declarativewebutils.h"
#include "webpagefactory.h"
#include "browserpaths.h"

#include <QPointer>
#include <QTimerEvent>
#include <QQuickWindow>
#include <QDir>
#include <QTransform>
#include <QStandardPaths>
#include <QtConcurrentRun>
#include <QGuiApplication>
#include <QScreen>
Expand Down Expand Up @@ -93,10 +92,8 @@ DeclarativeWebContainer::DeclarativeWebContainer(QWindow *parent)
connect(QMozContext::GetInstance(), SIGNAL(onInitialized()), this, SLOT(initialize()));
connect(this, SIGNAL(portraitChanged()), this, SLOT(resetHeight()));

QString cacheLocation = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
QDir dir(cacheLocation);
if(!dir.exists() && !dir.mkpath(cacheLocation)) {
qWarning() << "Can't create directory "+ cacheLocation;
QString cacheLocation = BrowserPaths::cacheLocation();
if (cacheLocation.isNull()) {
return;
}

Expand Down
14 changes: 5 additions & 9 deletions src/declarativewebutils.cpp
Expand Up @@ -31,6 +31,7 @@
#include "declarativewebutils.h"
#include "qmozcontext.h"
#include "opensearchconfigs.h"
#include "browserpaths.h"

static const QString gSystemComponentsTimeStamp("/var/lib/_MOZEMBED_CACHE_CLEAN_");
static const QString gProfilePath("/.mozilla/mozembed");
Expand Down Expand Up @@ -76,7 +77,7 @@ DeclarativeWebUtils::DeclarativeWebUtils()
connect(QMozContext::GetInstance(), SIGNAL(recvObserve(QString, QVariant)),
this, SLOT(handleObserve(QString, QVariant)));

QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QStringLiteral("/.firstUseDone");
QString path = BrowserPaths::dataLocation() + QStringLiteral("/.firstUseDone");
m_firstUseDone = fileExists(path);

connect(&m_homePage, SIGNAL(valueChanged()), this, SIGNAL(homePageChanged()));
Expand Down Expand Up @@ -158,7 +159,7 @@ void DeclarativeWebUtils::updateWebEngineSettings()
// see https://developer.mozilla.org/en-US/docs/Download_Manager_preferences
// Use custom downloads location defined in browser.download.dir
mozContext->setPref(QString("browser.download.folderList"), QVariant(2));
mozContext->setPref(QString("browser.download.dir"), downloadDir());
mozContext->setPref(QString("browser.download.dir"), BrowserPaths::downloadLocation());
// Downloads should never be removed automatically
mozContext->setPref(QString("browser.download.manager.retention"), QVariant(2));
// Downloads will be canceled on quit
Expand Down Expand Up @@ -212,7 +213,7 @@ void DeclarativeWebUtils::updateWebEngineSettings()
}

void DeclarativeWebUtils::setFirstUseDone(bool firstUseDone) {
QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QStringLiteral("/.firstUseDone");
QString path = BrowserPaths::dataLocation() + QStringLiteral("/.firstUseDone");
if (m_firstUseDone != firstUseDone) {
m_firstUseDone = firstUseDone;
if (!firstUseDone) {
Expand Down Expand Up @@ -359,14 +360,9 @@ DeclarativeWebUtils *DeclarativeWebUtils::instance()
return gSingleton;
}

QString DeclarativeWebUtils::downloadDir() const
{
return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
}

QString DeclarativeWebUtils::picturesDir() const
{
return QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
return BrowserPaths::picturesLocation();
}

QString DeclarativeWebUtils::displayableUrl(QString fullUrl) const
Expand Down
2 changes: 0 additions & 2 deletions src/declarativewebutils.h
Expand Up @@ -25,7 +25,6 @@ class DeclarativeWebUtils : public QObject
Q_OBJECT

Q_PROPERTY(QString homePage READ homePage NOTIFY homePageChanged FINAL)
Q_PROPERTY(QString downloadDir READ downloadDir CONSTANT FINAL)
Q_PROPERTY(QString picturesDir READ picturesDir CONSTANT FINAL)
Q_PROPERTY(bool firstUseDone READ firstUseDone WRITE setFirstUseDone NOTIFY firstUseDoneChanged)
Q_PROPERTY(bool debugMode READ debugMode CONSTANT FINAL)
Expand All @@ -40,7 +39,6 @@ class DeclarativeWebUtils : public QObject
public:
static DeclarativeWebUtils *instance();

QString downloadDir() const;
QString picturesDir() const;
bool firstUseDone() const;
void setFirstUseDone(bool firstUseDone);
Expand Down
4 changes: 2 additions & 2 deletions src/downloadmanager.cpp
Expand Up @@ -11,7 +11,7 @@

#include "downloadmanager.h"
#include "qmozcontext.h"
#include "declarativewebutils.h"
#include "browserpaths.h"

#include <transferengineinterface.h>
#include <transfertypes.h>
Expand Down Expand Up @@ -129,7 +129,7 @@ void DownloadManager::recvObserve(const QString message, const QVariant data)

bool DownloadManager::moveMyAppPackage(QString path)
{
QString aptoideDownloadPath = QString("%1/.aptoide/").arg(DeclarativeWebUtils::instance()->downloadDir());
QString aptoideDownloadPath = QString("%1/.aptoide/").arg(BrowserPaths::downloadLocation());
QDir dir(aptoideDownloadPath);

if (!dir.exists()) {
Expand Down
4 changes: 2 additions & 2 deletions src/qtmozembed/declarativewebpage.cpp
Expand Up @@ -12,9 +12,9 @@
#include "declarativewebpage.h"
#include "declarativewebcontainer.h"
#include "dbmanager.h"
#include "browserpaths.h"

#include <QtConcurrent>
#include <QStandardPaths>

static const QString gFullScreenMessage("embed:fullscreenchanged");
static const QString gDomContentLoadedMessage("embed:domcontentloaded");
Expand Down Expand Up @@ -333,7 +333,7 @@ QString DeclarativeWebPage::saveToFile(QImage image)
}

// 75% quality jpg produces small and good enough capture.
QString path = QString("%1/tab-%2-thumb.jpg").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)).arg(tabId());
QString path = QString("%1/tab-%2-thumb.jpg").arg(BrowserPaths::cacheLocation()).arg(tabId());
return !allBlack(image) && image.save(path, "jpg", 75) ? path : "";
}

Expand Down
2 changes: 2 additions & 0 deletions src/src.pro
Expand Up @@ -46,6 +46,8 @@ EE_QM = $$OUT_PWD/sailfish-browser_eng_en.qm
include(../translations/translations.pri)

include(../defaults.pri)
include(../common/opensearchconfigs.pri)
include(../common/paths.pri)
include(core/core.pri)
include(history/history.pri)
include(bookmarks/bookmarks.pri)
Expand Down
15 changes: 7 additions & 8 deletions src/storage/dbworker.cpp
Expand Up @@ -9,8 +9,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "dbworker.h"

#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
Expand All @@ -22,6 +20,9 @@
#include <QFile>
#include <QDateTime>

#include "dbworker.h"
#include "browserpaths.h"

#ifndef DEBUG_LOGS
#define DEBUG_LOGS 0
#endif
Expand Down Expand Up @@ -86,14 +87,12 @@ DBWorker::DBWorker(QObject *parent) :

void DBWorker::init()
{
QString databaseDir = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
const QString dbFileName = QLatin1String(DB_NAME);
QDir dir(databaseDir);

if(!dir.mkpath(databaseDir)) {
qWarning() << "Can't create directory "+ databaseDir;
QString databaseDir = BrowserPaths::dataLocation();
if (databaseDir.isNull()) {
return;
}
QDir dir(databaseDir);
const QString dbFileName(QLatin1String(DB_NAME));

m_database = QSqlDatabase::addDatabase("QSQLITE");
m_database.setDatabaseName(dir.absoluteFilePath(dbFileName));
Expand Down
5 changes: 0 additions & 5 deletions tests/auto/mocks/declarativewebutils/declarativewebutils.cpp
Expand Up @@ -85,8 +85,3 @@ DeclarativeWebUtils *DeclarativeWebUtils::instance()
return gSingleton;
}

QString DeclarativeWebUtils::downloadDir() const
{
QString dir;
return dir;
}
1 change: 0 additions & 1 deletion tests/auto/mocks/declarativewebutils/declarativewebutils.h
Expand Up @@ -35,7 +35,6 @@ class DeclarativeWebUtils : public QObject
bool firstUseDone() const;
void setFirstUseDone(bool firstUseDone);
qreal silicaPixelRatio() const;
QString downloadDir() const;

signals:
void homePageChanged();
Expand Down

0 comments on commit 7796948

Please sign in to comment.