//
//  PWLicenseCenter.h
//  PWFoundation
//
//  Created by Andreas Kaenner on 23.10.08.
//  Copyright 2006 ProjectWizards GmbH. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <stdlib.h>
#import <time.h>
#import <pthread.h>
#import "NSData-CPExtensions.h"
#import "PWFeatureDefaults.h"
#import "PWLicense.h"
#import "PWLicenseKit-Obfuscating.h"

#define ENCRYPT_BONJOUR_KEYS	0

extern NSString* const LICENSE_INFO_CHANGED_NOTIFICATION;

@class PWLicenseNetworkController;

@protocol PWLicenseCenterDelegate;
																																										
@interface PWLicenseCenter : NSObject
{
	NSArray								*licenseFileExtensions;
	NSArray								*baseProductNames;
	NSArray								*runningNumbersOfBlockedLicenses;
	NSString							*publicKey;
	NSString							*blacklistCheckURLString;
	NSString							*ownerName;
	NSString							*appName;
	double								*blacklist;	
	NSArray								*additionalLicensesSearchPaths;
	BOOL								isBlackListCheckFinished;
	BOOL								isNetworkCheckDisabled;

	NSMutableArray						*specialLicensesArray;
	NSMutableArray						*licenses;
	NSMutableArray						*licensesWithResolvedDependencies;
	NSMutableArray						*licensesWithUnresolvedDependencies;

	NSArray								*typelistForNetworkController;
	NSArray								*features;
	NSObject <PWLicenseCenterDelegate>	*delegate;

	BOOL								hasBlockedLicenses;
	BOOL								wantsMoreLicenses;
	PWLicenseNetworkController			*networkController;
	PWFeatureDefaults					*featureDefaults;

	NSMutableSet						*unusedFeatureNames;

	NSString							*clusterName;
	NSUInteger							maxClusterSize;
}

+ (PWLicenseCenter*) sharedLicenseCenter;										
+ (NSString*) obfuscatedStringFromData:(NSData*)data;							
+ (NSData*) dataFromObfuscatedString:(NSString*)hexString;						
+ (NSArray*) stringsFromBase64CArray:(NSString **)cArray;						

- (void) addFeatures:(NSArray*)features	inQuantity:(int)quantity toProduct:(NSString*)product owner:(NSString*)owner expirationDate:(NSDate*)expirationDate runningNumber:(int)runningNumber associatedWithLicenseFileAtPath:(NSString*)licensePath;	
- (void) removeSpecialLicensesWhichBelongsToPath:(NSString*)path;				
- (void) removeAllSpecialLicenses;												

- (void) checkLicenses;
- (void) checkNetworkLicenses;
- (void) updateBlackList;
- (void) publishUsedSerials;

// Setter

- (void) setLicenseFileExtensions:(NSArray*)extensions;							
- (void) setBaseProductNames:(NSArray*)baseProductNames;						
- (void) setPublicKey:(NSString*)key;											
- (void) setPublicKey:(void*)value length:(int)length;								
- (void) setBlackList:(double*)blacklist;											
- (void) setBlackListCheckURL:(NSURL*)url;											
- (void) setAdditionalLicensesSearchPaths:(NSArray*)paths;							
- (void) setLicenseApplicationName:(NSString*)theAppName;							
- (void) setIsNetworkCheckDisabled:(BOOL)value;										
- (void) setOwnerName:(NSString*)name;												
- (void) setDelegate:(NSObject <PWLicenseCenterDelegate> *)aDelegate;
- (void) setFeatureDefaults:(PWFeatureDefaults*)theFeatureDefaults;
- (void) setFeatureWithName:(NSString*)feature isUsed:(BOOL)isUsed;
- (void) setClusterName:(NSString*)clusterName maxClusterSize:(NSUInteger)clusterSize;

// Getter

- (NSArray*) licenseFileExtensions;																
- (NSArray*) baseProductNames;														
- (NSString*) firstBaseProductName;
- (NSString*) publicKey;																
- (double*) blacklist;																
- (NSURL*) blackListCheckURL;														
- (NSArray*) additionalLicensesSearchPaths;											
- (NSString*) licenseApplicationName;												
- (NSString*) networkIdentifier;																											
- (BOOL) isNetworkCheckDisabled;													
- (BOOL) isBlackListCheckFinished;													
- (NSString*) ownerName;															
- (NSArray*) licenses;	
- (NSDictionary*) obfuscatedUsedLicenseNumbersWithQuantity;
- (NSDictionary*) usedLicenseNumbersWithQuantity;
- (NSArray*) features;
- (NSArray*) licensesSearchPaths;
- (NSObject <PWLicenseCenterDelegate> *)delegate;
- (NSDictionary*) licenseNumbersAndQuantitiesInNetwork;
- (BOOL) hasBlockedLicenses;
- (BOOL) wantsMoreLicenses;
- (PWFeatureDefaults*) featureDefaults;
- (void) dispose;
- (NSString*)clusterName;
- (NSUInteger)maxClusterSize;
				
// featureWithName:forProductWithName:minVersionNumber:
//
// Parameters:
//
//	name			: Feature name			(can be nil to obtain a version feature)
//	product			: Product name
//	minVersionNumber: Product version
//
// Result:
//
//	Returns a feature object which contains the used and total count of the feature.
//  Returns nil if the feature is blocked or no license with this feature is installed.

- (PWFeature*)featureWithName:(NSString*)name forProductWithName:(NSString*)product minVersionNumber:(double)version;

@end

@protocol PWLicenseCenterDelegate

@optional

	// The delegate is called to provide a license if a license cannot be decoded by the license center

- (PWLicense*) licenseForLicenseFileAtPath:(NSString*)path;

	// Give the delegate the oportunity to modify licenses

- (PWLicense*) modifiedLicenseForLicense:(PWLicense*)aLicense;

	// Needed by the server pref pane (TODO: verify)
	
- (NSArray*) additionalNetworkIdentifiers;

	// Give the delegate the chance to do extra work after a license file has been deleted in the license manager

- (void)licenseCenter:(PWLicenseCenter*)licenseCenter didDeleteLicenseAtPath:(NSString*)path;

- (NSDictionary*) licenseNumbersAndQuantitiesInNetwork;

@end
