File: findfilesystemmodel.cpp

package info (click to toggle)
noblenote 1.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 1,308 kB
  • ctags: 464
  • sloc: cpp: 3,748; makefile: 6
file content (212 lines) | stat: -rw-r--r-- 7,918 bytes parent folder | download
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
/* nobleNote, a note taking application
 * Copyright (C) 2012 Christian Metscher <hakaishi@web.de>,
                      Fabian Deuchler <Taiko000@gmail.com>

 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.

 * nobleNote is licensed under the MIT, see `http://copyfree.org/licenses/mit/license.txt'.
 */

#include "findfilesystemmodel.h"
#include "findfilemodel.h"
#include "filesystemmodel.h"
#include <QFileSystemModel>
#include "htmlnotereader.h"

FindFileSystemModel::FindFileSystemModel(QObject *parent) :
    QSortFilterProxyModel(parent)
{
}

QString FindFileSystemModel::fileName(const QModelIndex &index) const
{
    if(QFileSystemModel* fsm= qobject_cast<QFileSystemModel*>(sourceModel()))
      return fsm->fileName(mapToSource(index));
    else if(FindFileModel* ffm= qobject_cast<FindFileModel*>(sourceModel()))
      return ffm->fileName(mapToSource(index));
    qDebug("FindFileSystemModel::fileName failed: cast failed");
    return QString();
}

QStringList FindFileSystemModel::fileNames(const QList<QModelIndex> &index) const
{
    QHash<QModelIndex,QString> names;
    foreach(QModelIndex idx, index)
    {
         if(QFileSystemModel* fsm= qobject_cast<QFileSystemModel*>(sourceModel()))
           names.insert(idx,fsm->fileName(mapToSource(idx)));
         else if(FindFileModel* ffm= qobject_cast<FindFileModel*>(sourceModel()))
           names.insert(idx,ffm->fileName(mapToSource(idx)));
    }
    return names.values();
}

QString FindFileSystemModel::filePath(const QModelIndex &index) const
{
    if(QFileSystemModel* fsm= qobject_cast<QFileSystemModel*>(sourceModel()))
      return fsm->filePath(mapToSource(index));
    else if(FindFileModel* ffm= qobject_cast<FindFileModel*>(sourceModel()))
      return ffm->filePath(mapToSource(index));
     qDebug("FindFileSystemModel::filePath failed: cast failed");
     return QString();
}

bool FindFileSystemModel::rmdir(const QModelIndex &index) const
{
    if(QFileSystemModel* fsm= qobject_cast<QFileSystemModel*>(sourceModel()))
      return fsm->rmdir(mapToSource(index));
    qDebug("FindFileSystemModel::rmdir failed: cast failed. This method is only implemented for QFileSystemModel");
    return false;
}

QModelIndex FindFileSystemModel::mkdir(const QModelIndex &parent, const QString &name)
{
    if(QFileSystemModel* fsm= qobject_cast<QFileSystemModel*>(sourceModel()))
      return mapFromSource(fsm->mkdir(mapToSource(parent),name));
    qDebug("FindFileSystemModel::mkdir failed: cast failed. This method is only implemented for QFileSystemModel");
    return QModelIndex();
}

bool FindFileSystemModel::remove(const QModelIndex &index) const
{
    if(QFileSystemModel* fsm= qobject_cast<QFileSystemModel*>(sourceModel()))
      return fsm->remove(mapToSource(index));
    else if(FindFileModel* ffm= qobject_cast<FindFileModel*>(sourceModel()))
      return ffm->remove(mapToSource(index));
    qDebug("FindFileSystemModel::remove failed: cast failed");
    return false;
}

bool FindFileSystemModel::removeList(const QModelIndexList &index) const
{
    bool successfull = true;
    foreach(QModelIndex idx, index)
    {
        if(QFileSystemModel* fsm= qobject_cast<QFileSystemModel*>(sourceModel()))
        {
            if(!fsm->remove(mapToSource(idx)))
                successfull = false;
        }
        else if(FindFileModel* ffm= qobject_cast<FindFileModel*>(sourceModel()))
        {
                if(!ffm->remove(mapToSource(idx)))
                    successfull = false;
        }
    }
    if(!successfull)
      qDebug("FindFileSystemModel::remove failed: one or more files could not be removed");

    return successfull;
}

