Skip to content

Commit

Permalink
Add commands for pausing and resuming torrents
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamuko committed Jun 9, 2024
1 parent e15fd00 commit f8c16f5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
42 changes: 42 additions & 0 deletions Dreadnought/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ class TorrentClient: ObservableObject {
}
}
}

func forceResume(hashes: Set<String>) {
guard let url = baseURL?.appending(path: "api/v2/torrents/setForceStart"), let cookie = self.cookies else {
return
}
let headers: HTTPHeaders = ["Cookie": cookie]
let parameters = ["hashes": hashes.joined(separator: "|"), "value": "true"]
AF.request(url, method: .post, parameters: parameters, encoder: URLEncodedFormParameterEncoder.default, headers: headers).response { response in
if let error = response.error {
Logger.torrentClient.info("Error when pausing torrent(s): \(error)")
return
}
}
}

func loadPreferences() {
if let clientCookie = UserDefaults.standard.string(forKey: PreferenceNames.clientCookie) {
Expand Down Expand Up @@ -201,6 +215,20 @@ class TorrentClient: ObservableObject {
}
}

func pause(hashes: Set<String>) {
guard let url = baseURL?.appending(path: "api/v2/torrents/pause"), let cookie = self.cookies else {
return
}
let headers: HTTPHeaders = ["Cookie": cookie]
let parameters = ["hashes": hashes.joined(separator: "|")]
AF.request(url, method: .post, parameters: parameters, encoder: URLEncodedFormParameterEncoder.default, headers: headers).response { response in
if let error = response.error {
Logger.torrentClient.info("Error when pausing torrent(s): \(error)")
return
}
}
}

/// Update client state from given main data.
func processMainData(mainData: MainData) {
self.rid = mainData.rid
Expand Down Expand Up @@ -250,6 +278,20 @@ class TorrentClient: ObservableObject {
}
}

func resume(hashes: Set<String>) {
guard let url = baseURL?.appending(path: "api/v2/torrents/resume"), let cookie = self.cookies else {
return
}
let headers: HTTPHeaders = ["Cookie": cookie]
let parameters = ["hashes": hashes.joined(separator: "|")]
AF.request(url, method: .post, parameters: parameters, encoder: URLEncodedFormParameterEncoder.default, headers: headers).response { response in
if let error = response.error {
Logger.torrentClient.info("Error when pausing torrent(s): \(error)")
return
}
}
}

func setCategory(hashes: Set<String>, category: String) {
guard let url = baseURL?.appending(path: "api/v2/torrents/setCategory"), let cookie = self.cookies else {
return
Expand Down
12 changes: 12 additions & 0 deletions Dreadnought/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@ struct TorrentList: View {
.focusedValue(\.torrents, selectedTorrents)
.focusedValue(\.torrentActions, torrentActions)
.contextMenu(forSelectionType: Torrent.ID.self) { items in
Button("Resume") {
client.resume(hashes: items)
}
Button("Pause") {
client.pause(hashes: items)
}
Button("Force resume") {
client.forceResume(hashes: items)
}

Divider()

Button("Remove", role: .destructive) {
torrentActions.torrentsPendingRemoval = items
}
Expand Down
22 changes: 21 additions & 1 deletion Dreadnought/DreadnoughtApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,31 @@ extension FocusedValues {
struct TorrentCommands: Commands {
@FocusedValue(\.torrents) var torrents: TorrentSelection?
@FocusedValue(\.torrentActions) var torrentActions: TorrentActions?

let client: TorrentClient

var disabled: Bool { self.torrents?.isEmpty ?? true }

var body: some Commands {
CommandMenu("Torrent") {
Button("Resume") {
guard let torrents = torrents else { return }
client.resume(hashes: torrents)
}
.disabled(disabled)
Button("Pause") {
guard let torrents = torrents else { return }
client.pause(hashes: torrents)
}
.disabled(disabled)
Button("Force resume") {
guard let torrents = torrents else { return }
client.forceResume(hashes: torrents)
}
.disabled(disabled)

Divider()

Button("Remove") {
guard let torrents = torrents else { return }
torrentActions?.torrentsPendingRemoval = torrents
Expand Down Expand Up @@ -64,7 +84,7 @@ struct DreadnoughtApp: App {
}
.keyboardShortcut("1", modifiers: .command)
.commands {
TorrentCommands()
TorrentCommands(client: client)
CommandGroup(before: .importExport) {
Button("Make default for magnet links", action: self.makeDefaultMagnetHandler)
}
Expand Down

0 comments on commit f8c16f5

Please sign in to comment.