<commit_before>'use strict';

const electron = require('electron');
const app = electron.app;
const path = require('path');

const menuTemplate = require('./menuTemplate.js');
const loadErrors = require('./dialogs/showLoadError.js');
const { isMac, getGuid } = require('./utils.js');
const log = require('./log.js')
const logLevels = require('./enums/logLevels.js');

// show dialog when certificate errors occur
require('./dialogs/showCertError.js');

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let windows = {};
let willQuitApp = false;
let isOnline = true;

// different preload script for main window and child windows.
// note: these files are generated by browserify prebuild process.
const preloadMainScript = path.join(__dirname, 'preload/_preloadMain.js');
const preloadChildScript = path.join(__dirname, 'preloda/_preloadChild.js');

function addWindowKey(key, browserWin) {
    windows[ key ] = browserWin;
}

function removeWindowKey(key) {
    delete windows[ key ];
}

function createMainWindow(url) {
    let key = getGuid();

    mainWindow = new electron.BrowserWindow({
        title: 'Symphony',
        width: 1024, height: 768,
        show: true,
        webPreferences: {
            sandbox: true,
            nodeIntegration: false,
            preload: preloadMainScript,
            winKey: key
        }
    });

    function retry() {
        if (!isOnline) {
            loadErrors.showNetworkConnectivityError(mainWindow, url, retry);
            return;
        }

        if (mainWindow.webContents) {
            mainWindow.webContents.reload();
        }
    }

    // content can be cached and will still finish load but
    // we might not have netowrk connectivity, so warn the user.
    mainWindow.webContents.on('did-finish-load', function() {
        if (!isOnline) {
            loadErrors.showNetworkConnectivityError(mainWindow, url, retry);
        } else {
            log.send(logLevels.INFO, 'main window loaded');
        }
    });

    mainWindow.webContents.on('did-fail-load', function(event, errorCode,
        errorDesc) {
        loadErrors.showLoadFailure(mainWindow, url, errorDesc, errorCode, retry);
    });

    addWindowKey(key, mainWindow);
    mainWindow.loadURL(url);

    const menu = electron.Menu.buildFromTemplate(menuTemplate(app));
    electron.Menu.setApplicationMenu(menu);

    mainWindow.on('close', function(e) {
        if (willQuitApp) {
            destroyMainWindow();
            return;
        }
        // mac should hide window when hitting x close
        if (isMac) {
            mainWindow.hide();
            e.preventDefault();
        }
    });

    function destroyMainWindow() {
        removeWindowKey(key);
        if (mainWindow) {
            mainWindow.removeAllListeners();
            if (mainWindow.webContents) {
                mainWindow.webContents.removeAllListeners();
            }
            mainWindow = null;
        }
    }

    mainWindow.on('closed', destroyMainWindow);

    // open external links in default browser - window.open
    mainWindow.webContents.on('new-window', function(event, newWinUrl) {
        event.preventDefault();
        electron.shell.openExternal(newWinUrl);
    });
}

app.on('before-quit', function() {
    willQuitApp = true;
});

function getMainWindow() {
    return mainWindow;
}

function showMainWindow() {
    mainWindow.show();
}

function isMainWindow(win) {
    return mainWindow === win;
}

function hasWindow(win, winKey) {
    if (win instanceof electron.BrowserWindow) {
        let browserWin = windows[ winKey ];
        return browserWin && win === browserWin;
    }

    return false;
}

function createChildWindow(url, title, width, height) {
    let winKey = getGuid();

    let childWindow = new electron.BrowserWindow({
        title: title,
        width: width,
        height: height,
        webPreferences: {
            sandbox: true,
            nodeIntegration: false,
            preload: preloadChildScript,
            winKey: winKey
        }
    });

    addWindowKey(winKey, childWindow);
    childWindow.loadURL(url);

    childWindow.on('closed', function() {
        removeWindowKey(winKey);
        if (childWindow) {
            childWindow.removeAllListeners();
            if (childWindow.webContents) {
                childWindow.webContents.removeAllListeners();
            }
        }
    });
}

function setIsOnline(status) {
    isOnline = status;
}

