Wt examples  4.10.4
AddresseeEdit.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 <boost/tokenizer.hpp>
8 #include <boost/algorithm/string.hpp>
9 
10 #include <Wt/WContainerWidget.h>
11 #include <Wt/WStringUtil.h>
12 
13 #include "AddresseeEdit.h"
14 #include "Label.h"
15 
17  : WTextArea()
18 {
19  label_ = labelParent->addWidget(std::make_unique<Label>(label, labelParent));
20 
21  setRows(3); setColumns(55);
22  resize(WLength(99, LengthUnit::Percentage), WLength::Auto);
23 
24  setInline(false); // for IE to position the suggestions well
25 }
26 
27 void AddresseeEdit::setAddressees(const std::vector<Contact>& contacts)
28 {
29  std::u32string text;
30 
31  for (unsigned i = 0; i < contacts.size(); ++i) {
32  if (i != 0)
33  text += U", ";
34  text += contacts[i].formatted();
35  }
36 
37  setText(text);
38 }
39 
40 bool AddresseeEdit::parse(std::vector<Contact>& contacts) const
41 {
42  typedef boost::tokenizer<boost::escaped_list_separator<char32_t>,
43  std::wstring::const_iterator, std::wstring>
44  CsvTokenizer;
45 
46  std::wstring t = text();
47  CsvTokenizer tok(t);
48 
49  for (CsvTokenizer::iterator i = tok.begin(); i != tok.end(); ++i) {
50  std::wstring addressee = *i;
51 
52  boost::trim(addressee);
53  std::wstring::size_type pos = addressee.find_last_of(' ');
54  if (pos != std::string::npos) {
55  std::wstring email = addressee.substr(pos + 1);
56  std::wstring name = addressee.substr(0, pos);
57 
58  boost::trim(email);
59  boost::trim(name);
60  if (email[0] == '<')
61  email = email.substr(1);
62  if (email[email.length() - 1] == '>')
63  email = email.substr(0, email.length() - 1);
64 
65  if (!email.empty())
66  contacts.push_back(Contact(Wt::toUTF32(name), Wt::toUTF32(email)));
67  } else
68  if (!addressee.empty())
69  contacts.push_back(Contact(U"", Wt::toUTF32(addressee)));
70  }
71  return true;
72 }
73 
74 std::vector<Contact> AddresseeEdit::addressees() const
75 {
76  std::vector<Contact> result;
77  parse(result);
78 
79  return result;
80 }
81 
82 void AddresseeEdit::setHidden(bool hidden, const WAnimation& animation)
83 {
84  WTextArea::setHidden(hidden, animation);
85  label_->setHidden(hidden, animation);
86 }
AddresseeEdit(const WString &label, WContainerWidget *labelParent)
Create a new addressee edit with the given label.
Definition: AddresseeEdit.C:16
virtual void setHidden(bool hidden, const WAnimation &animation)
Reimplement hide() and show() to also hide() and show() the label.
Definition: AddresseeEdit.C:82
std::vector< Contact > addressees() const
Get a list of addressees.
Definition: AddresseeEdit.C:74
Label * label_
The label associated with this edit.
Definition: AddresseeEdit.h:54
bool parse(std::vector< Contact > &contacts) const
Parse the addressees into a list of contacts.
Definition: AddresseeEdit.C:40
void setAddressees(const std::vector< Contact > &contacts)
Set a list of addressees.
Definition: AddresseeEdit.C:27
virtual void addWidget(std::unique_ptr< WWidget > widget)
WLabel * label() const
virtual void setText(const WString &text)
void setRows(int rows)
const WString & text() const
void setColumns(int cols)
virtual void resize(const WLength &width, const WLength &height) override
virtual void setInline(bool isInline) override
virtual void setHidden(bool hidden, const WAnimation &animation=WAnimation()) override
std::u32string toUTF32(const std::u16string &s)
An email contact.
Definition: Contact.h:20