Main Page   Class Hierarchy   Compound List   File List   Compound Members  

snddlg.cpp

00001 /*
00002  * Purpose: Sample application for demonstrating and testing the wxWindows email support
00003  * Author:  Frank Buß
00004  * Created: 2002
00005  */
00006 
00007 #include <wx/datstrm.h>
00008 
00009 #include "snddlg.h"
00010 #include "wino.h"
00011 
00012 static const wxUint32 g_admFileId = ('a' | ('d' << 8) | ('m' << 16));
00013 static const wxUint32 g_admFileVersion = 1;
00014 
00015 // file format tags
00016 enum {
00017     admTagEnd = 0,
00018     admTagGrid = 2
00019 };
00020 
00021 BEGIN_EVENT_TABLE(SendDialog, wxDialog)
00022 END_EVENT_TABLE()
00023 
00024 size_t g_emailCount;
00025 
00026 static bool checkEmail(wxString& email)
00027 {
00028     static bool validChars[256];
00029     static bool initialized = false;
00030     if (!initialized) {
00031         unsigned i;
00032         for (i = 0; i <= 255; i++) validChars[i] = false;
00033         for (i = 'a'; i <= 'z'; i++) validChars[i] = true;
00034         for (i = 'A'; i <= 'Z'; i++) validChars[i] = true;
00035         for (i = '0'; i <= '9'; i++) validChars[i] = true;
00036         validChars['@'] = true;
00037         validChars['.'] = true;
00038         validChars['_'] = true;
00039         validChars['-'] = true;
00040         validChars['~'] = true;
00041         initialized = true;
00042     }
00043 
00044     // check for valid emails
00045     unsigned atcount = 0;
00046     unsigned atposition = 0;
00047     if (email.Length() < 3) return false;
00048     for (unsigned i = 0; i < email.Length(); i++) {
00049         wxChar c = email[i];
00050         if (!validChars[c]) return false;
00051         if (c == '@') {
00052             atcount++;
00053             atposition = i;
00054             if (atcount > 1) return false;
00055         }
00056     }
00057     if (atcount != 1) return false;
00058     if (atposition == 0 || atposition == email.Length() - 1) return false;
00059 
00060     // valid
00061     return true;
00062 }
00063 
00064 static void addEmails(wxEmailMessage* pMessage, wxFileName* pFilename)
00065 {
00066     // open file; TODO: file locking
00067     wxFileInputStream fileInStream(pFilename->GetFullPath());
00068     wxDataInputStream in(fileInStream);
00069 
00070     // check if right file format and file is readable
00071     wxUint32 id = in.Read32();
00072     wxUint32 version = in.Read32();
00073     if (!in.IsOk() || id != g_admFileId || version > g_admFileVersion) {
00074         wxMessageDialog errorDialog(NULL, "Fehler beim Öffnen der Datei " + pFilename->GetFullPath() + ", diese Datei wird ignoriert.", "Wino", wxOK | wxICON_INFORMATION);
00075         errorDialog.ShowModal();
00076         return;
00077     }
00078 
00079     // read file
00080     bool end = false;
00081     while (!end) {
00082         wxUint32 tag = in.Read32();
00083         wxUint32 size = in.Read32();
00084         switch (tag) {
00085             case admTagGrid:
00086                 {
00087                     // read number of cols and rows
00088                     size_t cols = in.Read32();
00089                     size_t rows = in.Read32();
00090 
00091                     // read column labels and widths
00092                     bool found = false;
00093                     size_t emailIndex = 0;
00094                     size_t i;
00095                     for (i = 0; i < cols; i++) {
00096                         wxString colLabel = in.ReadString();
00097                         if (colLabel.CmpNoCase("email") == 0) {
00098                             found = true;
00099                             emailIndex = i;
00100                         }
00101                         in.Read32();
00102                     }
00103                     if (!found) {
00104                         wxString error;
00105                         error << "Die Datei " << pFilename->GetFullPath() << " enthält keine Tabellenspalte\n"
00106                             << "mit dem Namen 'eMail' und wird daher ignoriert.";
00107                         wxMessageDialog errorDialog(NULL, error, "Wino", wxOK | wxICON_INFORMATION);
00108                         errorDialog.ShowModal();
00109                         return;
00110                     }
00111 
00112                     // read emails
00113                     for (i = 0; i < rows; i++) {
00114                         for (size_t j = 0; j < cols; j++) {
00115                             if (j == emailIndex) {
00116                                 wxString email = in.ReadString();
00117                                 if (checkEmail(email)) {
00118                                     pMessage->AddRecipient(email);
00119                                     g_emailCount++;
00120                                 }
00121                             } else {
00122                                 in.ReadString();
00123                             }
00124                         }
00125                     }
00126                     return;
00127                 }
00128                 break;
00129             case admTagEnd:
00130                 end = true;
00131                 break;
00132             default:
00133                 fileInStream.SeekI(size, wxFromCurrent);
00134         }
00135     }
00136 }
00137 
00138 SendDialog::SendDialog(WinoDialog& winoDialog)
00139                : wxDialog(&winoDialog, -1,
00140                           _("Wino eMail Versand"),
00141                           wxDefaultPosition,
00142                           wxDefaultSize,
00143                           wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL)
00144 {
00145     wxString server = winoDialog.m_pServerTextCtrl->GetValue().Trim();
00146     wxString subject = winoDialog.m_pSubjectTextCtrl->GetValue().Trim();
00147     wxString text = winoDialog.m_pTextTextCtrl->GetValue().Trim();
00148     wxString from = winoDialog.m_pFromTextCtrl->GetValue().Trim();
00149     wxString to = winoDialog.m_pToTextCtrl->GetValue().Trim();
00150 
00151     g_emailCount = 0;
00152     m_successCount = 0;
00153     wxBusyCursor wait;
00154 
00155     // create transport class
00156     m_pSMTP = new wxSMTP(this);
00157     m_pSMTP->SetHost(server);
00158 
00159     // create message class
00160     m_pMessage = new wxEmailMessage(subject, text, from);
00161 
00162     // add 'to'-recipient to message
00163     m_pMessage->AddTo(to);
00164 
00165     // add files to message
00166     int i;
00167     for (i = 0; i < winoDialog.m_pFilelist->GetCount(); i++) {
00168         wxFileName* pFilename = (wxFileName*) winoDialog.m_pFilelist->GetClientData(i);
00169         m_pMessage->AddFile(*pFilename);
00170     }
00171 
00172     // add emails to message
00173     for (i = 0; i < winoDialog.m_pAddresslist->GetCount(); i++) {
00174         wxFileName* pFilename = (wxFileName*) winoDialog.m_pAddresslist->GetClientData(i);
00175         addEmails(m_pMessage, pFilename);
00176     }
00177 
00178     // convert email count to string
00179     wxString emailCount;
00180     emailCount << g_emailCount;
00181 
00182     // create dialog
00183     wxBoxSizer* pTopSizer = new wxBoxSizer(wxVERTICAL);
00184 
00185     wxBoxSizer* pStatusSizer = new wxBoxSizer(wxHORIZONTAL);
00186     pStatusSizer->Add(new wxStaticText(this, -1, "Status:"), 1, wxALIGN_CENTER | wxLEFT | wxTOP, 10);
00187     m_pStatusText = new wxStaticText(this, -1, "eMail-Adressen werden gesendet...", wxDefaultPosition, wxSize(100, 10));
00188     pStatusSizer->Add(m_pStatusText, 3, wxLEFT | wxTOP | wxRIGHT | wxALIGN_RIGHT | wxALIGN_CENTER | wxEXPAND, 10);
00189     pTopSizer->Add(pStatusSizer, 0, wxEXPAND);
00190 
00191     wxBoxSizer* pEmailCountSizer = new wxBoxSizer(wxHORIZONTAL);
00192     pEmailCountSizer->Add(new wxStaticText(this, -1, "Anzahl gültiger eMail Adressen:"), 1, wxALIGN_CENTER | wxLEFT | wxTOP, 10);
00193     m_pEmailCountText = new wxStaticText(this, -1, emailCount, wxDefaultPosition, wxSize(100, 10));
00194     pEmailCountSizer->Add(m_pEmailCountText, 3, wxLEFT | wxTOP | wxRIGHT | wxALIGN_RIGHT | wxALIGN_CENTER | wxEXPAND, 10);
00195     pTopSizer->Add(pEmailCountSizer, 0, wxEXPAND);
00196 
00197     wxBoxSizer* pEmailSuccessSizer = new wxBoxSizer(wxHORIZONTAL);
00198     pEmailSuccessSizer->Add(new wxStaticText(this, -1, "Davon erfolgreich versendet:"), 1, wxALIGN_CENTER | wxLEFT | wxTOP, 10);
00199     m_pEmailSuccessText = new wxStaticText(this, -1, "0", wxDefaultPosition, wxSize(100, 10));
00200     pEmailSuccessSizer->Add(m_pEmailSuccessText, 3, wxLEFT | wxTOP | wxRIGHT | wxALIGN_RIGHT | wxALIGN_CENTER | wxEXPAND, 10);
00201     pTopSizer->Add(pEmailSuccessSizer, 0, wxEXPAND);
00202 
00203     wxStaticBoxSizer* pLogSizer = new wxStaticBoxSizer(new wxStaticBox(this, -1, "Bericht"), wxVERTICAL);
00204     m_pLogTextCtrl = new wxTextCtrl(this, -1, "", wxDefaultPosition, wxSize(500, 300), wxTE_MULTILINE | wxTE_READONLY);
00205     pLogSizer->Add(m_pLogTextCtrl, 0, wxEXPAND | wxALL, 10);
00206     pTopSizer->Add(pLogSizer, 0, wxEXPAND | wxALL, 10);
00207 
00208     wxBoxSizer* pSendCloseSizer = new wxBoxSizer(wxHORIZONTAL);
00209     wxButton* pCloseButton = new wxButton(this, wxID_CANCEL, "Beenden");
00210     pCloseButton->SetDefault();
00211     pSendCloseSizer->Add(pCloseButton, 0, wxALL, 10);
00212     pTopSizer->Add(pSendCloseSizer, 0, wxALIGN_RIGHT);
00213 
00214     SetAutoLayout(true);
00215     SetSizer(pTopSizer);
00216     pTopSizer->Fit(this);
00217     pTopSizer->SetSizeHints(this);
00218 
00219     // show dialog
00220     Centre(wxBOTH | wxCENTRE_ON_SCREEN);
00221 
00222     // start email sending
00223     m_pSMTP->Send(m_pMessage);
00224 }
00225 
00226 SendDialog::~SendDialog()
00227 {
00228     delete m_pSMTP;
00229     delete m_pMessage;
00230 }
00231 
00232 
00233 void SendDialog::OnSocketError(int errorCode)
00234 {
00235 }
00236 
00237 void SendDialog::OnRecipientAdded(const wxString& address, int errorCode)
00238 {
00239     if (errorCode) {
00240         m_successCount++;
00241         wxString l;
00242         l << m_successCount;
00243         m_pEmailSuccessText->SetLabel(l);
00244     }
00245 }
00246 
00247 void SendDialog::OnDataSent(int errorCode)
00248 {
00249     m_pStatusText->SetLabel("eMail-Versand beendet.");
00250     wxMessageBox(_("eMail-Versand beendet."), _("Wino"), wxOK | wxICON_INFORMATION);
00251 }

Generated on Mon May 6 01:14:20 2002 for Wino by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001