Skip to content

Commit

Permalink
Autostart by default (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf authored and bberenz committed Apr 7, 2018
1 parent 657af75 commit 16127c0
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 193 deletions.
6 changes: 6 additions & 0 deletions ant/apple/apple-launcher.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ iconpath=$installpath/${apple.resources}/${apple.icon}
${apple.jvmver} > /dev/null 2>&1
fallback=$?

# If launched at startup, check override first
if [[ "$1" == "--autostart" ]] && [[ "$(cat "$installpath/.autostart")" == "0" ]]; then
echo "Skipping autostart"
exit 0
fi

# Fallback on Internet Plug-Ins version if needed
if [ $fallback -eq 0 ]; then
${apple.jvmcmd} java ${launch.opts} -Xdock:name="${project.name}" -Xdock:icon="$iconpath" -jar "$jarpath"
Expand Down
27 changes: 27 additions & 0 deletions ant/apple/apple-postinstall.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,33 @@ if [ $? -eq 0 ]; then
"${apple.installdir}/auth/firefox/${firefoxcert.name}" "install"
fi

# Install startup
site=$(echo "${vendor.website}"|rev|cut -d/ -f1|rev)
package=$(echo "$site" |rev |cut -d. -f1|rev).$(echo "$site" |rev |cut -d. -f2|rev).${project.filename}
cat > /Library/LaunchAgents/$package.plist << EOT
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key><string>$package</string>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key><false/>
<key>AfterInitialDemand</key><false/>
</dict>
<key>RunAtLoad</key><true/>
<key>ProgramArguments</key>
<array>
<string>${apple.installdir}/${apple.macos}/${project.name}</string>
<string>--autostart</string>
</array>
</dict>
</plist>
EOT

printf 1 > "${apple.installdir}/.autostart"
chmod 777 "${apple.installdir}/.autostart"

# Cleanup resources from previous versions
rm -rf "${apple.installdir}/demo/js/3rdparty"
rm "${apple.installdir}/demo/js/qz-websocket.js"
Expand Down
5 changes: 5 additions & 0 deletions ant/apple/apple-uninstall.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ else
echo -e "${bash.skipped}"
fi

# Remove startup entry
site=$(echo "${vendor.website}"|rev|cut -d/ -f1|rev)
package=$(echo "$site" |rev |cut -d. -f1|rev).$(echo "$site" |rev |cut -d. -f2|rev).${project.filename}
rm -f /Library/LaunchAgents/$package.plist

# Uninstall ${project.name} system certificates
"${apple.installdir}/auth/${apple.keygen.name}" "uninstall"

Expand Down
2 changes: 1 addition & 1 deletion ant/firefox/locator.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function get_targetdir()
lowerbin=$(echo "$with_bin" |tr '[:upper:]' '[:lower:]')
location=$(readlink -f "$(which $lowerdir)")
targetdir=$(dirname "$location")
if [ -f "$targetdir/$lowerbin" ]; then
if [[ "$targetdir" != "/usr/bin" ]] && [ -f "$targetdir/$lowerbin" ]; then
dir_out="$targetdir"
return 0
else
Expand Down
18 changes: 18 additions & 0 deletions ant/linux/linux-installer.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,24 @@ rm -f "/lib/udev/rules.d/${linux.udev.name}" > /dev/null 2>&1
mv "$destdir/${linux.udev.name}" "/lib/udev/rules.d/${linux.udev.name}" > /dev/null 2>&1
udevadm control --reload-rules > /dev/null 2>&1

progress_dialog 93 "Cleaning up old versions..."

# Remove old startup entries
for i in /home/* ; do
if [ -n "${project.filename}" ]; then
rm "$i/.config/autostart/${project.filename}.desktop" > /dev/null 2>&1
fi
if [ -n "${project.name}" ]; then
rm "$i/.config/autostart/${project.name}.desktop" > /dev/null 2>&1
fi
done

progress_dialog 94 "Adding startup entry..."
cp "${shortcut}" "/etc/xdg/autostart/${project.filename}.desktop"

# Allow oridinary users to write
chmod 777 "/etc/xdg/autostart/${project.filename}.desktop"

cd "${destdir}"
progress_dialog 95 "Installation complete... Starting ${project.name}..."
which sudo > /dev/null 2>&1
Expand Down
6 changes: 6 additions & 0 deletions ant/linux/linux-keygen.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ if [ -n "$trustedcertpath" ]; then
exit 0
fi

# Check for keytool command
"${jks.keytool} -help" > /dev/null 2>&1
if [ $? -ne 0 ]; then
export PATH=$PATH:/usr/java/latest/bin/
fi

# Handle self-signed certificate
echo -e "\nCreating keystore for secure websockets..."
# Delete old files if exist
Expand Down
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion ant/windows/windows-cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ function getArg(index, defaultVal) {
*/
function trim(val) {
return val.replace(/^\s+/,'').replace(/\s+$/,'');
}
}
10 changes: 9 additions & 1 deletion ant/windows/windows-packager.nsi.in
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ Section

