1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*
SPDX-FileCopyrightText: 2004 Jonas Bähr <jonas.baehr@web.de>
SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "useraction.h"
// QtCore
#include <QDebug>
#include <QFile>
#include <QHash>
#include <QStandardPaths>
#include <QTextStream>
#include <QUrl>
// QtXml
#include <QDomDocument>
#include <QDomElement>
// QtWidgets
#include <QMenu>
#include <KActionCollection>
#include <KActionMenu>
#include <KLocalizedString>
#include <KMessageBox>
#include "../FileSystem/filesystem.h"
#include "../Panel/PanelView/krview.h"
#include "../Panel/krpanel.h"
#include "../Panel/panelfunc.h"
#include "../krusader.h"
#include "../krusaderview.h"
#include "kraction.h"
UserAction::UserAction()
{
readAllFiles();
}
UserAction::~UserAction()
{
// KrActions are deleted by Krusader's KActionCollection
}
void UserAction::removeKrAction(KrAction *action)
{
_actions.removeAll(action);
if (_defaultActions.contains(action->objectName()))
_deletedActions.insert(action->objectName());
}
void UserAction::setAvailability()
{
setAvailability(ACTIVE_FUNC->files()->getUrl(ACTIVE_PANEL->view->getCurrentItem()));
}
void UserAction::setAvailability(const QUrl ¤tURL)
{
// qDebug() << "UserAction::setAvailability currentFile: " << currentURL.url();
// disable the entries that should not appear in this folder
QListIterator<KrAction *> it(_actions);
while (it.hasNext()) {
KrAction *action = it.next();
action->setEnabled(action->isAvailable(currentURL));
}
}
void UserAction::populateMenu(KActionMenu *menu, const QUrl *currentURL)
{
// I have not found any method in Qt/KDE
// for non-recursive searching of children by name ...
QMap<QString, KActionMenu *> categoryMap;
QList<KrAction *> uncategorised;
for (KrAction *action : std::as_const(_actions)) {
const QString category = action->category();
if (!action->isEnabled())
continue;
if (currentURL != nullptr && !action->isAvailable(*currentURL))
continue;
if (category.isEmpty()) {
uncategorised.append(action);
} else {
if (!categoryMap.contains(category)) {
auto *categoryMenu = new KActionMenu(category, menu);
categoryMenu->setObjectName(category);
categoryMap.insert(category, categoryMenu);
}
KActionMenu *targetMenu = categoryMap.value(category);
targetMenu->addAction(action);
}
}
QMutableMapIterator<QString, KActionMenu *> mapIter(categoryMap);
while (mapIter.hasNext()) {
mapIter.next();
menu->addAction(mapIter.value());
}
for (KrAction *action : std::as_const(uncategorised)) {
menu->addAction(action);
};
}
QStringList UserAction::allCategories()
{
QStringList actionCategories;
QListIterator<KrAction *> it(_actions);
while (it.hasNext()) {
KrAction *action = it.next();
if (actionCategories.indexOf(action->category()) == -1)
actionCategories.append(action->category());
}
return actionCategories;
}
QStringList UserAction::allNames()
{
QStringList actionNames;
QListIterator<KrAction *> it(_actions);
while (it.hasNext()) {
KrAction *action = it.next();
actionNames.append(action->objectName());
}
return actionNames;
}
void UserAction::readAllFiles()
{
QString filename = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
ACTION_XML); // locate returns the local file if it exists, else the global one is retrieved.
if (!filename.isEmpty())
readFromFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, ACTION_XML), renameDoublicated);
filename = QStandardPaths::locate(QStandardPaths::GenericDataLocation, ACTION_XML_EXAMPLES);
if (!filename.isEmpty())
readFromFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, ACTION_XML_EXAMPLES),
ignoreDoublicated); // ignore samples which are already in the normal file
}
void UserAction::readFromFile(const QString &filename, ReadMode mode, KrActionList *list)
{
QDomDocument *doc = new QDomDocument(ACTION_DOCTYPE);
QFile file(filename);
if (file.open(QIODevice::ReadOnly)) {
// qDebug() << "UserAction::readFromFile - " << filename << "could be opened";
if (!doc->setContent(&file)) {
// qDebug() << "UserAction::readFromFile - content set - failed";
// if the file doesn't exist till now, the content CAN be set but is empty.
// if the content can't be set, the file exists and is NOT an xml-file.
file.close();
delete doc;
doc = nullptr;
KMessageBox::error(MAIN_VIEW,
i18n("The file %1 does not contain valid UserActions.\n", filename), // text
i18n("UserActions - cannot read from file") // caption
);
}
file.close();
if (doc) {
QDomElement root = doc->documentElement();
// check if the file has got the right root-element (ACTION_ROOT)
// this finds out if the xml-file read to the DOM is really a krusader
// useraction-file
if (root.tagName() != ACTION_ROOT) {
KMessageBox::error(MAIN_VIEW,
i18n("The actionfile's root element is not called %1, using %2", QString::fromLatin1(ACTION_ROOT), filename),
i18n("UserActions - cannot read from file"));
delete doc;
doc = nullptr;
}
readFromElement(root, mode, list);
delete doc;
}
} // if ( file.open( QIODevice::ReadOnly ) )
else {
KMessageBox::error(MAIN_VIEW, i18n("Unable to open actions file %1", filename), i18n("UserActions - cannot read from file"));
}
}
void UserAction::readFromElement(const QDomElement &element, ReadMode mode, KrActionList *list)
{
for (QDomNode node = element.firstChild(); !node.isNull(); node = node.nextSibling()) {
QDomElement e = node.toElement();
if (e.isNull())
continue; // this should skip nodes which are not elements ( i.e. comments, <!-- -->, or text nodes)
if (e.tagName() == "action") {
QString name = e.attribute("name");
if (name.isEmpty()) {
KMessageBox::error(
MAIN_VIEW,
i18n("Action without name detected. This action will not be imported.\nThis is an error in the file, you may want to correct it."),
i18n("UserActions - invalid action"));
continue;
}
if (mode == ignoreDoublicated) {
_defaultActions.insert(name);
if (krApp->actionCollection()->action(name) || _deletedActions.contains(name))
continue;
}
QString basename = name + "_%1";
int i = 0;
// append a counter till the name is unique... (this checks every action, not only useractions)
while (krApp->actionCollection()->action(name))
name = basename.arg(++i);
KrAction *act = new KrAction(krApp->actionCollection(), name);
if (act->xmlRead(e)) {
_actions.append(act);
if (list)
list->append(act);
} else
delete act;
} else if (e.tagName() == "deletedAction") {
QString name = e.attribute("name");
if (name.isEmpty()) {
qWarning() << "A deleted action without name detected! \nThis is an error in the file.";
continue;
}
_deletedActions.insert(name);
}
} // for
}
QDomDocument UserAction::createEmptyDoc()
{
QDomDocument doc = QDomDocument(ACTION_DOCTYPE);
// adding: <?xml version="1.0" encoding="UTF-8" ?>
doc.appendChild(doc.createProcessingInstruction("xml", ACTION_PROCESSINSTR));
// adding root-element
doc.appendChild(doc.createElement(ACTION_ROOT)); // create new actionfile by adding a root-element ACTION_ROOT
return doc;
}
bool UserAction::writeActionFile()
{
QString filename = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + ACTION_XML;
QDomDocument doc = createEmptyDoc();
QDomElement root = doc.documentElement();
for (const QString &name : std::as_const(_deletedActions)) {
QDomElement element = doc.createElement("deletedAction");
element.setAttribute("name", name);
root.appendChild(element);
}
QListIterator<KrAction *> it(_actions);
while (it.hasNext()) {
KrAction *action = it.next();
root.appendChild(action->xmlDump(doc));
}
return writeToFile(doc, filename);
}
bool UserAction::writeToFile(const QDomDocument &doc, const QString &filename)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly))
return false;
/* // This is not needed, because each DomDocument created with UserAction::createEmptyDoc already contains the processinstruction
if ( ! doc.firstChild().isProcessingInstruction() ) {
// adding: <?xml version="1.0" encoding="UTF-8" ?> if not already present
QDomProcessingInstruction instr = doc.createProcessingInstruction( "xml", ACTION_PROCESSINSTR );
doc.insertBefore( instr, doc.firstChild() );
}
*/
//By default, UTF-8 is used for reading and writing
QTextStream ts(&file);
ts << doc.toString();
file.close();
return true;
}
|