Wt examples  3.7.1
AttachmentEdit.C
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Emweb bv, Herent, 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);
38  keep_->setChecked();
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>
58  (escapeText(WString::fromUTF8(info_.clientFileName())));
59 
61  = new WAnchor("", fn + L" (<i>" + WString::fromUTF8(info_.contentType())
62  + L"</i>) " + size, this);
63 
66  this);
68  downloadLink_->setLink(res);
69 }
70 
72  : WContainerWidget(parent),
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);
89  progress->setVerticalAlignment(AlignMiddle);
90  upload_->setProgressBar(progress);
91 
92  /*
93  * The 'remove' option.
94  */
95  remove_ = new Option(tr("msg.remove"), this);
96  upload_->decorationStyle().font().setSize(WFont::Smaller);
97  upload_->setVerticalAlignment(AlignMiddle);
98  remove_->setMargin(5, Left);
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...
113  upload_->changed().connect(upload_, &WFileUpload::upload);
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  */
170  uploadDone_.emit();
171 }
172 
174 {
176 }
177 
178 void AttachmentEdit::fileTooLarge(::int64_t size)
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  */
188  uploadDone_.emit();
189 }
190 
191 std::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
200  (WString::fromUTF8(f.clientFileName()),
201  WString::fromUTF8(f.contentType()),
202  f.spoolFileName()));
203  }
204  }
205 
206  return result;
207 }
virtual void setStyleClass(const WString &styleClass) override
A clickable option.
Definition: Option.h:31
JSignal< ::int64_t > & fileTooLarge()
void uploaded()
Slot triggered when the WFileUpload completed an upload.
Signal< void > uploadDone_
WInteractWidget * item()
Returns the clickable part.
Definition: Option.h:44
AttachmentEdit(Composer *composer, WContainerWidget *parent=0)
Creates an attachment edit field.
const std::string & contentType() const
virtual WCssDecorationStyle & decorationStyle() override
An email attachment.
Definition: Attachment.h:19
EventSignal< WMouseEvent > & clicked()
static WString tr(const char *key)
WAnchor * downloadLink_
Anchor referencing the file.
Wt::Signals::connection connect(F function)
virtual void setVerticalAlignment(AlignmentFlag alignment, const WLength &length=WLength()) override
An E-mail composer widget.
Definition: Composer.h:40
EventSignal & changed()
Option * remove_
The option to cancel the file upload.
void suggestFileName(const Wt::WString &name, ContentDisposition disposition=ContentDisposition::Attachment)
Http::UploadedFile info_
void setSize(FontSize size)
WCheckBox * keep_
The check box to keep or discard the uploaded file.
std::vector< Attachment > attachments()
Returns the attachment.
void emit(A... args) const
void removeAttachment(AttachmentEdit *attachment)
Remove the given attachment edit.
Definition: Composer.C:265
bool setText(const WString &text)
virtual void setMargin(const WLength &margin, WFlags< Side > sides=AllSides) override
void setFormat(const WString &format)
bool canUpload() const
void setMultiple(bool multiple)
WFileUpload * upload_
The WFileUpload control.
void remove()
Slot triggered when the users wishes to remove this attachment edit.
WWidget * parent() const
UploadInfo(const Http::UploadedFile &f, WContainerWidget *parent=0)
bool uploadNow()
Updates the file now.
const std::vector< Http::UploadedFile > & uploadedFiles() const
void setFileTextSize(int chars)
const std::string & clientFileName() const
EventSignal & uploaded()
void fileTooLarge(::int64_t size)
Slot triggered when the WFileUpload received an oversized file.
WText * error_
The text box to display an error (empty or too big file)
void attachmentDone()
Slotcalled when an attachment has been uploaded.
Definition: Composer.C:332
void setLink(const WLink &link)
bool uploadFailed_
The state of the last upload process.
Composer * composer_
void setChecked(bool checked)
Wt::Signals::connection connect(F function)
virtual Wt::Signals::connection connect(WObject *target, WObject::Method method) override
void stealSpoolFile() const
const std::string & spoolFileName() const
std::vector< UploadInfo * > uploadInfo_
void setProgressBar(WProgressBar *progressBar)

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13