AttachmentEdit Class Reference
[Composer example]

An edit field for an email attachment. More...

#include <AttachmentEdit.h>

Inheritance diagram for AttachmentEdit:

Inheritance graph
[legend]

List of all members.

Public Member Functions

 AttachmentEdit (Composer *composer, WContainerWidget *parent=0)
 Create an attachment edit field.
 ~AttachmentEdit ()
bool uploadNow ()
 Update the file now.
bool uploadFailed () const
 Return whether the upload failed.
bool include () const
 Return whether this attachment must be included in the message.
Attachment attachment ()
 Return the attachment.

Public Attributes

Signal< void > uploadDone
 Signal emitted when a new attachment has been uploaded (or failed to upload.

Private Slots

void uploaded ()
 Slot triggered when the WFileUpload completed an upload.
void fileTooLarge (int size)
 Slot triggered when the WFileUpload received an oversized file.
void remove ()
 Slot triggered when the users wishes to remove this attachment edit.

Private Attributes

Composercomposer_
WFileUploadupload_
 The WFileUpload control.
WTextuploaded_
 The text describing the uploaded file.
WCheckBoxkeep_
 The check box to keep or discard the uploaded file.
Optionremove_
 The option to remove the file.
WTexterror_
 The text box to display an error (empty or too big file).
bool uploadFailed_
 The state of the last upload process.
std::wstring fileName_
 The filename of the uploaded file.
std::string spoolFileName_
 The filename of the local spool file.
std::wstring contentDescription_
 The content description that was sent along with the file.
bool taken_
 Whether the spool file is "taken" and is no longer managed by the edit.


Detailed Description

An edit field for an email attachment.

This widget managements one attachment edit: it shows a file upload control, handles the upload, and gives feed-back on the file uploaded.

This widget is part of the Wt composer example.

Definition at line 37 of file AttachmentEdit.h.


Constructor & Destructor Documentation

AttachmentEdit::AttachmentEdit ( Composer composer,
WContainerWidget parent = 0 
)

Create an attachment edit field.

Definition at line 24 of file AttachmentEdit.C.

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_->item()->clicked.connect(SLOT(this, WWidget::hide));
00043   remove_->item()->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 }

AttachmentEdit::~AttachmentEdit (  ) 

Definition at line 85 of file AttachmentEdit.C.

00086 {
00087   // delete the local attachment file copy, if it was not taken from us.
00088   if (!taken_)
00089     unlink(spoolFileName_.c_str());
00090 }


Member Function Documentation

bool AttachmentEdit::uploadNow (  ) 

Update the file now.

Returns whether a new file will be uploaded. If so, the uploadDone signal will be signalled when the file is uploaded (or failed to upload).

Definition at line 92 of file AttachmentEdit.C.

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 }

bool AttachmentEdit::uploadFailed (  )  const [inline]

Return whether the upload failed.

Definition at line 55 of file AttachmentEdit.h.

00055 { return uploadFailed_; }

bool AttachmentEdit::include (  )  const

Return whether this attachment must be included in the message.

Definition at line 176 of file AttachmentEdit.C.

00177 {
00178   return keep_->isChecked();
00179 }

Attachment AttachmentEdit::attachment (  ) 

Return the attachment.

Definition at line 181 of file AttachmentEdit.C.

00182 {
00183   taken_ = true;
00184   return Attachment(fileName_, contentDescription_, spoolFileName_);
00185 }

void AttachmentEdit::uploaded (  )  [private, slot]

Slot triggered when the WFileUpload completed an upload.

Definition at line 108 of file AttachmentEdit.C.

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 }

void AttachmentEdit::fileTooLarge ( int  size  )  [private, slot]

Slot triggered when the WFileUpload received an oversized file.

Definition at line 165 of file AttachmentEdit.C.

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 }

void AttachmentEdit::remove (  )  [private, slot]

Slot triggered when the users wishes to remove this attachment edit.

Definition at line 160 of file AttachmentEdit.C.

00161 {
00162   composer_->removeAttachment(this);
00163 }


Member Data Documentation

Signal emitted when a new attachment has been uploaded (or failed to upload.

Definition at line 68 of file AttachmentEdit.h.

Definition at line 71 of file AttachmentEdit.h.

The WFileUpload control.

Definition at line 74 of file AttachmentEdit.h.

The text describing the uploaded file.

Definition at line 77 of file AttachmentEdit.h.

The check box to keep or discard the uploaded file.

Definition at line 80 of file AttachmentEdit.h.

The option to remove the file.

Definition at line 83 of file AttachmentEdit.h.

The text box to display an error (empty or too big file).

Definition at line 86 of file AttachmentEdit.h.

The state of the last upload process.

Definition at line 89 of file AttachmentEdit.h.

std::wstring AttachmentEdit::fileName_ [private]

The filename of the uploaded file.

Definition at line 92 of file AttachmentEdit.h.

std::string AttachmentEdit::spoolFileName_ [private]

The filename of the local spool file.

Definition at line 95 of file AttachmentEdit.h.

std::wstring AttachmentEdit::contentDescription_ [private]

The content description that was sent along with the file.

Definition at line 98 of file AttachmentEdit.h.

bool AttachmentEdit::taken_ [private]

Whether the spool file is "taken" and is no longer managed by the edit.

Definition at line 101 of file AttachmentEdit.h.


The documentation for this class was generated from the following files:

Generated on Mon Jan 26 14:14:20 2009 for Wt by doxygen 1.5.6