Skip to content
Snippets Groups Projects
main.js 5.72 KiB
Newer Older
const { app, shell, net, BrowserWindow, Notification } = require('electron');
function createWindow () {
    // Create the browser window.
    win = new BrowserWindow({ width: 800, height: 600 });

    // and load the index.html of the app. 
    win.loadFile("./dist/index.html");

    // Hide menu bar.
    win.setMenuBarVisibility(false)

    // Open the DevTools.
    // win.webContents.openDevTools();
mathias.chouet's avatar
mathias.chouet committed

    // Open external doc links in specific window
    /* win.webContents.on('new-window', (event, url, frameName, disposition, options) => {
mathias.chouet's avatar
mathias.chouet committed
        event.preventDefault();
        // win.src = url;
        const win2 = new BrowserWindow({
            webContents: options.webContents, // use existing webContents if provided
            show: false
        });
        win2.webContents.openDevTools();
        win2.once('ready-to-show', () => win2.show());
        if (!options.webContents) {
            win2.loadURL(url); // existing webContents will be navigated automatically
        }
        event.newGuest = win2;

    // Close the app.
    win.on('close', () => {
        // @TODO does not render properly on Linux
        /* const choice = dialog.showMessageBox(this,
              buttons: ['Certes', 'Fuque'],
              title: 'Confirme-z-y voir',
              message: "T'es-tu sûr de vouloir quitter, là ?"
           }
        );
        if(choice == 1){
            e.preventDefault();
        } */
        win.destroy();
    });

    // Emitted when the window is closed.
    win.on('closed', () => {
        win = null;
    });

    // Try to detect updates
    lookForUpdates();
};

/**
 * Calls the Cassiopee update server to check for available updates;
 * if so, displays a desktop notification which, if clicked, will
 * trigger the download of the latest version installer for the
 * current platform
 * 
 * Web updates system prerequisites
 * --------------------------------
 * 
 * ${URL} should point to an http-accessible directory containing:
 * 
 *  1. a file named "releases.json" with a contents of the form
 *    {
 *      "latest": "4.5.0",
 *      "4.5.0": {
 *        "darwin": "cassiopee-setup-4.5.0.dmg",
 *        "linux": "fr.irstea.cassiopee_4.5.0_amd64.deb",
 *        "win32": "Cassiopée Setup 4.5.0.exe"
 *      },
 *      "4.4.2": {
 *        "darwin": "cassiopee-setup-4.4.2.dmg",
 *        "linux": "fr.irstea.cassiopee_4.4.2_amd64.deb",
 *        "win32": "Cassiopée Setup 4.4.2.exe"
 *      }
 *    }
 * 
 *  2. all platform-dependent installer files specified in "releases.json",
 *     at the root of the directory (ex: "fr.irstea.cassiopee_4.5.0_amd64.deb")
 */
const lookForUpdates = function() {
    // Web update resources root directory
    const URL = "http://aubes.montpellier.irstea.priv/cassiopee-releases/";

    // detect current app version and platform
    // const version = "4.4.2"; // debug
    const version = app.getVersion();
    const platform = process.platform; // "win32", "linux" or "darwin"
    // console.log(process.arch); // "x32" or "x64"

    // fetch releases information
    const req = net.request(URL + "releases.json");
    req.on("response", (response) => {
        if (response.statusCode === 200) {
            response.on('data', (chunk) => {
                const data = JSON.parse(chunk);
                // compare current version to latest version
                if (data.latest && data.latest > version) {
                    // get download link for latest version, depending on platform
                    if (data[data.latest] && data[data.latest][platform]) {
                        const latestVersionURL = URL + data[data.latest][platform];
                        // show notification on desktop
                        if (Notification.isSupported()) {
                            let notif = new Notification({
                                title: `Cassiopee version ${data.latest} is available`,
                                body: `Click to download update installer`,
                                icon: "electron/icon.png"

                            });
                            notif.addListener("click", () => {
                                // ask system to open URL with default browser
                                shell.openExternal(latestVersionURL);
                            });
                        }
                    } else {
                        console.error(`could not find download link for version ${data.latest} on platform ${platform}`);
                    }
                } // else you already have the latest version
                
            })
        } else {
            console.error("error fetching releases information");
        }
    });
    req.end();
};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', () => {
    createWindow();
    if (process.platform === 'win32') {      
        // for notifications to work on windows; see https://stackoverflow.com/a/53510850/5986614
        app.setAppUserModelId(app.getName());
    }
});

// Quit when all windows are closed.
app.on('window-all-closed', () => {
    // On macOS it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q
    if (process.platform !== 'darwin') {
        app.quit();
    }
});

app.on('activate', () => {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (win === null) {