<commit_before>/**
 * Appcelerator Titanium - licensed under the Apache Public License 2
 * see LICENSE in the root folder for details on the license.
 * Copyright (c) 2008 Appcelerator, Inc. All Rights Reserved.
 */

#include "growl_osx.h"
#import "GrowlApplicationBridge.h"
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#include <sys/types.h>
#include <sys/stat.h>

using namespace kroll;
using namespace ti;

namespace ti {
	GrowlOSX::GrowlOSX(SharedBoundObject global) : GrowlBinding(global) {
		delegate = [[TiGrowlDelegate alloc] init];
		//Delegate will have a retain count of one from the alloc init.
	}

	GrowlOSX::~GrowlOSX() {
		[delegate release];
	}


	void * GrowlOSX::URLWithAppCString(const char * inputCString)
	{
		if (inputCString == NULL) return nil;
		NSString * inputString = [NSString stringWithUTF8String:inputCString];
		NSURL * result = [NSURL URLWithString:inputString];
		NSString * scheme = [result scheme];
		
		BOOL isAppPath = [scheme isEqualToString:@"app"] || [scheme isEqualToString:@"ti"];
		if ((scheme != nil) && !isAppPath) return result;
		
		NSString * resourceSpecifier = [result resourceSpecifier];
		if (isAppPath || [resourceSpecifier hasPrefix:@"//"]){
			resourceSpecifier = [resourceSpecifier substringFromIndex:2];
		}
		
		SharedValue iconPathValue = global->CallNS("App.appURLToPath", Value::NewString([resourceSpecifier UTF8String]));
		if (!(iconPathValue->IsString())) return nil;
		
		std::string iconPath = iconPathValue->ToString();
		const char * iconPathCString = iconPath.c_str();
		
		if (iconPathCString == NULL) return nil;
		return [NSURL fileURLWithPath:[NSString stringWithUTF8String:iconPathCString]];
	}
	

	void GrowlOSX::ShowNotification(std::string& title, std::string& description, std::string& iconURL, int notification_delay, SharedBoundMethod callback)
	{
		NSData *iconData = nil;

		if (iconURL.size() > 0) {
			const char * iconURLCString = iconURL.c_str();
			//TODO: This should be more generalized and using centralized methods, but for now,

			NSURL * iconNSURL = (NSURL *)URLWithAppCString(iconURLCString);

			if ([iconNSURL isFileURL]){
				iconData = [NSData dataWithContentsOfURL:iconNSURL];
			} else if (iconNSURL != nil){
				iconData = [NSData dataWithContentsOfURL:iconNSURL];
				//TODO: Delayed load and fire.
			}
		}

		NSMutableDictionary* clickContext = [[NSMutableDictionary alloc] initWithCapacity:10];

		if (!callback.isNull()) {
			[clickContext setObject:[[MethodWrapper alloc] initWithMethod:new SharedBoundMethod(callback)] forKey:@"method_wrapper"];
		}

		const char * titleCString = title.c_str();
		NSString * titleString = nil;
		if (titleCString != NULL) {
			titleString = [NSString stringWithUTF8String:titleCString];
		}
		
		const char * descriptionCString = description.c_str();
		NSString * descriptionString = nil;
		if (descriptionCString != NULL) {
			descriptionString = [NSString stringWithUTF8String:descriptionCString];
		}
		

		[GrowlApplicationBridge
			 notifyWithTitle:titleString
			 description:descriptionString
			 notificationName:@"tiNotification"
			 iconData:iconData
			 priority:0
			 isSticky:NO
			 clickContext:clickContext];
	}

	bool GrowlOSX::IsRunning()
	{
		return [GrowlApplicationBridge isGrowlRunning];
	}

