File indexing completed on 2024-05-05 04:50:48
0001 /* 0002 * SPDX-FileCopyrightText: 2021 George Florea Bănuș <georgefb899@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "customcommandsmodel.h" 0008 0009 #include <KConfigGroup> 0010 0011 #include "actionsmodel.h" 0012 #include "global.h" 0013 0014 CustomCommandsModel::CustomCommandsModel(QObject *parent) 0015 : QAbstractListModel(parent) 0016 { 0017 connect(this, &QAbstractListModel::rowsMoved, this, [=]() { 0018 for (int i = 0; i < m_customCommands.size(); ++i) { 0019 auto configGroup = m_customCommandsConfig->group(m_customCommands[i]->commandId); 0020 configGroup.writeEntry("Order", i); 0021 configGroup.sync(); 0022 } 0023 }); 0024 } 0025 0026 int CustomCommandsModel::rowCount(const QModelIndex &parent) const 0027 { 0028 if (parent.isValid()) 0029 return 0; 0030 0031 return m_customCommands.size(); 0032 } 0033 0034 QVariant CustomCommandsModel::data(const QModelIndex &index, int role) const 0035 { 0036 if (!index.isValid()) 0037 return QVariant(); 0038 0039 Command *command = m_customCommands[index.row()]; 0040 0041 switch (role) { 0042 case CommandIdRole: 0043 return QVariant(command->commandId); 0044 case CommandRole: 0045 return QVariant(command->command); 0046 case OsdMessageRole: 0047 return QVariant(command->osdMessage); 0048 case TypeRole: 0049 return QVariant(command->type); 0050 case ShortcutRole: 0051 return QVariant(command->shortcut); 0052 case SetOnStartupRole: 0053 return QVariant(command->setOnStartup); 0054 } 0055 0056 return QVariant(); 0057 } 0058 0059 QHash<int, QByteArray> CustomCommandsModel::roleNames() const 0060 { 0061 QHash<int, QByteArray> roles; 0062 roles[CommandIdRole] = "commandId"; 0063 roles[OsdMessageRole] = "osdMessage"; 0064 roles[CommandRole] = "command"; 0065 roles[TypeRole] = "type"; 0066 roles[ShortcutRole] = "shortcut"; 0067 roles[SetOnStartupRole] = "setOnStartup"; 0068 return roles; 0069 } 0070 0071 void CustomCommandsModel::init() 0072 { 0073 connect(appActionsModel(), &ActionsModel::shortcutChanged, this, [=](const QString &name, const QString &shortcut) { 0074 for (int i{0}; i < m_customCommands.count(); ++i) { 0075 if (m_customCommands[i]->commandId == name) { 0076 m_customCommands[i]->shortcut = shortcut; 0077 Q_EMIT dataChanged(index(i, 0), index(i, 0)); 0078 return; 0079 } 0080 } 0081 }); 0082 0083 QString ccConfig = Global::instance()->appConfigFilePath(Global::ConfigFile::CustomCommands); 0084 m_customCommandsConfig = KSharedConfig::openConfig(ccConfig, KConfig::SimpleConfig); 0085 QStringList groups = m_customCommandsConfig->groupList(); 0086 0087 beginInsertRows(QModelIndex(), 0, groups.size()); 0088 for (const QString &groupName : std::as_const(groups)) { 0089 auto configGroup = m_customCommandsConfig->group(groupName); 0090 auto c = new Command(); 0091 c->commandId = groupName; 0092 c->command = configGroup.readEntry("Command", QString()); 0093 c->osdMessage = configGroup.readEntry("OsdMessage", QString()); 0094 c->type = configGroup.readEntry("Type", QString()); 0095 c->shortcut = appActionsModel()->getShortcut(c->commandId, QString()); 0096 c->order = configGroup.readEntry("Order", 0); 0097 c->setOnStartup = configGroup.readEntry("SetOnStartup", true); 0098 m_customCommands << c; 0099 if (c->type == QStringLiteral("shortcut")) { 0100 Action action; 0101 action.name = c->commandId; 0102 action.text = c->command; 0103 action.description = c->osdMessage; 0104 action.shortcut = c->shortcut; 0105 action.type = QStringLiteral("CustomAction"); 0106 appActionsModel()->appendCustomAction(action); 0107 } 0108 } 0109 0110 std::sort(m_customCommands.begin(), m_customCommands.end(), [=](Command *c1, Command *c2) { 0111 return c1->order < c2->order; 0112 }); 0113 0114 endInsertRows(); 0115 } 0116 0117 void CustomCommandsModel::moveRows(int oldIndex, int newIndex) 0118 { 0119 if (oldIndex < newIndex) { 0120 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newIndex + 1); 0121 } else { 0122 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newIndex); 0123 } 0124 Command *c = m_customCommands.takeAt(oldIndex); 0125 m_customCommands.insert(newIndex, c); 0126 endMoveRows(); 0127 } 0128 0129 void CustomCommandsModel::saveCustomCommand(const QString &command, const QString &osdMessage, const QString &type) 0130 { 0131 int counter = m_customCommandsConfig->group(QString()).readEntry("Counter", 0); 0132 const QString &groupName = QString(QStringLiteral("Command_%1")).arg(counter); 0133 0134 if (m_customCommandsConfig->group(groupName).exists()) { 0135 return; 0136 } 0137 0138 m_customCommandsConfig->group(groupName).writeEntry(QStringLiteral("Command"), command); 0139 m_customCommandsConfig->group(groupName).writeEntry(QStringLiteral("OsdMessage"), osdMessage); 0140 m_customCommandsConfig->group(groupName).writeEntry(QStringLiteral("Type"), type); 0141 m_customCommandsConfig->group(groupName).writeEntry(QStringLiteral("Order"), rowCount()); 0142 m_customCommandsConfig->group(QString()).writeEntry(QStringLiteral("Counter"), counter + 1); 0143 m_customCommandsConfig->sync(); 0144 0145 beginInsertRows(QModelIndex(), rowCount(), rowCount()); 0146 auto configGroup = m_customCommandsConfig->group(groupName); 0147 auto c = new Command(); 0148 c->commandId = groupName; 0149 c->command = configGroup.readEntry("Command", QString()); 0150 c->osdMessage = configGroup.readEntry("OsdMessage", QString()), c->type = configGroup.readEntry("Type", QString()); 0151 m_customCommands << c; 0152 endInsertRows(); 0153 0154 if (c->type == QStringLiteral("shortcut")) { 0155 Action action; 0156 action.name = c->commandId; 0157 action.text = c->command; 0158 action.description = c->osdMessage; 0159 action.shortcut = appActionsModel()->getShortcut(action.name, QString()); 0160 action.type = QStringLiteral("CustomAction"); 0161 appActionsModel()->appendCustomAction(action); 0162 } 0163 } 0164 0165 void CustomCommandsModel::editCustomCommand(int row, const QString &command, const QString &osdMessage, const QString &type) 0166 { 0167 auto c = m_customCommands[row]; 0168 c->command = command; 0169 c->osdMessage = osdMessage; 0170 c->type = type; 0171 0172 QString groupName = c->commandId; 0173 m_customCommandsConfig->group(groupName).writeEntry(QStringLiteral("Command"), command); 0174 m_customCommandsConfig->group(groupName).writeEntry(QStringLiteral("OsdMessage"), osdMessage); 0175 m_customCommandsConfig->group(groupName).writeEntry(QStringLiteral("Type"), type); 0176 m_customCommandsConfig->sync(); 0177 0178 Q_EMIT dataChanged(index(row, 0), index(row, 0)); 0179 if (c->type == QStringLiteral("shortcut")) { 0180 appActionsModel()->editCustomAction(c->commandId, c->command, c->osdMessage); 0181 } 0182 } 0183 0184 void CustomCommandsModel::toggleCustomCommand(const QString &groupName, int row, bool setOnStartup) 0185 { 0186 auto group = m_customCommandsConfig->group(groupName); 0187 if (setOnStartup == group.readEntry("SetOnStartup", true)) { 0188 return; 0189 } 0190 auto customCommand = m_customCommands[row]; 0191 customCommand->setOnStartup = setOnStartup; 0192 0193 group.writeEntry("SetOnStartup", setOnStartup); 0194 m_customCommandsConfig->sync(); 0195 0196 Q_EMIT dataChanged(index(row, 0), index(row, 0)); 0197 } 0198 0199 void CustomCommandsModel::deleteCustomCommand(const QString &groupName, int row) 0200 { 0201 beginRemoveRows(QModelIndex(), row, row); 0202 m_customCommandsConfig->deleteGroup(groupName); 0203 m_customCommandsConfig->sync(); 0204 m_customCommands.removeAt(row); 0205 endRemoveRows(); 0206 0207 KSharedConfig::Ptr config = KSharedConfig::openConfig(Global::instance()->appConfigFilePath()); 0208 config->group(QStringLiteral("Shortcuts")).deleteEntry(groupName); 0209 config->sync(); 0210 } 0211 0212 ActionsModel *CustomCommandsModel::appActionsModel() 0213 { 0214 return m_appActionsModel; 0215 } 0216 0217 void CustomCommandsModel::setAppActionsModel(ActionsModel *_appActionsModel) 0218 { 0219 if (m_appActionsModel == _appActionsModel) { 0220 return; 0221 } 0222 m_appActionsModel = _appActionsModel; 0223 Q_EMIT appActionsModelChanged(); 0224 } 0225 0226 #include "moc_customcommandsmodel.cpp"