SOL9 2.0 Sample: SolMailer

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * SolMailer.cpp 
 * Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */



#include <sol\ApplicationView.h>
#include <sol\TextField.h>
#include <sol\LabelGadget.h>
#include <sol\Static.h>
#include <sol\PushButton.h>
#include <sol\ScrolledRichText.h>
#include <sol\DesktopWindow.h>
#include <sol\Profile.h>
#include <sol\Font.h>
#include "resource.h"
#include <sol\PaintDC.h>
#include <sol\PopupView.h>
#include <sol\Clientdc.h>
#include <sol\StringList.h>
#include <sol\StringBuffer.h>
#include <sol\StringTokenizer.h>
#include <sol\Exception.h>

#include <sol/StringT.h>
#include <sol/StringTokenizerT.h>
#include <sol/StringListT.h>

#include "SMTPClient.h"
#include "Mail.h"
#include "ConfigDialog.h"

namespace SOL {

class SolMailer :public ApplicationView {
private:
    Font               font;
    LabelGadget        subject;
    LabelGadget        to;
    LabelGadget        cc;

    RichText           textf;
    ScrolledRichText   tolist;
    ScrolledRichText   cclist;
    ScrolledRichText    bcclist;    // Currently unused.

    ScrolledRichText    sctext;
    PopupView*          setupdlg;
    int                 textHeight;
    ConfigDialog        configDialog;

    StringT<char>        server;    //SMTP Server smtpserver.foo.com
    ushort                port;    //25
    StringT<char>        address;    //someone@foo.com

    Profile profile;

public:
    /**
     *
     */
    SolMailer(Application& applet, const TCHAR* name, Args& args)
    :ApplicationView(applet, name, 
        args.set(XmNbackground, (ulong)(COLOR_BTNFACE+1))
            .set(XmNexStyle, (ulong)WS_EX_CLIENTEDGE)    )
    {
        LOGFONT lf;
        getDisplayFont(lf);

        Args ar;
        //ar.reset();
        //ar.set(XmNheight, (-12));
        font.create(&lf);
        ClientDC dc(this);
        HFONT prevFont = (HFONT)dc.select(&font);
        textHeight = dc.getTextHeight();
        dc.select(prevFont);

        subject.create(this, _T("Subject"), ar);
    //subject -> setFont(&font);

        ar.reset();
        to.create(this, _T("TO"), ar);
        //to -> setFont(&font);

        ar.reset();
        cc.create(this, _T("CC"), ar);
        //cc -> setFont(font);

        ar.reset();
        textf.create(this, NULL,   ar);
    //textf -> setFont(font);

        ar.reset();
        ar.set(XmNstyle, (ulong)ES_MULTILINE);
        tolist.create(this, NULL, ar);
        //tolist -> setFont(font);

        ar.reset();
        ar.set(XmNstyle, (ulong)ES_MULTILINE);
        cclist.create(this, NULL, ar);
    //cclist -> setFont(font);

        ar.reset();
        ar.set(XmNstyle, (ulong)ES_MULTILINE);
        bcclist.create(this, NULL, ar);
    //bcclist -> setFont(font);

        ar.reset();
        ar.set(XmNstyle, (ulong)ES_MULTILINE);
        sctext.create(this, NULL, ar);
    //sctext -> setFont(font);

        sctext.exLimitText(100*1000);

        addCallback(XmNmenuCallback, ID_NEW, this, 
            (Callback)&SolMailer::clear, NULL);

        addCallback(XmNmenuCallback, ID_SEND, this, 
            (Callback)&SolMailer::send, NULL);

        addCallback(XmNmenuCallback, ID_EXIT, this, 
            (Callback)&SolMailer::exit, NULL);

        addCallback(XmNmenuCallback, ID_CONFIG, this, 
            (Callback)&SolMailer::config, NULL);

        addEventHandler(WM_CLOSE, this,
            (Handler)&SolMailer::close, NULL);

        addEventHandler(WM_PAINT, this, 
            (Handler)&SolMailer::paint, NULL);

        restorePlacement();

        ar.reset();
        ar.set(XmNwidth, 340);
        ar.set(XmNheight, 200);
        ar.set(XmNstyle, (ulong)WS_THICKFRAME);
        configDialog.create(this, _T("Configuration"), ar);
        configDialog.addOkCallback(this, (Callback)&SolMailer::updateConfig);

        loadProfile();
    }

public:
    ~SolMailer()
    { 
    }

private:
    void loadProfile()
    {
        //Profile profile(_T("SolMailer"));
        //profile.get(_T("SMTPServer"), _T("Name"), server1, SizeOf(server1)-1);
        profile.get(_T("SMTPServer"), _T("Name"), this->server);

        this->port =  (ushort)profile.get(_T("SMTPServer"), _T("Port"), 25);
    
        profile.get(_T("Account"), _T("EmailAddress"), this->address);
    }

private:
    void config(Action& action)
    {
        configDialog.setServer((const char*)server);
        configDialog.setPort(port);
        configDialog.setAddress((const char*)address);

        configDialog.popup(action);
    }

private:
    void updateConfig(Action& action)
    {
        configDialog.getServer(this->server);

        port = configDialog.getPort();

        configDialog.getAddress(this->address);
        Printf(_T("SolMailer::updateConfig\r\n"));

        if (server.getLength() > 0 && port > 0 && address.getLength()>0) {
        //    Profile profile(_T("SolMailer"));
            profile.set(_T("SMTPServer"), _T("Name"), this->server);
            profile.set(_T("SMTPServer"), _T("Port"),   port);
            profile.set(_T("Account"),    _T("EmailAddress"), this->address);

            //Printf("%s  %d %s \r\n", server1, port1, address1);

            configDialog.popdown(action);
        } else {
            //Don't popdown configDialog.

        }
    }

private:
    void clear(Action& action)
    {
        int rc =MessageBox(NULL, _T("Are you sure to create a new mail?"), 
            _T("New"), MB_OKCANCEL);
        if(rc == IDOK) {
            sctext.setText(_T(""));
            tolist.setText(_T(""));
            cclist.setText(_T(""));
            bcclist.setText(_T(""));
            textf.setText(_T(""));
        }
    }

private:
    int getStringList(__in Text& text, __out StringListT<char>& list)
    {
        StringT<char> string;
        int rc = text.getText(string);
        if (rc <= 0) {
            //text is empty (doesn't contain any string).
            return rc;
        }

        const char* buffer = (const char*)string;

        if (buffer && strlen(buffer)) {
            char* ptr = (char*)buffer;
            // Replace "\r\n" by ", ".
            while (ptr = strstr(ptr, "\r\n") ) {
                *ptr = ',';
                *(ptr+1) = ' ';
                ptr = ptr + 2;
            }

            StringTokenizerT<char> tokenizer(buffer);

            tokenizer.clearSeparator();
            tokenizer.addSeparator(',');
            tokenizer.addSeparator(';');
            StringT<char> token = "";
        
            // Get a token and append it to the list.
            while (tokenizer.getToken(token)) {
                if (token.getLength() >0) {
                    list.add((const char*)token);
                    rc++;
                }
                token = "";
            }
        }
        return rc;
    }

private:
    void send(Action& action)
    {
        int rc = showMessageDialog(_T("SolMailer"), 
            _T("Are you sure to send this mail?"),
            MB_OKCANCEL|MB_ICONQUESTION);

        if(rc == IDOK) {
            StringListT<char> tos, ccs, bccs;

            getStringList(tolist,  tos);
            getStringList(cclist,  ccs);
            getStringList(bcclist, bccs);

            if (tos.getLength() == 0) {
                showMessageDialog(_T("Error"), _T("TO receiver is empty"));
                return;
            }

            StringT<char> subject;
            textf.getText(subject);
            StringT<char>  body;
            sctext.getText(body);

            //const char* sender = (const char*)address;

            Mail mail(subject, address, tos, ccs, bccs, body);

            //Create an SMTPClient thread.
            SMTPClient client(server, 
                            port, address, mail);
            
            //Start it.
            client.start();

            client.wait();
            int result = client.getResult();
            if (result == 0) {
                const TCHAR* error = client.getError();
                showMessageDialog(_T("Error"), error);
            } else {
                TCHAR buff[256];
                _stprintf_s(buff, SizeOf(buff), _T("Sent %d mail(s)"), result);
                showMessageDialog(_T("SolMailer"), buff);
            }
        }
    }

private:

