Skip to content

Commit

Permalink
补充attachment修改异常类 (#7)
Browse files Browse the repository at this point in the history
* modify exception

* modify exception2

* modify account

* append attachment
  • Loading branch information
mxydls authored and stardust95 committed May 30, 2016
1 parent da0d09f commit 98411c9
Show file tree
Hide file tree
Showing 10 changed files with 467 additions and 52 deletions.
28 changes: 21 additions & 7 deletions Exception/MailClientException.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
#ifndef MAILCLIENTEXCEPTION_H
#define MAILCLIENTEXCEPTION_H
#include <qstring.h>
#include <QString>
#include <QtDebug>

#include <string>
#include <exception>
#include <iostream>
using namespace std;

class MailClientException: public exception
{
public:
MailClientException() {}
MailClientException(const string& exc): exception(exc.c_str()) {}
MailClientException(const MailClientException& mce): exception(mce) {}
virtual ~MailClientException() throw() = 0;
virtual const char* what() const = 0;
string message;
public:
MailClientException() {
// qDebug() << "MailClientException default Constructor\n";
message = exception::what();
}
MailClientException(const string& exc): exception(exc.c_str()) {
// qDebug() << "MailClientException string Constructor\n";
message = exception::what();
}
MailClientException(const MailClientException& mce): exception(mce){
// qDebug() << "MailClientException copy Constructor\n";
message = exception::what();
}
virtual ~MailClientException() noexcept = 0 {}
virtual const char* what() = 0;
};

MailClientException::~exception() {}


#endif // MAILCLIENTEXCEPTION_H
22 changes: 15 additions & 7 deletions Exception/MailGenerationException.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
#ifndef MAILGENERATIONEXCEPTION_H
#define MAILGENERATIONEXCEPTION_H
#include "MailClientException.h"
using namespace std;

class MailGenerationException: public MailClientException
{
public:
MailGenerationException() {}
MailGenerationException(const string& exc): MailClientException(exc.c_str()) {}
MailClientException(const MailClientException& mce): MailClientException(mce) {}
MailGenerationException() {
// qDebug() << "MailGenerationException default Constructor\n";
message = "MailGenerationException: " + message;
}
MailGenerationException(const string& exc): MailClientException(exc.c_str()) {
// qDebug() << "MailGenerationException string Constructor\n";
message = "MailGenerationException: " + message;
}
MailGenerationException(const MailGenerationException& mce): MailClientException(mce) {
// qDebug() << "MailGenerationException copy Constructor\n";
message = "MailGenerationException: " + message;
}
~MailGenerationException() override {}
const char* what() const override{
string tmp = MailClientException::what();
tmp = "MailGenerationException: " + tmp;
return tmp.c_str();
const char* what() override{
return message.c_str();
}
};

Expand Down
18 changes: 11 additions & 7 deletions Exception/MailReceiveException.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
class MailReceiveException: public MailClientException
{
public:
MailReceiveException() {}
MailReceiveException(const string& exc): MailClientException(exc.c_str()) {}
MailClientException(const MailClientException& mce): MailClientException(mce) {}
MailReceiveException() {
message = "MailReceiveException: " + message;
}
MailReceiveException(const string& exc): MailClientException(exc.c_str()) {
message = "MailReceiveException: " + message;
}
MailReceiveException(const MailReceiveException& mce): MailClientException(mce) {
message = "MailReceiveException: " + message;
}
~MailReceiveException() override {}
const char* what() const override{
string tmp = MailClientException::what();
tmp = "MailGenerationException: " + tmp;
return tmp.c_str();
const char* what() override{
return message.c_str();
}
};

Expand Down
18 changes: 11 additions & 7 deletions Exception/MailSendException.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
class MailSendException: public MailClientException
{
public:
MailSendException() {}
MailSendException(const string& exc): MailClientException(exc.c_str()) {}
MailClientException(const MailClientException& mce): MailClientException(mce) {}
MailSendException() {
message = "MailSendException: " + message;
}
MailSendException(const string& exc): MailClientException(exc.c_str()) {
message = "MailSendException: " + message;
}
MailSendException(const MailSendException& mce): MailClientException(mce) {
message = "MailSendException: " + message;
}
~MailSendException() override {}
const char* what() const override{
string tmp = MailClientException::what();
tmp = "MailGenerationException: " + tmp;
return tmp.c_str();
const char* what() override{
return message.c_str();
}
};

Expand Down
2 changes: 1 addition & 1 deletion MicroMailClient.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.6.1, 2016-05-27T22:41:34. -->
<!-- Written by QtCreator 3.6.1, 2016-05-28T20:40:37. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
23 changes: 22 additions & 1 deletion Model/Account.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,29 @@
#include <QList>
#include <QSharedPointer>
#include <qvector.h>
#include<iostream>
#include <iostream>
#include <QVariant>
#include <QSharedPointer>
#include <QDebug>

class Account: public QObject
{
Q_OBJECT

Q_PROPERTY(QString username READ getUserName WRITE setUserName)

Q_PROPERTY(QString password READ getPassWord WRITE setPassWord)

Q_PROPERTY(QString mailhost READ getMailHost WRITE setMailHost)

Q_PROPERTY(QString smtphost READ getSMTPHost WRITE setSMTPHost)

Q_PROPERTY(QString pop3host READ getPOP3Host WRITE setPOP3Host)

Q_PROPERTY(QString imaphost READ getIMAPHost WRITE setIMAPHost)

Q_PROPERTY(bool requiressl READ getRequireSSL WRITE setRequireSSL)

QString _userName;
QString _passWord;
QString _mailHost;
Expand Down Expand Up @@ -83,6 +102,8 @@ class Account: public QObject
void setRequireSSL(bool i) {
_requireSSL = i;
}


};


Expand Down
110 changes: 102 additions & 8 deletions Model/Attachment.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,64 @@
#define ATTACHMENT_H
#include "Poco/Net/PartSource.h"
#include "Utils.h"
#include <qstring.h>
#include <QString>
#include <iostream>
#include <fstream>
#include <QVariant>
#include <QSharedPointer>
#include <QDebug>
#include <QByteArray>
#include <QFile>
#include "Exception/MailClientException.h"
#include "Exception/MailGenerationException.h"

using namespace std;

class Attachment : public Poco::Net::PartSource
{
private:
typedef enum{unDownloaded, isDownloading, isDownloaded} downloadState;

Utils::MediaType _attachmentType;
size_t _fileSize;
qint64 _fileSize;
qint64 _hasDownloaded;
QString _fileName;
QString _filePath;
downloadState _state;
QString _accessCommand;
fstream _fileStream;

const static qint64 _fileMaxSize = 1024*1024;

int findPostfix(const QString &src) {
int i = src.length() - 1;
while(src[i]!='.') i--;
return i;
}

public:

std::istream& stream() override {
return _fileStream;
}

Attachment(const std::string& mediaType):
_attachmentType(mediaType) {}
_attachmentType(mediaType) {
_state = unDownloaded;
_hasDownloaded = 0;
}

Attachment(const std::string& type, const std::string& subType):
_attachmentType(type, subType) {}
_attachmentType(type, subType) {
_state = unDownloaded;
_hasDownloaded = 0;
}

Attachment(const Utils::MediaType& mediaType):
_attachmentType(mediaType) {}
_attachmentType(mediaType) {
_state = unDownloaded;
_hasDownloaded = 0;
}

Utils::MediaType getAttachmentType() const {
return _attachmentType;
Expand All @@ -38,20 +77,75 @@ class Attachment : public Poco::Net::PartSource
return _fileName;
}

void setFileName(QString i) {
void setFileName(const QString& i) {
_fileName = i;
}

QString getFilePath() const {
return _filePath;
}

void setFilePath(const QString& i) {
_filePath = i;
_fileStream.open(_filePath.toStdString(), ios::in|ios::out|ios::binary);
}

QString getAccessCommand() const {
return _accessCommand;
}

void setAccessCommand(QString i) {
void setAccessCommand(const QString& i) {
_accessCommand = i;
}

bool Download(QByteArray a) {
bool Download(const QByteArray& data) {

QString tmp = _filePath;
int t = findPostfix(tmp);
QString postfix = tmp.mid(t, tmp.length()-t);
tmp = tmp.mid(0,t);
if(_state == unDownloaded) { //make sure the file name is not existed
for(int i = 1; QFile::exists(_filePath); i++) {
_filePath = tmp + "(";
_filePath += i + '0';
_filePath += ")";
_filePath += postfix;
}
_state = isDownloading;
}

QFile output(_filePath);
output.open(QIODevice::WriteOnly|QIODevice::Append);
qint64 size = data.size();
output.write(data.data(), size);
output.close();

_hasDownloaded += size;
if(_hasDownloaded == _fileSize)
_state = isDownloaded;
return true;
}

bool Upload(QByteArray& data) {
QFile input(_filePath);

try {
if(input.isOpen())
throw(MailGenerationException("File is being occupied!"));
if(!input.open(QIODevice::ReadOnly)) //file not existed
throw(MailGenerationException("File doesn't exists!"));
qint64 size = input.size();
if(size > _fileMaxSize)
throw(MailGenerationException("File size is beyond limit!"));
} catch(MailGenerationException& mse) {
qDebug() << mse.what() << endl;
return false;
}

data = input.readAll();

input.close();
return true;
}

};
Expand Down
2 changes: 1 addition & 1 deletion Model/MailBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MailBody : public QObject {

Q_PROPERTY(QString datetime READ getDateTime CONSTANT)

Q_PROPERTY(bool isread READ getIsread WRITE setIsread CONSTANT)
Q_PROPERTY(bool isread READ getIsread WRITE setIsread)

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

Expand Down
Loading

0 comments on commit 98411c9

Please sign in to comment.