Wt examples  3.2.0
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | Friends
Composer Class Reference

An E-mail composer widget. More...

#include <Composer.h>

Inheritance diagram for Composer:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 Composer (WContainerWidget *parent=0)
 Construct a new Composer.
void setTo (const std::vector< Contact > &to)
 Set message To: contacts.
void setSubject (const WString &subject)
 Set subject.
void setMessage (const WString &message)
 Set the message.
void setAddressBook (const std::vector< Contact > &addressBook)
 Set the address book, for autocomplete suggestions.
std::vector< Contactto () const
 Get the To: contacts.
std::vector< Contactcc () const
 Get the Cc: contacts.
std::vector< Contactbcc () const
 Get the Bc: contacts.
const WStringsubject () const
 Get the subject.
std::vector< Attachmentattachments () const
 Get the list of attachments.
const WStringmessage () const
 Get the message.

Public Attributes

Wt::Signal< void > send
 The message is ready to be sent...
Wt::Signal< void > discard
 The message must be discarded.

Private Member Functions

void attachMore ()
 Add an attachment edit.
void removeAttachment (AttachmentEdit *attachment)
 Remove the given attachment edit.
void sendIt ()
 Slot attached to the Send button.
void saveNow ()
 Slot attached to the Save now button.
void discardIt ()
 Slot attached to the Discard button.
void attachmentDone ()
 Slotcalled when an attachment has been uploaded.
void createUi ()
void saved ()
 All attachments have been processed, determine the result of saving the message.
void setStatus (const WString &text, const WString &style)
 Set the status, and apply the given style.

Private Attributes

WContainerWidgetlayout_
WPushButtontopSendButton_
WPushButtontopSaveNowButton_
WPushButtontopDiscardButton_
WPushButtonbotSendButton_
WPushButtonbotSaveNowButton_
WPushButtonbotDiscardButton_
WTextstatusMsg_
WTableedits_
AddresseeEdittoEdit_
 To: Addressees edit.
AddresseeEditccEdit_
 Cc: Addressees edit.
AddresseeEditbccEdit_
 Bcc: Addressees edit.
ContactSuggestionscontactSuggestions_
 The suggestions popup for the addressee edits.
WLineEditsubject_
 The subject line edit.
OptionListoptions_
 OptionsList for editing Cc or Bcc.
Optionaddcc_
 Option for editing Cc:
Optionaddbcc_
 Option for editing Bcc:
OptionattachFile_
 Option for attaching a file.
OptionattachOtherFile_
 Option for attaching another file.
std::vector< AttachmentEdit * > attachments_
 Array which holds all the attachments, including one extra invisible one.
WTextAreamessage_
 WTextArea for the main message.
bool saving_
 state when waiting asyncrhonously for attachments to be uploaded
bool sending_
int attachmentsPending_
 number of attachments waiting to be uploaded during saving

Friends

class AttachmentEdit

Detailed Description

An E-mail composer widget.

This widget is part of the Wt composer example.

Definition at line 40 of file Composer.h.


Constructor & Destructor Documentation

Composer::Composer ( WContainerWidget parent = 0)

Construct a new Composer.

Definition at line 25 of file Composer.C.


Member Function Documentation

void Composer::attachmentDone ( ) [private]

Slotcalled when an attachment has been uploaded.

This used during while saving the email and waiting for remaining attachments to be uploaded. It is connected to the AttachmentEdit control signals that are emitted when an attachment has been processed.

Definition at line 331 of file Composer.C.

{
  if (saving_) {
    --attachmentsPending_;
    std::cerr << "Attachments still: " << attachmentsPending_ << std::endl;

    if (attachmentsPending_ == 0)
      saved();
  }
}
std::vector< Attachment > Composer::attachments ( ) const

Get the list of attachments.

The ownership of the attachment spool files is transferred to the caller as well, be sure to delete them !

Definition at line 75 of file Composer.C.

{
  std::vector<Attachment> attachments;

  for (unsigned i = 0; i < attachments_.size() - 1; ++i) {
    std::vector<Attachment> toadd = attachments_[i]->attachments();

    attachments.insert(attachments.end(), toadd.begin(), toadd.end());
  }

  return attachments;
}
void Composer::attachMore ( ) [private]

Add an attachment edit.

Definition at line 249 of file Composer.C.

