File indexing completed on 2024-12-29 05:06:05
0001 // SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org> 0002 // SPDX-License-Identifier: GPL-2.0-or-later 0003 0004 #include "applicationfolder.h" 0005 0006 #include <QJsonArray> 0007 0008 ApplicationFolder::ApplicationFolder(QObject *parent, QString name) 0009 : QObject{parent} 0010 , m_name{name} 0011 , m_applicationFolderModel{new ApplicationFolderModel{this}} 0012 { 0013 } 0014 0015 ApplicationFolder *ApplicationFolder::fromJson(QJsonObject &obj, QObject *parent) 0016 { 0017 QString name = obj[QStringLiteral("name")].toString(); 0018 QList<Application *> apps; 0019 for (auto storageId : obj[QStringLiteral("apps")].toArray()) { 0020 if (KService::Ptr service = KService::serviceByStorageId(storageId.toString())) { 0021 apps.append(new Application(parent, service)); 0022 } 0023 } 0024 0025 ApplicationFolder *folder = new ApplicationFolder(parent, name); 0026 folder->setApplications(apps); 0027 return folder; 0028 } 0029 0030 QJsonObject ApplicationFolder::toJson() 0031 { 0032 QJsonObject obj; 0033 obj[QStringLiteral("type")] = "folder"; 0034 obj[QStringLiteral("name")] = m_name; 0035 0036 QJsonArray arr; 0037 for (auto *application : m_applications) { 0038 arr.append(QJsonValue::fromVariant(application->storageId())); 0039 } 0040 0041 obj[QStringLiteral("apps")] = arr; 0042 0043 return obj; 0044 } 0045 0046 QString ApplicationFolder::name() const 0047 { 0048 return m_name; 0049 } 0050 0051 void ApplicationFolder::setName(QString &name) 0052 { 0053 m_name = name; 0054 Q_EMIT nameChanged(); 0055 Q_EMIT saveRequested(); 0056 } 0057 0058 QList<Application *> ApplicationFolder::appPreviews() 0059 { 0060 QList<Application *> previews; 0061 // we give a maximum of 4 icons 0062 for (int i = 0; i < std::min<int>(m_applications.length(), 4); ++i) { 0063 previews.push_back(m_applications[i]); 0064 } 0065 return previews; 0066 } 0067 0068 ApplicationFolderModel *ApplicationFolder::applications() 0069 { 0070 return m_applicationFolderModel; 0071 } 0072 0073 void ApplicationFolder::setApplications(QList<Application *> applications) 0074 { 0075 if (m_applicationFolderModel) { 0076 m_applicationFolderModel->deleteLater(); 0077 } 0078 0079 m_applications = applications; 0080 m_applicationFolderModel = new ApplicationFolderModel{this}; 0081 0082 Q_EMIT applicationsChanged(); 0083 Q_EMIT applicationsReset(); 0084 Q_EMIT saveRequested(); 0085 } 0086 0087 void ApplicationFolder::moveEntry(int fromRow, int toRow) 0088 { 0089 m_applicationFolderModel->moveEntry(fromRow, toRow); 0090 } 0091 0092 void ApplicationFolder::addApp(const QString &storageId, int row) 0093 { 0094 m_applicationFolderModel->addApp(storageId, row); 0095 } 0096 0097 void ApplicationFolder::removeApp(int row) 0098 { 0099 m_applicationFolderModel->removeApp(row); 0100 } 0101 0102 void ApplicationFolder::moveAppOut(int row) 0103 { 0104 if (row < 0 || row >= m_applications.size()) { 0105 return; 0106 } 0107 0108 Q_EMIT moveAppOutRequested(m_applications[row]->storageId()); 0109 removeApp(row); 0110 } 0111 0112 ApplicationFolderModel::ApplicationFolderModel(ApplicationFolder *folder) 0113 : QAbstractListModel{folder} 0114 , m_folder{folder} 0115 { 0116 } 0117 0118 int ApplicationFolderModel::rowCount(const QModelIndex &parent) const 0119 { 0120 return m_folder->m_applications.size(); 0121 } 0122 0123 QVariant ApplicationFolderModel::data(const QModelIndex &index, int role) const 0124 { 0125 if (!index.isValid()) { 0126 return QVariant(); 0127 } 0128 0129 switch (role) { 0130 case ApplicationRole: 0131 return QVariant::fromValue(m_folder->m_applications.at(index.row())); 0132 } 0133 0134 return QVariant(); 0135 } 0136 0137 QHash<int, QByteArray> ApplicationFolderModel::roleNames() const 0138 { 0139 return {{ApplicationRole, "application"}}; 0140 } 0141 0142 void ApplicationFolderModel::moveEntry(int fromRow, int toRow) 0143 { 0144 if (fromRow < 0 || toRow < 0 || fromRow >= m_folder->m_applications.length() || toRow >= m_folder->m_applications.length() || fromRow == toRow) { 0145 return; 0146 } 0147 if (toRow > fromRow) { 0148 ++toRow; 0149 } 0150 0151 beginMoveRows(QModelIndex(), fromRow, fromRow, QModelIndex(), toRow); 0152 if (toRow > fromRow) { 0153 Application *app = m_folder->m_applications.at(fromRow); 0154 m_folder->m_applications.insert(toRow, app); 0155 m_folder->m_applications.takeAt(fromRow); 0156 } else { 0157 Application *app = m_folder->m_applications.takeAt(fromRow); 0158 m_folder->m_applications.insert(toRow, app); 0159 } 0160 endMoveRows(); 0161 Q_EMIT m_folder->applicationsChanged(); 0162 Q_EMIT m_folder->saveRequested(); 0163 } 0164 0165 void ApplicationFolderModel::addApp(const QString &storageId, int row) 0166 { 0167 if (row < 0 || row > m_folder->m_applications.size()) { 0168 return; 0169 } 0170 0171 if (KService::Ptr service = KService::serviceByStorageId(storageId)) { 0172 beginInsertRows(QModelIndex(), row, row); 0173 Application *app = new Application(this, service); 0174 m_folder->m_applications.insert(row, app); 0175 endInsertRows(); 0176 0177 Q_EMIT m_folder->applicationsChanged(); 0178 Q_EMIT m_folder->saveRequested(); 0179 } 0180 } 0181 0182 void ApplicationFolderModel::removeApp(int row) 0183 { 0184 if (row < 0 || row >= m_folder->m_applications.size()) { 0185 return; 0186 } 0187 0188 beginRemoveRows(QModelIndex(), row, row); 0189 m_folder->m_applications[row]->deleteLater(); 0190 m_folder->m_applications.removeAt(row); 0191 endRemoveRows(); 0192 0193 Q_EMIT m_folder->applicationsChanged(); 0194 Q_EMIT m_folder->saveRequested(); 0195 }