	void GrowlOSX::CopyToApp(kroll::Host *host, kroll::Module *module)
	{
		std::string dir = host->GetApplicationHome() + KR_PATH_SEP + "Contents" +
			KR_PATH_SEP + "Frameworks" + KR_PATH_SEP + "Growl.framework";

		if (!FileUtils::IsDirectory(dir))<commit_msg>Disable callbacks for Growl notifications<commit_after>/**
 * Appcelerator Titanium - licensed under the Apache Public License 2
 * see LICENSE in the root folder for details on the license.
 * Copyright (c) 2008 Appcelerator, Inc. All Rights Reserved.
 */

#include "growl_osx.h"
#import "GrowlApplicationBridge.h"
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#include <sys/types.h>
#include <sys/stat.h>

using namespace kroll;
using namespace ti;

namespace ti {
	GrowlOSX::GrowlOSX(SharedBoundObject global) : GrowlBinding(global) {
		delegate = [[TiGrowlDelegate alloc] init];
		//Delegate will have a retain count of one from the alloc init.
	}

	GrowlOSX::~GrowlOSX() {
		[delegate release];
	}


	void * GrowlOSX::URLWithAppCString(const char * inputCString)
	{
		if (inputCString == NULL) return nil;
		NSString * inputString = [NSString stringWithUTF8String:inputCString];
		NSURL * result = [NSURL URLWithString:inputString];
		NSString * scheme = [result scheme];
		
		BOOL isAppPath = [scheme isEqualToString:@"app"] || [scheme isEqualToString:@"ti"];
		if ((scheme != nil) && !isAppPath) return result;
		
		NSString * resourceSpecifier = [result resourceSpecifier];
		if (isAppPath || [resourceSpecifier hasPrefix:@"//"]){
			resourceSpecifier = [resourceSpecifier substringFromIndex:2];
		}
		
		SharedValue iconPathValue = global->CallNS("App.appURLToPath", Value::NewString([resourceSpecifier UTF8String]));
		if (!(iconPathValue->IsString())) return nil;
		
		std::string iconPath = iconPathValue->ToString();
		const char * iconPathCString = iconPath.c_str();
		
		if (iconPathCString == NULL) return nil;
		return [NSURL fileURLWithPath:[NSString stringWithUTF8String:iconPathCString]];
	}
	

	void GrowlOSX::ShowNotification(std::string& title, std::string& description, std::string& iconURL, int notification_delay, SharedBoundMethod callback)
	{
		NSData *iconData = nil;

		if (iconURL.size() > 0) {
			const char * iconURLCString = iconURL.c_str();
			//TODO: This should be more generalized and using centralized methods, but for now,

			NSURL * iconNSURL = (NSURL *)URLWithAppCString(iconURLCString);

			if ([iconNSURL isFileURL]){
				iconData = [NSData dataWithContentsOfURL:iconNSURL];
			} else if (iconNSURL != nil){
				iconData = [NSData dataWithContentsOfURL:iconNSURL];
				//TODO: Delayed load and fire.
			}
		}

		NSMutableDictionary* clickContext = [[NSMutableDictionary alloc] initWithCapacity:10];

		if (!callback.isNull()) {
			[clickContext setObject:[[MethodWrapper alloc] initWithMethod:new SharedBoundMethod(callback)] forKey:@"method_wrapper"];
		}

		const char * titleCString = title.c_str();
		NSString * titleString = nil;
		if (titleCString != NULL) {
			titleString = [NSString stringWithUTF8String:titleCString];
		}
		
		const char * descriptionCString = description.c_str();
		NSString * descriptionString = nil;
		if (descriptionCString != NULL) {
			descriptionString = [NSString stringWithUTF8String:descriptionCString];
		}
		

		[GrowlApplicationBridge
			 notifyWithTitle:titleString
			 description:descriptionString
			 notificationName:@"tiNotification"
			 iconData:iconData
			 priority:0
			 isSticky:NO
			 clickContext:nil];
	}

	bool GrowlOSX::IsRunning()
	{
		return [GrowlApplicationBridge isGrowlRunning];
	}

	void GrowlOSX::CopyToApp(kroll::Host *host, kroll::Module *module)
	{
		std::string dir = host->GetApplicationHome() + KR_PATH_SEP + "Contents" +
			KR_PATH_SEP + "Frameworks" + KR_PATH_SEP + "Growl.framework";

		if (!FileUtils::IsDirectory(dir))
{'fraction_non_alphanumeric': 0.1177799745798616, 'fraction_numerical': 0.0042366897330885465, 'mean_word_length': 5.016992353440951, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '4919190', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2171, 'n_words': 531}