{
  /*
   * Create and append the next AttachmentEdit, that will be hidden.
   */
  AttachmentEdit *edit = new AttachmentEdit(this);
  edits_->elementAt(5, 1)->insertBefore(edit, attachOtherFile_);
  attachments_.push_back(edit);
  attachments_.back()->hide();

  // Connect the attachOtherFile_ option to show this attachment.
  attachOtherFile_->item()->clicked()
    .connect(attachments_.back(), &WWidget::show);
}
std::vector< Contact > Composer::bcc ( ) const

Get the Bc: contacts.

Definition at line 60 of file Composer.C.

{
  return bccEdit_->addressees();
}
std::vector< Contact > Composer::cc ( ) const

Get the Cc: contacts.

Definition at line 55 of file Composer.C.

{
  return ccEdit_->addressees();
}
void Composer::createUi ( ) [private]

Definition at line 93 of file Composer.C.

{
  setStyleClass("darker");

  // horizontal layout container, used for top and bottom buttons.
  WContainerWidget *horiz;

  /*
   * Top buttons
   */
  horiz = new WContainerWidget(layout_);
  horiz->setPadding(5);
  topSendButton_ = new WPushButton(tr("msg.send"), horiz);
  topSendButton_->setStyleClass("default"); // default action
  topSaveNowButton_ = new WPushButton(tr("msg.savenow"), horiz);
  topDiscardButton_ = new WPushButton(tr("msg.discard"), horiz);

  // Text widget which shows status messages, next to the top buttons.
  statusMsg_ = new WText(horiz);
  statusMsg_->setMargin(15, Left);

  /*
   * To, Cc, Bcc, Subject, Attachments
   *
   * They are organized in a two-column table: left column for
   * labels, and right column for the edit.
   */
  edits_ = new WTable(layout_);
  edits_->setStyleClass("lighter");
  edits_->resize(WLength(100, WLength::Percentage), WLength::Auto);
  edits_->elementAt(0, 0)->resize(WLength(1, WLength::Percentage),
                                  WLength::Auto);

  /*
   * To, Cc, Bcc
   */
  toEdit_ = new AddresseeEdit(tr("msg.to"), edits_->elementAt(0, 1),
                              edits_->elementAt(0, 0));
  // add some space above To:
  edits_->elementAt(0, 1)->setMargin(5, Top);
  ccEdit_ = new AddresseeEdit(tr("msg.cc"), edits_->elementAt(1, 1),
                              edits_->elementAt(1, 0));
  bccEdit_ = new AddresseeEdit(tr("msg.bcc"), edits_->elementAt(2, 1),
                               edits_->elementAt(2, 0));

  ccEdit_->hide();
  bccEdit_->hide();

  /*
   * Addressbook suggestions popup
   */
  contactSuggestions_ = new ContactSuggestions(layout_);

  contactSuggestions_->forEdit(toEdit_);
  contactSuggestions_->forEdit(ccEdit_);
  contactSuggestions_->forEdit(bccEdit_);

  /*
   * We use an OptionList widget to show the expand options for
   * ccEdit_ and bccEdit_ nicely next to each other, separated
   * by pipe characters.
   */
  options_ = new OptionList(edits_->elementAt(3, 1));

  options_->add(addcc_ = new Option(tr("msg.addcc")));
  options_->add(addbcc_ = new Option(tr("msg.addbcc")));

  /*
   * Subject
   */
  new Label(tr("msg.subject"), edits_->elementAt(4, 0));
  subject_ = new WLineEdit(edits_->elementAt(4, 1));
  subject_->resize(WLength(99, WLength::Percentage), WLength::Auto);

  /*
   * Attachments
   */
  new WImage("icons/paperclip.png", edits_->elementAt(5, 0));
  edits_->elementAt(5, 0)->setContentAlignment(AlignRight | AlignTop);
  edits_->elementAt(5, 0)->setPadding(3);
  
  // Attachment edits: we always have the next attachmentedit ready
  // but hidden. This improves the response time, since the show()
  // and hide() slots are stateless.
  attachments_.push_back(new AttachmentEdit(this, edits_->elementAt(5, 1)));
  attachments_.back()->hide();

  /*
   * Two options for attaching files. The first does not say 'another'.
   */
  attachFile_ = new Option(tr("msg.attachfile"),
                           edits_->elementAt(5, 1));
  attachOtherFile_ = new Option(tr("msg.attachanother"),
                                edits_->elementAt(5, 1));
  attachOtherFile_->hide();

  /*
   * Message
   */
  message_ = new WTextArea(layout_);
  message_->setColumns(80);
  message_->setRows(10); // should be 20, but let's keep it smaller
  message_->setMargin(10);

  /*
   * Bottom buttons
   */
  horiz = new WContainerWidget(layout_);
  horiz->setPadding(5);
  botSendButton_ = new WPushButton(tr("msg.send"), horiz);
  botSendButton_->setStyleClass("default");
  botSaveNowButton_ = new WPushButton(tr("msg.savenow"), horiz);
  botDiscardButton_ = new WPushButton(tr("msg.discard"), horiz);

  /*
   * Button events.
   */
  topSendButton_->clicked().connect(this, &Composer::sendIt);
  botSendButton_->clicked().connect(this, &Composer::sendIt);
  topSaveNowButton_->clicked().connect(this, &Composer::saveNow);
  botSaveNowButton_->clicked().connect(this, &Composer::saveNow);
  topDiscardButton_->clicked().connect(this, &Composer::discardIt);
  botDiscardButton_->clicked().connect(this, &Composer::discardIt);

  /*
   * Option events to show the cc or Bcc edit.
   *
   * Clicking on the option should both show the corresponding edit, and
   * hide the option itself.
   */
  addcc_->item()->clicked().connect(ccEdit_, &WWidget::show);
  addcc_->item()->clicked().connect(addcc_, &WWidget::hide);
  addcc_->item()->clicked().connect(options_, &OptionList::update);
  addcc_->item()->clicked().connect(ccEdit_, &WFormWidget::setFocus);

  addbcc_->item()->clicked().connect(bccEdit_, &WWidget::show);
  addbcc_->item()->clicked().connect(addbcc_, &WWidget::hide);
  addbcc_->item()->clicked().connect(options_, &OptionList::update);
  addbcc_->item()->clicked().connect(bccEdit_, &WFormWidget::setFocus);

  /*
   * Option event to attach the first attachment.
   *
   * We show the first attachment, and call attachMore() to prepare the
   * next attachment edit that will be hidden.
   *
   * In addition, we need to show the 'attach More' option, and hide the
   * 'attach' option.
   */
  attachFile_->item()->clicked().connect(attachments_.back(), &WWidget::show);
  attachFile_->item()->clicked().connect(attachOtherFile_, &WWidget::show);
  attachFile_->item()->clicked().connect(attachFile_, &WWidget::hide);
  attachFile_->item()->clicked().connect(this, &Composer::attachMore);
  attachOtherFile_->item()->clicked().connect(this, &Composer::attachMore);
}
void Composer::discardIt ( ) [private]

