001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.main;
017:
018: import java.awt.BorderLayout;
019: import java.util.logging.Logger;
020:
021: import javax.swing.JCheckBox;
022: import javax.swing.JOptionPane;
023: import javax.swing.JPanel;
024:
025: import org.apache.commons.cli.CommandLine;
026: import org.apache.commons.cli.Option;
027: import org.apache.commons.cli.OptionBuilder;
028: import org.columa.core.config.IDefaultItem;
029: import org.columba.api.plugin.PluginLoadingFailedException;
030: import org.columba.core.backgroundtask.BackgroundTaskManager;
031: import org.columba.core.component.api.IComponentPlugin;
032: import org.columba.core.config.DefaultItem;
033: import org.columba.core.connectionstate.ConnectionStateImpl;
034: import org.columba.core.facade.ServiceFacadeRegistry;
035: import org.columba.core.gui.base.MultiLineLabel;
036: import org.columba.core.gui.frame.DefaultContainer;
037: import org.columba.core.gui.frame.FrameManager;
038: import org.columba.core.main.ColumbaCmdLineParser;
039: import org.columba.core.shutdown.ShutdownManager;
040: import org.columba.mail.config.IncomingItem;
041: import org.columba.mail.config.MailConfig;
042: import org.columba.mail.facade.DialogFacade;
043: import org.columba.mail.folder.IMailFolder;
044: import org.columba.mail.folder.virtual.ActivateVirtualFolderCommand;
045: import org.columba.mail.gui.composer.ComposerController;
046: import org.columba.mail.gui.composer.ComposerModel;
047: import org.columba.mail.gui.config.accountwizard.AccountWizardLauncher;
048: import org.columba.mail.gui.tree.FolderTreeModel;
049: import org.columba.mail.nativ.defaultmailclient.SystemDefaultMailClientHandler;
050: import org.columba.mail.parser.MailUrlParser;
051: import org.columba.mail.pgp.MultipartEncryptedRenderer;
052: import org.columba.mail.pgp.MultipartSignedRenderer;
053: import org.columba.mail.shutdown.ClearRecentFlagPlugin;
054: import org.columba.mail.shutdown.SaveAllFoldersPlugin;
055: import org.columba.mail.shutdown.SavePOP3CachePlugin;
056: import org.columba.mail.spam.SaveSpamDBPlugin;
057: import org.columba.mail.util.MailResourceLoader;
058: import org.columba.ristretto.composer.MimeTreeRenderer;
059: import org.columba.ristretto.parser.ParserException;
060:
061: /**
062: * Main entrypoint for mail component.
063: *
064: * @author fdietz
065: */
066: public class MailMain implements IComponentPlugin {
067: /** JDK 1.4+ logging framework logger, used for logging. */
068: private static final Logger LOG = Logger
069: .getLogger("org.columba.mail.main");
070:
071: public MailMain() {
072: }
073:
074: /**
075: *
076: */
077: public void init() {
078: // Init PGP
079: MimeTreeRenderer renderer = MimeTreeRenderer.getInstance();
080: renderer.addMimePartRenderer(new MultipartSignedRenderer());
081: renderer.addMimePartRenderer(new MultipartEncryptedRenderer());
082:
083: // Init Plugins
084: /*PluginManager.getInstance().addHandlers(
085: "org/columba/mail/plugin/pluginhandler.xml");
086:
087: try {
088: InputStream is = this.getClass().getResourceAsStream(
089: "/org/columba/mail/action/action.xml");
090: PluginManager.getInstance().getHandler(IExtensionHandlerKeys.ORG_COLUMBA_CORE_ACTION)
091: .loadExtensionsFromStream(is);
092: } catch (PluginHandlerNotFoundException ex) {
093: ex.printStackTrace();
094: }*/
095:
096: Runnable plugin = new SaveAllFoldersPlugin();
097: BackgroundTaskManager.getInstance().register(plugin);
098:
099: plugin = new SavePOP3CachePlugin();
100: BackgroundTaskManager.getInstance().register(plugin);
101: ShutdownManager.getInstance().register(plugin);
102:
103: plugin = new SaveSpamDBPlugin();
104: BackgroundTaskManager.getInstance().register(plugin);
105: ShutdownManager.getInstance().register(plugin);
106:
107: plugin = new ClearRecentFlagPlugin();
108: ShutdownManager.getInstance().register(plugin);
109:
110: ServiceFacadeRegistry.getInstance().register(
111: org.columba.mail.facade.IDialogFacade.class,
112: new DialogFacade());
113: }
114:
115: public void registerCommandLineArguments() {
116: ColumbaCmdLineParser parser = ColumbaCmdLineParser
117: .getInstance();
118:
119: Option composeOption = new Option("compose", true,
120: "compose message");
121: parser.addOption(composeOption);
122:
123: Option mailOption = OptionBuilder.create("mail");
124: mailOption.setOptionalArg(true);
125:
126: parser.addOption(mailOption);
127: }
128:
129: /**
130: * @see org.columba.core.component.api.IComponentPlugin#handleCommandLineParameters(java.lang.String[])
131: */
132: public void handleCommandLineParameters(CommandLine commandLine) {
133:
134: if (commandLine.hasOption("mail")) {
135: // check if the optional value exists
136: if (commandLine.getOptionValue("mail") != null) {
137: try {
138: ComposerModel model = new ComposerModel(
139: MailUrlParser.parse(commandLine
140: .getOptionValue("mail")));
141:
142: // new NewMessageAction().actionPerformed(null);
143: ComposerController controller = new ComposerController();
144: new DefaultContainer(controller);
145:
146: controller.setComposerModel(model);
147:
148: ColumbaCmdLineParser.getInstance()
149: .setRestoreLastSession(false);
150: } catch (ParserException e1) {
151: LOG.warning(e1.getLocalizedMessage());
152: }
153: } else {
154: try {
155: FrameManager.getInstance()
156: .openView("ThreePaneMail");
157:
158: ColumbaCmdLineParser.getInstance()
159: .setRestoreLastSession(false);
160: } catch (PluginLoadingFailedException e) {
161: LOG.severe(e.getLocalizedMessage());
162: }
163: }
164: }
165:
166: if (commandLine.hasOption("compose")) {
167: String option = commandLine.getOptionValue("compose");
168: ComposerModel model = new ComposerModel(MessageOptionParser
169: .parse(option));
170:
171: ComposerController controller = new ComposerController();
172: new DefaultContainer(controller);
173:
174: controller.setComposerModel(model);
175:
176: ColumbaCmdLineParser.getInstance().setRestoreLastSession(
177: false);
178: }
179: }
180:
181: /**
182: *
183: */
184: public void postStartup() {
185: // Check default mail client
186: checkDefaultClient();
187:
188: // Show first time Account Wizard
189: if (MailConfig.getInstance().getAccountList().count() == 0) {
190: new AccountWizardLauncher().launchWizard(true);
191: }
192:
193: // Check Internet Connection
194: if (MailConfig.getInstance().getAccountList().count() > 0) {
195: try {
196: IncomingItem testConnection = MailConfig.getInstance()
197: .getAccountList().getDefaultAccount()
198: .getIncomingItem();
199: ConnectionStateImpl.getInstance().setTestConnection(
200: testConnection.get("host"),
201: testConnection.getInteger("port"));
202: ConnectionStateImpl.getInstance().checkPhysicalState();
203: } catch (RuntimeException e) {
204: e.printStackTrace();
205: }
206: }
207:
208: // Activate all Virtual Folders
209: ActivateVirtualFolderCommand
210: .activateAll((IMailFolder) FolderTreeModel
211: .getInstance().getRoot());
212: }
213:
214: private void checkDefaultClient() {
215: // Check if Columba is the default mail client
216: SystemDefaultMailClientHandler defaultClientHandler = new SystemDefaultMailClientHandler();
217: IDefaultItem item = new DefaultItem(MailConfig.getInstance()
218: .get("options"));
219:
220: boolean checkDefault = item.getBooleanWithDefault(
221: "options/defaultclient", "check", true);
222:
223: if (checkDefault
224: && defaultClientHandler
225: .platfromSupportsDefaultMailClient()) {
226: if (!defaultClientHandler.isDefaultMailClient()) {
227:
228: JPanel panel = new JPanel(new BorderLayout(0, 10));
229:
230: panel.add(new MultiLineLabel(MailResourceLoader
231: .getString("dialog", "defaultclient",
232: "make_default")), BorderLayout.NORTH);
233:
234: JCheckBox askAgain = new JCheckBox(MailResourceLoader
235: .getString("dialog", "defaultclient",
236: "ask_no_more"));
237: panel.add(askAgain, BorderLayout.CENTER);
238:
239: // Some error in the client/server communication
240: // --> fall back to default login process
241:
242: // @author hubms: wait until the first frame is displayable
243: // fix for bug [#CA-177]
244: while (!(FrameManager.getInstance().getActiveFrame()
245: .isDisplayable())) {
246: try {
247: Thread.sleep(100);
248: } catch (InterruptedException e) {
249: LOG.severe(e.getMessage());
250: }
251: }
252:
253: // fix for bug [#CA-199] java 1.6.0-b105 trick
254: if (!FrameManager.getInstance().getActiveFrame()
255: .isValid())
256: FrameManager.getInstance().getActiveFrame()
257: .doLayout();
258:
259: int result = JOptionPane.showConfirmDialog(FrameManager
260: .getInstance().getActiveFrame(), panel,
261: MailResourceLoader.getString("dialog",
262: "defaultclient", "title"),
263: JOptionPane.YES_NO_OPTION);
264: if (result == JOptionPane.OK_OPTION) {
265: defaultClientHandler.setDefaultMailClient();
266: }
267:
268: item.setBoolean("options/defaultclient", "check",
269: !askAgain.isSelected());
270: }
271: }
272: }
273:
274: }
|