From 21b30a9369c231073ffffe36c76e09217bf1e12a Mon Sep 17 00:00:00 2001 From: nimbold <11913706+nimbold@users.noreply.github.com> Date: Wed, 3 Jun 2026 05:32:39 +0330 Subject: [PATCH] feat: add site logins integration to Add Downloads window --- Sources/Firelink/AddDownloadsView.swift | 53 +++++++++++++++++++++++ Sources/Firelink/DownloadController.swift | 3 +- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/Sources/Firelink/AddDownloadsView.swift b/Sources/Firelink/AddDownloadsView.swift index 2e5c75c..c76e305 100644 --- a/Sources/Firelink/AddDownloadsView.swift +++ b/Sources/Firelink/AddDownloadsView.swift @@ -21,6 +21,10 @@ struct AddDownloadsView: View { @State private var headerText = "" @State private var cookieText = "" @State private var mirrorText = "" + @State private var useAuthorization = false + @State private var authUsername = "" + @State private var authPassword = "" + @State private var saveLogin = false var body: some View { VStack(spacing: 0) { @@ -139,6 +143,30 @@ struct AddDownloadsView: View { } } } + + GridRow(alignment: .top) { + Label("Authorization", systemImage: "lock.shield") + .font(.headline) + .padding(.top, 4) + VStack(alignment: .leading, spacing: 10) { + Toggle("Use authorization", isOn: $useAuthorization) + .toggleStyle(.switch) + + if useAuthorization { + HStack(spacing: 10) { + TextField("Username", text: $authUsername) + .textFieldStyle(.roundedBorder) + .frame(width: 160) + SecureField("Password", text: $authPassword) + .textFieldStyle(.roundedBorder) + .frame(width: 160) + } + Toggle("Save login for this website", isOn: $saveLogin) + .font(.caption) + .foregroundStyle(.secondary) + } + } + } } } @@ -365,6 +393,13 @@ struct AddDownloadsView: View { ) } + if let firstURL = urls.first, let creds = settings.credentials(for: firstURL) { + useAuthorization = true + authUsername = creds.username + authPassword = creds.password + saveLogin = false + } + metadataTask = Task { var loaded: [PendingDownload] = [] for url in urls { @@ -400,12 +435,30 @@ struct AddDownloadsView: View { } private func addDownloads(start: Bool) { + var explicitCredentials: DownloadCredentials? = nil + if useAuthorization { + let cleanUsername = authUsername.trimmingCharacters(in: .whitespacesAndNewlines) + if !cleanUsername.isEmpty { + explicitCredentials = DownloadCredentials(username: cleanUsername, password: authPassword) + if saveLogin { + var savedHosts = Set() + for item in pendingDownloads { + if let host = item.url.host, !savedHosts.contains(host) { + settings.addSiteLogin(urlPattern: "*.\(host)", username: cleanUsername, password: authPassword) + savedHosts.insert(host) + } + } + } + } + } + controller.addPendingDownloads( pendingDownloads, connectionsPerServer: Int(connectionsPerServer), overrideDirectory: overrideDirectory, startImmediately: start, queueID: targetQueueID, + credentials: explicitCredentials, transferOptions: transferOptions, speedLimitKiBPerSecond: speedLimitEnabled ? speedLimitKiBPerSecond : nil ) diff --git a/Sources/Firelink/DownloadController.swift b/Sources/Firelink/DownloadController.swift index fce7a73..add2db5 100644 --- a/Sources/Firelink/DownloadController.swift +++ b/Sources/Firelink/DownloadController.swift @@ -116,6 +116,7 @@ final class DownloadController: ObservableObject { overrideDirectory: URL?, startImmediately: Bool, queueID: UUID = DownloadQueue.mainQueueID, + credentials: DownloadCredentials? = nil, transferOptions: DownloadTransferOptions = DownloadTransferOptions(), speedLimitKiBPerSecond: Int? = nil ) { @@ -130,7 +131,7 @@ final class DownloadController: ObservableObject { category: pending.category, destinationDirectory: overrideDirectory ?? pending.defaultDirectory, connectionsPerServer: clampedConnections, - credentials: settings.credentials(for: pending.url), + credentials: credentials ?? settings.credentials(for: pending.url), checksum: transferOptions.checksum, requestHeaders: transferOptions.requestHeaders, cookieHeader: transferOptions.cookieHeader,