dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
notificationitem.cpp
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 
30 // Qt headers
31 #include <QtCore/QDataStream>
32 #include <QFile>
33 
34 // GCS headers
36 #include "utils/pathutils.h"
38 #include "uavobjects/uavobject.h"
39 
40 // Notify plugin headers
41 #include "notificationitem.h"
42 #include "notifylogging.h"
43 
46 
48  : QObject(parent)
49  , isNowPlaying(0)
50  , _isPlayed(false)
51  , _currentUpdatePlayed(false)
52  , _timer(NULL)
53  , _expireTimer(NULL)
54  , _soundCollectionPath("")
55  , _currentLanguage("default")
56  , _dataObject("")
57  , _objectField("")
58  , _condition(0)
59  , _sound1("")
60  , _sound2("")
61  , _sound3("")
62  , _sayOrder(never)
63  , _singleValue(0)
64  , _valueRange2(0)
65  , _repeatValue(repeatInstantly)
66  , _expireTimeout(eDefaultTimeout)
67  , _mute(false)
68 {
69 
71  NotificationItem::sayOrderValues.insert(never, QString(tr("Never")));
72  NotificationItem::sayOrderValues.insert(beforeFirst, QString(tr("Before first")));
73  NotificationItem::sayOrderValues.insert(beforeSecond, QString(tr("Before second")));
74  NotificationItem::sayOrderValues.insert(afterSecond, QString(tr("After second")));
75 
77  NotificationItem::retryValues.insert(repeatOnce, QString(tr("Repeat Once")));
79  QString(tr("Repeat Once per update")));
80  NotificationItem::retryValues.insert(repeatInstantly, QString(tr("Repeat Instantly")));
81  NotificationItem::retryValues.insert(repeat10seconds, QString(tr("Repeat 10 seconds")));
82  NotificationItem::retryValues.insert(repeat30seconds, QString(tr("Repeat 30 seconds")));
83  NotificationItem::retryValues.insert(repeat1minute, QString(tr("Repeat 1 minute")));
84 }
85 
87 {
88  that->isNowPlaying = isNowPlaying;
89  that->_isPlayed = _isPlayed;
90  that->_soundCollectionPath = _soundCollectionPath;
91  that->_currentLanguage = _currentLanguage;
92  that->_soundCollectionPath = _soundCollectionPath;
93  that->_dataObject = _dataObject;
94  that->_objectField = _objectField;
95  that->_condition = _condition;
96  that->_sound1 = _sound1;
97  that->_sound2 = _sound2;
98  that->_sound3 = _sound3;
99  that->_sayOrder = _sayOrder;
100  that->_singleValue = _singleValue;
101  that->_valueRange2 = _valueRange2;
102  that->_repeatValue = _repeatValue;
103  that->_expireTimeout = _expireTimeout;
104  that->_mute = _mute;
105 }
106 
107 void NotificationItem::saveState(QSettings *settings) const
108 {
109  settings->setValue("SoundCollectionPath",
110  Utils::PathUtils().RemoveDataPath(getSoundCollectionPath()));
111  settings->setValue(QLatin1String("CurrentLanguage"), getCurrentLanguage());
112  settings->setValue(QLatin1String("ObjectField"), getObjectField());
113  settings->setValue(QLatin1String("DataObject"), getDataObject());
114  settings->setValue(QLatin1String("RangeLimit"), getCondition());
115  settings->setValue(QLatin1String("Value1"), singleValue());
116  settings->setValue(QLatin1String("Value2"), valueRange2());
117  settings->setValue(QLatin1String("Sound1"), getSound1());
118  settings->setValue(QLatin1String("Sound2"), getSound2());
119  settings->setValue(QLatin1String("Sound3"), getSound3());
120  settings->setValue(QLatin1String("SayOrder"), getSayOrder());
121  settings->setValue(QLatin1String("Repeat"), retryValue());
122  settings->setValue(QLatin1String("ExpireTimeout"), lifetime());
123  settings->setValue(QLatin1String("Mute"), mute());
124 }
125 
126 void NotificationItem::restoreState(QSettings *settings)
127 {
128  // settings = Core::ICore::instance()->settings();
129  setSoundCollectionPath(Utils::PathUtils().InsertDataPath(
130  settings->value(QLatin1String("SoundCollectionPath"), tr("")).toString()));
131  setCurrentLanguage(settings->value(QLatin1String("CurrentLanguage"), tr("")).toString());
132  setDataObject(settings->value(QLatin1String("DataObject"), tr("")).toString());
133  setObjectField(settings->value(QLatin1String("ObjectField"), tr("")).toString());
134  setCondition(settings->value(QLatin1String("RangeLimit"), tr("")).toInt());
135  setSound1(settings->value(QLatin1String("Sound1"), tr("")).toString());
136  setSound2(settings->value(QLatin1String("Sound2"), tr("")).toString());
137  setSound3(settings->value(QLatin1String("Sound3"), tr("")).toString());
138  setSayOrder(settings->value(QLatin1String("SayOrder"), tr("")).toInt());
139  QVariant value = settings->value(QLatin1String("Value1"), tr(""));
140  setSingleValue(value);
141  setValueRange2(settings->value(QLatin1String("Value2"), tr("")).toDouble());
142  setRetryValue(settings->value(QLatin1String("Repeat"), tr("")).toInt());
143  setLifetime(settings->value(QLatin1String("ExpireTimeout"), tr("")).toInt());
144  setMute(settings->value(QLatin1String("Mute"), tr("")).toInt());
145 }
146 
147 void NotificationItem::serialize(QDataStream &stream)
148 {
149  stream << this->_soundCollectionPath;
150  stream << this->_currentLanguage;
151  stream << this->_dataObject;
152  stream << this->_objectField;
153  stream << this->_condition;
154  qNotifyDebug() << "getOptionsPageValues seriaize" << _condition;
155  stream << this->_sound1;
156  stream << this->_sound2;
157  stream << this->_sound3;
158  stream << this->_sayOrder;
159  stream << this->_singleValue;
160  stream << this->_valueRange2;
161  stream << this->_repeatValue;
162  stream << this->_expireTimeout;
163  stream << this->_mute;
164 }
165 
166 void NotificationItem::deserialize(QDataStream &stream)
167 {
168  stream >> this->_soundCollectionPath;
169  stream >> this->_currentLanguage;
170  stream >> this->_dataObject;
171  stream >> this->_objectField;
172  stream >> this->_condition;
173  stream >> this->_sound1;
174  stream >> this->_sound2;
175  stream >> this->_sound3;
176  stream >> this->_sayOrder;
177  stream >> this->_singleValue;
178  stream >> this->_valueRange2;
179  stream >> this->_repeatValue;
180  stream >> this->_expireTimeout;
181  stream >> this->_mute;
182 }
183 
185 {
186  if (!_timer) {
187  _timer = new QTimer(this);
188  _timer->setInterval(msec);
189  }
190  if (!_timer->isActive())
191  _timer->start();
192 }
193 
195 {
196  if (!_timer) {
197  if (!_timer->isActive())
198  _timer->start();
199  }
200 }
201 
203 {
204  if (_timer) {
205  if (_timer->isActive())
206  _timer->stop();
207  }
208 }
209 
211 {
212  if (_timer) {
213  _timer->stop();
214  delete _timer;
215  _timer = NULL;
216  }
217 }
218 
220 {
221  if (!_expireTimer) {
222  _expireTimer = new QTimer(this);
223  }
224  _expireTimer->start(_expireTimeout * 1000);
225 }
226 
228 {
229  if (_expireTimer) {
230  if (_expireTimer)
231  _expireTimer->stop();
232  }
233 }
234 
236 {
237  if (_expireTimer) {
238  _expireTimer->stop();
239  delete _expireTimer;
240  _expireTimer = NULL;
241  }
242 }
243 
244 int getValuePosition(QString sayOrder)
245 {
246  return NotificationItem::sayOrderValues.indexOf(sayOrder) - 1;
247 }
248 
249 QString NotificationItem::checkSoundExists(QString fileName)
250 {
251  QString name(fileName + ".wav");
252  QString filePath = QDir::toNativeSeparators(getSoundCollectionPath() + "/"
253  + getCurrentLanguage() + "/" + name);
254  if (QFile::exists(filePath))
255  return filePath;
256  else {
257  filePath = QDir::toNativeSeparators(getSoundCollectionPath() + "/default/" + name);
258  if (!QFile::exists(filePath))
259  filePath.clear();
260  }
261  return filePath;
262 }
263 
264 QStringList valueToSoundList(QString value)
265 {
266  qNotifyDebug() << "notificationItem valueToSoundList input param" << value;
267  // replace point chr if exists
268  value = value.replace(',', '.');
269  QStringList numberParts = value.trimmed().split(".");
270  QStringList digitWavs;
271  bool negative = false;
272  if (numberParts.at(0).toInt() < 0) {
273  negative = true;
274  digitWavs.append("minus");
275  numberParts[0] = QString::number(numberParts.at(0).toInt() * -1);
276  }
277  if ((numberParts.at(0).size() == 1) || (numberParts.at(0).toInt() < 20)) {
278  // [1] check, is this number < 20, these numbers played by one wav file
279  digitWavs.append(numberParts.at(0));
280  } else {
281  int i = 0;
282  // [2] store two lowest digits of number
283  int num = numberParts.at(0).right(2).toInt();
284  if (num < 20 && num != 0) {
285  // store eighter number in range [0...10) or in range [10...20)
286  digitWavs.append(numberParts.at(0).right(1 + num / 11));
287  i = 2;
288  }
289  // [3] prepend 100 and 1000 digits of number
290  for (; i < numberParts.at(0).size(); i++) {
291  int offset = 0;
292  if (negative)
293  offset = 1;
294  digitWavs.insert(offset, numberParts.at(0).at(numberParts.at(0).size() - i - 1));
295  if (digitWavs.at(offset) == QString("0")) {
296  digitWavs.removeAt(offset);
297  continue;
298  }
299  if (i == 1)
300  digitWavs.replace(0 + offset, digitWavs.at(offset) + '0');
301  if (i == 2)
302  digitWavs.insert(1 + offset, "100");
303  if (i == 3)
304  digitWavs.insert(1 + offset, "1000");
305  }
306  }
307  // check, is there fractional part of number?
308  if (1 < numberParts.size()) {
309  digitWavs.append("point");
310  if (numberParts.at(1).size() == 1) {
311  // this mean -> number < 1
312  digitWavs.append(numberParts.at(1));
313  } else {
314  // append fractional part of number
315  QString left = numberParts.at(1).left(1);
316  (left == "0") ? digitWavs.append(left) : digitWavs.append(left + '0');
317  digitWavs.append(numberParts.at(1).right(1));
318  }
319  }
320  qNotifyDebug() << "notificationItem valueToSoundList return value" << digitWavs;
321  return digitWavs;
322 }
323 
324 QString stringFromValue(QVariant value, UAVObjectField *field)
325 {
326  if (field == NULL)
327  return "";
328  Q_ASSERT(field);
329  Q_ASSERT(!value.isNull());
330  QString str;
331  if (UAVObjectField::ENUM == field->getType()) {
332  if (!field->getOptions().contains(value.toString()))
333  return QString();
334  str = value.toString();
335  } else {
336  str = QString("%L1").arg(value.toDouble());
337  }
338  return str;
339 }
340 
342 {
343  QString str;
345  QString value = stringFromValue(singleValue(), field);
346 
347  int pos = getSayOrder() - 1;
348  QStringList lst;
349  lst.append(getSoundCaption(getSound1()));
350  lst.append(getSoundCaption(getSound2()));
351  lst.append(getSoundCaption(getSound3()));
352  QStringList valueSounds = valueToSoundList(value);
353  bool missed = false;
354  foreach (QString sound, valueSounds) {
355  if (checkSoundExists(sound).isEmpty()) {
356  missed = true;
357  break;
358  }
359  }
360 
361  // if not "Never" case
362  if (-1 != pos) {
363  if (missed)
364  lst.insert(pos, "[missed]" + value);
365  else
366  lst.insert(pos, value);
367  }
368  str = lst.join(" ");
369  return str;
370 }
371 
373 {
374  // tips:
375  // check of *.wav files exist needed for playing phonon queues;
376  // if phonon player don't find next file in queue, it buzz
378  QString value = stringFromValue(singleValue(), field);
379 
380  // generate queue of sound files to play
381  _messageSequence.clear();
382  int pos = getSayOrder() - 1;
383  QStringList lst;
384  if (!getSound1().isEmpty())
385  lst.append(getSound1());
386  if (!getSound2().isEmpty())
387  lst.append(getSound2());
388  if (!getSound3().isEmpty())
389  lst.append(getSound3());
390 
391  // if not "Never" case
392  if (-1 != pos) {
393  QStringList valueSounds = valueToSoundList(value);
394  foreach (QString sound, valueSounds)
395  lst.insert(pos++, sound);
396  }
397 
398  foreach (QString sound, lst) {
399  QString path = checkSoundExists(sound);
400  if (!path.isEmpty()) {
401  _messageSequence.append(path);
402  } else {
403  _messageSequence.clear();
404  break;
405  }
406  }
407  return _messageSequence;
408 }
409 
410 QString NotificationItem::getSoundCaption(QString fileName)
411 {
412  if (fileName.isEmpty())
413  return QString();
414  if (checkSoundExists(fileName).isEmpty()) {
415  return QString("[missed]") + fileName;
416  }
417  return fileName;
418 }
419 
421 {
422  return getUAVObject()->getField(getObjectField());
423 }
424 
426 {
427  return dynamic_cast<UAVDataObject *>(
428  (ExtensionSystem::PluginManager::instance()->getObject<UAVObjectManager>())
429  ->getObject(getDataObject()));
430 }
QVariant singleValue() const
Uses to logging only inside notify plugin, can be convinient turned on/off.
void setObjectField(QString text)
QStringList & toSoundList()
QString getObjectField() const
void setMute(bool value)
QString getCurrentLanguage() const
void copyTo(NotificationItem *) const
int getSayOrder() const
int getCondition() const
for i
Definition: OPPlots.m:140
void setSayOrder(int text)
int getValuePosition(QString sayOrder)
QString stringFromValue(QVariant value, UAVObjectField *field)
void saveState(QSettings *settings) const
bool mute() const
QDebug qNotifyDebug()
QStringList getOptions() const
void restoreState(QSettings *settings)
void setDataObject(QString text)
static QStringList sayOrderValues
UAVObjectField * getField(const QString &name)
Definition: uavobject.cpp:236
int lifetime() const
void setSoundCollectionPath(QString path)
static QStringList retryValues
QString getSoundCollectionPath() const
void startTimer(int value)
double valueRange2() const
NotificationItem(QObject *parent=nullptr)
UAVObjectField * getUAVObjectField(void)
QString getDataObject() const
void deserialize(QDataStream &stream)
void serialize(QDataStream &stream)
QStringList valueToSoundList(QString value)
int retryValue() const
void setCurrentLanguage(QString text)
void setSingleValue(QVariant value)
UAVDataObject * getUAVObject(void)
void setCondition(int value)
QString getSoundCaption(QString fileName)
void setLifetime(int value)
void setRetryValue(int value)
void setValueRange2(double value)
FieldType getType() const