Changeset View
Changeset View
Standalone View
Standalone View
src/widgets/rename/batchrenametypes_p.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2018 by Emirald Mateli <aldo.mateli@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify * | ||||
5 | * it under the terms of the GNU General Public License as published by * | ||||
6 | * the Free Software Foundation; either version 2 of the License, or * | ||||
7 | * (at your option) any later version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, * | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
12 | * GNU General Public License for more details. * | ||||
13 | * * | ||||
14 | * You should have received a copy of the GNU General Public License * | ||||
15 | * along with this program; if not, write to the * | ||||
16 | * Free Software Foundation, Inc., * | ||||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | ||||
18 | ***************************************************************************/ | ||||
19 | | ||||
20 | #include "batchrenametypes_p.h" | ||||
21 | | ||||
22 | #include <QString> | ||||
23 | #include <QVariant> | ||||
24 | #include <QDateTime> | ||||
25 | #include <QRegularExpression> | ||||
26 | #include <QMimeDatabase> | ||||
27 | #include <QFileInfo> | ||||
28 | #include "batchrenamevar_p.h" | ||||
29 | | ||||
30 | namespace KIO | ||||
31 | { | ||||
32 | | ||||
33 | QString BatchRenameTypes::macroRenamer(const QHash<QString, QVariant> ¶ms) | ||||
34 | { | ||||
35 | QString sequenceFormatted = params[QStringLiteral("macroReplace.sequence")] | ||||
36 | .toString().rightJustified(params[QStringLiteral("macroReplace.padding")].toInt(), QLatin1Char('0')); | ||||
37 | const QDateTime timestamp = params[QStringLiteral("timestamp")].toDateTime(); | ||||
38 | | ||||
39 | return params["macroReplace.text"].toString() | ||||
40 | .replace(BatchRenameVar::dateYear4Digit(), timestamp.toString(QStringLiteral("yyyy"))) | ||||
41 | .replace(BatchRenameVar::dateYear2Digit(), timestamp.toString(QStringLiteral("yy"))) | ||||
42 | .replace(BatchRenameVar::dateMonth(), timestamp.toString(QStringLiteral("MM"))) | ||||
43 | .replace(BatchRenameVar::dateDay(), timestamp.toString(QStringLiteral("dd"))) | ||||
44 | .replace(BatchRenameVar::dateHour(), timestamp.toString(QStringLiteral("hh"))) | ||||
45 | .replace(BatchRenameVar::dateMinute(), timestamp.toString(QStringLiteral("mm"))) | ||||
46 | .replace(BatchRenameVar::dateSecond(), timestamp.toString(QStringLiteral("ss"))) | ||||
47 | .replace(BatchRenameVar::fullName(), params[QStringLiteral("name")].toString()) | ||||
48 | .replace(BatchRenameVar::baseName(), params[QStringLiteral("baseName")].toString()) | ||||
49 | .replace(BatchRenameVar::suffix(), params[QStringLiteral("extension")].toString()) | ||||
50 | .replace(BatchRenameVar::index(), sequenceFormatted); | ||||
51 | } | ||||
52 | | ||||
53 | QString BatchRenameTypes::findReplaceRenamer(const QHash<QString, QVariant> ¶ms) | ||||
54 | { | ||||
55 | const QString find = params[QStringLiteral("findReplace.find")].toString(); | ||||
56 | const QString replace = params[QStringLiteral("findReplace.replace")].toString(); | ||||
57 | const bool caseInsensitive = params[QStringLiteral("findReplace.caseInsensitive")].toBool(); | ||||
58 | | ||||
59 | if (!params[QStringLiteral("findReplace.useRegex")].toBool()) { | ||||
60 | return params[QStringLiteral("name")].toString().replace( | ||||
61 | find, replace, caseInsensitive ? Qt::CaseInsensitive : Qt::CaseSensitive); | ||||
62 | } | ||||
63 | | ||||
64 | auto opt = caseInsensitive | ||||
65 | ? QRegularExpression::CaseInsensitiveOption | ||||
66 | : QRegularExpression::NoPatternOption; | ||||
67 | return params[QStringLiteral("name")].toString().replace(QRegularExpression(find, opt), replace); | ||||
68 | } | ||||
69 | | ||||
70 | QString BatchRenameTypes::captureRegexGroups(const QHash<QString, QVariant> ¶ms) | ||||
71 | { | ||||
72 | const auto find = params[QStringLiteral("newName.find")].toString(); | ||||
73 | auto replace = params[QStringLiteral("newName.replace")].toString(); | ||||
74 | | ||||
75 | QRegularExpression re(QStringLiteral("(\\\\(\\d))")); | ||||
76 | QRegularExpressionMatchIterator match = re.globalMatch(replace); | ||||
77 | | ||||
78 | QRegularExpression exp(find); | ||||
79 | QRegularExpressionMatch findMatches = exp.match(params[QStringLiteral("name")].toString()); | ||||
80 | | ||||
81 | if (capturedGroups.isEmpty()) { | ||||
82 | capturedGroups = findMatches.capturedTexts(); | ||||
83 | } | ||||
84 | | ||||
85 | while (match.hasNext()) { | ||||
86 | auto m = match.next(); | ||||
87 | auto num = m.captured(2); | ||||
88 | | ||||
89 | replace = replace.replace(QStringLiteral("\\") + num, findMatches.captured(num.toInt())); | ||||
90 | } | ||||
91 | | ||||
92 | return replace; | ||||
93 | } | ||||
94 | | ||||
95 | QStringList BatchRenameTypes::firstCapturedGroups() | ||||
96 | { | ||||
97 | return capturedGroups; | ||||
98 | } | ||||
99 | | ||||
100 | void BatchRenameTypes::clearCapturedGroups() | ||||
101 | { | ||||
102 | capturedGroups.clear(); | ||||
103 | } | ||||
104 | | ||||
105 | QStringList BatchRenameTypes::capturedGroups; | ||||
106 | | ||||
107 | } |