<commit_before>//
//  OneRoster.swift
//  OneChat
//
//  Created by Paul on 26/02/2015.
//  Copyright (c) 2015 ProcessOne. All rights reserved.
//

import Foundation
import XMPPFramework

public protocol OneRosterDelegate {
	func oneRosterContentChanged(controller: NSFetchedResultsController)
}

public class OneRoster: NSObject, NSFetchedResultsControllerDelegate {
	public var delegate: OneRosterDelegate?
	public var fetchedResultsControllerVar: NSFetchedResultsController?
	
	// MARK: Singleton
	
	public class var sharedInstance : OneRoster {
		struct OneRosterSingleton {
			static let instance = OneRoster()
		}
		return OneRosterSingleton.instance
	}
	
	public class var buddyList: NSFetchedResultsController {
		get {
			if sharedInstance.fetchedResultsControllerVar != nil {
				return sharedInstance.fetchedResultsControllerVar!
			}
			return sharedInstance.fetchedResultsController()!
		}
	}
	
	// MARK: Core Data
	
	func managedObjectContext_roster() -> NSManagedObjectContext {
		return OneChat.sharedInstance.xmppRosterStorage.mainThreadManagedObjectContext
	}
	
	private func managedObjectContext_capabilities() -> NSManagedObjectContext {
		return OneChat.sharedInstance.xmppRosterStorage.mainThreadManagedObjectContext
	}
	
	public func fetchedResultsController() -> NSFetchedResultsController? {
		if fetchedResultsControllerVar == nil {
			let moc = OneRoster.sharedInstance.managedObjectContext_roster() as NSManagedObjectContext?
			let entity = NSEntityDescription.entityForName("XMPPUserCoreDataStorageObject", inManagedObjectContext: moc!)
			let sd1 = NSSortDescriptor(key: "sectionNum", ascending: true)
			let sd2 = NSSortDescriptor(key: "displayName", ascending: true)
			
			let sortDescriptors = [sd1, sd2]
			let fetchRequest = NSFetchRequest()
			
			fetchRequest.entity = entity
			fetchRequest.sortDescriptors = sortDescriptors
			fetchRequest.fetchBatchSize = 10
			
			fetchedResultsControllerVar = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc!, sectionNameKeyPath: "sectionNum", cacheName: nil)
			fetchedResultsControllerVar?.delegate = self
			
			do {
				try fetchedResultsControllerVar!.performFetch()
			} catch let error as NSError {
				print("Error: \(error.localizedDescription)")
				abort()
			}
			//  if fetchedResultsControllerVar?.performFetch() == nil {
			//Handle fetch error
			//}
		}
		
		return fetchedResultsControllerVar!
	}
	
	public class func userFromRosterAtIndexPath(indexPath indexPath: NSIndexPath) -> XMPPUserCoreDataStorageObject {
		return sharedInstance.fetchedResultsController()!.objectAtIndexPath(indexPath) as! XMPPUserCoreDataStorageObject
	}
	
	public class func userFromRosterForJID(jid jid: String) -> XMPPUserCoreDataStorageObject? {
		let userJID = XMPPJID.jidWithString(jid)
		
		if let user = OneChat.sharedInstance.xmppRosterStorage.userForJID(userJID, xmppStream: OneChat.sharedInstance.xmppStream, managedObjectContext: sharedInstance.managedObjectContext_roster()) {
			return user
		} else {
			return nil
		}
	}
	
	public class func removeUserFromRosterAtIndexPath(indexPath indexPath: NSIndexPath) {
		let user = userFromRosterAtIndexPath(indexPath: indexPath)
		sharedInstance.fetchedResultsControllerVar?.managedObjectContext.deleteObject(user)
	}
	
	public func controllerDidChangeContent(controller: NSFetchedResultsController) {
		delegate?.oneRosterContentChanged(controller)
	}
}

extension OneRoster: XMPPRosterDelegate {
	
