File indexing completed on 2024-04-28 03:52:07

0001 /*
0002  * BluezQt - Asynchronous BlueZ wrapper library
0003  *
0004  * SPDX-FileCopyrightText: 2019 Manuel Weichselbaumer <mincequi@web.de>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #include "Interface.h"
0010 
0011 #include <QRegularExpression>
0012 
0013 Interface::Interface()
0014 {
0015 }
0016 
0017 bool Interface::parse(const QString &line)
0018 {
0019     if (line.startsWith(QLatin1String("Service\t"))) {
0020         m_state = State::Service;
0021     } else if (line.startsWith(QLatin1String("Interface\t"))) {
0022         m_state = State::Interface;
0023     } else if (line.startsWith(QLatin1String("Object path\t"))) {
0024         m_state = State::ObjectPath;
0025     } else if (line.startsWith(QLatin1String("Methods\t")) || Methods::isMethod(line)) { // Argh! AgentManager is missing the Methods keyword
0026         m_state = State::Methods;
0027     } else if (line.startsWith(QLatin1String("Properties\t"))) {
0028         m_state = State::Properties;
0029     } else if (m_state != State::Comment && !line.isEmpty() && !line.startsWith(QLatin1String("\t"))) {
0030         // If we do not parse comment, but line starts with characters, we are done.
0031         return false;
0032     }
0033 
0034     switch (m_state) {
0035     case State::Comment:
0036         parseComment(line);
0037         break;
0038     case State::Service:
0039         parseService(line);
0040         break;
0041     case State::Interface:
0042         parseInterface(line);
0043         break;
0044     case State::ObjectPath:
0045         parseObjectPath(line);
0046         break;
0047     case State::Methods:
0048         m_methods.parse(line);
0049         break;
0050     case State::Properties:
0051         m_properties.parse(line);
0052         break;
0053     }
0054 
0055     return true;
0056 }
0057 
0058 bool Interface::finalize()
0059 {
0060     bool success = true;
0061 
0062     success &= m_methods.finalize();
0063     success &= m_properties.finalize();
0064 
0065     return success;
0066 }
0067 
0068 QStringList Interface::comment() const
0069 {
0070     return m_comment;
0071 }
0072 QString Interface::service() const
0073 {
0074     return m_service;
0075 }
0076 
0077 QString Interface::name() const
0078 {
0079     return m_name;
0080 }
0081 
0082 QString Interface::objectPath() const
0083 {
0084     return m_objectPath;
0085 }
0086 
0087 Methods Interface::methods() const
0088 {
0089     return m_methods;
0090 }
0091 
0092 Properties Interface::properties() const
0093 {
0094     return m_properties;
0095 }
0096 
0097 void Interface::parseComment(const QString &line)
0098 {
0099     if (line.isEmpty()) {
0100         m_comment.append(QString());
0101         return;
0102     } else if (line.startsWith(QLatin1Char(' ')) || line.startsWith(QStringLiteral("\t"))) {
0103         m_comment.append(QString());
0104     }
0105 
0106     if (!m_comment.last().isEmpty()) {
0107         m_comment.last() += QLatin1Char(' ');
0108     }
0109     m_comment.last() += line;
0110 }
0111 
0112 void Interface::parseService(const QString &line)
0113 {
0114     const QRegularExpression rx(QStringLiteral("Service\\t+(.+)"));
0115     QRegularExpressionMatch match = rx.match(line);
0116     if (match.hasMatch()) {
0117         m_service = match.captured(1);
0118     }
0119 }
0120 
0121 void Interface::parseInterface(const QString &line)
0122 {
0123     const QRegularExpression rx(QStringLiteral("Interface\\t+(.+)"));
0124     QRegularExpressionMatch match = rx.match(line);
0125     if (match.hasMatch()) {
0126         m_name = match.captured(1);
0127     }
0128 }
0129 
0130 void Interface::parseObjectPath(const QString &line)
0131 {
0132     const QRegularExpression rx(QStringLiteral("Object path\\t+(.+)"));
0133     QRegularExpressionMatch match = rx.match(line);
0134     if (match.hasMatch()) {
0135         m_objectPath = match.captured(1);
0136     }
0137 }