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 2, 2015
1 parent 4f121a1 commit ff4ae0e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 46 deletions.
49 changes: 4 additions & 45 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.nativeext.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 @@ -61,12 +62,6 @@
import net.sf.jabref.logic.logging.CacheableHandler;
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 +145,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 +158,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
67 changes: 67 additions & 0 deletions src/main/java/net/sf/jabref/gui/nativeext/WindowsExtensions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package net.sf.jabref.gui.nativeext;

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) {
Native.register("shell32");
LOGGER.info("Registered Shell32 DLL");
}
}

/**
* Sets the application user model id so that JabRef can be pinned to the taskbar
* Only supported by Windows 7 and up!
*
* AppUserModelId must also be set in NSIS setup.nsi
* WinShell::SetLnkAUMI "$INSTDIR\$(^Name).lnk" "${AppUserModelId}"
* Structure: JabRef.${VERSION}
*
* Based on http://stackoverflow.com/a/1928830
* http://stackoverflow.com/questions/5438651/launch4j-nsis-and-duplicate-pinned-windows-7-taskbar-icons
*/
public static void enablePinToTaskbar() {
if(supportsPinToTaskbar()) {
setCurrentProcessExplicitAppUserModelID("JabRef." + Globals.BUILD_INFO.getVersion());
} else {
LOGGER.info("Does not support pin to taskbar.");
}
}

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 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 ff4ae0e

Please sign in to comment.