From 214b274ee587a7fa1851ac0965972e2635333374 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Sat, 19 Nov 2011 18:09:10 +0000
Subject: [PATCH] Removable drive monitor for OS X 10.4 (JNotify requires at
 least 10.5).

---
 .../plugins/file/RemovableDrivePluginFactory.java  |  7 ++++++-
 .../sf/briar/plugins/PluginManagerImplTest.java    | 11 +++++++----
 .../file/UnixRemovableDriveMonitorTest.java        |  9 +++++++++
 util/net/sf/briar/util/OsUtils.java                | 14 ++++++++++++++
 4 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/components/net/sf/briar/plugins/file/RemovableDrivePluginFactory.java b/components/net/sf/briar/plugins/file/RemovableDrivePluginFactory.java
index f42d322c77..92850a4e03 100644
--- a/components/net/sf/briar/plugins/file/RemovableDrivePluginFactory.java
+++ b/components/net/sf/briar/plugins/file/RemovableDrivePluginFactory.java
@@ -18,9 +18,14 @@ public class RemovableDrivePluginFactory implements BatchPluginFactory {
 		if(OsUtils.isLinux()) {
 			finder = new LinuxRemovableDriveFinder();
 			monitor = new LinuxRemovableDriveMonitor();
-		} else if(OsUtils.isMac()) {
+		} else if(OsUtils.isMacLeopardOrNewer()) {
 			finder = new MacRemovableDriveFinder();
 			monitor = new MacRemovableDriveMonitor();
+		} else if(OsUtils.isMac()) {
+			// JNotify requires OS X 10.5 or newer, so we have to poll
+			finder = new MacRemovableDriveFinder();
+			monitor = new PollingRemovableDriveMonitor(finder,
+					POLLING_INTERVAL);
 		} else if(OsUtils.isWindows()) {
 			finder = new WindowsRemovableDriveFinder();
 			monitor = new PollingRemovableDriveMonitor(finder,
diff --git a/test/net/sf/briar/plugins/PluginManagerImplTest.java b/test/net/sf/briar/plugins/PluginManagerImplTest.java
index 0a1b4693ff..276f16236a 100644
--- a/test/net/sf/briar/plugins/PluginManagerImplTest.java
+++ b/test/net/sf/briar/plugins/PluginManagerImplTest.java
@@ -41,9 +41,12 @@ public class PluginManagerImplTest extends TestCase {
 		Poller poller = new PollerImpl();
 		PluginManagerImpl p = new PluginManagerImpl(db, executor, poller,
 				dispatcher, uiCallback);
-		// The Bluetooth plugin will not start without a Bluetooth device, so
-		// we expect two plugins to be started
-		assertEquals(2, p.startPlugins());
-		assertEquals(2, p.stopPlugins());
+		// We expect either 2 or 3 plugins to be started, depending on whether
+		// the test machine has a Bluetooth device
+		int started = p.startPlugins();
+		int stopped = p.stopPlugins();
+		assertEquals(started, stopped);
+		assertTrue(started >= 2);
+		assertTrue(started <= 3);
 	}
 }
diff --git a/test/net/sf/briar/plugins/file/UnixRemovableDriveMonitorTest.java b/test/net/sf/briar/plugins/file/UnixRemovableDriveMonitorTest.java
index 1ed814d038..c320968d0e 100644
--- a/test/net/sf/briar/plugins/file/UnixRemovableDriveMonitorTest.java
+++ b/test/net/sf/briar/plugins/file/UnixRemovableDriveMonitorTest.java
@@ -9,6 +9,7 @@ import java.util.concurrent.TimeUnit;
 import junit.framework.TestCase;
 import net.sf.briar.TestUtils;
 import net.sf.briar.plugins.file.RemovableDriveMonitor.Callback;
+import net.sf.briar.util.OsUtils;
 
 import org.junit.After;
 import org.junit.Before;
@@ -25,6 +26,10 @@ public class UnixRemovableDriveMonitorTest extends TestCase {
 
 	@Test
 	public void testNonexistentDir() throws Exception {
+		if(!OsUtils.isLinux() || OsUtils.isMacLeopardOrNewer()) {
+			System.err.println("Warning: Skipping test");
+			return;
+		}
 		File doesNotExist = new File(testDir, "doesNotExist");
 		RemovableDriveMonitor monitor = createMonitor(doesNotExist);
 		monitor.start(null);
@@ -33,6 +38,10 @@ public class UnixRemovableDriveMonitorTest extends TestCase {
 
 	@Test
 	public void testOneCallbackPerFile() throws Exception {
+		if(!OsUtils.isLinux() || OsUtils.isMacLeopardOrNewer()) {
+			System.err.println("Warning: Skipping test");
+			return;
+		}
 		// Create a callback that will wait for two files before stopping
 		final List<File> detected = new ArrayList<File>();
 		final CountDownLatch latch = new CountDownLatch(2);
diff --git a/util/net/sf/briar/util/OsUtils.java b/util/net/sf/briar/util/OsUtils.java
index f8e8553da5..dd32ad897f 100644
--- a/util/net/sf/briar/util/OsUtils.java
+++ b/util/net/sf/briar/util/OsUtils.java
@@ -3,6 +3,7 @@ package net.sf.briar.util;
 public class OsUtils {
 
 	private static final String os = System.getProperty("os.name");
+	private static final String version = System.getProperty("os.version");
 
 	public static boolean isWindows() {
 		return os.indexOf("Windows") != -1;
@@ -12,6 +13,19 @@ public class OsUtils {
 		return os.indexOf("Mac OS") != -1;
 	}
 
+	public static boolean isMacLeopardOrNewer() {
+		if(!isMac() || version == null) return false;
+		try {
+			String[] v = version.split("\\.");
+			if(v.length != 3) return false;
+			int major = Integer.parseInt(v[0]);
+			int minor = Integer.parseInt(v[1]);
+			return major >= 10 && minor >= 5;
+		} catch(NumberFormatException e) {
+			return false;
+		}
+	}
+
 	public static boolean isLinux() {
 		return os.indexOf("Linux") != -1;
 	}
-- 
GitLab