; Remove ${vendor.company} certificates
nsExec::ExecToLog "cscript.exe //NoLogo //E:jscript $\"$INSTDIR\auth\${windows.keygen.name}$\" $\"$INSTDIR$\" uninstall"


; Perform cleanup operations from previous install
nsExec::ExecToLog "cscript.exe //NoLogo //E:jscript $\"$INSTDIR\utils\${windows.cleanup.name}$\" $\"${project.name}$\""

keygen:
; Exports a self-signed certificate and properties file
DetailPrint "Generating a unique certificate for HTTPS support..."
Expand Down Expand Up @@ -122,6 +125,10 @@ Section
${EndIf}

CreateShortCut "$SMPROGRAMS\${project.name}.lnk" "$INSTDIR\${project.filename}.exe" "" "$INSTDIR\${windows.icon}" 0
CreateShortCut "$SMSTARTUP\${project.name}.lnk" "$INSTDIR\${project.filename}.exe" "" "$INSTDIR\${windows.icon}" 0

; Grant R+W to Authenticated Users (S-1-5-11)
AccessControl::GrantOnFile "$SMSTARTUP\${project.name}.lnk" "(S-1-5-11)" "GenericRead + GenericWrite"

; Delete matching firewall rules
DetailPrint "Removing ${project.name} firewall rules..."
Expand Down Expand Up @@ -177,6 +184,7 @@ Section "Uninstall"

Delete "$DESKTOP\${project.name}.url"
Delete "$DESKTOP\${project.name}.lnk"
Delete "$SMSTARTUP\${project.name}.lnk"

; Sets the context of shell folders to current user
SetShellVarContext current
Expand Down
2 changes: 2 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@
<filterchain><expandproperties/></filterchain>
</copy>

<copy file="${windows.cleanup.in}" tofile="${windows.cleanup.out}" />