/*static*/ bool FindFileSystemModel::removeList(const QFileInfoList &fileInfos)
{
    qDebug("FindFileSystemModel::removeList FIXME: removal works?");
    bool successfull = true;
    foreach(const QFileInfo& fileInfo, fileInfos)
    {
        QString path = fileInfo.path();
        if(!QFile::remove(fileInfo.path()))
            successfull = false;
    }
    return successfull;
}



QFileInfo FindFileSystemModel::fileInfo(const QModelIndex &index) const
{
    if(QFileSystemModel* fsm= qobject_cast<QFileSystemModel*>(sourceModel()))
      return fsm->fileInfo(mapToSource(index));
    else if(FindFileModel* ffm= qobject_cast<FindFileModel*>(sourceModel()))
      return ffm->fileInfo(mapToSource(index));
    qDebug("FindFileSystemModel::fileInfo failed : cast failed");
    return QFileInfo();
}

void FindFileSystemModel::appendFile(QString filePath)
{
    if(FindFileModel* ffm= qobject_cast<FindFileModel*>(sourceModel()))
      ffm->appendFile(filePath);
    else
     qDebug("FindFileSystemModel::appendFile failed: cast failed");
}

QModelIndex FindFileSystemModel::setRootPath(const QString &newPath)
{
    if(QFileSystemModel* fsm= qobject_cast<QFileSystemModel*>(sourceModel()))
      return mapFromSource(fsm->setRootPath(newPath));
    qDebug("FindFileSystemModel::setRootPath failed: cast failed");
    return QModelIndex();
}

QString FindFileSystemModel::rootPath() const
{
    if(QFileSystemModel* fsm= qobject_cast<QFileSystemModel*>(sourceModel()))
      return fsm->rootPath();
    qDebug("FindFileSystemModel::rootPath failed: cast failed");
    return QString();
}

void FindFileSystemModel::clear()
{
    if(FindFileModel* ffm= qobject_cast<FindFileModel*>(sourceModel()))
        ffm->clear();
    else
        qDebug("FindFileSystemModel::clear failed: cast failed");
}

void FindFileSystemModel::findInFiles(const QString &fileName, const QString &content, const QString &path)
{
    if(FindFileModel* ffm= qobject_cast<FindFileModel*>(sourceModel()))
    {
        ffm->findInFiles(fileName,content,path);
        return;
    }
    qDebug("FindFileSystemModel::findInFiles failed: cast failed");
}

QModelIndex FindFileSystemModel::index(const QString &path, int column) const
{
    if(QFileSystemModel* fsm= qobject_cast<QFileSystemModel*>(sourceModel()))
        return mapFromSource(fsm->index(path,column));
    qDebug("FindFileSystemModel::index failed: cast failed. This method is only implemented for QFileSystemModel");
    return QModelIndex();
}

void FindFileSystemModel::copyNotesToBackupDir(const QModelIndexList& indexes) const
{
    foreach(const QModelIndex& index, indexes)
    {
        QString filePath = this->filePath(index);
        QUuid uuid = HtmlNoteReader::uuid(filePath);
        if(!uuid.isNull())
            QFile::copy(filePath, QSettings().value("backup_dir_path").toString() + "/" + uuid.toString().mid(1,36));
    }
}

/*static*/ void FindFileSystemModel::copyNotesToBackupDir(const QFileInfoList& fileInfos)
{
    foreach(const QFileInfo& fileInfo, fileInfos)
    {
        QUuid uuid = HtmlNoteReader::uuid(fileInfo.path());
        if(!uuid.isNull())
            QFile::copy(fileInfo.path(), QSettings().value("backup_dir_path").toString() + "/" + uuid.toString().mid(1,36));
    }
}