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