-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.cpp
131 lines (105 loc) · 3.16 KB
/
message.cpp
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
#include "message.h"
#define MESSAGE_FILE "message.txt"
#define SEP_DATA ","
void Message::send()
{
QFile message(MESSAGE_FILE);
QTextStream out(&message);
if (!message.open(QIODevice::Append | QIODevice::Text))
return;
out << this->toString();
message.close();
}
void Message::setSubject(const QString &subject)
{
this->subject = subject;
}
void Message::setText(const QString &text)
{
this->text = text;
}
Message::Message(const QString &sender, const QString &receiver)
{
this->code = QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz");
this->isRead = false;
this->sender = sender;
this->receiver = receiver;
this->subject = "No Subject";
this->text = "";
}
QString Message::toString()
{
QStringList qsl;
qsl << this->code << this->sender << this->receiver << this->subject << this->text << QString::number(this->isRead);
return qsl.join(SEP_DATA) + "/";
}
QMap<QString, QStringList> Message::loadMessages()
{
QMap<QString, QStringList> msgdb;
QFile msgdata(MESSAGE_FILE);
QTextStream in(&msgdata);
msgdata.open(QIODevice::ReadOnly | QIODevice::Text);
QStringList allmsg = in.readAll().split("/");
for (auto it = allmsg.constBegin(); it != allmsg.constEnd(); ++it)
{
QStringList msg = it->split(SEP_DATA);
if (!msg[0].length())
break;
for (int i = 1; i < msg.size(); ++i)
msgdb[msg[0]] << msg.at(i);
}
msgdata.close();
return msgdb;
}
bool Message::saveChanges(QMap<QString, QStringList> &data)
{
QFile new_data(MESSAGE_FILE);
QTextStream out(&new_data);
if (!new_data.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
for (auto it = data.constBegin(); it != data.constEnd(); ++it)
out << it.key() << SEP_DATA << it.value().join(SEP_DATA) << "/";
new_data.close();
return true;
}
void Message::deleteMessages(const QString &username)
{
QMap<QString, QStringList> msgdb = loadMessages();
for (auto it = msgdb.constBegin(); it != msgdb.constEnd();)
{
if (it.value().at(1) == username)
msgdb.erase(it++);
else
++it;
}
saveChanges(msgdb);
}
int Message::numUnreadMessages(const QString &username)
{
int cnt = 0;
QFile message(MESSAGE_FILE);
QTextStream in(&message);
if (!message.open(QIODevice::ReadOnly | QIODevice::Text))
return cnt;
QStringList messages = in.readAll().split("/");
for (auto it = messages.constBegin(); it != messages.constEnd(); ++it)
{
QStringList msg = it->split(SEP_DATA);
if (!msg[0].length())
break;
if (msg[2] == username && !msg[5].toInt())
cnt++;
}
message.close();
return cnt;
}
bool Message::isSend(const QString &date, const QString &receiver, QString book_isbn, QString sender)
{
QMap<QString, QStringList> msgdb = loadMessages();
for (auto it = msgdb.constBegin(); it != msgdb.constEnd(); ++it)
{
if (it.key().startsWith(date) && it.value().at(0) == sender && it.value().at(1) == receiver && it.value().at(3).contains(book_isbn))
return true;
}
return false;
}