001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.mail.util.multiaccount;
022:
023: import com.liferay.portal.kernel.util.StringPool;
024:
025: import java.io.Serializable;
026:
027: import javax.mail.Folder;
028: import javax.mail.Message;
029: import javax.mail.MessagingException;
030: import javax.mail.Store;
031:
032: import org.apache.commons.lang.builder.ToStringBuilder;
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: /**
037: * <a href="MailAccount.java.html"><b><i>View Source</i></b></a>
038: *
039: * <p>
040: * MailAccount represents a mail account. It contains all the necessary
041: * information to connect to the IMAP server with its credentials.
042: * </p>
043: *
044: * @author Jorge Ferrer
045: *
046: */
047: public class MailAccount implements Serializable {
048:
049: public MailAccount(String accountName, long userId,
050: String password, String emailAddress) {
051:
052: this (accountName, String.valueOf(userId), password,
053: emailAddress);
054: }
055:
056: public MailAccount(String accountName, String userId,
057: String password, String emailAddress) {
058:
059: _name = accountName;
060: _userId = userId;
061: _password = password;
062: _emailAddress = emailAddress;
063: }
064:
065: public String getName() {
066: return _name;
067: }
068:
069: public String getUserId() {
070: return _userId;
071: }
072:
073: public String getPassword() {
074: return _password;
075: }
076:
077: public String getEmailAddress() {
078: return _emailAddress;
079: }
080:
081: public String getRole() {
082: return _role;
083: }
084:
085: public void setRole(String role) {
086: _role = role;
087: }
088:
089: public Store getStore() {
090: return _store;
091: }
092:
093: public void setStore(Store store) {
094: _store = store;
095: _size = calculateStoreSize(store);
096: }
097:
098: public boolean isActive() {
099: return _active;
100: }
101:
102: public void setActive(boolean active) {
103: _active = active;
104: }
105:
106: public long getQuota() {
107: return _quota;
108: }
109:
110: public void setQuota(long quota) {
111: _quota = quota;
112: }
113:
114: public long getQuotaInMb() {
115: return Math.round((float) getQuota() / _MB);
116: }
117:
118: public long getSize() {
119: return _size;
120: }
121:
122: public long getSizeInMb() {
123: return _size / _MB;
124: }
125:
126: public long getFreeSpace() {
127: return Math.max(0, getQuota() - getSize());
128: }
129:
130: public long getFreeSpaceInMb() {
131: return Math.round((float) getFreeSpace() / _MB);
132: }
133:
134: public long getFreeSpaceInPercentage() {
135: return Math.round(((float) getSize() / getQuota()) * 100);
136: }
137:
138: private static long calculateStoreSize(Store store) {
139: return 1;
140:
141: /*try {
142: return calculateFolderSize(store.getDefaultFolder());
143: }
144: catch (MessagingException me) {
145: _log.warn("Error calculating store size", me);
146: }*/
147: }
148:
149: private static long calculateFolderSize(Folder folder)
150: throws MessagingException {
151:
152: long size = 0;
153:
154: if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
155: folder.open(Folder.READ_ONLY);
156:
157: try {
158: Message[] folderMsgs = folder.getMessages();
159:
160: for (int i = 0; i < folderMsgs.length; i++) {
161: int msgSize = folderMsgs[i].getSize();
162:
163: if (msgSize != -1) {
164: size += msgSize;
165: } else {
166: if (_log.isWarnEnabled()) {
167: _log
168: .warn("Could not determine the size of message "
169: + folderMsgs[i]
170: .getSubject());
171: }
172: }
173: }
174: } catch (Exception e) {
175: if (_log.isWarnEnabled()) {
176: _log.warn(
177: "Error calculating the size of the folder "
178: + folder.getName(), e);
179: }
180: } finally {
181: folder.close(false);
182: }
183: }
184:
185: if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
186: Folder[] folderChildren = folder.list();
187:
188: for (int i = 0; i < folderChildren.length; i++) {
189: size += calculateFolderSize(folderChildren[i]);
190: }
191: }
192:
193: return size;
194: }
195:
196: public String toString() {
197: return ToStringBuilder.reflectionToString(this );
198: }
199:
200: public static final long _MB = 1024 * 1024;
201:
202: private static Log _log = LogFactory.getLog(MailAccount.class);
203:
204: private String _name;
205: private String _userId;
206: private String _password;
207: private String _emailAddress;
208: private String _role = StringPool.BLANK;
209: private Store _store;
210: private boolean _active = true;
211: private long _quota = 0;
212: private long _size = 0;
213:
214: }
|