source: main/trunk/gli/src/org/greenstone/gatherer/gui/DownloadPane.java@ 24287

Last change on this file since 24287 was 24287, checked in by ak19, 13 years ago

John Rose and Luigi requested the button pane in GLI's DownloadPane that houses the Download, Clear Cache, Server Information and Preferences buttons to be resizable.

  • Property svn:keywords set to Author Date Id Revision
File size: 26.2 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.gui;
38
39import java.awt.*;
40import java.awt.event.*;
41import java.io.*;
42import java.net.*;
43import java.util.*;
44import javax.swing.*;
45import javax.swing.border.*;
46import javax.swing.event.*;
47import javax.swing.tree.*;
48import org.greenstone.gatherer.Configuration;
49import org.greenstone.gatherer.DebugStream;
50import org.greenstone.gatherer.Dictionary;
51import org.greenstone.gatherer.Gatherer;
52import org.greenstone.gatherer.file.WorkspaceTree;
53import org.greenstone.gatherer.greenstone.LocalGreenstone;
54import org.greenstone.gatherer.util.StaticStrings;
55import org.greenstone.gatherer.util.Utility;
56import org.greenstone.gatherer.download.Download;
57import org.greenstone.gatherer.download.DownloadScrollPane;
58import org.greenstone.gatherer.download.ServerInfoDialog;
59import org.greenstone.gatherer.util.XMLTools;
60import org.greenstone.gatherer.cdm.*;
61import org.greenstone.gatherer.gui.*;
62import org.w3c.dom.*;
63import org.xml.sax.*;
64import org.greenstone.gatherer.GAuthenticator;
65
66/**
67 * @author John Thompson, Greenstone Digital Library, University of Waikato
68 * @version 2.1
69 */
70public class DownloadPane
71 extends JPanel {
72
73 static final private Dimension LABEL_SIZE = new Dimension(225, 25);
74 static final private Dimension TREE_SIZE = new Dimension(150, 500);
75 static final private String CONTENTS[] = { "DOWNLOAD.MODE.WebDownload", "DOWNLOAD.MODE.MediaWikiDownload", "DOWNLOAD.MODE.OAIDownload", "DOWNLOAD.MODE.ZDownload" , "DOWNLOAD.MODE.SRWDownload"};
76
77 private boolean download_button_enabled = false;
78 private boolean ready = false;
79
80 private JPanel options_pane;
81 // TODO should use Vector to store all loaded downloads!!
82
83 private DesignTree tree;
84 private HashMap download_map;
85 private ServerInfoDialog server_info;
86 private JScrollPane list_scroll;
87 private DownloadScrollPane getter;
88 private String mode = null;
89 private TreePath previous_path;
90 private String proxy_url = "";
91
92 /** Main System code */
93 public DownloadPane() {
94 super();
95 JScrollPane scrol_tmp;
96 this.setComponentOrientation(Dictionary.getOrientation());
97 // TODO: Download the WDownload and the download panel fixed!!
98 getter = new DownloadScrollPane();
99 getter.start();
100 list_scroll = getter.getDownloadJobList();
101 list_scroll.setComponentOrientation(Dictionary.getOrientation());
102 // TODO should use Vector to store all loaded downloads!!
103 String lang = Configuration.getLanguage();
104 download_map = new HashMap();
105 download_map.put("Web", loadDownload("WebDownload",lang));
106 download_map.put("MediaWiki", loadDownload("MediaWikiDownload",lang));
107 download_map.put("OAI", loadDownload("OAIDownload",lang));
108 download_map.put("Z3950", loadDownload("Z3950Download",lang));
109 download_map.put("SRW", loadDownload("SRWDownload",lang));
110
111 // Creation
112 tree = new DesignTree();
113 tree.setComponentOrientation(Dictionary.getOrientation());
114 options_pane = new JPanel();
115 options_pane.setComponentOrientation(Dictionary.getOrientation());
116
117 JButton clear_cache_button = new GLIButton(Dictionary.get("Mirroring.ClearCache"), Dictionary.get("Mirroring.ClearCache_Tooltip"));
118 clear_cache_button.setEnabled(true);
119 clear_cache_button.setMnemonic(KeyEvent.VK_C);
120
121 JButton download_button = new GLIButton(Dictionary.get("Mirroring.Download"), Dictionary.get("Mirroring.Download_Tooltip"));
122 download_button.setEnabled(true);
123 download_button.setMnemonic(KeyEvent.VK_D);
124
125 JButton information_button = new GLIButton(Dictionary.get("Download.ServerInformation"), Dictionary.get("Download.ServerInformation_Tooltip"));
126 information_button.setEnabled(true);
127 information_button.setMnemonic(KeyEvent.VK_S);
128
129
130 JButton preferences_button = new GLIButton(Dictionary.get("Mirroring.Preferences"), Dictionary.get("Mirroring.Preferences_Tooltip"));
131 preferences_button.setEnabled(true);
132 preferences_button.setMnemonic(KeyEvent.VK_P);
133
134 // Connect
135 clear_cache_button.addActionListener(new ClearCacheListener());
136 download_button.addActionListener(new DownloadButtonListener());
137 preferences_button.addActionListener(new PreferencesButtonActionListener());
138 information_button.addActionListener(new InformationButtonActionListener());
139 tree.addTreeSelectionListener(new TreeListener());
140
141 // Add to Panel
142 JPanel button_pane = new JPanel();
143 button_pane.setComponentOrientation(Dictionary.getOrientation());
144 button_pane.setLayout(new GridLayout(1,4)); // GridLayout so button pane resizes with window-width
145 button_pane.setBorder(BorderFactory.createEtchedBorder());
146 button_pane.add(clear_cache_button);
147 button_pane.add(download_button);
148 button_pane.add(information_button);
149 button_pane.add(preferences_button);
150
151 JPanel tree_pane = new JPanel();
152 tree_pane.setComponentOrientation(Dictionary.getOrientation());
153 tree_pane.setLayout(new BorderLayout());
154 scrol_tmp = new JScrollPane(tree);
155 scrol_tmp.setComponentOrientation(Dictionary.getOrientation());
156 tree_pane.add(scrol_tmp, BorderLayout.CENTER);
157 tree_pane.setPreferredSize(TREE_SIZE);
158
159
160 Color colour_two = Configuration.getColor("coloring.collection_tree_background", false);
161 options_pane.setBackground(colour_two);
162 options_pane.setBorder(BorderFactory.createEtchedBorder());
163
164
165 JScrollPane options_scroll_pane = new JScrollPane(options_pane);
166 options_scroll_pane.setComponentOrientation(Dictionary.getOrientation());
167 JSplitPane mode_pane = new JSplitPane();
168 mode_pane.setComponentOrientation(Dictionary.getOrientation());
169 mode_pane.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
170 if (Dictionary.getOrientation().isLeftToRight()){
171 mode_pane.add(tree_pane,JSplitPane.LEFT);
172 mode_pane.add(options_scroll_pane,JSplitPane.RIGHT);
173 mode_pane.setDividerLocation(TREE_SIZE.width);
174 }else{
175 mode_pane.add(tree_pane,JSplitPane.RIGHT);
176 mode_pane.add(options_scroll_pane,JSplitPane.LEFT);
177 mode_pane.setDividerLocation(1-TREE_SIZE.width);
178 }
179
180
181 JPanel edit_pane = new JPanel();
182 edit_pane.setComponentOrientation(Dictionary.getOrientation());
183 edit_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(2,0,0,0), BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Download Setting"), BorderFactory.createEmptyBorder(2,2,2,2))));
184 edit_pane.setLayout(new BorderLayout());
185 edit_pane.add(mode_pane,BorderLayout.CENTER);
186 edit_pane.add(button_pane,BorderLayout.PAGE_END);
187
188 // Add to "this"
189 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
190 setLayout(new GridLayout(2,1));
191 add(edit_pane);
192 add(list_scroll);
193
194 mode = "Web";
195 generateOptions(options_pane,(Download)download_map.get(mode));
196 previous_path = tree.getSelectionPath();
197 }
198
199 /** System Utilities */
200 public void modeChanged(int gli_mode) {
201 // do nothing at this stage - should we be renewing download options??
202 }
203
204 private void addHeader(String name, Color color, JPanel target_pane) {
205 JPanel header = new JPanel();
206 header.setComponentOrientation(Dictionary.getOrientation());
207 header.setBackground(color);
208 JPanel inner_pane = new JPanel();
209 inner_pane.setComponentOrientation(Dictionary.getOrientation());
210 inner_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5,5,5,5), BorderFactory.createRaisedBevelBorder()));
211 inner_pane.setBackground(color);
212 JLabel header_label = new JLabel("<html><strong>" + name + "</strong></html>");
213 header_label.setComponentOrientation(Dictionary.getOrientation());
214 header_label.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
215 header_label.setHorizontalAlignment(JLabel.CENTER);
216 header_label.setOpaque(true);
217
218 // Layout
219 inner_pane.setLayout(new BorderLayout());
220 inner_pane.add(header_label, BorderLayout.CENTER);
221
222 header.setLayout(new BorderLayout());
223 header.add(inner_pane, BorderLayout.CENTER);
224 target_pane.add(header);
225 }
226
227 /** Supporting Functions */
228 private Download loadDownload(String download_name, String lang) {
229 Document document = null;
230
231 try {
232 if (Gatherer.isGsdlRemote) {
233 String output = Gatherer.remoteGreenstoneServer.getScriptOptions("downloadinfo.pl", "&downloader="+download_name); //OR: "&listall";
234 Reader reader = new StringReader(output);
235 document = XMLTools.parseXML(reader);
236 }
237 else {
238 ArrayList args_list = new ArrayList();
239 String args[] = null;
240 if(Configuration.perl_path != null) {
241 args_list.add(Configuration.perl_path);
242 } else if(Utility.isWindows()) {
243 args_list.add("Perl.exe");
244 } else {
245 args_list.add("perl");
246 }
247 args_list.add("-S");
248 args_list.add(LocalGreenstone.getBinScriptDirectoryPath()+"downloadinfo.pl");
249 args_list.add("-xml");
250 args_list.add("-language");
251 args_list.add(lang);
252 args_list.add(download_name);
253
254 // Create the process.
255 args = (String []) args_list.toArray(new String[0]);
256 Runtime runtime = Runtime.getRuntime();
257 DebugStream.println("Getting Download Info: "+args_list);
258 Process process = runtime.exec(args);
259
260 InputStream input_stream = process.getErrorStream();
261 document = XMLTools.parseXML(input_stream);
262 }
263
264
265 }
266 catch (Exception error) {
267 System.err.println("Failed when trying to parse: " + download_name);
268 error.printStackTrace();
269 }
270
271 if(document != null) {
272 return parseXML(document.getDocumentElement());
273 }
274
275 return null;
276 }
277
278 private Download parseXML(Node root) {
279 Download download = new Download();
280 String node_name = null;
281 for (Node node = root.getFirstChild(); node != null; node = node.getNextSibling()) {
282 node_name = node.getNodeName();
283 if(node_name.equalsIgnoreCase("Name")) {
284 String name = XMLTools.getValue(node);
285 download.setName(name);
286 }
287 else if (node_name.equalsIgnoreCase("Desc")) {
288 download.setDescription(XMLTools.getValue(node));
289 }
290 else if (node_name.equalsIgnoreCase("Abstract")) {
291 download.setIsAbstract(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
292 }
293 else if(node_name.equalsIgnoreCase("Arguments")) {
294 for(Node arg = node.getFirstChild(); arg != null; arg = arg.getNextSibling()) {
295 node_name = arg.getNodeName();
296 if(node_name.equalsIgnoreCase("Option")) {
297 Argument argument = new Argument((Element)arg);
298 argument.parseXML((Element)arg);
299 argument.setValue(argument.getDefaultValue());
300 download.addArgument(argument);
301 }
302
303 }
304 }
305 else if(node_name.equalsIgnoreCase("DownloadInfo")) {
306 Download super_download = parseXML(node);
307 download.setSuper(super_download);
308 }
309 }
310
311 if(download.getName() != null) {
312 return download;
313 }
314 return null;
315 }
316
317 /** Update the previous setup */
318 private boolean updateArguments(boolean checkRequired)
319 {
320 boolean cont = true;
321 for(int i = 0; i < options_pane.getComponentCount(); i++) {
322
323 Component component = options_pane.getComponent(i);
324 if(component instanceof ArgumentControl) {
325 cont = cont && ((ArgumentControl)component).updateArgument(checkRequired);
326 }
327 }
328
329 if(cont){return true; }
330
331 return false;
332 }
333
334 /** Generate Controls for Options */
335 /* at some stage we should think about which options should be shown for
336 * different modes. Currently, always show all options (unless hidden)*/
337 private void generateOptions(JPanel options_pane, ArgumentContainer data) {
338 options_pane.removeAll();
339 /** Create the current option panel */
340
341 ArrayList arguments = data.getArguments(true, false);
342 int mode = Configuration.getMode();
343 ArrayList added_arguments = new ArrayList();
344
345 for(int i = 0; i < arguments.size(); i++) {
346 Argument argument = (Argument) arguments.get(i);
347
348 if (argument.isHiddenGLI()) continue;
349 ArgumentControl argument_control = new ArgumentControl(argument,false,null);
350 added_arguments.add(argument_control);
351 }
352
353
354 options_pane.setLayout(new GridLayout(added_arguments.size(),1));
355 for(int i = 0; i < added_arguments.size(); i++) {
356 options_pane.add((ArgumentControl)added_arguments.get(i));
357
358 }
359 }
360
361 /** Behaviour Functions */
362 public void afterDisplay() {
363 ready = true;
364 }
365
366
367 public void gainFocus() {
368 if(!ready) {
369 return;
370 }
371
372 // It is also a good time to determine if the download should be enabled - ie if its allowed to be enabled and a valid URL is present in the field.
373 download_button_enabled = true;
374 //download_button.setEnabled(download_button_enabled);
375 }
376
377
378
379 public void refresh(int refresh_reason, boolean ready)
380 {
381 }
382
383 /** Private classes */
384 /** This tree provides a 'table of contents' for the various components of the design process (collection configuration in more technical terms). */
385 private class DesignTree extends JTree {
386
387 private DesignNode root = null;
388 /** Constructor. Automatically generates all of the nodes, in the order of CONTENTS. */
389 public DesignTree() {
390 super();
391 this.setComponentOrientation(Dictionary.getOrientation());
392 resetModel(Configuration.getMode());
393 expandRow(0);
394 setRootVisible(false);
395 setSelectionRow(0);
396 }
397
398 /** Reset the model used by the design page contents tree. This is necessary to hide the partitions entry when in lower detail modes
399 * @param mode the current detail mode as an int
400 */
401 public void resetModel(int mode) {
402 root = new DesignNode("DOWNLOAD.MODE.Root");
403 // Now add the design categories.
404 for(int i = 0; i < CONTENTS.length; i++) {
405 root.add(new DesignNode(CONTENTS[i]));
406 }
407 this.setModel(new DefaultTreeModel(root));
408 updateUI();
409 }
410 /** Set the current view to the one specified.
411 * @param type the name of the desired view as a String
412 */
413 public void setSelectedView(String type) {
414 type = Dictionary.get(type);
415 for(int i = 0; i < root.getChildCount(); i++) {
416 DesignNode child = (DesignNode) root.getChildAt(i);
417 if(child.toString().equals(type)) {
418 TreePath path = new TreePath(child.getPath());
419 setSelectionPath(path);
420 }
421 }
422 }
423 }
424
425 /** A tree node that retains a reference to one of the possible design sub-views relating to the different sub-managers. */
426 private class DesignNode extends DefaultMutableTreeNode {
427 /** Constructor.
428 * @param object The <strong>Object</strong> assigned to this node.
429 */
430 public DesignNode(String object) {
431 super(object);
432 }
433 /** Retrieve a textual representation of the object.
434 * @return a String
435 */
436 public String toString() {
437 // return Dictionary.get("CDM.GUI." + (String)getUserObject());
438 return Dictionary.get((String) getUserObject());
439 }
440 }
441
442 /** Listens for selection changes in the 'contents' tree, and switches to the appropriate view. */
443 private class TreeListener
444 implements TreeSelectionListener {
445 /** Called whenever the selection changes, we must update the view so it matches the node selected.
446 * @param event A <strong>TreeSelectionEvent</strong> containing more information about the tree selection.
447 * @see org.greenstone.gatherer.cdm.ClassifierManager
448 * @see org.greenstone.gatherer.cdm.CollectionDesignManager
449 * @see org.greenstone.gatherer.cdm.CollectionMetaManager
450 * @see org.greenstone.gatherer.cdm.FormatManager
451 * @see org.greenstone.gatherer.cdm.LanguageManager
452 * @see org.greenstone.gatherer.cdm.MetadataSetView
453 * @see org.greenstone.gatherer.cdm.SubcollectionManager
454 * @see org.greenstone.gatherer.cdm.TranslationView
455 * @see org.greenstone.gatherer.cdm.PlugInManager
456 */
457 public void valueChanged(TreeSelectionEvent event) {
458 if(!tree.isSelectionEmpty()) {
459 TreePath path = tree.getSelectionPath();
460
461 DesignNode node = (DesignNode)path.getLastPathComponent();
462 String type = (String)node.getUserObject();
463 Gatherer.g_man.wait(true);
464 if(type == CONTENTS[0]) {
465 mode = "Web";
466 generateOptions(options_pane,(Download)download_map.get(mode));
467 }
468 else if(type == CONTENTS[1]) {
469 mode = "MediaWiki";
470 generateOptions(options_pane,(Download)download_map.get(mode));
471 }
472 else if(type == CONTENTS[2]) {
473 mode = "OAI";
474 generateOptions(options_pane,(Download)download_map.get(mode));
475 }
476 else if(type == CONTENTS[3]) {
477 mode = "Z3950";
478 generateOptions(options_pane,(Download)download_map.get(mode));
479 }
480 else if(type == CONTENTS[4]) {
481 mode = "SRW";
482 generateOptions(options_pane,(Download)download_map.get(mode));
483 }
484 tree.setSelectionPath(path);
485 previous_path = path;
486 repaint();
487
488 Gatherer.g_man.wait(false);
489 }
490 }
491 }
492
493 private class ClearCacheListener
494 implements ActionListener {
495 public void actionPerformed(ActionEvent event) {
496 // Retrieve the cache folder and delete it.
497 Utility.delete(Utility.getCacheDir());
498 // ...and refresh the node in the workspace tree to show it's all gone
499 Gatherer.g_man.refreshWorkspaceTree(WorkspaceTree.DOWNLOADED_FILES_CHANGED);
500 }
501 }
502
503 private class DownloadButtonListener
504 implements ActionListener {
505 public void actionPerformed(ActionEvent event) {
506
507 if(checkURL(true) && checkProxy() == true) {
508
509 // Proxy settings are now set. Check that the url is not a redirect, else get
510 // redirect url (we do this step in order to avoid some unintuitive behaviour from wget)
511 Download current_download = (Download)download_map.get(mode);
512 Argument arg_url = current_download.getArgument("url");
513 if(arg_url != null) { // it's null for z3950 and possibly for other downloaders
514 String url_str = arg_url.getValue();
515 String redirect_url_str = getRedirectURL(url_str);
516
517 // only update the Argument and its GUI ArgumentControl if the URL
518 // has in fact changed
519 if(!url_str.equals(redirect_url_str)) {
520 arg_url.setValue(redirect_url_str);
521 updateArgument(arg_url, redirect_url_str);
522 }
523 }
524 getter.newDownloadJob((Download)download_map.get(mode) ,mode,proxy_url);
525 }
526 }
527 }
528
529 /**
530 * The Java code here will retrieve the page at the given url. If the response code is
531 * a redirect, it will get the redirect url so that wget may be called with the proper url.
532 * This preprocessing of the URL is necessary because:
533 * Wget does not behave the way the browser does when faced with urls of the form
534 * http://www.englishhistory.net/tudor/citizens and if that page does not exist.
535 * The directory listing with a slash at the end (http://www.englishhistory.net/tudor/citizens/)
536 * does exist, however. In order to prevent wget from assuming that the root URL
537 * to traverse is http://www.englishhistory.net/tudor/ instead of the intended
538 * http://www.englishhistory.net/tudor/citizens/, we need give wget the redirect location
539 * that's returned when we initially make a request for http://www.englishhistory.net/tudor/citizens
540 * The proper url is sent back in the Location header, allowing us to bypass wget's
541 * unexpected behaviour.
542 * This method ensures that urls like http://www.nzdl.org/niupepa also continue to work:
543 * there is no http://www.nzdl.org/niupepa/ page, because this url actually redirects to an
544 * entirely different URL.
545 * @return the redirect url for the given url if any redirection is involved, or the
546 * url_str.
547 */
548 private String getRedirectURL(String url_str) {
549 HttpURLConnection connection = null;
550 if(url_str.startsWith("http:")) { // only test http urls
551 try {
552 URL url = new URL(url_str);
553 connection = (HttpURLConnection)url.openConnection(); //new HttpURLConnection(url);
554 // don't let it automatically follow redirects, since we want to
555 // find out whether we are dealing with redirects in the first place
556 connection.setInstanceFollowRedirects(false);
557
558 // now check for whether we get a redirect response
559 // HTTP Codes 3xx are redirects, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
560 int responseCode = connection.getResponseCode();
561 if(responseCode >= 300 && responseCode < 400) {
562 String responseMsg = connection.getResponseMessage();
563
564 // Get the Location header since this specifies the new location of the resource
565 String location = connection.getHeaderField("Location");
566
567 // this becomes the url that wget should download from
568 url_str = location.trim();
569 }
570
571 connection.disconnect();
572 } catch(Exception e) {
573 if(connection != null) {
574 connection.disconnect();
575 }
576 System.err.println("Checking redirection. Tried to connect to "
577 + url_str + ",\nbut got exception: " + e);
578 }
579 }
580
581 return url_str;
582 }
583
584
585 /** For a string-based Argument whose value has changed, this method
586 * updates the GUI ArgumentControl's value correspondingly. */
587 private void updateArgument(Argument arg, String value) {
588 for(int i = 0; i < options_pane.getComponentCount(); i++) {
589 Component component = options_pane.getComponent(i);
590 if(component instanceof ArgumentControl) {
591 ArgumentControl control = (ArgumentControl)component;
592 if(control.getArgument() == arg) {
593 control.setValue(value);
594 control.repaint();
595 }
596 }
597 }
598 }
599
600 private boolean checkURL(boolean checkRequired){
601
602 if (!updateArguments(checkRequired)){
603 return false;
604 }
605
606 Download current_download = (Download)download_map.get(mode);
607 Argument arg_url = current_download.getArgument("url");
608
609 if (arg_url == null) return true;
610
611 String url_str = arg_url.getValue();
612 URL url = null;
613 try {
614 url = new URL(url_str);
615 }
616 catch(MalformedURLException error) {
617 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Mirroring.Invalid_URL"), Dictionary.get("Mirroring.Invalid_URL_Title"), JOptionPane.ERROR_MESSAGE);
618 return false;
619 }
620
621 return true;
622 }
623
624
625 private boolean checkProxy(){
626
627 proxy_url = null;
628
629 Download current_download = (Download)download_map.get(mode);
630
631 Argument arg = current_download.getArgument("proxy_on");
632
633 if (arg == null) return true;
634
635 // Determine if we have to use a proxy.
636 if(Configuration.get("general.use_proxy", true)) {
637
638 String proxy_host = Configuration.getString("general.proxy_host", true);
639 String proxy_port = Configuration.getString("general.proxy_port", true);
640 // Find out whether the user has already authenticated themselves
641 String user_pass = "";
642 String address = proxy_host + ":" + proxy_port;
643
644 int count = 0;
645 // Only for wget, need to avoid a second automatic authentication popup (first asks
646 // the proxy authentication for wget, and the second will ask the same for the realm)
647 // Once the authentication has been reused, it will set the GAuthenticator state back to REGULAR
648 GAuthenticator.setMode(GAuthenticator.DOWNLOAD);
649 while(count < 3 && (user_pass = (String) GAuthenticator.authentications.get(address)) == null) {
650 Authenticator.requestPasswordAuthentication(proxy_host, null, Integer.parseInt(proxy_port), "http://", Dictionary.get("WGet.Prompt"), "HTTP");
651 count++;
652 }
653 if(count >= 3) {
654 return false;
655 }
656
657 if(user_pass.indexOf("@") != -1) {
658
659 // Write the use proxy command - we don't do this anymore, instead we set environment variables - hopefully these can't be spied on like the following can (using ps) - actually the environment stuff didn't work for windows, so lets go back to this
660 if (Utility.isWindows()) {
661
662 arg.setValue("true");
663 arg.setAssigned(true);
664
665 arg = current_download.getArgument("proxy_host");
666 arg.setValue(proxy_host);
667 arg.setAssigned(true);
668
669 arg = current_download.getArgument("proxy_port");
670 arg.setValue(proxy_port);
671 arg.setAssigned(true);
672
673 arg = current_download.getArgument("user_name");
674 arg.setValue(user_pass.substring(0, user_pass.indexOf("@")));
675 arg.setAssigned(true);
676
677 arg = current_download.getArgument("user_password");
678 arg.setValue(user_pass.substring(user_pass.indexOf("@") + 1));
679 arg.setAssigned(true);
680 }
681 else{
682 String user_name = user_pass.substring(0, user_pass.indexOf("@"));
683 String user_pwd = user_pass.substring(user_pass.indexOf("@") + 1);
684 proxy_url = user_name+":"+user_pwd+"@"+proxy_host+":"+proxy_port+"/";
685
686 }
687
688 return true;
689 }
690 else{
691 return false;
692 }
693
694 }
695
696 return true;
697 }
698
699 /*
700 private class PreferencesButtonActionListener
701 implements ActionListener {
702 public void actionPerformed(ActionEvent event) {
703 new Preferences(Preferences.CONNECTION_PREFS);
704 }
705 }*/
706
707 private class InformationButtonActionListener
708 implements ActionListener {
709 public void actionPerformed(ActionEvent event) {
710 //turn off the check for find argument
711 Download current_download = (Download)download_map.get(mode);
712
713 if (!checkProxy() || !checkURL(false) )return;
714
715
716 if(server_info != null) {
717 server_info.dispose();
718 }
719
720
721 Argument arg_url = current_download.getArgument("url");
722 String str_url = "";
723
724 if( arg_url!= null && arg_url.isAssigned()) {
725 str_url = arg_url.getValue();
726 }
727
728
729 server_info = new ServerInfoDialog(str_url ,proxy_url, mode,(Download)download_map.get(mode));
730
731 }
732 }
733
734 private class PreferencesButtonActionListener
735 implements ActionListener {
736 public void actionPerformed(ActionEvent event) {
737 new Preferences(Preferences.CONNECTION_PREFS);
738 }
739 }
740}
Note: See TracBrowser for help on using the repository browser.