2f538d0804a1d477e7cc9bd588df4b52826a6d1a
[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.File;
25 import java.io.IOException;
26 import java.text.MessageFormat;
27 import java.util.HashMap;
28 import java.util.Locale;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.Map.Entry;
32
33 import javax.swing.AbstractAction;
34 import javax.swing.Action;
35 import javax.swing.ButtonGroup;
36 import javax.swing.Icon;
37 import javax.swing.JList;
38 import javax.swing.JMenu;
39 import javax.swing.JMenuBar;
40 import javax.swing.JOptionPane;
41 import javax.swing.JPanel;
42 import javax.swing.JRadioButtonMenuItem;
43 import javax.swing.event.ListSelectionEvent;
44 import javax.swing.event.ListSelectionListener;
45
46 import de.todesbaum.jsite.application.FileOption;
47 import de.todesbaum.jsite.application.Freenet7Interface;
48 import de.todesbaum.jsite.application.Node;
49 import de.todesbaum.jsite.application.Project;
50 import de.todesbaum.jsite.gui.NodeManagerListener;
51 import de.todesbaum.jsite.gui.NodeManagerPage;
52 import de.todesbaum.jsite.gui.ProjectFilesPage;
53 import de.todesbaum.jsite.gui.ProjectInsertPage;
54 import de.todesbaum.jsite.gui.ProjectPage;
55 import de.todesbaum.jsite.i18n.I18n;
56 import de.todesbaum.jsite.i18n.I18nContainer;
57 import de.todesbaum.util.image.IconLoader;
58 import de.todesbaum.util.swing.TWizard;
59 import de.todesbaum.util.swing.TWizardPage;
60 import de.todesbaum.util.swing.WizardListener;
61
62 /**
63  * The main class that ties together everything.
64  *
65  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
66  */
67 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
68
69         /** Whether the debug mode is activated. */
70         private static boolean debug = false;
71
72         /** The configuration. */
73         private Configuration configuration;
74
75         /** The freenet interface. */
76         private Freenet7Interface freenetInterface = new Freenet7Interface();
77
78         /** The jSite icon. */
79         private Icon jSiteIcon;
80
81         /**
82          * Enumeration for all possible pages.
83          *
84          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
85          */
86         private static enum PageType {
87
88                 /** The node manager page. */
89                 PAGE_NODE_MANAGER,
90
91                 /** The project page. */
92                 PAGE_PROJECTS,
93
94                 /** The project files page. */
95                 PAGE_PROJECT_FILES,
96
97                 /** The project insert page. */
98                 PAGE_INSERT_PROJECT
99
100         }
101
102         /** The supported locales. */
103         private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, Locale.ITALIAN, new Locale("pl") };
104
105         /** The actions that switch the language. */
106         private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
107
108         /** The “manage nodes” action. */
109         private Action manageNodeAction;
110
111         /** The “about jSite” action. */
112         private Action aboutAction;
113
114         /** The wizard. */
115         private TWizard wizard;
116
117         /** The node menu. */
118         private JMenu nodeMenu;
119
120         /** The currently selected node. */
121         private Node selectedNode;
122
123         /** Mapping from page type to page. */
124         private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
125
126         /**
127          * Creates a new core with the default configuration file.
128          */
129         private Main() {
130                 this(null);
131         }
132
133         /**
134          * Creates a new core with the given configuration from the given file.
135          *
136          * @param configFilename
137          *            The name of the configuration file
138          */
139         private Main(String configFilename) {
140                 if (configFilename != null) {
141                         configuration = new Configuration(configFilename);
142                 } else {
143                         configuration = new Configuration();
144                 }
145                 Locale.setDefault(configuration.getLocale());
146                 I18n.setLocale(configuration.getLocale());
147                 if (!configuration.createLockFile()) {
148                         JOptionPane.showMessageDialog(null, I18n.getMessage("jsite.main.already-running"), null, JOptionPane.ERROR_MESSAGE);
149                         return;
150                 }
151                 wizard = new TWizard();
152                 createActions();
153                 wizard.setJMenuBar(createMenuBar());
154                 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
155                 wizard.setPreviousEnabled(false);
156                 wizard.setNextEnabled(true);
157                 wizard.addWizardListener(this);
158                 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
159                 wizard.setIcon(jSiteIcon);
160
161                 initPages();
162                 showPage(PageType.PAGE_PROJECTS);
163         }
164
165         /**
166          * Creates all actions.
167          */
168         private void createActions() {
169                 for (final Locale locale : SUPPORTED_LOCALES) {
170                         languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
171
172                                 @SuppressWarnings("synthetic-access")
173                                 public void actionPerformed(ActionEvent actionEvent) {
174                                         switchLanguage(locale);
175                                 }
176                         });
177                 }
178                 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
179
180                         @SuppressWarnings("synthetic-access")
181                         public void actionPerformed(ActionEvent actionEvent) {
182                                 showPage(PageType.PAGE_NODE_MANAGER);
183                                 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
184                                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
185                         }
186                 };
187                 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
188
189                         @SuppressWarnings("synthetic-access")
190                         public void actionPerformed(ActionEvent e) {
191                                 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), Version.getVersion()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
192                         }
193                 };
194
195                 I18nContainer.getInstance().registerRunnable(new Runnable() {
196
197                         @SuppressWarnings("synthetic-access")
198                         public void run() {
199                                 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
200                                 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
201                         }
202                 });
203         }
204
205         /**
206          * Creates the menu bar.
207          *
208          * @return The menu bar
209          */
210         private JMenuBar createMenuBar() {
211                 JMenuBar menuBar = new JMenuBar();
212                 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
213                 menuBar.add(languageMenu);
214                 ButtonGroup languageButtonGroup = new ButtonGroup();
215                 for (Locale locale : SUPPORTED_LOCALES) {
216                         Action languageAction = languageActions.get(locale);
217                         JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
218                         if (locale.equals(Locale.getDefault())) {
219                                 menuItem.setSelected(true);
220                         }
221                         languageAction.putValue("menuItem", menuItem);
222                         languageButtonGroup.add(menuItem);
223                         languageMenu.add(menuItem);
224                 }
225                 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
226                 menuBar.add(nodeMenu);
227                 selectedNode = configuration.getSelectedNode();
228                 nodesUpdated(configuration.getNodes());
229
230                 /* evil hack to right-align the help menu */
231                 JPanel panel = new JPanel();
232                 panel.setOpaque(false);
233                 menuBar.add(panel);
234
235                 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
236                 menuBar.add(helpMenu);
237                 helpMenu.add(aboutAction);
238
239                 I18nContainer.getInstance().registerRunnable(new Runnable() {
240
241                         @SuppressWarnings("synthetic-access")
242                         public void run() {
243                                 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
244                                 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
245                                 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
246                                 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
247                                         languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
248                                 }
249                         }
250                 });
251
252                 return menuBar;
253         }
254
255         /**
256          * Initializes all pages.
257          */
258         private void initPages() {
259                 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
260                 nodeManagerPage.setName("page.node-manager");
261                 nodeManagerPage.addNodeManagerListener(this);
262                 nodeManagerPage.setNodes(configuration.getNodes());
263                 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
264
265                 ProjectPage projectPage = new ProjectPage(wizard);
266                 projectPage.setName("page.project");
267                 projectPage.setProjects(configuration.getProjects());
268                 projectPage.setFreenetInterface(freenetInterface);
269                 projectPage.addListSelectionListener(this);
270                 pages.put(PageType.PAGE_PROJECTS, projectPage);
271
272                 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
273                 projectFilesPage.setName("page.project.files");
274                 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
275
276                 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
277                 projectInsertPage.setDebug(debug);
278                 projectInsertPage.setName("page.project.insert");
279                 projectInsertPage.setFreenetInterface(freenetInterface);
280                 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
281         }
282
283         /**
284          * Shows the page with the given type.
285          *
286          * @param pageType
287          *            The page type to show
288          */
289         private void showPage(PageType pageType) {
290                 wizard.setPreviousEnabled(pageType.ordinal() > 0);
291                 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
292                 wizard.setPage(pages.get(pageType));
293                 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
294         }
295
296         /**
297          * Saves the configuration.
298          *
299          * @return <code>true</code> if the configuration could be saved,
300          *         <code>false</code> otherwise
301          */
302         private boolean saveConfiguration() {
303                 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
304                 configuration.setNodes(nodeManagerPage.getNodes());
305                 if (selectedNode != null) {
306                         configuration.setSelectedNode(selectedNode);
307                 }
308
309                 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
310                 configuration.setProjects(projectPage.getProjects());
311
312                 return configuration.save();
313         }
314
315         /**
316          * Finds a supported locale for the given locale.
317          *
318          * @param forLocale
319          *            The locale to find a supported locale for
320          * @return The supported locale that was found, or the default locale if no
321          *         supported locale could be found
322          */
323         private Locale findSupportedLocale(Locale forLocale) {
324                 for (Locale locale : SUPPORTED_LOCALES) {
325                         if (locale.equals(forLocale)) {
326                                 return locale;
327                         }
328                 }
329                 for (Locale locale : SUPPORTED_LOCALES) {
330                         if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
331                                 return locale;
332                         }
333                 }
334                 for (Locale locale : SUPPORTED_LOCALES) {
335                         if (locale.getLanguage().equals(forLocale.getLanguage())) {
336                                 return locale;
337                         }
338                 }
339                 return SUPPORTED_LOCALES[0];
340         }
341
342         //
343         // ACTIONS
344         //
345
346         /**
347          * Switches the language of the interface to the given locale.
348          *
349          * @param locale
350          *            The locale to switch to
351          */
352         private void switchLanguage(Locale locale) {
353                 Locale supportedLocale = findSupportedLocale(locale);
354                 Action languageAction = languageActions.get(supportedLocale);
355                 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
356                 menuItem.setSelected(true);
357                 I18n.setLocale(supportedLocale);
358                 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
359                         try {
360                                 i18nRunnable.run();
361                         } catch (Throwable t) {
362                                 /* we probably shouldn't swallow this. */
363                         }
364                 }
365                 wizard.setPage(wizard.getPage());
366                 configuration.setLocale(supportedLocale);
367         }
368
369         //
370         // INTERFACE ListSelectionListener
371         //
372
373         /**
374          * {@inheritDoc}
375          */
376         public void valueChanged(ListSelectionEvent e) {
377                 JList list = (JList) e.getSource();
378                 int selectedRow = list.getSelectedIndex();
379                 wizard.setNextEnabled(selectedRow > -1);
380         }
381
382         //
383         // INTERFACE WizardListener
384         //
385
386         /**
387          * {@inheritDoc}
388          */
389         public void wizardNextPressed(TWizard wizard) {
390                 String pageName = wizard.getPage().getName();
391                 if ("page.node-manager".equals(pageName)) {
392                         showPage(PageType.PAGE_PROJECTS);
393                 } else if ("page.project".equals(pageName)) {
394                         ProjectPage projectPage = (ProjectPage) wizard.getPage();
395                         Project project = projectPage.getSelectedProject();
396                         if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
397                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
398                                 return;
399                         }
400                         if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
401                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
402                                 return;
403                         }
404                         ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
405                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
406                         showPage(PageType.PAGE_PROJECT_FILES);
407                 } else if ("page.project.files".equals(pageName)) {
408                         ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
409                         Project project = projectPage.getSelectedProject();
410                         if (selectedNode == null) {
411                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
412                                 return;
413                         }
414                         if (project.getIndexFile() == null) {
415                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
416                                         return;
417                                 }
418                         } else {
419                                 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
420                                 if (!indexFile.exists()) {
421                                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
422                                         return;
423                                 }
424                         }
425                         String indexFile = project.getIndexFile();
426                         boolean hasIndexFile = (indexFile != null);
427                         if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
428                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
429                                         return;
430                                 }
431                         }
432                         if (hasIndexFile && !project.getFileOption(indexFile).getMimeType().equals("text/html")) {
433                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
434                                         return;
435                                 }
436                         }
437                         Map<String, FileOption> fileOptions = project.getFileOptions();
438                         Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
439                         for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
440                                 FileOption fileOption = fileOptionEntry.getValue();
441                                 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
442                                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
443                                         return;
444                                 }
445                         }
446                         boolean nodeRunning = false;
447                         try {
448                                 nodeRunning = freenetInterface.isNodePresent();
449                         } catch (IOException e) {
450                                 /* ignore. */
451                         }
452                         if (!nodeRunning) {
453                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
454                                 return;
455                         }
456                         configuration.save();
457                         showPage(PageType.PAGE_INSERT_PROJECT);
458                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).startInsert();
459                         nodeMenu.setEnabled(false);
460                 } else if ("page.project.insert".equals(pageName)) {
461                         showPage(PageType.PAGE_PROJECTS);
462                         nodeMenu.setEnabled(true);
463                 }
464         }
465
466         /**
467          * {@inheritDoc}
468          */
469         public void wizardPreviousPressed(TWizard wizard) {
470                 String pageName = wizard.getPage().getName();
471                 if ("page.project".equals(pageName)) {
472                         showPage(PageType.PAGE_NODE_MANAGER);
473                 } else if ("page.project.files".equals(pageName)) {
474                         showPage(PageType.PAGE_PROJECTS);
475                 } else if ("page.project.insert".equals(pageName)) {
476                         showPage(PageType.PAGE_PROJECT_FILES);
477                 }
478         }
479
480         /**
481          * {@inheritDoc}
482          */
483         public void wizardQuitPressed(TWizard wizard) {
484                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
485                         if (saveConfiguration()) {
486                                 System.exit(0);
487                         }
488                         if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
489                                 System.exit(0);
490                         }
491                 }
492         }
493
494         //
495         // INTERFACE NodeManagerListener
496         //
497
498         /**
499          * {@inheritDoc}
500          */
501         public void nodesUpdated(Node[] nodes) {
502                 nodeMenu.removeAll();
503                 ButtonGroup nodeButtonGroup = new ButtonGroup();
504                 Node newSelectedNode = null;
505                 for (Node node : nodes) {
506                         JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
507                         nodeMenuItem.putClientProperty("Node", node);
508                         nodeMenuItem.addActionListener(this);
509                         nodeButtonGroup.add(nodeMenuItem);
510                         if (node.equals(selectedNode)) {
511                                 newSelectedNode = node;
512                                 nodeMenuItem.setSelected(true);
513                         }
514                         nodeMenu.add(nodeMenuItem);
515                 }
516                 nodeMenu.addSeparator();
517                 nodeMenu.add(manageNodeAction);
518                 selectedNode = newSelectedNode;
519                 freenetInterface.setNode(selectedNode);
520         }
521
522         /**
523          * {@inheritDoc}
524          */
525         public void actionPerformed(ActionEvent e) {
526                 Object source = e.getSource();
527                 if (source instanceof JRadioButtonMenuItem) {
528                         JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
529                         Node node = (Node) menuItem.getClientProperty("Node");
530                         selectedNode = node;
531                         freenetInterface.setNode(selectedNode);
532                 }
533         }
534
535         //
536         // MAIN METHOD
537         //
538
539         /**
540          * Main method that is called by the VM.
541          *
542          * @param args
543          *            The command-line arguments
544          */
545         public static void main(String[] args) {
546                 String configFilename = null;
547                 boolean nextIsConfigFilename = false;
548                 for (String argument : args) {
549                         if (nextIsConfigFilename) {
550                                 configFilename = argument;
551                                 nextIsConfigFilename = false;
552                         }
553                         if ("--help".equals(argument)) {
554                                 printHelp();
555                                 return;
556                         } else if ("--debug".equals(argument)) {
557                                 debug = true;
558                         } else if ("--config-file".equals(argument)) {
559                                 nextIsConfigFilename = true;
560                         }
561                 }
562                 if (nextIsConfigFilename) {
563                         System.out.println("--config-file needs parameter!");
564                         return;
565                 }
566                 new Main(configFilename);
567         }
568
569         /**
570          * Prints a small syntax help.
571          */
572         private static void printHelp() {
573                 System.out.println("--help\tshows this cruft");
574                 System.out.println("--debug\tenables some debug output");
575                 System.out.println("--config-file <file>\tuse specified configuration file");
576         }
577
578 }