2023-05-01 17:13:20 -04:00
|
|
|
#ifndef CHATLISTMODEL_H
|
|
|
|
#define CHATLISTMODEL_H
|
|
|
|
|
|
|
|
#include <QAbstractListModel>
|
|
|
|
#include "chat.h"
|
|
|
|
|
2023-05-06 20:01:14 -04:00
|
|
|
class ChatsRestoreThread : public QThread
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
void run() override;
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
2023-05-07 09:20:09 -04:00
|
|
|
void chatRestored(Chat *chat);
|
2023-05-06 20:01:14 -04:00
|
|
|
};
|
|
|
|
|
2023-06-20 17:14:11 -04:00
|
|
|
class ChatSaver : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit ChatSaver();
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void saveChatsFinished();
|
|
|
|
|
|
|
|
public Q_SLOTS:
|
|
|
|
void saveChats(const QVector<Chat*> &chats);
|
|
|
|
|
|
|
|
private:
|
|
|
|
QThread m_thread;
|
|
|
|
};
|
|
|
|
|
2023-05-01 17:13:20 -04:00
|
|
|
class ChatListModel : public QAbstractListModel
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(int count READ count NOTIFY countChanged)
|
|
|
|
Q_PROPERTY(Chat *currentChat READ currentChat WRITE setCurrentChat NOTIFY currentChatChanged)
|
|
|
|
|
|
|
|
public:
|
2023-06-22 15:44:49 -04:00
|
|
|
static ChatListModel *globalInstance();
|
2023-05-01 17:13:20 -04:00
|
|
|
|
|
|
|
enum Roles {
|
|
|
|
IdRole = Qt::UserRole + 1,
|
|
|
|
NameRole
|
|
|
|
};
|
|
|
|
|
|
|
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent)
|
|
|
|
return m_chats.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
|
|
|
|
{
|
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_chats.size())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
const Chat *item = m_chats.at(index.row());
|
|
|
|
switch (role) {
|
|
|
|
case IdRole:
|
|
|
|
return item->id();
|
|
|
|
case NameRole:
|
|
|
|
return item->name();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> roleNames() const override
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles;
|
|
|
|
roles[IdRole] = "id";
|
|
|
|
roles[NameRole] = "name";
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
2023-05-05 12:30:11 -04:00
|
|
|
bool shouldSaveChats() const;
|
|
|
|
void setShouldSaveChats(bool b);
|
|
|
|
|
2023-05-15 18:36:41 -04:00
|
|
|
bool shouldSaveChatGPTChats() const;
|
|
|
|
void setShouldSaveChatGPTChats(bool b);
|
|
|
|
|
2024-05-15 14:09:32 -04:00
|
|
|
Q_INVOKABLE void loadChats();
|
|
|
|
|
2023-05-02 07:48:40 -04:00
|
|
|
Q_INVOKABLE void addChat()
|
2023-05-01 17:13:20 -04:00
|
|
|
{
|
2024-05-15 14:09:32 -04:00
|
|
|
// Select the existing new chat if we already have one
|
|
|
|
if (m_newChat) {
|
|
|
|
setCurrentChat(m_newChat);
|
2023-05-02 07:48:40 -04:00
|
|
|
return;
|
2024-05-15 14:09:32 -04:00
|
|
|
}
|
2023-05-02 07:48:40 -04:00
|
|
|
|
|
|
|
// Create a new chat pointer and connect it to determine when it is populated
|
|
|
|
m_newChat = new Chat(this);
|
|
|
|
connect(m_newChat->chatModel(), &ChatModel::countChanged,
|
|
|
|
this, &ChatListModel::newChatCountChanged);
|
2023-05-02 11:19:17 -04:00
|
|
|
connect(m_newChat, &Chat::nameChanged,
|
|
|
|
this, &ChatListModel::nameChanged);
|
2023-05-02 07:48:40 -04:00
|
|
|
|
2023-05-01 20:27:07 -04:00
|
|
|
beginInsertRows(QModelIndex(), 0, 0);
|
2023-05-02 07:48:40 -04:00
|
|
|
m_chats.prepend(m_newChat);
|
2023-05-01 17:13:20 -04:00
|
|
|
endInsertRows();
|
|
|
|
emit countChanged();
|
2023-05-02 07:48:40 -04:00
|
|
|
setCurrentChat(m_newChat);
|
2023-05-01 17:13:20 -04:00
|
|
|
}
|
|
|
|
|
2023-05-11 16:46:25 -04:00
|
|
|
Q_INVOKABLE void addServerChat()
|
|
|
|
{
|
|
|
|
// Create a new dummy chat pointer and don't connect it
|
|
|
|
if (m_serverChat)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_serverChat = new Chat(true /*isServer*/, this);
|
|
|
|
beginInsertRows(QModelIndex(), m_chats.size(), m_chats.size());
|
|
|
|
m_chats.append(m_serverChat);
|
|
|
|
endInsertRows();
|
|
|
|
emit countChanged();
|
|
|
|
}
|
|
|
|
|
2023-05-01 17:13:20 -04:00
|
|
|
Q_INVOKABLE void removeChat(Chat* chat)
|
|
|
|
{
|
2023-05-13 19:05:35 -04:00
|
|
|
Q_ASSERT(chat != m_serverChat);
|
2023-05-01 17:13:20 -04:00
|
|
|
if (!m_chats.contains(chat)) {
|
2023-05-04 15:31:41 -04:00
|
|
|
qWarning() << "WARNING: Removing chat failed with id" << chat->id();
|
2023-05-01 17:13:20 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-04 15:31:41 -04:00
|
|
|
removeChatFile(chat);
|
|
|
|
|
2023-05-02 09:07:28 -04:00
|
|
|
if (chat == m_newChat) {
|
|
|
|
m_newChat->disconnect(this);
|
|
|
|
m_newChat = nullptr;
|
|
|
|
}
|
|
|
|
|
2024-03-06 12:59:34 -05:00
|
|
|
chat->markForDeletion();
|
|
|
|
|
2023-05-01 17:13:20 -04:00
|
|
|
const int index = m_chats.indexOf(chat);
|
2023-05-13 19:05:35 -04:00
|
|
|
if (m_chats.count() < 3 /*m_serverChat included*/) {
|
2023-05-01 20:56:53 -04:00
|
|
|
addChat();
|
|
|
|
} else {
|
|
|
|
int nextIndex;
|
2023-05-13 19:05:35 -04:00
|
|
|
if (index == m_chats.count() - 2 /*m_serverChat is last*/)
|
2023-05-01 20:56:53 -04:00
|
|
|
nextIndex = index - 1;
|
|
|
|
else
|
|
|
|
nextIndex = index + 1;
|
|
|
|
Chat *nextChat = get(nextIndex);
|
|
|
|
Q_ASSERT(nextChat);
|
|
|
|
setCurrentChat(nextChat);
|
|
|
|
}
|
2023-05-02 11:26:21 -04:00
|
|
|
|
|
|
|
const int newIndex = m_chats.indexOf(chat);
|
|
|
|
beginRemoveRows(QModelIndex(), newIndex, newIndex);
|
2023-05-01 17:13:20 -04:00
|
|
|
m_chats.removeAll(chat);
|
|
|
|
endRemoveRows();
|
2023-05-13 19:05:35 -04:00
|
|
|
chat->unloadAndDeleteLater();
|
2023-05-01 17:13:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Chat *currentChat() const
|
|
|
|
{
|
|
|
|
return m_currentChat;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setCurrentChat(Chat *chat)
|
|
|
|
{
|
|
|
|
if (!m_chats.contains(chat)) {
|
2023-05-04 15:31:41 -04:00
|
|
|
qWarning() << "ERROR: Setting current chat failed with id" << chat->id();
|
2023-05-01 17:13:20 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-13 19:33:19 -04:00
|
|
|
if (m_currentChat && m_currentChat != m_serverChat)
|
2023-05-06 20:01:14 -04:00
|
|
|
m_currentChat->unloadModel();
|
2023-05-01 17:13:20 -04:00
|
|
|
m_currentChat = chat;
|
|
|
|
emit currentChatChanged();
|
2024-02-07 09:37:59 -05:00
|
|
|
if (!m_currentChat->isModelLoaded() && m_currentChat != m_serverChat)
|
|
|
|
m_currentChat->trySwitchContextOfLoadedModel();
|
2023-05-01 17:13:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Q_INVOKABLE Chat* get(int index)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= m_chats.size()) return nullptr;
|
|
|
|
return m_chats.at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int count() const { return m_chats.size(); }
|
|
|
|
|
2024-03-06 16:42:59 -05:00
|
|
|
// stop ChatLLM threads for clean shutdown
|
2024-05-15 14:07:03 -04:00
|
|
|
void destroyChats()
|
|
|
|
{
|
|
|
|
for (auto *chat: m_chats) { chat->destroy(); }
|
|
|
|
ChatLLM::destroyStore();
|
|
|
|
}
|
2024-01-31 14:17:44 -05:00
|
|
|
|
2023-05-04 15:31:41 -04:00
|
|
|
void removeChatFile(Chat *chat) const;
|
2023-06-20 17:14:11 -04:00
|
|
|
Q_INVOKABLE void saveChats();
|
2023-05-07 09:20:09 -04:00
|
|
|
void restoreChat(Chat *chat);
|
|
|
|
void chatsRestoredFinished();
|
2023-05-04 15:31:41 -04:00
|
|
|
|
2023-05-11 16:46:25 -04:00
|
|
|
public Q_SLOTS:
|
|
|
|
void handleServerEnabledChanged();
|
|
|
|
|
2023-05-01 17:13:20 -04:00
|
|
|
Q_SIGNALS:
|
|
|
|
void countChanged();
|
|
|
|
void currentChatChanged();
|
2023-06-20 17:14:11 -04:00
|
|
|
void chatsSavedFinished();
|
|
|
|
void requestSaveChats(const QVector<Chat*> &);
|
|
|
|
void saveChatsFinished();
|
2023-05-01 17:13:20 -04:00
|
|
|
|
2023-05-02 07:48:40 -04:00
|
|
|
private Q_SLOTS:
|
|
|
|
void newChatCountChanged()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_newChat && m_newChat->chatModel()->count());
|
2023-05-02 11:19:17 -04:00
|
|
|
m_newChat->chatModel()->disconnect(this);
|
2023-05-02 07:48:40 -04:00
|
|
|
m_newChat = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-05-02 11:19:17 -04:00
|
|
|
void nameChanged()
|
|
|
|
{
|
|
|
|
Chat *chat = qobject_cast<Chat *>(sender());
|
|
|
|
if (!chat)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int row = m_chats.indexOf(chat);
|
|
|
|
if (row < 0 || row >= m_chats.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QModelIndex index = createIndex(row, 0);
|
|
|
|
emit dataChanged(index, index, {NameRole});
|
|
|
|
}
|
|
|
|
|
2023-05-02 11:26:21 -04:00
|
|
|
void printChats()
|
|
|
|
{
|
|
|
|
for (auto c : m_chats) {
|
|
|
|
qDebug() << c->name()
|
|
|
|
<< (c == m_currentChat ? "currentChat: true" : "currentChat: false")
|
|
|
|
<< (c == m_newChat ? "newChat: true" : "newChat: false");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-01 17:13:20 -04:00
|
|
|
private:
|
2023-12-09 12:55:46 -05:00
|
|
|
Chat* m_newChat = nullptr;
|
|
|
|
Chat* m_serverChat = nullptr;
|
|
|
|
Chat* m_currentChat = nullptr;
|
2023-05-01 17:13:20 -04:00
|
|
|
QList<Chat*> m_chats;
|
2023-06-22 15:44:49 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
explicit ChatListModel();
|
|
|
|
~ChatListModel() {}
|
|
|
|
friend class MyChatListModel;
|
2023-05-01 17:13:20 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CHATITEMMODEL_H
|