Changeset View
Changeset View
Standalone View
Standalone View
addons/externaltools/kateexternaltool.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * Copyright 2019 Dominik Haumann <dhaumann@kde.org> | ||||
4 | * | ||||
5 | * This library is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU Library General Public | ||||
7 | * License as published by the Free Software Foundation; either | ||||
8 | * version 2 of the License, or (at your option) any later version. | ||||
9 | * | ||||
10 | * This library is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | * Library General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU Library General Public License | ||||
16 | * along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | * Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | #include "kateexternaltool.h" | ||||
21 | | ||||
22 | #include <KConfigGroup> | ||||
23 | #include <QStandardPaths> | ||||
24 | | ||||
25 | namespace { | ||||
26 | QString toString(KateExternalTool::SaveMode saveMode) | ||||
27 | { | ||||
28 | switch (saveMode) { | ||||
29 | case KateExternalTool::SaveMode::None: return QStringLiteral("None"); | ||||
30 | case KateExternalTool::SaveMode::CurrentDocument: return QStringLiteral("CurrentDocument"); | ||||
31 | case KateExternalTool::SaveMode::AllDocuments: return QStringLiteral("AllDocuments"); | ||||
32 | } | ||||
33 | Q_ASSERT(false); // yout forgot a case above | ||||
34 | return QStringLiteral("None"); | ||||
35 | } | ||||
36 | | ||||
37 | KateExternalTool::SaveMode toSaveMode(const QString & mode) | ||||
38 | { | ||||
39 | if (mode == QStringLiteral("None")) return KateExternalTool::SaveMode::None; | ||||
40 | if (mode == QStringLiteral("CurrentDocument")) return KateExternalTool::SaveMode::CurrentDocument; | ||||
41 | if (mode == QStringLiteral("AllDocuments")) return KateExternalTool::SaveMode::AllDocuments; | ||||
42 | return KateExternalTool::SaveMode::None; | ||||
43 | } | ||||
44 | | ||||
45 | QString toString(KateExternalTool::OutputMode outputMode) | ||||
46 | { | ||||
47 | switch (outputMode) { | ||||
48 | case KateExternalTool::OutputMode::Ignore: return QStringLiteral("Ignore"); | ||||
49 | case KateExternalTool::OutputMode::InsertAtCursor: return QStringLiteral("InsertAtCursor"); | ||||
50 | case KateExternalTool::OutputMode::ReplaceSelectedText: return QStringLiteral("ReplaceSelectedText"); | ||||
51 | case KateExternalTool::OutputMode::ReplaceCurrentDocument: return QStringLiteral("ReplaceCurrentDocument"); | ||||
52 | case KateExternalTool::OutputMode::AppendToCurrentDocument: return QStringLiteral("AppendToCurrentDocument"); | ||||
53 | case KateExternalTool::OutputMode::InsertInNewDocument: return QStringLiteral("InsertInNewDocument"); | ||||
54 | case KateExternalTool::OutputMode::DisplayInPane: return QStringLiteral("DisplayInPane"); | ||||
55 | } | ||||
56 | Q_ASSERT(false); // yout forgot a case above | ||||
57 | return QStringLiteral("Ignore"); | ||||
58 | } | ||||
59 | | ||||
60 | KateExternalTool::OutputMode toOutputMode(const QString & mode) | ||||
61 | { | ||||
62 | if (mode == QStringLiteral("Ignore")) return KateExternalTool::OutputMode::Ignore; | ||||
63 | if (mode == QStringLiteral("InsertAtCursor")) return KateExternalTool::OutputMode::InsertAtCursor; | ||||
64 | if (mode == QStringLiteral("ReplaceSelectedText")) return KateExternalTool::OutputMode::ReplaceSelectedText; | ||||
65 | if (mode == QStringLiteral("ReplaceCurrentDocument")) return KateExternalTool::OutputMode::ReplaceCurrentDocument; | ||||
66 | if (mode == QStringLiteral("AppendToCurrentDocument")) return KateExternalTool::OutputMode::AppendToCurrentDocument; | ||||
67 | if (mode == QStringLiteral("InsertInNewDocument")) return KateExternalTool::OutputMode::InsertInNewDocument; | ||||
68 | if (mode == QStringLiteral("DisplayInPane")) return KateExternalTool::OutputMode::DisplayInPane; | ||||
69 | return KateExternalTool::OutputMode::Ignore; | ||||
70 | } | ||||
71 | } | ||||
72 | | ||||
73 | bool KateExternalTool::checkExec() const | ||||
74 | { | ||||
75 | return !QStandardPaths::findExecutable(executable).isEmpty(); | ||||
76 | } | ||||
77 | | ||||
78 | bool KateExternalTool::matchesMimetype(const QString& mt) const | ||||
79 | { | ||||
80 | return mimetypes.isEmpty() || mimetypes.contains(mt); | ||||
81 | } | ||||
82 | | ||||
83 | void KateExternalTool::load(const KConfigGroup& cg) | ||||
84 | { | ||||
85 | category = cg.readEntry("category", ""); | ||||
86 | name = cg.readEntry("name", ""); | ||||
87 | icon = cg.readEntry("icon", ""); | ||||
88 | executable = cg.readEntry("executable", ""); | ||||
89 | arguments = cg.readEntry("arguments", ""); | ||||
90 | input = cg.readEntry("input", ""); | ||||
91 | workingDir = cg.readEntry("workingDir", ""); | ||||
92 | mimetypes = cg.readEntry("mimetypes", QStringList()); | ||||
93 | actionName = cg.readEntry("actionName"); | ||||
94 | cmdname = cg.readEntry("cmdname"); | ||||
95 | saveMode = toSaveMode(cg.readEntry("save", "None")); | ||||
96 | reload = cg.readEntry("reload", false); | ||||
97 | outputMode = toOutputMode(cg.readEntry("output", "Ignore")); | ||||
98 | | ||||
99 | hasexec = checkExec(); | ||||
100 | } | ||||
101 | | ||||
102 | void KateExternalTool::save(KConfigGroup& cg) const | ||||
103 | { | ||||
104 | cg.writeEntry("category", category); | ||||
105 | cg.writeEntry("name", name); | ||||
106 | cg.writeEntry("icon", icon); | ||||
107 | cg.writeEntry("executable", executable); | ||||
108 | cg.writeEntry("arguments", arguments); | ||||
109 | cg.writeEntry("input", input); | ||||
110 | cg.writeEntry("workingDir", workingDir); | ||||
111 | cg.writeEntry("mimetypes", mimetypes); | ||||
112 | cg.writeEntry("actionName", actionName); | ||||
113 | cg.writeEntry("cmdname", cmdname); | ||||
114 | cg.writeEntry("save", toString(saveMode)); | ||||
115 | cg.writeEntry("reload", reload); | ||||
116 | cg.writeEntry("output", toString(outputMode)); | ||||
117 | } | ||||
118 | | ||||
119 | bool operator==(const KateExternalTool & lhs, const KateExternalTool & rhs) | ||||
120 | { | ||||
121 | return lhs.category == rhs.category | ||||
122 | && lhs.name == rhs.name | ||||
123 | && lhs.icon == rhs.icon | ||||
124 | && lhs.executable == rhs.executable | ||||
125 | && lhs.arguments == rhs.arguments | ||||
126 | && lhs.input == rhs.input | ||||
127 | && lhs.workingDir == rhs.workingDir | ||||
128 | && lhs.mimetypes == rhs.mimetypes | ||||
129 | && lhs.actionName == rhs.actionName | ||||
130 | && lhs.cmdname == rhs.cmdname | ||||
131 | && lhs.saveMode == rhs.saveMode | ||||
132 | && lhs.reload == rhs.reload | ||||
133 | && lhs.outputMode == rhs.outputMode; | ||||
134 | } | ||||
135 | | ||||
136 | // kate: space-indent on; indent-width 4; replace-tabs on; |