    long close(Event& event)
    {
        savePlacement();

        return defaultProc(event);
    }


private:
    long paint(Event& event)    
    {
        PaintDC dc(this);
        subject.draw(&dc);
        to.draw(&dc);
        cc.draw(&dc);

        return 0L;
    }

    
private:
    long size(Event& event)
    {

        LPARAM pos = event.getLParam();
        int h = HIWORD(pos);
        int w = LOWORD(pos);
        int x = 80;
        int y1 = textHeight+4;    //30;
        int y2 = textHeight*2+4;
        int lw = textHeight*2; //40;
        int top = 2;
        int m  = 2;
        int ww, hh;
    
        subject.getSize(ww, hh);
        ww +=2;
        to.setLocation(m, top);
        tolist.reshape(m+ww,top, w-m*2-ww, y2);

        cc.setLocation(m, top+y2+m);
        cclist.reshape(m+ww, top+y2+m, w-m*2-ww, y2);

        subject.setLocation(m, top+(y2+m)*2);
    
        textf.reshape(m+ww, top+(y2+m)*2, w-m*2-ww, y1);

        sctext.reshape(m, top+y1+m+(y2+m)*2, w-m*2, 
            h-top-y1-m-(y2+m)*2 -m);
        return 0L;
    }

};

}


// SolMailer Main
void    Main(int argc, TCHAR** argv)
{
    const String appClass = "SolMailer";

    try {
        Application applet(appClass, argc, argv);
        
        Args args;
        args.set(XmNbackground, (ulong)(COLOR_BTNFACE+1));
        args.set(XmNexStyle, (ulong)WS_EX_CONTROLPARENT);
        SolMailer solMailer(applet, appClass, args);
        solMailer.realize();

        applet.run();
    } catch (...) {

    }
}

Last modified: 11 Nov 2009

Copyright (c) 2009 Antillia.com ALL RIGHTS RESERVED.