2023-05-04 22:31:41 +03:00
|
|
|
#include "chatlistmodel.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QDataStream>
|
|
|
|
|
|
|
|
void ChatListModel::removeChatFile(Chat *chat) const
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
QFileInfo settingsInfo(settings.fileName());
|
|
|
|
QString settingsPath = settingsInfo.absolutePath();
|
|
|
|
QFile file(settingsPath + "/gpt4all-" + chat->id() + ".chat");
|
|
|
|
if (!file.exists())
|
|
|
|
return;
|
|
|
|
bool success = file.remove();
|
|
|
|
if (!success)
|
|
|
|
qWarning() << "ERROR: Couldn't remove chat file:" << file.fileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatListModel::saveChats() const
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
QFileInfo settingsInfo(settings.fileName());
|
|
|
|
QString settingsPath = settingsInfo.absolutePath();
|
|
|
|
for (Chat *chat : m_chats) {
|
2023-05-05 17:47:05 +03:00
|
|
|
QString fileName = "gpt4all-" + chat->id() + ".chat";
|
|
|
|
QFile file(settingsPath + "/" + fileName);
|
2023-05-04 22:31:41 +03:00
|
|
|
bool success = file.open(QIODevice::WriteOnly);
|
|
|
|
if (!success) {
|
|
|
|
qWarning() << "ERROR: Couldn't save chat to file:" << file.fileName();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QDataStream out(&file);
|
2023-05-05 17:47:05 +03:00
|
|
|
qDebug() << "serializing chat" << fileName;
|
2023-05-04 22:31:41 +03:00
|
|
|
if (!chat->serialize(out)) {
|
|
|
|
qWarning() << "ERROR: Couldn't serialize chat to file:" << file.fileName();
|
|
|
|
file.remove();
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatListModel::restoreChats()
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
QFileInfo settingsInfo(settings.fileName());
|
|
|
|
QString settingsPath = settingsInfo.absolutePath();
|
|
|
|
QDir dir(settingsPath);
|
|
|
|
dir.setNameFilters(QStringList() << "gpt4all-*.chat");
|
|
|
|
QStringList fileNames = dir.entryList();
|
|
|
|
beginResetModel();
|
|
|
|
for (QString f : fileNames) {
|
|
|
|
QString filePath = settingsPath + "/" + f;
|
|
|
|
QFile file(filePath);
|
|
|
|
bool success = file.open(QIODevice::ReadOnly);
|
|
|
|
if (!success) {
|
|
|
|
qWarning() << "ERROR: Couldn't restore chat from file:" << file.fileName();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QDataStream in(&file);
|
|
|
|
Chat *chat = new Chat(this);
|
|
|
|
if (!chat->deserialize(in)) {
|
|
|
|
qWarning() << "ERROR: Couldn't deserialize chat from file:" << file.fileName();
|
|
|
|
file.remove();
|
|
|
|
} else {
|
|
|
|
connect(chat, &Chat::nameChanged, this, &ChatListModel::nameChanged);
|
|
|
|
m_chats.append(chat);
|
|
|
|
}
|
2023-05-05 17:47:05 +03:00
|
|
|
qDebug() << "deserializing chat" << f;
|
2023-05-04 22:31:41 +03:00
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
std::sort(m_chats.begin(), m_chats.end(), [](const Chat* a, const Chat* b) {
|
|
|
|
return a->creationDate() > b->creationDate();
|
|
|
|
});
|
|
|
|
endResetModel();
|
|
|
|
}
|