/home/koen/project/wt/cvs/wt/examples/composer/AttachmentEdit.C

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
00003  *
00004  * See the LICENSE file for terms of use.
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    * The file upload itself.
00032    */
00033   upload_ = new WFileUpload(this);
00034   upload_->setFileTextSize(40);
00035 
00036   /*
00037    * The 'remove' option.
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    * Fields that will display the feedback.
00047    */
00048 
00049   // The check box to include or exclude the attachment.
00050   keep_ = new WCheckBox(this);
00051   keep_->hide();
00052 
00053   // The uploaded file information.
00054   uploaded_ = new WText("", this);
00055   uploaded_->setStyleClass("option");
00056   uploaded_->hide();
00057 
00058   // The error message.
00059   error_ = new WText("", this);
00060   error_->setStyleClass("error");
00061   error_->setMargin(WLength(5), Left);
00062 
00063   /*
00064    * React to events.
00065    */
00066 
00067   // Try to catch the fileupload change signal to trigger an upload.
00068   // We could do like google and at a delay with a WTimer as well...
00069   upload_->changed.connect(SLOT(upload_, WFileUpload::upload));
00070 
00071   // React to a succesfull upload.
00072   upload_->uploaded.connect(SLOT(this, AttachmentEdit::uploaded));
00073 
00074   // React to a fileupload problem.
00075   upload_->fileTooLarge.connect(SLOT(this, AttachmentEdit::fileTooLarge));
00076 
00077   /*
00078    * Connect the uploadDone signal to the Composer's attachmentDone,
00079    * so that the Composer can keep track of attachment upload progress,
00080    * if it wishes.
00081    */
00082   uploadDone.connect(SLOT(composer, Composer::attachmentDone));
00083 }
00084 
00085 AttachmentEdit::~AttachmentEdit()
00086 {
00087   // delete the local attachment file copy, if it was not taken from us.
00088   if (!taken_)
00089     unlink(spoolFileName_.c_str());
00090 }
00091 
00092 bool AttachmentEdit::uploadNow()
00093 {
00094   /*
00095    * See if this attachment still needs to be uploaded,
00096    * and return if a new asyncrhonous upload is started.
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      * Delete this widgets since we have a succesfull upload.
00118      */
00119     delete upload_;
00120     upload_ = 0;
00121     delete remove_;
00122     remove_ = 0;
00123 
00124     error_->setText("");
00125 
00126     /*
00127      * Include the file ?
00128      */
00129     keep_->show();
00130     keep_->setChecked();
00131 
00132     /*
00133      * Give information on the file uploaded.
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    * Signal to the Composer that a new asyncrhonous file upload was processed.
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    * Signal to the Composer that a new asyncrhonous file upload was processed.
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 }

Generated on Mon Apr 14 15:15:04 2008 for Wt by doxygen 1.5.3