[app] Added BooksStorage::tmpStorage()

To support opening books located outside of the normal directory
hierarchy.
This commit is contained in:
Slava Monich 2021-11-07 02:54:06 +02:00
parent 134e9738f3
commit e07cea347a
2 changed files with 48 additions and 23 deletions

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2015-2020 Jolla Ltd. * Copyright (C) 2015-2021 Jolla Ltd.
* Copyright (C) 2015-2020 Slava Monich <slava.monich@jolla.com> * Copyright (C) 2015-2021 Slava Monich <slava.monich@jolla.com>
* *
* You may use this file under the terms of the BSD license as follows: * You may use this file under the terms of the BSD license as follows:
* *
@ -55,6 +55,7 @@
#include <libudev.h> #include <libudev.h>
#define TMP_STATE_DIR "tmp"
#define INTERNAL_STATE_DIR "internal" #define INTERNAL_STATE_DIR "internal"
#define REMOVABLE_STATE_DIR "removable" #define REMOVABLE_STATE_DIR "removable"
@ -68,7 +69,7 @@ class BooksStorage::Private: public QObject
public: public:
Private(QString aDevice, QString aMountPoint, QString aBooksDir, Private(QString aDevice, QString aMountPoint, QString aBooksDir,
bool aInternal); Type aType);
bool isRemoved() const; bool isRemoved() const;
bool equal(const Private& aData) const; bool equal(const Private& aData) const;
@ -86,7 +87,7 @@ public:
QString iMountPoint; QString iMountPoint;
QDir iBooksDir; QDir iBooksDir;
QDir iConfigDir; QDir iConfigDir;
bool iInternal; Type iType;
bool iPresent; bool iPresent;
}; };
@ -94,35 +95,42 @@ BooksStorage::Private::Private(
QString aDevice, QString aDevice,
QString aMountPoint, QString aMountPoint,
QString aBooksDir, QString aBooksDir,
bool aInternal) : Type aType) :
iRef(1), iRef(1),
iDevice(aDevice), iDevice(aDevice),
iMountPoint(aMountPoint), iMountPoint(aMountPoint),
iBooksDir(aBooksDir), iBooksDir(aBooksDir),
iInternal(aInternal), iType(aType),
iPresent(true) iPresent(true)
{ {
QString cfgDir; QString cfgDir;
cfgDir = QString::fromStdString(ZLibrary::ApplicationWritableDirectory()); cfgDir = QString::fromStdString(ZLibrary::ApplicationWritableDirectory());
if (!cfgDir.endsWith('/')) cfgDir += '/'; if (!cfgDir.endsWith('/')) cfgDir += '/';
if (aInternal) { QString subDir;
switch (aType) {
case InternalStorage:
cfgDir += INTERNAL_STATE_DIR; cfgDir += INTERNAL_STATE_DIR;
QString subDir(aDevice); subDir = aDevice;
if (subDir.startsWith("/dev")) subDir.remove(0,4); if (subDir.startsWith("/dev")) subDir.remove(0,4);
if (!subDir.startsWith('/')) cfgDir += '/'; if (!subDir.startsWith('/')) cfgDir += '/';
cfgDir += subDir; cfgDir += subDir;
} else { break;
case RemovableStorage:
cfgDir += REMOVABLE_STATE_DIR "/"; cfgDir += REMOVABLE_STATE_DIR "/";
QString label = QDir(Private::mountPoint(aBooksDir)).dirName(); subDir = QDir(Private::mountPoint(aBooksDir)).dirName();
if (label.isEmpty()) label = "sdcard"; if (subDir.isEmpty()) subDir = "sdcard";
cfgDir += label; cfgDir += subDir;
break;
case TmpStorage:
cfgDir += TMP_STATE_DIR "/";
break;
} }
iConfigDir.setPath(cfgDir); iConfigDir.setPath(cfgDir);
} }
bool BooksStorage::Private::equal(const BooksStorage::Private& aData) const bool BooksStorage::Private::equal(const BooksStorage::Private& aData) const
{ {
return iInternal == aData.iInternal && return iType == aData.iType &&
iPresent == aData.iPresent && iPresent == aData.iPresent &&
iMountPoint == aData.iMountPoint && iMountPoint == aData.iMountPoint &&
iDevice == aData.iDevice && iDevice == aData.iDevice &&
@ -182,8 +190,8 @@ BooksStorage::BooksStorage(const BooksStorage& aStorage) :
} }
BooksStorage::BooksStorage(QString aDevice, QString aMount, QString aBooksDir, BooksStorage::BooksStorage(QString aDevice, QString aMount, QString aBooksDir,
bool aInternal) : QObject(NULL), Type aType) : QObject(NULL),
iPrivate(new Private(aDevice, aMount, aBooksDir, aInternal)), iPrivate(new Private(aDevice, aMount, aBooksDir, aType)),
iPassThrough(false) iPassThrough(false)
{ {
HDEBUG("config dir" << qPrintable(configDir().path())); HDEBUG("config dir" << qPrintable(configDir().path()));
@ -194,6 +202,11 @@ BooksStorage::~BooksStorage()
if (iPrivate && !iPrivate->iRef.deref()) delete iPrivate; if (iPrivate && !iPrivate->iRef.deref()) delete iPrivate;
} }
BooksStorage BooksStorage::tmpStorage()
{
return BooksStorage("tmpfs", "/tmp", "/", BooksStorage::TmpStorage);
}
void BooksStorage::connectNotify(const QMetaMethod& aSignal) void BooksStorage::connectNotify(const QMetaMethod& aSignal)
{ {
if (iPrivate && !iPassThrough) { if (iPrivate && !iPassThrough) {
@ -223,7 +236,7 @@ QDir BooksStorage::configDir() const
bool BooksStorage::isInternal() const bool BooksStorage::isInternal() const
{ {
return iPrivate && iPrivate->iInternal; return iPrivate && iPrivate->iType == InternalStorage;
} }
bool BooksStorage::isPresent() const bool BooksStorage::isPresent() const
@ -383,7 +396,8 @@ BooksStorageManager::Private::Private(BooksStorageManager* aParent) :
QString path(entry.path); QString path(entry.path);
if (!path.endsWith('/')) path += '/'; if (!path.endsWith('/')) path += '/';
path += iSettings->removableRoot(); path += iSettings->removableRoot();
BooksStorage bs(entry.dev, entry.path, path, false); BooksStorage bs(entry.dev, entry.path, path,
BooksStorage::RemovableStorage);
HDEBUG("removable" << entry.dev << bs.booksDir().path()); HDEBUG("removable" << entry.dev << bs.booksDir().path());
iStorageList.append(bs); iStorageList.append(bs);
} else { } else {
@ -394,7 +408,8 @@ BooksStorageManager::Private::Private(BooksStorageManager* aParent) :
mounts.close(); mounts.close();
} }
iStorageList.insert(0, BooksStorage(homeDevice, homeMount, homeBooks, true)); iStorageList.insert(0, BooksStorage(homeDevice, homeMount, homeBooks,
BooksStorage::InternalStorage));
if (iUdev) { if (iUdev) {
iMonitor = udev_monitor_new_from_netlink(iUdev, "udev"); iMonitor = udev_monitor_new_from_netlink(iUdev, "udev");
@ -495,7 +510,8 @@ bool BooksStorageManager::Private::scanMounts()
if (!path.endsWith('/')) path += '/'; if (!path.endsWith('/')) path += '/';
path += iSettings->removableRoot(); path += iSettings->removableRoot();
HDEBUG("new removable device" << entry.dev << path); HDEBUG("new removable device" << entry.dev << path);
BooksStorage storage(entry.dev, entry.path, path, false); BooksStorage storage(entry.dev, entry.path, path,
BooksStorage::RemovableStorage);
iStorageList.append(storage); iStorageList.append(storage);
Q_EMIT iParent->storageAdded(storage); Q_EMIT iParent->storageAdded(storage);
newStorageFound = true; newStorageFound = true;
@ -585,7 +601,8 @@ void BooksStorageManager::Private::onRemovableRootChanged()
if (!path.endsWith('/')) path += '/'; if (!path.endsWith('/')) path += '/';
path += iSettings->removableRoot(); path += iSettings->removableRoot();
BooksStorage updated(storage.iPrivate->iDevice, BooksStorage updated(storage.iPrivate->iDevice,
storage.iPrivate->iMountPoint, path, false); storage.iPrivate->iMountPoint, path,
BooksStorage::RemovableStorage);
if (!storage.equal(updated)) { if (!storage.equal(updated)) {
replaced.append(storage); replaced.append(storage);
replaced.append(updated); replaced.append(updated);

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2015-2019 Jolla Ltd. * Copyright (C) 2015-2021 Jolla Ltd.
* Copyright (C) 2015-2019 Slava Monich <slava.monich@jolla.com> * Copyright (C) 2015-2021 Slava Monich <slava.monich@jolla.com>
* *
* You may use this file under the terms of the BSD license as follows: * You may use this file under the terms of the BSD license as follows:
* *
@ -48,6 +48,12 @@ class BooksStorage: public QObject
Q_PROPERTY(bool internal READ isInternal CONSTANT) Q_PROPERTY(bool internal READ isInternal CONSTANT)
Q_PROPERTY(bool valid READ isValid CONSTANT) Q_PROPERTY(bool valid READ isValid CONSTANT)
enum Type {
InternalStorage,
RemovableStorage,
TmpStorage
};
public: public:
BooksStorage(); BooksStorage();
BooksStorage(const BooksStorage& aStorage); BooksStorage(const BooksStorage& aStorage);
@ -72,12 +78,14 @@ public:
bool operator == (const BooksStorage& aStorage) const bool operator == (const BooksStorage& aStorage) const
{ return equal(aStorage); } { return equal(aStorage); }
static BooksStorage tmpStorage();
Q_SIGNALS: Q_SIGNALS:
void removed(); void removed();
private: private:
friend class BooksStorageManager; friend class BooksStorageManager;
BooksStorage(QString, QString, QString, bool); BooksStorage(QString, QString, QString, Type);
void connectNotify(const QMetaMethod& aSignal); void connectNotify(const QMetaMethod& aSignal);
private: private: