Make update URL a bit easier to update.
[jSite.git] / src / de / todesbaum / jsite / gui / UpdateChecker.java
1 /*
2  * jSite-remote - UpdateChecker.java -
3  * Copyright © 2008 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.gui;
21
22 import java.awt.BorderLayout;
23 import java.awt.event.ActionEvent;
24 import java.io.IOException;
25 import java.text.MessageFormat;
26
27 import javax.swing.AbstractAction;
28 import javax.swing.Action;
29 import javax.swing.JButton;
30 import javax.swing.JDialog;
31 import javax.swing.JFrame;
32 import javax.swing.JLabel;
33 import javax.swing.JOptionPane;
34 import javax.swing.JPanel;
35 import javax.swing.JProgressBar;
36
37 import de.todesbaum.jsite.application.Freenet7Interface;
38 import de.todesbaum.jsite.i18n.I18n;
39 import de.todesbaum.jsite.i18n.I18nContainer;
40 import de.todesbaum.util.freenet.fcp2.Connection;
41
42 /**
43  * Checks for newer versions of jSite.
44  *
45  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
46  */
47 public class UpdateChecker {
48
49         /** The edition for the update check URL. */
50         private static final int UPDATE_EDITION = 2;
51
52         /** The URL for update checks. */
53         @SuppressWarnings("unused")
54         private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE/jSite/" + UPDATE_EDITION + "/jSite.properties";
55
56         /** Object used for synchronization. */
57         private final Object syncObject = new Object();
58
59         /** The parent of the dialog. */
60         private final JFrame parent;
61
62         /** The freenet interface. */
63         private final Freenet7Interface freenetInterface;
64
65         /** The cancel action. */
66         private Action cancelAction;
67
68         /** Whether the busy dialog has been cancelled. */
69         private boolean cancelled;
70
71         /**
72          * Creates a new update checker that uses the given frame as its parent and
73          * communications via the given freenet interface.
74          *
75          * @param parent
76          *            The parent of the dialog
77          * @param freenetInterface
78          *            The freenet interface
79          */
80         public UpdateChecker(JFrame parent, Freenet7Interface freenetInterface) {
81                 this.parent = parent;
82                 this.freenetInterface = freenetInterface;
83                 cancelled = false;
84                 initActions();
85         }
86
87         //
88         // ACTIONS
89         //
90
91         /**
92          * Checks for updates, showing a dialog with an indeterminate progress bar.
93          */
94         public void checkForUpdates() {
95                 JDialog busyDialog = showBusyDialog();
96                 Connection connection = freenetInterface.getConnection("jSite-update-check");
97                 try {
98                         if (!connection.connect()) {
99                                 busyDialog.setVisible(false);
100                                 JOptionPane.showMessageDialog(parent, I18n.getMessage(""), I18n.getMessage(""), JOptionPane.ERROR_MESSAGE);
101                                 return;
102                         }
103                 } catch (IOException ioe1) {
104                         busyDialog.setVisible(false);
105                         JOptionPane.showMessageDialog(parent, MessageFormat.format(I18n.getMessage(""), ioe1.getMessage()), I18n.getMessage(""), JOptionPane.ERROR_MESSAGE);
106                 } finally {
107                         connection.disconnect();
108                 }
109         }
110
111         //
112         // PRIVATE METHODS
113         //
114
115         /**
116          * Initializes all actions.
117          */
118         private void initActions() {
119                 cancelAction = new AbstractAction(I18n.getMessage("")) {
120
121                         /**
122                          * {@inheritDoc}
123                          */
124                         @SuppressWarnings("synthetic-access")
125                         public void actionPerformed(ActionEvent actionEvent) {
126                                 synchronized (syncObject) {
127                                         cancelled = true;
128                                 }
129                         }
130                 };
131         }
132
133         /**
134          * Shows a “please wait” dialog.
135          *
136          * @return The dialog
137          */
138         private JDialog showBusyDialog() {
139                 BusyPanel busyPanel = new BusyPanel();
140                 JButton cancelButton = new JButton(cancelAction);
141                 JOptionPane optionPane = new JOptionPane(busyPanel, JOptionPane.INFORMATION_MESSAGE, 0, null, new Object[] { cancelButton });
142                 final JDialog busyDialog = optionPane.createDialog(parent, I18n.getMessage(""));
143                 new Thread(new Runnable() {
144
145                         /**
146                          * {@inheritDoc}
147                          */
148                         public void run() {
149                                 busyDialog.setVisible(true);
150                         }
151                 }).start();
152                 return busyDialog;
153         }
154
155         /**
156          * A panel that shows a busy progress bar and a “please wait” message.
157          *
158          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
159          */
160         private class BusyPanel extends JPanel {
161
162                 /**
163                  * Creates a new busy panel.
164                  */
165                 public BusyPanel() {
166                         super(new BorderLayout(12, 12));
167                         initComponents();
168                 }
169
170                 //
171                 // PRIVATE METHODS
172                 //
173
174                 /**
175                  * Initializes all components of this panel.
176                  */
177                 private void initComponents() {
178                         final JLabel label = new JLabel(I18n.getMessage("")); /* TODO */
179                         JProgressBar progressBar = new JProgressBar();
180                         progressBar.setIndeterminate(true);
181
182                         add(label, BorderLayout.PAGE_START);
183                         add(progressBar, BorderLayout.PAGE_END);
184
185                         I18nContainer.getInstance().registerRunnable(new Runnable() {
186
187                                 /**
188                                  * {@inheritDoc}
189                                  */
190                                 public void run() {
191                                         label.setText(I18n.getMessage("")); /* TODO */
192                                 }
193                         });
194                 }
195
196         }
197
198 }