Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

Commit

Permalink
[+] Control mode
Browse files Browse the repository at this point in the history
[+] Fallback display for task name of notifications
  • Loading branch information
15cm committed Mar 20, 2017
1 parent 1998ed0 commit 8ccc4ac
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</subviews>
</view>
<color key="borderColor" white="0.90000000000000002" alpha="0.5" colorSpace="calibratedWhite"/>
<color key="fillColor" white="0.90000000000000002" alpha="0.5" colorSpace="calibratedWhite"/>
<color key="fillColor" white="0.84999999999999998" alpha="0.5" colorSpace="calibratedWhite"/>
</box>
</subviews>
<point key="canvasLocation" x="-117" y="-37"/>
Expand Down
46 changes: 38 additions & 8 deletions AMM/Menu/ServerProfileMenuItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
import Cocoa

class ServerProfileMenuItem: NSMenuItem, Aria2NotificationDelegate {
let pref = AMMPreferences.instance
var server: ServerProfile
var viewController: ServerProfileMenuItemViewController?
var notificationsShouldHandle = Set<Aria2Notifications>()
var timer: DispatchSourceTimer?
var startIndexOfActive: Int
init(_ profile: ServerProfile) {
server = profile
server.startConnectTimer()
viewController = ServerProfileMenuItemViewController(nibName: "ServerProfileMenuItemViewController", bundle: nil)
viewController?.server = server
startIndexOfActive = pref.controlModeEnabled ? 2 : 1 // Set beginning offset of task menu items
super.init(title: server.remark, action: nil, keyEquivalent: "")
self.view = viewController?.view

Expand All @@ -27,8 +30,18 @@ class ServerProfileMenuItem: NSMenuItem, Aria2NotificationDelegate {
if server.notiOnTaskCompleteEnabled { notificationsShouldHandle.insert(.onDownloadComplete) }
server.registerNofificationDelegate(delegate: self)

// Init fixed task array
submenu = NSMenu(title: "Tasks")
// Init control menu
if pref.controlModeEnabled {
let controlMenuItem = NSMenuItem(title: "Control", action: nil, keyEquivalent: "")
controlMenuItem.submenu = NSMenu(title: "Control")
let addUriMenuItem = NSMenuItem(title: "Add urls from clipboard(Each url on a seperate line)", action: #selector(ServerProfileMenuItem.addUriFromClipboard), keyEquivalent: "")
addUriMenuItem.target = self
controlMenuItem.submenu?.addItem(addUriMenuItem)
submenu?.addItem(controlMenuItem)
}

// Init fixed task menu items
submenu?.addItem(TaskMenuItemSeperator(name: "Active"))
for _ in 1...server.activeTaskMaxNum { submenu?.addItem(TaskMenuItem()) }
submenu?.addItem(TaskMenuItemSeperator(name: "Waiting"))
Expand All @@ -52,10 +65,8 @@ class ServerProfileMenuItem: NSMenuItem, Aria2NotificationDelegate {
[weak self] in
if let strongSelf = self {
if strongSelf.server.aria2?.status == .connected {
let startIndexOfActive = 1
let startIndexOfWaiting = startIndexOfActive + strongSelf.server.activeTaskMaxNum + 1
let startIndexOfWaiting = (self?.startIndexOfActive)! + strongSelf.server.activeTaskMaxNum + 1
let startIndexOfStopped = startIndexOfWaiting + strongSelf.server.waitingTaskMaxNum + 1

func updateTasksMenuItems(menuItem: ServerProfileMenuItem, tasks: [Aria2Task], startIndexInMenu: Int, maxNum: Int) {
for i in 0 ..< maxNum {
let indexInMenu = i + startIndexInMenu
Expand All @@ -68,7 +79,7 @@ class ServerProfileMenuItem: NSMenuItem, Aria2NotificationDelegate {
}

strongSelf.server.tellActive(callback: {tasks in
updateTasksMenuItems(menuItem: strongSelf, tasks: tasks, startIndexInMenu: startIndexOfActive, maxNum: strongSelf.server.activeTaskMaxNum)
updateTasksMenuItems(menuItem: strongSelf, tasks: tasks, startIndexInMenu: (self?.startIndexOfActive)!, maxNum: strongSelf.server.activeTaskMaxNum)
})
strongSelf.server.tellWaiting(callback: {tasks in
updateTasksMenuItems(menuItem: strongSelf, tasks: tasks, startIndexInMenu: startIndexOfWaiting, maxNum: strongSelf.server.waitingTaskMaxNum)
Expand All @@ -92,9 +103,28 @@ class ServerProfileMenuItem: NSMenuItem, Aria2NotificationDelegate {
for gid in gids {
let noti = NSUserNotification()
noti.title = server.remark
server.tellStatus(gid: gid, callback: {task in
noti.informativeText = "\(task.title) \(_type.toString())."
NSUserNotificationCenter.default.deliver(noti)
if _type == .onDownloadStart {
server.tellStatus(gid: gid, callback: {task in
noti.informativeText = "\(!task.title.isEmpty ? task.title : task.gid) \(_type.toString())."
NSUserNotificationCenter.default.deliver(noti)
})
}
}
}
}

func addUriFromClipboard() {
let urlText = NSPasteboard.general().string(forType: NSPasteboardTypeString)
let urls = urlText?.components(separatedBy: "\n").filter({!$0.isEmpty}).map({$0.trimmingCharacters(in: [" "])})
if let urls = urls {
for url in urls {
server.addUri(url: [url], callback: {res in
if let errorMsg = res["error"]["message"].string {
let noti = NSUserNotification()
noti.title = self.server.remark
noti.informativeText = "Error: \(errorMsg)"
NSUserNotificationCenter.default.deliver(noti)
}
})
}
}
Expand Down

0 comments on commit 8ccc4ac

Please sign in to comment.