File indexing completed on 2024-04-28 05:25:54

0001 // SPDX-FileCopyrightText: 2020 Han Young <hanyoung@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "savedcommandsmodel.h"
0005 #include "terminalsettings.h"
0006 
0007 SavedCommandsModel::SavedCommandsModel(QObject *parent)
0008     : QAbstractListModel(parent)
0009     , m_actions(TerminalSettings::self()->actions())
0010 {
0011 }
0012 
0013 SavedCommandsModel::~SavedCommandsModel()
0014 {
0015 }
0016 
0017 QVariant SavedCommandsModel::data(const QModelIndex &index, int role) const
0018 {
0019     Q_UNUSED(role)
0020 
0021     if (!index.isValid() || index.row() < 0 || index.row() >= m_actions.count()) {
0022         return false;
0023     }
0024 
0025     return m_actions.at(index.row());
0026 }
0027 
0028 void SavedCommandsModel::save()
0029 {
0030     TerminalSettings::self()->setActions(m_actions);
0031     TerminalSettings::self()->save();
0032 }
0033 
0034 void SavedCommandsModel::addAction(const QString &action)
0035 {
0036     beginInsertRows(QModelIndex(), m_actions.size(), m_actions.size());
0037     m_actions.push_back(action);
0038     endInsertRows();
0039 
0040     save();
0041 }
0042 
0043 bool SavedCommandsModel::removeRow(int row)
0044 {
0045     if (row < 0 || row >= m_actions.size()) {
0046         return false;
0047     }
0048 
0049     beginRemoveRows(QModelIndex(), row, row);
0050     m_actions.erase(m_actions.begin() + row);
0051     endRemoveRows();
0052 
0053     save();
0054     return true;
0055 }