File indexing completed on 2024-05-05 04:50:49

0001 /*
0002  * SPDX-FileCopyrightText: 2020 George Florea Bănuș <georgefb899@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "subtitlesfoldersmodel.h"
0008 
0009 #include <KConfig>
0010 #include <KConfigGroup>
0011 
0012 #include "global.h"
0013 #include "subtitlessettings.h"
0014 
0015 SubtitlesFoldersModel::SubtitlesFoldersModel(QObject *parent)
0016     : QAbstractListModel(parent)
0017     , m_list(SubtitlesSettings::subtitlesFolders())
0018 {
0019 }
0020 
0021 int SubtitlesFoldersModel::rowCount(const QModelIndex &parent) const
0022 {
0023     // For list models only the root node (an invalid parent) should return the list's size. For all
0024     // other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
0025     if (parent.isValid()) {
0026         return 0;
0027     }
0028 
0029     return m_list.size();
0030 }
0031 
0032 QVariant SubtitlesFoldersModel::data(const QModelIndex &index, int role) const
0033 {
0034     if (!index.isValid()) {
0035         return QVariant();
0036     }
0037 
0038     QString path = m_list[index.row()];
0039 
0040     switch (role) {
0041     case Qt::DisplayRole:
0042         return QVariant(path);
0043     }
0044 
0045     return QVariant();
0046 }
0047 
0048 void SubtitlesFoldersModel::updateFolder(const QString &folder, int row)
0049 {
0050     m_list.replace(row, folder);
0051     QStringList newList = m_list;
0052     // remove empty strings
0053     // removing directly from m_list messes with the ui logic
0054     newList.removeAll(QString());
0055 
0056     SubtitlesSettings::self()->setSubtitlesFolders(newList);
0057     SubtitlesSettings::self()->save();
0058 }
0059 
0060 void SubtitlesFoldersModel::deleteFolder(int row)
0061 {
0062     beginRemoveRows(QModelIndex(), row, row);
0063     m_list.removeAt(row);
0064     endRemoveRows();
0065 
0066     SubtitlesSettings::setSubtitlesFolders(m_list);
0067     SubtitlesSettings::self()->save();
0068 }
0069 
0070 void SubtitlesFoldersModel::addFolder()
0071 {
0072     beginInsertRows(QModelIndex(), m_list.size(), m_list.size());
0073     m_list.append(QString());
0074     endInsertRows();
0075 }
0076 
0077 #include "moc_subtitlesfoldersmodel.cpp"