	public func xmppRoster(sender: XMPPRoster, didReceiveBuddyRequest presence:XMPPPresence) {
		//was let user
		_ = OneChat.sharedInstance.xmppRosterStorage.userForJID(presence.from(), xmppStream: OneChat.sharedInstance.xmppStream, managedObjectContext: managedObjectContext_roster())
	}
	
	public func xmppRosterDidEndPopulating(sender: XMPPRoster?) {
		let jidList = OneChat.sharedInstance.xmppRosterStorage.jidsForXMPPStream(OneChat.sharedInstance.xmppStream)
		print("List=\(jidList)")
		
	}
	
	public func sendBuddyRequestTo(username: String) {
		let presence: DDXMLElement = DDXMLElement.elementWithName("presence") as! DDXMLElement
		presence.addAttributeWithName("type", stringValue: "subscribe")
        	presence.addAttributeWithName("to", stringValue: username)
        	presence.addAttributeWithName("from", stringValue: OneChat.sharedInstance.xmppStream?.myJID.bare())
        
		OneChat.sharedInstance.xmppStream?.sendElement(presence)
    	}
}

extension OneRoster: XMPPStreamDelegate {
	
	public func xmppStream(sender: XMPPStream, didReceiveIQ ip: XMPPIQ) -> Bool {
		if let msg = ip.attributeForName("from") {
			if msg.stringValue() == "conference.process-one.net"  {
				
			}
		}
		return false
	}
}<commit_msg>Adding Accept & Decline Buddy Request<commit_after>//
//  OneRoster.swift
//  OneChat
//
//  Created by Paul on 26/02/2015.
//  Copyright (c) 2015 ProcessOne. All rights reserved.
//

import Foundation
import XMPPFramework

public protocol OneRosterDelegate {
	func oneRosterContentChanged(controller: NSFetchedResultsController)
}

public class OneRoster: NSObject, NSFetchedResultsControllerDelegate {
	public var delegate: OneRosterDelegate?
	public var fetchedResultsControllerVar: NSFetchedResultsController?
	
	// MARK: Singletonsen
	
	public class var sharedInstance : OneRoster {
		struct OneRosterSingleton {
			static let instance = OneRoster()
		}
		return OneRosterSingleton.instance
	}
	
	public class var buddyList: NSFetchedResultsController {
		get {
			if sharedInstance.fetchedResultsControllerVar != nil {
				return sharedInstance.fetchedResultsControllerVar!
			}
			return sharedInstance.fetchedResultsController()!
		}
	}
	
	// MARK: Core Data
	
	func managedObjectContext_roster() -> NSManagedObjectContext {
		return OneChat.sharedInstance.xmppRosterStorage.mainThreadManagedObjectContext
	}
	
	private func managedObjectContext_capabilities() -> NSManagedObjectContext {
		return OneChat.sharedInstance.xmppRosterStorage.mainThreadManagedObjectContext
	}
	
	public func fetchedResultsController() -> NSFetchedResultsController? {
		if fetchedResultsControllerVar == nil {
			let moc = OneRoster.sharedInstance.managedObjectContext_roster() as NSManagedObjectContext?
			let entity = NSEntityDescription.entityForName("XMPPUserCoreDataStorageObject", inManagedObjectContext: moc!)
			let sd1 = NSSortDescriptor(key: "sectionNum", ascending: true)
			let sd2 = NSSortDescriptor(key: "displayName", ascending: true)
			
			let sortDescriptors = [sd1, sd2]
			let fetchRequest = NSFetchRequest()
			
			fetchRequest.entity = entity
			fetchRequest.sortDescriptors = sortDescriptors
			fetchRequest.fetchBatchSize = 10
			
			fetchedResultsControllerVar = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc!, sectionNameKeyPath: "sectionNum", cacheName: nil)
			fetchedResultsControllerVar?.delegate = self
			
			do {
				try fetchedResultsControllerVar!.performFetch()
			} catch let error as NSError {
				print("Error: \(error.localizedDescription)")
				abort()
			}
			//  if fetchedResultsControllerVar?.performFetch() == nil {
			//Handle fetch error
			//}
		}
		
