2023-05-04 22:31:41 +03:00
|
|
|
#include "chatlistmodel.h"
|
2024-06-04 21:47:11 +03:00
|
|
|
|
|
|
|
#include "database.h" // IWYU pragma: keep
|
2023-06-28 23:05:35 +03:00
|
|
|
#include "mysettings.h"
|
2023-05-04 22:31:41 +03:00
|
|
|
|
|
|
|
#include <QDataStream>
|
2024-06-04 21:47:11 +03:00
|
|
|
#include <QDir>
|
|
|
|
#include <QElapsedTimer>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QGlobalStatic>
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QIODevice>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QString>
|
2024-06-25 01:49:23 +03:00
|
|
|
#include <QStringList>
|
2024-06-04 21:47:11 +03:00
|
|
|
#include <Qt>
|
|
|
|
|
|
|
|
#include <algorithm>
|
2023-05-04 22:31:41 +03:00
|
|
|
|
2023-05-07 01:51:30 +03:00
|
|
|
#define CHAT_FORMAT_MAGIC 0xF5D553CC
|
2024-06-25 01:49:23 +03:00
|
|
|
#define CHAT_FORMAT_VERSION 8
|
2023-05-07 01:51:30 +03:00
|
|
|
|
2023-06-22 22:44:49 +03:00
|
|
|
class MyChatListModel: public ChatListModel { };
|
|
|
|
Q_GLOBAL_STATIC(MyChatListModel, chatListModelInstance)
|
|
|
|
ChatListModel *ChatListModel::globalInstance()
|
|
|
|
{
|
|
|
|
return chatListModelInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
ChatListModel::ChatListModel()
|
2024-07-12 23:15:40 +03:00
|
|
|
: QAbstractListModel(nullptr) {
|
|
|
|
|
|
|
|
QCoreApplication::instance()->installEventFilter(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatListModel::eventFilter(QObject *obj, QEvent *ev)
|
|
|
|
{
|
|
|
|
if (obj == QCoreApplication::instance() && ev->type() == QEvent::LanguageChange)
|
|
|
|
emit dataChanged(index(0, 0), index(m_chats.size() - 1, 0));
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-15 21:09:32 +03:00
|
|
|
|
|
|
|
void ChatListModel::loadChats()
|
2023-05-07 03:01:14 +03:00
|
|
|
{
|
2023-10-11 17:23:33 +03:00
|
|
|
addChat();
|
2023-05-07 03:01:14 +03:00
|
|
|
|
|
|
|
ChatsRestoreThread *thread = new ChatsRestoreThread;
|
2024-05-15 21:09:32 +03:00
|
|
|
connect(thread, &ChatsRestoreThread::chatRestored, this, &ChatListModel::restoreChat, Qt::QueuedConnection);
|
|
|
|
connect(thread, &ChatsRestoreThread::finished, this, &ChatListModel::chatsRestoredFinished, Qt::QueuedConnection);
|
2023-05-07 03:01:14 +03:00
|
|
|
connect(thread, &ChatsRestoreThread::finished, thread, &QObject::deleteLater);
|
|
|
|
thread->start();
|
2023-05-05 19:30:11 +03:00
|
|
|
|
2023-06-28 23:05:35 +03:00
|
|
|
connect(MySettings::globalInstance(), &MySettings::serverChatChanged, this, &ChatListModel::handleServerEnabledChanged);
|
2023-05-16 01:36:41 +03:00
|
|
|
}
|
|
|
|
|
2023-05-04 22:31:41 +03:00
|
|
|
void ChatListModel::removeChatFile(Chat *chat) const
|
|
|
|
{
|
2023-05-14 02:05:35 +03:00
|
|
|
Q_ASSERT(chat != m_serverChat);
|
2023-06-28 23:05:35 +03:00
|
|
|
const QString savePath = MySettings::globalInstance()->modelPath();
|
2023-05-07 01:51:30 +03:00
|
|
|
QFile file(savePath + "/gpt4all-" + chat->id() + ".chat");
|
2023-05-04 22:31:41 +03:00
|
|
|
if (!file.exists())
|
|
|
|
return;
|
|
|
|
bool success = file.remove();
|
|
|
|
if (!success)
|
|
|
|
qWarning() << "ERROR: Couldn't remove chat file:" << file.fileName();
|
|
|
|
}
|
|
|
|
|
2023-06-21 00:14:11 +03:00
|
|
|
ChatSaver::ChatSaver()
|
|
|
|
: QObject(nullptr)
|
|
|
|
{
|
|
|
|
moveToThread(&m_thread);
|
|
|
|
m_thread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatListModel::saveChats()
|
2023-05-04 22:31:41 +03:00
|
|
|
{
|
2023-06-21 00:14:11 +03:00
|
|
|
QVector<Chat*> toSave;
|
2023-05-04 22:31:41 +03:00
|
|
|
for (Chat *chat : m_chats) {
|
2023-05-14 02:05:35 +03:00
|
|
|
if (chat == m_serverChat)
|
|
|
|
continue;
|
2023-10-11 17:23:33 +03:00
|
|
|
if (chat->isNewChat())
|
2023-05-16 01:36:41 +03:00
|
|
|
continue;
|
2023-06-21 00:14:11 +03:00
|
|
|
toSave.append(chat);
|
|
|
|
}
|
2023-06-21 01:30:17 +03:00
|
|
|
if (toSave.isEmpty()) {
|
|
|
|
emit saveChatsFinished();
|
2023-06-21 00:14:11 +03:00
|
|
|
return;
|
2023-06-21 01:30:17 +03:00
|
|
|
}
|
2023-06-21 00:14:11 +03:00
|
|
|
|
|
|
|
ChatSaver *saver = new ChatSaver;
|
|
|
|
connect(this, &ChatListModel::requestSaveChats, saver, &ChatSaver::saveChats, Qt::QueuedConnection);
|
|
|
|
connect(saver, &ChatSaver::saveChatsFinished, this, &ChatListModel::saveChatsFinished, Qt::QueuedConnection);
|
|
|
|
emit requestSaveChats(toSave);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatSaver::saveChats(const QVector<Chat *> &chats)
|
|
|
|
{
|
|
|
|
QElapsedTimer timer;
|
|
|
|
timer.start();
|
2023-06-28 23:05:35 +03:00
|
|
|
const QString savePath = MySettings::globalInstance()->modelPath();
|
2023-06-21 00:14:11 +03:00
|
|
|
for (Chat *chat : chats) {
|
2023-05-05 17:47:05 +03:00
|
|
|
QString fileName = "gpt4all-" + chat->id() + ".chat";
|
2023-10-07 20:54:38 +03:00
|
|
|
QString filePath = savePath + "/" + fileName;
|
|
|
|
QFile originalFile(filePath);
|
|
|
|
QFile tempFile(filePath + ".tmp"); // Temporary file
|
|
|
|
|
|
|
|
bool success = tempFile.open(QIODevice::WriteOnly);
|
2023-05-04 22:31:41 +03:00
|
|
|
if (!success) {
|
2023-10-07 20:54:38 +03:00
|
|
|
qWarning() << "ERROR: Couldn't save chat to temporary file:" << tempFile.fileName();
|
2023-05-04 22:31:41 +03:00
|
|
|
continue;
|
|
|
|
}
|
2023-10-07 20:54:38 +03:00
|
|
|
QDataStream out(&tempFile);
|
2023-05-07 01:51:30 +03:00
|
|
|
|
|
|
|
out << (quint32)CHAT_FORMAT_MAGIC;
|
|
|
|
out << (qint32)CHAT_FORMAT_VERSION;
|
2023-05-07 13:39:32 +03:00
|
|
|
out.setVersion(QDataStream::Qt_6_2);
|
2023-05-07 01:51:30 +03:00
|
|
|
|
2023-05-05 17:47:05 +03:00
|
|
|
qDebug() << "serializing chat" << fileName;
|
2023-05-08 12:52:57 +03:00
|
|
|
if (!chat->serialize(out, CHAT_FORMAT_VERSION)) {
|
2023-10-07 20:54:38 +03:00
|
|
|
qWarning() << "ERROR: Couldn't serialize chat to file:" << tempFile.fileName();
|
|
|
|
tempFile.remove();
|
|
|
|
continue;
|
2023-05-04 22:31:41 +03:00
|
|
|
}
|
2023-10-07 20:54:38 +03:00
|
|
|
|
|
|
|
if (originalFile.exists())
|
|
|
|
originalFile.remove();
|
|
|
|
tempFile.rename(filePath);
|
2023-05-04 22:31:41 +03:00
|
|
|
}
|
2023-10-07 20:54:38 +03:00
|
|
|
|
2023-05-07 03:01:14 +03:00
|
|
|
qint64 elapsedTime = timer.elapsed();
|
|
|
|
qDebug() << "serializing chats took:" << elapsedTime << "ms";
|
2023-06-21 00:14:11 +03:00
|
|
|
emit saveChatsFinished();
|
2023-05-04 22:31:41 +03:00
|
|
|
}
|
|
|
|
|
2023-05-07 03:01:14 +03:00
|
|
|
void ChatsRestoreThread::run()
|
2023-05-04 22:31:41 +03:00
|
|
|
{
|
2023-05-07 03:01:14 +03:00
|
|
|
QElapsedTimer timer;
|
|
|
|
timer.start();
|
2023-05-07 16:20:09 +03:00
|
|
|
struct FileInfo {
|
|
|
|
bool oldFile;
|
|
|
|
qint64 creationDate;
|
|
|
|
QString file;
|
|
|
|
};
|
|
|
|
QList<FileInfo> files;
|
2023-05-07 01:51:30 +03:00
|
|
|
{
|
|
|
|
// Look for any files in the original spot which was the settings config directory
|
|
|
|
QSettings settings;
|
|
|
|
QFileInfo settingsInfo(settings.fileName());
|
|
|
|
QString settingsPath = settingsInfo.absolutePath();
|
|
|
|
QDir dir(settingsPath);
|
|
|
|
dir.setNameFilters(QStringList() << "gpt4all-*.chat");
|
|
|
|
QStringList fileNames = dir.entryList();
|
2023-06-02 22:46:41 +03:00
|
|
|
for (const QString &f : fileNames) {
|
2023-05-07 01:51:30 +03:00
|
|
|
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);
|
2023-05-07 16:20:09 +03:00
|
|
|
FileInfo info;
|
|
|
|
info.oldFile = true;
|
|
|
|
info.file = filePath;
|
|
|
|
in >> info.creationDate;
|
|
|
|
files.append(info);
|
2023-05-07 01:51:30 +03:00
|
|
|
file.close();
|
2023-05-04 22:31:41 +03:00
|
|
|
}
|
2023-05-07 01:51:30 +03:00
|
|
|
}
|
|
|
|
{
|
2023-06-28 23:05:35 +03:00
|
|
|
const QString savePath = MySettings::globalInstance()->modelPath();
|
2023-05-07 01:51:30 +03:00
|
|
|
QDir dir(savePath);
|
|
|
|
dir.setNameFilters(QStringList() << "gpt4all-*.chat");
|
|
|
|
QStringList fileNames = dir.entryList();
|
2023-06-02 22:46:41 +03:00
|
|
|
for (const QString &f : fileNames) {
|
2023-05-07 01:51:30 +03:00
|
|
|
QString filePath = savePath + "/" + 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);
|
|
|
|
// Read and check the header
|
|
|
|
quint32 magic;
|
|
|
|
in >> magic;
|
|
|
|
if (magic != CHAT_FORMAT_MAGIC) {
|
|
|
|
qWarning() << "ERROR: Chat file has bad magic:" << file.fileName();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the version
|
|
|
|
qint32 version;
|
|
|
|
in >> version;
|
2023-05-08 23:50:21 +03:00
|
|
|
if (version < 1) {
|
2023-05-07 01:51:30 +03:00
|
|
|
qWarning() << "ERROR: Chat file has non supported version:" << file.fileName();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-05-08 23:50:21 +03:00
|
|
|
if (version <= 1)
|
2023-05-07 13:39:32 +03:00
|
|
|
in.setVersion(QDataStream::Qt_6_2);
|
2023-05-07 01:51:30 +03:00
|
|
|
|
2023-05-07 16:20:09 +03:00
|
|
|
FileInfo info;
|
|
|
|
info.oldFile = false;
|
|
|
|
info.file = filePath;
|
|
|
|
in >> info.creationDate;
|
|
|
|
files.append(info);
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(files.begin(), files.end(), [](const FileInfo &a, const FileInfo &b) {
|
|
|
|
return a.creationDate > b.creationDate;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (FileInfo &f : files) {
|
2023-10-11 17:23:33 +03:00
|
|
|
QFile file(f.file);
|
|
|
|
bool success = file.open(QIODevice::ReadOnly);
|
|
|
|
if (!success) {
|
|
|
|
qWarning() << "ERROR: Couldn't restore chat from file:" << file.fileName();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QDataStream in(&file);
|
|
|
|
|
|
|
|
qint32 version = 0;
|
|
|
|
if (!f.oldFile) {
|
|
|
|
// Read and check the header
|
|
|
|
quint32 magic;
|
|
|
|
in >> magic;
|
|
|
|
if (magic != CHAT_FORMAT_MAGIC) {
|
|
|
|
qWarning() << "ERROR: Chat file has bad magic:" << file.fileName();
|
2023-05-07 16:20:09 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-10-11 17:23:33 +03:00
|
|
|
// Read the version
|
|
|
|
in >> version;
|
|
|
|
if (version < 1) {
|
|
|
|
qWarning() << "ERROR: Chat file has non supported version:" << file.fileName();
|
|
|
|
continue;
|
2023-05-07 16:20:09 +03:00
|
|
|
}
|
|
|
|
|
2023-10-11 17:23:33 +03:00
|
|
|
if (version <= 1)
|
|
|
|
in.setVersion(QDataStream::Qt_6_2);
|
|
|
|
}
|
2023-05-07 16:20:09 +03:00
|
|
|
|
2023-10-11 17:23:33 +03:00
|
|
|
qDebug() << "deserializing chat" << f.file;
|
|
|
|
|
|
|
|
Chat *chat = new Chat;
|
2024-06-04 21:47:11 +03:00
|
|
|
chat->moveToThread(qGuiApp->thread());
|
2023-10-11 17:23:33 +03:00
|
|
|
if (!chat->deserialize(in, version)) {
|
|
|
|
qWarning() << "ERROR: Couldn't deserialize chat from file:" << file.fileName();
|
|
|
|
} else {
|
|
|
|
emit chatRestored(chat);
|
|
|
|
}
|
|
|
|
if (f.oldFile)
|
|
|
|
file.remove(); // No longer storing in this directory
|
|
|
|
file.close();
|
2023-05-04 22:31:41 +03:00
|
|
|
}
|
2023-05-07 16:20:09 +03:00
|
|
|
|
2023-05-07 03:01:14 +03:00
|
|
|
qint64 elapsedTime = timer.elapsed();
|
|
|
|
qDebug() << "deserializing chats took:" << elapsedTime << "ms";
|
|
|
|
}
|
|
|
|
|
2023-05-07 16:20:09 +03:00
|
|
|
void ChatListModel::restoreChat(Chat *chat)
|
2023-05-07 03:01:14 +03:00
|
|
|
{
|
2023-05-07 16:20:09 +03:00
|
|
|
chat->setParent(this);
|
|
|
|
connect(chat, &Chat::nameChanged, this, &ChatListModel::nameChanged);
|
|
|
|
|
2023-10-11 17:23:33 +03:00
|
|
|
beginInsertRows(QModelIndex(), m_chats.size(), m_chats.size());
|
|
|
|
m_chats.append(chat);
|
|
|
|
endInsertRows();
|
2023-05-07 16:20:09 +03:00
|
|
|
}
|
2023-05-07 03:01:14 +03:00
|
|
|
|
2023-05-07 16:20:09 +03:00
|
|
|
void ChatListModel::chatsRestoredFinished()
|
|
|
|
{
|
2023-05-11 23:46:25 +03:00
|
|
|
addServerChat();
|
2023-05-04 22:31:41 +03:00
|
|
|
}
|
2023-05-11 23:46:25 +03:00
|
|
|
|
|
|
|
void ChatListModel::handleServerEnabledChanged()
|
|
|
|
{
|
2023-06-28 23:05:35 +03:00
|
|
|
if (MySettings::globalInstance()->serverChat() || m_serverChat != m_currentChat)
|
2023-05-11 23:46:25 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
Chat *nextChat = get(0);
|
|
|
|
Q_ASSERT(nextChat && nextChat != m_serverChat);
|
|
|
|
setCurrentChat(nextChat);
|
2023-06-27 18:54:34 +03:00
|
|
|
}
|