Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright © 2015 Jacob Keep (Jnk1296). All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * * Neither the name of JuNK Software nor the names of its contributors may
- * be used to endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
- package net.risenphoenix.filebrowser.client.commands;
- import net.risenphoenix.filebrowser.network.PacketSender;
- import net.risenphoenix.filebrowser.packet.Packet;
- import net.risenphoenix.filebrowser.packet.PacketType;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Random;
- public class CmdChangeDir extends Command {
- public CmdChangeDir(CommandInterpreter interpreter,
- String[] callArgs, CommandType type) {
- super(interpreter, callArgs, type);
- setHelp("Change Directory");
- setHelp("Changes your current operating directory. Arguments \n" +
- "accepted are either a case sensitive file name (for Unix \n" +
- "support) or the UP operator \"..\".");
- setSyntax("cd <dir_name || .. || .>");
- }
- @Override
- public void onExecute(String[] args) {
- StringBuilder sb = new StringBuilder();
- String directoryName;
- for (int i = 1; i < args.length; i++) {
- sb.append(args[i] + " ");
- }
- directoryName = sb.toString().substring(0, sb.toString().length() - 1);
- Object[] data = (Object[])getInterpreter().getDirectoryManager().getPacket().getData();
- ArrayList<File> fileListing = (ArrayList<File>)data[1];
- File currentDir = getInterpreter().getDirectoryManager().getCurrentDirectory();
- if(directoryName.equals("..")) {
- String dir = currentDir.getPath();
- int endIndex = dir.lastIndexOf("/");
- if (endIndex != -1) {
- String newDir = dir.substring(0, endIndex);
- if (newDir.length() >= getInterpreter().getDirectoryManager()
- .getRootDirectory().getPath().length()) {
- requestDirectoryUpdate(newDir);
- return;
- } else if (currentDir.getPath().equals(getInterpreter()
- .getDirectoryManager().getRootDirectory().getPath())) {
- System.out.println("Unable to go higher.");
- return;
- } else {
- requestDirectoryUpdate(getInterpreter()
- .getDirectoryManager().getRootDirectory()
- .getPath());
- return;
- }
- } else {
- System.out.println("Unable to go higher.");
- }
- } else if (directoryName.equals(".")) {
- String dir = getInterpreter().getDirectoryManager()
- .getRootDirectory().getPath();
- requestDirectoryUpdate(dir);
- return;
- } else {
- for(File f:fileListing) {
- if (f.getName().equals(directoryName)) {
- if (f.isDirectory()) {
- // All clear, request Directory Update
- String path = f.getPath();
- requestDirectoryUpdate(path);
- return;
- } else {
- System.out.println("The requested object is not a directory.");
- return;
- }
- }
- }
- }
- System.out.println("The directory requested does not exist.");
- }
- private void requestDirectoryUpdate(String directory) {
- int transactionID = new Random().nextInt();
- File newDirectory = new File(directory);
- Packet p = new Packet(PacketType.REQUEST_DIRECTORY_LISTING,
- new Object[]{newDirectory, transactionID});
- PacketSender ps = new PacketSender(getInterpreter().getClient()
- .getRemoteNetworkInfo(), p);
- ps.start();
- while(true) {
- if (getInterpreter().getDirectoryManager().getTransactionID()
- .equals(transactionID)) {
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement