mirror of
https://github.com/nomic-ai/gpt4all
synced 2024-11-06 09:20:33 +00:00
76 lines
2.5 KiB
C++
76 lines
2.5 KiB
C++
#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) {
|
|
QString fileName = "gpt4all-" + chat->id() + ".chat";
|
|
QFile file(settingsPath + "/" + fileName);
|
|
bool success = file.open(QIODevice::WriteOnly);
|
|
if (!success) {
|
|
qWarning() << "ERROR: Couldn't save chat to file:" << file.fileName();
|
|
continue;
|
|
}
|
|
QDataStream out(&file);
|
|
qDebug() << "serializing chat" << fileName;
|
|
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);
|
|
}
|
|
qDebug() << "deserializing chat" << f;
|
|
file.close();
|
|
}
|
|
std::sort(m_chats.begin(), m_chats.end(), [](const Chat* a, const Chat* b) {
|
|
return a->creationDate() > b->creationDate();
|
|
});
|
|
endResetModel();
|
|
}
|