File indexing completed on 2024-12-22 03:48:44

0001 /*
0002     SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "reviewboardrc.h"
0008 #include <QDebug>
0009 #include <QFile>
0010 #include <QHash>
0011 #include <QRegularExpression>
0012 #include <QTextStream>
0013 
0014 ReviewboardRC::ReviewboardRC(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 void ReviewboardRC::setPath(const QUrl &filePath)
0020 {
0021     if (filePath == m_path || !filePath.isLocalFile())
0022         return;
0023 
0024     // The .reviewboardrc files are python files, we'll read and if it doesn't work
0025     // Well bad luck. See: http://www.reviewboard.org/docs/rbtools/dev/rbt/configuration/
0026 
0027     QFile f(filePath.toLocalFile());
0028     if (!f.open(QFile::ReadOnly | QFile::Text)) {
0029         qWarning() << "couldn't open" << filePath;
0030         return;
0031     }
0032 
0033     const QRegularExpression rx(QRegularExpression::anchoredPattern(QStringLiteral("([\\w]+) *= *[\"'](.*)[\"']")));
0034     QHash<QString, QString> values;
0035     QTextStream stream(&f);
0036     for (; !stream.atEnd();) {
0037         QRegularExpressionMatch match = rx.match(stream.readLine());
0038         if (match.hasMatch()) {
0039             values.insert(match.captured(1), match.captured(2));
0040         }
0041     }
0042 
0043     if (values.contains(QStringLiteral("REVIEWBOARD_URL")))
0044         m_server = QUrl(values[QStringLiteral("REVIEWBOARD_URL")]);
0045     if (values.contains(QStringLiteral("REPOSITORY")))
0046         m_repository = values[QStringLiteral("REPOSITORY")];
0047     addExtraData(QStringLiteral("target_groups"), values[QStringLiteral("TARGET_GROUPS")]);
0048     addExtraData(QStringLiteral("target_people"), values[QStringLiteral("TARGET_PEOPLE")]);
0049     addExtraData(QStringLiteral("branch"), values[QStringLiteral("BRANCH")]);
0050 
0051     Q_EMIT dataChanged();
0052 }
0053 
0054 void ReviewboardRC::addExtraData(const QString &key, const QString &value)
0055 {
0056     if (!value.isEmpty())
0057         m_extraData.insert(key, value);
0058 }
0059 
0060 #include "moc_reviewboardrc.cpp"