()
| 2330 | } |
| 2331 | |
| 2332 | public void handleAddLibrary() { |
| 2333 | JFileChooser fileChooser = new JFileChooser(System.getProperty("user.home")); |
| 2334 | fileChooser.setDialogTitle(tr("Select a zip file or a folder containing the library you'd like to add")); |
| 2335 | fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); |
| 2336 | fileChooser.setFileFilter(new FileNameExtensionFilter(tr("ZIP files or folders"), "zip")); |
| 2337 | |
| 2338 | Dimension preferredSize = fileChooser.getPreferredSize(); |
| 2339 | fileChooser.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200)); |
| 2340 | |
| 2341 | int returnVal = fileChooser.showOpenDialog(activeEditor); |
| 2342 | |
| 2343 | if (returnVal != JFileChooser.APPROVE_OPTION) { |
| 2344 | return; |
| 2345 | } |
| 2346 | |
| 2347 | File sourceFile = fileChooser.getSelectedFile(); |
| 2348 | File tmpFolder = null; |
| 2349 | |
| 2350 | try { |
| 2351 | // unpack ZIP |
| 2352 | if (!sourceFile.isDirectory()) { |
| 2353 | try { |
| 2354 | tmpFolder = FileUtils.createTempFolder(); |
| 2355 | ZipDeflater zipDeflater = new ZipDeflater(sourceFile, tmpFolder); |
| 2356 | zipDeflater.deflate(); |
| 2357 | File[] foldersInTmpFolder = tmpFolder.listFiles(new OnlyDirs()); |
| 2358 | if (foldersInTmpFolder.length != 1) { |
| 2359 | throw new IOException(tr("Zip doesn't contain a library")); |
| 2360 | } |
| 2361 | sourceFile = foldersInTmpFolder[0]; |
| 2362 | } catch (IOException e) { |
| 2363 | activeEditor.statusError(e); |
| 2364 | return; |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | File libFolder = sourceFile; |
| 2369 | if (FileUtils.isSubDirectory(new File(PreferencesData.get("sketchbook.path")), libFolder)) { |
| 2370 | activeEditor.statusError(tr("A subfolder of your sketchbook is not a valid library")); |
| 2371 | return; |
| 2372 | } |
| 2373 | |
| 2374 | if (FileUtils.isSubDirectory(libFolder, new File(PreferencesData.get("sketchbook.path")))) { |
| 2375 | activeEditor.statusError(tr("You can't import a folder that contains your sketchbook")); |
| 2376 | return; |
| 2377 | } |
| 2378 | |
| 2379 | String libName = libFolder.getName(); |
| 2380 | if (!BaseNoGui.isSanitaryName(libName)) { |
| 2381 | String mess = format(tr("The library \"{0}\" cannot be used.\n" |
| 2382 | + "Library names must contain only basic letters and numbers.\n" |
| 2383 | + "(ASCII only and no spaces, and it cannot start with a number)"), |
| 2384 | libName); |
| 2385 | activeEditor.statusError(mess); |
| 2386 | return; |
| 2387 | } |
| 2388 | |
| 2389 | String[] headers; |
no test coverage detected