/**
  * Change a playlist name.
  *
  * @param plfOld DOCUMENT_ME
  * @param sNewName DOCUMENT_ME
  * @return new playlist
  * @throws JajukException the jajuk exception
  */
 public synchronized Playlist changePlaylistFileName(Playlist plfOld, String sNewName)
     throws JajukException {
   // check given name is different
   if (plfOld.getName().equals(sNewName)) {
     return plfOld;
   }
   // check if this file still exists
   if (!plfOld.getFIO().exists()) {
     throw new JajukException(135);
   }
   java.io.File ioNew =
       new java.io.File(
           plfOld.getFIO().getParentFile().getAbsolutePath() + java.io.File.separator + sNewName);
   // recalculate file ID
   plfOld.getDirectory();
   String sNewId = PlaylistManager.createID(sNewName, plfOld.getDirectory());
   // create a new playlist (with own fio and sAbs)
   Playlist plfNew = new Playlist(sNewId, sNewName, plfOld.getDirectory());
   // Transfer all properties (id and name)
   // We use a shallow copy of properties to avoid any properties share between
   // two items
   plfNew.setProperties(plfOld.getShallowProperties());
   plfNew.setProperty(Const.XML_ID, sNewId); // reset new id and name
   plfNew.setProperty(Const.XML_NAME, sNewName); // reset new id and name
   // check file name and extension
   if (plfNew.getName().lastIndexOf('.') != plfNew.getName().indexOf('.') // just
       // one
       // '.'
       || !(UtilSystem.getExtension(ioNew).equals(Const.EXT_PLAYLIST))) { // check
     // extension
     Messages.showErrorMessage(134);
     throw new JajukException(134);
   }
   // check if future file exists (under windows, file.exists
   // return true even with different case so we test file name is
   // different)
   if (!ioNew.getName().equalsIgnoreCase(plfOld.getName()) && ioNew.exists()) {
     throw new JajukException(134);
   }
   // try to rename file on disk
   try {
     boolean result = plfOld.getFIO().renameTo(ioNew);
     if (!result) {
       throw new IOException();
     }
   } catch (Exception e) {
     throw new JajukException(134, e);
   }
   // OK, remove old file and register this new file
   removeItem(plfOld);
   registerItem(plfNew);
   return plfNew;
 }
 /**
  * Scan recursively.
  *
  * @param dir top directory to scan
  * @param bDeepScan whether we want to perform a deep scan (read tags again)
  */
 private void scanRecursively(final Directory dir, final boolean bDeepScan) {
   dir.scan(bDeepScan, reporter);
   if (reporter != null) {
     reporter.updateState(dir);
   }
   final File[] files = dir.getFio().listFiles(UtilSystem.getDirFilter());
   if (files != null) {
     for (final File element : files) {
       // Leave ASAP if exit request
       if (ExitService.isExiting()) {
         return;
       }
       final Directory subDir =
           DirectoryManager.getInstance().registerDirectory(element.getName(), dir, this);
       scanRecursively(subDir, bDeepScan);
     }
   }
 }
 /**
  * Synchronize a device with another one (unidirectional).
  *
  * @param dSrc
  * @param dest
  * @return nb of created files
  */
 private int synchronizeUnidirectonal(final Device dSrc, final Device dest) {
   final Set<Directory> hsSourceDirs = new HashSet<Directory>(100);
   // contains paths ( relative to device) of desynchronized dirs
   final Set<String> hsDesynchroPaths = new HashSet<String>(10);
   final Set<Directory> hsDestDirs = new HashSet<Directory>(100);
   int iNbCreatedFiles = 0;
   List<Directory> dirs = DirectoryManager.getInstance().getDirectories();
   for (Directory dir : dirs) {
     if (dir.getDevice().equals(dSrc)) {
       // don't take desynchronized dirs into account
       if (dir.getBooleanValue(Const.XML_DIRECTORY_SYNCHRONIZED)) {
         hsSourceDirs.add(dir);
       } else {
         hsDesynchroPaths.add(dir.getRelativePath());
       }
     }
   }
   for (Directory dir : dirs) {
     if (dir.getDevice().equals(dest)) {
       if (dir.getBooleanValue(Const.XML_DIRECTORY_SYNCHRONIZED)) {
         // don't take desynchronized dirs into account
         hsDestDirs.add(dir);
       } else {
         hsDesynchroPaths.add(dir.getRelativePath());
       }
     }
   }
   // handle known extensions and image files
   final FileFilter filter =
       new JajukFileFilter(
           false,
           new JajukFileFilter[] {KnownTypeFilter.getInstance(), ImageFilter.getInstance()});
   for (Directory dir : hsSourceDirs) {
     // give a chance to exit during sync
     if (ExitService.isExiting()) {
       return iNbCreatedFiles;
     }
     boolean bNeedCreate = true;
     final String sPath = dir.getRelativePath();
     // check the directory on source is not desynchronized. If it
     // is, leave without checking files
     if (hsDesynchroPaths.contains(sPath)) {
       continue;
     }
     for (Directory dir2 : hsDestDirs) {
       if (dir2.getRelativePath().equals(sPath)) {
         // directory already exists on this device
         bNeedCreate = false;
         break;
       }
     }
     // create it if needed
     final File fileNewDir = new File(new StringBuilder(dest.getUrl()).append(sPath).toString());
     if (bNeedCreate && !fileNewDir.mkdirs()) {
       Log.warn("Could not create directory " + fileNewDir);
     }
     // synchronize files
     final File fileSrc = new File(new StringBuilder(dSrc.getUrl()).append(sPath).toString());
     final File[] fSrcFiles = fileSrc.listFiles(filter);
     if (fSrcFiles != null) {
       for (final File element : fSrcFiles) {
         File[] filesArray = fileNewDir.listFiles(filter);
         if (filesArray == null) {
           // fileNewDir is not a directory or an error occurred (
           // read/write right ? )
           continue;
         }
         final List<File> files = Arrays.asList(filesArray);
         // Sort so files are copied in the filesystem order
         Collections.sort(files);
         boolean bNeedCopy = true;
         for (final File element2 : files) {
           if (element.getName().equalsIgnoreCase(element2.getName())) {
             bNeedCopy = false;
           }
         }
         if (bNeedCopy) {
           try {
             UtilSystem.copyToDir(element, fileNewDir);
             iNbCreatedFiles++;
             lVolume += element.length();
             InformationJPanel.getInstance()
                 .setMessage(
                     new StringBuilder(Messages.getString("Device.41"))
                         .append(dSrc.getName())
                         .append(',')
                         .append(dest.getName())
                         .append(Messages.getString("Device.42"))
                         .append(element.getAbsolutePath())
                         .append("]")
                         .toString(),
                     InformationJPanel.MessageType.INFORMATIVE);
           } catch (final JajukException je) {
             Messages.showErrorMessage(je.getCode(), element.getAbsolutePath());
             Messages.showErrorMessage(27);
             Log.error(je);
             return iNbCreatedFiles;
           } catch (final Exception e) {
             Messages.showErrorMessage(20, element.getAbsolutePath());
             Messages.showErrorMessage(27);
             Log.error(20, "{{" + element.getAbsolutePath() + "}}", e);
             return iNbCreatedFiles;
           }
         }
       }
     }
   }
   return iNbCreatedFiles;
 }