mirror of
https://github.com/YACReader/yacreader
synced 2025-05-28 03:10:27 -04:00
Refactor YACReaderLibraries
It includes a new UUID per library (which isn't used yet) and a new class YACReaderLibrary that replaces the old `QMap<QString, QPair<int, QString>>`.
This commit is contained in:
parent
dd411db9ef
commit
be7a16efa6
@ -1,6 +1,29 @@
|
|||||||
#include "yacreader_libraries.h"
|
#include "yacreader_libraries.h"
|
||||||
#include "yacreader_global.h"
|
#include "yacreader_global.h"
|
||||||
|
|
||||||
|
void writeIdToLibraryFolder(const QString &path, const QUuid &id)
|
||||||
|
{
|
||||||
|
QFile file(path + "/.yacreaderlibrary/id");
|
||||||
|
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||||
|
QTextStream stream(&file);
|
||||||
|
stream << id.toString(QUuid::WithoutBraces);
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QUuid readFromLibraryFolder(const QString &path)
|
||||||
|
{
|
||||||
|
QFile file(path + "/.yacreaderlibrary/id");
|
||||||
|
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
|
QTextStream stream(&file);
|
||||||
|
QString id = stream.readLine();
|
||||||
|
file.close();
|
||||||
|
return QUuid(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return QUuid();
|
||||||
|
}
|
||||||
|
|
||||||
YACReaderLibraries::YACReaderLibraries()
|
YACReaderLibraries::YACReaderLibraries()
|
||||||
: QObject()
|
: QObject()
|
||||||
{
|
{
|
||||||
@ -13,20 +36,21 @@ YACReaderLibraries::YACReaderLibraries(const YACReaderLibraries &source)
|
|||||||
|
|
||||||
QList<QString> YACReaderLibraries::getNames()
|
QList<QString> YACReaderLibraries::getNames()
|
||||||
{
|
{
|
||||||
return libraries.keys();
|
QList<QString> names;
|
||||||
|
std::transform(libraries.cbegin(), libraries.cend(), std::back_inserter(names), [](const YACReaderLibrary &library) { return library.getName(); });
|
||||||
|
return names;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString YACReaderLibraries::getPath(const QString &name)
|
QString YACReaderLibraries::getPath(const QString &name)
|
||||||
{
|
{
|
||||||
return libraries.value(name).second;
|
auto library = std::find_if(libraries.cbegin(), libraries.cend(), [name](const YACReaderLibrary &library) { return library.getName() == name; });
|
||||||
|
return library != libraries.cend() ? library->getPath() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
QString YACReaderLibraries::getPath(int id)
|
QString YACReaderLibraries::getPath(int id)
|
||||||
{
|
{
|
||||||
foreach (QString name, libraries.keys())
|
auto library = std::find_if(libraries.cbegin(), libraries.cend(), [id](const YACReaderLibrary &library) { return library.getLegacyId() == id; });
|
||||||
if (libraries.value(name).first == id)
|
return library != libraries.cend() ? library->getPath() : "";
|
||||||
return libraries.value(name).second;
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString YACReaderLibraries::getDBPath(int id)
|
QString YACReaderLibraries::getDBPath(int id)
|
||||||
@ -36,10 +60,8 @@ QString YACReaderLibraries::getDBPath(int id)
|
|||||||
|
|
||||||
QString YACReaderLibraries::getName(int id)
|
QString YACReaderLibraries::getName(int id)
|
||||||
{
|
{
|
||||||
foreach (QString name, libraries.keys())
|
auto library = std::find_if(libraries.cbegin(), libraries.cend(), [id](const YACReaderLibrary &library) { return library.getLegacyId() == id; });
|
||||||
if (libraries.value(name).first == id)
|
return library != libraries.cend() ? library->getName() : "";
|
||||||
return name;
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool YACReaderLibraries::isEmpty()
|
bool YACReaderLibraries::isEmpty()
|
||||||
@ -49,34 +71,33 @@ bool YACReaderLibraries::isEmpty()
|
|||||||
|
|
||||||
bool YACReaderLibraries::contains(const QString &name)
|
bool YACReaderLibraries::contains(const QString &name)
|
||||||
{
|
{
|
||||||
return libraries.contains(name);
|
auto library = std::find_if(libraries.cbegin(), libraries.cend(), [name](const YACReaderLibrary &library) { return library.getName() == name; });
|
||||||
|
return library != libraries.cend();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool YACReaderLibraries::contains(int id)
|
bool YACReaderLibraries::contains(int id)
|
||||||
{
|
{
|
||||||
foreach (QString name, libraries.keys())
|
auto library = std::find_if(libraries.cbegin(), libraries.cend(), [id](const YACReaderLibrary &library) { return library.getLegacyId() == id; });
|
||||||
if (libraries.value(name).first == id)
|
return library != libraries.cend();
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void YACReaderLibraries::remove(const QString &name)
|
void YACReaderLibraries::remove(const QString &name)
|
||||||
{
|
{
|
||||||
libraries.remove(name);
|
auto library = std::find_if(libraries.cbegin(), libraries.cend(), [name](const YACReaderLibrary &library) { return library.getName() == name; });
|
||||||
|
libraries.erase(library);
|
||||||
}
|
}
|
||||||
|
|
||||||
void YACReaderLibraries::rename(const QString &oldName, const QString &newName)
|
void YACReaderLibraries::rename(const QString &oldName, const QString &newName)
|
||||||
{
|
{
|
||||||
if (libraries.contains(oldName)) {
|
auto library = std::find_if(libraries.cbegin(), libraries.cend(), [oldName](const YACReaderLibrary &library) { return library.getName() == oldName; });
|
||||||
QPair<int, QString> value = libraries.value(oldName);
|
libraries.erase(library);
|
||||||
libraries.remove(oldName);
|
libraries.append(YACReaderLibrary(newName, library->getPath(), library->getLegacyId(), library->getId()));
|
||||||
libraries.insert(newName, value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int YACReaderLibraries::getId(const QString &name)
|
int YACReaderLibraries::getId(const QString &name)
|
||||||
{
|
{
|
||||||
return libraries.value(name).first;
|
auto library = std::find_if(libraries.cbegin(), libraries.cend(), [name](const YACReaderLibrary &library) { return library.getName() == name; });
|
||||||
|
return library != libraries.cend() ? library->getLegacyId() : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
YACReaderLibraries &YACReaderLibraries::operator=(const YACReaderLibraries &source)
|
YACReaderLibraries &YACReaderLibraries::operator=(const YACReaderLibraries &source)
|
||||||
@ -85,18 +106,25 @@ YACReaderLibraries &YACReaderLibraries::operator=(const YACReaderLibraries &sour
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
QMap<QString, QPair<int, QString>> YACReaderLibraries::getLibraries()
|
QList<YACReaderLibrary> YACReaderLibraries::getLibraries() const
|
||||||
{
|
{
|
||||||
return libraries;
|
return libraries;
|
||||||
}
|
}
|
||||||
|
|
||||||
void YACReaderLibraries::addLibrary(const QString &name, const QString &path)
|
void YACReaderLibraries::addLibrary(const QString &name, const QString &path)
|
||||||
{
|
{
|
||||||
int newID = 0;
|
int legacyId = 0;
|
||||||
foreach (QString lName, libraries.keys())
|
foreach (YACReaderLibrary l, libraries)
|
||||||
newID = qMax(newID, libraries.value(lName).first);
|
legacyId = qMax(legacyId, l.getLegacyId());
|
||||||
newID++;
|
legacyId++;
|
||||||
libraries.insert(name, QPair<int, QString>(newID, path));
|
|
||||||
|
auto id = readFromLibraryFolder(path);
|
||||||
|
|
||||||
|
if (id.isNull()) {
|
||||||
|
id = QUuid::createUuid();
|
||||||
|
writeIdToLibraryFolder(path, id);
|
||||||
|
}
|
||||||
|
libraries.append(YACReaderLibrary(name, path, legacyId, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
void YACReaderLibraries::load()
|
void YACReaderLibraries::load()
|
||||||
@ -105,28 +133,33 @@ void YACReaderLibraries::load()
|
|||||||
|
|
||||||
if (settings.value(LIBRARIES).isValid()) {
|
if (settings.value(LIBRARIES).isValid()) {
|
||||||
QByteArray data = settings.value(LIBRARIES).toByteArray();
|
QByteArray data = settings.value(LIBRARIES).toByteArray();
|
||||||
QDataStream in(&data, QIODevice::ReadOnly);
|
|
||||||
in >> libraries;
|
|
||||||
} else // only for compatibility with old versions (<7.0)
|
|
||||||
{
|
|
||||||
QFile f(QCoreApplication::applicationDirPath() + "/libraries.yacr");
|
|
||||||
f.open(QIODevice::ReadOnly);
|
|
||||||
QTextStream txtS(&f);
|
|
||||||
QString content = txtS.readAll();
|
|
||||||
QStringList lines = content.split('\n');
|
|
||||||
QString line, name;
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
foreach (line, lines) {
|
// in 9.14 the format of libraries has changeg, so we need to check if we can do a migration
|
||||||
if ((i % 2) == 0)
|
QDataStream legacyIn(&data, QIODevice::ReadOnly);
|
||||||
name = line;
|
QMap<QString, QPair<int, QString>> legacyLibraries;
|
||||||
else
|
legacyIn >> legacyLibraries;
|
||||||
addLibrary(name.trimmed(), line.trimmed());
|
|
||||||
i++;
|
auto needsMigration = !legacyLibraries.isEmpty();
|
||||||
|
if (needsMigration) {
|
||||||
|
QList<YACReaderLibrary> migratedLibraries;
|
||||||
|
|
||||||
|
for (auto i = legacyLibraries.cbegin(), end = legacyLibraries.cend(); i != end; ++i) {
|
||||||
|
auto name = i.key();
|
||||||
|
auto value = i.value();
|
||||||
|
auto newId = QUuid::createUuid();
|
||||||
|
|
||||||
|
writeIdToLibraryFolder(value.second, newId);
|
||||||
|
|
||||||
|
auto library = YACReaderLibrary(name, value.second, value.first, newId);
|
||||||
|
migratedLibraries.append(library);
|
||||||
|
}
|
||||||
|
|
||||||
|
libraries = migratedLibraries;
|
||||||
|
save();
|
||||||
|
} else {
|
||||||
|
QDataStream in(&data, QIODevice::ReadOnly);
|
||||||
|
in >> libraries;
|
||||||
}
|
}
|
||||||
f.close();
|
|
||||||
if (save())
|
|
||||||
f.remove();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,3 +174,59 @@ bool YACReaderLibraries::save()
|
|||||||
|
|
||||||
return settings.isWritable();
|
return settings.isWritable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
YACReaderLibrary::YACReaderLibrary()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
YACReaderLibrary::YACReaderLibrary(const QString &name, const QString &path, int legacyId, const QUuid &id)
|
||||||
|
: name(name), path(path), legacyId(legacyId), id(id)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
QString YACReaderLibrary::getName() const
|
||||||
|
{
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString YACReaderLibrary::getPath() const
|
||||||
|
{
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString YACReaderLibrary::getDBPath() const
|
||||||
|
{
|
||||||
|
return path + "/.yacreaderlibrary";
|
||||||
|
}
|
||||||
|
|
||||||
|
int YACReaderLibrary::getLegacyId() const
|
||||||
|
{
|
||||||
|
return legacyId;
|
||||||
|
}
|
||||||
|
|
||||||
|
QUuid YACReaderLibrary::getId() const
|
||||||
|
{
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool YACReaderLibrary::operator==(const YACReaderLibrary &other) const
|
||||||
|
{
|
||||||
|
return id == other.id && name == other.name && path == other.path && legacyId == other.legacyId;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool YACReaderLibrary::operator!=(const YACReaderLibrary &other) const
|
||||||
|
{
|
||||||
|
return !(*this == other);
|
||||||
|
}
|
||||||
|
|
||||||
|
QDataStream &operator<<(QDataStream &out, const YACReaderLibrary &library)
|
||||||
|
{
|
||||||
|
out << library.name << library.path << library.legacyId << library.id;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
QDataStream &operator>>(QDataStream &in, YACReaderLibrary &library)
|
||||||
|
{
|
||||||
|
in >> library.name >> library.path >> library.legacyId >> library.id;
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include <QtCore>
|
#include <QtCore>
|
||||||
|
|
||||||
|
class YACReaderLibrary;
|
||||||
|
|
||||||
class YACReaderLibraries : public QObject
|
class YACReaderLibraries : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@ -22,15 +24,37 @@ public:
|
|||||||
void rename(const QString &oldName, const QString &newName);
|
void rename(const QString &oldName, const QString &newName);
|
||||||
int getId(const QString &name);
|
int getId(const QString &name);
|
||||||
YACReaderLibraries &operator=(const YACReaderLibraries &source);
|
YACReaderLibraries &operator=(const YACReaderLibraries &source);
|
||||||
QMap<QString, QPair<int, QString>> getLibraries();
|
QList<YACReaderLibrary> getLibraries() const;
|
||||||
public slots:
|
public slots:
|
||||||
void addLibrary(const QString &name, const QString &path);
|
void addLibrary(const QString &name, const QString &path);
|
||||||
void load();
|
void load();
|
||||||
bool save();
|
bool save();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// name <id,path>
|
QList<YACReaderLibrary> libraries;
|
||||||
QMap<QString, QPair<int, QString>> libraries;
|
};
|
||||||
|
|
||||||
|
class YACReaderLibrary
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
YACReaderLibrary();
|
||||||
|
YACReaderLibrary(const QString &name, const QString &path, int legacyId, const QUuid &id);
|
||||||
|
QString getName() const;
|
||||||
|
QString getPath() const;
|
||||||
|
QString getDBPath() const;
|
||||||
|
int getLegacyId() const;
|
||||||
|
QUuid getId() const;
|
||||||
|
bool operator==(const YACReaderLibrary &other) const;
|
||||||
|
bool operator!=(const YACReaderLibrary &other) const;
|
||||||
|
friend QDataStream &operator<<(QDataStream &out, const YACReaderLibrary &library);
|
||||||
|
friend QDataStream &operator>>(QDataStream &in, YACReaderLibrary &library);
|
||||||
|
operator QString() const { return QString("%1 [%2, %3, %4]").arg(name).arg(legacyId).arg(id.toString(QUuid::WithoutBraces).arg(path)); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString name;
|
||||||
|
QString path;
|
||||||
|
int legacyId;
|
||||||
|
QUuid id; // stored in `id` file in .yacreaderlibrary
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // YACREADER_LIBRARIES_H
|
#endif // YACREADER_LIBRARIES_H
|
||||||
|
Loading…
Reference in New Issue
Block a user