summaryrefslogtreecommitdiffstats
path: root/smdirmodel.cpp
blob: b6bc85359bcfd2f7c0a79f02b1f3e29c4bbf757d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
  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
  2 of the License, or (at your option) any later version.
*/

#include <QDateTime>
#include <QTimer>
#include <QSettings>

#include "smdirmodel.h"
#include "smdirwatcher.h"
#include "smtreeitem.h"
#include "smglobals.h"
#include "helper.h"

SmDirModel::SmDirModel(const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent), mHeaders(headers), mExpensiveOps(true){
    mWatch = new SmDirWatcher(NumFields, this);
    connect(mWatch, SIGNAL(needRefresh()), this, SLOT(refresh()));
    mRunTimer = new QTimer(this);
    mRunTimer->setInterval(2000);
    connect(mRunTimer, SIGNAL(timeout()), mWatch, SLOT(start()));
    mRunTimer->start();
    mRefreshTimer = new QTimer(this);
    readSettings();

    connect(mWatch, SIGNAL(population(SmTreeItem*)), this, SLOT(populate(SmTreeItem*)));
    connect(mWatch, SIGNAL(newData(QList<QVariant>,int)), this, SLOT(dirEvent(QList<QVariant>,int)));
}

SmDirModel::~SmDirModel(){
    if(mWatch->isRunning()){
        mWatch->stop();
    }
    mWatch->deleteLater();
}

QVariant SmDirModel::data(const QModelIndex &index, int role) const{
    if(!index.isValid()){
        return QVariant();
    }
    SmTreeItem *i = itemAt(index);
    switch (role){
        case NameRole:
            return i->data(Name);
        case SizeRole:
            return i->data(Size);
        case TypeRole:
            return i->data(Type);
        case CreatedRole:
            return i->data(Created);
        case Md5sumRole:
            return i->data(Md5sum);
        case DurSizeRole:
            return i->data(DurSize);
        case BitrateRole:
            return i->data(Bitrate);
        case FullPathRole:
            return i->data(FullPath);
        case PresentRole:
            return i->data(Present);
        case Qt::DecorationRole: {
            if(index.column() == 0){
                QFileInfo fi(i->data(FullPath).toString());
                if(fi.isDir()){
                    return mIcons.value("folder");
                }
                if(fi.isFile()){
                    return mIcons.value("file");
                }
            }
            return QVariant();
        }
        case Qt::ForegroundRole:
            if(index.column() == 0){
                if((i->data(Present).toInt() == InFiles) || (i->data(Present).toInt() == InPictures)){
                    return QVariant(QColor(Qt::darkGreen));
                }
                else if(i->data(Present).toInt() == InOrigin){
                    return QVariant(QColor(Qt::darkRed));
                }else{
                    return SmTreeModel::data(index, role);
                }
            }
            return SmTreeModel::data(index, role);
        default:
            return SmTreeModel::data(index, role);
    }
}

bool SmDirModel::setData(const QModelIndex &index, const QVariant &value, int role){
    if(!index.isValid()){
        return false;
    }
    if(role == Qt::EditRole && index.column() == Name){
        //this is a rename
        QString newName = value.toString();
        if(newName.contains(QDir::separator())){
            return false;
        }
        SmTreeItem *i = itemAt(index);
        QString old = i->data(FullPath).toString();
        QString dir = fileInfo(index).absolutePath();
        QString newPath = QString("%1/%2").arg(dir).arg(newName);
        QFile::rename(old, newPath);
        return true;
    }
    return SmTreeModel::setData(index, value, role);
}

bool SmDirModel::isDir(const QModelIndex &idx) const {
    if(!idx.isValid()){
        return false;
    }
    SmTreeItem *i = itemAt(idx);
    QFileInfo fi(i->data(FullPath).toString());
    return fi.isDir();
}

QDir SmDirModel::dir() const{
    return QDir(mCurrentDir);
}

QFileInfo SmDirModel::fileInfo(const QModelIndex &idx) const {
    if(!idx.isValid()){
        return QFileInfo();
    }
    SmTreeItem *i = itemAt(idx);
    return QFileInfo(i->data(FullPath).toString());
}

void SmDirModel::setDir(const QString &dir){
    mCurrentDir = dir;
    mWatch->setDir(mCurrentDir);
}

void SmDirModel::dirEvent(const QList<QVariant> &data, int e){
    if(e == SmDirWatcher::Added){
        /* for some reason SmTreeModel::addRow() doesn't work,
         * couldn't figure it out in 5 hours, so customize it
         * and reset the model... gatherAsync is done by
         * modelReset();
         */
        addFile(data);
        emit fsFreeChanged();
        return;
    }
    QModelIndex idx = find(data.at(Name), Name, rootIndex());
    if(!idx.isValid()){
        return;
    }
    if(e == SmDirWatcher::Deleted){
        removeRow(idx.row());
    }
    if(e == SmDirWatcher::CloseWrite){
        for(int i = 0; i < mHeaders.count(); ++i){
            QModelIndex c = index(idx.row(), i, QModelIndex());
            setData(c, data.at(i), Qt::EditRole);
        }
    }
    emit fsFreeChanged();
}

void SmDirModel::readSettings(){
    mIcons.insert("folder", SmGlobals::instance()->iconFor("folder"));
    mIcons.insert("file", SmGlobals::instance()->iconFor("file"));
    QSettings s;
    bool autorefresh = s.value("ui/autorefresh", false).toBool();
    if(autorefresh){
        mRefreshTimer->stop();
        mRefreshTimer->disconnect();
        int interval = s.value("ui/autorefreshvalue").toInt();
        interval *= 1000;
        mRefreshTimer->setInterval(interval);
        mRefreshTimer->start();
        connect(mRefreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
    }
    bool expensive = s.value("ui/expensiveops", true).toBool();
    mWatch->setExpensiveOps(expensive);
}

void SmDirModel::refresh(){
    setDir(mCurrentDir);
    emit fsFreeChanged();
}

void SmDirModel::populate(SmTreeItem *root){
    setRoot(root);
    emit needResize();
}

void SmDirModel::addFile(const QList<QVariant> &data){
    SmTreeItem *newItem = new SmTreeItem(data, root());
    int w = root()->childCount();
    int i = 0;
    while(i < root()->childCount()){
        if(newItem->data(Name).toString().toLower() < root()->child(i)->data(Name).toString().toLower()){
            w = i;
            break;
        }
        ++i;
    }
    beginResetModel();
    root()->insertChild(w, newItem);
    endResetModel();
    return;
}

TimerHandler::TimerHandler(QTimer *timer) : mTimer(timer) {
    mTimer->stop();
}

TimerHandler::~TimerHandler(){
    QSettings s;
    bool autoRefresh = s.value("ui/autorefresh", false).toBool();
    if(autoRefresh){
        mTimer->start();
    }
}

WatcherHandler::WatcherHandler(SmDirWatcher *watcher) : mWatcher(watcher){
    mWatcher->quit();
    mWatcher->wait();
    mWatcher->blockSignals(true);
}

WatcherHandler::~WatcherHandler(){
    mWatcher->blockSignals(false);
    mWatcher->start();
}