summaryrefslogtreecommitdiffstats
path: root/kdejava/koala/examples/simplemail/SimpleMailFrm.java
blob: f4c22a53afd46e1d2a9a4b1691bd46b8fbc5a759 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// package com.werpu.simplemail;


/**
 * SimpleMailFrm
 * This is the main window from of the simple mail application
 * @author Werner Punz werpu@gmx.at
 * This class was generated by Qt Designer and then converted to Java and
 * redesigned to fit into the KDE Framework
 */

import org.kde.qt.*;
import org.kde.koala.*;
import java.util.StringTokenizer;
import java.util.LinkedList;



class SimpleMailFrm extends KMainWindow {
    
    
    TQGroupBox groupBox1;
    KLineEdit txtServer;
    KLineEdit txtUserName;
    KPasswordEdit txtPassword;
    TQLabel textLabel1;
    TQLabel textLabel2;
    TQLabel textLabel3;
    KLineEdit txtFrom;
    KLineEdit txtTo;
    KLineEdit txtBCC;
    TQLabel textLabel4;
    TQLabel textLabel6;
    TQLabel textLabel5;
    KPushButton btSend;
    KPushButton btCancel;
    TQMultiLineEdit txtMessage;
    
    
    TQGridLayout form1Layout;
    TQGridLayout groupBox1Layout;
    
    TDEApplication parentApp = null;
    
    public SimpleMailFrm(TDEApplication kApp)
    
    {
        super( null, null,0);
        parentApp = kApp;
        
        
        resize( 582, 486 );
        setCaption( trUtf8( "Simple Mailer" ) );
        
        TQGroupBox centralBox = new TQGroupBox((TQWidget) this,"");
        
        form1Layout = new TQGridLayout(centralBox, 1, 1, 11, 6, "form1Layout");
        
        groupBox1 = new TQGroupBox( centralBox, "groupBox1" );
        groupBox1.setTitle( trUtf8( "Mailserver data" ) );
        groupBox1.setColumnLayout(0, Qt.Vertical );
        groupBox1.layout().setSpacing( 6 );
        groupBox1.layout().setMargin( 11 );
        groupBox1Layout = new TQGridLayout( groupBox1.layout() );
        groupBox1Layout.setAlignment( Qt.AlignTop );
        
        txtServer = new KLineEdit( groupBox1, "txtServer" );
        
        groupBox1Layout.addWidget( txtServer, 0, 1 );
        
        txtUserName = new KLineEdit( groupBox1, "txtUserName" );
        
        groupBox1Layout.addWidget( txtUserName, 1, 1 );
        
        txtPassword = new KPasswordEdit( groupBox1, "txtPassword" );
        
        groupBox1Layout.addWidget( txtPassword, 2, 1 );
        
        textLabel1 = new TQLabel( groupBox1, "textLabel1" );
        textLabel1.setText( trUtf8( "Server" ) );
        
        groupBox1Layout.addWidget( textLabel1, 0, 0 );
        
        textLabel2 = new TQLabel( groupBox1, "textLabel2" );
        textLabel2.setText( trUtf8( "Username" ) );
        
        groupBox1Layout.addWidget( textLabel2, 1, 0 );
        
        textLabel3 = new TQLabel( groupBox1, "textLabel3" );
        textLabel3.setText( trUtf8( "Password" ) );
        
        groupBox1Layout.addWidget( textLabel3, 2, 0 );
        
        txtFrom = new KLineEdit( groupBox1, "txtFrom" );
        
        groupBox1Layout.addWidget( txtFrom, 0, 3 );
        
        txtTo = new KLineEdit( groupBox1, "txtTo" );
        
        groupBox1Layout.addWidget( txtTo, 1, 3 );
        
        txtBCC = new KLineEdit( groupBox1, "txtBCC" );
        
        groupBox1Layout.addWidget( txtBCC, 2, 3 );
        
        textLabel4 = new TQLabel( groupBox1, "textLabel4" );
        textLabel4.setText( trUtf8( "From" ) );
        
        groupBox1Layout.addWidget( textLabel4, 0, 2 );
        
        textLabel6 = new TQLabel( groupBox1, "textLabel6" );
        textLabel6.setText( trUtf8( "BCC" ) );
        
        groupBox1Layout.addWidget( textLabel6, 2, 2 );
        
        textLabel5 = new TQLabel( groupBox1, "textLabel5" );
        textLabel5.setText( trUtf8( "To" ) );
        
        groupBox1Layout.addWidget( textLabel5, 1, 2 );
        
        form1Layout.addMultiCellWidget( groupBox1, 0, 0, 0, 1 );
        
        btSend = new KPushButton( centralBox, "btSend" );
        btSend.setText( trUtf8( "Send" ) );
        
        form1Layout.addWidget( btSend, 2, 0 );
        
        btCancel = new KPushButton( centralBox, "btCancel" );
        btCancel.setText( trUtf8( "Cancel" ) );
        
        form1Layout.addWidget( btCancel, 2, 1 );
        
        txtMessage = new TQMultiLineEdit(centralBox, "txtMessage" );
        
        form1Layout.addMultiCellWidget( txtMessage, 1, 1, 0, 1 );
        
        setCentralWidget(centralBox);
        // tab order
        setTabOrder( txtServer, txtUserName );
        setTabOrder( txtUserName, txtPassword );
        setTabOrder( txtPassword, txtFrom );
        setTabOrder( txtFrom, txtTo );
        setTabOrder( txtTo, txtBCC );
        setTabOrder( txtBCC, txtMessage );
        setTabOrder( txtMessage, btSend );
        setTabOrder( btSend, btCancel );
        
        setEventHandlers();
    }
    
