00001
00002
00003
00004
00005
00006
00007 #include <sys/types.h>
00008 #include <sys/stat.h>
00009 #ifndef WIN32
00010 #include <unistd.h>
00011 #endif
00012 #include <boost/lexical_cast.hpp>
00013
00014 #include <WApplication>
00015 #include <WCheckBox>
00016 #include <WText>
00017 #include <WFileUpload>
00018
00019 #include "Attachment.h"
00020 #include "AttachmentEdit.h"
00021 #include "Composer.h"
00022 #include "Option.h"
00023
00024 AttachmentEdit::AttachmentEdit(Composer *composer, WContainerWidget *parent)
00025 : WContainerWidget(parent),
00026 composer_(composer),
00027 uploadFailed_(false),
00028 taken_(false)
00029 {
00030
00031
00032
00033 upload_ = new WFileUpload(this);
00034 upload_->setFileTextSize(40);
00035
00036
00037
00038
00039 remove_ = new Option(tr("msg.remove"), this);
00040 upload_->decorationStyle().font().setSize(WFont::Smaller);
00041 remove_->setMargin(5, Left);
00042 remove_->clicked.connect(SLOT(this, WWidget::hide));
00043 remove_->clicked.connect(SLOT(this, AttachmentEdit::remove));
00044
00045
00046
00047
00048
00049
00050 keep_ = new WCheckBox(this);
00051 keep_->hide();
00052
00053
00054 uploaded_ = new WText("", this);
00055 uploaded_->setStyleClass("option");
00056 uploaded_->hide();
00057
00058
00059 error_ = new WText("", this);
00060 error_->setStyleClass("error");
00061 error_->setMargin(WLength(5), Left);
00062
00063
00064
00065
00066
00067
00068
00069 upload_->changed.connect(SLOT(upload_, WFileUpload::upload));
00070
00071
00072 upload_->uploaded.connect(SLOT(this, AttachmentEdit::uploaded));
00073
00074
00075 upload_->fileTooLarge.connect(SLOT(this, AttachmentEdit::fileTooLarge));
00076
00077
00078
00079
00080
00081
00082 uploadDone.connect(SLOT(composer, Composer::attachmentDone));
00083 }
00084
00085 AttachmentEdit::~AttachmentEdit()
00086 {
00087
00088 if (!taken_)
00089 unlink(spoolFileName_.c_str());
00090 }
00091
00092 bool AttachmentEdit::uploadNow()
00093 {
00094
00095
00096
00097
00098 if (upload_) {
00099 if (!upload_->isUploaded()) {
00100 upload_->upload();
00101 return true;
00102 } else
00103 return false;
00104 } else
00105 return false;
00106 }
00107
00108 void AttachmentEdit::uploaded()
00109 {
00110 if (!upload_->emptyFileName()) {
00111 fileName_ = upload_->clientFileName();
00112 spoolFileName_ = upload_->spoolFileName();
00113 upload_->stealSpooledFile();
00114 contentDescription_ = upload_->contentDescription();
00115
00116
00117
00118
00119 delete upload_;
00120 upload_ = 0;
00121 delete remove_;
00122 remove_ = 0;
00123
00124 error_->setText("");
00125
00126
00127
00128
00129 keep_->show();
00130 keep_->setChecked();
00131
00132
00133
00134
00135 struct stat buf;
00136 stat(spoolFileName_.c_str(), &buf);
00137 std::wstring size;
00138 if (buf.st_size < 1024)
00139 size = boost::lexical_cast<std::wstring>(buf.st_size) + L" bytes";
00140 else
00141 size = boost::lexical_cast<std::wstring>((int)(buf.st_size / 1024))
00142 + L"kb";
00143
00144 uploaded_->setText(static_cast<std::wstring>(escapeText(fileName_))
00145 + L" (<i>" + contentDescription_ + L"</i>) " + size);
00146 uploaded_->show();
00147
00148 uploadFailed_ = false;
00149 } else {
00150 error_->setText(tr("msg.file-empty"));
00151 uploadFailed_ = true;
00152 }
00153
00154
00155
00156
00157 uploadDone.emit();
00158 }
00159
00160 void AttachmentEdit::remove()
00161 {
00162 composer_->removeAttachment(this);
00163 }
00164
00165 void AttachmentEdit::fileTooLarge(int size)
00166 {
00167 error_->setText(tr("msg.file-too-large"));
00168 uploadFailed_ = true;
00169
00170
00171
00172
00173 uploadDone.emit();
00174 }
00175
00176 bool AttachmentEdit::include() const
00177 {
00178 return keep_->isChecked();
00179 }
00180
00181 Attachment AttachmentEdit::attachment()
00182 {
00183 taken_ = true;
00184 return Attachment(fileName_, contentDescription_, spoolFileName_);
00185 }