Slot attached to the Discard button.

Discards the current message: emits the discard event.

Definition at line 386 of file Composer.C.

{ 
  discard.emit();
}
const WString & Composer::message ( ) const

Get the message.

Definition at line 88 of file Composer.C.

{
  return message_->text();
}
void Composer::removeAttachment ( AttachmentEdit attachment) [private]

Remove the given attachment edit.

Definition at line 264 of file Composer.C.

{
  /*
   * Remove the given attachment from the attachments list.
   */
  std::vector<AttachmentEdit *>::iterator i
    = std::find(attachments_.begin(), attachments_.end(), attachment);

  if (i != attachments_.end()) {
    attachments_.erase(i);
    delete attachment;

    if (attachments_.size() == 1) {
      /*
       * This was the last visible attachment, thus, we should switch
       * the option control again.
       */
      attachOtherFile_->hide();
      attachFile_->show();
      attachFile_->item()->clicked()
        .connect(attachments_.back(), &WWidget::show);
    }
  }
}
void Composer::saved ( ) [private]

All attachments have been processed, determine the result of saving the message.

Definition at line 348 of file Composer.C.

{
  /*
   * All attachments have been processed.
   */

  bool attachmentsFailed = false;
  for (unsigned i = 0; i < attachments_.size() - 1; ++i)
    if (attachments_[i]->uploadFailed()) {
      attachmentsFailed = true;
      break;
    }

  if (attachmentsFailed) {
    setStatus(tr("msg.attachment.failed"), "error");
  } else {
#ifndef WIN32
    time_t t = time(0);
    struct tm td;
    gmtime_r(&t, &td);
    char buffer[100];
    strftime(buffer, 100, "%H:%M", &td);
#else
    char buffer[] = "server"; // Should fix this; for now just make sense
#endif
    setStatus(tr("msg.ok"), "status");
    statusMsg_->setText(std::string("Draft saved at ") + buffer);

    if (sending_) {
      send.emit();
      return;
    }
  }

  saving_ = false;
  sending_ = false;
}
void Composer::saveNow ( ) [private]

