Browse Source

More work on URL handling

git-svn-id: http://svn.dmdirc.com/trunk@2705 00569f92-eb28-0410-84fd-f71c24880f
tags/0.5.5
Gregory Holmes 16 years ago
parent
commit
fc6742e774

+ 2
- 0
src/com/dmdirc/commandline/CommandLineParser.java View File

@@ -22,6 +22,8 @@
22 22
 
23 23
 package com.dmdirc.commandline;
24 24
 
25
+import com.dmdirc.util.InvalidAddressException;
26
+import com.dmdirc.util.IrcAddress;
25 27
 import com.dmdirc.Main;
26 28
 import com.dmdirc.Server;
27 29
 import com.dmdirc.config.IdentityManager;

+ 4
- 0
src/com/dmdirc/config/defaults/default/defaults View File

@@ -130,3 +130,7 @@ actions.highlightcolour=4
130 130
 
131 131
 popups.nick=Query:query %1$s\nWhois:whois %1$s
132 132
 popups.chan-nick=-\nBan:ban %1$s\nKick:kick %1$s
133
+
134
+protocol.http=BROWSER
135
+protocol.https=BROWSER
136
+protocol.irc=DMDIRC

+ 1
- 1
src/com/dmdirc/ui/dummy/DummyController.java View File

