KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > actions > ReceiveAction


1 package SnowMailClient.view.actions;
2
3 import SnowMailClient.model.folders.*;
4 import SnowMailClient.model.accounts.*;
5 import SnowMailClient.model.*;
6 import snow.utils.gui.*;
7
8 import SnowMailClient.utils.*;
9 import SnowMailClient.MailEngine.*;
10 import SnowMailClient.SnowMailClientApp;
11 import SnowMailClient.view.folders.*;
12 import SnowMailClient.Language.Language;
13
14 import java.awt.*;
15 import java.util.*;
16 import java.awt.event.*;
17 import javax.swing.*;
18 import javax.swing.event.*;
19 import javax.swing.tree.*;
20 import java.io.*;
21
22 public final class ReceiveAction extends AbstractAction
23 {
24   final private FolderTreeNode inbox, outbox;
25   private MailFolder inboxFolder, outboxFolder;
26   final private MailAccounts accounts;
27   final private SnowMailClientApp ref;
28
29   private ReceiveAction(
30      SnowMailClientApp ref,
31      MailAccounts accounts,
32      FolderTreeNode inbox,
33      FolderTreeNode outbox)
34   {
35      super(Language.translate("(Old) Receive"));
36
37      this.accounts = accounts;
38      this.inbox = inbox;
39      this.outbox = outbox;
40      this.ref = ref;
41
42      putValue(AbstractAction.SMALL_ICON, SnowMailClientApp.loadImageIcon("pics/receivemail2.PNG") );
43
44 /* putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke( KeyEvent.VK_R, KeyEvent.CTRL_MASK ) ); */
45
46      putValue(AbstractAction.SHORT_DESCRIPTION, Language.translate("Receive new mail from all accounts"));
47
48      try
49      {
50        inboxFolder = inbox.getMailFolder();
51        outboxFolder = outbox.getMailFolder();
52      }
53      catch(Exception JavaDoc ex)
54      {
55        ex.printStackTrace();
56      }
57   } // Constructor
58

59   public void actionPerformed(ActionEvent e)
60   {
61      final ProgressModalDialog progressDialog = new ProgressModalDialog(
62             ref, "Retrieving new mails", true, true);
63      progressDialog.start();
64
65      // here we're in the EDT
66
Thread JavaDoc t = new Thread JavaDoc()
67      {
68        public void run()
69        {
70
71          try
72          {
73             receiveAction(progressDialog);
74          }
75          finally
76          {
77             progressDialog.closeDialog();
78          }
79
80        }
81      };
82      t.setPriority(Thread.NORM_PRIORITY-1);
83      t.start();
84   }
85
86
87   /** Must be called in a thread !
88   */

89   public void receiveAction(final ProgressModalDialog progressDialog)
90   {
91     if( SwingUtilities.isEventDispatchThread())
92     {
93       throw new RuntimeException JavaDoc("Must be called from another thread than EDT");
94     }
95     SnowMailClientApp.getInstance().saveAllMails(false);
96
97     EventQueue.invokeLater(new Runnable JavaDoc() { public void run() {
98       progressDialog.setTitle(Language.translate("Receiving mails"));
99     }});
100
101     if(accounts.getRowCount()==0)
102     {
103       JOptionPane.showMessageDialog(progressDialog,
104          Language.translate("There are no accounts to read mail from.\nDefine the account(s) in the Account Editor."),
105          Language.translate("No mail account defined"),
106          JOptionPane.INFORMATION_MESSAGE);
107
108       return;
109     }
110
111     try
112     {
113       Vector<MailAccount> accountsToRead = new Vector<MailAccount>();
114       for(int i=0; i<accounts.getRowCount(); i++)
115       {
116          if(progressDialog.wasCancelled()) break;
117
118          MailAccount ma = accounts.getAccount(i);
119          if(ma.getUseToReadMails())
120          {
121             accountsToRead.add(ma);
122          }
123       }
124
125       if(accountsToRead.size()==0)
126       {
127          JOptionPane.showMessageDialog(progressDialog,
128            Language.translate("There are no accounts to read mail from.\nActivate the checkbox read in the accounts editor."),
129            Language.translate("No accounts to check"), JOptionPane.INFORMATION_MESSAGE);
130
131          return;
132       }
133
134       int numberRead = 0;
135       for(MailAccount ma: accountsToRead)
136       {
137          if(progressDialog.wasCancelled()) break;
138          numberRead += readMails(ma, progressDialog);
139       }
140
141 /* SnowMailClientApp.getInstance().getGlobalConsole().appendLine(
142            "You have "+numberRead+" new mail"
143            +(numberRead==1?"":"s"));*/

144     }
145     catch(Exception JavaDoc ex)
146     {
147       ex.printStackTrace();
148     }
149   }
150
151
152   /** @return the number of mails read
153   */

154   private int readMails(MailAccount ma, final ProgressModalDialog progressDialog)
155   {
156     progressDialog.setMessageProgressValue(0);
157
158     int numberRead = 0;
159     SecurePopConnection sp = null;
160     try
161     {
162        sp = ma.getCheckedPopConnection();
163        int[] nm = sp.getNumberOfMessages();
164        int numberOfMessages = nm[0];
165
166        progressDialog.setProgressBounds(numberOfMessages);
167        progressDialog.setProgressValue(0, "");
168        progressDialog.setCommentLabel(Language.translate("Account : %",ma.getAddress()));
169
170        String JavaDoc[] uidls = sp.getMessagesUIDLs();
171        int[] messSizes = sp.getSizes_from_MessagesLIST(sp.getMessagesLIST_());
172
173     ml:for(int i=0; i<messSizes.length; i++)
174        {
175           if(progressDialog.wasCancelled()) break;
176
177           // test & ask for big messages
178
if(messSizes[i] > 100000) // 100k
179
{
180              // get header...
181
Header header = new Header();
182              try
183              {
184                String JavaDoc str = sp.getMessageTop(uidls[i], 0);
185                Header.parseHeader(new NumberedLineReader(str), header);
186              }
187              catch(Exception JavaDoc ignored)
188              {
189                // not so important...
190
}
191
192              String JavaDoc from = header.getEntryValue("from", "?");
193              String JavaDoc to = header.getEntryValue("to", "?");
194              String JavaDoc subject = header.getEntryValue("subject", "?");
195
196              int rep = JOptionPane.showConfirmDialog(progressDialog,
197                 Language.translate("The message")
198                 +"\n"+Language.translate(" from: %",from)
199                 +"\n"+Language.translate(" to: %",to)
200                 +"\n"+Language.translate(" subject: %",subject)
201                 +"\n"+Language.translate("has a size of %.\nDo you want to download it ?", MailMessageUtils.formatSize(messSizes[i])),
202                 Language.translate("Big message warning"),
203                 JOptionPane.YES_NO_OPTION);
204
205              if(!(rep==JOptionPane.OK_OPTION))
206              {
207                continue ml; // go to the next message
208
}
209           }
210
211           progressDialog.setProgressValue(i, Language.translate("message %1 / %2", ""+(i+1), ""+numberOfMessages));
212           progressDialog.setMessageProgressActive(messSizes[i]);
213
214           try
215           {
216             String JavaDoc messContent = sp.getMessage(uidls[i], progressDialog.interrupter, null);
217             final MailMessage mess = new MailMessage();
218             mess.parse(messContent);
219
220             try
221             {
222               double p = SnowMailClientApp.getInstance().getWordStatistic().calculateSpamProbability(mess).getProbability();
223               mess.setSPAMProbability(p);
224             }
225             catch(Exception JavaDoc es)
226             {
227               es.printStackTrace();
228             }
229
230             mess.setHasBeenReceived(true);
231             mess.setEditable(false);
232
233
234             EventQueue.invokeLater(new Runnable JavaDoc()
235             {
236               public void run()
237               {
238                 inboxFolder.addMessage(mess);
239               }
240             });
241
242             numberRead++;
243
244             // increment the number of mails received from IF the address is in the book
245
SnowMailClientApp.getInstance().getAddressBook().incrementMailsReceivedFromIfPresent( mess.getFromAddress() );
246             SnowMailClientApp.getInstance().getSpamBook().incrementMailsReceivedFromIfPresent( mess.getFromAddress() );
247
248             // read was successful => delete from server
249
sp.deleteMessage(uidls[i]);
250           }
251           catch(Exception JavaDoc ex)
252           {
253             ex.printStackTrace();
254           }
255        }
256
257     }
258     catch(Exception JavaDoc e)
259     {
260 /* SnowMailClientApp.getInstance().getGlobalConsole().appendSmallLine(
261         "Cannot receive mail: "+e.getMessage());*/

262
263       JOptionPane.showMessageDialog(ref.getContentPane(),
264         ""+e.getMessage(),
265         Language.translate("Cannot receive mail"), JOptionPane.ERROR_MESSAGE);
266       //e.printStackTrace();
267
}
268     finally
269     {
270       if(sp!=null)
271       {
272         try{
273           sp.terminateSession();
274         } catch(Exception JavaDoc bof) {}
275       }
276     }
277
278     return numberRead;
279   }
280
281
282 } // ReceiveAction
Popular Tags