<commit_before>//
//  AppDelegate.swift
//  skugga
//
//  Created by arnaud on 25/01/2015.
//
//

import Cocoa

let statusIconHeight: CGFloat = 18.0
let statusIconWidth: CGFloat = 24.0

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate, NSDraggingDestination, NSUserNotificationCenterDelegate {
    
    @IBOutlet weak var window: NSWindow!

    @IBOutlet weak var popover: NSPopover!
    
    @IBOutlet weak var statusItemMenu: NSMenu!
    
    @IBOutlet weak var settingsWindow: SettingsWindow!
    
    // TODO : Maybe make a class that controls the statusbar icon
    var statusItem: NSStatusItem!
    
    var statusNormalImage: NSImage! // The smiling one ! Yeah it could be "I'm finished !" but I like it smiling all the time

    var statusEmptyImage: NSImage!
    
    var statusFinishedImage: NSImage!
    
    var statusProgressImage: NSImage! //Programatically drawn
    
    var notificationCenter: NSUserNotificationCenter!

    func applicationDidFinishLaunching(aNotification: NSNotification)
    {
        
        // Insert code here to initialize your application
        initStatusItem()
        
        // Make the popover close when the user clicks outside of it
        popover.behavior = NSPopoverBehavior.Transient
        
        // Set this if we want to force a light popover appearance
        //popover.appearance = NSAppearance(named: NSAppearanceNameVibrantLight)
        
        notificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()
        notificationCenter.delegate = self
        
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "uploadFromNotification",
            name: NSUserDefaultsDidChangeNotification,
            object: RMSharedUserDefaults.standardUserDefaults())
        
        refreshFileList()
    }

    func applicationWillTerminate(aNotification: NSNotification)
    {
        // Insert code here to tear down your application
    }

    // MARK: NSDraggingDestination protocol methods
    func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation
    {
        return NSDragOperation.Copy
    }
    
    func performDragOperation(sender: NSDraggingInfo) -> Bool
    {
        var pasteboard = sender.draggingPasteboard()
        if (pasteboard.types?.filter({$0 as NSString == NSURLPboardType}).count > 0)
        {
            var file = NSURL(fromPasteboard: pasteboard)
            uploadURL(file!)
        }
        
        return true
    }
    
    func refreshFileList()
    {
        FileListClient().getFileList({ (files: [RemoteFile]) -> () in
            if let controller = self.popover.contentViewController as? PopoverViewController
            {
                controller.refreshWithRemoteFiles(files)
            }
        }, failure: { (error: NSError) -> () in
            NSLog("Error while refreshing file list %@", error)
        })
    }
    
    func uploadFromNotification()
    {
        var defaults = RMSharedUserDefaults.standardUserDefaults()
        if let targetURL = defaults.URLForKey("shareExtensionURL")
        {
            defaults.removeObjectForKey("shareExtensionURL")
            uploadURL(targetURL)
        }
    }
    
    private func uploadURL(url: NSURL)
    {
        UploadClient().uploadFile(url, progress: { (bytesSent:Int64, bytesToSend:Int64) -> Void in
            self.drawStatusIconForProgress(Float(Double(bytesSent) / Double(bytesToSend)))
            }, success: { (data: [NSObject: AnyObject]) -> Void in
                var url = data["name"] as NSString
                url = Configuration.endpoint + url
                
                var pasteboard = NSPasteboard.generalPasteboard()
                pasteboard.clearContents()
                pasteboard.setString(url, forType: NSStringPboardType)
                
                NSLog("Upload succeeded ! \(url)")
                
                var notification = NSUserNotification()
                notification.title = "Skugga"
                notification.subtitle = "File uploaded : \(url)"
                notification.deliveryDate = NSDate()
                notification.soundName = "Glass.aiff"
                notification.userInfo = ["url": url]
                
                // Private API to have buttons on non-alert notifications
                // I don't get why Apple doesn't want us to have buttons on notifications that go away, but keeps
                // that for iTunes and Mail.app
                notification.setValue(true, forKey: "_showsButtons")
                
                notification.actionButtonTitle = "Open"
                
                self.notificationCenter.scheduleNotification(notification)
                
                self.setNormalStatusIcon()
                
                self.refreshFileList()
                
            }, failure: { (error: NSError) -> Void in
                NSLog("Upload failed ! \(error)")
                
                var errorSubtitle = "Error while uploading file"
                
                if let statusCode = error.userInfo?["statusCode"] as? Int
                {
                    errorSubtitle += " (\(statusCode))"
                }
                
                var notification = NSUserNotification()
                notification.title = "Skugga"
                notification.subtitle = errorSubtitle
                notification.deliveryDate = NSDate()
                notification.soundName = "Glass.aiff"
                
                self.notificationCenter.scheduleNotification(notification)
                
                self.setNormalStatusIcon()
            }
        )<commit_msg>Let it be<commit_after>//
//  AppDelegate.swift
//  skugga
//
//  Created by arnaud on 25/01/2015.
//
//

import Cocoa

let statusIconHeight: CGFloat = 18.0
let statusIconWidth: CGFloat = 24.0

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate, NSDraggingDestination, NSUserNotificationCenterDelegate {
    
    @IBOutlet weak var window: NSWindow!

    @IBOutlet weak var popover: NSPopover!
    
    @IBOutlet weak var statusItemMenu: NSMenu!
    
    @IBOutlet weak var settingsWindow: SettingsWindow!
    
    // TODO : Maybe make a class that controls the statusbar icon
    var statusItem: NSStatusItem!
    
    var statusNormalImage: NSImage! // The smiling one ! Yeah it could be "I'm finished !" but I like it smiling all the time

    var statusEmptyImage: NSImage!
    
    var statusFinishedImage: NSImage!
    
    var statusProgressImage: NSImage! //Programatically drawn
    
    var notificationCenter: NSUserNotificationCenter!

    func applicationDidFinishLaunching(aNotification: NSNotification)
    {
        
        // Insert code here to initialize your application
        initStatusItem()
        
        // Make the popover close when the user clicks outside of it
        popover.behavior = NSPopoverBehavior.Transient
        
        // Set this if we want to force a light popover appearance
        //popover.appearance = NSAppearance(named: NSAppearanceNameVibrantLight)
        
        notificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()
        notificationCenter.delegate = self
        
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "uploadFromNotification",
            name: NSUserDefaultsDidChangeNotification,
            object: RMSharedUserDefaults.standardUserDefaults())
        
        refreshFileList()
    }

    func applicationWillTerminate(aNotification: NSNotification)
    {
        // Insert code here to tear down your application
    }

    // MARK: NSDraggingDestination protocol methods
    func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation
    {
        return NSDragOperation.Copy
    }
    
    func performDragOperation(sender: NSDraggingInfo) -> Bool
    {
        var pasteboard = sender.draggingPasteboard()
        if (pasteboard.types?.filter({$0 as NSString == NSURLPboardType}).count > 0)
        {
            var file = NSURL(fromPasteboard: pasteboard)
            uploadURL(file!)
        }
        
        return true
    }
    
    func refreshFileList()
    {
        FileListClient().getFileList({ (files: [RemoteFile]) -> () in
            if let controller = self.popover.contentViewController as? PopoverViewController
            {
                controller.refreshWithRemoteFiles(files)
            }
        }, failure: { (error: NSError) -> () in
            NSLog("Error while refreshing file list %@", error)
        })
    }
    
    func uploadFromNotification()
    {
        var defaults = RMSharedUserDefaults.standardUserDefaults()
        if let targetURL = defaults.URLForKey("shareExtensionURL")
        {
            defaults.removeObjectForKey("shareExtensionURL")
            uploadURL(targetURL)
        }
    }
    
    private func uploadURL(url: NSURL)
    {
        UploadClient().uploadFile(url, progress: { (bytesSent:Int64, bytesToSend:Int64) -> Void in
            self.drawStatusIconForProgress(Float(Double(bytesSent) / Double(bytesToSend)))
            }, success: { (data: [NSObject: AnyObject]) -> Void in
                var url = data["name"] as NSString
                url = Configuration.endpoint + url
                
                let pasteboard = NSPasteboard.generalPasteboard()
                pasteboard.clearContents()
                pasteboard.setString(url, forType: NSStringPboardType)
                
                NSLog("Upload succeeded ! \(url)")
                
                let notification = NSUserNotification()
                notification.title = "Skugga"
                notification.subtitle = "File uploaded : \(url)"
                notification.deliveryDate = NSDate()
                notification.soundName = "Glass.aiff"
                notification.userInfo = ["url": url]
                
                // Private API to have buttons on non-alert notifications
                // I don't get why Apple doesn't want us to have buttons on notifications that go away, but keeps
                // that for iTunes and Mail.app
                notification.setValue(true, forKey: "_showsButtons")
                
                notification.actionButtonTitle = "Open"
                
                self.notificationCenter.scheduleNotification(notification)
                
                self.setNormalStatusIcon()
                
                self.refreshFileList()
                
            }, failure: { (error: NSError) -> Void in
                NSLog("Upload failed ! \(error)")
                
                var errorSubtitle = "Error while uploading file"
                
                if let statusCode = error.userInfo?["statusCode"] as? Int
                {
                    errorSubtitle += " (\(statusCode))"
                }
                
                let notification = NSUserNotification()
                notification.title = "Skugga"
                notification.subtitle = errorSubtitle
                notification.deliveryDate = NSDate()
                notification.soundName = "Glass.aiff"
                
                self.notificationCenter.scheduleNotification(notification)
                
                self.setNormalStatusIcon()
            }
        )
{'fraction_non_alphanumeric': 0.0766935766935767, 'fraction_numerical': 0.00351000351000351, 'mean_word_length': 1.8729518527854803, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2460690', 'n_tokens_mistral': 2903, 'n_tokens_neox': 2741, 'n_words': 690}