Skip to content

Commit

Permalink
Extracted native Windows logic and fixed #194
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed Oct 1, 2015
1 parent 4f121a1 commit d0d6789
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 44 deletions.
47 changes: 4 additions & 43 deletions src/main/java/net/sf/jabref/JabRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import javax.swing.plaf.FontUIResource;

import net.sf.jabref.gui.*;
import net.sf.jabref.gui.native_ext.WindowsExtensions;
import net.sf.jabref.importer.fetcher.EntryFetcher;
import net.sf.jabref.importer.fetcher.EntryFetchers;
import net.sf.jabref.logic.journals.Abbreviations;
Expand Down Expand Up @@ -62,10 +63,6 @@
import net.sf.jabref.wizard.auximport.AuxCommandLine;

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.WString;
import com.sun.jna.ptr.PointerByReference;

/**
* JabRef Main Class - The application gets started here.
Expand Down Expand Up @@ -150,10 +147,8 @@ public void start(String[] args) {
Globals.NEWLINE = Globals.prefs.get(JabRefPreferences.NEWLINE);

if (OS.WINDOWS) {
// Set application user model id so that pinning JabRef to the Win7/8 taskbar works
// Based on http://stackoverflow.com/a/1928830
JabRef.setCurrentProcessExplicitAppUserModelID("JabRef." + Globals.BUILD_INFO.getVersion());
//System.out.println(getCurrentProcessExplicitAppUserModelID());
// activate pin to taskbar for Windows 7 and up
WindowsExtensions.enablePinToTaskbar();
}

Vector<ParserResult> loaded = processArguments(args, true);
Expand All @@ -165,46 +160,12 @@ public void start(String[] args) {

openWindow(loaded);
}

private void setupLogHandlerForErrorConsole() {
Globals.handler = new CacheableHandler();
((Jdk14Logger)LOGGER).getLogger().addHandler(Globals.handler);
}

// Do not use this code in release version, it contains some memory leaks
public static String getCurrentProcessExplicitAppUserModelID()
{
final PointerByReference r = new PointerByReference();

if (JabRef.GetCurrentProcessExplicitAppUserModelID(r).longValue() == 0)
{
final Pointer p = r.getValue();

return p.getString(0, true); // here we leak native memory by lazyness
}
return "N/A";
}

private static void setCurrentProcessExplicitAppUserModelID(final String appID)
{
if (JabRef.SetCurrentProcessExplicitAppUserModelID(new WString(appID)).longValue() != 0) {
throw new RuntimeException("unable to set current process explicit AppUserModelID to: " + appID);
}
}

private static native NativeLong GetCurrentProcessExplicitAppUserModelID(PointerByReference appID);

private static native NativeLong SetCurrentProcessExplicitAppUserModelID(WString appID);


static
{
if (OS.WINDOWS) {
Native.register("shell32");
}
}


public Vector<ParserResult> processArguments(String[] args, boolean initialStartup) {

cli = new JabRefCLI(args);
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/net/sf/jabref/gui/native_ext/WindowsExtensions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package net.sf.jabref.gui.native_ext;

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.WString;
import com.sun.jna.ptr.PointerByReference;
import net.sf.jabref.Globals;
import net.sf.jabref.logic.util.OS;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Native extensions for Windows.
*/
public class WindowsExtensions {
private static final Log LOGGER = LogFactory.getLog(WindowsExtensions.class);

// Register native calls for pin to taskbar functionality
static {
if (OS.WINDOWS) {
LOGGER.info("Registered Shell32 DLL");
Native.register("shell32");
}
}

public static void enablePinToTaskbar() {
// Set application user model id so that pinning JabRef to the Win7/8 taskbar works
// Based on http://stackoverflow.com/a/1928830
// Only do this if Windows is capable >= Windows 7
if(supportsPinToTaskbar()) {
setCurrentProcessExplicitAppUserModelID("JabRef." + Globals.BUILD_INFO.getVersion());
}
}

// Do not use this code in release version, it contains some memory leaks
private static String getCurrentProcessExplicitAppUserModelID() {
final PointerByReference r = new PointerByReference();

if (GetCurrentProcessExplicitAppUserModelID(r).longValue() == 0) {
final Pointer p = r.getValue();
return p.getString(0, true); // here we leak native memory by lazyness
}
return "N/A";
}

private static void setCurrentProcessExplicitAppUserModelID(final String appID) {
if (SetCurrentProcessExplicitAppUserModelID(new WString(appID)).longValue() != 0) {
throw new RuntimeException("unable to set current process explicit AppUserModelID to: " + appID);
}
}

private static native NativeLong GetCurrentProcessExplicitAppUserModelID(PointerByReference appID);

private static native NativeLong SetCurrentProcessExplicitAppUserModelID(WString appID);

private static boolean supportsPinToTaskbar() {
if (!OS.WINDOWS) {
return false;
}

try {
Float version = Float.parseFloat(System.getProperty("os.version"));
// Windows 7 == 6.1
return version >= 6.1;
} catch (NumberFormatException ex) {
return false;
}
}
}
1 change: 0 additions & 1 deletion src/main/java/net/sf/jabref/logic/util/OS.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Operating system (OS) detection
*/
public class OS {

// TODO: what OS do we support?
// https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/SystemUtils.html
public static final String osName = System.getProperty("os.name", "unknown").toLowerCase();
Expand Down

0 comments on commit d0d6789

Please sign in to comment.