File: filemodel.cpp

package info (click to toggle)
kget 4%3A16.08.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,440 kB
  • ctags: 4,323
  • sloc: cpp: 37,020; xml: 341; python: 290; perl: 41; sh: 11; makefile: 4
file content (698 lines) | stat: -rw-r--r-- 17,459 bytes parent folder | download | duplicates (3)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/***************************************************************************
*   Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net>                     *
*                                                                         *
*   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.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
***************************************************************************/

#include "filemodel.h"

#include "signature.h"
#include "verifier.h"

#include <KIcon>
#include <KLocale>
#include <KMimeType>

FileItem::FileItem(const QString &name, FileItem *parent)
  : m_name(name),
    m_state(Qt::Checked),
    m_status(Job::Stopped),
    m_totalSize(0),
    m_checkusmVerified(0),
    m_signatureVerified(0),
    m_parent(parent)
{
}

FileItem::~FileItem()
{
    qDeleteAll(m_childItems);
}

void FileItem::appendChild(FileItem *child)
{
    m_childItems.append(child);
}

FileItem *FileItem::child(int row)
{
    return m_childItems.value(row);
}

int FileItem::childCount() const
{
    return m_childItems.count();
}

bool FileItem::isFile() const
{
    return m_childItems.isEmpty();
}

int FileItem::columnCount() const
{
    return 5;
}

QVariant FileItem::data(int column, int role) const
{
    if (column == FileItem::File)
    {
        if (role == Qt::CheckStateRole)
        {
            return m_state;
        }
        else if (role == Qt::DisplayRole)
        {
            return m_name;
        }
        else if (role == Qt::DecorationRole)
        {
            if (m_mimeType.isNull()) {
                if (isFile()) {
                    m_mimeType = KIcon(KMimeType::iconNameForUrl(KUrl(m_name)));
                } else {
                    m_mimeType = KIcon("folder");
                }
            }

            return m_mimeType;
        }
    }
    else if (column == FileItem::Status)
    {
        if ((role == Qt::DisplayRole) || (role == Qt::DecorationRole))
        {
            if (isFile()) {
                return m_status;
            }
        }
    }
    else if (column == FileItem::Size)
    {
        if (role == Qt::DisplayRole)
        {
            return KIO::convertSize(m_totalSize);
        }
    } else if (column == FileItem::ChecksumVerified) {
        if (role == Qt::DecorationRole) {
            switch (m_checkusmVerified) {
                case Verifier::Verified:
                    return KIcon("dialog-ok");
                case Verifier::NotVerified:
                    return KIcon("dialog-error");
                case Verifier::NoResult:
                default:
                    return KIcon();
            }
        }
    } else if (column == FileItem::SignatureVerified) {//TODO implement all cases
        if (role == Qt::DecorationRole) {
            switch (m_signatureVerified) {
                case Signature::Verified:
                    return KIcon("dialog-ok");
                case Signature::VerifiedInformation:
                    return KIcon("dialog-information");
                case Signature::VerifiedWarning:
                    return KIcon("dialog-warning");
                case Signature::NotVerified:
                    return KIcon("dialog-error");
                case Signature::NoResult:
                default:
                    return KIcon();
            }
        }
    }

    return QVariant();
}

bool FileItem::setData(int column, const QVariant &value, FileModel *model, int role)
{
    if (value.isNull())
    {
        return false;
    }

    if (column == FileItem::File)
    {
        if (role == Qt::CheckStateRole)
        {
            m_state = static_cast<Qt::CheckState>(value.toInt());
            model->changeData(this->row(), column, this);
            checkParents(m_state, model);
            checkChildren(m_state, model);
            return true;
        }
        else if (role == Qt::EditRole)
        {
            m_name = value.toString();
            model->changeData(this->row(), column, this);
            return true;
        }
    }
    else if (column == FileItem::Status)
    {
        if (role == Qt::EditRole)
        {
            if (isFile()) {
                m_status = static_cast<Job::Status>(value.toInt());
                bool finished = (m_status == Job::Finished);
                model->changeData(this->row(), column, this, finished);

                return true;
            }
        }
    }
    else if (column == FileItem::Size)
    {
        if (role == Qt::EditRole)
        {
            KIO::fileoffset_t newSize = value.toLongLong();
            if (m_parent)
            {
                m_parent->addSize(newSize - m_totalSize, model);
            }
            m_totalSize = newSize;
            model->changeData(this->row(), column, this);
            return true;
        }
    } else if (column == FileItem::ChecksumVerified) {
        m_checkusmVerified = value.toInt();
        model->changeData(this->row(), column, this);
        return true;
    } else if (column == FileItem::SignatureVerified) {
        m_signatureVerified = value.toInt();
        model->changeData(this->row(), column, this);
        return true;
    }

    return false;
}

void FileItem::checkParents(Qt::CheckState state, FileModel *model)
{
    if (!model)
    {
        return;
    }

    if (!m_parent)
    {
        return;
    }

    foreach (FileItem *child, m_parent->m_childItems)
    {
        if (child->m_state != state)
        {
            state = Qt::Unchecked;
            break;
        }
    }

    m_parent->m_state = state;
    model->changeData(m_parent->row(), FileItem::File, m_parent);
    m_parent->checkParents(state, model);
}

