Wt examples 3.3.12
AttachmentEdit.C
Go to the documentation of this file.
1/*
2 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
3 *
4 * See the LICENSE file for terms of use.
5 */
6
7#include <fstream>
8#ifndef WIN32
9#include <unistd.h>
10#endif
11#include <boost/lexical_cast.hpp>
12
13#include <iostream>
14
15#include <Wt/WAnchor>
16#include <Wt/WApplication>
17#include <Wt/WCheckBox>
18#include <Wt/WCssDecorationStyle>
19#include <Wt/WFileResource>
20#include <Wt/WFileUpload>
21#include <Wt/WProgressBar>
22#include <Wt/WText>
23
24#include "Attachment.h"
25#include "AttachmentEdit.h"
26#include "Composer.h"
27#include "Option.h"
28
30 WContainerWidget *parent)
31 : WContainerWidget(parent),
32 info_(f)
33{
34 /*
35 * Include the file ?
36 */
37 keep_ = new WCheckBox(this);
39
40 /*
41 * Give information on the file uploaded.
42 */
43 std::streamsize fsize = 0;
44 {
45 std::ifstream theFile(info_.spoolFileName().c_str());
46 theFile.seekg(0, std::ios_base::end);
47 fsize = theFile.tellg();
48 theFile.seekg(0);
49 }
50 std::wstring size;
51 if (fsize < 1024)
52 size = boost::lexical_cast<std::wstring>(fsize) + L" bytes";
53 else
54 size = boost::lexical_cast<std::wstring>((int)(fsize / 1024))
55 + L"kb";
56
57 std::wstring fn = static_cast<std::wstring>
59
61 = new WAnchor("", fn + L" (<i>" + WString::fromUTF8(info_.contentType())
62 + L"</i>) " + size, this);
63
66 this);
69}
70
73 composer_(composer),
74 uploadDone_(this),
75 uploadFailed_(false)
76{
77 /*
78 * The file upload itself.
79 */
80 upload_ = new WFileUpload(this);
81 upload_->setMultiple(true);
83
84 /*
85 * A progress bar
86 */
87 WProgressBar *progress = new WProgressBar();
88 progress->setFormat(WString::Empty);
90 upload_->setProgressBar(progress);
91
92 /*
93 * The 'remove' option.
94 */
95 remove_ = new Option(tr("msg.remove"), this);
99 remove_->item()->clicked().connect(this, &WWidget::hide);
100 remove_->item()->clicked().connect(this, &AttachmentEdit::remove);
101
102 // The error message.
103 error_ = new WText("", this);
104 error_->setStyleClass("error");
106
107 /*
108 * React to events.
109 */
110
111 // Try to catch the fileupload change signal to trigger an upload.
112 // We could do like google and at a delay with a WTimer as well...
114
115 // React to a succesfull upload.
117
118 // React to a fileupload problem.
120
121 /*
122 * Connect the uploadDone signal to the Composer's attachmentDone,
123 * so that the Composer can keep track of attachment upload progress,
124 * if it wishes.
125 */
127}
128
130{
131 /*
132 * See if this attachment still needs to be uploaded,
133 * and return if a new asynchronous upload is started.
134 */
135 if (upload_) {
136 if (upload_->canUpload()) {
137 upload_->upload();
138 return true;
139 } else
140 return false;
141 } else
142 return false;
143}
144
146{
147 std::vector<Http::UploadedFile> files = upload_->uploadedFiles();
148
149 if (!files.empty()) {
150 /*
151 * Delete this widgets since we have a succesfull upload.
152 */
153 delete upload_;
154 upload_ = 0;
155 delete remove_;
156 remove_ = 0;
157 delete error_;
158 error_ = 0;
159
160 for (unsigned i = 0; i < files.size(); ++i)
161 uploadInfo_.push_back(new UploadInfo(files[i], this));
162 } else {
163 error_->setText(tr("msg.file-empty"));
164 uploadFailed_ = true;
165 }
166
167 /*
168 * Signal to the Composer that a new asynchronous file upload was processed.
169 */
171}
172
177
179{
180 error_->setText(tr("msg.file-too-large")
181 .arg(size / 1024)
182 .arg(WApplication::instance()->maximumRequestSize() / 1024));
183 uploadFailed_ = true;
184
185 /*
186 * Signal to the Composer that a new asyncrhonous file upload was processed.
187 */
189}
190
191std::vector<Attachment> AttachmentEdit::attachments()
192{
193 std::vector<Attachment> result;
194
195 for (unsigned i = 0; i < uploadInfo_.size(); ++i) {
196 if (uploadInfo_[i]->keep_->isChecked()) {
197 Http::UploadedFile& f = uploadInfo_[i]->info_;
198 f.stealSpoolFile();
199 result.push_back(Attachment
202 f.spoolFileName()));
203 }
204 }
205
206 return result;
207}
WCheckBox * keep_
The check box to keep or discard the uploaded file.
WAnchor * downloadLink_
Anchor referencing the file.
Http::UploadedFile info_
UploadInfo(const Http::UploadedFile &f, WContainerWidget *parent=0)
bool uploadNow()
Updates the file now.
Signal< void > uploadDone_
void remove()
Slot triggered when the users wishes to remove this attachment edit.
WText * error_
The text box to display an error (empty or too big file)
Composer * composer_
Option * remove_
The option to cancel the file upload.
bool uploadFailed_
The state of the last upload process.
void uploaded()
Slot triggered when the WFileUpload completed an upload.
std::vector< Attachment > attachments()
Returns the attachment.
WFileUpload * upload_
The WFileUpload control.
std::vector< UploadInfo * > uploadInfo_
void fileTooLarge(::int64_t size)
Slot triggered when the WFileUpload received an oversized file.
AttachmentEdit(Composer *composer, WContainerWidget *parent=0)
Creates an attachment edit field.
An email attachment.
Definition Attachment.h:20
An E-mail composer widget.
Definition Composer.h:41
void attachmentDone()
Slotcalled when an attachment has been uploaded.
Definition Composer.C:331
void removeAttachment(AttachmentEdit *attachment)
Remove the given attachment edit.
Definition Composer.C:264
A clickable option.
Definition Option.h:32
WInteractWidget * item()
Returns the clickable part.
Definition Option.h:44
Wt::Signals::connection connect(const F &function)
const std::string & spoolFileName() const
const std::string & clientFileName() const
void stealSpoolFile() const
const std::string & contentType() const
void emit(A1 a1=NoClass::none, A2 a2=NoClass::none, A3 a3=NoClass::none, A4 a4=NoClass::none, A5 a5=NoClass::none, A6 a6=NoClass::none) const
virtual Wt::Signals::connection connect(WObject *target, WObject::Method method)
void setChecked(bool checked)
void setLink(const WLink &link)
void setMultiple(bool multiple)
EventSignal & changed()
JSignal< ::int64_t > & fileTooLarge()
void setProgressBar(WProgressBar *progressBar)
const std::vector< Http::UploadedFile > & uploadedFiles() const
EventSignal & uploaded()
bool canUpload() const
void setFileTextSize(int chars)
void setSize(Size size, const WLength &length)
EventSignal< WMouseEvent > & clicked()
void setFormat(const WString &format)
void suggestFileName(const Wt::WString &name, DispositionType dispositionType=Attachment)
static WString fromUTF8(const std::string &value, bool checkValid=false)
static const WString Empty
bool setText(const WString &text)
static WString escapeText(const WString &text, bool newlinesToo=false)
virtual void setMargin(const WLength &margin, WFlags< Side > sides=All)
virtual WCssDecorationStyle & decorationStyle()
virtual void setVerticalAlignment(AlignmentFlag alignment, const WLength &length=WLength())
virtual void setStyleClass(const WString &styleClass)
static WString tr(const char *key)
WWidget * parent() const
AlignMiddle

Generated on Fri May 17 2024 for the C++ Web Toolkit (Wt) by doxygen 1.9.8