labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

MQTTSubscription.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 File : MQTTSubscription.cpp
3 Project : LabPlot
4 Description : Represents a subscription made in MQTTClient
5 --------------------------------------------------------------------
6 Copyright : (C) 2018 Kovacs Ferencz (kferike98@gmail.com)
7 
8 ***************************************************************************/
9 
10 /***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 * This program is distributed in the hope that it will be useful, *
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20 * GNU General Public License for more details. *
21 * *
22 * You should have received a copy of the GNU General Public License *
23 * along with this program; if not, write to the Free Software *
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
25 * Boston, MA 02110-1301 USA *
26 * *
27 ***************************************************************************/
31 
32 #include <KLocalizedString>
33 #include <QIcon>
34 
35 /*!
36  \class MQTTSubscription
37  \brief Represents a subscription made in a MQTTClient object. It plays a role in managing MQTTTopic objects
38  and makes possible representing the subscriptions and topics in a tree like structure
39 
40  \ingroup datasources
41 */
43  m_subscriptionName(name) {
44  qDebug() << "New MQTTSubscription: " << name;
45 }
46 
48  qDebug() << "Delete MQTTSubscription: " << m_subscriptionName;
49 }
50 
51 /*!
52  *\brief Returns the object's MQTTTopic children
53  *
54  * \return a vector of pointers to the children of the MQTTSubscription
55  */
57  return children<MQTTTopic>();
58 }
59 
60 /*!
61  *\brief Returns the object's parent
62  *
63  * \return a pointer to the parent MQTTTopic of the object
64  */
66  return m_MQTTClient;
67 }
68 
69 /*!
70  *\brief Called when a message arrived to a topic contained by the MQTTSubscription
71  * If the topic can't be found among the children, a new MQTTTopic is instantiated
72  * Passes the messages to the appropriate MQTTTopic
73  *
74  * \param message the message to pass
75  * \param topicName the name of the topic the message was sent to
76  */
77 void MQTTSubscription::messageArrived(const QString& message, const QString& topicName) {
78  bool found = false;
79  QVector<MQTTTopic*> topics = children<MQTTTopic>();
80  //search for the topic among the MQTTTopic children
81  for (auto* topic: topics) {
82  if (topicName == topic->topicName()) {
83  //pass the message to the topic
84  topic->newMessage(message);
85 
86  //read the message if needed
89  topic->read();
90 
91  found = true;
92  break;
93  }
94  }
95 
96  //if the topic can't be found, we add it as a new MQTTTopic, and read from it if needed
97  if (!found) {
98  auto* newTopic = new MQTTTopic(topicName, this, false);
99  addChildFast(newTopic); //no need for undo/redo here
100  newTopic->newMessage(message);
102  newTopic->read();
103  }
104 }
105 
106 /*!
107  *\brief Returns the subscription's name
108  *
109  * \return m_subscriptionName
110  */
112  return m_subscriptionName;
113 }
114 
115 /*!
116  *\brief Sets the MQTTClient the subscription belongs to
117  *
118  * \param client
119  */
121  m_MQTTClient = client;
122 }
123 
124 /*!
125  *\brief Returns the icon of MQTTSubscription
126  */
127 QIcon MQTTSubscription::icon() const {
128  return QIcon::fromTheme("mail-signed-full");
129 }
130 
131 //##############################################################################
132 //################## Serialization/Deserialization ###########################
133 //##############################################################################
134 /*!
135  Saves as XML.
136  */
137 void MQTTSubscription::save(QXmlStreamWriter* writer) const {
138  writer->writeStartElement("MQTTSubscription");
139  writeBasicAttributes(writer);
140  writeCommentElement(writer);
141 
142  //general
143  writer->writeStartElement("general");
144  writer->writeAttribute("subscriptionName", m_subscriptionName);
145  writer->writeEndElement();
146 
147  //MQTTTopics
148  for (auto* topic : children<MQTTTopic>(AbstractAspect::ChildIndexFlag::IncludeHidden))
149  topic->save(writer);
150 
151  writer->writeEndElement(); // "MQTTSubscription"
152 }
153 
154 /*!
155  Loads from XML.
156 */
157 bool MQTTSubscription::load(XmlStreamReader* reader, bool preview) {
158  if (!readBasicAttributes(reader))
159  return false;
160 
161  QXmlStreamAttributes attribs;
162 
163  while (!reader->atEnd()) {
164  reader->readNext();
165  if (reader->isEndElement() && reader->name() == "MQTTSubscription")
166  break;
167 
168  if (!reader->isStartElement())
169  continue;
170 
171  if (reader->name() == "comment") {
172  if (!readCommentElement(reader))
173  return false;
174  } else if (reader->name() == "general") {
175  attribs = reader->attributes();
176  m_subscriptionName = attribs.value("subscriptionName").toString();
177  } else if(reader->name() == QLatin1String("MQTTTopic")) {
178  auto* topic = new MQTTTopic(QString(), this, false);
179  if (!topic->load(reader, preview)) {
180  delete topic;
181  return false;
182  }
183  addChildFast(topic);
184  } else {// unknown element
185  reader->raiseWarning(i18n("unknown element '%1'", reader->name().toString()));
186  if (!reader->skipToEndElement()) return false;
187  }
188  }
189 
190  emit loaded(this->subscriptionName());
191  return !reader->hasError();
192 }
193 
AspectType
bool readCommentElement(XmlStreamReader *)
Load comment from an XML element.
@ IncludeHidden
Include aspects marked as "hidden" in numbering or listing children.
void addChildFast(AbstractAspect *)
Add the given Aspect to my list of children without any checks and without putting this step onto the...
QString name() const
void writeBasicAttributes(QXmlStreamWriter *) const
Save name and creation time to XML.
bool readBasicAttributes(XmlStreamReader *)
Load name and creation time from XML.
void writeCommentElement(QXmlStreamWriter *) const
Save the comment to XML.
Folder in a project.
Definition: Folder.h:35
The MQTT Client connects to the broker set in ImportFileWidget. It manages the MQTTSubscriptions,...
Definition: MQTTClient.h:48
bool isPaused() const
Provides information about whether the reading is paused or not.
Definition: MQTTClient.cpp:160
UpdateType updateType() const
Returns the MQTTClient's update type.
Definition: MQTTClient.cpp:208
Represents a subscription made in a MQTTClient object. It plays a role in managing MQTTTopic objects ...
QIcon icon() const override
Returns the icon of MQTTSubscription.
const QVector< MQTTTopic * > topics() const
Returns the object's MQTTTopic children.
MQTTClient * mqttClient() const
Returns the object's parent.
void setMQTTClient(MQTTClient *)
Sets the MQTTClient the subscription belongs to.
void messageArrived(const QString &, const QString &)
Called when a message arrived to a topic contained by the MQTTSubscription If the topic can't be foun...
QString m_subscriptionName
QString subscriptionName() const
Returns the subscription's name.
void loaded(const QString &)
~MQTTSubscription() override
bool load(XmlStreamReader *, bool preview) override
MQTTClient * m_MQTTClient
void save(QXmlStreamWriter *) const override
MQTTSubscription(const QString &name)
XML stream parser that supports errors as well as warnings. This class also adds line and column numb...
void raiseWarning(const QString &)
#define i18n(m)
Definition: nsl_common.h:38