try to close all streams as soon as possible to prevent leaks (inserting large sites...
[jSite.git] / src / de / todesbaum / jsite / main / Main.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package de.todesbaum.jsite.main;
21
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.io.IOException;
25 import java.text.MessageFormat;
26 import java.util.HashMap;
27 import java.util.Locale;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.Map.Entry;
31
32 import javax.swing.AbstractAction;
33 import javax.swing.Action;
34 import javax.swing.ButtonGroup;
35 import javax.swing.Icon;
36 import javax.swing.JList;
37 import javax.swing.JMenu;
38 import javax.swing.JMenuBar;
39 import javax.swing.JOptionPane;
40 import javax.swing.JPanel;
41 import javax.swing.JRadioButtonMenuItem;
42 import javax.swing.event.ListSelectionEvent;
43 import javax.swing.event.ListSelectionListener;
44
45 import de.todesbaum.jsite.application.FileOption;
46 import de.todesbaum.jsite.application.Freenet7Interface;
47 import de.todesbaum.jsite.application.Node;
48 import de.todesbaum.jsite.application.Project;
49 import de.todesbaum.jsite.gui.NodeManagerListener;
50 import de.todesbaum.jsite.gui.NodeManagerPage;
51 import de.todesbaum.jsite.gui.ProjectFilesPage;
52 import de.todesbaum.jsite.gui.ProjectInsertPage;
53 import de.todesbaum.jsite.gui.ProjectPage;
54 import de.todesbaum.jsite.i18n.I18n;
55 import de.todesbaum.util.image.IconLoader;
56 import de.todesbaum.util.swing.TWizard;
57 import de.todesbaum.util.swing.TWizardPage;
58 import de.todesbaum.util.swing.WizardListener;
59
60 /**
61  * @author <a href="mailto:droden@gmail.com">David Roden </a>
62  * @version $Id$
63  */
64 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
65
66         private static boolean debug = false;
67         private Configuration configuration;
68         private Freenet7Interface freenetInterface = new Freenet7Interface();
69         protected Icon jSiteIcon;
70
71         private static enum PageType {
72                 PAGE_NODE_MANAGER, PAGE_PROJECTS, PAGE_PROJECT_FILES, PAGE_INSERT_PROJECT
73         }
74
75         private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH };
76         private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
77         private Action manageNodeAction;
78         private Action aboutAction;
79         protected TWizard wizard;
80         protected JMenu nodeMenu;
81         private Node selectedNode;
82         private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
83
84         private Main() {
85                 this(null);
86         }
87         
88         private Main(String configFilename) {
89                 if (configFilename != null) {
90                         configuration = new Configuration(configFilename);
91                 } else {
92                         configuration = new Configuration();
93                 }
94                 Locale.setDefault(configuration.getLocale());
95                 I18n.setLocale(configuration.getLocale());
96                 if (!configuration.createLockFile()) {
97                         JOptionPane.showMessageDialog(null, I18n.getMessage("jsite.main.already-running"), null, JOptionPane.ERROR_MESSAGE);
98                         return;
99                 }
100                 wizard = new TWizard();
101                 createActions();
102                 wizard.setJMenuBar(createMenuBar());
103                 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
104                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
105                 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
106                 wizard.setPreviousEnabled(false);
107                 wizard.setNextEnabled(true);
108                 wizard.addWizardListener(this);
109                 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
110                 wizard.setIcon(jSiteIcon);
111
112                 initPages();
113                 showPage(PageType.PAGE_PROJECTS);
114                 wizard.setPreviousName((String) manageNodeAction.getValue(Action.NAME));
115         }
116
117         private void createActions() {
118                 for (final Locale locale: SUPPORTED_LOCALES) {
119                         languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage())) {
120
121                                 public void actionPerformed(ActionEvent actionEvent) {
122                                         switchLanguage(locale);
123                                 }
124                         });
125                 }
126                 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
127                         public void actionPerformed(ActionEvent actionEvent) {
128                                 showPage(PageType.PAGE_NODE_MANAGER);
129                         }
130                 };
131                 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
132
133                         public void actionPerformed(ActionEvent e) {
134                                 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), Version.getVersion()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
135                         }
136                 };
137         }
138
139         private JMenuBar createMenuBar() {
140                 JMenuBar menuBar = new JMenuBar();
141                 JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
142                 menuBar.add(languageMenu);
143                 ButtonGroup languageButtonGroup = new ButtonGroup();
144                 for (Locale locale: SUPPORTED_LOCALES) {
145                         Action languageAction = languageActions.get(locale);
146                         JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
147                         if (locale.equals(Locale.getDefault())) {
148                                 menuItem.setSelected(true);
149                         }
150                         languageAction.putValue("menuItem", menuItem);
151                         languageButtonGroup.add(menuItem);
152                         languageMenu.add(menuItem);
153                 }
154                 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
155                 menuBar.add(nodeMenu);
156                 selectedNode = configuration.getSelectedNode();
157                 nodesUpdated(configuration.getNodes());
158
159                 /* evil hack to right-align the help menu */
160                 JPanel panel = new JPanel();
161                 panel.setOpaque(false);
162                 menuBar.add(panel);
163
164                 JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
165                 menuBar.add(helpMenu);
166                 helpMenu.add(aboutAction);
167                 return menuBar;
168         }
169
170         private void initPages() {
171                 NodeManagerPage nodeManagerPage = new NodeManagerPage();
172                 nodeManagerPage.setName("page.node-manager");
173                 nodeManagerPage.addNodeManagerListener(this);
174                 nodeManagerPage.setNodes(configuration.getNodes());
175                 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
176
177                 ProjectPage projectPage = new ProjectPage();
178                 projectPage.setName("page.project");
179                 projectPage.setProjects(configuration.getProjects());
180                 projectPage.setFreenetInterface(freenetInterface);
181                 projectPage.addListSelectionListener(this);
182                 pages.put(PageType.PAGE_PROJECTS, projectPage);
183
184                 ProjectFilesPage projectFilesPage = new ProjectFilesPage();
185                 projectFilesPage.setName("page.project.files");
186                 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
187
188                 ProjectInsertPage projectInsertPage = new ProjectInsertPage();
189                 projectInsertPage.setDebug(debug);
190                 projectInsertPage.setName("page.project.insert");
191                 projectInsertPage.setFreenetInterface(freenetInterface);
192                 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
193         }
194
195         protected void showPage(PageType pageType) {
196                 wizard.setPreviousEnabled(pageType.ordinal() > 0);
197                 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
198                 wizard.setPage(pages.get(pageType));
199                 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
200         }
201
202         private boolean saveConfiguration() {
203                 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
204                 configuration.setNodes(nodeManagerPage.getNodes());
205                 if (selectedNode != null) {
206                         configuration.setSelectedNode(selectedNode);
207                 }
208
209                 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
210                 configuration.setProjects(projectPage.getProjects());
211
212                 return configuration.save();
213         }
214
215         private Locale findSupportedLocale(Locale forLocale) {
216                 for (Locale locale: SUPPORTED_LOCALES) {
217                         if (locale.equals(forLocale)) {
218                                 return locale;
219                         }
220                 }
221                 for (Locale locale: SUPPORTED_LOCALES) {
222                         if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
223                                 return locale;
224                         }
225                 }
226                 for (Locale locale: SUPPORTED_LOCALES) {
227                         if (locale.getLanguage().equals(forLocale.getLanguage())) {
228                                 return locale;
229                         }
230                 }
231                 return SUPPORTED_LOCALES[0];
232         }
233
234         //
235         // ACTIONS
236         //
237
238         protected void switchLanguage(Locale locale) {
239                 Locale supportedLocale = findSupportedLocale(locale);
240                 Action languageAction = languageActions.get(supportedLocale);
241                 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
242                 menuItem.setSelected(true);
243                 /* show the restart message in the other language! */
244                 Locale currentLocale = I18n.getLocale();
245                 I18n.setLocale(supportedLocale);
246                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.menu.language.change.restart-message"), null, JOptionPane.INFORMATION_MESSAGE);
247                 I18n.setLocale(currentLocale);
248                 configuration.setLocale(supportedLocale);
249         }
250
251         //
252         // INTERFACE ListSelectionListener
253         //
254
255         /**
256          * {@inheritDoc}
257          */
258         public void valueChanged(ListSelectionEvent e) {
259                 JList list = (JList) e.getSource();
260                 int selectedRow = list.getSelectedIndex();
261                 wizard.setNextEnabled(selectedRow > -1);
262         }
263
264         //
265         // INTERFACE WizardListener
266         //
267
268         /**
269          * {@inheritDoc}
270          */
271         public void wizardNextPressed(TWizard wizard) {
272                 String pageName = wizard.getPage().getName();
273                 if ("page.node-manager".equals(pageName)) {
274                         showPage(PageType.PAGE_PROJECTS);
275                         wizard.setPreviousName((String) manageNodeAction.getValue(Action.NAME));
276                 } else if ("page.project".equals(pageName)) {
277                         ProjectPage projectPage = (ProjectPage) wizard.getPage();
278                         Project project = projectPage.getSelectedProject();
279                         if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
280                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
281                                 return;
282                         }
283                         if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
284                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
285                                 return;
286                         }
287                         ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
288                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
289                         showPage(PageType.PAGE_PROJECT_FILES);
290                         wizard.setNextName(I18n.getMessage("jsite.project-files.insert-now"));
291                         wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
292                 } else if ("page.project.files".equals(pageName)) {
293                         ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
294                         Project project = projectPage.getSelectedProject();
295                         if (selectedNode == null) {
296                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
297                                 return;
298                         }
299                         if (project.getIndexFile() == null) {
300                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
301                                         return;
302                                 }
303                         }
304                         if (!project.getFileOption(project.getIndexFile()).getContainer().equals("")) {
305                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
306                                         return;
307                                 }
308                         }
309                         if (!project.getFileOption(project.getIndexFile()).getMimeType().equals("text/html")) {
310                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
311                                         return;
312                                 }
313                         }
314                         Map<String, FileOption> fileOptions = project.getFileOptions();
315                         Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
316                         for (Entry<String, FileOption> fileOptionEntry: fileOptionEntries) {
317                                 FileOption fileOption = fileOptionEntry.getValue();
318                                 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
319                                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
320                                         return;
321                                 }
322                         }
323                         boolean nodeRunning = false;
324                         try {
325                                 nodeRunning = freenetInterface.isNodePresent();
326                         } catch (IOException e) {
327                         }
328                         if (!nodeRunning) {
329                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
330                                 return;
331                         }
332                         configuration.save();
333                         wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
334                         showPage(PageType.PAGE_INSERT_PROJECT);
335                         nodeMenu.setEnabled(false);
336                 } else if ("page.project.insert".equals(pageName)) {
337                         showPage(PageType.PAGE_PROJECTS);
338                         nodeMenu.setEnabled(true);
339                 }
340         }
341
342         /**
343          * {@inheritDoc}
344          */
345         public void wizardPreviousPressed(TWizard wizard) {
346                 String pageName = wizard.getPage().getName();
347                 if ("page.project".equals(pageName)) {
348                         showPage(PageType.PAGE_NODE_MANAGER);
349                         wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
350                 } else if ("page.project.files".equals(pageName)) {
351                         showPage(PageType.PAGE_PROJECTS);
352                         wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
353                         wizard.setPreviousName((String) manageNodeAction.getValue(Action.NAME));
354                 } else if ("page.project.insert".equals(pageName)) {
355                         showPage(PageType.PAGE_PROJECT_FILES);
356                 }
357         }
358
359         /**
360          * {@inheritDoc}
361          */
362         public void wizardQuitPressed(TWizard wizard) {
363                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
364                         if (saveConfiguration()) {
365                                 System.exit(0);
366                         }
367                         if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
368                                 System.exit(0);
369                         }
370                 }
371         }
372
373         //
374         // INTERFACE NodeManagerListener
375         //
376
377         /**
378          * {@inheritDoc}
379          */
380         public void nodesUpdated(Node[] nodes) {
381                 nodeMenu.removeAll();
382                 ButtonGroup nodeButtonGroup = new ButtonGroup();
383                 Node newSelectedNode = null;
384                 for (Node node: nodes) {
385                         JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
386                         nodeMenuItem.putClientProperty("Node", node);
387                         nodeMenuItem.addActionListener(this);
388                         nodeButtonGroup.add(nodeMenuItem);
389                         if (node.equals(selectedNode)) {
390                                 newSelectedNode = node;
391                                 nodeMenuItem.setSelected(true);
392                         }
393                         nodeMenu.add(nodeMenuItem);
394                 }
395                 nodeMenu.addSeparator();
396                 nodeMenu.add(manageNodeAction);
397                 selectedNode = newSelectedNode;
398                 freenetInterface.setNode(selectedNode);
399         }
400
401         /**
402          * {@inheritDoc}
403          */
404         public void actionPerformed(ActionEvent e) {
405                 Object source = e.getSource();
406                 if (source instanceof JRadioButtonMenuItem) {
407                         JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
408                         Node node = (Node) menuItem.getClientProperty("Node");
409                         selectedNode = node;
410                         freenetInterface.setNode(selectedNode);
411                 }
412         }
413
414         //
415         // MAIN METHOD
416         //
417         public static void main(String[] args) {
418                 String configFilename = null;
419                 boolean nextIsConfigFilename = false;
420                 for (String argument: args) {
421                         if (nextIsConfigFilename) {
422                                 configFilename = argument;
423                                 nextIsConfigFilename = false;
424                         }
425                         if ("--help".equals(argument)) {
426                                 printHelp();
427                                 return;
428                         } else if ("--debug".equals(argument)) {
429                                 debug = true;
430                         } else if ("--config-file".equals(argument)) {
431                                 nextIsConfigFilename = true;
432                         }
433                 }
434                 if (nextIsConfigFilename) {
435                         System.out.println("--config-file needs parameter!");
436                         return;
437                 }
438                 new Main(configFilename);
439         }
440
441         private static void printHelp() {
442                 System.out.println("--help\tshows this cruft");
443                 System.out.println("--debug\tenables some debug output");
444                 System.out.println("--config-file <file>\tuse specified configuration file");
445         }
446         
447 }