File: krservices.cpp

package info (click to toggle)
krusader 2%3A2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,448 kB
  • sloc: cpp: 56,112; ansic: 1,187; xml: 811; sh: 23; makefile: 3
file content (258 lines) | stat: -rw-r--r-- 7,581 bytes parent folder | download
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
/*
    SPDX-FileCopyrightText: 2002 Shie Erlich <erlich@users.sourceforge.net>
    SPDX-FileCopyrightText: 2002 Rafi Yanai <yanai@users.sourceforge.net>
    SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "krservices.h"

// QtCore
#include <QDir>
#include <QSet>
#include <QTextStream>
#include <QtGlobal>

#include <KProtocolManager>
#include <KSharedConfig>
#include <utility>

#include "defaults.h"
#include "krglobal.h"

QString KrServices::GLOBAL_MESSAGE_PATTERN = "%{time hh:mm:ss.zzz}-%{type} %{category} %{function}@%{line} # %{message}";

bool KrServices::cmdExist(const QString &cmdName)
{
    // Reminder: If that function is modified, it's important to research if the
    // changes must also be applied to `KrServices::fullPathName()`
    // and `kio_krarcProtocol::fullPathName()`

    KConfigGroup dependGrp(krConfig, "Dependencies");
    QString supposedName = dependGrp.readEntry(cmdName, QString());
    if (QFileInfo::exists(supposedName))
        return true;

    if ((supposedName = QStandardPaths::findExecutable(cmdName)).isEmpty())
        return false;

    // Because an executable file has been found, its path is remembered
    // in order to avoid some future searches
    dependGrp.writeEntry(cmdName, supposedName);

    return true;
}

QString KrServices::fullPathName(const QString &name, QString confName)
{
    // Reminder: If that function is modified, it's important to research if the
    // changes must also be applied to `kio_krarcProtocol::fullPathName()`
    // and `KrServices::cmdExist()`

    if (confName.isNull())
        confName = name;

    KConfigGroup dependGrp(krConfig, "Dependencies");
    QString supposedName = dependGrp.readEntry(confName, QString());
    if (QFileInfo::exists(supposedName))
        return supposedName;

    if ((supposedName = QStandardPaths::findExecutable(name)).isEmpty())
        return QString();

    // Because an executable file has been found, its path is remembered
    // in order to avoid some future searches
    dependGrp.writeEntry(confName, supposedName);

    return supposedName;
}

QString KrServices::chooseFullPathName(QStringList names, const QString &confName)
{
    for (const QString &name : std::as_const(names)) {
        QString foundTool = KrServices::fullPathName(name, confName);
        if (!foundTool.isEmpty()) {
            return foundTool;
        }
    }

    return "";
}

bool KrServices::isExecutable(const QString &path)
{
    QFileInfo info(path);
    return info.isFile() && info.isExecutable();
}

bool KrServices::isoSupported(const QString &mimetype)
{
#ifdef KRARC_QUERY_ENABLED
    return KProtocolInfo::archiveMimetypes("iso").contains(mimetype);
#else
    return false;
#endif
}

bool KrServices::fileToStringList(QTextStream *stream, QStringList &target, bool keepEmptyLines)
{
    if (!stream)
        return false;
    QString line;
    while (!stream->atEnd()) {
        line = stream->readLine().trimmed();
        if (keepEmptyLines || !line.isEmpty())
            target.append(line);
    }
    return true;
}

bool KrServices::fileToStringList(QFile *file, QStringList &target, bool keepEmptyLines)
{
    QTextStream stream(file);
    return fileToStringList(&stream, target, keepEmptyLines);
}

QString KrServices::quote(const QString &name)
{
    if (!name.contains('\''))
        return '\'' + name + '\'';
    if (!name.contains('"') && !name.contains('$'))
        return '\"' + name + '\"';
    return escape(name);
}

QStringList KrServices::quote(const QStringList &names)
{
    QStringList result;
    for (int i = 0; i < names.size(); ++i)
        result.append(quote(names[i]));
    return result;
}

QList<QUrl> KrServices::toUrlList(const QStringList &list)
{
    QList<QUrl> result;
    for (QStringList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it) {
        result.append(QUrl::fromUserInput(*it, QDir::currentPath(), QUrl::AssumeLocalFile));
    }
    return result;
}

QStringList KrServices::toStringList(const QList<QUrl> &list)
{
    QStringList result;
    for (QList<QUrl>::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it) {
        result.append(it->toString());
    }
    return result;
}

// Adds one tool to the list in the supportedTools method
void supportedTool(QStringList &tools, const QString &toolType, QStringList names, QString confName)
{
    QString foundTool = KrServices::chooseFullPathName(std::move(names), std::move(confName));
    if (!foundTool.isEmpty()) {
        tools.append(toolType);
        tools.append(foundTool);
    }
}

// return a list in the format of TOOLS,PATH. for example
// DIFF,kdiff,TERMINAL,konsole,...
//
// currently supported tools: DIFF, MAIL, RENAME
//
// to use it: QStringList lst = supportedTools();
//            int i = lst.indexOf("DIFF");
//            if (i!=-1) pathToDiff=lst[i+1];
QStringList KrServices::supportedTools()
{
    QStringList tools;

    // first, a diff program: kdiff
    supportedTool(tools,
                  "DIFF",
                  QStringList() << "kdiff3"
                                << "kompare"
                                << "xxdiff",
                  "diff utility");

    // a mailer: kmail or thunderbird
    supportedTool(tools,
                  "MAIL",
                  QStringList() << "thunderbird"
                                << "kmail",
                  "mailer");

    // rename tool: krename
    supportedTool(tools, "RENAME", QStringList() << "krename", "krename");

    // checksum utility
    supportedTool(tools, "MD5", QStringList() << "md5sum", "checksum utility");

    return tools;
}

QString KrServices::escape(QString name)
{
    const QString evilstuff = "\\\"'`()[]{}!?;$&<>| \t\r\n"; // stuff that should get escaped

    for (auto i : evilstuff)
        name.replace(i, ('\\' + i));

    return name;
}

QString KrServices::escapeFileUrl(QString urlString)
{
    // Avoid that if a path contains a '#' then what follows the '#' be interpreted as the fragment identifier of
    // the URL and not a part of the file path; for more information https://bugs.kde.org/show_bug.cgi?id=270150 can be seen
    return urlString.replace('#', "%23").replace('?', "%3F");
}

QUrl KrServices::escapeFileUrl(const QUrl &url)
{
    return QUrl(KrServices::escapeFileUrl(url.toString()));
}

QString KrServices::urlToLocalPath(const QUrl &url)
{
    QUrl fileUrl = QUrl(url);
    // QUrl::toLocalFile() does not work if the protocol is "file" e.g. when opening an archive
    fileUrl.setScheme("file");
    QString path = fileUrl.toLocalFile();
    REPLACE_DIR_SEP2(path);

#ifdef Q_OS_WIN
    if (path.startsWith(DIR_SEPARATOR)) {
        int p = 1;
        while (p < path.length() && path[p] == DIR_SEPARATOR_CHAR)
            p++;
        /* /C:/Folder */
        if (p + 2 <= path.length() && path[p].isLetter() && path[p + 1] == ':') {
            path = path.mid(p);
        }
    }
#endif
    return path;
}

static bool s_withDebugMessages;
static QtMessageHandler s_defaultMessageHandler;

void KrServices::setGlobalKrMessageHandler(bool withDebugMessages)
{
    s_withDebugMessages = withDebugMessages;
    s_defaultMessageHandler = qInstallMessageHandler(nullptr);
    qInstallMessageHandler(&krMessageHandler);
}

void KrServices::krMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    // filter debug if not enabled
    if (type != QtDebugMsg || s_withDebugMessages) {
        s_defaultMessageHandler(type, context, msg);
    }
}