    //----------------------------------------------------------
    // Getter Methods to access the data outside of the
    // current class
    //----------------------------------------------------------
    
    public String getServer() {
        return txtServer.text();
    }
    
    public String getUserName() {
        return txtUserName.text();
    }
    
    public String getPassword() {
        return txtPassword.text();
    }
    
    public String getFrom() {
        return txtFrom.text();
    }
    
    public String getTo() {
        return txtTo.text();
    }
    
    public String getBCC() {
        return txtBCC.text();
    }
    
    public String getMessage() {
        return txtMessage.text();
    }
    
    void setEventHandlers() {
        connect( btCancel, SIGNAL("clicked()"), parentApp, SLOT("quit()"));
        connect( btSend,SIGNAL("clicked()"),this,SLOT("sendMail()"));
    }

    
    //--------------------------------------------
    // Slots
    //-------------------------------------------
    public void sendMail() {
        String server = getServer();
        String userName = getUserName();
        String password = getPassword();
        String from = getFrom();
        String to = getTo();
        String message = getMessage();
        LinkedList bccs = getBCCs();
        
        try {
            MailHelper mailer = new MailHelper();
            mailer.setMessage(message);
            mailer.setSubject("Simple Mail");
            mailer.setSender(from);
            mailer.setRecipient(to);
            mailer.addCCAddress(bccs.iterator());
            mailer.setServer(server);
            mailer.setUsername(userName);
            mailer.setPassword(password);
            mailer.send();
        }
        catch (Exception ex) {
            KMessageBox.error(this , ex.getMessage());
            return;
        }
        KMessageBox.information(this,"Mail was successfully sent!");
    }
    
    //-------------------------------------
    //Helpers
    //-------------------------------------
    
    /**
     * Split the BCCs into single entries if nedded
     */
    LinkedList getBCCs() {
        LinkedList retVal = new LinkedList();
        
        String bcc = getBCC();
        StringTokenizer splitter = new StringTokenizer(bcc,",;");
        
        while(splitter.hasMoreTokens())
            retVal.add(splitter.nextToken());
        
        return retVal;
    }
}