let pathToFile = ''; let fetchUsersInterval = null; document.addEventListener('DOMContentLoaded', async function () { const selectFileButton = document.getElementById('selectFile'); const submitButton = document.getElementById('submitButton'); const backButton = document.getElementById('backButton'); selectFileButton.addEventListener('click', async function (event) { event.preventDefault(); try { pathToFile = await window.electronAPI.selectFile(); updateFileName(); } catch (error) { console.error('Error opening file dialog:', error); } }); submitButton.addEventListener('click', async function (event) { event.preventDefault(); // Check if a file was chosen if (!pathToFile.trim()) { await window.electronAPI.showAlert('File not chosen!'); return; } const user_info = await window.electronAPI.readUserConfig('user_info'); if (!user_info) { return; } const form = document.getElementById('userDestForm'); const checkboxes = form.querySelectorAll('input[name="users"]'); const selectedUserIps = Array.from(checkboxes) .filter(checkbox => checkbox.checked) .map(checkbox => checkbox.value); // Get IP of the selected users if (!selectedUserIps.length) { await window.electronAPI.showAlert('No user selected!'); return; } for (const selectedUserIp of selectedUserIps) { try { // Add task to send file to the queue const task = { ip: selectedUserIp, // Destination IP for the file path: pathToFile, // File path userName: user_info.name // Sender's username from userConfig }; await window.electronAPI.addTaskToSendFileQueue(task); await window.electronAPI.showAlert('File sent to the queue.'); console.log(`Task added to send file to IP ${selectedUserIp}`); } catch (error) { console.error(`Error processing IP ${selectedUserIp}:`, error); } } }); backButton.addEventListener('click', function (event) { event.preventDefault(); fadeOut('main_menu'); }); setInterval(fetchUsersAndCreateCheckboxes, 5000); }); // Function to update the file name display function updateFileName() { const fileNameElement = document.getElementById('selectFile'); if (!fileNameElement) { return; } if (pathToFile.trim() === '') { fileNameElement.textContent = 'No file chosen'; } else { fileNameElement.textContent = pathToFile.split('\\').pop().split('/').pop(); } } async function fetchUsersAndCreateCheckboxes() { // Step 1: Get the currently checked users before refreshing the list const selectedUserIps = new Set( Array.from(document.querySelectorAll('input[name="users"]:checked')) .map(checkbox => checkbox.value) ); const usersInfoId = await window.electronAPI.readApplicationInfo('active_users_info'); if (!usersInfoId) { await window.electronAPI.showAlert('Internal error.'); return; } const usersInfo = await window.electronAPI.readMemoryEntry(usersInfoId); if (!usersInfo || usersInfo.length === 0) { await window.electronAPI.showAlert('No active users found.'); clearInterval(fetchUsersInterval); fetchUsersInterval = null; await window.electronAPI.changeContent('main_menu'); return; } const usersDiv = document.getElementById('choose_user_form_content'); usersDiv.innerHTML = ''; // Clear the list before updating usersInfo.forEach(user => { const checkbox = document.createElement('input'); checkbox.type = 'checkbox'; checkbox.name = 'users'; checkbox.value = user.ip; // Store user's IP in the value // Step 2: Check if this user was previously selected if (selectedUserIps.has(user.ip)) { checkbox.checked = true; // Restore checked state } const label = document.createElement('label'); label.innerHTML = `${user.user_info.name}`; // Display user's name label.insertBefore(checkbox, label.firstChild); usersDiv.appendChild(label); }); toggleScroll('choose_user_form_content'); }