Browse Source

Moves extraction code for plugins to resource manager (removes duplicated code)

Change-Id: I425945ec03dcddcdfe49b66956e5b664597daaba
Reviewed-on: http://gerrit.dmdirc.com/441
Tested-by: Gregory Holmes <greboid@dmdirc.com>
Reviewed-by: Shane Mc Cormack <shane@dmdirc.com>
tags/0.6.3
Gregory Holmes 14 years ago
parent
commit
42929da886

+ 6
- 30
src/com/dmdirc/addons/freedesktop_notifications/FreeDesktopNotificationsPlugin.java View File

31
 import com.dmdirc.Main;
31
 import com.dmdirc.Main;
32
 import java.io.File;
32
 import java.io.File;
33
 import java.io.IOException;
33
 import java.io.IOException;
34
-import java.util.Map;
35
-import java.util.Map.Entry;
36
 import java.util.ArrayList;
34
 import java.util.ArrayList;
37
 import com.dmdirc.commandparser.CommandManager;
35
 import com.dmdirc.commandparser.CommandManager;
38
 import com.dmdirc.installer.StreamReader;
36
 import com.dmdirc.installer.StreamReader;
157
                 }
155
                 }
158
             
156
             
159
                 // Now extract the files needed
157
                 // Now extract the files needed
160
-                extractFiles(res, newDir, ".py");
161
-                extractFiles(res, newDir, ".png");
158
+                try {
159
+                    res.extractResoucesEndingWith(newDir, ".py");
160
+                    res.extractResoucesEndingWith(newDir, ".png");
161
+                } catch (IOException ex) {
162
+                    Logger.userError(ErrorLevel.MEDIUM, "Unable to extract files for Free desktop notifications: " + ex.getMessage(), ex);
163
+                }
162
             } catch (IOException ioe) {
164
             } catch (IOException ioe) {
163
                 Logger.userError(ErrorLevel.LOW, "Unable to open ResourceManager for freedesktop_notifications: "+ioe.getMessage(), ioe);
165
                 Logger.userError(ErrorLevel.LOW, "Unable to open ResourceManager for freedesktop_notifications: "+ioe.getMessage(), ioe);
164
             }
166
             }
165
         }
167
         }
166
     }
168
     }
167
-    
168
-    /**
169
-     * Use the given resource manager to extract files ending with the given suffix
170
-     *
171
-     * @param res ResourceManager
172
-     * @param newDir Directory to extract to.
173
-     * @param suffix Suffix to extract
174
-     */
175
-    private static void extractFiles(final ResourceManager res, final File newDir, final String suffix) {
176
-        final Map<String, byte[]> resources = res.getResourcesEndingWithAsBytes(suffix);
177
-        for (Entry<String, byte[]> resource : resources.entrySet()) {
178
-            try {
179
-                final String key = resource.getKey();
180
-                final String resourceName = key.substring(key.lastIndexOf('/'), key.length());
181
-
182
-                final File newFile = new File(newDir, resourceName);
183
-
184
-                if (!newFile.isDirectory()) {
185
-                    if (newFile.exists()) { newFile.delete(); }
186
-                    ResourceManager.getResourceManager().resourceToFile(resource.getValue(), newFile);
187
-                }
188
-            } catch (IOException ex) {
189
-                Logger.userError(ErrorLevel.LOW, "Failed to extract "+suffix+"s for freedesktop_notifications: "+ex.getMessage(), ex);
190
-            }
191
-        }
192
-    }
193
 
169
 
194
     /**
170
     /**
195
      * Called when this plugin is Unloaded.
171
      * Called when this plugin is Unloaded.

+ 6
- 32
src/com/dmdirc/addons/mediasource_windows/WindowsMediaSourcePlugin.java View File

34
 
34
 
35
 import java.util.ArrayList;
35
 import java.util.ArrayList;
36
 import java.util.List;
36
 import java.util.List;
37
-import java.util.Map;
38
-import java.util.Map.Entry;
39
 import java.io.File;
37
 import java.io.File;
40
 import java.io.IOException;
38
 import java.io.IOException;
41
 
39
 
94
         return new MediaInfoOutput(-1, "Error executing GetMediaInfo.exe");
92
         return new MediaInfoOutput(-1, "Error executing GetMediaInfo.exe");
95
     }
93
     }
96
 
94
 
97
-    /**
98
-     * Use the given resource manager to extract files ending with the given suffix
99
-     *
100
-     * @param res ResourceManager
101
-     * @param newDir Directory to extract to.
102
-     * @param suffix Suffix to extract
103
-     */
104
-    private void extractFiles(final ResourceManager res, final File newDir, final String suffix) {
105
-        final Map<String, byte[]> resources = res.getResourcesEndingWithAsBytes(suffix);
106
-        for (Entry<String, byte[]> resource : resources.entrySet()) {
107
-            try {
108
-                final String key = resource.getKey();
109
-                final String resourceName = key.substring(key.lastIndexOf('/'), key.length());
110
-
111
-                final File newFile = new File(newDir, resourceName);
112
-
113
-                if (!newFile.isDirectory()) {
114
-                    if (newFile.exists()) {
115
-                        newFile.delete();
116
-                    }
117
-                    ResourceManager.getResourceManager().resourceToFile(resource.getValue(), newFile);
118
-                }
119
-            } catch (IOException ex) {
120
-                Logger.userError(ErrorLevel.LOW, "Failed to extract " + suffix + "s for windowsmediasource: " + ex.getMessage(), ex);
121
-            }
122
-        }
123
-    }
124
-
125
     /** {@inheritDoc} */
95
     /** {@inheritDoc} */
126
     @Override
96
     @Override
127
     public void onLoad() {
97
     public void onLoad() {
144
             }
114
             }
145
 
115
 
146
             // Now extract the .dlls and .exe
116
             // Now extract the .dlls and .exe
147
-            extractFiles(res, newDir, ".dll");
148
-            extractFiles(res, newDir, ".exe");
117
+            try {
118
+                res.extractResoucesEndingWith(newDir, ".dll");
119
+                res.extractResoucesEndingWith(newDir, ".exe");
120
+            } catch (IOException ex) {
121
+                Logger.userError(ErrorLevel.MEDIUM, "Unable to extract files for windows media source: " + ex.getMessage(), ex);
122
+            }
149
         } catch (IOException ioe) {
123
         } catch (IOException ioe) {
150
             Logger.userError(ErrorLevel.LOW, "Unable to open ResourceManager for windowsmediasource: " + ioe.getMessage(), ioe);
124
             Logger.userError(ErrorLevel.LOW, "Unable to open ResourceManager for windowsmediasource: " + ioe.getMessage(), ioe);
151
         }
125
         }

Loading…
Cancel
Save