251 lines
42 KiB
C++
251 lines
42 KiB
C++
/*
|
|
* Purpose: Sample application for demonstrating and testing the wxWindows email support
|
|
* Author: Frank Buß
|
|
* Created: 2002
|
|
*/
|
|
|
|
#include <wx/datstrm.h>
|
|
|
|
#include "snddlg.h"
|
|
#include "wino.h"
|
|
|
|
static const wxUint32 g_admFileId = ('a' | ('d' << 8) | ('m' << 16));
|
|
static const wxUint32 g_admFileVersion = 1;
|
|
|
|
// file format tags
|
|
enum {
|
|
admTagEnd = 0,
|
|
admTagGrid = 2
|
|
};
|
|
|
|
BEGIN_EVENT_TABLE(SendDialog, wxDialog)
|
|
END_EVENT_TABLE()
|
|
|
|
size_t g_emailCount;
|
|
|
|
static bool checkEmail(wxString& email)
|
|
{
|
|
static bool validChars[256];
|
|
static bool initialized = false;
|
|
if (!initialized) {
|
|
unsigned i;
|
|
for (i = 0; i <= 255; i++) validChars[i] = false;
|
|
for (i = 'a'; i <= 'z'; i++) validChars[i] = true;
|
|
for (i = 'A'; i <= 'Z'; i++) validChars[i] = true;
|
|
for (i = '0'; i <= '9'; i++) validChars[i] = true;
|
|
validChars['@'] = true;
|
|
validChars['.'] = true;
|
|
validChars['_'] = true;
|
|
validChars['-'] = true;
|
|
validChars['~'] = true;
|
|
initialized = true;
|
|
}
|
|
|
|
// check for valid emails
|
|
unsigned atcount = 0;
|
|
unsigned atposition = 0;
|
|
if (email.Length() < 3) return false;
|
|
for (unsigned i = 0; i < email.Length(); i++) {
|
|
wxChar c = email[i];
|
|
if (!validChars[c]) return false;
|
|
if (c == '@') {
|
|
atcount++;
|
|
atposition = i;
|
|
if (atcount > 1) return false;
|
|
}
|
|
}
|
|
if (atcount != 1) return false;
|
|
if (atposition == 0 || atposition == email.Length() - 1) return false;
|
|
|
|
// valid
|
|
return true;
|
|
}
|
|
|
|
static void addEmails(wxEmailMessage* pMessage, wxFileName* pFilename)
|
|
{
|
|
// open file; TODO: file locking
|
|
wxFileInputStream fileInStream(pFilename->GetFullPath());
|
|
wxDataInputStream in(fileInStream);
|
|
|
|
// check if right file format and file is readable
|
|
wxUint32 id = in.Read32();
|
|
wxUint32 version = in.Read32();
|
|
if (!in.IsOk() || id != g_admFileId || version > g_admFileVersion) {
|
|
wxMessageDialog errorDialog(NULL, "Fehler beim Öffnen der Datei " + pFilename->GetFullPath() + ", diese Datei wird ignoriert.", "Wino", wxOK | wxICON_INFORMATION);
|
|
errorDialog.ShowModal();
|
|
return;
|
|
}
|
|
|
|
// read file
|
|
bool end = false;
|
|
while (!end) {
|
|
wxUint32 tag = in.Read32();
|
|
wxUint32 size = in.Read32();
|
|
switch (tag) {
|
|
case admTagGrid:
|
|
{
|
|
// read number of cols and rows
|
|
size_t cols = in.Read32();
|
|
size_t rows = in.Read32();
|
|
|
|
// read column labels and widths
|
|
bool found = false;
|
|
size_t emailIndex = 0;
|
|
size_t i;
|
|
for (i = 0; i < cols; i++) {
|
|
wxString colLabel = in.ReadString();
|
|
if (colLabel.CmpNoCase("email") == 0) {
|
|
found = true;
|
|
emailIndex = i;
|
|
}
|
|
in.Read32();
|
|
}
|
|
if (!found) {
|
|
wxString error;
|
|
error << "Die Datei " << pFilename->GetFullPath() << " enthält keine Tabellenspalte\n"
|
|
<< "mit dem Namen 'eMail' und wird daher ignoriert.";
|
|
wxMessageDialog errorDialog(NULL, error, "Wino", wxOK | wxICON_INFORMATION);
|
|
errorDialog.ShowModal();
|
|
return;
|
|
}
|
|
|
|
// read emails
|
|
for (i = 0; i < rows; i++) {
|
|
for (size_t j = 0; j < cols; j++) {
|
|
if (j == emailIndex) {
|
|
wxString email = in.ReadString();
|
|
if (checkEmail(email)) {
|
|
pMessage->AddRecipient(email);
|
|
g_emailCount++;
|
|
}
|
|
} else {
|
|
in.ReadString();
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
break;
|
|
case admTagEnd:
|
|
end = true;
|
|
break;
|
|
default:
|
|
fileInStream.SeekI(size, wxFromCurrent);
|
|
}
|
|
}
|
|
}
|
|
|
|
SendDialog::SendDialog(WinoDialog& winoDialog)
|
|
: wxDialog(&winoDialog, -1,
|
|
_("Wino eMail Versand"),
|
|
wxDefaultPosition,
|
|
wxDefaultSize,
|
|
wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL)
|
|
{
|
|
wxString server = winoDialog.m_pServerTextCtrl->GetValue().Trim();
|
|
wxString subject = winoDialog.m_pSubjectTextCtrl->GetValue().Trim();
|
|
wxString text = winoDialog.m_pTextTextCtrl->GetValue().Trim();
|
|
wxString from = winoDialog.m_pFromTextCtrl->GetValue().Trim();
|
|
wxString to = winoDialog.m_pToTextCtrl->GetValue().Trim();
|
|
|
|
g_emailCount = 0;
|
|
m_successCount = 0;
|
|
wxBusyCursor wait;
|
|
|
|
// create transport class
|
|
m_pSMTP = new wxSMTP(this);
|
|
m_pSMTP->SetHost(server);
|
|
|
|
// create message class
|
|
m_pMessage = new wxEmailMessage(subject, text, from);
|
|
|
|
// add 'to'-recipient to message
|
|
m_pMessage->AddTo(to);
|
|
|
|
// add files to message
|
|
int i;
|
|
for (i = 0; i < winoDialog.m_pFilelist->GetCount(); i++) {
|
|
wxFileName* pFilename = (wxFileName*) winoDialog.m_pFilelist->GetClientData(i);
|
|
m_pMessage->AddFile(*pFilename);
|
|
}
|
|
|
|
// add emails to message
|
|
for (i = 0; i < winoDialog.m_pAddresslist->GetCount(); i++) {
|
|
wxFileName* pFilename = (wxFileName*) winoDialog.m_pAddresslist->GetClientData(i);
|
|
addEmails(m_pMessage, pFilename);
|
|
}
|
|
|
|
// convert email count to string
|
|
wxString emailCount;
|
|
emailCount << g_emailCount;
|
|
|
|
// create dialog
|
|
wxBoxSizer* pTopSizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
wxBoxSizer* pStatusSizer = new wxBoxSizer(wxHORIZONTAL);
|
|
pStatusSizer->Add(new wxStaticText(this, -1, "Status:"), 1, wxALIGN_CENTER | wxLEFT | wxTOP, 10);
|
|
m_pStatusText = new wxStaticText(this, -1, "eMail-Adressen werden gesendet...", wxDefaultPosition, wxSize(100, 10));
|
|
pStatusSizer->Add(m_pStatusText, 3, wxLEFT | wxTOP | wxRIGHT | wxALIGN_RIGHT | wxALIGN_CENTER | wxEXPAND, 10);
|
|
pTopSizer->Add(pStatusSizer, 0, wxEXPAND);
|
|
|
|
wxBoxSizer* pEmailCountSizer = new wxBoxSizer(wxHORIZONTAL);
|
|
pEmailCountSizer->Add(new wxStaticText(this, -1, "Anzahl gültiger eMail Adressen:"), 1, wxALIGN_CENTER | wxLEFT | wxTOP, 10);
|
|
m_pEmailCountText = new wxStaticText(this, -1, emailCount, wxDefaultPosition, wxSize(100, 10));
|
|
pEmailCountSizer->Add(m_pEmailCountText, 3, wxLEFT | wxTOP | wxRIGHT | wxALIGN_RIGHT | wxALIGN_CENTER | wxEXPAND, 10);
|
|
pTopSizer->Add(pEmailCountSizer, 0, wxEXPAND);
|
|
|
|
wxBoxSizer* pEmailSuccessSizer = new wxBoxSizer(wxHORIZONTAL);
|
|
pEmailSuccessSizer->Add(new wxStaticText(this, -1, "Davon erfolgreich versendet:"), 1, wxALIGN_CENTER | wxLEFT | wxTOP, 10);
|
|
m_pEmailSuccessText = new wxStaticText(this, -1, "0", wxDefaultPosition, wxSize(100, 10));
|
|
pEmailSuccessSizer->Add(m_pEmailSuccessText, 3, wxLEFT | wxTOP | wxRIGHT | wxALIGN_RIGHT | wxALIGN_CENTER | wxEXPAND, 10);
|
|
pTopSizer->Add(pEmailSuccessSizer, 0, wxEXPAND);
|
|
|
|
wxStaticBoxSizer* pLogSizer = new wxStaticBoxSizer(new wxStaticBox(this, -1, "Bericht"), wxVERTICAL);
|
|
m_pLogTextCtrl = new wxTextCtrl(this, -1, "", wxDefaultPosition, wxSize(500, 300), wxTE_MULTILINE | wxTE_READONLY);
|
|
pLogSizer->Add(m_pLogTextCtrl, 0, wxEXPAND | wxALL, 10);
|
|
pTopSizer->Add(pLogSizer, 0, wxEXPAND | wxALL, 10);
|
|
|
|
wxBoxSizer* pSendCloseSizer = new wxBoxSizer(wxHORIZONTAL);
|
|
wxButton* pCloseButton = new wxButton(this, wxID_CANCEL, "Beenden");
|
|
pCloseButton->SetDefault();
|
|
pSendCloseSizer->Add(pCloseButton, 0, wxALL, 10);
|
|
pTopSizer->Add(pSendCloseSizer, 0, wxALIGN_RIGHT);
|
|
|
|
SetAutoLayout(true);
|
|
SetSizer(pTopSizer);
|
|
pTopSizer->Fit(this);
|
|
pTopSizer->SetSizeHints(this);
|
|
|
|
// show dialog
|
|
Centre(wxBOTH | wxCENTRE_ON_SCREEN);
|
|
|
|
// start email sending
|
|
m_pSMTP->Send(m_pMessage);
|
|
}
|
|
|
|
SendDialog::~SendDialog()
|
|
{
|
|
delete m_pSMTP;
|
|
delete m_pMessage;
|
|
}
|
|
|
|
|
|
void SendDialog::OnSocketError(int errorCode)
|
|
{
|
|
}
|
|
|
|
void SendDialog::OnRecipientAdded(const wxString& address, int errorCode)
|
|
{
|
|
if (errorCode) {
|
|
m_successCount++;
|
|
wxString l;
|
|
l << m_successCount;
|
|
m_pEmailSuccessText->SetLabel(l);
|
|
}
|
|
}
|
|
|
|
void SendDialog::OnDataSent(int errorCode)
|
|
{
|
|
m_pStatusText->SetLabel("eMail-Versand beendet.");
|
|
wxMessageBox(_("eMail-Versand beendet."), _("Wino"), wxOK | wxICON_INFORMATION);
|
|
}
|