@@ -158,7 +158,7 @@ public final class DummyController implements UIController {
158 158
 
159 159
     /** {@inheritDoc} */
160 160
     @Override
161
-    public void showURLDialog(String procotol, String url) {
161
+    public void showURLDialog(String protocol, String url) {
162 162
         throw new UnsupportedOperationException("Not supported yet.");
163 163
     }
164 164
     

+ 2
- 2
src/com/dmdirc/ui/interfaces/UIController.java View File

@@ -161,9 +161,9 @@ public interface UIController {
161 161
     /**
162 162
      * Shows the unknown URL protocol handling dialog for a URL.
163 163
      * 
164
-     * @param procotol URL Protocol
164
+     * @param protocol URL Protocol
165 165
      * @param url full url
166 166
      */
167
-    void showURLDialog(final String procotol, final String url);
167
+    void showURLDialog(final String protocol, final String url);
168 168
     
169 169
 }

+ 2
- 2
src/com/dmdirc/ui/swing/SwingController.java View File

@@ -45,6 +45,7 @@ import com.dmdirc.ui.interfaces.Window;
45 45
 import com.dmdirc.ui.swing.components.SwingPreferencesPanel;
46 46
 import com.dmdirc.ui.swing.components.SwingStatusBar;
47 47
 import com.dmdirc.ui.swing.dialogs.SwingUpdaterDialog;
48
+import com.dmdirc.ui.swing.dialogs.URLDialog;
48 49
 import com.dmdirc.ui.swing.dialogs.channelsetting.ChannelSettingsDialog;
49 50
 import com.dmdirc.ui.swing.dialogs.error.ErrorListDialog;
50 51
 import com.dmdirc.ui.swing.dialogs.wizard.firstrun.SwingFirstRunWizard;
@@ -268,7 +269,6 @@ public final class SwingController implements UIController {
268 269
     /** {@inheritDoc} */
269 270
     @Override
270 271
     public void showURLDialog(String protocol, String url) {
271
-        System.out.println("Protocol: " + protocol);
272
-        System.out.println("URL: " + url);
272
+        URLDialog.showURLDialog(protocol, url);
273 273
     }
274 274
 }

+ 52
- 0
src/com/dmdirc/ui/swing/components/ExecutableFileFilter.java View File

@@ -0,0 +1,52 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.ui.swing.components;
24
+
25
+import java.io.File;
26
+import javax.swing.filechooser.FileFilter;
27
+
28
+/**
29
+ * Filters non executable files.
30
+ */
31
+public class ExecutableFileFilter extends FileFilter {
32
+
33
+    /**
34
+     * {@inheritDoc}
35
+     * 
36
+     * @param file File to check
37
+     */
38
+    @Override
39
+    public boolean accept(final File file) {
40
+        return (file.isDirectory() || file.canExecute());
41
+    }
42
+
43
+    /** 
44
+     * {@inheritDoc}
45
+     * 
46
+     * @return Description
47
+     */
48
+    @Override
49
+    public String getDescription() {
50
+        return "Executable files";
51
+    }
52
+}

+ 3
- 3
src/com/dmdirc/ui/swing/components/Frame.java View File

@@ -588,9 +588,9 @@ public abstract class Frame extends JInternalFrame implements Window,
588 588
         if (event.isPopupTrigger()) {
589 589
             //Show hyperlink popup
590 590
         } else {
591
-            Main.getUI().getStatusBar().setMessage("Opening: " + url);
592
-            BrowserLauncher.openURL(url);
593
-            //URLHandler.getURLHander().launchApp(url);
591
+            //Main.getUI().getStatusBar().setMessage("Opening: " + url);
592
+            //BrowserLauncher.openURL(url);
593
+            URLHandler.getURLHander().launchApp(url);
594 594
         }
595 595
     }
596 596
     

+ 167
- 0
src/com/dmdirc/ui/swing/dialogs/URLDialog.java View File

@@ -0,0 +1,167 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.ui.swing.dialogs;
24
+
25
+import com.dmdirc.Main;
26
+import com.dmdirc.ui.swing.MainFrame;
27
+import com.dmdirc.ui.swing.components.ExecutableFileFilter;
28
+import com.dmdirc.ui.swing.components.StandardDialog;
29
+import java.awt.event.ActionEvent;
30
+import java.awt.event.ActionListener;
31
+import javax.swing.JButton;
32
+import javax.swing.JFileChooser;
33
+import javax.swing.JLabel;
34
+import net.miginfocom.swing.MigLayout;
35
+
36
+/** URL Protocol dialog. */
37
+public class URLDialog extends StandardDialog implements ActionListener {
38
+
39
+    /**
40
+     * A version number for this class. It should be changed whenever the class
41
+     * structure is changed (or anything else that would prevent serialized
42
+     * objects being unserialized with the new class).
43
+     */
44
+    private static final long serialVersionUID = 1;
45
+    /** A previously created instance of URLDialog. */
46
+    private static URLDialog me;
47
+    /** File chooser. */
48
+    private JFileChooser fileChooser;
49
+    /** Show file chooser. */
50
+    private JButton showFileChooser;
51
+    /** Info label. */
52
+    private JLabel infoLabel;
53
+    /** Protocol. */
54
+    private final String protocol;
55
+    /** URL. */
56
+    private final String url;
57
+
58
+    /**
59
+     * Instantiates the URLDialog.
60
+     *
61
+     * @param protocol Protocol to add
62
+     * @param url URL to open once added
63
+     */
64
+    private URLDialog(final String protocol, final String url) {
65
+        super((MainFrame) Main.getUI().getMainWindow(), false);
66
+
67
+        this.protocol = protocol;
68
+        this.url = url;
69
+        
70
+        initComponents();
71
+        layoutComponents();
72
+        addListeners();
73
+
74
+        setTitle("DMDirc: Unknown URL Protocol");
75
+
76
+        pack();
77
+    }
78
+
79
+    /**
80
+     * Creates the new URLDialog if one doesn't exist, and displays it.
81
+     *
82
+     * @param protocol Protocol to add
83
+     * @param url URL to open once added
84
+     */
85
+    public static synchronized void showURLDialog(final String protocol,
86
+            final String url) {
87
+        me = getURLDialog(protocol, url);
88
+
89
+        me.setLocationRelativeTo((MainFrame) Main.getUI().getMainWindow());
90
+        me.setVisible(true);
91
+        me.requestFocus();
92
+    }
93
+
94
+    /**
95
+     * Returns the current instance of the URLDialog.
96
+     *
97
+     * @param protocol Protocol to add
98
+     * @param url URL to open once added
99
+     *
100
+     * @return The current URLDialog instance
101
+     */
102
+    public static synchronized URLDialog getURLDialog(final String protocol,
103
+            final String url) {
104
+        if (me == null) {
105
+            me = new URLDialog(protocol, url);
106
+        }
107
+
108
+        return me;
109
+    }
110
+
111
+    /** Initialises the components. */
112
+    private void initComponents() {
113
+        orderButtons(new JButton(), new JButton());
114
+        fileChooser = new JFileChooser();
115
+        showFileChooser = new JButton();
116
+        infoLabel = new JLabel();
117
+        
118
+        fileChooser.addChoosableFileFilter(new ExecutableFileFilter());
119
+        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
120
+        showFileChooser.setText("Browse");
121
+        infoLabel.setText("/set protocol " + protocol + " [DMDIRC|BROWSER|MAIL|some kind of list]");
122
+    }
123
+
124
+    /** Lays out the components. */
125
+    private void layoutComponents() {
126
+        setLayout(new MigLayout("fill"));
127
+        
128
+        add(infoLabel, "grow");
129
+    }
130
+
131
+    /** Adds listeners to the components. */
132
+    private void addListeners() {
133
+        getOkButton().addActionListener(this);
134
+        getCancelButton().addActionListener(this);
135
+        showFileChooser.addActionListener(this);
136
+    }
137
+
138
+    /** Saves the settings. */
139
+    private void save() {
140
+        dispose();
141
+    }
142
+
143
+    /**
144
+     * {@inheritDoc}
145
+     *
146
+     * @param e action event
147
+     */
148
+    @Override
149
+    public void actionPerformed(final ActionEvent e) {
150
+        if (e.getSource() == getOkButton()) {
151
+            save();
152
+        } else if (e.getSource() == getCancelButton()) {
153
+            dispose();
154
+        } else if (e.getSource() == showFileChooser) {
155
+            fileChooser.showDialog(this, "OK");
156
+        }
157
+    }
158
+
159
+    /** {@inheritDoc} */
160
+    @Override
161
+    public void dispose() {
162
+        synchronized (me) {
163
+            super.dispose();
164
+            me = null;
165
+        }
166
+    }
167
+}

src/com/dmdirc/commandline/InvalidAddressException.java → src/com/dmdirc/util/InvalidAddressException.java View File

@@ -20,7 +20,7 @@
20 20
  * SOFTWARE.
21 21
  */
22 22
 
23
-package com.dmdirc.commandline;
23
+package com.dmdirc.util;
24 24
 
25 25
 /**
26 26
  * Thrown to indicate that an invalid IRC address has been used.

+ 185
- 0
src/com/dmdirc/util/IrcAddress.java View File

@@ -0,0 +1,185 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util;
24
+
25
+import java.util.ArrayList;
26
+import java.util.List;
27
+
28
+/**
29
+ * Parses an IRC address. IRC addresses take the following form:
30
+ * irc[s]://[[username][:password]@]<server>[:[+]port][/channel1[,channel2[,...]]]
31
+ * 
32
+ * @author Chris
33
+ */
34
+public class IrcAddress {
35
+    
36
+    /** Whether or not this address uses SSL. */
37
+    private boolean usesSSL;
38
+    
39
+    /** The server name for this address. */
40
+    private String server;
41
+    
42
+    /** The port number for this address. */
43
+    private int port = 6667;
44
+    
45
+    /** A list of channels to auto-connect to. */
46
+    private List<String> channels = new ArrayList<String>();
47
+    
48
+    /** The password for this address. */
49
+    private String pass = "";
50
+
51
+    /**
52
+     * Creates a new instance of IrcAddress.
53
+     * 
54
+     * @param address The address to parse
55
+     * @throws InvalidAddressException If an invalid address is passed
56
+     */
57
+    public IrcAddress(final String address) throws InvalidAddressException {
58
+        StringBuilder builder;
59
+        
60
+        if (address.toLowerCase().startsWith("ircs://")) {
61
+            usesSSL = true;
62
+            builder = new StringBuilder(address.substring(7));
63
+        } else if (address.toLowerCase().startsWith("irc://")) {
64
+            usesSSL = false;
65
+            builder = new StringBuilder(address.substring(6));
66
+        } else {
67
+            throw new InvalidAddressException("Invalid protocol specified");
68
+        }
69
+        
70
+        final int atIndex = builder.indexOf("@");
71
+        if (atIndex > -1) {
72
+            doPass(builder.substring(0, atIndex));
73
+            builder.delete(0, atIndex + 1);
74
+        }
75
+        
76
+        final int slashIndex = builder.indexOf("/");
77
+        if (slashIndex > -1) {
78
+            doChannels(builder.substring(slashIndex + 1));
79
+            builder.delete(slashIndex, builder.length());
80
+        }
81
+        
82
+        final int colonIndex = builder.indexOf(":");
83
+        if (colonIndex > -1) {
84
+            doPort(builder.substring(colonIndex + 1));
85
+            builder.delete(colonIndex, builder.length());
86
+        }
87
+        
88
+        doServer(builder.toString());
89
+    }
90
+    
91
+    /**
92
+     * Processes the password part of this address.
93
+     *
94
+     * @param pass The password part of this address
95
+     */
96
+    private void doPass(final String pass) {
97
+        this.pass = pass;
98
+    }
99
+
100
+    /**
101
+     * Processes the channels part of this address.
102
+     *
103
+     * @param channels The channels part of this address
104
+     */    
105
+    private void doChannels(final String channels) {
106
+        for (String channel : channels.split(",")) {
107
+            this.channels.add(channel);
108
+        }
109
+    }
110
+    
111
+    /**
112
+     * Processes the port part of this address.
113
+     *
114
+     * @param port The port part of this address
115
+     * @throws InvalidAddressException if the port is non-numeric
116
+     */    
117
+    private void doPort(final String port) throws InvalidAddressException {
118
+        String actualPort = port;
119
+        
120
+        if (port.charAt(0) == '+') {
121
+            usesSSL = true;
122
+            actualPort = port.substring(1);
123
+        }
124
+        
125
+        try {
126
+            this.port = Integer.valueOf(actualPort);
127
+        } catch (NumberFormatException ex) {
128
+            throw new InvalidAddressException("Invalid port number", ex);
129
+        }
130
+    }
131
+    
132
+    /**
133
+     * Processes the server part of this address.
134
+     *
135
+     * @param server The server part of this address
136
+     */    
137
+    private void doServer(final String server) {
138
+        this.server = server;
139
+    }
140
+    
141
+    /**
142
+     * Determines if this address requires the use of SSL or not.
143
+     *
144
+     * @return True if the address requires SSL, false otherwise
145
+     */
146
+    public boolean isSSL() {
147
+        return usesSSL;
148
+    }
149
+    
150
+    /**
151
+     * Retrieves the server from this address.
152
+     *
153
+     * @return This address's server
154
+     */
155
+    public String getServer() {
156
+        return server;
157
+    }
158
+    
159
+    /**
160
+     * Retrieves the port used for this address.
161
+     *
162
+     * @return This address's port
163
+     */
164
+    public int getPort() {
165
+        return port;
166
+    }
167
+    
168
+    /**
169
+     * Retrieves the password used for this address.
170
+     *
171
+     * @return This address's password
172
+     */
173
+    public String getPassword() {
174
+        return pass;
175
+    }
176
+    
177
+    /**
178
+     * Retrieves the list of channels for this address.
179
+     *
180
+     * @return This address's channels
181
+     */
182
+    public List<String> getChannels() {
183
+        return channels;
184
+    }
185
+}

+ 32
- 30
src/com/dmdirc/util/URLHandler.java View File

@@ -27,6 +27,7 @@ import com.dmdirc.config.ConfigManager;
27 27
 import com.dmdirc.config.IdentityManager;
28 28
 import com.dmdirc.logger.ErrorLevel;
29 29
 import com.dmdirc.logger.Logger;
30
+
30 31
 import java.awt.Desktop;
31 32
 import java.io.IOException;
32 33
 import java.net.URI;
@@ -73,37 +74,39 @@ public class URLHandler {
73 74
         final int end = url.indexOf(':');
74 75
         final String protocol = url.substring(0, end);
75 76
         final String info = url.substring(end + 3);
76
-        
77
-        System.out.println("URL: " + url + " -> " + protocol + " -> " + info);
78
-        
77
+
79 78
         if (!config.hasOption("protocol", protocol)) {
80 79
             Main.getUI().showURLDialog(protocol, info);
81 80
             return;
82 81
         }
83 82
 
84 83
         final List<String> app = config.getOptionList("protocol", protocol);
85
-        
84
+
86 85
         if (app.size() < 1) {
87 86
             Main.getUI().showURLDialog(protocol, info);
88 87
             return;
89 88
         }
90
-        
89
+
91 90
         final String command = app.get(0);
92
-        
93
-        if ("DMDIRC".equals(command)) {
94
-            System.out.println("handling IRC link internally.");
95
-        }else if ("BROWSER".equals(command)) {
96
-            execBrowser(url);
97
-        } else if ("MAIL".equals(command)) {
98
-            execMail(url);
99
-        } else {
100
-            execApp(app.toArray(new String[app.size()]));
91
+
92
+        try {
93
+            if ("DMDIRC".equals(command)) {
94
+                //Handle DMDirc link
95
+            } else if ("BROWSER".equals(command)) {
96
+                execBrowser(new URI(url));
97
+            } else if ("MAIL".equals(command)) {
98
+                execMail(new URI(url));
99
+            } else {
100
+                execApp(app.toArray(new String[app.size()]));
101
+            }
102
+        } catch (URISyntaxException ex) {
103
+            Logger.userError(ErrorLevel.LOW, "Invalid URL: " + ex.getMessage());
101 104
         }
102 105
     }
103 106
 
104 107
     /**
105 108
      * Launches an application.
106
-     * 
109
+     *
107 110
      * @param application Application and arguments
108 111
      */
109 112
     private void execApp(final String[] application) {
@@ -114,39 +117,38 @@ public class URLHandler {
114 117
                     "Unable to run application: " + ex.getMessage(), ex);
115 118
         }
116 119
     }
117
-    
120
+
118 121
     /**
119 122
      * Opens the specified URL in the users browser.
120
-     * 
123
+     *
121 124
      * @param url URL to open
122 125
      */
123
-    private void execBrowser(final String url) {
124
-        if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
126
+    private void execBrowser(final URI url) {
127
+        if (desktop != null &&
128
+                desktop.isSupported(Desktop.Action.BROWSE)) {
125 129
             try {
126
-                desktop.browse(new URI(url));
130
+                desktop.browse(url);
127 131
             } catch (IOException ex) {
128
-                Logger.userError(ErrorLevel.LOW, "Unable to open URL: " + ex.getMessage());
129
-            } catch (URISyntaxException ex) {
130
-                Logger.userError(ErrorLevel.LOW, "Invalid URL: " + ex.getMessage());
132
+                Logger.userError(ErrorLevel.LOW,
133
+                        "Unable to open URL: " + ex.getMessage());
131 134
             }
132 135
         } else {
133 136
             Logger.userError(ErrorLevel.LOW, "Unable to open your browser.");
134 137
         }
135 138
     }
136
-    
139
+
137 140
     /**
138 141
      * Opens the specified URL in the users mail client.
139
-     * 
142
+     *
140 143
      * @param url URL to open
141 144
      */
142
-    private void execMail(final String url) {
145
+    private void execMail(final URI url) {
143 146
         if (desktop != null && desktop.isSupported(Desktop.Action.MAIL)) {
144 147
             try {
145
-                desktop.mail(new URI(url));
148
+                desktop.mail(url);
146 149
             } catch (IOException ex) {
147
-                Logger.userError(ErrorLevel.LOW, "Unable to open URL: " + ex.getMessage());
148
-            } catch (URISyntaxException ex) {
149
-                Logger.userError(ErrorLevel.LOW, "Invalid URL: " + ex.getMessage());
150
+                Logger.userError(ErrorLevel.LOW,
151
+                        "Unable to open URL: " + ex.getMessage());
150 152
             }
151 153
         } else {
152 154
             Logger.userError(ErrorLevel.LOW, "Unable to open your mail client.");

Loading…
Cancel
Save