document.addEventListener('DOMContentLoaded', async function () { let user_info = {}; let pathToFile = ''; user_info = await window.electronAPI.readUserConfig('user_info'); if (!user_info) { await window.electronAPI.showAlert('No user_info entry found.'); return; } // Function to update the file name display function updateFileName() { const fileNameElement = document.getElementById('fileName'); if (!fileNameElement) { return; } if (pathToFile.trim() === '') { fileNameElement.textContent = 'No file chosen'; } else { fileNameElement.textContent = pathToFile.split('\\').pop().split('/').pop(); } } updateFileName(); // Fetch user information from applicationInfo and create checkboxes for selecting users async function fetchUsersAndCreateCheckboxes() { 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.'); await window.electronAPI.changeContent('main_menu'); return; } const usersDiv = document.querySelector('.choose_user_form_content'); usersDiv.innerHTML = ''; 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 const label = document.createElement('label'); label.innerHTML = `${user.user_info.name}`; // Display user's name label.insertBefore(checkbox, label.firstChild); usersDiv.appendChild(label); }); } // Event listener for selecting the file document.getElementById('selectFile').addEventListener('click', async function () { try { pathToFile = await window.electronAPI.selectFile(); updateFileName(); } catch (error) { console.error('Error opening file dialog:', error); } }); // Event listener for back button to navigate to main menu document.getElementById('backButton').addEventListener('click', function () { fadeOut('main_menu'); }); // Submit button logic to queue file send tasks and then navigate to main_menu.html document.getElementById('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 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); console.log(`Task added to send file to IP ${selectedUserIp}`); } catch (error) { console.error(`Error processing IP ${selectedUserIp}:`, error); } } // Navigate back to the main menu after all tasks are queued fadeOut('main_menu'); // Navigate to the main menu }); // Fetch users and create checkboxes every 5 seconds (in case of updates) setInterval(fetchUsersAndCreateCheckboxes, 5000); fetchUsersAndCreateCheckboxes().then(() => console.log('User checkboxes rendered')); });