001: /*
002: * Aliases.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: Aliases.java,v 1.3 2003/06/29 00:19:33 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.OutputStream;
027: import java.util.Properties;
028: import org.armedbear.j.mail.ImapMailbox;
029: import org.armedbear.j.mail.LocalMailbox;
030: import org.armedbear.j.mail.PopMailbox;
031:
032: public final class Aliases implements PreferencesChangeListener {
033: private static Properties systemAliases;
034:
035: private final File file;
036:
037: private Properties userAliases;
038:
039: public Aliases() {
040: file = File.getInstance(Directories.getEditorDirectory(),
041: "aliases");
042: // Set up system aliases.
043: if (systemAliases == null) {
044: systemAliases = new Properties();
045: systemAliases.setProperty("prefs", Preferences
046: .getPreferencesFile().netPath());
047: systemAliases.setProperty("aliases", file.netPath());
048: String inbox = Editor.preferences().getStringProperty(
049: Property.INBOX);
050: if (inbox != null)
051: systemAliases.setProperty("inbox", inbox);
052: systemAliases.setProperty("drafts", "mailbox:"
053: .concat(Directories.getDraftsFolder().netPath()));
054: }
055: // Sign up to be notified when preferences change so we can update the
056: // "inbox" system alias.
057: Editor.preferences().addPreferencesChangeListener(this );
058: // Load user aliases from file.
059: loadUserAliases();
060: }
061:
062: public final void reload() {
063: loadUserAliases();
064: }
065:
066: public final File getFile() {
067: return file;
068: }
069:
070: public static final boolean isSystemAlias(String alias) {
071: return systemAliases.containsKey(alias);
072: }
073:
074: public final String get(String alias) {
075: // Look for system alias first (system aliases cannot be overridden).
076: String value = systemAliases.getProperty(alias);
077: if (value != null)
078: return value;
079: // Not a system alias.
080: return userAliases.getProperty(alias);
081: }
082:
083: public final void setAlias(String alias, String value) {
084: // Ignore attempt to set system alias.
085: if (isSystemAlias(alias))
086: return;
087: userAliases.setProperty(alias, value);
088: save();
089: }
090:
091: // "alias foo here"
092: public void setAliasForBuffer(String alias, Buffer buffer) {
093: // Ignore attempt to set system alias.
094: if (isSystemAlias(alias))
095: return;
096: String value = null;
097: if (buffer instanceof ImapMailbox)
098: value = ((ImapMailbox) buffer).getUrl().toString();
099: else if (buffer instanceof PopMailbox)
100: value = ((PopMailbox) buffer).getUrl().toString();
101: else if (buffer instanceof LocalMailbox)
102: value = "mailbox:"
103: + ((LocalMailbox) buffer).getMailboxFile()
104: .netPath();
105: else if (buffer.getFile() != null)
106: value = buffer.getFile().netPath();
107: if (value != null)
108: setAlias(alias, value);
109: }
110:
111: public final void remove(String alias) {
112: if (userAliases.remove(alias) != null)
113: save();
114: }
115:
116: private void loadUserAliases() {
117: userAliases = new Properties();
118: if (file.isFile()) {
119: try {
120: InputStream inputStream = file.getInputStream();
121: userAliases.load(inputStream);
122: inputStream.close();
123: } catch (IOException e) {
124: Log.error(e);
125: }
126: }
127: }
128:
129: private void save() {
130: try {
131: OutputStream outputStream = file.getOutputStream();
132: userAliases.store(outputStream, null);
133: outputStream.close();
134: } catch (IOException e) {
135: Log.error(e);
136: }
137: }
138:
139: public void preferencesChanged() {
140: String inbox = Editor.preferences().getStringProperty(
141: Property.INBOX);
142: if (inbox != null)
143: systemAliases.setProperty("inbox", inbox);
144: else
145: systemAliases.remove("inbox");
146: }
147: }
|