KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Aliases


1 /*
2  * Aliases.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: Aliases.java,v 1.3 2003/06/29 00:19:33 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.Properties JavaDoc;
28 import org.armedbear.j.mail.ImapMailbox;
29 import org.armedbear.j.mail.LocalMailbox;
30 import org.armedbear.j.mail.PopMailbox;
31
32 public final class Aliases implements PreferencesChangeListener
33 {
34     private static Properties JavaDoc systemAliases;
35
36     private final File file;
37
38     private Properties JavaDoc userAliases;
39
40     public Aliases()
41     {
42         file = File.getInstance(Directories.getEditorDirectory(), "aliases");
43         // Set up system aliases.
44
if (systemAliases == null) {
45             systemAliases = new Properties JavaDoc();
46             systemAliases.setProperty("prefs",
47                 Preferences.getPreferencesFile().netPath());
48             systemAliases.setProperty("aliases", file.netPath());
49             String JavaDoc inbox =
50                 Editor.preferences().getStringProperty(Property.INBOX);
51             if (inbox != null)
52                 systemAliases.setProperty("inbox", inbox);
53             systemAliases.setProperty("drafts",
54                 "mailbox:".concat(Directories.getDraftsFolder().netPath()));
55         }
56         // Sign up to be notified when preferences change so we can update the
57
// "inbox" system alias.
58
Editor.preferences().addPreferencesChangeListener(this);
59         // Load user aliases from file.
60
loadUserAliases();
61     }
62
63     public final void reload()
64     {
65         loadUserAliases();
66     }
67
68     public final File getFile()
69     {
70         return file;
71     }
72
73     public static final boolean isSystemAlias(String JavaDoc alias)
74     {
75         return systemAliases.containsKey(alias);
76     }
77
78     public final String JavaDoc get(String JavaDoc alias)
79     {
80         // Look for system alias first (system aliases cannot be overridden).
81
String JavaDoc value = systemAliases.getProperty(alias);
82         if (value != null)
83             return value;
84         // Not a system alias.
85
return userAliases.getProperty(alias);
86     }
87
88     public final void setAlias(String JavaDoc alias, String JavaDoc value)
89     {
90         // Ignore attempt to set system alias.
91
if (isSystemAlias(alias))
92             return;
93         userAliases.setProperty(alias, value);
94         save();
95     }
96
97     // "alias foo here"
98
public void setAliasForBuffer(String JavaDoc alias, Buffer buffer)
99     {
100         // Ignore attempt to set system alias.
101
if (isSystemAlias(alias))
102             return;
103         String JavaDoc value = null;
104         if (buffer instanceof ImapMailbox)
105             value = ((ImapMailbox) buffer).getUrl().toString();
106         else if (buffer instanceof PopMailbox)
107             value = ((PopMailbox) buffer).getUrl().toString();
108         else if (buffer instanceof LocalMailbox)
109             value = "mailbox:" + ((LocalMailbox) buffer).getMailboxFile().netPath();
110         else if (buffer.getFile() != null)
111             value = buffer.getFile().netPath();
112         if (value != null)
113             setAlias(alias, value);
114     }
115
116     public final void remove(String JavaDoc alias)
117     {
118         if (userAliases.remove(alias) != null)
119             save();
120     }
121
122     private void loadUserAliases()
123     {
124         userAliases = new Properties JavaDoc();
125         if (file.isFile()) {
126             try {
127                 InputStream JavaDoc inputStream = file.getInputStream();
128                 userAliases.load(inputStream);
129                 inputStream.close();
130             }
131             catch (IOException JavaDoc e) {
132                 Log.error(e);
133             }
134         }
135     }
136
137     private void save()
138     {
139         try {
140             OutputStream JavaDoc outputStream = file.getOutputStream();
141             userAliases.store(outputStream, null);
142             outputStream.close();
143         }
144         catch (IOException JavaDoc e) {
145             Log.error(e);
146         }
147     }
148
149     public void preferencesChanged()
150     {
151         String JavaDoc inbox = Editor.preferences().getStringProperty(Property.INBOX);
152         if (inbox != null)
153             systemAliases.setProperty("inbox", inbox);
154         else
155             systemAliases.remove("inbox");
156     }
157 }
158
Popular Tags