-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.cpp
More file actions
38 lines (35 loc) · 1.31 KB
/
classes.cpp
File metadata and controls
38 lines (35 loc) · 1.31 KB
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
#include "classes.h"
#include <QString>
#include <QtSql>
std::vector<uint8_t> Message::getLoraPacket()
{
std::vector<uint8_t> result;
result.push_back(channelNumber);
std::string stdName = senderName.toStdString();
result.insert(result.end(), stdName.begin(), stdName.end());
std::string stdText = text.toStdString();
result.insert(result.end(), stdText.begin(), stdText.end());
return result;
}
QListWidgetItem *Message::getListItem()
{
QListWidgetItem *item = new QListWidgetItem;
QString zwspText(text.split("", Qt::SkipEmptyParts).join("\u200b"));
item->setText(QString("%1: %2").arg(senderName, zwspText)); // time.toString("d.M.yyyy h:m:s"),
return item;
}
void Message::save()
{
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
query.prepare("INSERT INTO messages (senderName, text, channelNumber, time) VALUES (:senderName, :text, :channelNumber, :time);");
query.bindValue(":senderName", senderName);
query.bindValue(":text", text);
query.bindValue(":channelNumber", channelNumber);
query.bindValue(":time", time.toSecsSinceEpoch());
if (!query.exec()) {
qDebug() << query.lastError().text();
throw std::runtime_error("Can't insert message to \"messages\": "+query.lastError().text().toStdString());
}
db.commit();
}