Create data source actions and create more utility classes

This commit is contained in:
Christopher Schnick 2022-11-07 18:10:17 +01:00
parent 89132fd1dc
commit 02b622fcf9
10 changed files with 223 additions and 2 deletions

View file

@ -16,6 +16,15 @@ import java.util.Optional;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public interface DataStore {
default boolean isComplete() {
try {
checkComplete();
return true;
} catch (Exception ignored) {
return false;
}
}
default DataFlow getFlow() {
return DataFlow.INPUT_OUTPUT;
}

View file

@ -27,6 +27,10 @@ public class FileStore extends JacksonizedValue implements FilenameStore, Stream
this.file = file;
}
public final boolean isLocal() {
return machine instanceof LocalStore;
}
public static FileStore local(Path p) {
return new FileStore(new LocalStore(), p.toString());
}

View file

@ -0,0 +1,49 @@
package io.xpipe.extension;
import io.xpipe.core.source.DataSource;
import io.xpipe.extension.event.ErrorEvent;
import javafx.beans.value.ObservableValue;
import javafx.scene.layout.Region;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
public interface DataSourceActionProvider<T extends DataSource<?>> {
static List<DataSourceActionProvider<?>> ALL = new ArrayList<>();
public static void init(ModuleLayer layer) {
if (ALL.size() == 0) {
ALL.addAll(ServiceLoader.load(layer, DataSourceActionProvider.class).stream()
.map(p -> (DataSourceActionProvider<?>) p.get())
.filter(provider -> {
try {
return provider.isActive();
} catch (Exception e) {
ErrorEvent.fromThrowable(e).handle();
return false;
}
})
.toList());
}
}
Class<T> getApplicableClass();
default boolean isActive() throws Exception {
return true;
}
default boolean isApplicable(T o) throws Exception {
return true;
}
default void applyToRegion(T store, Region region) {}
ObservableValue<String> getName(T store);
String getIcon(T store);
default void execute(T store) throws Exception {}
}

View file

@ -28,6 +28,7 @@ public class XPipeServiceProviders {
}
DataStoreActionProvider.init(layer);
DataSourceActionProvider.init(layer);
SupportedApplicationProviders.loadAll(layer);
PrefsProviders.init(layer);

View file

@ -49,10 +49,14 @@ public class WriteModeChoiceComp extends SimpleComp implements Validatable {
PlatformThread.sync(available).addListener((ListChangeListener<? super WriteMode>) c -> {
var newMap = new LinkedHashMap<WriteMode, ObservableValue<String>>();
for (WriteMode writeMode : a) {
for (WriteMode writeMode : c.getList()) {
newMap.put(writeMode,I18n.observable(writeMode.getId()));
}
map.setValue(newMap);
if (c.getList().size() == 1) {
selected.setValue(c.getList().get(0));
}
});
return new ToggleGroupComp<>(selected, map)

View file

@ -0,0 +1,18 @@
package io.xpipe.extension.util;
import javafx.beans.property.BooleanProperty;
public class BusyProperty implements AutoCloseable {
private final BooleanProperty prop;
public BusyProperty(BooleanProperty prop) {
this.prop = prop;
prop.setValue(true);
}
@Override
public void close() {
prop.setValue(false);
}
}

View file

@ -21,7 +21,7 @@ public class OsHelper {
}
}
public static void browseFile(Path file) {
public static void browsePath(Path file) {
if (!Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
return;
}

View file

@ -0,0 +1,75 @@
package io.xpipe.extension.util;
import org.apache.commons.lang3.SystemUtils;
import java.nio.file.Path;
import java.util.UUID;
public interface SupportedOs {
Windows WINDOWS = new Windows();
Linux LINUX = new Linux();
Mac MAC = new Mac();
public static SupportedOs get() {
if (SystemUtils.IS_OS_WINDOWS) {
return WINDOWS;
} else if (SystemUtils.IS_OS_LINUX) {
return LINUX;
} else if (SystemUtils.IS_OS_MAC) {
return MAC;
} else {
throw new UnsupportedOperationException("Unsupported operating system");
}
}
Path getBaseInstallPath();
UUID getSystemUUID();
static class Windows implements SupportedOs {
@Override
public Path getBaseInstallPath() {
return Path.of(System.getenv("LOCALAPPDATA"), "X-Pipe");
}
@Override
public UUID getSystemUUID() {
var s = WindowsRegistry.readRegistry(
"Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography", "MachineGuid")
.orElse(null);
if (s == null) {
return null;
}
return UUID.fromString(s);
}
}
static class Linux implements SupportedOs {
@Override
public Path getBaseInstallPath() {
return Path.of("/opt/xpipe");
}
@Override
public UUID getSystemUUID() {
return null;
}
}
static class Mac implements SupportedOs {
@Override
public Path getBaseInstallPath() {
return Path.of(System.getProperty("user.home"), "Application Support", "X-Pipe");
}
@Override
public UUID getSystemUUID() {
return null;
}
}
}

View file

@ -0,0 +1,60 @@
package io.xpipe.extension.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Optional;
public class WindowsRegistry {
public static Optional<String> readRegistry(String location, String key) {
try {
Process process =
Runtime.getRuntime().exec("reg query " + '"' + location + "\"" + (key != null ? " /v " + key : " /ve"));
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String output = reader.getResult();
// Output has the following format:
// \n<Version information>\n\n<key>\t<registry type>\t<value>
if (output.contains("\t")) {
String[] parsed = output.split("\t");
return Optional.of(parsed[parsed.length - 1]);
}
if (output.contains(" ")) {
String[] parsed = output.split(" ");
return Optional.of(parsed[parsed.length - 1].substring(0, parsed[parsed.length - 1].length() - 4));
}
return Optional.empty();
} catch (Exception e) {
return Optional.empty();
}
}
static class StreamReader extends Thread {
private final InputStream is;
private final StringWriter sw = new StringWriter();
public StreamReader(InputStream is) {
this.is = is;
}
public void run() {
try {
int c;
while ((c = is.read()) != -1) sw.write(c);
} catch (IOException e) {
System.err.println(e.toString());
}
}
public String getResult() {
return sw.toString();
}
}
}

View file

@ -39,4 +39,5 @@ open module io.xpipe.extension {
uses io.xpipe.extension.DataStoreProvider;
uses XPipeDaemon;
uses io.xpipe.extension.Cache;
uses io.xpipe.extension.DataSourceActionProvider;
}