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 | |
25 | // The .reviewboardrc files are python files, we'll read and if it doesn't work |
26 | // Well bad luck. See: http://www.reviewboard.org/docs/rbtools/dev/rbt/configuration/ |
27 | |
28 | QFile f(filePath.toLocalFile()); |
29 | if (!f.open(flags: QFile::ReadOnly | QFile::Text)) { |
30 | qWarning() << "couldn't open" << filePath; |
31 | return; |
32 | } |
33 | |
34 | const QRegularExpression rx(QRegularExpression::anchoredPattern(QStringLiteral("([\\w]+) *= *[\"'](.*)[\"']" ))); |
35 | QHash<QString, QString> values; |
36 | QTextStream stream(&f); |
37 | while (!stream.atEnd()) { |
38 | QRegularExpressionMatch match = rx.match(subject: stream.readLine()); |
39 | if (match.hasMatch()) { |
40 | values.insert(key: match.captured(nth: 1), value: match.captured(nth: 2)); |
41 | } |
42 | } |
43 | |
44 | if (values.contains(QStringLiteral("REVIEWBOARD_URL" ))) { |
45 | m_server = QUrl(values[QStringLiteral("REVIEWBOARD_URL" )]); |
46 | } |
47 | if (values.contains(QStringLiteral("REPOSITORY" ))) { |
48 | m_repository = values[QStringLiteral("REPOSITORY" )]; |
49 | } |
50 | addExtraData(QStringLiteral("target_groups" ), value: values[QStringLiteral("TARGET_GROUPS" )]); |
51 | addExtraData(QStringLiteral("target_people" ), value: values[QStringLiteral("TARGET_PEOPLE" )]); |
52 | addExtraData(QStringLiteral("branch" ), value: values[QStringLiteral("BRANCH" )]); |
53 | |
54 | Q_EMIT dataChanged(); |
55 | } |
56 | |
57 | void ReviewboardRC::(const QString &key, const QString &value) |
58 | { |
59 | if (!value.isEmpty()) { |
60 | m_extraData.insert(key, value); |
61 | } |
62 | } |
63 | |
64 | #include "moc_reviewboardrc.cpp" |
65 | |