File: AttachmentEdit.C

package info (click to toggle)
witty 3.1.2-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 45,512 kB
  • ctags: 35,832
  • sloc: cpp: 69,469; ansic: 66,945; xml: 4,383; sh: 594; perl: 108; makefile: 106
file content (188 lines) | stat: -rw-r--r-- 4,408 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */

#include <sys/types.h>
#include <sys/stat.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <boost/lexical_cast.hpp>

#include <iostream>

#include <Wt/WApplication>
#include <Wt/WCheckBox>
#include <Wt/WText>
#include <Wt/WFileUpload>

#include "Attachment.h"
#include "AttachmentEdit.h"
#include "Composer.h"
#include "Option.h"

AttachmentEdit::AttachmentEdit(Composer *composer, WContainerWidget *parent)
  : WContainerWidget(parent),
    composer_(composer),
    uploadDone_(this),
    uploadFailed_(false),
    taken_(false)
{
  /*
   * The file upload itself.
   */
  upload_ = new WFileUpload(this);
  upload_->setFileTextSize(40);

  /*
   * The 'remove' option.
   */
  remove_ = new Option(tr("msg.remove"), this);
  upload_->decorationStyle().font().setSize(WFont::Smaller);
  remove_->setMargin(5, Left);
  remove_->item()->clicked().connect(SLOT(this, WWidget::hide));
  remove_->item()->clicked().connect(SLOT(this, AttachmentEdit::remove));

  /*
   * Fields that will display the feedback.
   */

  // The check box to include or exclude the attachment.
  keep_ = new WCheckBox(this);
  keep_->hide();

  // The uploaded file information.
  uploaded_ = new WText("", this);
  uploaded_->setStyleClass("option");
  uploaded_->hide();

  // The error message.
  error_ = new WText("", this);
  error_->setStyleClass("error");
  error_->setMargin(WLength(5), Left);

  /*
   * React to events.
   */

  // Try to catch the fileupload change signal to trigger an upload.
  // We could do like google and at a delay with a WTimer as well...
  upload_->changed().connect(SLOT(upload_, WFileUpload::upload));

  // React to a succesfull upload.
  upload_->uploaded().connect(SLOT(this, AttachmentEdit::uploaded));

  // React to a fileupload problem.
  upload_->fileTooLarge().connect(SLOT(this, AttachmentEdit::fileTooLarge));

  /*
   * Connect the uploadDone signal to the Composer's attachmentDone,
   * so that the Composer can keep track of attachment upload progress,
   * if it wishes.
   */
  uploadDone_.connect(SLOT(composer, Composer::attachmentDone));
}

AttachmentEdit::~AttachmentEdit()
{
  // delete the local attachment file copy, if it was not taken from us.
  if (!taken_)
    unlink(spoolFileName_.c_str());
}

bool AttachmentEdit::uploadNow()
{
  /*
   * See if this attachment still needs to be uploaded,
   * and return if a new asynchronous upload is started.
   */
  if (upload_) {
    if (upload_->canUpload()) {
      upload_->upload();
      return true;
    } else
      return false;
  } else
    return false;
}

void AttachmentEdit::uploaded()
{
  if (!upload_->emptyFileName()) {
    fileName_ = upload_->clientFileName();
    spoolFileName_ = upload_->spoolFileName();
    upload_->stealSpooledFile();
    contentDescription_ = upload_->contentDescription();

    /*
     * Delete this widgets since we have a succesfull upload.
     */
    delete upload_;
    upload_ = 0;
    delete remove_;
    remove_ = 0;

    error_->setText("");

    /*
     * Include the file ?
     */
    keep_->show();
    keep_->setChecked();

    /*
     * Give information on the file uploaded.
     */
    struct stat buf;
    stat(spoolFileName_.c_str(), &buf);
    std::wstring size;
    if (buf.st_size < 1024)
      size = boost::lexical_cast<std::wstring>(buf.st_size) + L" bytes";
    else
      size = boost::lexical_cast<std::wstring>((int)(buf.st_size / 1024))
	+ L"kb";

    uploaded_->setText(static_cast<std::wstring>(escapeText(fileName_))
		       + L" (<i>" + contentDescription_ + L"</i>) " + size);
    uploaded_->show();

    uploadFailed_ = false;
  } else {
    error_->setText(tr("msg.file-empty"));
    uploadFailed_ = true;
  }

  /*
   * Signal to the Composer that a new asyncrhonous file upload was processed.
   */
  uploadDone_.emit();
}

void AttachmentEdit::remove()
{
  composer_->removeAttachment(this);
}

void AttachmentEdit::fileTooLarge(int size)
{
  error_->setText(tr("msg.file-too-large"));
  uploadFailed_ = true;

  /*
   * Signal to the Composer that a new asyncrhonous file upload was processed.
   */
  uploadDone_.emit();
}

bool AttachmentEdit::include() const
{
  return keep_->isChecked();
}

Attachment AttachmentEdit::attachment()
{
  taken_ = true;
  return Attachment(fileName_, contentDescription_, spoolFileName_);
}