1 | /* |
2 | SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.0-or-later |
5 | */ |
6 | |
7 | #include "reviewboardrc.h" |
8 | #include <QDebug> |
9 | #include <QFile> |
10 | #include <QHash> |
11 | #include <QRegularExpression> |
12 | #include <QTextStream> |
13 | |
14 | ReviewboardRC::ReviewboardRC(QObject *parent) |
15 | : QObject(parent) |
16 | { |
17 | } |
18 | |
19 | void ReviewboardRC::setPath(const QUrl &filePath) |
20 | { |
21 | if (filePath == m_path || !filePath.isLocalFile()) |
22 | return; |
23 | |
24 | // The .reviewboardrc files are python files, we'll read and if it doesn't work |
25 | // Well bad luck. See: http://www.reviewboard.org/docs/rbtools/dev/rbt/configuration/ |
26 | |
27 | QFile f(filePath.toLocalFile()); |
28 | if (!f.open(flags: QFile::ReadOnly | QFile::Text)) { |
29 | qWarning() << "couldn't open" << filePath; |
30 | return; |
31 | } |
32 | |
33 | const QRegularExpression rx(QRegularExpression::anchoredPattern(QStringLiteral("([\\w]+) *= *[\"'](.*)[\"']" ))); |
34 | QHash<QString, QString> values; |
35 | QTextStream stream(&f); |
36 | for (; !stream.atEnd();) { |
37 | QRegularExpressionMatch match = rx.match(subject: stream.readLine()); |
38 | if (match.hasMatch()) { |
39 | values.insert(key: match.captured(nth: 1), value: match.captured(nth: 2)); |
40 | } |
41 | } |
42 | |
43 | if (values.contains(QStringLiteral("REVIEWBOARD_URL" ))) |
44 | m_server = QUrl(values[QStringLiteral("REVIEWBOARD_URL" )]); |
45 | if (values.contains(QStringLiteral("REPOSITORY" ))) |
46 | m_repository = values[QStringLiteral("REPOSITORY" )]; |
47 | addExtraData(QStringLiteral("target_groups" ), value: values[QStringLiteral("TARGET_GROUPS" )]); |
48 | addExtraData(QStringLiteral("target_people" ), value: values[QStringLiteral("TARGET_PEOPLE" )]); |
49 | addExtraData(QStringLiteral("branch" ), value: values[QStringLiteral("BRANCH" )]); |
50 | |
51 | Q_EMIT dataChanged(); |
52 | } |
53 | |
54 | void ReviewboardRC::(const QString &key, const QString &value) |
55 | { |
56 | if (!value.isEmpty()) |
57 | m_extraData.insert(key, value); |
58 | } |
59 | |
60 | #include "moc_reviewboardrc.cpp" |
61 | |