Now compares gnome shell version w/ installed plugin and auto-updates
gnome extension during run if necessary
This commit is contained in:
parent
af1fdcca2c
commit
f7e9660233
|
@ -19,14 +19,7 @@ import dorkbox.util.Property;
|
|||
import dorkbox.util.process.ShellProcessBuilder;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.*;
|
||||
|
||||
public
|
||||
class GnomeShellExtension {
|
||||
|
@ -61,12 +54,94 @@ class GnomeShellExtension {
|
|||
boolean hasTopIcons = output.contains("topIcons@adel.gadllah@gmail.com");
|
||||
boolean hasSystemTray = output.contains(UID);
|
||||
|
||||
if (hasTopIcons) {
|
||||
// topIcons will convert ALL icons to be at the top of the screen, so there is no reason to have both installed
|
||||
if (!hasTopIcons && !hasSystemTray) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// have to copy the extension over and enable it.
|
||||
String userHome = System.getProperty("user.home");
|
||||
|
||||
// where the extension is saved
|
||||
final File file = new File(userHome + "/.local/share/gnome-shell/extensions/" + UID);
|
||||
final File metaDatafile = new File(file, "metadata.json");
|
||||
final File extensionFile = new File(file, "extension.js");
|
||||
|
||||
|
||||
// have to create the metadata.json file (and make it so that it's **always** current).
|
||||
// we do this via getting the shell version
|
||||
|
||||
// GNOME Shell 3.14.1
|
||||
String versionOutput = shellVersionString.replaceAll("[^\\d.]", ""); // should just be 3.14.1 or 3.20 or similar
|
||||
|
||||
// We want "3.14" or "3.20" or whatever the latest version is (excluding the patch version info).
|
||||
final int indexOf = versionOutput.indexOf('.');
|
||||
final int nextIndexOf = versionOutput.indexOf('.', indexOf + 1);
|
||||
if (indexOf < nextIndexOf) {
|
||||
versionOutput = versionOutput.substring(0, nextIndexOf);
|
||||
}
|
||||
|
||||
String metadata = "{\n" +
|
||||
" \"description\": \"Shows a java tray icon on the top notification tray\",\n" +
|
||||
" \"name\": \"Dorkbox SystemTray\",\n" +
|
||||
" \"shell-version\": [\n" +
|
||||
" \"" + versionOutput + "\"\n" +
|
||||
" ],\n" +
|
||||
" \"url\": \"https://github.com/dorkbox/SystemTray\",\n" +
|
||||
" \"uuid\": \"" + UID + "\",\n" +
|
||||
" \"version\": 1\n" +
|
||||
"}\n";
|
||||
|
||||
|
||||
if (hasSystemTray) {
|
||||
// have to check to see if the version is correct as well (otherwise we have to reinstall it)
|
||||
|
||||
StringBuilder builder = new StringBuilder(256);
|
||||
BufferedReader bin = null;
|
||||
try {
|
||||
bin = new BufferedReader(new FileReader(metaDatafile));
|
||||
String line;
|
||||
while ((line = bin.readLine()) != null) {
|
||||
builder.append(line)
|
||||
.append("\n");
|
||||
}
|
||||
} finally {
|
||||
if (bin != null) {
|
||||
try {
|
||||
bin.close();
|
||||
} catch (IOException ioe) {
|
||||
System.err.println("Error closing the metadata file:" + bin);
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// the metadata string we CHECK should equal the metadata string we PROVIDE
|
||||
if (metadata.equals(builder.toString())) {
|
||||
// this means that our version info, etc. is the same - there is no need to update anything
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BufferedWriter outputWriter = null;
|
||||
try {
|
||||
outputWriter = new BufferedWriter(new FileWriter(metaDatafile, false));
|
||||
// FileWriter always assumes default encoding is OK
|
||||
outputWriter.write(metadata);
|
||||
outputWriter.flush();
|
||||
outputWriter.close();
|
||||
} finally {
|
||||
if (outputWriter != null) {
|
||||
try {
|
||||
outputWriter.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!file.isDirectory()) {
|
||||
final boolean mkdirs = file.mkdirs();
|
||||
if (!mkdirs) {
|
||||
|
@ -76,10 +151,11 @@ class GnomeShellExtension {
|
|||
}
|
||||
}
|
||||
|
||||
// copies our provided extension.js file to the correct location on disk
|
||||
InputStream reader = null;
|
||||
FileOutputStream fileOutputStream = null;
|
||||
try {
|
||||
fileOutputStream = new FileOutputStream(new File(file, "extension.js"));
|
||||
fileOutputStream = new FileOutputStream(extensionFile);
|
||||
reader = GnomeShellExtension.class.getResourceAsStream("extension.js");
|
||||
|
||||
byte[] buffer = new byte[4096];
|
||||
|
@ -102,49 +178,10 @@ class GnomeShellExtension {
|
|||
}
|
||||
}
|
||||
|
||||
// have to create the metadata.json file (and make it so that it's **always** current).
|
||||
// we do this via getting the shell version
|
||||
|
||||
if (!hasSystemTray) {
|
||||
// now we have to enable us if we aren't already enabled
|
||||
|
||||
// GNOME Shell 3.14.1
|
||||
String versionOutput = shellVersionString.replaceAll("[^\\d.]", ""); // should just be 3.14.1
|
||||
|
||||
// now change to major version only (only if applicable)
|
||||
final int indexOf = versionOutput.indexOf('.');
|
||||
final int lastIndexOf = versionOutput.lastIndexOf('.');
|
||||
if (indexOf < lastIndexOf) {
|
||||
versionOutput = versionOutput.substring(0, indexOf);
|
||||
}
|
||||
|
||||
String metadata = "{\n" +
|
||||
" \"description\": \"Shows a java tray icon on the top notification tray\",\n" +
|
||||
" \"name\": \"Dorkbox SystemTray\",\n" +
|
||||
" \"shell-version\": [\n" +
|
||||
" \"" + versionOutput + "\"\n" +
|
||||
" ],\n" +
|
||||
" \"url\": \"https://github.com/dorkbox/SystemTray\",\n" +
|
||||
" \"uuid\": \"" + UID + "\",\n" +
|
||||
" \"version\": 1\n" +
|
||||
"}";
|
||||
|
||||
|
||||
BufferedWriter outputWriter = null;
|
||||
try {
|
||||
outputWriter = new BufferedWriter(new FileWriter(new File(file, "metadata.json"), false));
|
||||
// FileWriter always assumes default encoding is OK
|
||||
outputWriter.write(metadata);
|
||||
outputWriter.flush();
|
||||
outputWriter.close();
|
||||
} catch (Exception e) {
|
||||
if (outputWriter != null) {
|
||||
try {
|
||||
outputWriter.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// now we have to enable us
|
||||
// gsettings get org.gnome.shell enabled-extensions (['background-logo@fedorahosted.org'] on fedora 23) different on openSuse
|
||||
final StringBuilder stringBuilder = new StringBuilder(output);
|
||||
|
||||
|
@ -177,10 +214,10 @@ class GnomeShellExtension {
|
|||
setGsettings.addArgument("enabled-extensions");
|
||||
setGsettings.addArgument(stringBuilder.toString());
|
||||
setGsettings.start();
|
||||
|
||||
}
|
||||
|
||||
if (ENABLE_SHELL_RESTART) {
|
||||
logger.info("Restarting gnome-shell, so tray notification changes can be applied.");
|
||||
logger.info("Restarting gnome-shell so tray notification changes can be applied.");
|
||||
|
||||
// now we have to restart the gnome shell via bash
|
||||
final ShellProcessBuilder restartShell = new ShellProcessBuilder();
|
||||
|
@ -198,5 +235,4 @@ class GnomeShellExtension {
|
|||
logger.info("Shell restarted.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user