Skip to content

Commit 98411c9

Browse files
mxydlsstardust95
authored andcommitted
补充attachment修改异常类 (#7)
* modify exception * modify exception2 * modify account * append attachment
1 parent da0d09f commit 98411c9

File tree

10 files changed

+467
-52
lines changed

10 files changed

+467
-52
lines changed

Exception/MailClientException.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
#ifndef MAILCLIENTEXCEPTION_H
22
#define MAILCLIENTEXCEPTION_H
3-
#include <qstring.h>
3+
#include <QString>
4+
#include <QtDebug>
5+
6+
#include <string>
47
#include <exception>
58
#include <iostream>
69
using namespace std;
710

811
class MailClientException: public exception
912
{
1013
public:
11-
MailClientException() {}
12-
MailClientException(const string& exc): exception(exc.c_str()) {}
13-
MailClientException(const MailClientException& mce): exception(mce) {}
14-
virtual ~MailClientException() throw() = 0;
15-
virtual const char* what() const = 0;
14+
string message;
15+
public:
16+
MailClientException() {
17+
// qDebug() << "MailClientException default Constructor\n";
18+
message = exception::what();
19+
}
20+
MailClientException(const string& exc): exception(exc.c_str()) {
21+
// qDebug() << "MailClientException string Constructor\n";
22+
message = exception::what();
23+
}
24+
MailClientException(const MailClientException& mce): exception(mce){
25+
// qDebug() << "MailClientException copy Constructor\n";
26+
message = exception::what();
27+
}
28+
virtual ~MailClientException() noexcept = 0 {}
29+
virtual const char* what() = 0;
1630
};
1731

18-
MailClientException::~exception() {}
32+
1933

2034
#endif // MAILCLIENTEXCEPTION_H

Exception/MailGenerationException.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
#ifndef MAILGENERATIONEXCEPTION_H
22
#define MAILGENERATIONEXCEPTION_H
33
#include "MailClientException.h"
4+
using namespace std;
45

56
class MailGenerationException: public MailClientException
67
{
78
public:
8-
MailGenerationException() {}
9-
MailGenerationException(const string& exc): MailClientException(exc.c_str()) {}
10-
MailClientException(const MailClientException& mce): MailClientException(mce) {}
9+
MailGenerationException() {
10+
// qDebug() << "MailGenerationException default Constructor\n";
11+
message = "MailGenerationException: " + message;
12+
}
13+
MailGenerationException(const string& exc): MailClientException(exc.c_str()) {
14+
// qDebug() << "MailGenerationException string Constructor\n";
15+
message = "MailGenerationException: " + message;
16+
}
17+
MailGenerationException(const MailGenerationException& mce): MailClientException(mce) {
18+
// qDebug() << "MailGenerationException copy Constructor\n";
19+
message = "MailGenerationException: " + message;
20+
}
1121
~MailGenerationException() override {}
12-
const char* what() const override{
13-
string tmp = MailClientException::what();
14-
tmp = "MailGenerationException: " + tmp;
15-
return tmp.c_str();
22+
const char* what() override{
23+
return message.c_str();
1624
}
1725
};
1826

Exception/MailReceiveException.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
class MailReceiveException: public MailClientException
66
{
77
public:
8-
MailReceiveException() {}
9-
MailReceiveException(const string& exc): MailClientException(exc.c_str()) {}
10-
MailClientException(const MailClientException& mce): MailClientException(mce) {}
8+
MailReceiveException() {
9+
message = "MailReceiveException: " + message;
10+
}
11+
MailReceiveException(const string& exc): MailClientException(exc.c_str()) {
12+
message = "MailReceiveException: " + message;
13+
}
14+
MailReceiveException(const MailReceiveException& mce): MailClientException(mce) {
15+
message = "MailReceiveException: " + message;
16+
}
1117
~MailReceiveException() override {}
12-
const char* what() const override{
13-
string tmp = MailClientException::what();
14-
tmp = "MailGenerationException: " + tmp;
15-
return tmp.c_str();
18+
const char* what() override{
19+
return message.c_str();
1620
}
1721
};
1822

Exception/MailSendException.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
class MailSendException: public MailClientException
66
{
77
public:
8-
MailSendException() {}
9-
MailSendException(const string& exc): MailClientException(exc.c_str()) {}
10-
MailClientException(const MailClientException& mce): MailClientException(mce) {}
8+
MailSendException() {
9+
message = "MailSendException: " + message;
10+
}
11+
MailSendException(const string& exc): MailClientException(exc.c_str()) {
12+
message = "MailSendException: " + message;
13+
}
14+
MailSendException(const MailSendException& mce): MailClientException(mce) {
15+
message = "MailSendException: " + message;
16+
}
1117
~MailSendException() override {}
12-
const char* what() const override{
13-
string tmp = MailClientException::what();
14-
tmp = "MailGenerationException: " + tmp;
15-
return tmp.c_str();
18+
const char* what() override{
19+
return message.c_str();
1620
}
1721
};
1822

MicroMailClient.pro.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 3.6.1, 2016-05-27T22:41:34. -->
3+
<!-- Written by QtCreator 3.6.1, 2016-05-28T20:40:37. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>

Model/Account.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,29 @@
33
#include <QList>
44
#include <QSharedPointer>
55
#include <qvector.h>
6-
#include<iostream>
6+
#include <iostream>
7+
#include <QVariant>
8+
#include <QSharedPointer>
9+
#include <QDebug>
710

811
class Account: public QObject
912
{
13+
Q_OBJECT
14+
15+
Q_PROPERTY(QString username READ getUserName WRITE setUserName)
16+
17+
Q_PROPERTY(QString password READ getPassWord WRITE setPassWord)
18+
19+
Q_PROPERTY(QString mailhost READ getMailHost WRITE setMailHost)
20+
21+
Q_PROPERTY(QString smtphost READ getSMTPHost WRITE setSMTPHost)
22+
23+
Q_PROPERTY(QString pop3host READ getPOP3Host WRITE setPOP3Host)
24+
25+
Q_PROPERTY(QString imaphost READ getIMAPHost WRITE setIMAPHost)
26+
27+
Q_PROPERTY(bool requiressl READ getRequireSSL WRITE setRequireSSL)
28+
1029
QString _userName;
1130
QString _passWord;
1231
QString _mailHost;
@@ -83,6 +102,8 @@ class Account: public QObject
83102
void setRequireSSL(bool i) {
84103
_requireSSL = i;
85104
}
105+
106+
86107
};
87108

88109

Model/Attachment.h

Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,64 @@
22
#define ATTACHMENT_H
33
#include "Poco/Net/PartSource.h"
44
#include "Utils.h"
5-
#include <qstring.h>
5+
#include <QString>
66
#include <iostream>
7+
#include <fstream>
8+
#include <QVariant>
9+
#include <QSharedPointer>
10+
#include <QDebug>
11+
#include <QByteArray>
12+
#include <QFile>
13+
#include "Exception/MailClientException.h"
14+
#include "Exception/MailGenerationException.h"
15+
16+
using namespace std;
717

818
class Attachment : public Poco::Net::PartSource
919
{
20+
private:
21+
typedef enum{unDownloaded, isDownloading, isDownloaded} downloadState;
22+
1023
Utils::MediaType _attachmentType;
11-
size_t _fileSize;
24+
qint64 _fileSize;
25+
qint64 _hasDownloaded;
1226
QString _fileName;
27+
QString _filePath;
28+
downloadState _state;
1329
QString _accessCommand;
30+
fstream _fileStream;
31+
32+
const static qint64 _fileMaxSize = 1024*1024;
33+
34+
int findPostfix(const QString &src) {
35+
int i = src.length() - 1;
36+
while(src[i]!='.') i--;
37+
return i;
38+
}
1439

1540
public:
41+
42+
std::istream& stream() override {
43+
return _fileStream;
44+
}
45+
1646
Attachment(const std::string& mediaType):
17-
_attachmentType(mediaType) {}
47+
_attachmentType(mediaType) {
48+
_state = unDownloaded;
49+
_hasDownloaded = 0;
50+
}
1851

1952
Attachment(const std::string& type, const std::string& subType):
20-
_attachmentType(type, subType) {}
53+
_attachmentType(type, subType) {
54+
_state = unDownloaded;
55+
_hasDownloaded = 0;
56+
}
2157

2258
Attachment(const Utils::MediaType& mediaType):
23-
_attachmentType(mediaType) {}
59+
_attachmentType(mediaType) {
60+
_state = unDownloaded;
61+
_hasDownloaded = 0;
62+
}
2463

2564
Utils::MediaType getAttachmentType() const {
2665
return _attachmentType;
@@ -38,20 +77,75 @@ class Attachment : public Poco::Net::PartSource
3877
return _fileName;
3978
}
4079

41-
void setFileName(QString i) {
80+
void setFileName(const QString& i) {
4281
_fileName = i;
4382
}
4483

84+
QString getFilePath() const {
85+
return _filePath;
86+
}
87+
88+
void setFilePath(const QString& i) {
89+
_filePath = i;
90+
_fileStream.open(_filePath.toStdString(), ios::in|ios::out|ios::binary);
91+
}
92+
4593
QString getAccessCommand() const {
4694
return _accessCommand;
4795
}
4896

49-
void setAccessCommand(QString i) {
97+
void setAccessCommand(const QString& i) {
5098
_accessCommand = i;
5199
}
52100

53-
bool Download(QByteArray a) {
101+
bool Download(const QByteArray& data) {
102+
103+
QString tmp = _filePath;
104+
int t = findPostfix(tmp);
105+
QString postfix = tmp.mid(t, tmp.length()-t);
106+
tmp = tmp.mid(0,t);
107+
if(_state == unDownloaded) { //make sure the file name is not existed
108+
for(int i = 1; QFile::exists(_filePath); i++) {
109+
_filePath = tmp + "(";
110+
_filePath += i + '0';
111+
_filePath += ")";
112+
_filePath += postfix;
113+
}
114+
_state = isDownloading;
115+
}
116+
117+
QFile output(_filePath);
118+
output.open(QIODevice::WriteOnly|QIODevice::Append);
119+
qint64 size = data.size();
120+
output.write(data.data(), size);
121+
output.close();
122+
123+
_hasDownloaded += size;
124+
if(_hasDownloaded == _fileSize)
125+
_state = isDownloaded;
126+
return true;
127+
}
128+
129+
bool Upload(QByteArray& data) {
130+
QFile input(_filePath);
131+
132+
try {
133+
if(input.isOpen())
134+
throw(MailGenerationException("File is being occupied!"));
135+
if(!input.open(QIODevice::ReadOnly)) //file not existed
136+
throw(MailGenerationException("File doesn't exists!"));
137+
qint64 size = input.size();
138+
if(size > _fileMaxSize)
139+
throw(MailGenerationException("File size is beyond limit!"));
140+
} catch(MailGenerationException& mse) {
141+
qDebug() << mse.what() << endl;
142+
return false;
143+
}
144+
145+
data = input.readAll();
54146

147+
input.close();
148+
return true;
55149
}
56150

57151
};

Model/MailBody.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MailBody : public QObject {
2727

2828
Q_PROPERTY(QString datetime READ getDateTime CONSTANT)
2929

30-
Q_PROPERTY(bool isread READ getIsread WRITE setIsread CONSTANT)
30+
Q_PROPERTY(bool isread READ getIsread WRITE setIsread)
3131

3232
// Q_PROPERTY(QList<QString> recipients READ getRecipient CONSTANT)
3333

0 commit comments

Comments
 (0)