Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[office] Add a flag in the DocumentListModelEntry structure to set if…
… the entry is out of sync with provided entries from tracker (or other sources). Use the new DocumentListModelEntry flag to labelled entries in case of a tracker signal so to avoid the clear() call on model. It should solve the bug #10.
  • Loading branch information
dcaliste committed Mar 21, 2015
1 parent 00e0dc6 commit 54be1d8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
34 changes: 32 additions & 2 deletions models/documentlistmodel.cpp
Expand Up @@ -29,6 +29,7 @@ struct DocumentListModelEntry
QDateTime fileRead;
QString mimeType;
DocumentListModel::DocumentClass documentClass;
bool dirty; // When true, should be removed from list.
};

class DocumentListModel::Private
Expand Down Expand Up @@ -97,15 +98,32 @@ QHash< int, QByteArray > DocumentListModel::roleNames() const
return d->roles;
}

void DocumentListModel::setAllItemsDirty(bool status)
{
for(QList<DocumentListModelEntry>::iterator entry = d->entries.begin();
entry != d->entries.end(); entry++) {
entry->dirty = status;
}
}

void DocumentListModel::addItem(QString name, QString path, QString type, int size, QDateTime lastRead, QString mimeType)
{
// We sometimes get duplicate entries... and that's kind of silly.
foreach(const DocumentListModelEntry& entry, d->entries) {
if(entry.filePath == path)
for(QList<DocumentListModelEntry>::iterator entry = d->entries.begin();
entry != d->entries.end(); entry++) {
if(entry->filePath == path) {
entry->dirty = false;
entry->fileType = type;
entry->fileSize = size;
entry->fileRead = lastRead;
entry->mimeType = mimeType;
entry->documentClass = static_cast<DocumentClass>(mimeTypeToDocumentClass(mimeType));
return;
}
}

DocumentListModelEntry entry;
entry.dirty = false;
entry.fileName = name;
entry.filePath = path;
entry.fileType = type;
Expand All @@ -125,6 +143,18 @@ void DocumentListModel::addItem(QString name, QString path, QString type, int si
endInsertRows();
}

void DocumentListModel::removeItemsDirty()
{
for (int index=0; index < d->entries.count(); index++) {
if (d->entries.at(index).dirty) {
beginRemoveRows(QModelIndex(), index, index);
d->entries.removeAt(index);
endRemoveRows();
}
}
}


void DocumentListModel::removeAt(int index)
{
if(index > -1 && index < d->entries.count())
Expand Down
2 changes: 2 additions & 0 deletions models/documentlistmodel.h
Expand Up @@ -58,7 +58,9 @@ class DocumentListModel : public QAbstractListModel
virtual int rowCount(const QModelIndex& parent) const;
virtual QHash< int, QByteArray > roleNames() const;

void setAllItemsDirty(bool status);
void addItem(QString name, QString path, QString type, int size, QDateTime lastRead, QString mimeType);
void removeItemsDirty();
void removeAt(int index);
void clear();

Expand Down
8 changes: 7 additions & 1 deletion models/trackerdocumentprovider.cpp
Expand Up @@ -113,9 +113,13 @@ void TrackerDocumentProvider::searchFinished()
QSparqlResult* r = qobject_cast<QSparqlResult*>(sender());
if(!r->hasError())
{
d->model->clear();
// d->model->clear();
// Mark all current entries in the model dirty.
d->model->setAllItemsDirty(true);
while(r->next())
{
// This will remove the dirty flag for already
// existing entries.
d->model->addItem(
r->binding(0).value().toString(),
r->binding(1).value().toString(),
Expand All @@ -125,6 +129,8 @@ void TrackerDocumentProvider::searchFinished()
r->binding(4).value().toString()
);
}
// Remove all entries with the dirty mark.
d->model->removeItemsDirty();
}

emit countChanged();
Expand Down

0 comments on commit 54be1d8

Please sign in to comment.