		return fetchedResultsControllerVar!
	}
	
	public class func userFromRosterAtIndexPath(indexPath indexPath: NSIndexPath) -> XMPPUserCoreDataStorageObject {
		return sharedInstance.fetchedResultsController()!.objectAtIndexPath(indexPath) as! XMPPUserCoreDataStorageObject
	}
	
	public class func userFromRosterForJID(jid jid: String) -> XMPPUserCoreDataStorageObject? {
		let userJID = XMPPJID.jidWithString(jid)
		
		if let user = OneChat.sharedInstance.xmppRosterStorage.userForJID(userJID, xmppStream: OneChat.sharedInstance.xmppStream, managedObjectContext: sharedInstance.managedObjectContext_roster()) {
			return user
		} else {
			return nil
		}
	}
	
	public class func removeUserFromRosterAtIndexPath(indexPath indexPath: NSIndexPath) {
		let user = userFromRosterAtIndexPath(indexPath: indexPath)
		sharedInstance.fetchedResultsControllerVar?.managedObjectContext.deleteObject(user)
	}
	
	public func controllerDidChangeContent(controller: NSFetchedResultsController) {
		delegate?.oneRosterContentChanged(controller)
	}
}

extension OneRoster: XMPPRosterDelegate {
	
	public func xmppRoster(sender: XMPPRoster, didReceiveBuddyRequest presence:XMPPPresence) {
		//was let user
		_ = OneChat.sharedInstance.xmppRosterStorage.userForJID(presence.from(), xmppStream: OneChat.sharedInstance.xmppStream, managedObjectContext: managedObjectContext_roster())
	}
	
	public func xmppRosterDidEndPopulating(sender: XMPPRoster?) {
		let jidList = OneChat.sharedInstance.xmppRosterStorage.jidsForXMPPStream(OneChat.sharedInstance.xmppStream)
		print("List=\(jidList)")
		
	}
	
	public func sendBuddyRequestTo(username: String) {
		let presence: DDXMLElement = DDXMLElement.elementWithName("presence") as! DDXMLElement
		presence.addAttributeWithName("type", stringValue: "subscribe")
        	presence.addAttributeWithName("to", stringValue: username)
        	presence.addAttributeWithName("from", stringValue: OneChat.sharedInstance.xmppStream?.myJID.bare())
        
		OneChat.sharedInstance.xmppStream?.sendElement(presence)
    	}
    	
    	public func acceptBuddyRequestFrom(username: String) {
        	let presence: DDXMLElement = DDXMLElement.elementWithName("presence") as! DDXMLElement
        	presence.addAttributeWithName("to", stringValue: username)
        	presence.addAttributeWithName("from", stringValue: OneChat.sharedInstance.xmppStream?.myJID.bare())
        	presence.addAttributeWithName("type", stringValue: "subscribed")
        	
        	OneChat.sharedInstance.xmppStream?.sendElement(presence)
    	}
    
    	public func declineBuddyRequestFrom(username: String) {
        	let presence: DDXMLElement = DDXMLElement.elementWithName("presence") as! DDXMLElement
        	presence.addAttributeWithName("to", stringValue: username)
        	presence.addAttributeWithName("from", stringValue: OneChat.sharedInstance.xmppStream?.myJID.bare())
        	presence.addAttributeWithName("type", stringValue: "unsubscribed")

        	OneChat.sharedInstance.xmppStream?.sendElement(presence)
    	}
}

extension OneRoster: XMPPStreamDelegate {
	
	public func xmppStream(sender: XMPPStream, didReceiveIQ ip: XMPPIQ) -> Bool {
		if let msg = ip.attributeForName("from") {
			if msg.stringValue() == "conference.process-one.net"  {
				
			}
		}
		return false
	}
}
{'fraction_non_alphanumeric': 0.08528846153846154, 'fraction_numerical': 0.0034615384615384616, 'mean_word_length': 5.154437869822485, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6857669', 'n_tokens_mistral': 3459, 'n_tokens_neox': 3066, 'n_words': 578}