version 0.4.11:
[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  * @author <a href="mailto:droden@gmail.com">David Roden </a>
64  * @version $Id$
65  */
66 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
67
68         private static boolean debug = false;
69         private Configuration configuration;
70         private Freenet7Interface freenetInterface = new Freenet7Interface();
71         protected Icon jSiteIcon;
72
73         private static enum PageType {
74                 PAGE_NODE_MANAGER, PAGE_PROJECTS, PAGE_PROJECT_FILES, PAGE_INSERT_PROJECT
75         }
76
77         private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, Locale.ITALIAN };
78         protected Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
79         protected Action manageNodeAction;
80         protected Action aboutAction;
81         protected TWizard wizard;
82         protected JMenu nodeMenu;
83         private Node selectedNode;
84         private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
85
86         private Main() {
87                 this(null);
88         }
89
90         private Main(String configFilename) {
91                 if (configFilename != null) {
92                         configuration = new Configuration(configFilename);
93                 } else {
94                         configuration = new Configuration();
95                 }
96                 Locale.setDefault(configuration.getLocale());
97                 I18n.setLocale(configuration.getLocale());
98                 if (!configuration.createLockFile()) {
99                         JOptionPane.showMessageDialog(null, I18n.getMessage("jsite.main.already-running"), null, JOptionPane.ERROR_MESSAGE);
100                         return;
101                 }
102                 wizard = new TWizard();
103                 createActions();
104                 wizard.setJMenuBar(createMenuBar());
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         }
115
116         private void createActions() {
117                 for (final Locale locale: SUPPORTED_LOCALES) {
118                         languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage())) {
119
120                                 public void actionPerformed(ActionEvent actionEvent) {
121                                         switchLanguage(locale);
122                                 }
123                         });
124                 }
125                 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
126                         public void actionPerformed(ActionEvent actionEvent) {
127                                 showPage(PageType.PAGE_NODE_MANAGER);
128                                 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
129                                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
130                         }
131                 };
132                 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
133
134                         public void actionPerformed(ActionEvent e) {
135                                 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), Version.getVersion()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
136                         }
137                 };
138
139                 I18nContainer.getInstance().registerRunnable(new Runnable() {
140                         public void run() {
141                                 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
142                                 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
143                         }
144                 });
145         }
146
147         private JMenuBar createMenuBar() {
148                 JMenuBar menuBar = new JMenuBar();
149                 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
150                 menuBar.add(languageMenu);
151                 ButtonGroup languageButtonGroup = new ButtonGroup();
152                 for (Locale locale: SUPPORTED_LOCALES) {
153                         Action languageAction = languageActions.get(locale);
154                         JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
155                         if (locale.equals(Locale.getDefault())) {
156                                 menuItem.setSelected(true);
157                         }
158                         languageAction.putValue("menuItem", menuItem);
159                         languageButtonGroup.add(menuItem);
160                         languageMenu.add(menuItem);
161                 }
162                 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
163                 menuBar.add(nodeMenu);
164                 selectedNode = configuration.getSelectedNode();
165                 nodesUpdated(configuration.getNodes());
166
167                 /* evil hack to right-align the help menu */
168                 JPanel panel = new JPanel();
169                 panel.setOpaque(false);
170                 menuBar.add(panel);
171
172                 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
173                 menuBar.add(helpMenu);
174                 helpMenu.add(aboutAction);
175
176                 I18nContainer.getInstance().registerRunnable(new Runnable() {
177                         public void run() {
178                                 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
179                                 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
180                                 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
181                                 for (Map.Entry<Locale, Action> languageActionEntry: languageActions.entrySet()) {
182                                         languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
183                                 }
184                         }
185                 });
186
187                 return menuBar;
188         }
189
190         private void initPages() {
191                 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
192                 nodeManagerPage.setName("page.node-manager");
193                 nodeManagerPage.addNodeManagerListener(this);
194                 nodeManagerPage.setNodes(configuration.getNodes());
195                 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
196
197                 ProjectPage projectPage = new ProjectPage(wizard);
198                 projectPage.setName("page.project");
199                 projectPage.setProjects(configuration.getProjects());
200                 projectPage.setFreenetInterface(freenetInterface);
201                 projectPage.addListSelectionListener(this);
202                 pages.put(PageType.PAGE_PROJECTS, projectPage);
203
204                 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
205                 projectFilesPage.setName("page.project.files");
206                 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
207
208                 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
209                 projectInsertPage.setDebug(debug);
210                 projectInsertPage.setName("page.project.insert");
211                 projectInsertPage.setFreenetInterface(freenetInterface);
212                 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
213         }
214
215         protected void showPage(PageType pageType) {
216                 wizard.setPreviousEnabled(pageType.ordinal() > 0);
217                 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
218                 wizard.setPage(pages.get(pageType));
219                 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
220         }
221
222         private boolean saveConfiguration() {
223                 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
224                 configuration.setNodes(nodeManagerPage.getNodes());
225                 if (selectedNode != null) {
226                         configuration.setSelectedNode(selectedNode);
227                 }
228
229                 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
230                 configuration.setProjects(projectPage.getProjects());
231
232                 return configuration.save();
233         }
234
235         private Locale findSupportedLocale(Locale forLocale) {
236                 for (Locale locale: SUPPORTED_LOCALES) {
237                         if (locale.equals(forLocale)) {
238                                 return locale;
239                         }
240                 }
241                 for (Locale locale: SUPPORTED_LOCALES) {
242                         if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
243                                 return locale;
244                         }
245                 }
246                 for (Locale locale: SUPPORTED_LOCALES) {
247                         if (locale.getLanguage().equals(forLocale.getLanguage())) {
248                                 return locale;
249                         }
250                 }
251                 return SUPPORTED_LOCALES[0];
252         }
253
254         //
255         // ACTIONS
256         //
257
258         protected void switchLanguage(Locale locale) {
259                 Locale supportedLocale = findSupportedLocale(locale);
260                 Action languageAction = languageActions.get(supportedLocale);
261                 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
262                 menuItem.setSelected(true);
263                 I18n.setLocale(supportedLocale);
264                 for (Runnable i18nRunnable: I18nContainer.getInstance()) {
265                         try {
266                                 i18nRunnable.run();
267                         } catch (Throwable t) {
268                                 /* we probably shouldn't swallow this. */
269                         }
270                 }
271                 wizard.setPage(wizard.getPage());
272                 configuration.setLocale(supportedLocale);
273         }
274
275         //
276         // INTERFACE ListSelectionListener
277         //
278
279         /**
280          * {@inheritDoc}
281          */
282         public void valueChanged(ListSelectionEvent e) {
283                 JList list = (JList) e.getSource();
284                 int selectedRow = list.getSelectedIndex();
285                 wizard.setNextEnabled(selectedRow > -1);
286         }
287
288         //
289         // INTERFACE WizardListener
290         //
291
292         /**
293          * {@inheritDoc}
294          */
295         public void wizardNextPressed(TWizard wizard) {
296                 String pageName = wizard.getPage().getName();
297                 if ("page.node-manager".equals(pageName)) {
298                         showPage(PageType.PAGE_PROJECTS);
299                 } else if ("page.project".equals(pageName)) {
300                         ProjectPage projectPage = (ProjectPage) wizard.getPage();
301                         Project project = projectPage.getSelectedProject();
302                         if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
303                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
304                                 return;
305                         }
306                         if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
307                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
308                                 return;
309                         }
310                         ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
311                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
312                         showPage(PageType.PAGE_PROJECT_FILES);
313                 } else if ("page.project.files".equals(pageName)) {
314                         ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
315                         Project project = projectPage.getSelectedProject();
316                         if (selectedNode == null) {
317                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
318                                 return;
319                         }
320                         if (project.getIndexFile() == null) {
321                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
322                                         return;
323                                 }
324                         } else {
325                                 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
326                                 if (!indexFile.exists()) {
327                                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
328                                         return;
329                                 }
330                         }
331                         String indexFile = project.getIndexFile();
332                         boolean hasIndexFile = (indexFile != null);
333                         if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
334                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
335                                         return;
336                                 }
337                         }
338                         if (hasIndexFile && !project.getFileOption(indexFile).getMimeType().equals("text/html")) {
339                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
340                                         return;
341                                 }
342                         }
343                         Map<String, FileOption> fileOptions = project.getFileOptions();
344                         Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
345                         for (Entry<String, FileOption> fileOptionEntry: fileOptionEntries) {
346                                 FileOption fileOption = fileOptionEntry.getValue();
347                                 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
348                                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
349                                         return;
350                                 }
351                         }
352                         boolean nodeRunning = false;
353                         try {
354                                 nodeRunning = freenetInterface.isNodePresent();
355                         } catch (IOException e) {
356                         }
357                         if (!nodeRunning) {
358                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
359                                 return;
360                         }
361                         configuration.save();
362                         showPage(PageType.PAGE_INSERT_PROJECT);
363                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).startInsert();
364                         nodeMenu.setEnabled(false);
365                 } else if ("page.project.insert".equals(pageName)) {
366                         showPage(PageType.PAGE_PROJECTS);
367                         nodeMenu.setEnabled(true);
368                 }
369         }
370
371         /**
372          * {@inheritDoc}
373          */
374         public void wizardPreviousPressed(TWizard wizard) {
375                 String pageName = wizard.getPage().getName();
376                 if ("page.project".equals(pageName)) {
377                         showPage(PageType.PAGE_NODE_MANAGER);
378                 } else if ("page.project.files".equals(pageName)) {
379                         showPage(PageType.PAGE_PROJECTS);
380                 } else if ("page.project.insert".equals(pageName)) {
381                         showPage(PageType.PAGE_PROJECT_FILES);
382                 }
383         }
384
385         /**
386          * {@inheritDoc}
387          */
388         public void wizardQuitPressed(TWizard wizard) {
389                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
390                         if (saveConfiguration()) {
391                                 System.exit(0);
392                         }
393                         if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
394                                 System.exit(0);
395                         }
396                 }
397         }
398
399         //
400         // INTERFACE NodeManagerListener
401         //
402
403         /**
404          * {@inheritDoc}
405          */
406         public void nodesUpdated(Node[] nodes) {
407                 nodeMenu.removeAll();
408                 ButtonGroup nodeButtonGroup = new ButtonGroup();
409                 Node newSelectedNode = null;
410                 for (Node node: nodes) {
411                         JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
412                         nodeMenuItem.putClientProperty("Node", node);
413                         nodeMenuItem.addActionListener(this);
414                         nodeButtonGroup.add(nodeMenuItem);
415                         if (node.equals(selectedNode)) {
416                                 newSelectedNode = node;
417                                 nodeMenuItem.setSelected(true);
418                         }
419                         nodeMenu.add(nodeMenuItem);
420                 }
421                 nodeMenu.addSeparator();
422                 nodeMenu.add(manageNodeAction);
423                 selectedNode = newSelectedNode;
424                 freenetInterface.setNode(selectedNode);
425         }
426
427         /**
428          * {@inheritDoc}
429          */
430         public void actionPerformed(ActionEvent e) {
431                 Object source = e.getSource();
432                 if (source instanceof JRadioButtonMenuItem) {
433                         JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
434                         Node node = (Node) menuItem.getClientProperty("Node");
435                         selectedNode = node;
436                         freenetInterface.setNode(selectedNode);
437                 }
438         }
439
440         //
441         // MAIN METHOD
442         //
443         public static void main(String[] args) {
444                 String configFilename = null;
445                 boolean nextIsConfigFilename = false;
446                 for (String argument: args) {
447                         if (nextIsConfigFilename) {
448                                 configFilename = argument;
449                                 nextIsConfigFilename = false;
450                         }
451                         if ("--help".equals(argument)) {
452                                 printHelp();
453                                 return;
454                         } else if ("--debug".equals(argument)) {
455                                 debug = true;
456                         } else if ("--config-file".equals(argument)) {
457                                 nextIsConfigFilename = true;
458                         }
459                 }
460                 if (nextIsConfigFilename) {
461                         System.out.println("--config-file needs parameter!");
462                         return;
463                 }
464                 new Main(configFilename);
465         }
466
467         private static void printHelp() {
468                 System.out.println("--help\tshows this cruft");
469                 System.out.println("--debug\tenables some debug output");
470                 System.out.println("--config-file <file>\tuse specified configuration file");
471         }
472
473 }