/**
  * @param sName
  * @param dParentDirectory
  * @return ItemManager ID
  */
 protected static String createID(String sName, Directory dParentDirectory) {
   return MD5Processor.hash(
       new StringBuilder(dParentDirectory.getDevice().getName())
           .append(dParentDirectory.getRelativePath())
           .append(sName)
           .toString());
 }
 /**
  * Walks through all directories and removes the ones for this device.
  *
  * @param dirsToRefresh list of the directory to refresh, null if all of them
  * @return true if there was any directory removed
  */
 private boolean cleanDirectories(List<Directory> dirsToRefresh) {
   boolean bChanges = false;
   List<Directory> dirs = null;
   if (dirsToRefresh == null) {
     dirs = DirectoryManager.getInstance().getDirectories();
   } else {
     // If one or more named directories are provided, not only clean them up but also their sub
     // directories
     dirs = new ArrayList<Directory>(dirsToRefresh);
     for (Directory dir : dirsToRefresh) {
       dirs.addAll(dir.getDirectoriesRecursively());
     }
   }
   for (final Directory dir : dirs) {
     if (!ExitService.isExiting()
         && dir.getDevice().equals(this)
         && dir.getDevice().isMounted()
         && !dir.getFio().exists()) {
       // note that associated files are removed too
       DirectoryManager.getInstance().removeDirectory(dir.getID());
       Log.debug("Removed: " + dir);
       bChanges = true;
     }
   }
   return bChanges;
 }
 /**
  * Register an Playlist with a known id.
  *
  * @param sName DOCUMENT_ME
  * @param sId DOCUMENT_ME
  * @param dParentDirectory DOCUMENT_ME
  * @return the playlist
  */
 public synchronized Playlist registerPlaylistFile(
     String sId, String sName, Directory dParentDirectory) {
   Playlist playlistFile = getPlaylistByID(sId);
   if (playlistFile != null) {
     return playlistFile;
   }
   playlistFile = new Playlist(sId, sName, dParentDirectory);
   registerItem(playlistFile);
   if (dParentDirectory.getDevice().isRefreshing()) {
     Log.debug("Registered new playlist: " + playlistFile);
   }
   return playlistFile;
 }
 /**
  * Register an Playlist with a known id
  *
  * @param sName
  */
 public synchronized Playlist registerPlaylistFile(
     String sId, String sName, Directory dParentDirectory) throws Exception {
   synchronized (PlaylistManager.getInstance().getLock()) {
     if (!hmItems.containsKey(sId)) {
       Playlist playlistFile = null;
       playlistFile = new Playlist(sId, sName, dParentDirectory);
       hmItems.put(sId, playlistFile);
       if (dParentDirectory.getDevice().isRefreshing()) {
         Log.debug("Registered new playlist: " + playlistFile);
       }
     }
     return (Playlist) hmItems.get(sId);
   }
 }
 /**
  * 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;
 }