summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--filesystemwidget.cpp1
-rw-r--r--smdirmodel.cpp29
-rw-r--r--smdirmodel.h10
-rw-r--r--smdirwatcher.cpp19
-rw-r--r--smdirwatcher.h1
-rw-r--r--smtreeitem.h2
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<int>(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 <QDir>
#include <QDateTime>
#include <QFile>
+#include <QTimer>
#include <sys/stat.h>
@@ -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<QVariant> 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 <smdirwatcher.h>
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<QVariant> 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 <QDebug>
-
void SmDirWatcher::run(){
QMutexLocker lock(&mWatchMx);
- QVarLengthArray<char, 4096> data(ioctl(mFd, FIONREAD) + 1);
+ QVarLengthArray<char, 4096> 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<inotify_event*>(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