2023-05-18 22:59:10 +00:00
|
|
|
#include "localdocs.h"
|
2024-06-04 18:47:11 +00:00
|
|
|
|
|
|
|
#include "database.h"
|
2024-06-24 22:49:23 +00:00
|
|
|
#include "embllm.h"
|
2023-06-29 00:42:40 +00:00
|
|
|
#include "mysettings.h"
|
2023-05-18 22:59:10 +00:00
|
|
|
|
2024-06-04 18:47:11 +00:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QGlobalStatic>
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <Qt>
|
|
|
|
|
2023-05-18 22:59:10 +00:00
|
|
|
class MyLocalDocs: public LocalDocs { };
|
|
|
|
Q_GLOBAL_STATIC(MyLocalDocs, localDocsInstance)
|
|
|
|
LocalDocs *LocalDocs::globalInstance()
|
|
|
|
{
|
|
|
|
return localDocsInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalDocs::LocalDocs()
|
|
|
|
: QObject(nullptr)
|
2023-05-23 02:13:42 +00:00
|
|
|
, m_localDocsModel(new LocalDocsModel(this))
|
2023-05-24 00:26:31 +00:00
|
|
|
, m_database(nullptr)
|
2023-05-18 22:59:10 +00:00
|
|
|
{
|
2023-06-29 00:42:40 +00:00
|
|
|
connect(MySettings::globalInstance(), &MySettings::localDocsChunkSizeChanged, this, &LocalDocs::handleChunkSizeChanged);
|
2024-06-24 22:49:23 +00:00
|
|
|
connect(MySettings::globalInstance(), &MySettings::localDocsFileExtensionsChanged, this, &LocalDocs::handleFileExtensionsChanged);
|
2023-05-24 00:26:31 +00:00
|
|
|
|
|
|
|
// Create the DB with the chunk size from settings
|
2024-06-24 22:49:23 +00:00
|
|
|
m_database = new Database(MySettings::globalInstance()->localDocsChunkSize(),
|
|
|
|
MySettings::globalInstance()->localDocsFileExtensions());
|
2023-05-24 00:26:31 +00:00
|
|
|
|
2024-04-25 17:16:52 +00:00
|
|
|
connect(this, &LocalDocs::requestStart, m_database,
|
|
|
|
&Database::start, Qt::QueuedConnection);
|
2024-06-24 22:49:23 +00:00
|
|
|
connect(this, &LocalDocs::requestForceIndexing, m_database,
|
|
|
|
&Database::forceIndexing, Qt::QueuedConnection);
|
|
|
|
connect(this, &LocalDocs::forceRebuildFolder, m_database,
|
|
|
|
&Database::forceRebuildFolder, Qt::QueuedConnection);
|
2023-05-18 22:59:10 +00:00
|
|
|
connect(this, &LocalDocs::requestAddFolder, m_database,
|
|
|
|
&Database::addFolder, Qt::QueuedConnection);
|
|
|
|
connect(this, &LocalDocs::requestRemoveFolder, m_database,
|
|
|
|
&Database::removeFolder, Qt::QueuedConnection);
|
2023-05-24 00:26:31 +00:00
|
|
|
connect(this, &LocalDocs::requestChunkSizeChange, m_database,
|
|
|
|
&Database::changeChunkSize, Qt::QueuedConnection);
|
2024-06-24 22:49:23 +00:00
|
|
|
connect(this, &LocalDocs::requestFileExtensionsChange, m_database,
|
|
|
|
&Database::changeFileExtensions, Qt::QueuedConnection);
|
|
|
|
connect(m_database, &Database::databaseValidChanged,
|
|
|
|
this, &LocalDocs::databaseValidChanged, Qt::QueuedConnection);
|
2023-10-24 16:13:32 +00:00
|
|
|
|
|
|
|
// Connections for modifying the model and keeping it updated with the database
|
2024-06-24 22:49:23 +00:00
|
|
|
connect(m_database, &Database::requestUpdateGuiForCollectionItem,
|
|
|
|
m_localDocsModel, &LocalDocsModel::updateCollectionItem, Qt::QueuedConnection);
|
|
|
|
connect(m_database, &Database::requestAddGuiCollectionItem,
|
2023-10-24 16:13:32 +00:00
|
|
|
m_localDocsModel, &LocalDocsModel::addCollectionItem, Qt::QueuedConnection);
|
2024-06-24 22:49:23 +00:00
|
|
|
connect(m_database, &Database::requestRemoveGuiFolderById,
|
2023-10-24 16:13:32 +00:00
|
|
|
m_localDocsModel, &LocalDocsModel::removeFolderById, Qt::QueuedConnection);
|
2024-06-24 22:49:23 +00:00
|
|
|
connect(m_database, &Database::requestGuiCollectionListUpdated,
|
2023-10-24 16:13:32 +00:00
|
|
|
m_localDocsModel, &LocalDocsModel::collectionListUpdated, Qt::QueuedConnection);
|
|
|
|
|
2024-06-04 18:47:11 +00:00
|
|
|
connect(qGuiApp, &QCoreApplication::aboutToQuit, this, &LocalDocs::aboutToQuit);
|
2023-10-24 16:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalDocs::aboutToQuit()
|
|
|
|
{
|
|
|
|
delete m_database;
|
|
|
|
m_database = nullptr;
|
2023-05-18 22:59:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalDocs::addFolder(const QString &collection, const QString &path)
|
|
|
|
{
|
2023-05-23 02:13:42 +00:00
|
|
|
const QUrl url(path);
|
2023-06-03 14:08:59 +00:00
|
|
|
const QString localPath = url.isLocalFile() ? url.toLocalFile() : path;
|
2024-06-24 22:49:23 +00:00
|
|
|
|
|
|
|
const QString embedding_model = EmbeddingLLM::model();
|
|
|
|
if (embedding_model.isEmpty()) {
|
|
|
|
qWarning() << "ERROR: We have no embedding model";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit requestAddFolder(collection, localPath, embedding_model);
|
2023-05-18 22:59:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalDocs::removeFolder(const QString &collection, const QString &path)
|
|
|
|
{
|
|
|
|
emit requestRemoveFolder(collection, path);
|
|
|
|
}
|
|
|
|
|
2024-06-24 22:49:23 +00:00
|
|
|
void LocalDocs::forceIndexing(const QString &collection)
|
|
|
|
{
|
|
|
|
const QString embedding_model = EmbeddingLLM::model();
|
|
|
|
if (embedding_model.isEmpty()) {
|
|
|
|
qWarning() << "ERROR: We have no embedding model";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit requestForceIndexing(collection, embedding_model);
|
|
|
|
}
|
|
|
|
|
2023-06-29 00:42:40 +00:00
|
|
|
void LocalDocs::handleChunkSizeChanged()
|
2023-05-24 00:26:31 +00:00
|
|
|
{
|
2023-06-29 00:42:40 +00:00
|
|
|
emit requestChunkSizeChange(MySettings::globalInstance()->localDocsChunkSize());
|
2023-05-18 22:59:10 +00:00
|
|
|
}
|
2024-06-24 22:49:23 +00:00
|
|
|
|
|
|
|
void LocalDocs::handleFileExtensionsChanged()
|
|
|
|
{
|
|
|
|
emit requestFileExtensionsChange(MySettings::globalInstance()->localDocsFileExtensions());
|
|
|
|
}
|