Skip to content

Commit abfe16c

Browse files
committed
Merge pull request #80 from scijava/welcome-only-once
Show the welcome message only once
2 parents e82c704 + c6a05c7 commit abfe16c

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/main/java/org/scijava/welcome/DefaultWelcomeService.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333

3434
import java.io.File;
3535
import java.io.IOException;
36+
import java.io.UnsupportedEncodingException;
37+
import java.security.MessageDigest;
38+
import java.security.NoSuchAlgorithmException;
3639

3740
import org.scijava.app.AppService;
3841
import org.scijava.command.CommandService;
@@ -60,7 +63,8 @@ public class DefaultWelcomeService extends AbstractService implements
6063
WelcomeService
6164
{
6265

63-
private String WELCOME_FILE = "WELCOME.md";
66+
private final static String WELCOME_FILE = "WELCOME.md";
67+
private final static String CHECKSUM_PREFS_KEY = "checksum";
6468

6569
@Parameter
6670
private LogService log;
@@ -69,7 +73,7 @@ public class DefaultWelcomeService extends AbstractService implements
6973
private AppService appService;
7074

7175
@Parameter
72-
DisplayService displayService;
76+
private DisplayService displayService;
7377

7478
@Parameter
7579
private CommandService commandService;
@@ -89,6 +93,10 @@ public void displayWelcome() {
8993
try {
9094
if (welcomeFile.exists()) {
9195
final String welcomeText = textService.asHTML(welcomeFile);
96+
final String checksum = getChecksum(welcomeText);
97+
final String previousChecksum = Prefs.get(getClass(), CHECKSUM_PREFS_KEY);
98+
if (checksum.equals(previousChecksum)) return;
99+
Prefs.put(getClass(), CHECKSUM_PREFS_KEY, checksum);
92100
displayService.createDisplay(welcomeText);
93101
}
94102
}
@@ -126,4 +134,34 @@ private String firstRunPrefKey() {
126134
return "firstRun-" + appService.getApp().getVersion();
127135
}
128136

137+
// TODO: move this into TextUtils or some such
138+
// see https://github.com/scijava/scijava-common/issues/82
139+
// get digest of the file as according to fullPath
140+
private String getChecksum(final String text)
141+
{
142+
try {
143+
final MessageDigest digest = MessageDigest.getInstance("SHA-1");
144+
digest.update(text.getBytes("UTF-8"));
145+
return toHex(digest.digest());
146+
}
147+
catch (NoSuchAlgorithmException e) {
148+
return "" + text.hashCode();
149+
}
150+
catch (UnsupportedEncodingException e) {
151+
return "" + text.hashCode();
152+
}
153+
}
154+
155+
private final static char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7',
156+
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
157+
158+
private String toHex(final byte[] bytes) {
159+
final char[] buffer = new char[bytes.length * 2];
160+
for (int i = 0; i < bytes.length; i++) {
161+
buffer[i * 2] = hex[(bytes[i] & 0xf0) >> 4];
162+
buffer[i * 2 + 1] = hex[bytes[i] & 0xf];
163+
}
164+
return new String(buffer);
165+
}
166+
129167
}

0 commit comments

Comments
 (0)