document.addEventListener('DOMContentLoaded', async function () { setInterval(loadReceivedFiles, 3000); // Call loadReceivedFiles every 3000 ms (3 seconds) const backupButton = document.getElementById('backup-dir'); const shareButton = document.getElementById('share-dir'); const departmentButton = document.getElementById('department-dir'); const changeInfoButton = document.getElementById('change-info'); const shareFileButton = document.getElementById('share-file'); const manageUsersButton = document.getElementById('manage-users'); const manageDepartmentsButton = document.getElementById('manage-departments'); const resetDatabaseButton = document.getElementById('reset-database'); const restoreBackupButton = document.getElementById('restore-backup'); const logoutButton = document.getElementById('logout'); const disclaimerModal = document.getElementById('disclaimer-modal'); const cancelDisclaimerButton = document.getElementById('disclaimer-cancel'); const submitDisclaimerButton = document.getElementById('disclaimer-submit'); await fetchUserInfo(); await checkAndSetAllDirectories(); backupButton.addEventListener('click', async function () { await setPath('backupDirectory'); }); shareButton.addEventListener('click', async function () { await setPath('shareDirectory'); }); departmentButton.addEventListener('click', async function () { await setPath('departmentDirectory'); }); changeInfoButton.addEventListener('click', function () { fadeOut('profile'); }); shareFileButton.addEventListener('click', function () { fadeOut('share_file'); }); manageUsersButton.addEventListener('click', function () { fadeOut('users_management'); }); manageDepartmentsButton.addEventListener('click', function () { fadeOut('departments_management' + ''); }); resetDatabaseButton.addEventListener('click', function () { disclaimerModal.style.display = 'block'; // Show disclaimer modal }); cancelDisclaimerButton.addEventListener('click', function () { disclaimerModal.style.display = 'none'; // Hide modal if cancel is clicked }); submitDisclaimerButton.addEventListener('click', async function () { disclaimerModal.style.display = 'none'; // Hide modal await resetDatabase(); // Perform database reset fadeOut('login'); // Fade out and go back to login }); logoutButton.addEventListener('click', async function () { fadeOut('login'); }); restoreBackupButton.addEventListener('click', async function () { await restoreBackup(); }); async function restoreBackup() { const backupDirectory = await window.electronAPI.readApplicationInfo('backupDirectory'); if (backupDirectory && backupDirectory.path) { await window.electronAPI.showAlert('You have already set a backup directory. You cannot restore again.'); return; } const destinationPath = await window.electronAPI.selectDirectory(); if (!destinationPath) return; await window.electronAPI.startBackupRetrieval(destinationPath); fadeOut('backup_retrieve'); } async function checkAndSetAllDirectories() { await attachNotificationButton('backupDirectory', 'Set your backup directory!', 'backup_alert', 'alert'); await attachNotificationButton('shareDirectory', 'Set your share directory!', 'share_alert', 'alert'); await attachNotificationButton('departmentDirectory', 'Set your department directory!', 'department_alert', 'alert'); } async function checkPathExistence(pathKey) { return await window.electronAPI.readApplicationInfo(pathKey); } async function setPath(pathKey) { const path = await window.electronAPI.selectDirectory(); if (path === undefined) return; const id = await window.electronAPI.createMemoryEntry() console.log({id, path}) await window.electronAPI.writeApplicationInfo(pathKey, {id, path}); } async function attachNotificationButton(pathKey, buttonText, buttonId, buttonName) { const path = await checkPathExistence(pathKey); if (!path) { const notificationsDiv = document.getElementById('notifications'); const button = document.createElement('button'); button.id = buttonId; button.name = buttonName; button.textContent = buttonText; button.addEventListener('click', async function () { await setPath(pathKey); button.remove(); }); notificationsDiv.appendChild(button); } } async function fetchUserInfo() { const usernameField = document.getElementById('username-field'); let userInfo = await window.electronAPI.readUserConfig('user_info'); if (userInfo && userInfo.name) { usernameField.textContent = userInfo.name; } else { console.error("Username field is not available in the DOM."); } } async function loadReceivedFiles() { const shareDirectoryData = await window.electronAPI.readApplicationInfo('shareDirectory'); if (!shareDirectoryData || !shareDirectoryData.id) { console.log('No received files or directory ID found.'); return; } const directoryId = shareDirectoryData.id; const directoryStructure = await window.electronAPI.readMemoryEntry(directoryId); if (!directoryStructure || !directoryStructure.structure) { console.log('No received files found in the directory structure.'); return; } const notificationsDiv = document.getElementById('notifications'); const existingButtons = Array.from(notificationsDiv.children).map(button => button.getAttribute('data-filepath')); Object.keys(directoryStructure.structure).forEach(userName => { const userFiles = directoryStructure.structure[userName]; Object.keys(userFiles).forEach(fileName => { const filePath = userFiles[fileName]; if (!existingButtons.includes(filePath)) { const button = document.createElement('button'); button.setAttribute('data-filepath', filePath); button.name = 'notification'; button.textContent = `You received a file "${fileName}" from ${userName}`; button.addEventListener('click', () => handleFileReceivedButtonPressed(filePath, button)); notificationsDiv.appendChild(button); } }); }); } async function removeReceivedFile(filePath) { const shareDirectoryData = await window.electronAPI.readApplicationInfo('shareDirectory'); if (!shareDirectoryData || !shareDirectoryData.id) { console.log('No directory ID found to update.'); return; } const directoryId = shareDirectoryData.id; let directoryStructure = await window.electronAPI.readMemoryEntry(directoryId); if (directoryStructure && directoryStructure.structure) { for (let userName in directoryStructure.structure) { let userFiles = directoryStructure.structure[userName]; if (userFiles[filePath]) { delete userFiles[filePath]; if (Object.keys(userFiles).length === 0) { delete directoryStructure.structure[userName]; } await window.electronAPI.updateMemoryEntry(directoryId, directoryStructure); console.log(`File ${filePath} removed from memory.`); return; } } } else { console.log('No directory structure found in memory to update.'); } } async function handleFileReceivedButtonPressed(filePath, button) { await window.electronAPI.showFileInExplorer(filePath) .then(() => console.log('File explorer opened for: ' + filePath)) .catch(error => console.error('Error opening file explorer:', error)); button.remove(); await removeReceivedFile(filePath); } async function resetDatabase() { const operationCodes = await window.electronAPI.getOperationsCodes(); if (!operationCodes) { await window.electronAPI.showAlert('Internal error of the application. Failed to retrieve operation codes.'); return; } const codeOk = operationCodes.OK; const codeResetDatabase = operationCodes.RESET_DATABASE; if(!await window.electronAPI.openUcSocket()){ await window.electronAPI.showAlert('Internal application error.'); return; } // Send the request to reset the database if (!await window.electronAPI.sendUcMessage(codeResetDatabase)) { await window.electronAPI.showAlert('Failed to send reset database request.'); return; } // Wait for the response await waitForResponse(); await window.electronAPI.changeContent('login'); } });