/
faxtomailmodule.cpp
173 lines (149 loc) · 5.97 KB
/
faxtomailmodule.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
#include "faxtomailmodule.h"
#include "smtp.h"
#include "Base64.h"
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QPrinter>
#include <QTimer>
//#include <QTest>
#include <QPainter>
#include <QPrintPreviewDialog>
#include <QSemaphore>
#include "systemsource.h"
FaxToMailModule::FaxToMailModule(SystemSource *SysS,QWidget *parent) :
QWidget(parent)
{
MailServer="smtp.sogou.com";
MailPassword="so123456";
MailUsername="fax_test@sogou.com";
MailFrom="fax_test@sogou.com";
this->SysS=SysS;
FaxToMailTimer.setSingleShot(true);
connect(&FaxToMailTimer, SIGNAL(timeout()), this, SLOT(Process()));
FaxToMailTimer.start(1*1000);
}
bool FaxToMailModule::FaxIdentify(QString qcode,QString &Addr)
{
if (qcode=="147") Addr="ruixuejishu@sina.com";
else if (qcode=="130") Addr="ruixuegufen@sina.com";
else Addr="ruixuegufen@sina.com";
return true;
}
qint8 FaxToMailModule::Process()
{
qDebug()<<"FaxToMailProcess Start\n";
this->FIFolder.setPath(this->SysS->faxInfoFolderPath);//FaxInfoFolder
this->FDFolder.setPath(this->SysS->faxDataFolderPath);//FaxDateFolder
this->FileFilter.clear();
this->FileFilter.append("*.FI");
this->FIFolder.setNameFilters(this->FileFilter);
qint32 FileCount=this->FIFolder.count();
qDebug()<<"FileCount="<<FileCount<<"\n";
QStringList FIFileList = this->FIFolder.entryList(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot);
while (!FIFileList.isEmpty())
{
QString FIFilePath,qcode;
//qDebug()<<FIFileList.first();
FIFilePath.clear();
FIFilePath.append(this->FIFolder.absolutePath()+"/"+FIFileList.first());
qDebug()<<FIFilePath;
QFile FIfile(FIFilePath);
if (!FIfile.open(QIODevice::ReadWrite|QIODevice::Text)) return -1;
QByteArray tmp;
tmp=FIfile.readLine();
qDebug()<<"Identify code="<<QString(tmp);
//qcode=QString(tmp);
tmp=FIfile.readLine();
qDebug()<<"Identify Mailaddr="<<QString(tmp);
this->MailTo=QString(tmp);
tmp=FIfile.readLine();
while(!QString(tmp).isEmpty())
{
//qDebug()<<QString(tmp);
FDFilePath.clear();
FDFilePath.append(tmp);
FDFilePath.remove("\n");
//qDebug()<<FDFilePath;
//Re=FaxIdentify(qcode,MailTo);
if (Re==true)
{
qDebug()<<"begin to print\n";
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOrientation(QPrinter::Landscape);
printer.setPageSize(QPrinter::B0);
printer.setOutputFileName("/tmp/nonwritable.pdf");
QPainter painter;
QImage Image;
Image.load(FDFilePath);
qDebug()<<Image.size();
if (! painter.begin(&printer))
{ // failed to open file
qWarning("failed to open file, is it writable?");
return -1;
}
painter.drawImage(0,0,Image);
/*
QFont font("Times",50);
QString text = tr("Hello World");
painter.setPen(Qt::blue);
painter.setFont(font);
painter.drawText(QRect(0,0, 1000, 100), text);
*/
painter.end();
QFile PDFFile("/tmp/nonwritable.pdf");
if(PDFFile.open(QIODevice::ReadOnly))
{
QByteArray ls;
ls=PDFFile.readAll();
PDFFile.close();
//qDebug()<<"File Size="<<ls.size();
this->MailAttachment=ls.toBase64();
//qDebug()<<ls.toBase64();
if (this->CheckMailServerInfo()<0)
{
qDebug()<<"Error:MailServerInfo="<<this->CheckMailServerInfo();
}
else
{
QList<QString> bcc;
bcc<<"dlbn@sina.com";
Smtp *mail=new Smtp(this->MailServer,this->MailFrom,this->MailTo,bcc,"You have a fax !!!!!","You got a fax!!!",true,this->MailAttachment);
mail->current_user_name=this->MailUsername;
mail->current_password=this->MailPassword;
//qDebug()<<"ready,begin to sent mail";
mail->send();
//mail->~Smtp();
}
}
else
{
qFatal("Can Not find the PDF file!!!");
}
}
else
{
}
tmp=FIfile.readLine();
}
FIfile.close();
FIfile.remove(FIfile.fileName());
FIFileList.removeFirst();
}
//}
FaxToMailTimer.start(3*1000);
qDebug()<<"FaxToMailProcess End\n";
return true;
}
qint8 FaxToMailModule::CheckMailServerInfo()
{
if (this->MailServer.isNull()) return -1;
if (this->MailPassword.isNull()) return -2;
if (this->MailUsername.isNull()) return -3;
return true;
}
qint8 FaxToMailModule::SetMailServerInfo()
{
return true;
}