const { app, BrowserWindow, ipcMain, dialog } = require('electron'); const path = require('path'); const fs = require('fs'); const exec = require('child_process').exec; const spawn = require('child_process').spawn; const version = require('./package.json').version; //TODO: Search for update const axios = require('axios'); async function getLatestRelease() { try { const https = require('https'); return new Promise((resolve, reject) => { https.get('https://codefirst.iut.uca.fr/git/api/v1/repos/mathis.chirat/Wallify/releases?pre-release=true&limit=1&access_token=043f410b985c5dc4b58f49c80661a4b90afe638b', (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { try { resolve(JSON.parse(data)); } catch (error) { reject(error); } }); }).on('error', (error) => { reject(error); }); }); } catch (error) { if (error.response && error.response.status === 502) { console.error('Error fetching JSON data: AxiosError: Request failed with status code 502'); } else { console.error('Error fetching JSON data:', error); } return null; } } getLatestRelease().then(latestRelease => { if(latestRelease && latestRelease[0].tag_name != version) { console.log('New version available:', latestRelease[0].tag_name); console.log('Cuurent version:', version); dialog.showMessageBox({ type: 'info', title: 'New version available', message: 'A new version of Wallify is available. Do you want to download it now?', buttons: ['Yes', 'No'], defaultId: 0, cancelId: 1 }).then(result => { if (result.response === 0) { // If 'Yes' is clicked const newAppImagePath = '/home/UCA/machirat1/public/Wallify/Wallify.AppImage'; exec(`cp ${newAppImagePath} ${path.join(__dirname, 'Wallify.AppImage')}`, (error, stdout, stderr) => { if (error) { console.error('Error copying the new version:', error); } else { console.log('New version copied successfully.'); } }); } }); } }).catch(error => { console.error('Error fetching JSON data:', error); }); //Setup Wallify config folder if(!fs.existsSync(app.getPath('userData'))) { console.log('Creating user data directory...'); fs.mkdirSync(app.getPath('userData'), { recursive: true }, (err) => { if (err) { console.error('Error creating user data directory:', err); process.exit(1); } }); } // Set wallpaper directory const wallpaperDir = path.join(app.getPath('userData'), 'wallpapers'); // Create wallpaper directory if it doesn't exist if (!fs.existsSync(wallpaperDir)) { console.log('Creating wallpaper directory...'); fs.mkdirSync(wallpaperDir); } // Create start.sh file const startFile = path.join(app.getPath('userData'), 'start.sh'); if (!fs.existsSync(startFile)) { console.log('Creating start.sh file...'); const user = require('os').userInfo().username; fs.writeFileSync(startFile, `#!/bin/sh\n\nsleep 2\nWALLPAPER_FILE=\"$(cat /home/UCA/${user}/.config/Wallify/wallpaper.conf`+" | sed 's/WALLPAPER_FILE=//g')\"\n/home/UCA/machirat1/public/WallPaper/wallify -ni -fs -s -sp -st -b -nf -- mpv -wid WID --loop --no-audio ${WALLPAPER_FILE} &", { mode: 0o755 }); exec(`chmod +x ${startFile}`, (error, stdout, stderr) => { if (error) { console.error(`Error applying execute permission to start.sh: ${error}`); } }); } // Create autostartup file const autostartupFile = path.join(app.getPath('home'), '.config', 'autostart', 'wallify.desktop'); if (!fs.existsSync(autostartupFile)) { console.log('Creating autostartup file...'); const user = require('os').userInfo().username; fs.writeFileSync(autostartupFile, `[Desktop Entry] Type=Application Version=1.0 Name=Wallify Comment=Wallify Exec=/home/UCA/${user}/.config/Wallify/start.sh `, { mode: 0o755 }); } // Create wallpaper.conf file const configFile = path.join(app.getPath('userData'), 'wallpaper.conf'); if(!fs.existsSync(configFile)) { console.log('Creating wallpaper.conf file...'); fs.writeFileSync(configFile, ''); } // Create main window let mainWindow; function createWindow() { mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true, contextIsolation: false, }, icon: __dirname + '/Wallify.png' }); mainWindow.setAutoHideMenuBar(true); mainWindow.setMenuBarVisibility(false); //mainWindow.setMenu(null); mainWindow.loadURL(`file://${__dirname}/index.html`); // Handle wallpaper apply command ipcMain.on('apply-wallpaper', (event, wallpaperPath) => { applyWallpaper(wallpaperPath); }); // Handle show dialog command ipcMain.on('show-dialog', (event) => { dialog.showOpenDialog({ title: 'Select a wallpaper file', filters: [ { name: 'Wallpaper files', extensions: ['gif', 'mp4'] } ], properties: ['openFile'] }).then((result) => { if (result.filePaths.length > 0) { event.sender.send('dialog-response', result.filePaths[0]); } else { event.sender.send('dialog-response', null); } }); }); } // Apply wallpaper using command function applyWallpaper(wallpaperPath) { const command = `gsettings set org.gnome.desktop.background picture-uri 'file://${wallpaperPath}'`; exec(command, (error, stdout, stderr) => { if (error) { console.error(`Error applying wallpaper: ${error}`); } else { console.log(`Wallpaper applied successfully`); } }); } // Create window on app ready app.on('ready', createWindow); // Quit app on window close app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); // Open window on app activate (MacOS) app.on('activate', () => { if (mainWindow === null) { createWindow(); } });