<exec executable="${nsisbin}" failonerror="true" >
<arg value="${windows.launcher.out}"/>
</exec>
Expand Down
40 changes: 6 additions & 34 deletions src/qz/deploy/DeployUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public boolean createShortcut(ToggleType toggleType) {
* @param toggleType Shortcut type, i.e. <code>ToggleType.STARTUP</code> or <code>ToggleType.DESKTOP</code>
* @return Whether or not the shortcut already exists
*/
public boolean hasShortcut(ToggleType toggleType) {
private boolean hasShortcut(ToggleType toggleType) {
boolean hasShortcut = false;
switch(toggleType) {
case STARTUP:
Expand Down Expand Up @@ -165,7 +165,7 @@ public boolean removeShortcut(ToggleType toggleType) {
* @return The calculated working path value, or an empty string if one
* could not be determined
*/
static String getParentDirectory(String filePath) {
private static String getParentDirectory(String filePath) {
// Working path should always default to the JARs parent folder
int lastSlash = filePath.lastIndexOf(File.separator);
return lastSlash < 0? "":filePath.substring(0, lastSlash);
Expand Down Expand Up @@ -201,25 +201,6 @@ public static DeployUtilities getSystemShortcutCreator() {
}
}

/**
* Creates all appropriate parent folders for the file path specified
*
* @param filePath The file in which to create parent directories for
* @return Whether or not the parent folder creation was successful
*/
static boolean createParentFolder(String filePath) {
String parentDirectory = getParentDirectory(filePath);
File f = new File(parentDirectory);
try {
f.mkdirs();
return f.exists();
}
catch(SecurityException e) {
log.error("Error while creating parent directories for: {}", filePath, e);
}
return false;
}

/**
* Returns whether or not a file exists
*
Expand Down Expand Up @@ -295,7 +276,8 @@ static boolean writeArrayToFile(String filePath, String[] array) {
* @param filePath The full file path to set the execute flag on
* @return <code>true</code> if successful, <code>false</code> otherwise
*/
static boolean setExecutable(String filePath) {
@SuppressWarnings("ResultOfMethodCallIgnored")
private static boolean setExecutable(String filePath) {
if (!SystemUtilities.isWindows()) {
try {
File f = new File(filePath);
Expand All @@ -314,7 +296,7 @@ static boolean setExecutable(String filePath) {
/**
* Gets the path to qz-tray.properties
*/
public static String detectPropertiesPath() {
private static String detectPropertiesPath() {
// Use supplied path from IDE or command line
// i.e -DsslPropertiesFile=C:\qz-tray.properties
String override = System.getProperty("sslPropertiesFile");
Expand All @@ -326,7 +308,6 @@ public static String detectPropertiesPath() {
String propFile = Constants.PROPS_FILE + ".properties";

// Use relative path based on qz-tray.jar, fix %20
// TODO: Find a better way to fix the unicode chars?
return fixWhitespaces(getParentDirectory(jarPath) + File.separator + propFile);
}

Expand Down Expand Up @@ -356,7 +337,7 @@ public static Properties loadTrayProperties() {
* @return A String value representing the absolute path to the currently running
* jar
*/
public static String detectJarPath() {
private static String detectJarPath() {
try {
return new File(DeployUtilities.class.getProtectionDomain()
.getCodeSource().getLocation().getPath()).getCanonicalPath();
Expand All @@ -379,15 +360,6 @@ public String getJarPath() {
return jarPath;
}

/**
* Set the jar path for which we will create a shortcut for
*
* @param jarPath The full file path of the jar file
*/
public void setJarPath(String jarPath) {
this.jarPath = jarPath;
}

/**
* Small Enum for differentiating "desktop" and "startup"
*/
Expand Down
97 changes: 51 additions & 46 deletions src/qz/deploy/LinuxDeploy.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,97 +10,102 @@

package qz.deploy;

import java.awt.Toolkit;
import java.lang.reflect.Field;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qz.common.Constants;
import qz.utils.ShellUtilities;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import qz.utils.*;

/**
* @author Tres Finocchiaro
*/
public class LinuxDeploy extends DeployUtilities {
class LinuxDeploy extends DeployUtilities {

private static final Logger log = LoggerFactory.getLogger(LinuxDeploy.class);

private static String STARTUP = "/etc/xdg/autostart/";
private static String DESKTOP = System.getProperty("user.home") + "/Desktop/";
private static String DELETED_ICON = "process-stop";

private String appLauncher = "/usr/share/applications/" + getShortcutName();

@Override
public boolean createStartupShortcut() {
ShellUtilities.execute(new String[] {
"mkdir", "-p", System.getProperty("user.home") + "/.config/autostart/"
});
return createShortcut(System.getProperty("user.home") + "/.config/autostart/");
return copyShortcut(appLauncher, STARTUP);
}

@Override
public boolean createDesktopShortcut() {
return createShortcut(System.getProperty("user.home") + "/Desktop/");
return copyShortcut(appLauncher, DESKTOP);
}

@Override
public boolean removeStartupShortcut() {
return deleteFile(System.getProperty("user.home") + "/.config/autostart/" + getShortcutName());
return writeArrayToFile(STARTUP + getShortcutName(), new String[]{
"[Desktop Entry]",
"Type=Application",
"Name=" + Constants.ABOUT_TITLE + " (Disabled)",
"Exec=/bin/true",
"Icon=" + DELETED_ICON,
"Terminal=false",
});
}

@Override
public boolean removeDesktopShortcut() {
return deleteFile(System.getProperty("user.home") + "/Desktop/" + getShortcutName());
return deleteFile(DESKTOP + getShortcutName());
}


@Override
public boolean hasStartupShortcut() {
String target = System.getProperty("user.home") + "/.config/autostart/";
upgradeLegacyShortcut(target);
return fileExists(target + getShortcutName());
try {
String contents = FileUtilities.readLocalFile(STARTUP + getShortcutName());
return !contents.contains("Icon=" + DELETED_ICON);
} catch (Exception ex) {
log.warn("Unable to read startup shortcut {}", STARTUP + getShortcutName(), ex);
}
return false;
}

@Override
public boolean hasDesktopShortcut() {
String target = System.getProperty("user.home") + "/Desktop/";
upgradeLegacyShortcut(target);
return fileExists(target + getShortcutName());
// Upgrade legacy "QZ Tray.desktop" shortcuts
String shortcut = DESKTOP + Constants.ABOUT_TITLE + ".desktop";
if (fileExists(shortcut)) {
if (ShellUtilities.execute(new String[] { "rm", shortcut })) {
copyShortcut(appLauncher, DESKTOP);
}
}
return fileExists(DESKTOP + getShortcutName());
}

/**
* Creates a Linux ".desktop" shortcut
*
* @param target target location of shortcut
* @return Whether or not the shortcut was created successfully
*/
public boolean createShortcut(String target) {
private static boolean copyShortcut(String source, String target) {
return ShellUtilities.execute(new String[] {
"cp", getAppPath(), target
"cp", source, target
});
}

@Override
public String getShortcutName() {
return Constants.PROPS_FILE + ".desktop";
}

/**
* Returns the path to the jar executable or desktop launcher
* @return
*/
public String getAppPath() {
return "/usr/share/applications/" + getShortcutName();
public void setShortcutName(String name) {
super.setShortcutName(name);
// Fix window titles on Gnome 3 per JDK-6528430
try {
Toolkit t = Toolkit.getDefaultToolkit();
Field f = t.getClass().getDeclaredField("awtAppClassName");
f.setAccessible(true);
f.set(t, name);
}
catch (Exception ignore) {}
}

/**
* Upgrade 1.9 shortcut to new 2.0 format
* @return
*/
private boolean upgradeLegacyShortcut(String target) {
String shortcut = target + Constants.ABOUT_TITLE + ".desktop";
if (fileExists(shortcut)) {
if (ShellUtilities.execute(new String[] { "rm", shortcut })) {
return createShortcut(target);
}
}
return false;
@Override
public String getShortcutName() {
return Constants.PROPS_FILE + ".desktop";
}
}

Loading

0 comments on commit 16127c0

Please sign in to comment.