void FileItem::checkChildren(Qt::CheckState state, FileModel *model)
{
    if (!model)
    {
        return;
    }

    m_state = state;
    model->changeData(row(), FileItem::File, this);

    foreach (FileItem *child, m_childItems)
    {
        child->checkChildren(state, model);
    }
}

FileItem *FileItem::parent()
{
    return m_parent;
}

int FileItem::row() const
{
    if (m_parent)
    {
        return m_parent->m_childItems.indexOf(const_cast<FileItem*>(this));
    }

    return 0;
}

void FileItem::addSize(KIO::fileoffset_t size, FileModel *model)
{
    if (!isFile())
    {
        m_totalSize += size;
        model->changeData(this->row(), FileItem::Size, this);
        if (m_parent)
        {
            m_parent->addSize(size, model);
        }
    }
}


FileModel::FileModel(const QList<KUrl> &files, const KUrl &destDirectory, QObject *parent)
  : QAbstractItemModel(parent),
    m_destDirectory(destDirectory),
    m_checkStateChanged(false)
{
    m_rootItem = new FileItem("root");
    m_header << i18nc("file in a filesystem", "File") << i18nc("status of the download", "Status") << i18nc("size of the download", "Size") << i18nc("checksum of a file", "Checksum") << i18nc("signature of a file", "Signature");

    setupModelData(files);
}

FileModel::~FileModel()
{
    delete m_rootItem;
}

void FileModel::setupModelData(const QList<KUrl> &files)
{
    QString destDirectory = m_destDirectory.pathOrUrl();

    foreach (const KUrl &file, files)
    {
        FileItem *parent = m_rootItem;
        QStringList directories = file.pathOrUrl().remove(destDirectory).split('/', QString::SkipEmptyParts);
        FileItem *child = 0;
        while (directories.count())
        {
            QString part = directories.takeFirst();
            for (int i = 0; i < parent->childCount(); ++i)
            {
                //folder already exists
                if (parent->child(i)->data(0, Qt::DisplayRole).toString() == part)
                {
                    parent = parent->child(i);
                    //file already exists
                    if (!directories.count())
                    {
                        break;
                    }
                    part = directories.takeFirst();
                    i = -1;
                    continue;
                }
            }
            child = new FileItem(part, parent);
            parent->appendChild(child);
            parent = parent->child(parent->childCount() - 1);
        }
        if (child)
        {
            m_files.append(child);
        }
    }
}

int FileModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
    {
        return static_cast<FileItem*>(parent.internalPointer())->columnCount();
    }
    else
    {
        return m_rootItem->columnCount();
    }
}

QVariant FileModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }


    FileItem *item = static_cast<FileItem*>(index.internalPointer());
    const QVariant data = item->data(index.column(), role);

    //get the status icon as well as status text
    if (index.column() == FileItem::Status)
    {
        const Job::Status status = static_cast<Job::Status>(data.toInt());
        if (item->isFile()) {
            if (role == Qt::DisplayRole)
            {
                if (m_customStatusTexts.contains(status))
                {
                    return m_customStatusTexts[status];
                }
                else
                {
                    return Transfer::statusText(status);
                }
            }
            else if (role == Qt::DecorationRole)
            {
                if (m_customStatusIcons.contains(status))
                {
                    return m_customStatusIcons[status];
                }
                else
                {
                    return Transfer::statusPixmap(status);
                }
            }
        } else {
            return QVariant();
        }
    }

    return data;
}

bool FileModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
    {
        return false;
    }

    FileItem *item = static_cast<FileItem*>(index.internalPointer());

    if ((index.column() == FileItem::File) && (role == Qt::CheckStateRole)) {
        const bool worked = item->setData(index.column(), value, this, role);
        if (worked) {
            m_checkStateChanged = true;
        }

        return worked;
    }

    return item->setData(index.column(), value, this, role);
}

Qt::ItemFlags FileModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
    {
        return 0;
    }

    if (index.column() == FileItem::File)
    {
        return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable;
    }

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant FileModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
    {
        return m_header.value(section);
    }

    return QVariant();
}

QModelIndex FileModel::index(int row, int column, const QModelIndex &parent) const
{
    if (!hasIndex(row, column, parent))
    {
        return QModelIndex();
    }

    FileItem *parentItem;
    if (parent.isValid())
    {
        parentItem = static_cast<FileItem*>(parent.internalPointer());
    }
    else
    {
        parentItem = m_rootItem;
    }

    FileItem *childItem = parentItem->child(row);
    if (childItem)
    {
        return createIndex(row, column, childItem);
    }
    else
    {
        return QModelIndex();
    }
}

QModelIndex FileModel::index(const KUrl &file, int column)
{
    FileItem *item = getItem(file);
    if (!item)
    {
        return QModelIndex();
    }

    return createIndex(item->row(), column, item);
}

