001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.rGUI.RFileBrowser;
020
021import java.io.File;
022import java.util.ArrayList;
023import java.util.concurrent.Semaphore;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028import javax.swing.filechooser.FileSystemView;
029
030/**
031 *
032 * @author Samuel Campos <samuelcampos@ua.pt>
033 */
034@Deprecated
035public class RemoteFileSystemServer implements IRemoteFileSystem {
036
037    private FileSystemView fs = FileSystemView.getFileSystemView();
038
039    private static Semaphore sem = new Semaphore(1, true);
040    private static RemoteFileSystemServer instance = null;
041
042    public static synchronized RemoteFileSystemServer getInstance() {
043        try {
044            sem.acquire();
045            if (instance == null) {
046                instance = new RemoteFileSystemServer();
047            }
048            sem.release();
049        } catch (InterruptedException ex) {
050            LoggerFactory.getLogger(RemoteFileSystemServer.class).error(ex.getMessage(), ex);
051        }
052
053        return instance;
054    }
055
056    private RemoteFileSystemServer(){
057        
058    }
059
060    /**
061     * get FilePath from root to actualFile (One RemoteFile for each folder)
062     *
063     * @param filePath
064     * @return
065     */
066    @Override
067    public RemoteFile[] getFilePath(String filePath) {
068        if(filePath == null || filePath.equals(""))
069            filePath = ".";
070
071        ArrayList<RemoteFile> files = new ArrayList<RemoteFile>();
072        RemoteFile rFile;
073
074        File file = new File(filePath);
075        rFile = new RemoteFile(file);
076
077        if(file.isDirectory())
078            files.add(rFile);
079
080        while(rFile != null && !isRoot(rFile.getPath())){
081            rFile = getParentDirectory(rFile.getPath());
082
083            if (rFile != null)
084                files.add(rFile);
085        }
086
087        if(System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1)
088        {
089            File[] roots = File.listRoots();
090            
091            for(File f: roots)
092                if(f.exists())
093                    files.add(new RemoteFile(f));
094        }
095
096        RemoteFile[] rFilesArray = new RemoteFile[files.size()];
097        return files.toArray(rFilesArray);
098    }
099
100    @Override
101    public RemoteFile[] getFiles(String dirPath, boolean useFileHiding) {
102        if(dirPath == null || dirPath.equals(""))
103            dirPath = ".";
104
105
106        File dir = new File(dirPath);
107        
108        if (!dir.exists())
109           dir = new File(".");
110        
111        File[] file = fs.getFiles(dir, useFileHiding);
112
113        
114        RemoteFile[] remoteFile = new RemoteFile[file.length];
115
116        for(int i = 0; i < file.length; i++)
117            remoteFile[i] = new RemoteFile(file[i]);
118
119        return remoteFile;
120    }
121
122    @Override
123    public boolean isFileSystemRoot(String dirPath) {
124        File dir = new File(dirPath);
125
126        if (!dir.exists())
127           return false;
128        
129        return fs.isFileSystemRoot(dir);
130    }
131
132    @Override
133    public boolean isRoot(String dirPath){
134        File dir = new File(dirPath);
135
136        if (!dir.exists())
137           return false;
138
139        return fs.isRoot(dir);
140    }
141
142    @Override
143    public boolean isFileSystem(String dirPath) {
144        File f = new File(dirPath);
145
146        if (!f.exists())
147           return false;
148
149        return fs.isFileSystem(f);
150    }
151
152    @Override
153    public boolean isDrive(String dirPath) {
154        File dir = new File(dirPath);
155
156        return fs.isDrive(dir);
157    }
158
159    @Override
160    public boolean isComputerNode(String dirPath) {
161        File dir = new File(dirPath);
162
163        if (!dir.exists())
164           dir = new File(".");
165        
166        return fs.isComputerNode(dir);
167    }
168
169    @Override
170    public RemoteFile getParentDirectory(String dirPath) {
171        File dir = new File(dirPath);
172        
173        if (!dir.exists())
174           dir = new File(".");
175        
176        if (fs.isRoot(dir))
177            return null;
178
179        if(System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1){
180            if(dir.getAbsolutePath().lastIndexOf('\\') == (dir.getAbsolutePath().length()-1) )
181                return null;
182        }
183        
184        File parent = fs.getParentDirectory(dir);
185
186        if(parent != null)
187            return new RemoteFile(parent);
188
189        return null;
190    }
191
192    @Override
193    public String getSystemDisplayName(String filePath) {
194        File f = new File(filePath);
195
196        return fs.getSystemDisplayName(f);
197    }
198
199    @Override
200    public String getSystemTypeDescription(String filePath) {
201        File f = new File(filePath);
202
203        return fs.getSystemTypeDescription(f);
204    }
205
206    @Override
207    public RemoteFile getDefaultDirectory() {
208        return new RemoteFile(fs.getDefaultDirectory());
209    }
210
211    @Override
212    public RemoteFile getHomeDirectory() {
213        return new RemoteFile(fs.getHomeDirectory());
214    }
215
216    @Override
217    public RemoteFile[] getRoots() {
218        File[] file = fs.getRoots();
219
220        RemoteFile[] remoteFile = new RemoteFile[file.length];
221
222        for(int i = 0; i < file.length; i++)
223            remoteFile[i] = new RemoteFile(file[i]);
224
225        return remoteFile;
226    }
227}