mirror of
https://github.com/YACReader/yacreader
synced 2025-05-27 10:50: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_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()
|
||||
: QObject()
|
||||
{
|
||||
@ -13,20 +36,21 @@ YACReaderLibraries::YACReaderLibraries(const YACReaderLibraries &source)
|
||||
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
foreach (QString name, libraries.keys())
|
||||
if (libraries.value(name).first == id)
|
||||
return libraries.value(name).second;
|
||||
return "";
|
||||
auto library = std::find_if(libraries.cbegin(), libraries.cend(), [id](const YACReaderLibrary &library) { return library.getLegacyId() == id; });
|
||||
return library != libraries.cend() ? library->getPath() : "";
|
||||
}
|
||||
|
||||
QString YACReaderLibraries::getDBPath(int id)
|
||||
@ -36,10 +60,8 @@ QString YACReaderLibraries::getDBPath(int id)
|
||||
|
||||
QString YACReaderLibraries::getName(int id)
|
||||
{
|
||||
foreach (QString name, libraries.keys())
|
||||
if (libraries.value(name).first == id)
|
||||
return name;
|
||||
return "";
|
||||
auto library = std::find_if(libraries.cbegin(), libraries.cend(), [id](const YACReaderLibrary &library) { return library.getLegacyId() == id; });
|
||||
return library != libraries.cend() ? library->getName() : "";
|
||||
}
|
||||
|
||||
bool YACReaderLibraries::isEmpty()
|
||||
@ -49,34 +71,33 @@ bool YACReaderLibraries::isEmpty()
|
||||
|
||||
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)
|
||||
{
|
||||
foreach (QString name, libraries.keys())
|
||||
if (libraries.value(name).first == id)
|
||||
return true;
|
||||
return false;
|
||||
auto library = std::find_if(libraries.cbegin(), libraries.cend(), [id](const YACReaderLibrary &library) { return library.getLegacyId() == id; });
|
||||
return library != libraries.cend();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (libraries.contains(oldName)) {
|
||||
QPair<int, QString> value = libraries.value(oldName);
|
||||
libraries.remove(oldName);
|
||||
libraries.insert(newName, value);
|
||||
}
|
||||
auto library = std::find_if(libraries.cbegin(), libraries.cend(), [oldName](const YACReaderLibrary &library) { return library.getName() == oldName; });
|
||||
libraries.erase(library);
|
||||
libraries.append(YACReaderLibrary(newName, library->getPath(), library->getLegacyId(), library->getId()));
|
||||
}
|
||||
|
||||
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)
|
||||
@ -85,18 +106,25 @@ YACReaderLibraries &YACReaderLibraries::operator=(const YACReaderLibraries &sour
|
||||
return *this;
|
||||
}
|
||||
|
||||
QMap<QString, QPair<int, QString>> YACReaderLibraries::getLibraries()
|
||||
QList<YACReaderLibrary> YACReaderLibraries::getLibraries() const
|
||||
{
|
||||
return libraries;
|
||||
}
|
||||
|
||||
void YACReaderLibraries::addLibrary(const QString &name, const QString &path)
|
||||
{
|
||||
int newID = 0;
|
||||
foreach (QString lName, libraries.keys())
|
||||
newID = qMax(newID, libraries.value(lName).first);
|
||||
newID++;
|
||||
libraries.insert(name, QPair<int, QString>(newID, path));
|
||||
int legacyId = 0;
|
||||
foreach (YACReaderLibrary l, libraries)
|
||||
legacyId = qMax(legacyId, l.getLegacyId());
|
||||
legacyId++;
|
||||
|
||||
auto id = readFromLibraryFolder(path);
|
||||
|
||||
if (id.isNull()) {
|
||||
id = QUuid::createUuid();
|
||||
writeIdToLibraryFolder(path, id);
|
||||
}
|
||||
libraries.append(YACReaderLibrary(name, path, legacyId, id));
|
||||
}
|
||||
|
||||
void YACReaderLibraries::load()
|
||||
@ -105,28 +133,33 @@ void YACReaderLibraries::load()
|
||||
|
||||
if (settings.value(LIBRARIES).isValid()) {
|
||||
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) {
|
||||
if ((i % 2) == 0)
|
||||
name = line;
|
||||
else
|
||||
addLibrary(name.trimmed(), line.trimmed());
|
||||
i++;
|
||||
// in 9.14 the format of libraries has changeg, so we need to check if we can do a migration
|
||||
QDataStream legacyIn(&data, QIODevice::ReadOnly);
|
||||
QMap<QString, QPair<int, QString>> legacyLibraries;
|
||||
legacyIn >> legacyLibraries;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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>
|
||||
|
||||
class YACReaderLibrary;
|
||||
|
||||
class YACReaderLibraries : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -22,15 +24,37 @@ public:
|
||||
void rename(const QString &oldName, const QString &newName);
|
||||
int getId(const QString &name);
|
||||
YACReaderLibraries &operator=(const YACReaderLibraries &source);
|
||||
QMap<QString, QPair<int, QString>> getLibraries();
|
||||
QList<YACReaderLibrary> getLibraries() const;
|
||||
public slots:
|
||||
void addLibrary(const QString &name, const QString &path);
|
||||
void load();
|
||||
bool save();
|
||||
|
||||
private:
|
||||
// name <id,path>
|
||||
QMap<QString, QPair<int, QString>> libraries;
|
||||
QList<YACReaderLibrary> 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
|
||||
|
Loading…
Reference in New Issue
Block a user