Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/de/tobifleig/lxc/LXC.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import de.tobifleig.lxc.net.NetworkManager;
import de.tobifleig.lxc.net.NetworkManagerListener;
import de.tobifleig.lxc.net.TransFileList;
import de.tobifleig.lxc.plaf.GuiInterface;
import de.tobifleig.lxc.plaf.impl.ui.UserInterface;
import de.tobifleig.lxc.plaf.GuiListener;
import de.tobifleig.lxc.plaf.Platform;

Expand Down Expand Up @@ -59,7 +59,7 @@ public class LXC {
/**
* Our GUI.
*/
private GuiInterface gui;
private final UserInterface gui;
/**
* The networkmanager.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/de/tobifleig/lxc/data/LXCFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ public String getShownName() {
return shownName;
}

/**
* Return a String with exactly 30 chars containing the name. The name is cut or suffixed with extra spaces to fill the 30 chars.
*
* @return
*/
public String getFormattedName() {
return String.format("%-30.30s", getShownName());
}

/**
* Returns the type.
*
Expand Down
164 changes: 164 additions & 0 deletions src/de/tobifleig/lxc/main/LxcDaemonController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* Copyright 2009, 2010, 2011, 2012, 2013, 2014 Tobias Fleig (tobifleig gmail com)
*
* All rights reserved.
*
* This file is part of LanXchange.
*
* LanXchange is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LanXchange is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LanXchange. If not, see <http://www.gnu.org/licenses/>.
*/
package de.tobifleig.lxc.main;

import de.tobifleig.lxc.plaf.impl.textbased.LxcDaemon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Arrays;

/**
* Control the Lxc-Daemon. Sends start, stop, file-upload etc. commands to the
* daemon
*
* @author Michael
*/
public class LxcDaemonController {

/**
* The socket used to communicate to the Lxc-Daemon
*/
private Socket socket;
/**
* The reader to read from the sockets inputstream.
*/
private BufferedReader reader;

public LxcDaemonController(String args[]) {

if (Arrays.asList(args).contains("status")) {
if (tryConnectSocket()) {
System.out.println("LxcDaemon is running.");
try {
socket.close();
} catch (Exception ex) {
};
} else {
System.out.println("LxcDaemon is NOT running.");
}
} else if (Arrays.asList(args).contains("start")) {
if (startDaemon()) {
System.out.println("LxcDaemon started successfully.");
} else {
System.out.println("Error starting LxcDaemon.");
}
} else if (Arrays.asList(args).contains("daemonize")) {
LxcDaemon daemon = new LxcDaemon();
} else if (Arrays.asList(args).contains("stop")) {
if (tryConnectSocket()) {
try {
socket.getOutputStream().write("stop\n".getBytes());
String answer = reader.readLine();
if (answer.equals("stopping...")) {
System.out.println("LxcDaemon shutting down.");
} else {
System.out.println("Error stopping LxcDaemon.");
}
socket.close();
} catch (IOException ex) {
System.out.println("Error stopping LxcDaemon.");
}
} else {
System.out.println("LxcDaemon is not running.");
}
} else if (Arrays.asList(args).contains("help") || Arrays.asList(args).contains("halp") || Arrays.asList(args).contains("hlep")) {
System.out.println("Usage:");
System.out.println("Show status: lxc -nogui status");
System.out.println("Start daemon: lxc -nogui start");
System.out.println("Stop daemon: lxc -nogui stop");
System.out.println("List available files: lxc -nogui list");
System.out.println("Download file: lxc -nogui download [number]");
System.out.println("Upload file: lxc -nogui upload-file [file]");
System.out.println("Stop Uploading file: lxc -nogui stop-uploading-file [file]");
} else {
// Its a command to the deamon, send it and print the answer:
if (!tryConnectSocket()) {
if (startDaemon()) {
System.out.println("LxcDaemon started.");
} else {
System.out.println("Cant start LxcDaemon!");
tryCloseSocket();
return;
}
}
String command = "";
boolean daemonargs = false;
for (int i = 0; i < args.length; i++) {
if (daemonargs) {
command += " " + args[i];
} else if (args[i].equals("-nogui")) {
daemonargs = true;
}
}
command = command.substring(1);
try {
socket.getOutputStream().write(command.getBytes());
socket.getOutputStream().write("\n".getBytes());
String answer;
do {
answer = reader.readLine();
System.out.println(answer);
} while (!answer.isEmpty());
socket.close();
} catch (IOException ex) {
System.out.println("Cant connect to LXCDaemon.");
}
}
tryCloseSocket();
}

private boolean startDaemon() {
try {
ProcessBuilder builder = new ProcessBuilder();
builder.environment().putAll(System.getenv());
// builder.command("java", "-jar", "LXC.jar", "-nogui", "daemonize");
builder.command("lxc", "-nogui", "daemonize");
builder.start();
return tryConnectSocket();
} catch (IOException ex) {
ex.printStackTrace();
return false;
}
}

private boolean tryConnectSocket() {
try {
socket = new Socket();
socket.connect(new InetSocketAddress("localhost", LxcDaemon.LXC_DAEMON_PORT));
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
return true;
} catch (IOException ex) {
return false;
}
}

private void tryCloseSocket() {
try {
reader.close();
socket.close();
} catch (Exception ex) {

}
}
}
46 changes: 46 additions & 0 deletions src/de/tobifleig/lxc/main/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2009, 2010, 2011, 2012, 2013, 2014 Tobias Fleig (tobifleig gmail com)
*
* All rights reserved.
*
* This file is part of LanXchange.
*
* LanXchange is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LanXchange is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LanXchange. If not, see <http://www.gnu.org/licenses/>.
*/
package de.tobifleig.lxc.main;

import java.util.Arrays;

/**
* Application entry point. Every project should have a main class.
*
* @author Michael
*/
public class Main {

/**
* Entry point Launch Swing-GUI unless -nogui was given as argument
*
* @param args the command line arguments
*/
public static void main(String args[]) {
if (Arrays.asList(args).contains("-nogui")) {
new LxcDaemonController(args);
} else {
new SwingGUILauncher(args);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@
* You should have received a copy of the GNU General Public License
* along with LanXchange. If not, see <http://www.gnu.org/licenses/>.
*/
package de.tobifleig.lxc.plaf.impl;

package de.tobifleig.lxc.main;

import de.tobifleig.lxc.plaf.impl.GenericPCPlatform;
import de.tobifleig.lxc.plaf.impl.swing.SwingGui;

/**
* Windows-specific behaviors/settings.
* Start LXC with a swing GUI.
*
* @author Tobias Fleig <tobifleig googlemail com>
* @author Michael
*/
public class WinPlatform extends GenericPCPlatform {
// Room for future enhancements
public class SwingGUILauncher {

public SwingGUILauncher(String args[]) {
GenericPCPlatform.startLXC(new SwingGui(), args);
}

}
5 changes: 4 additions & 1 deletion src/de/tobifleig/lxc/plaf/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*/
package de.tobifleig.lxc.plaf;

import de.tobifleig.lxc.plaf.impl.ui.UserInterface;
import java.io.File;
import de.tobifleig.lxc.data.LXCFile;
import java.io.File;

import de.tobifleig.lxc.data.LXCFile;
Expand Down Expand Up @@ -52,7 +55,7 @@ public interface Platform {
* @param args the start-parameters
* @return the platform-specific user interface
*/
public GuiInterface getGui(String[] args);
public UserInterface getGui(String[] args);

/**
* Reads and returns the configuration.
Expand Down
30 changes: 19 additions & 11 deletions src/de/tobifleig/lxc/plaf/impl/GenericPCPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import de.tobifleig.lxc.Configuration;
import de.tobifleig.lxc.LXC;
import de.tobifleig.lxc.data.LXCFile;
import de.tobifleig.lxc.plaf.GuiInterface;
import de.tobifleig.lxc.plaf.impl.ui.UserInterface;
import de.tobifleig.lxc.plaf.Platform;
import de.tobifleig.lxc.plaf.impl.swing.LXCUpdater;
import de.tobifleig.lxc.plaf.impl.swing.SwingGui;
Expand Down Expand Up @@ -52,7 +52,16 @@ public class GenericPCPlatform implements Platform {
/**
* the swing-gui.
*/
private static SwingGui gui = new SwingGui();
private static UserInterface userInterface;

/**
* Create a new GenericPCPlatform and use the given UserInterface to talk to the user.
*
* @param userInterface
*/
public GenericPCPlatform(UserInterface userInterface) {
this.userInterface = userInterface;
}

@Override
public boolean hasAutoUpdates() {
Expand Down Expand Up @@ -104,8 +113,7 @@ public void checkAndPerformUpdates(String[] args) {
@Override
public void run() {
try {
LXCUpdater.checkAndPerformUpdate(gui, force,
noVerification, managed);
LXCUpdater.checkAndPerformUpdate(userInterface, force, noVerification, managed);
} catch (Exception ex) {
ex.printStackTrace();
}
Expand All @@ -123,8 +131,8 @@ public void run() {
}

@Override
public GuiInterface getGui(String[] args) {
return gui;
public UserInterface getGui(String[] args) {
return userInterface;
}

@Override
Expand Down Expand Up @@ -217,10 +225,10 @@ public String getDefaultDownloadTarget() {
/**
* Starts LanXchange on PC plaforms.
*
* @param args
* any arguments you want to pass to LanXchange
* @param userInterface the interface (graphical or text) to talk to the user
* @param args any arguments you want to pass to LanXchange
*/
public static void main(String[] args) {
public static void startLXC(UserInterface userInterface, String[] args) {
// check permission for own folder
try {
File.createTempFile("testacl", null, new File(".")).delete();
Expand All @@ -230,10 +238,10 @@ public static void main(String[] args) {
System.out.println("ERROR: Cannot write to my directory ("
+ new File(".").getAbsolutePath()
+ "). Try running LXC in your home directory.");
gui.showError("LXC is not allowed to create/modify files in the folder it is located. Please move to your home directory or start as administrator.");
userInterface.showError("LXC is not allowed to create/modify files in the folder it is located. Please move to your home directory or start as administrator.");
System.exit(1);
}
LXC lxc = new LXC(new GenericPCPlatform(), args);
LXC lxc = new LXC(new GenericPCPlatform(userInterface), args);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import de.tobifleig.lxc.R;
import de.tobifleig.lxc.data.LXCFile;
import de.tobifleig.lxc.data.VirtualFile;
import de.tobifleig.lxc.plaf.GuiInterface;
import de.tobifleig.lxc.plaf.impl.ui.UserInterface;
import de.tobifleig.lxc.plaf.GuiListener;
import de.tobifleig.lxc.plaf.Platform;
import de.tobifleig.lxc.plaf.impl.AndroidPlatform;
Expand Down Expand Up @@ -122,8 +122,8 @@ public void checkAndPerformUpdates(String[] args) {
}

@Override
public GuiInterface getGui(String[] args) {
return new GuiInterface() {
public UserInterface getGui(String[] args) {
return new UserInterface() {

@Override
public void update() {
Expand Down
Loading