Skip to content
Snippets Groups Projects
Select Git revision
  • master default protected
  • beta/202505231812
  • stable/20250523.0
  • nightly/20250523.0
  • nightly/20250515.0
  • nightly/20250510.0
  • nightly/20250509.1
  • nightly/20250509.0
  • stable/20250430.1
  • stable/20250430.0
  • beta/202504301614
  • nightly/20250430.0
  • stable/20250424.1
  • beta/202504241506
  • stable/20250424.0
  • nightly/20250424.1
  • nightly/20250424.0
  • nightly/20250422.0
  • beta/202504120241
  • stable/20250411.0
  • nightly/20250411.0
21 results

filestosendlistmodel.cpp

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    filestosendlistmodel.cpp 2.78 KiB
    /*
     * Copyright (C) 2021 by Savoir-faire Linux
     * Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com>
     *
     * 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 3 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, see <http://www.gnu.org/licenses/>.
     */
    
    #include "filestosendlistmodel.h"
    
    #include "utils.h"
    
    #include <QFileInfo>
    #include <QImageReader>
    
    FilesToSendListModel::FilesToSendListModel(QObject* parent)
        : QAbstractListModel(parent)
    {}
    
    int
    FilesToSendListModel::rowCount(const QModelIndex& parent) const
    {
        if (parent.isValid())
            return 0;
        return pendingFiles_.size();
    }
    
    QHash<int, QByteArray>
    FilesToSendListModel::roleNames() const
    {
        using namespace FilesToSend;
        QHash<int, QByteArray> roles;
    #define X(role) roles[role] = #role;
        FS_ROLES
    #undef X
        return roles;
    }
    
    Q_INVOKABLE void
    FilesToSendListModel::addToPending(QString filePath)
    {
        auto fileInfo = QFileInfo(filePath);
        if (!fileInfo.exists())
            return;
    
        // QImageReader will treat .gz file (Jami archive) as svgz image format
        // so decideFormatFromContent is needed
        bool isImage = false;
        QImageReader reader;
        reader.setDecideFormatFromContent(true);
        reader.setFileName(filePath);
    
        if (!reader.read().isNull())
            isImage = true;
    
        beginInsertRows(QModelIndex(), pendingFiles_.size(), pendingFiles_.size());
        auto item = FilesToSend::Item(filePath, fileInfo.fileName(), isImage, fileInfo.size());
        pendingFiles_.append(item);
        endInsertRows();
    }
    
    void
    FilesToSendListModel::removeFromPending(int index)
    {
        beginRemoveRows(QModelIndex(), index, index);
        pendingFiles_.removeAt(index);
        endRemoveRows();
    }
    
    Q_INVOKABLE void
    FilesToSendListModel::flush()
    {
        beginRemoveRows(QModelIndex(), 0, pendingFiles_.size() - 1);
        pendingFiles_.clear();
        endRemoveRows();
    }
    
    QVariant
    FilesToSendListModel::data(const QModelIndex& index, int role) const
    {
        using namespace FilesToSend;
    
        auto item = pendingFiles_.at(index.row());
    
        switch (role) {
        case Role::FileName:
            return QVariant(item.fileName);
        case Role::FilePath:
            return QVariant(item.filePath);
        case Role::FileSize:
            return QVariant(Utils::humanFileSize(item.fileSizeInByte));
        case Role::IsImage:
            return QVariant(item.isImage);
        }
        return QVariant();
    }