From 744c94e9a1197068457cc52dde57472cb89e1819 Mon Sep 17 00:00:00 2001 From: Arno Date: Sun, 17 Mar 2013 02:18:31 +0100 Subject: Make SmDirModel/SmDirWatcher do something I think I got it working! It does what I want it to do :) --- filesystemwidget.cpp | 1 + smdirmodel.cpp | 29 +++++++++++++++++++++++++++-- smdirmodel.h | 10 +++++++--- smdirwatcher.cpp | 19 ++++++++++--------- smdirwatcher.h | 1 + smtreeitem.h | 2 +- 6 files changed, 47 insertions(+), 15 deletions(-) diff --git a/filesystemwidget.cpp b/filesystemwidget.cpp index e4363f0..f91955e 100644 --- a/filesystemwidget.cpp +++ b/filesystemwidget.cpp @@ -74,6 +74,7 @@ FilesystemWidget::FilesystemWidget(QWidget *parent) : QWidget(parent), mClipboar mFileView->setEditTriggers(QAbstractItemView::NoEditTriggers); mFileView->setAlternatingRowColors(true); mFileView->setColumnHidden(static_cast(SmDirModel::FullPath), true); + mFileProxy->setDynamicSortFilter(true); connect(mFileView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), mFileView, SLOT(selectedFilesChanged())); mPicViewer = SmGlobals::instance()->pictureViewer(); diff --git a/smdirmodel.cpp b/smdirmodel.cpp index 0a0578b..9b35706 100644 --- a/smdirmodel.cpp +++ b/smdirmodel.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include @@ -18,10 +19,17 @@ SmDirModel::SmDirModel(const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent), mHeaders(headers){ mWatch = new SmDirWatcher(this); + connect(mWatch, SIGNAL(dwEvent(QString,int)), this, SLOT(dirEvent(QString,int))); + connect(mWatch, SIGNAL(needRefresh()), this, SLOT(populate())); + mRunTimer = new QTimer(this); + mRunTimer->setInterval(2000); + connect(mRunTimer, SIGNAL(timeout()), mWatch, SLOT(start())); + mRunTimer->start(); } SmDirModel::~SmDirModel(){ delete mWatch; + delete mRunTimer; } QVariant SmDirModel::data(const QModelIndex &index, int role) const{ @@ -65,9 +73,8 @@ bool SmDirModel::setData(const QModelIndex &index, const QVariant &value, int ro QString dir = fileInfo(index).absolutePath(); QString newPath = QString("%1/%2").arg(dir).arg(newName); QFile::rename(old, newPath); - return true; } - return false; + return SmTreeModel::setData(index, value, role); } bool SmDirModel::isDir(const QModelIndex &idx) const { @@ -97,6 +104,24 @@ void SmDirModel::setDir(const QString &dir){ mWatch->setDir(dir); } +void SmDirModel::dirEvent(const QString &file, int e){ + QFileInfo fi(file); + const QList fData = fileData(fi); + if(e == SmDirWatcher::Added){ + addRow(fData, rootIndex(), true); + } + QModelIndex idx = find(fData.at(Name), Name, rootIndex()); + if(e == SmDirWatcher::Deleted){ + removeRow(idx.row()); + } + if(e == SmDirWatcher::Modified){ + for(int i = 0; i < mHeaders.count(); ++i){ + QModelIndex c = index(idx.row(), i, QModelIndex()); + setData(c, fData.at(i), Qt::EditRole); + } + } +} + void SmDirModel::populate(){ SmTreeItem *root = new SmTreeItem(mHeaders.size()); QDir d = QDir(mCur); diff --git a/smdirmodel.h b/smdirmodel.h index 5e1dceb..690efac 100644 --- a/smdirmodel.h +++ b/smdirmodel.h @@ -18,6 +18,8 @@ #include class SmDirWatcher; +class QTimer; +class SmTreeItem; class SmDirModel : public SmTreeModel { Q_OBJECT @@ -34,15 +36,17 @@ class SmDirModel : public SmTreeModel { public slots: void setDir(const QString &dir); + void dirEvent(const QString &file, int e); - private: + private slots: void populate(); + + private: const QList fileData(const QFileInfo &fi) const; SmDirWatcher *mWatch; QStringList mHeaders; QString mCur; - - + QTimer *mRunTimer; }; #endif // SMDIRMODEL_H diff --git a/smdirwatcher.cpp b/smdirwatcher.cpp index a216af5..78dae2a 100644 --- a/smdirwatcher.cpp +++ b/smdirwatcher.cpp @@ -38,15 +38,13 @@ void SmDirWatcher::setDir(const QString &dir){ * since we're only watching one directory at all times * don't care about the other events */ - mDescr = inotify_add_watch(mFd, qPrintable(dir), IN_CLOSE_WRITE | IN_CREATE | IN_DELETE | IN_MODIFY); + mDescr = inotify_add_watch(mFd, qPrintable(dir), IN_CLOSE_WRITE | IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_FROM); mCurrent = dir; } -#include - void SmDirWatcher::run(){ QMutexLocker lock(&mWatchMx); - QVarLengthArray data(ioctl(mFd, FIONREAD) + 1); + QVarLengthArray data(4096); int r = read(mFd, data.data(), data.size()); while(r < 0){ int err = errno; @@ -65,19 +63,22 @@ void SmDirWatcher::run(){ char *end = at + data.size(); while(at < end){ inotify_event *e = reinterpret_cast(at); - qDebug() << "inotify:" << e->name; if(e->mask & IN_IGNORED){ at += sizeof(inotify_event) + e->len; continue; } + QString name = QString("%1/%2").arg(mCurrent).arg(e->name); if(e->mask & IN_CREATE){ - emit dwEvent(e->name, Added); + emit dwEvent(name, Added); } if(e->mask & IN_DELETE){ - emit dwEvent(e->name, Deleted); + emit dwEvent(name, Deleted); + } + if(e->mask & IN_CLOSE_WRITE || e->mask & IN_MODIFY){ + emit dwEvent(name, Modified); } - if((e->mask & IN_CLOSE_WRITE) || (e->mask & IN_MODIFY)){ - emit dwEvent(e->name, Modified); + if(e->mask & IN_MOVED_FROM){ + emit needRefresh(); } at += sizeof(inotify_event) + e->len; } diff --git a/smdirwatcher.h b/smdirwatcher.h index 656d677..0da5b4b 100644 --- a/smdirwatcher.h +++ b/smdirwatcher.h @@ -20,6 +20,7 @@ class SmDirWatcher : public QThread { signals: void dwEvent(const QString& file, int event); + void needRefresh(); public slots: void run(); diff --git a/smtreeitem.h b/smtreeitem.h index 61e121e..a048714 100644 --- a/smtreeitem.h +++ b/smtreeitem.h @@ -1,4 +1,4 @@ -/* + /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version -- cgit v1.2.3-70-g09d2