2023-05-22 22:13:42 -04:00
|
|
|
#include "localdocsmodel.h"
|
|
|
|
|
2023-10-24 12:13:32 -04:00
|
|
|
#include "localdocs.h"
|
|
|
|
|
|
|
|
LocalDocsCollectionsModel::LocalDocsCollectionsModel(QObject *parent)
|
|
|
|
: QSortFilterProxyModel(parent)
|
|
|
|
{
|
|
|
|
setSourceModel(LocalDocs::globalInstance()->localDocsModel());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalDocsCollectionsModel::filterAcceptsRow(int sourceRow,
|
|
|
|
const QModelIndex &sourceParent) const
|
|
|
|
{
|
|
|
|
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
|
|
|
|
const QString collection = sourceModel()->data(index, LocalDocsModel::CollectionRole).toString();
|
|
|
|
return m_collections.contains(collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalDocsCollectionsModel::setCollections(const QList<QString> &collections)
|
|
|
|
{
|
|
|
|
m_collections = collections;
|
|
|
|
invalidateFilter();
|
|
|
|
}
|
|
|
|
|
2023-05-22 22:13:42 -04:00
|
|
|
LocalDocsModel::LocalDocsModel(QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int LocalDocsModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
return m_collectionList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant LocalDocsModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_collectionList.size())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
const CollectionItem item = m_collectionList.at(index.row());
|
|
|
|
switch (role) {
|
|
|
|
case CollectionRole:
|
|
|
|
return item.collection;
|
|
|
|
case FolderPathRole:
|
|
|
|
return item.folder_path;
|
2023-06-03 10:08:59 -04:00
|
|
|
case InstalledRole:
|
|
|
|
return item.installed;
|
2023-10-24 12:13:32 -04:00
|
|
|
case IndexingRole:
|
|
|
|
return item.indexing;
|
2024-01-22 12:36:01 -05:00
|
|
|
case ErrorRole:
|
|
|
|
return item.error;
|
2023-10-24 12:13:32 -04:00
|
|
|
case CurrentDocsToIndexRole:
|
|
|
|
return item.currentDocsToIndex;
|
|
|
|
case TotalDocsToIndexRole:
|
|
|
|
return item.totalDocsToIndex;
|
|
|
|
case CurrentBytesToIndexRole:
|
|
|
|
return quint64(item.currentBytesToIndex);
|
|
|
|
case TotalBytesToIndexRole:
|
|
|
|
return quint64(item.totalBytesToIndex);
|
2024-01-22 12:36:01 -05:00
|
|
|
case CurrentEmbeddingsToIndexRole:
|
|
|
|
return quint64(item.currentEmbeddingsToIndex);
|
|
|
|
case TotalEmbeddingsToIndexRole:
|
|
|
|
return quint64(item.totalEmbeddingsToIndex);
|
2023-05-22 22:13:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> LocalDocsModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles;
|
|
|
|
roles[CollectionRole] = "collection";
|
|
|
|
roles[FolderPathRole] = "folder_path";
|
2023-06-03 10:08:59 -04:00
|
|
|
roles[InstalledRole] = "installed";
|
2023-10-24 12:13:32 -04:00
|
|
|
roles[IndexingRole] = "indexing";
|
2024-01-22 12:36:01 -05:00
|
|
|
roles[ErrorRole] = "error";
|
2023-10-24 12:13:32 -04:00
|
|
|
roles[CurrentDocsToIndexRole] = "currentDocsToIndex";
|
|
|
|
roles[TotalDocsToIndexRole] = "totalDocsToIndex";
|
|
|
|
roles[CurrentBytesToIndexRole] = "currentBytesToIndex";
|
|
|
|
roles[TotalBytesToIndexRole] = "totalBytesToIndex";
|
2024-01-22 12:36:01 -05:00
|
|
|
roles[CurrentEmbeddingsToIndexRole] = "currentEmbeddingsToIndex";
|
|
|
|
roles[TotalEmbeddingsToIndexRole] = "totalEmbeddingsToIndex";
|
2023-05-22 22:13:42 -04:00
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
2023-11-15 12:14:05 -05:00
|
|
|
template<typename T>
|
|
|
|
void LocalDocsModel::updateField(int folder_id, T value,
|
|
|
|
const std::function<void(CollectionItem&, T)>& updater,
|
|
|
|
const QVector<int>& roles)
|
2023-10-24 12:13:32 -04:00
|
|
|
{
|
|
|
|
for (int i = 0; i < m_collectionList.size(); ++i) {
|
|
|
|
if (m_collectionList.at(i).folder_id != folder_id)
|
|
|
|
continue;
|
|
|
|
|
2023-11-15 12:14:05 -05:00
|
|
|
updater(m_collectionList[i], value);
|
|
|
|
emit dataChanged(this->index(i), this->index(i), roles);
|
2023-10-24 12:13:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 12:14:05 -05:00
|
|
|
void LocalDocsModel::updateInstalled(int folder_id, bool b)
|
2023-10-24 12:13:32 -04:00
|
|
|
{
|
2023-11-15 12:14:05 -05:00
|
|
|
updateField<bool>(folder_id, b,
|
|
|
|
[](CollectionItem& item, bool val) { item.installed = val; }, {InstalledRole});
|
|
|
|
}
|
2023-10-24 12:13:32 -04:00
|
|
|
|
2023-11-15 12:14:05 -05:00
|
|
|
void LocalDocsModel::updateIndexing(int folder_id, bool b)
|
|
|
|
{
|
|
|
|
updateField<bool>(folder_id, b,
|
|
|
|
[](CollectionItem& item, bool val) { item.indexing = val; }, {IndexingRole});
|
2023-10-24 12:13:32 -04:00
|
|
|
}
|
|
|
|
|
2024-01-22 12:36:01 -05:00
|
|
|
void LocalDocsModel::updateError(int folder_id, const QString &error)
|
|
|
|
{
|
|
|
|
updateField<QString>(folder_id, error,
|
|
|
|
[](CollectionItem& item, QString val) { item.error = val; }, {ErrorRole});
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:13:32 -04:00
|
|
|
void LocalDocsModel::updateCurrentDocsToIndex(int folder_id, size_t currentDocsToIndex)
|
|
|
|
{
|
2023-11-15 12:14:05 -05:00
|
|
|
updateField<size_t>(folder_id, currentDocsToIndex,
|
|
|
|
[](CollectionItem& item, size_t val) { item.currentDocsToIndex = val; }, {CurrentDocsToIndexRole});
|
2023-10-24 12:13:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalDocsModel::updateTotalDocsToIndex(int folder_id, size_t totalDocsToIndex)
|
|
|
|
{
|
2023-11-15 12:14:05 -05:00
|
|
|
updateField<size_t>(folder_id, totalDocsToIndex,
|
|
|
|
[](CollectionItem& item, size_t val) { item.totalDocsToIndex = val; }, {TotalDocsToIndexRole});
|
2023-10-24 12:13:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalDocsModel::subtractCurrentBytesToIndex(int folder_id, size_t subtractedBytes)
|
|
|
|
{
|
2023-11-15 12:14:05 -05:00
|
|
|
updateField<size_t>(folder_id, subtractedBytes,
|
|
|
|
[](CollectionItem& item, size_t val) { item.currentBytesToIndex -= val; }, {CurrentBytesToIndexRole});
|
2023-10-24 12:13:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalDocsModel::updateCurrentBytesToIndex(int folder_id, size_t currentBytesToIndex)
|
|
|
|
{
|
2023-11-15 12:14:05 -05:00
|
|
|
updateField<size_t>(folder_id, currentBytesToIndex,
|
|
|
|
[](CollectionItem& item, size_t val) { item.currentBytesToIndex = val; }, {CurrentBytesToIndexRole});
|
2023-10-24 12:13:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalDocsModel::updateTotalBytesToIndex(int folder_id, size_t totalBytesToIndex)
|
|
|
|
{
|
2023-11-15 12:14:05 -05:00
|
|
|
updateField<size_t>(folder_id, totalBytesToIndex,
|
|
|
|
[](CollectionItem& item, size_t val) { item.totalBytesToIndex = val; }, {TotalBytesToIndexRole});
|
2023-10-24 12:13:32 -04:00
|
|
|
}
|
|
|
|
|
2024-01-22 12:36:01 -05:00
|
|
|
void LocalDocsModel::updateCurrentEmbeddingsToIndex(int folder_id, size_t currentEmbeddingsToIndex)
|
|
|
|
{
|
|
|
|
updateField<size_t>(folder_id, currentEmbeddingsToIndex,
|
|
|
|
[](CollectionItem& item, size_t val) { item.currentEmbeddingsToIndex += val; }, {CurrentEmbeddingsToIndexRole});
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalDocsModel::updateTotalEmbeddingsToIndex(int folder_id, size_t totalEmbeddingsToIndex)
|
|
|
|
{
|
|
|
|
updateField<size_t>(folder_id, totalEmbeddingsToIndex,
|
|
|
|
[](CollectionItem& item, size_t val) { item.totalEmbeddingsToIndex += val; }, {TotalEmbeddingsToIndexRole});
|
|
|
|
}
|
|
|
|
|
2023-06-03 10:08:59 -04:00
|
|
|
void LocalDocsModel::addCollectionItem(const CollectionItem &item)
|
|
|
|
{
|
|
|
|
beginInsertRows(QModelIndex(), m_collectionList.size(), m_collectionList.size());
|
|
|
|
m_collectionList.append(item);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:13:32 -04:00
|
|
|
void LocalDocsModel::removeFolderById(int folder_id)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_collectionList.size();) {
|
|
|
|
if (m_collectionList.at(i).folder_id == folder_id) {
|
|
|
|
beginRemoveRows(QModelIndex(), i, i);
|
|
|
|
m_collectionList.removeAt(i);
|
|
|
|
endRemoveRows();
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalDocsModel::removeCollectionPath(const QString &name, const QString &path)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_collectionList.size();) {
|
|
|
|
if (m_collectionList.at(i).collection == name && m_collectionList.at(i).folder_path == path) {
|
|
|
|
beginRemoveRows(QModelIndex(), i, i);
|
|
|
|
m_collectionList.removeAt(i);
|
|
|
|
endRemoveRows();
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalDocsModel::removeCollectionItem(const QString &collectionName)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_collectionList.size();) {
|
|
|
|
if (m_collectionList.at(i).collection == collectionName) {
|
|
|
|
beginRemoveRows(QModelIndex(), i, i);
|
|
|
|
m_collectionList.removeAt(i);
|
|
|
|
endRemoveRows();
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalDocsModel::collectionListUpdated(const QList<CollectionItem> &collectionList)
|
2023-05-22 22:13:42 -04:00
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
m_collectionList = collectionList;
|
|
|
|
endResetModel();
|
2023-05-25 11:16:01 -04:00
|
|
|
}
|