-
Notifications
You must be signed in to change notification settings - Fork 3
Feature: Implementation of Quick Access API for GNOME Nautilus #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5568025
implement sidebar integration for GNOME Nautilus
infeo d0bc09f
add isSupported method
infeo 5d124a1
fix typo
infeo f43238a
add priority
infeo 4e67c36
use new name QuickAccess
infeo 87274a6
prepare PR
infeo dbf2826
add operating system annotation
infeo 7040c0e
add display name
infeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
97 changes: 97 additions & 0 deletions
97
src/main/java/org/cryptomator/linux/quickaccess/NautilusBookmarks.java
This file contains hidden or 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,97 @@ | ||
| package org.cryptomator.linux.quickaccess; | ||
|
|
||
| import org.cryptomator.integrations.common.CheckAvailability; | ||
| import org.cryptomator.integrations.common.DisplayName; | ||
| import org.cryptomator.integrations.common.OperatingSystem; | ||
| import org.cryptomator.integrations.common.Priority; | ||
| import org.cryptomator.integrations.quickaccess.QuickAccessService; | ||
| import org.cryptomator.integrations.quickaccess.QuickAccessServiceException; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardCopyOption; | ||
| import java.nio.file.StandardOpenOption; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.locks.Lock; | ||
| import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
|
||
| @Priority(100) | ||
| @CheckAvailability | ||
| @OperatingSystem(OperatingSystem.Value.LINUX) | ||
| @DisplayName("GNOME Nautilus Bookmarks") | ||
| public class NautilusBookmarks implements QuickAccessService { | ||
|
|
||
| private static final int MAX_FILE_SIZE = 4096; | ||
| private static final Path BOOKMARKS_FILE = Path.of(System.getProperty("user.home"), ".config/gtk-3.0/bookmarks"); | ||
| private static final Path TMP_FILE = BOOKMARKS_FILE.resolveSibling("bookmarks.cryptomator.tmp"); | ||
| private static final Lock BOOKMARKS_LOCK = new ReentrantReadWriteLock().writeLock(); | ||
|
|
||
| @Override | ||
| public QuickAccessService.QuickAccessEntry add(Path target, String displayName) throws QuickAccessServiceException { | ||
| String entryLine = "file://" + target.toAbsolutePath() + " " + displayName; | ||
| try { | ||
| BOOKMARKS_LOCK.lock(); | ||
| if (Files.size(BOOKMARKS_FILE) > MAX_FILE_SIZE) { | ||
| throw new IOException("File %s exceeds size of %d bytes".formatted(BOOKMARKS_FILE, MAX_FILE_SIZE)); | ||
| } | ||
| //by reading all lines, we ensure that each line is terminated with EOL | ||
| var entries = Files.readAllLines(BOOKMARKS_FILE, StandardCharsets.UTF_8); | ||
| entries.add(entryLine); | ||
| Files.write(TMP_FILE, entries, StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); | ||
| Files.move(TMP_FILE, BOOKMARKS_FILE, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); | ||
| return new NautilusQuickAccessEntry(entryLine); | ||
| } catch (IOException e) { | ||
| throw new QuickAccessServiceException("Adding entry to Nautilus bookmarks file failed.", e); | ||
| } finally { | ||
| BOOKMARKS_LOCK.unlock(); | ||
| } | ||
| } | ||
|
infeo marked this conversation as resolved.
|
||
|
|
||
| static class NautilusQuickAccessEntry implements QuickAccessEntry { | ||
|
|
||
| private final String line; | ||
| private volatile boolean isRemoved = false; | ||
|
|
||
| NautilusQuickAccessEntry(String line) { | ||
| this.line = line; | ||
| } | ||
|
|
||
| @Override | ||
| public void remove() throws QuickAccessServiceException { | ||
| try { | ||
| BOOKMARKS_LOCK.lock(); | ||
| if (isRemoved) { | ||
| return; | ||
| } | ||
| if (Files.size(BOOKMARKS_FILE) > MAX_FILE_SIZE) { | ||
| throw new IOException("File %s exceeds size of %d bytes".formatted(BOOKMARKS_FILE, MAX_FILE_SIZE)); | ||
| } | ||
| var entries = Files.readAllLines(BOOKMARKS_FILE); | ||
| if (entries.remove(line)) { | ||
| Files.write(TMP_FILE, entries, StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); | ||
| Files.move(TMP_FILE, BOOKMARKS_FILE, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); | ||
| } | ||
| isRemoved = true; | ||
| } catch (IOException e) { | ||
| throw new QuickAccessServiceException("Removing entry from Nautilus bookmarks file failed", e); | ||
| } finally { | ||
| BOOKMARKS_LOCK.unlock(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @CheckAvailability | ||
| public static boolean isSupported() { | ||
| try { | ||
| var nautilusExistsProc = new ProcessBuilder().command("test", "`command -v nautilus`").start(); | ||
|
infeo marked this conversation as resolved.
|
||
| if (nautilusExistsProc.waitFor(5000, TimeUnit.MILLISECONDS)) { | ||
| return nautilusExistsProc.exitValue() == 0; | ||
| } | ||
| } catch (IOException | InterruptedException e) { | ||
| //NO-OP | ||
| } | ||
| return false; | ||
|
infeo marked this conversation as resolved.
|
||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
...n/resources/META-INF/services/org.cryptomator.integrations.quickaccess.QuickAccessService
This file contains hidden or 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.quickaccess.NautilusSidebarService |
22 changes: 22 additions & 0 deletions
22
src/test/java/org/cryptomator/linux/quickaccess/NautilusBookmarksIT.java
This file contains hidden or 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,22 @@ | ||
| package org.cryptomator.linux.quickaccess; | ||
|
|
||
| import org.cryptomator.integrations.quickaccess.QuickAccessServiceException; | ||
| import org.junit.jupiter.api.Disabled; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.time.Duration; | ||
|
|
||
| public class NautilusBookmarksIT { | ||
|
|
||
| @Test | ||
| @DisplayName("Adds for 20s an entryto the Nautilus sidebar") | ||
| @Disabled | ||
| public void testSidebarIntegration(@TempDir Path tmpdir) throws QuickAccessServiceException, InterruptedException { | ||
| var entry = new NautilusBookmarks().add(tmpdir, "integrations-linux"); | ||
| Thread.sleep(Duration.ofSeconds(20)); | ||
| entry.remove(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.