1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.mail.util.multiaccount;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  
27  import java.io.Serializable;
28  
29  import javax.mail.Folder;
30  import javax.mail.Message;
31  import javax.mail.MessagingException;
32  import javax.mail.Store;
33  
34  import org.apache.commons.lang.builder.ToStringBuilder;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  /**
39   * <a href="MailAccount.java.html"><b><i>View Source</i></b></a>
40   *
41   * <p>
42   * MailAccount represents a mail account. It contains all the necessary
43   * information to connect to the IMAP server with its credentials.
44   * </p>
45   *
46   * @author Jorge Ferrer
47   *
48   */
49  public class MailAccount implements Serializable {
50  
51      public MailAccount(
52          String accountName, long userId, String password, String emailAddress) {
53  
54          this(accountName, String.valueOf(userId), password, emailAddress);
55      }
56  
57      public MailAccount(
58          String accountName, String userId, String password,
59          String emailAddress) {
60  
61          _name = accountName;
62          _userId = userId;
63          _password = password;
64          _emailAddress = emailAddress;
65      }
66  
67      public String getName() {
68          return _name;
69      }
70  
71      public String getUserId() {
72          return _userId;
73      }
74  
75      public String getPassword() {
76          return _password;
77      }
78  
79      public String getEmailAddress() {
80          return _emailAddress;
81      }
82  
83      public String getRole() {
84          return _role;
85      }
86  
87      public void setRole(String role) {
88          _role = role;
89      }
90  
91      public Store getStore() {
92          return _store;
93      }
94  
95      public void setStore(Store store) {
96          _store = store;
97          _size = calculateStoreSize(store);
98      }
99  
100     public boolean isActive() {
101         return _active;
102     }
103 
104     public void setActive(boolean active) {
105         _active = active;
106     }
107 
108     public long getQuota() {
109         return _quota;
110     }
111 
112     public void setQuota(long quota) {
113         _quota = quota;
114     }
115 
116     public long getQuotaInMb() {
117         return Math.round((float)getQuota() / _MB);
118     }
119 
120     public long getSize() {
121         return _size;
122     }
123 
124     public long getSizeInMb() {
125         return _size / _MB;
126     }
127 
128     public long getFreeSpace() {
129         return Math.max(0, getQuota() - getSize());
130     }
131 
132     public long getFreeSpaceInMb() {
133         return Math.round((float)getFreeSpace() / _MB);
134     }
135 
136     public long getFreeSpaceInPercentage() {
137         return Math.round(((float)getSize() / getQuota()) * 100);
138     }
139 
140     private static long calculateStoreSize(Store store) {
141         return 1;
142 
143         /*try {
144             return calculateFolderSize(store.getDefaultFolder());
145         }
146         catch (MessagingException me) {
147             _log.warn("Error calculating store size", me);
148         }*/
149     }
150 
151     private static long calculateFolderSize(Folder folder)
152         throws MessagingException {
153 
154         long size = 0;
155 
156         if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
157             folder.open(Folder.READ_ONLY);
158 
159             try {
160                 Message[] folderMsgs = folder.getMessages();
161 
162                 for (int i = 0; i < folderMsgs.length; i++) {
163                     int msgSize = folderMsgs[i].getSize();
164 
165                     if (msgSize != -1) {
166                         size += msgSize;
167                     }
168                     else {
169                         if (_log.isWarnEnabled()) {
170                             _log.warn(
171                                 "Could not determine the size of message " +
172                                     folderMsgs[i].getSubject());
173                         }
174                     }
175                 }
176             }
177             catch (Exception e) {
178                 if (_log.isWarnEnabled()) {
179                     _log.warn(
180                         "Error calculating the size of the folder " +
181                             folder.getName(),
182                         e);
183                 }
184             }
185             finally {
186                 folder.close(false);
187             }
188         }
189 
190         if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
191             Folder[] folderChildren = folder.list();
192 
193             for (int i = 0; i < folderChildren.length; i++) {
194                 size += calculateFolderSize(folderChildren[i]);
195             }
196         }
197 
198         return size;
199     }
200 
201     public String toString() {
202         return ToStringBuilder.reflectionToString(this);
203     }
204 
205     public static final long _MB = 1024 * 1024;
206 
207     private static Log _log = LogFactory.getLog(MailAccount.class);
208 
209     private String _name;
210     private String _userId;
211     private String _password;
212     private String _emailAddress;
213     private String _role = StringPool.BLANK;
214     private Store _store;
215     private boolean _active = true;
216     private long _quota = 0;
217     private long _size = 0;
218 
219 }