-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from cryptomator/feature/reveal-path-service
Feature: RevealPathService via dbus freedesktop FileManger interface
- Loading branch information
Showing
4 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/org/cryptomator/linux/revealpath/DBusFileMangerRevealPath.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.cryptomator.linux.revealpath; | ||
|
||
import org.cryptomator.integrations.revealpath.RevealFailedException; | ||
import org.cryptomator.integrations.revealpath.RevealPathService; | ||
|
||
import java.io.IOException; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.LinkOption; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.util.Arrays; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
|
||
public class DBusFileMangerRevealPath implements RevealPathService { | ||
|
||
private static final String FOR_FOLDERS = "org.freedesktop.FileManager1.ShowFolders"; | ||
private static final String FOR_FILES = "org.freedesktop.FileManager1.ShowItems"; | ||
private static final int TIMEOUT_THRESHOLD=5000; | ||
|
||
@Override | ||
public void reveal(Path path) throws RevealFailedException { | ||
try { | ||
var attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); | ||
var uriPath = Arrays.stream(path.toUri().getPath().split("/")).map(s -> URLEncoder.encode(s, StandardCharsets.UTF_8).replace("+", "%20")).collect(Collectors.joining("/")); | ||
ProcessBuilder pb = new ProcessBuilder().command("dbus-send", | ||
"--print-reply", | ||
"--reply-timeout="+TIMEOUT_THRESHOLD, | ||
"--dest=org.freedesktop.FileManager1", | ||
"--type=method_call", | ||
"/org/freedesktop/FileManager1", | ||
attrs.isDirectory() ? FOR_FOLDERS : FOR_FILES, | ||
String.format("array:string:file://%s", uriPath), | ||
"string:\"\"" | ||
); | ||
var process = pb.start(); | ||
if (process.waitFor(TIMEOUT_THRESHOLD, TimeUnit.MILLISECONDS)) { | ||
int exitValue = process.exitValue(); | ||
if (exitValue != 0) { | ||
throw new RevealFailedException("dbus-send returned with code" + exitValue); | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new RevealFailedException(e); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new RevealFailedException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isSupported() { | ||
CountDownLatch waitBarrier = new CountDownLatch(3); | ||
ProcessBuilder builderExistsDbusSend = new ProcessBuilder().command("which", "dbus-send"); | ||
ProcessBuilder builderExistsNautilus = new ProcessBuilder().command("which", "nautilus"); | ||
ProcessBuilder builderExistsDolphin = new ProcessBuilder().command("which", "dolphin"); | ||
try { | ||
var existsDbusSend = builderExistsDbusSend.start(); | ||
existsDbusSend.onExit().thenRun(waitBarrier::countDown); | ||
var existsNautilus = builderExistsNautilus.start(); | ||
existsNautilus.onExit().thenRun(waitBarrier::countDown); | ||
var existsDolphin = builderExistsDolphin.start(); | ||
existsDolphin.onExit().thenRun(waitBarrier::countDown); | ||
if (waitBarrier.await(TIMEOUT_THRESHOLD, TimeUnit.MILLISECONDS)) { | ||
return existsDbusSend.exitValue() == 0 && (existsNautilus.exitValue() == 0 | existsDolphin.exitValue() == 0); | ||
} | ||
} catch (IOException | InterruptedException e) { | ||
//NO-OP | ||
} | ||
return false; | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...ain/resources/META-INF/services/org.cryptomator.integrations.revealpath.RevealPathService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.cryptomator.linux.revealpath.DBusFileMangerRevealPath |
42 changes: 42 additions & 0 deletions
42
src/test/java/org/cryptomator/linux/keychain/DbusFileManagerRevealPathTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.cryptomator.linux.keychain; | ||
|
||
import org.cryptomator.integrations.revealpath.RevealFailedException; | ||
import org.cryptomator.linux.revealpath.DBusFileMangerRevealPath; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Assumptions; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.EnabledOnOs; | ||
import org.junit.jupiter.api.condition.OS; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import java.nio.file.Path; | ||
|
||
@EnabledOnOs(OS.LINUX) | ||
@Disabled | ||
public class DbusFileManagerRevealPathTest { | ||
|
||
@TempDir Path tmpDir; | ||
DBusFileMangerRevealPath inTest = new DBusFileMangerRevealPath(); | ||
|
||
@Test | ||
public void testIsSupported() { | ||
Assertions.assertDoesNotThrow(() -> inTest.isSupported()); | ||
} | ||
|
||
@Test | ||
public void testRevealSuccess() { | ||
DBusFileMangerRevealPath revealPathService = new DBusFileMangerRevealPath(); | ||
Assumptions.assumeTrue(revealPathService.isSupported()); | ||
|
||
Assertions.assertDoesNotThrow(() -> revealPathService.reveal(tmpDir)); | ||
} | ||
|
||
@Test | ||
public void testRevealFail() { | ||
DBusFileMangerRevealPath revealPathService = new DBusFileMangerRevealPath(); | ||
Assumptions.assumeTrue(revealPathService.isSupported()); | ||
|
||
Assertions.assertThrows(RevealFailedException.class, () -> revealPathService.reveal(tmpDir.resolve("foobar"))); | ||
} | ||
} |