Slot attached to the Save now button.

Tries to save the mail message, and gives feedback on failure and on success.

Definition at line 302 of file Composer.C.

{
  if (!saving_) {
    saving_ = true;

    /*
     * Check if any attachments still need to be uploaded.
     * This may be the case when fileupload change events could not
     * be caught (for example in Konqueror).
     */
    attachmentsPending_ = 0;

    for (unsigned i = 0; i < attachments_.size() - 1; ++i) {
      if (attachments_[i]->uploadNow()) {
        ++attachmentsPending_;

        // this will trigger attachmentDone() when done, see
        // the AttachmentEdit constructor.
      }
    }

    std::cerr << "Attachments pending: " << attachmentsPending_ << std::endl;
    if (attachmentsPending_)
      setStatus(tr("msg.uploading"), "status");
    else
      saved();
  }
}
void Composer::sendIt ( ) [private]

Slot attached to the Send button.

Tries to save the mail message, and if succesfull, sends it.

Definition at line 289 of file Composer.C.

{
  if (!sending_) {
    sending_ = true;

    /*
     * First save -- this will check for the sending_ state
     * signal if successfull.
     */
    saveNow();
  }
}
void Composer::setAddressBook ( const std::vector< Contact > &  addressBook)

Set the address book, for autocomplete suggestions.

Definition at line 65 of file Composer.C.

void Composer::setMessage ( const WString message)

Set the message.

Definition at line 45 of file Composer.C.

{
  message_->setText(message);
}
void Composer::setStatus ( const WString text,
const WString style 
) [private]

Set the status, and apply the given style.

Definition at line 342 of file Composer.C.

void Composer::setSubject ( const WString subject)

Set subject.

Definition at line 40 of file Composer.C.

{
  subject_->setText(subject);
}
void Composer::setTo ( const std::vector< Contact > &  to)

Set message To: contacts.

Definition at line 35 of file Composer.C.

const WString & Composer::subject ( ) const

Get the subject.

Definition at line 70 of file Composer.C.

{
  return subject_->text();
}
std::vector< Contact > Composer::to ( ) const

Get the To: contacts.

Definition at line 50 of file Composer.C.

{
  return toEdit_->addressees();
}

Friends And Related Function Documentation

friend class AttachmentEdit [friend]

Definition at line 194 of file Composer.h.


Member Data Documentation

Option for editing Bcc:

Definition at line 127 of file Composer.h.

Option for editing Cc:

Definition at line 125 of file Composer.h.

Option for attaching a file.

Definition at line 129 of file Composer.h.

std::vector<AttachmentEdit *> Composer::attachments_ [private]

Array which holds all the attachments, including one extra invisible one.

Definition at line 134 of file Composer.h.

number of attachments waiting to be uploaded during saving

Definition at line 143 of file Composer.h.

Option for attaching another file.

Definition at line 131 of file Composer.h.

Bcc: Addressees edit.

Definition at line 113 of file Composer.h.

Definition at line 103 of file Composer.h.

Definition at line 103 of file Composer.h.

Definition at line 103 of file Composer.h.

Cc: Addressees edit.

Definition at line 111 of file Composer.h.

The suggestions popup for the addressee edits.

Definition at line 116 of file Composer.h.

The message must be discarded.

Definition at line 97 of file Composer.h.

Definition at line 106 of file Composer.h.

Definition at line 100 of file Composer.h.

WTextArea for the main message.

Definition at line 137 of file Composer.h.

OptionsList for editing Cc or Bcc.

Definition at line 122 of file Composer.h.

bool Composer::saving_ [private]

state when waiting asyncrhonously for attachments to be uploaded

Definition at line 140 of file Composer.h.

The message is ready to be sent...

Definition at line 93 of file Composer.h.

bool Composer::sending_ [private]

Definition at line 140 of file Composer.h.

Definition at line 104 of file Composer.h.

The subject line edit.

Definition at line 119 of file Composer.h.

To: Addressees edit.

Definition at line 109 of file Composer.h.

Definition at line 102 of file Composer.h.

Definition at line 102 of file Composer.h.

Definition at line 102 of file Composer.h.


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

Generated on Tue Nov 29 2011 for the C++ Web Toolkit (Wt) by doxygen 1.7.5.1