File: Interface.cpp

package info (click to toggle)
bluez-qt 5.116.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,932 kB
  • sloc: cpp: 16,676; xml: 554; ansic: 337; sh: 13; makefile: 8
file content (137 lines) | stat: -rw-r--r-- 3,307 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * BluezQt - Asynchronous BlueZ wrapper library
 *
 * SPDX-FileCopyrightText: 2019 Manuel Weichselbaumer <mincequi@web.de>
 *
 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
 */

#include "Interface.h"

#include <QRegularExpression>

Interface::Interface()
{
}

bool Interface::parse(const QString &line)
{
    if (line.startsWith(QLatin1String("Service\t"))) {
        m_state = State::Service;
    } else if (line.startsWith(QLatin1String("Interface\t"))) {
        m_state = State::Interface;
    } else if (line.startsWith(QLatin1String("Object path\t"))) {
        m_state = State::ObjectPath;
    } else if (line.startsWith(QLatin1String("Methods\t")) || Methods::isMethod(line)) { // Argh! AgentManager is missing the Methods keyword
        m_state = State::Methods;
    } else if (line.startsWith(QLatin1String("Properties\t"))) {
        m_state = State::Properties;
    } else if (m_state != State::Comment && !line.isEmpty() && !line.startsWith(QLatin1String("\t"))) {
        // If we do not parse comment, but line starts with characters, we are done.
        return false;
    }

    switch (m_state) {
    case State::Comment:
        parseComment(line);
        break;
    case State::Service:
        parseService(line);
        break;
    case State::Interface:
        parseInterface(line);
        break;
    case State::ObjectPath:
        parseObjectPath(line);
        break;
    case State::Methods:
        m_methods.parse(line);
        break;
    case State::Properties:
        m_properties.parse(line);
        break;
    }

    return true;
}

bool Interface::finalize()
{
    bool success = true;

    success &= m_methods.finalize();
    success &= m_properties.finalize();

    return success;
}

QStringList Interface::comment() const
{
    return m_comment;
}
QString Interface::service() const
{
    return m_service;
}

QString Interface::name() const
{
    return m_name;
}

QString Interface::objectPath() const
{
    return m_objectPath;
}

Methods Interface::methods() const
{
    return m_methods;
}

Properties Interface::properties() const
{
    return m_properties;
}

void Interface::parseComment(const QString &line)
{
    if (line.isEmpty()) {
        m_comment.append(QString());
        return;
    } else if (line.startsWith(QLatin1Char(' ')) || line.startsWith(QStringLiteral("\t"))) {
        m_comment.append(QString());
    }

    if (!m_comment.last().isEmpty()) {
        m_comment.last() += QLatin1Char(' ');
    }
    m_comment.last() += line;
}

void Interface::parseService(const QString &line)
{
    const QRegularExpression rx(QStringLiteral("Service\\t+(.+)"));
    QRegularExpressionMatch match = rx.match(line);
    if (match.hasMatch()) {
        m_service = match.captured(1);
    }
}

void Interface::parseInterface(const QString &line)
{
    const QRegularExpression rx(QStringLiteral("Interface\\t+(.+)"));
    QRegularExpressionMatch match = rx.match(line);
    if (match.hasMatch()) {
        m_name = match.captured(1);
    }
}

void Interface::parseObjectPath(const QString &line)
{
    const QRegularExpression rx(QStringLiteral("Object path\\t+(.+)"));
    QRegularExpressionMatch match = rx.match(line);
    if (match.hasMatch()) {
        m_objectPath = match.captured(1);
    }
}