mirror of
https://github.com/xpipe-io/xpipe.git
synced 2024-07-04 22:10:56 +12:00
Create data source actions and create more utility classes
This commit is contained in:
parent
89132fd1dc
commit
02b622fcf9
|
@ -16,6 +16,15 @@ import java.util.Optional;
|
||||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
|
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
|
||||||
public interface DataStore {
|
public interface DataStore {
|
||||||
|
|
||||||
|
default boolean isComplete() {
|
||||||
|
try {
|
||||||
|
checkComplete();
|
||||||
|
return true;
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
default DataFlow getFlow() {
|
default DataFlow getFlow() {
|
||||||
return DataFlow.INPUT_OUTPUT;
|
return DataFlow.INPUT_OUTPUT;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,10 @@ public class FileStore extends JacksonizedValue implements FilenameStore, Stream
|
||||||
this.file = file;
|
this.file = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final boolean isLocal() {
|
||||||
|
return machine instanceof LocalStore;
|
||||||
|
}
|
||||||
|
|
||||||
public static FileStore local(Path p) {
|
public static FileStore local(Path p) {
|
||||||
return new FileStore(new LocalStore(), p.toString());
|
return new FileStore(new LocalStore(), p.toString());
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {}
|
||||||
|
}
|
|
@ -28,6 +28,7 @@ public class XPipeServiceProviders {
|
||||||
}
|
}
|
||||||
|
|
||||||
DataStoreActionProvider.init(layer);
|
DataStoreActionProvider.init(layer);
|
||||||
|
DataSourceActionProvider.init(layer);
|
||||||
|
|
||||||
SupportedApplicationProviders.loadAll(layer);
|
SupportedApplicationProviders.loadAll(layer);
|
||||||
PrefsProviders.init(layer);
|
PrefsProviders.init(layer);
|
||||||
|
|
|
@ -49,10 +49,14 @@ public class WriteModeChoiceComp extends SimpleComp implements Validatable {
|
||||||
|
|
||||||
PlatformThread.sync(available).addListener((ListChangeListener<? super WriteMode>) c -> {
|
PlatformThread.sync(available).addListener((ListChangeListener<? super WriteMode>) c -> {
|
||||||
var newMap = new LinkedHashMap<WriteMode, ObservableValue<String>>();
|
var newMap = new LinkedHashMap<WriteMode, ObservableValue<String>>();
|
||||||
for (WriteMode writeMode : a) {
|
for (WriteMode writeMode : c.getList()) {
|
||||||
newMap.put(writeMode,I18n.observable(writeMode.getId()));
|
newMap.put(writeMode,I18n.observable(writeMode.getId()));
|
||||||
}
|
}
|
||||||
map.setValue(newMap);
|
map.setValue(newMap);
|
||||||
|
|
||||||
|
if (c.getList().size() == 1) {
|
||||||
|
selected.setValue(c.getList().get(0));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return new ToggleGroupComp<>(selected, map)
|
return new ToggleGroupComp<>(selected, map)
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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)) {
|
if (!Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -39,4 +39,5 @@ open module io.xpipe.extension {
|
||||||
uses io.xpipe.extension.DataStoreProvider;
|
uses io.xpipe.extension.DataStoreProvider;
|
||||||
uses XPipeDaemon;
|
uses XPipeDaemon;
|
||||||
uses io.xpipe.extension.Cache;
|
uses io.xpipe.extension.Cache;
|
||||||
|
uses io.xpipe.extension.DataSourceActionProvider;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue