Debian Codegraph @

tools/bluezapi2qt/Interface.cpp

raw
1 /*
2 * BluezQt - Asynchronous BlueZ wrapper library
3 *
4 * SPDX-FileCopyrightText: 2019 Manuel Weichselbaumer <[email protected]>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9 #include "Interface.h"
10
11 #include <QRegularExpression>
12
13 Interface::Interface()
14 {
15 }
16
17 bool Interface::parse(const QString &line)
18 {
19 if (line.startsWith(QLatin1String("Service\t"))) {
20 m_state = State::Service;
21 } else if (line.startsWith(QLatin1String("Interface\t"))) {
22 m_state = State::Interface;
23 } else if (line.startsWith(QLatin1String("Object path\t"))) {
24 m_state = State::ObjectPath;
25 } else if (line.startsWith(QLatin1String("Methods\t")) || Methods::isMethod(line)) { // Argh! AgentManager is missing the Methods keyword
26 m_state = State::Methods;
27 } else if (line.startsWith(QLatin1String("Properties\t"))) {
28 m_state = State::Properties;
29 } else if (m_state != State::Comment && !line.isEmpty() && !line.startsWith(QLatin1String("\t"))) {
30 // If we do not parse comment, but line starts with characters, we are done.
31 return false;
32 }
33
34 switch (m_state) {
35 case State::Comment:
36 parseComment(line);
37 break;
38 case State::Service:
39 parseService(line);
40 break;
41 case State::Interface:
42 parseInterface(line);
43 break;
44 case State::ObjectPath:
45 parseObjectPath(line);
46 break;
47 case State::Methods:
48 m_methods.parse(line);
49 break;
50 case State::Properties:
51 m_properties.parse(line);
52 break;
53 }
54
55 return true;
56 }
57
58 bool Interface::finalize()
59 {
60 bool success = true;
61
62 success &= m_methods.finalize();
63 success &= m_properties.finalize();
64
65 return success;
66 }
67
68 QStringList Interface::comment() const
69 {
70 return m_comment;
71 }
72 QString Interface::service() const
73 {
74 return m_service;
75 }
76
77 QString Interface::name() const
78 {
79 return m_name;
80 }
81
82 QString Interface::objectPath() const
83 {
84 return m_objectPath;
85 }
86
87 Methods Interface::methods() const
88 {
89 return m_methods;
90 }
91
92 Properties Interface::properties() const
93 {
94 return m_properties;
95 }
96
97 void Interface::parseComment(const QString &line)
98 {
99 if (line.isEmpty()) {
100 m_comment.append(QString());
101 return;
102 } else if (line.startsWith(QLatin1Char(' ')) || line.startsWith(QStringLiteral("\t"))) {
103 m_comment.append(QString());
104 }
105
106 if (!m_comment.last().isEmpty()) {
107 m_comment.last() += QLatin1Char(' ');
108 }
109 m_comment.last() += line;
110 }
111
112 void Interface::parseService(const QString &line)
113 {
114 const QRegularExpression rx(QStringLiteral("Service\\t+(.+)"));
115 QRegularExpressionMatch match = rx.match(line);
116 if (match.hasMatch()) {
117 m_service = match.captured(1);
118 }
119 }
120
121 void Interface::parseInterface(const QString &line)
122 {
123 const QRegularExpression rx(QStringLiteral("Interface\\t+(.+)"));
124 QRegularExpressionMatch match = rx.match(line);
125 if (match.hasMatch()) {
126 m_name = match.captured(1);
127 }
128 }
129
130 void Interface::parseObjectPath(const QString &line)
131 {
132 const QRegularExpression rx(QStringLiteral("Object path\\t+(.+)"));
133 QRegularExpressionMatch match = rx.match(line);
134 if (match.hasMatch()) {
135 m_objectPath = match.captured(1);
136 }
137 }