1   /**
2    * Copyright (c) 2000-2007 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(String accountName, long userId, String password,
52                         String emailAddress) {
53  
54          this(accountName, String.valueOf(userId), password, emailAddress);
55      }
56  
57      public MailAccount(String accountName, String userId, String password,
58                         String emailAddress) {
59  
60          _name = accountName;
61          _userId = userId;
62          _password = password;
63          _emailAddress = emailAddress;
64      }
65  
66      public String getName() {
67          return _name;
68      }
69  
70      public String getUserId() {
71          return _userId;
72      }
73  
74      public String getPassword() {
75          return _password;
76      }
77  
78      public String getEmailAddress() {
79          return _emailAddress;
80      }
81  
82      public String getRole() {
83          return _role;
84      }
85  
86      public void setRole(String role) {
87          _role = role;
88      }
89  
90      public Store getStore() {
91          return _store;
92      }
93  
94      public void setStore(Store store) {
95          _store = store;
96          _size = calculateStoreSize(store);
97      }
98  
99      public boolean isActive() {
100         return _active;
101     }
102 
103     public void setActive(boolean active) {
104         _active = active;
105     }
106 
107     public long getQuota() {
108         return _quota;
109     }
110 
111     public void setQuota(long quota) {
112         _quota = quota;
113     }
114 
115     public long getQuotaInMb() {
116         return Math.round((float)getQuota() / _MB);
117     }
118 
119     public long getSize() {
120         return _size;
121     }
122 
123     public long getSizeInMb() {
124         return _size / _MB;
125     }
126 
127     public long getFreeSpace() {
128         return Math.max(0, getQuota() - getSize());
129     }
130 
131     public long getFreeSpaceInMb() {
132         return Math.round((float)getFreeSpace() / _MB);
133     }
134 
135     public long getFreeSpaceInPercentage() {
136         return Math.round(((float)getSize() / getQuota()) * 100);
137     }
138 
139     private static long calculateStoreSize(Store store) {
140         return 1;
141 
142         /*try {
143             return calculateFolderSize(store.getDefaultFolder());
144         }
145         catch (MessagingException me) {
146             _log.warn("Error calculating store size", me);
147         }*/
148     }
149 
150     private static long calculateFolderSize(Folder folder)
151         throws MessagingException {
152 
153         long size = 0;
154 
155         if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
156             folder.open(Folder.READ_ONLY);
157 
158             try {
159                 Message[] folderMsgs = folder.getMessages();
160 
161                 for (int i = 0; i < folderMsgs.length; i++) {
162                     int msgSize = folderMsgs[i].getSize();
163 
164                     if (msgSize != -1) {
165                         size += msgSize;
166                     }
167                     else {
168                         if (_log.isWarnEnabled()) {
169                             _log.warn(
170                                 "Could not determine the size of message " +
171                                     folderMsgs[i].getSubject());
172                         }
173                     }
174                 }
175             }
176             catch (Exception e) {
177                 if (_log.isWarnEnabled()) {
178                     _log.warn(
179                         "Error calculating the size of the folder " +
180                             folder.getName(),
181                         e);
182                 }
183             }
184             finally {
185                 folder.close(false);
186             }
187         }
188 
189         if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
190             Folder[] folderChildren = folder.list();
191 
192             for (int i = 0; i < folderChildren.length; i++) {
193                 size += calculateFolderSize(folderChildren[i]);
194             }
195         }
196 
197         return size;
198     }
199 
200     public String toString() {
201         return ToStringBuilder.reflectionToString(this);
202     }
203 
204     public static final long _MB = 1024 * 1024;
205 
206     private static Log _log = LogFactory.getLog(MailAccount.class);
207 
208     private String _name;
209     private String _userId;
210     private String _password;
211     private String _emailAddress;
212     private String _role = StringPool.BLANK;
213     private Store _store;
214     private boolean _active = true;
215     private long _quota = 0;
216     private long _size = 0;
217 
218 }