QModelIndexList FileModel::fileIndexes(int column) const
{
    QModelIndexList indexList;
    foreach (FileItem *item, m_files)
    {
        int row = item->row();
        indexList.append(createIndex(row, column, item));
    }

    return indexList;
}

QModelIndex FileModel::parent(const QModelIndex &index) const
{
    if (!index.isValid())
    {
        return QModelIndex();
    }

    FileItem *childItem = static_cast<FileItem*>(index.internalPointer());
    FileItem *parentItem = childItem->parent();
    if ((parentItem == m_rootItem) || (!parentItem))
    {
        return QModelIndex();
    }
    else
    {
        return createIndex(parentItem->row(), 0, parentItem);
    }
}

int FileModel::rowCount(const QModelIndex &parent) const
{
    if (parent.column() > 0)
    {
        return 0;
    }

    FileItem *parentItem;
    if (parent.isValid())
    {
        parentItem = static_cast<FileItem*>(parent.internalPointer());
    }
    else
    {
        parentItem = m_rootItem;
    }

    return parentItem->childCount();
}

void FileModel::changeData(int row, int column, FileItem *item, bool finished)
{
    QModelIndex index = createIndex(row, column, item);
    emit dataChanged(index, index);

    if (finished) {
        const KUrl file = getUrl(index);
        emit fileFinished(file);
    }
}


void FileModel::setDirectory(const KUrl &newDirectory)
{
    m_destDirectory = newDirectory;
    m_itemCache.clear();
}

KUrl FileModel::getUrl(const QModelIndex &index)
{
    if (!index.isValid()) {
        return KUrl();
    }

    const QModelIndex file = index.sibling(index.row(), FileItem::File);

    return getUrl(static_cast<FileItem*>(file.internalPointer()));
}

KUrl FileModel::getUrl(FileItem *item)
{
    const QString path = getPath(item);
    const QString name = item->data(FileItem::File, Qt::DisplayRole).toString();
    KUrl url = m_destDirectory;
    url.addPath(path + name);

    return url;
}

QString FileModel::getPath(FileItem *item)
{
    FileItem *parent = item->parent();
    QString path;
    while (parent && parent->parent())
    {
        path = parent->data(FileItem::File, Qt::DisplayRole).toString() + '/' + path;
        parent = parent->parent();
    }

    return path;
}

FileItem *FileModel::getItem(const KUrl &file)
{
    if (m_itemCache.contains(file))
    {
        return m_itemCache[file];
    }

    QString destDirectory = m_destDirectory.pathOrUrl();

    FileItem *item = m_rootItem;
    QStringList directories = file.pathOrUrl().remove(destDirectory).split('/', QString::SkipEmptyParts);
    while (directories.count())
    {
        QString part = directories.takeFirst();
        for (int i = 0; i < item->childCount(); ++i)
        {
            //folder already exists
            if (item->child(i)->data(FileItem::File, Qt::DisplayRole).toString() == part)
            {
                item = item->child(i);
                //file already exists
                if (!directories.count())
                {
                    break;
                }
                part = directories.takeFirst();
                i = -1;
                continue;
            }
        }
    }

    if (item == m_rootItem)
    {
        item = 0;
    }
    else
    {
        m_itemCache[file] = item;
    }

    return item;
}

bool FileModel::downloadFinished(const KUrl &file)
{
    FileItem *item = getItem(file);
    if (item)
    {
        const Job::Status status = static_cast<Job::Status>(item->data(FileItem::Status, Qt::DisplayRole).toInt());
        if (status == Job::Finished)
        {
            return true;
        }
    }

    return false;
}

bool FileModel::isFile(const QModelIndex &index) const
{
    if (!index.isValid()) {
        return false;
    }

    FileItem *item = static_cast<FileItem*>(index.internalPointer());

    //only files can be renamed, no folders
    return item->isFile();
}

void FileModel::rename(const QModelIndex &file, const QString &newName)
{
    if (!file.isValid() || (file.column() != FileItem::File))
    {
        return;
    }

    FileItem *item = static_cast<FileItem*>(file.internalPointer());
    //only files can be renamed, no folders
    if (!item->isFile()) {
        return;
    }

    //Find out the old and the new KUrl
    QString oldName = file.data(Qt::DisplayRole).toString();
    QString path = getPath(item);

    KUrl oldUrl = m_destDirectory;
    oldUrl.addPath(path + oldName);
    KUrl newUrl = m_destDirectory;
    newUrl.addPath(path + newName);

    m_itemCache.remove(oldUrl);

    setData(file, newName);

    emit rename(oldUrl, newUrl);
}

void FileModel::renameFailed(const KUrl &beforeRename, const KUrl &afterRename)
{
    Q_UNUSED(beforeRename)
    Q_UNUSED(afterRename)
}

void FileModel::watchCheckState()
{
    m_checkStateChanged = false;
}

void FileModel::stopWatchCheckState()
{
    if (m_checkStateChanged)
    {
        emit checkStateChanged();
    }

    m_checkStateChanged = false;
}