[app] Support for SD-card labels containing spaces

Spaces which are part of the path get replaced with \040 in /proc/mounts
This commit is contained in:
Slava Monich 2018-06-08 17:02:21 +03:00
parent 5ae0aa0db3
commit 7b38eca3f2

View file

@ -285,13 +285,18 @@ class BooksStorageManager::Private : public QObject {
Q_OBJECT Q_OBJECT
public: public:
static BooksStorageManager* gInstance; static BooksStorageManager* gInstance;
static const QString STORAGE_MOUNT_PREFIX;
static const QString STORAGE_MOUNT_PREFIX2; struct MountEntry {
QString dev;
QString path;
bool parse(QString aLine);
bool isMediaMount();
};
Private(BooksStorageManager* aParent); Private(BooksStorageManager* aParent);
~Private(); ~Private();
static bool isMediaMount(QString aPath);
int findDevice(QString aDevice) const; int findDevice(QString aDevice) const;
int findPath(QString aPath, QString* aRelPath) const; int findPath(QString aPath, QString* aRelPath) const;
bool scanMounts(); bool scanMounts();
@ -314,8 +319,30 @@ public:
}; };
BooksStorageManager* BooksStorageManager::Private::gInstance = NULL; BooksStorageManager* BooksStorageManager::Private::gInstance = NULL;
const QString BooksStorageManager::Private::STORAGE_MOUNT_PREFIX("/media/");
const QString BooksStorageManager::Private::STORAGE_MOUNT_PREFIX2("/run/media/"); bool BooksStorageManager::Private::MountEntry::parse(QString aLine)
{
QStringList entries = aLine.split(' ', QString::SkipEmptyParts);
if (entries.count() > 2) {
dev = entries.at(0);
path = entries.at(1);
// If the path contains spaces, those get replaced with \040
static const QString ENCODED_SPACE("\\040");
static const QString SPACE(" ");
path.replace(ENCODED_SPACE, SPACE);
return true;
} else {
dev = path = QString();
return false;
}
}
bool BooksStorageManager::Private::MountEntry::isMediaMount()
{
static const QString MOUNT_PREFIX("/media/");
static const QString MOUNT_PREFIX2("/run/media/nemo/");
return path.startsWith(MOUNT_PREFIX) || path.startsWith(MOUNT_PREFIX2);
}
BooksStorageManager::Private::Private(BooksStorageManager* aParent) : BooksStorageManager::Private::Private(BooksStorageManager* aParent) :
QObject(aParent), QObject(aParent),
@ -340,21 +367,18 @@ BooksStorageManager::Private::Private(BooksStorageManager* aParent) :
// For some reason QTextStream can't read /proc/mounts line by line // For some reason QTextStream can't read /proc/mounts line by line
QByteArray contents = mounts.readAll(); QByteArray contents = mounts.readAll();
QTextStream in(&contents); QTextStream in(&contents);
MountEntry entry;
while (!in.atEnd()) { while (!in.atEnd()) {
QString line = in.readLine(); if (entry.parse(in.readLine())) {
QStringList entries = line.split(' ', QString::SkipEmptyParts); if (entry.path == homeMount) {
if (entries.count() > 2) { homeDevice = entry.dev;
const QString mount(entries.at(1));
if (mount == homeMount) {
homeDevice = entries.at(0);
HDEBUG("internal" << homeDevice); HDEBUG("internal" << homeDevice);
} else if (isMediaMount(mount)) { } else if (entry.isMediaMount()) {
const QString dev = entries.at(0); QString path(entry.path);
QString path(mount);
if (!path.endsWith('/')) path += '/'; if (!path.endsWith('/')) path += '/';
path += iSettings->removableRoot(); path += iSettings->removableRoot();
BooksStorage bs(dev, mount, path, false); BooksStorage bs(entry.dev, entry.path, path, false);
HDEBUG("removable" << dev << bs.booksDir().path()); HDEBUG("removable" << entry.dev << bs.booksDir().path());
iStorageList.append(bs); iStorageList.append(bs);
} }
} }
@ -398,12 +422,6 @@ BooksStorageManager::Private::~Private()
} }
} }
bool BooksStorageManager::Private::isMediaMount(QString aPath)
{
return aPath.startsWith(STORAGE_MOUNT_PREFIX) ||
aPath.startsWith(STORAGE_MOUNT_PREFIX2);
}
int BooksStorageManager::Private::findDevice(QString aDevice) const int BooksStorageManager::Private::findDevice(QString aDevice) const
{ {
const int n = iStorageList.count(); const int n = iStorageList.count();
@ -448,25 +466,19 @@ bool BooksStorageManager::Private::scanMounts()
// For some reason QTextStream can't read /proc/mounts line by line // For some reason QTextStream can't read /proc/mounts line by line
QByteArray contents = mounts.readAll(); QByteArray contents = mounts.readAll();
QTextStream in(&contents); QTextStream in(&contents);
MountEntry entry;
while (!in.atEnd()) { while (!in.atEnd()) {
QString line = in.readLine(); if (entry.parse(in.readLine()) &&
QStringList entries = line.split(' ', QString::SkipEmptyParts); entry.isMediaMount() &&
if (entries.count() > 2) { findDevice(entry.dev) < 0) {
const QString mount(entries.at(1)); QString path(entry.path);
if (isMediaMount(mount)) { if (!path.endsWith('/')) path += '/';
const QString dev = entries.at(0); path += iSettings->removableRoot();
int index = findDevice(dev); HDEBUG("new removable device" << entry.dev << path);
if (index < 0) { BooksStorage storage(entry.dev, entry.path, path, false);
QString path = mount; iStorageList.append(storage);
if (!path.endsWith('/')) path += '/'; Q_EMIT iParent->storageAdded(storage);
path += iSettings->removableRoot(); newStorageFound = true;
HDEBUG("new removable device" << dev << path);
BooksStorage storage(dev, mount, path, false);
iStorageList.append(storage);
Q_EMIT iParent->storageAdded(storage);
newStorageFound = true;
}
}
} }
} }
mounts.close(); mounts.close();