tpasswordsortproxymodel.cpp - sailfish-safe - Sailfish frontend for safe(1)
 (HTM) git clone git://git.z3bra.org/sailfish-safe.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       tpasswordsortproxymodel.cpp (2210B)
       ---
            1 /*
            2  *   Copyright (C) 2018  Daniel Vrátil <dvratil@kde.org>
            3  *                 2021  Willy Goiffon <contact@z3bra.org>
            4  *
            5  *   This program is free software; you can redistribute it and/or modify
            6  *   it under the terms of the GNU Library General Public License as
            7  *   published by the Free Software Foundation; either version 2, or
            8  *   (at your option) any later version.
            9  *
           10  *   This program is distributed in the hope that it will be useful,
           11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
           12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
           13  *   GNU General Public License for more details
           14  *
           15  *   You should have received a copy of the GNU Library General Public
           16  *   License along with this program; if not, write to the
           17  *   Free Software Foundation, Inc.,
           18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
           19  */
           20 
           21 #include "passwordsortproxymodel.h"
           22 #include "passwordsmodel.h"
           23 
           24 #include <QDebug>
           25 
           26 PasswordSortProxyModel::PasswordSortProxyModel(QObject *parent)
           27     : QSortFilterProxyModel(parent)
           28 {
           29     sort(0); // enable sorting
           30 }
           31 
           32 bool PasswordSortProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
           33 {
           34     const auto typeLeft = static_cast<PasswordsModel::EntryType>(source_left.data(PasswordsModel::EntryTypeRole).toInt());
           35     const auto typeRight = static_cast<PasswordsModel::EntryType>(source_right.data(PasswordsModel::EntryTypeRole).toInt());
           36 
           37     // Folders first
           38     if (typeLeft != typeRight) {
           39         return typeLeft == PasswordsModel::FolderEntry;
           40     }
           41 
           42     return QSortFilterProxyModel::lessThan(source_left, source_right);
           43 }
           44 
           45 void PasswordSortProxyModel::addPassword(const QModelIndex &parent, const QString &name,
           46                                          const QString &password, const QString &extras)
           47 {
           48     qobject_cast<PasswordsModel*>(sourceModel())->addPassword(mapToSource(parent), name, password, extras);
           49 }
           50 
           51 void PasswordSortProxyModel::setPassphrase(const QString &passphrase)
           52 {
           53     qobject_cast<PasswordsModel*>(sourceModel())->setPassphrase(passphrase);
           54 }
           55 
           56 void PasswordSortProxyModel::forgetPassphrase()
           57 {
           58     qobject_cast<PasswordsModel*>(sourceModel())->forgetPassphrase();
           59 }