Wt examples  3.3.5
/usr/src/RPM/BUILD/wt-3.3.5-rc2/examples/composer/ComposeExample.C
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
00003  *
00004  * See the LICENSE file for terms of use.
00005  */
00006 
00007 #include <Wt/WApplication>
00008 #include <Wt/WBreak>
00009 #include <Wt/WText>
00010 #include <Wt/WPushButton>
00011 #include <Wt/WContainerWidget>
00012 #include <Wt/WStringUtil>
00013 #ifndef _MSC_VER
00014 #include <unistd.h>
00015 #endif
00016 
00017 #include "Composer.h"
00018 #include "ComposeExample.h"
00019 #include "Contact.h"
00020 
00021 ComposeExample::ComposeExample(WContainerWidget *parent)
00022   : WContainerWidget(parent)
00023 {
00024   composer_ = new Composer(this);
00025 
00026   std::vector<Contact> addressBook;
00027   addressBook.push_back(Contact(L"Koen Deforche",
00028                                 L"koen.deforche@gmail.com"));
00029   addressBook.push_back(Contact(L"Koen alias1",
00030                                 L"koen.alias1@yahoo.com"));
00031   addressBook.push_back(Contact(L"Koen alias2",
00032                                 L"koen.alias2@yahoo.com"));
00033   addressBook.push_back(Contact(L"Koen alias3",
00034                                 L"koen.alias3@yahoo.com"));
00035   addressBook.push_back(Contact(L"Bartje",
00036                                 L"jafar@hotmail.com"));
00037   composer_->setAddressBook(addressBook);
00038 
00039   std::vector<Contact> contacts;
00040   contacts.push_back(Contact(L"Koen Deforche", L"koen.deforche@gmail.com"));
00041 
00042   composer_->setTo(contacts);
00043   composer_->setSubject("That's cool! Want to start your own google?");
00044 
00045   composer_->send.connect(this, &ComposeExample::send);
00046   composer_->discard.connect(this, &ComposeExample::discard);
00047 
00048   details_ = new WContainerWidget(this);
00049 
00050   new WText(tr("example.info"), details_);
00051 }
00052 
00053 void ComposeExample::send()
00054 {
00055   WContainerWidget *feedback = new WContainerWidget(this);
00056   feedback->setStyleClass(L"feedback");
00057 
00058   WContainerWidget *horiz = new WContainerWidget(feedback);
00059   new WText(L"<p>We could have, but did not send the following email:</p>",
00060             horiz);
00061 
00062   std::vector<Contact> contacts = composer_->to();
00063   if (!contacts.empty())
00064     horiz = new WContainerWidget(feedback);
00065   for (unsigned i = 0; i < contacts.size(); ++i) {
00066     new WText(L"To: \"" + contacts[i].name + L"\" <"
00067               + contacts[i].email + L">", PlainText, horiz);
00068     new WBreak(horiz);
00069   }
00070 
00071   contacts = composer_->cc();
00072   if (!contacts.empty())
00073     horiz = new WContainerWidget(feedback);
00074   for (unsigned i = 0; i < contacts.size(); ++i) {
00075     new WText(L"Cc: \"" + contacts[i].name + L"\" <"
00076               + contacts[i].email + L">", PlainText, horiz);
00077     new WBreak(horiz);
00078   }
00079   
00080   contacts = composer_->bcc();
00081   if (!contacts.empty())
00082     horiz = new WContainerWidget(feedback);
00083   for (unsigned i = 0; i < contacts.size(); ++i) {
00084     new WText(L"Bcc: \"" + contacts[i].name + L"\" <"
00085               + contacts[i].email + L">", PlainText, horiz);
00086     new WBreak(horiz);
00087   }
00088 
00089   horiz = new WContainerWidget(feedback);
00090   new WText("Subject: \"" + composer_->subject() + "\"", PlainText, horiz);
00091 
00092   std::vector<Attachment> attachments = composer_->attachments();
00093   if (!attachments.empty())
00094     horiz = new WContainerWidget(feedback);
00095   for (unsigned i = 0; i < attachments.size(); ++i) {
00096     new WText(L"Attachment: \""
00097               + attachments[i].fileName
00098               + L"\" (" + attachments[i].contentDescription
00099               + L")", PlainText, horiz);
00100 
00101     unlink(attachments[i].spoolFileName.c_str());
00102 
00103     new WText(", was in spool file: "
00104               + attachments[i].spoolFileName, horiz);
00105     new WBreak(horiz);
00106   }
00107 
00108   std::wstring message = composer_->message();
00109 
00110   horiz = new WContainerWidget(feedback);
00111   new WText("Message body: ", horiz);
00112   new WBreak(horiz);
00113 
00114   if (!message.empty()) {
00115     new WText(message, PlainText, horiz);
00116   } else
00117     new WText("<i>(empty)</i>", horiz);
00118 
00119   delete composer_;
00120   delete details_;
00121 
00122   wApp->quit();
00123 }
00124 
00125 void ComposeExample::discard()
00126 {
00127   WContainerWidget *feedback = new WContainerWidget(this);
00128   feedback->setStyleClass("feedback");
00129 
00130   WContainerWidget *horiz = new WContainerWidget(feedback);
00131   new WText("<p>Wise decision! Everyone's mailbox is already full anyway.</p>",
00132             horiz);
00133 
00134   delete composer_;
00135   delete details_;
00136 
00137   wApp->quit();
00138 }
00139 
00140 WApplication *createApplication(const WEnvironment& env)
00141 {
00142   WApplication *app = new WApplication(env);
00143 
00144   // The following assumes composer.xml is in the webserver working directory
00145   // (but does not need to be deployed within docroot):
00146   app->messageResourceBundle().use(WApplication::appRoot() + "composer");
00147 
00148   // The following assumes composer.css is deployed in the seb server at the
00149   // same location as the application:
00150   app->useStyleSheet("composer.css");
00151 
00152   app->setTitle("Composer example");
00153 
00154   app->root()->addWidget(new ComposeExample());
00155 
00156   return app;
00157 }
00158 
00159 int main(int argc, char **argv)
00160 {
00161    return WRun(argc, argv, &createApplication);
00162 }
00163 

Generated on Tue Mar 22 2016 for the C++ Web Toolkit (Wt) by doxygen 1.7.6.1