// // Menulet.swift // XCode Perforce // // Created by Rob Crawford on 8/26/16. // Copyright © 2016 Rob Crawford. All rights reserved. // import Cocoa class Menulet: NSObject, NSMenuDelegate { var p4Functionatlity: P4Functionality = P4Functionality() var p4Icon: NSImage? @IBOutlet weak var preferencesWindow: NSWindow! @IBOutlet weak var p4PathEditBox: NSTextField! @IBOutlet weak var p4vPathEditBox: NSTextField! var statusItem:NSStatusItem! @IBOutlet weak var menu: NSMenu! @IBAction func p4info(sender: AnyObject) { let currentDoc = XCodeHelper.currentProjectURL() if currentDoc == nil { return } if p4Functionatlity.p4ClientSet() == true { let filePath = XCodeHelper.pathToCurrentDocument() as NSString let parentFolder = filePath.stringByDeletingLastPathComponent var resultMessage = p4Functionatlity.issueP4InfoForFolder(parentFolder) if resultMessage.isEmpty { resultMessage = "Error detected while attempting to query p4 info" } displayCustomAlertBox("p4 info", informativeText: resultMessage) } else { displayDefaultP4NotSetError() } } func p4editFile(filePath: String) { let currentDoc = XCodeHelper.currentProjectURL() if currentDoc == nil { return } if p4Functionatlity.p4ClientSet() == true { // let filePath = XCodeHelper.pathToCurrentDocument() let resultMessage = p4Functionatlity.issueP4EditForFile(filePath) displayCustomAlertBox("p4 edit", informativeText: resultMessage) } else { displayDefaultP4NotSetError() } } @IBAction func p4edit(sender: AnyObject) { let currentDoc = XCodeHelper.currentProjectURL() if currentDoc == nil { return } if p4Functionatlity.p4ClientSet() == true { let filePath = XCodeHelper.pathToCurrentDocument() let resultMessage = p4Functionatlity.issueP4EditForFile(filePath!) displayCustomAlertBox("p4 edit", informativeText: resultMessage) } else { displayDefaultP4NotSetError() } } @IBAction func p4add(sender: AnyObject) { let currentDoc = XCodeHelper.currentProjectURL() if currentDoc == nil { return } if p4Functionatlity.p4ClientSet() == true { let filePath = XCodeHelper.pathToCurrentDocument() let resultMessage = p4Functionatlity.issueP4AddForFile(filePath!) displayCustomAlertBox("p4 add", informativeText: resultMessage) } else { displayDefaultP4NotSetError() } } @IBAction func p4revert(sender: AnyObject) { let currentDoc = XCodeHelper.currentProjectURL() if currentDoc == nil { return } if p4Functionatlity.p4ClientSet() == true { let a = NSAlert() let fileName: NSString = XCodeHelper.pathToCurrentDocument() a.addButtonWithTitle("Cancel") a.addButtonWithTitle("Revert") a.messageText = "p4 revert"; a.informativeText = "Are you sure you want to revert " + fileName.lastPathComponent + ", this is not undoable." if p4Icon != nil { a.icon = p4Icon } let response = a.runModal(); if (response == NSAlertSecondButtonReturn) { let filePath = XCodeHelper.pathToCurrentDocument() let resultMessage = p4Functionatlity.issueP4RevertForFile(filePath!) displayCustomAlertBox("p4 revert", informativeText: resultMessage) } } else { displayDefaultP4NotSetError() } } @IBAction func p4submit(sender: AnyObject) { let currentDoc = XCodeHelper.currentProjectURL() if currentDoc == nil { return } if p4Functionatlity.p4ClientSet() == true { let a = NSAlert() let fileName: NSString = XCodeHelper.pathToCurrentDocument() a.messageText = "Submit file: " + fileName.lastPathComponent; a.informativeText = "Please Enter Submit Comment:" a.addButtonWithTitle("Submit") a.addButtonWithTitle("Cancel") if p4Icon != nil { a.icon = p4Icon } let inputTextField = NSTextView(frame: NSRect(x: 0, y: 0, width: 300, height: 24*4)) a.accessoryView = inputTextField let response = a.runModal(); if response == NSAlertFirstButtonReturn { let enteredString = inputTextField.string if (!(enteredString?.isEmpty)!) { let filePath = XCodeHelper.pathToCurrentDocument() let resultMessage = p4Functionatlity.issueP4SubmitForFile(filePath!, withDescription: enteredString!) displayCustomAlertBox("p4 Submit", informativeText: resultMessage) } else { displayCustomAlertBox("p4 Submit", informativeText: "p4 Submit requires a description!") } } } else { displayDefaultP4NotSetError() } } func installStatusItem() { if (statusItem == nil) { statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength) statusItem.highlightMode = true statusItem.enabled = true statusItem.toolTip = "XCode Perforce" statusItem.title = "" statusItem.menu = menu; let myImage = NSImage.init(named: "AppIcon") myImage!.size = NSMakeSize(16.0, 16.0) statusItem.image = myImage p4Icon = myImage } } override func awakeFromNib() { installStatusItem() } func displayCustomAlertBox(messageText:NSString?, informativeText:NSString?) { let alertBox = NSAlert() alertBox.messageText = (messageText as! String) alertBox.informativeText = (informativeText as! String) if p4Icon != nil { alertBox.icon = p4Icon } alertBox.runModal() } func displayDefaultP4NotSetError() { var errorMessage = "" if p4Functionatlity.p4Installed == false { errorMessage = "The p4 command line app is not installed" } if p4Functionatlity.p4vcInstalled == false { if errorMessage.isEmpty == false { errorMessage += "\n" } errorMessage += "The p4vc command line app is not installed" } if p4Functionatlity.P4CONFIG_Set == false { if errorMessage.isEmpty == false { errorMessage += "\n" } errorMessage += "The P4CONFIG environment variable is not set" } displayCustomAlertBox("Error", informativeText: errorMessage) } @IBAction func preferencesCancel(sender: AnyObject) { preferencesWindow.orderOut(NSApplication.sharedApplication()) NSApplication.sharedApplication().stopModal() } @IBAction func preferencesOK(sender: AnyObject) { let p4path = self.p4PathEditBox.stringValue let p4vcpath = self.p4vPathEditBox.stringValue if p4path.characters.count != 0{ NSUserDefaults.standardUserDefaults().setObject(p4path, forKey: "P4PATH") } if p4vcpath.characters.count != 0 { NSUserDefaults.standardUserDefaults().setObject(p4vcpath, forKey: "P4VCPATH") } NSUserDefaults.standardUserDefaults().synchronize() p4Functionatlity.updateP4Status() p4Functionatlity.updateP4VCStatus() preferencesWindow.orderOut(NSApplication.sharedApplication()) NSApplication.sharedApplication().stopModal() } @IBAction func doPreferences(sender: AnyObject) { self.p4PathEditBox.stringValue = NSUserDefaults.standardUserDefaults().stringForKey("P4PATH")! self.p4vPathEditBox.stringValue = NSUserDefaults.standardUserDefaults().stringForKey("P4VCPATH")! preferencesWindow.center() preferencesWindow.makeKeyAndOrderFront(NSApplication.sharedApplication()); NSApplication.sharedApplication().runModalForWindow(preferencesWindow); } func menuNeedsUpdate(menu: NSMenu) { var menuEnabled:Bool = false; let runningApps = NSWorkspace.sharedWorkspace().runningApplications if let aeApp = runningApps.filter( { ($0.bundleIdentifier != nil) && ($0.bundleIdentifier! == "com.apple.dt.Xcode") }).first { menuEnabled = true; } if menuEnabled == true { if p4Functionatlity.p4ClientSet() == false { menuEnabled = false; } } for index in 0...5 { let myItem:NSMenuItem = menu.itemArray[index]; myItem.enabled = menuEnabled; } } }