-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetmsg.cpp
182 lines (153 loc) · 4.52 KB
/
getmsg.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "getmsg.h"
extern QList<MsgNode*> g_msgList;
extern QList<MsgNode*> g_bakList;
static QList<MsgNode*> gs_msgList;
GetMsg::GetMsg()
{
}
int GetMsg::openXml(const QString &filename)
{
QFile rfile(filename); //建立指向“my.xml”文件的QFile对象
if (!rfile.open(QIODevice::ReadOnly))
{
qDebug() << "get msg open err";
return 0; //以只读方式打开
}
m_doc.clear();
if (!m_doc.setContent(&rfile))
{
qDebug() << "set err";
rfile.close();
return 0;
}
//将文件内容读到doc中
rfile.close();
return 1;
}
void GetMsg::getMsg()
{
this->parseNode ();
this->newInfo ();
}
void GetMsg::parseNode()
{
QDomElement docElem = m_doc.documentElement(); //返回根元素
QDomNode n = docElem.firstChild(); //返回根节点的第一个子节点
gs_msgList.clear();
while(!n.isNull())
{
//如果节点不为空
if (n.isElement()) //如果节点是元素
{
QDomElement e = n.toElement(); //将其转换为元素
/*
qDebug() << qPrintable(e.tagName()) //返回元素标记
<< qPrintable(e.attribute("id")) //返回元素id属性的值
<< qPrintable(e.attribute("title"))
<< qPrintable(e.attribute("timestamp"))
<< qPrintable(e.text());
*/
// 获取发送时间
long tms = e.attribute("timestamp").toLong();
QDateTime dateTime;
dateTime.setTime_t(tms);
MsgNode* node = new MsgNode;
node->id = e.attribute("id").toInt();
node->title = e.attribute("title");
node->timeCnt = e.attribute("timestamp").toLong();
node->msgText = e.text();
node->sendTime = dateTime.toString("yyyy/MM/dd hh:mm:ss");
node->read = false;
node->sendType = e.attribute("send_type");
node->newsType = e.attribute("news_type");
if (node->newsType == "meeting")
{
node->type = 'A';
}
else if (node->newsType == "policy")
{
node->type = 'B';
}
else if (node->newsType == "personnel")
{
node->type = 'C';
}
gs_msgList.append(node);
}
n = n.nextSibling(); //下一个兄弟节点
}
}
void GetMsg::newInfo()
{
// 首次刷新
if (0 == g_msgList.count())
{
for (int i = 0; i < gs_msgList.count(); ++i)
{
gs_msgList.at(i)->isNewInfo = true;
g_msgList.push_back(gs_msgList.at(i));
// 对比备份信息
for (int j = 0; j < g_bakList.count(); ++j)
{
if (g_bakList.at(j)->id == g_msgList.at(i)->id)
{
g_msgList.at(i)->isNewInfo = false;
g_msgList.at(i)->read = g_bakList.at(j)->read;
break;
}
}
}
// 清空备份链表
for (int i = 0; i < g_bakList.count(); ++i)
{
free (g_bakList.at(i));
}
g_bakList.clear();
return ;
}
// 上次的消息全部标记为旧消息
for (int i = 0; i < g_msgList.count(); ++i)
{
g_msgList.at(i)->isNewInfo = false;
}
// 对比,添加新消息
MsgNode* pNode = NULL;
MsgNode* pNode0 = g_msgList.at(0);
for (int i = gs_msgList.count() - 1; i >= 0; --i)
{
if (gs_msgList.at(i)->timeCnt > pNode0->timeCnt
&& gs_msgList.at(i)->id > pNode0->id)
{
pNode = gs_msgList.at(i);
pNode->isNewInfo = true;
g_msgList.push_front(pNode);
}
}
// 删除服务器已经删除的消息
bool flag = false;
for (int i = 0; i < g_msgList.count(); ++i)
{
flag = false;
for (int j = 0; j < gs_msgList.count(); ++j)
{
if (gs_msgList.at(j)->id == g_msgList.at(i)->id)
{
flag = true;
break;
}
}
if (!flag)
{
g_msgList.removeAt(i);
}
}
// qDebug () << "g_msgList: " << g_msgList.count();
}
void GetMsg::sortMsg()
{
// qSort (g_msgList.begin(), g_msgList.end(), this->compareTime());
}
bool GetMsg::compareTime()
{
return true;
}