|
| 1 | +package org.cryptomator.linux.quickaccess; |
| 2 | + |
| 3 | +import org.cryptomator.integrations.common.CheckAvailability; |
| 4 | +import org.cryptomator.integrations.common.DisplayName; |
| 5 | +import org.cryptomator.integrations.common.OperatingSystem; |
| 6 | +import org.cryptomator.integrations.common.Priority; |
| 7 | +import org.cryptomator.integrations.quickaccess.QuickAccessService; |
| 8 | +import org.cryptomator.integrations.quickaccess.QuickAccessServiceException; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.nio.file.StandardCopyOption; |
| 15 | +import java.nio.file.StandardOpenOption; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import java.util.concurrent.locks.Lock; |
| 18 | +import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 19 | + |
| 20 | +@Priority(100) |
| 21 | +@CheckAvailability |
| 22 | +@OperatingSystem(OperatingSystem.Value.LINUX) |
| 23 | +@DisplayName("GNOME Nautilus Bookmarks") |
| 24 | +public class NautilusBookmarks implements QuickAccessService { |
| 25 | + |
| 26 | + private static final int MAX_FILE_SIZE = 4096; |
| 27 | + private static final Path BOOKMARKS_FILE = Path.of(System.getProperty("user.home"), ".config/gtk-3.0/bookmarks"); |
| 28 | + private static final Path TMP_FILE = BOOKMARKS_FILE.resolveSibling("bookmarks.cryptomator.tmp"); |
| 29 | + private static final Lock BOOKMARKS_LOCK = new ReentrantReadWriteLock().writeLock(); |
| 30 | + |
| 31 | + @Override |
| 32 | + public QuickAccessService.QuickAccessEntry add(Path target, String displayName) throws QuickAccessServiceException { |
| 33 | + String entryLine = "file://" + target.toAbsolutePath() + " " + displayName; |
| 34 | + try { |
| 35 | + BOOKMARKS_LOCK.lock(); |
| 36 | + if (Files.size(BOOKMARKS_FILE) > MAX_FILE_SIZE) { |
| 37 | + throw new IOException("File %s exceeds size of %d bytes".formatted(BOOKMARKS_FILE, MAX_FILE_SIZE)); |
| 38 | + } |
| 39 | + //by reading all lines, we ensure that each line is terminated with EOL |
| 40 | + var entries = Files.readAllLines(BOOKMARKS_FILE, StandardCharsets.UTF_8); |
| 41 | + entries.add(entryLine); |
| 42 | + Files.write(TMP_FILE, entries, StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); |
| 43 | + Files.move(TMP_FILE, BOOKMARKS_FILE, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); |
| 44 | + return new NautilusQuickAccessEntry(entryLine); |
| 45 | + } catch (IOException e) { |
| 46 | + throw new QuickAccessServiceException("Adding entry to Nautilus bookmarks file failed.", e); |
| 47 | + } finally { |
| 48 | + BOOKMARKS_LOCK.unlock(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + static class NautilusQuickAccessEntry implements QuickAccessEntry { |
| 53 | + |
| 54 | + private final String line; |
| 55 | + private volatile boolean isRemoved = false; |
| 56 | + |
| 57 | + NautilusQuickAccessEntry(String line) { |
| 58 | + this.line = line; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void remove() throws QuickAccessServiceException { |
| 63 | + try { |
| 64 | + BOOKMARKS_LOCK.lock(); |
| 65 | + if (isRemoved) { |
| 66 | + return; |
| 67 | + } |
| 68 | + if (Files.size(BOOKMARKS_FILE) > MAX_FILE_SIZE) { |
| 69 | + throw new IOException("File %s exceeds size of %d bytes".formatted(BOOKMARKS_FILE, MAX_FILE_SIZE)); |
| 70 | + } |
| 71 | + var entries = Files.readAllLines(BOOKMARKS_FILE); |
| 72 | + if (entries.remove(line)) { |
| 73 | + Files.write(TMP_FILE, entries, StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); |
| 74 | + Files.move(TMP_FILE, BOOKMARKS_FILE, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); |
| 75 | + } |
| 76 | + isRemoved = true; |
| 77 | + } catch (IOException e) { |
| 78 | + throw new QuickAccessServiceException("Removing entry from Nautilus bookmarks file failed", e); |
| 79 | + } finally { |
| 80 | + BOOKMARKS_LOCK.unlock(); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @CheckAvailability |
| 86 | + public static boolean isSupported() { |
| 87 | + try { |
| 88 | + var nautilusExistsProc = new ProcessBuilder().command("test", "`command -v nautilus`").start(); |
| 89 | + if (nautilusExistsProc.waitFor(5000, TimeUnit.MILLISECONDS)) { |
| 90 | + return nautilusExistsProc.exitValue() == 0; |
| 91 | + } |
| 92 | + } catch (IOException | InterruptedException e) { |
| 93 | + //NO-OP |
| 94 | + } |
| 95 | + return false; |
| 96 | + } |
| 97 | +} |
0 commit comments