/
logic.cpp
188 lines (174 loc) · 5.83 KB
/
logic.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
183
184
185
186
187
188
#include "logic.h"
#include <QEventLoop>
#include <QDebug>
Logic::Logic(QObject *parent) :
QObject(parent)
{
}
void Logic::ParsingFile(int NumberMessage,QString filter,QString& Output)
{
int i = NumberMessage;
QFile textFile("Message"+QString::number(i,10)+".txt");
textFile.open(QIODevice::ReadOnly | QIODevice::Text);
bool exit = false;
while(!exit)
{
QString textLine = textFile.readLine();
if(textLine.startsWith(filter))
{
Output = textLine;
exit = true;
}
}
textFile.close();
}
void Logic::ParsingFileForInputMessage(int NumberMessage,QString& Output)
{
if(NumberMessage!=0)
{
int i = NumberMessage-1;
QFile textFile("send_message"+QString::number(i,10)+".txt");
textFile.open(QIODevice::ReadOnly | QIODevice::Text);
Output = textFile.readAll();
textFile.close();
}
}
void Logic::ParsingFile(int NumberMessage,QString filterStart,QString filterFinish,QString& Output)
{
int i = NumberMessage;
QFile textFile("Message"+QString::number(i,10)+".txt");
textFile.open(QIODevice::ReadOnly | QIODevice::Text);
bool exit = false;
bool base64 = false;
while(!exit)
{
bool flag = true;
QString textLine = textFile.readLine();
if(textLine.startsWith("Content-Transfer-Encoding: base64"))
base64 = true;
if(textLine.startsWith(filterStart))
{
while(!exit)
{
Output += textLine;
textLine = textFile.readLine();
if(textFile.atEnd())exit = true;
}
}
Output.replace(0,4,"\n");
}
if(base64)
{
QByteArray xcode("");;
xcode.append(Output);
QByteArray precode(QByteArray::fromBase64(xcode));
Output = precode.data();
}
textFile.close();
}
int Logic::GetCountMessage(QString strInput)
{
QString output;
for(int i = 0;i<strInput.length();i++)
{
if(strInput[i].isDigit())
{
for(int j=i;j<strInput.length();j++)
{
output.append(strInput[j]);
if(!strInput[j+1].isDigit())
j = strInput.length();
}
i = strInput.length();
}
}
return output.toInt(0,10);
}
void Logic::SetUp()
{
qDebug("SetUp begin");
settingObject = new QSettings("setting.ini",QSettings::IniFormat,0);
settingObject->beginGroup("Proxy");
if(settingObject->value("UseProxy","0").toString() == "1")
{
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
proxy.setHostName(settingObject->value("ProxyServer","0").toString());
proxy.setPort(settingObject->value("ProxyPort","0").toInt());
proxy.setUser(settingObject->value("ProxyUserName","0").toString());
proxy.setPassword(settingObject->value("ProxyPassword","0").toString());
QNetworkProxy::setApplicationProxy(proxy);
}
// else
// {
// QNetworkProxy proxy;
// QNetworkProxy::setApplicationProxy(proxy);
// }
settingObject->endGroup();
settingObject->beginGroup("Message");
CountReceiveMessage = settingObject->value("CountMessage", "0").toInt();
qDebug("CountReceiveMessage: " + settingObject->value("CountMessage", "0").toString().toLatin1());
CountSentMessage = settingObject->value("CountSendMessage","0").toInt();
settingObject->endGroup();
qDebug("SetUp end");
}
void Logic::UpdateCountSentMessage()
{
QSettings* setting = new QSettings("setting.ini",QSettings::IniFormat,0);
setting->beginGroup("Message");
setting->setValue("CountSendMessage",QString::number(CountSentMessage,10));
setting->endGroup();
}
void Logic::ReceiveMessages()
{
settingObject->beginGroup("MailServerSettings");
QString hostStr = settingObject->value("MailServer","0").toString();
qDebug(hostStr.toLatin1());
int port = settingObject->value("Port","0").toInt();
QString Login = settingObject->value("Login","0").toString();
qDebug(Login.toLatin1());
QString Pass = settingObject->value("Password","0").toString();
qDebug(Pass.toLatin1());
settingObject->endGroup();
pop = new POP3();
pop->Connect(hostStr,port);
pop->Login(Login, Pass);
QString CountMessageString = pop->doCommand("stat\r\n",false);
CountReceiveMessage = GetCountMessage(CountMessageString);
settingObject->beginGroup("Message");
settingObject->setValue("CountMessage",QString::number(CountReceiveMessage,10));
settingObject->endGroup();
for(int i = 1;i<=CountReceiveMessage;i++)
{
QString message;
pop->GetMessage(QString::number(i,10), message);
QFile textFile("Message"+QString::number(i,10)+".txt");
textFile.open(QIODevice::WriteOnly | QIODevice::Text);
//***********textFile.write((const char *)message.toAscii());
textFile.write((const char *)message.toLatin1());
textFile.close();
}
pop->Quit();
}
void Logic::SendMessageW(QString from, QStringList to,QString subject,QString body)
{
//We create an event loop and connect to it to prevent the main thread from being crippled when sending large or slow emails
QEventLoop loop;
Smtp *smtp = new Smtp(strServerName, strLogin, strPassword, from, to, subject, body);
QObject::connect(smtp, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
CountSentMessage++;
UpdateCountSentMessage();
QFile textFile;
bool endfile;
endfile = true;
for (int i=0;endfile;i++)
{
textFile.setFileName(QString("send_message%1.txt").arg(i));
if (!textFile.exists())endfile=false;
}
textFile.open(QIODevice::WriteOnly | QIODevice::Text);
//*******************textFile.write((const char *)body.toAscii());
textFile.write((const char *)body.toLatin1());
textFile.close();
}