module.exports = {
    createMainWindow: createMainWindow,
    getMainWindow: getMainWindow,
    showMainWindow: showMainWindow,
    isMainWindow: isMainWindow,
    hasWindow: hasWindow,<commit_msg>fix crash on exit (#31)<commit_after>'use strict';

const electron = require('electron');
const app = electron.app;
const path = require('path');

const menuTemplate = require('./menuTemplate.js');
const loadErrors = require('./dialogs/showLoadError.js');
const { isMac, getGuid } = require('./utils.js');
const log = require('./log.js')
const logLevels = require('./enums/logLevels.js');

// show dialog when certificate errors occur
require('./dialogs/showCertError.js');

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let windows = {};
let willQuitApp = false;
let isOnline = true;

// different preload script for main window and child windows.
// note: these files are generated by browserify prebuild process.
const preloadMainScript = path.join(__dirname, 'preload/_preloadMain.js');
const preloadChildScript = path.join(__dirname, 'preloda/_preloadChild.js');

function addWindowKey(key, browserWin) {
    windows[ key ] = browserWin;
}

function removeWindowKey(key) {
    delete windows[ key ];
}

function createMainWindow(url) {
    let key = getGuid();

    mainWindow = new electron.BrowserWindow({
        title: 'Symphony',
        width: 1024, height: 768,
        show: true,
        webPreferences: {
            sandbox: true,
            nodeIntegration: false,
            preload: preloadMainScript,
            winKey: key
        }
    });

    function retry() {
        if (!isOnline) {
            loadErrors.showNetworkConnectivityError(mainWindow, url, retry);
            return;
        }

        if (mainWindow.webContents) {
            mainWindow.webContents.reload();
        }
    }

    // content can be cached and will still finish load but
    // we might not have netowrk connectivity, so warn the user.
    mainWindow.webContents.on('did-finish-load', function() {
        if (!isOnline) {
            loadErrors.showNetworkConnectivityError(mainWindow, url, retry);
        } else {
            log.send(logLevels.INFO, 'main window loaded');
        }
    });

    mainWindow.webContents.on('did-fail-load', function(event, errorCode,
        errorDesc) {
        loadErrors.showLoadFailure(mainWindow, url, errorDesc, errorCode, retry);
    });

    addWindowKey(key, mainWindow);
    mainWindow.loadURL(url);

    const menu = electron.Menu.buildFromTemplate(menuTemplate(app));
    electron.Menu.setApplicationMenu(menu);

    mainWindow.on('close', function(e) {
        if (willQuitApp) {
            destroyMainWindow();
            return;
        }
        // mac should hide window when hitting x close
        if (isMac) {
            mainWindow.hide();
            e.preventDefault();
        }
    });

    function destroyMainWindow() {
        removeWindowKey(key);
        if (mainWindow) {
            mainWindow.removeAllListeners();
            if (!mainWindow.isDestroyed() && mainWindow.webContents) {
                mainWindow.webContents.removeAllListeners();
            }
            mainWindow = null;
        }
    }

    mainWindow.on('closed', destroyMainWindow);

    // open external links in default browser - window.open
    mainWindow.webContents.on('new-window', function(event, newWinUrl) {
        event.preventDefault();
        electron.shell.openExternal(newWinUrl);
    });
}

app.on('before-quit', function() {
    willQuitApp = true;
});

function getMainWindow() {
    return mainWindow;
}

function showMainWindow() {
    mainWindow.show();
}

function isMainWindow(win) {
    return mainWindow === win;
}

function hasWindow(win, winKey) {
    if (win instanceof electron.BrowserWindow) {
        let browserWin = windows[ winKey ];
        return browserWin && win === browserWin;
    }

    return false;
}

function createChildWindow(url, title, width, height) {
    let winKey = getGuid();

    let childWindow = new electron.BrowserWindow({
        title: title,
        width: width,
        height: height,
        webPreferences: {
            sandbox: true,
            nodeIntegration: false,
            preload: preloadChildScript,
            winKey: winKey
        }
    });

    addWindowKey(winKey, childWindow);
    childWindow.loadURL(url);

    childWindow.on('closed', function() {
        removeWindowKey(winKey);
        if (childWindow) {
            childWindow.removeAllListeners();
            if (!childWindow.isDestroyed() && childWindow.webContents) {
                childWindow.webContents.removeAllListeners();
            }
        }
    });
}

function setIsOnline(status) {
    isOnline = status;
}

module.exports = {
    createMainWindow: createMainWindow,
    getMainWindow: getMainWindow,
    showMainWindow: showMainWindow,
    isMainWindow: isMainWindow,
    hasWindow: hasWindow,
{'fraction_non_alphanumeric': 0.11210575621942334, 'fraction_numerical': 0.0016654522743832622, 'mean_word_length': 2.605253283302064, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2077749', 'n_tokens_mistral': 2695, 'n_tokens_neox': 2629, 'n_words': 643}