Commit b8657b24 authored by Pierre's avatar Pierre

Various fixes.

parent 3f7d4746
......@@ -39,7 +39,7 @@ enum {
@interface CXMLDocument : CXMLNode {
NSMutableSet *nodePool;
}
@property (retain,readwrite) NSMutableSet *nodePool;
- (id)initWithData:(NSData *)inData options:(NSUInteger)inOptions error:(NSError **)outError;
- (id)initWithData:(NSData *)inData encoding:(NSStringEncoding)encoding options:(NSUInteger)inOptions error:(NSError **)outError;
- (id)initWithXMLString:(NSString *)inString options:(NSUInteger)inOptions error:(NSError **)outError;
......
......@@ -45,7 +45,7 @@
if ((self = [super init]) != NULL)
{
NSError *theError = NULL;
#if TOUCHXMLUSETIDY
if (inOptions & CXMLDocumentTidyHTML)
{
......@@ -56,7 +56,7 @@ if ((self = [super init]) != NULL)
inString = [[CTidy tidy] tidyString:inString inputFormat:TidyFormat_XML outputFormat:TidyFormat_XML diagnostics:NULL error:&theError];
}
#endif
xmlDocPtr theDoc = xmlParseDoc((xmlChar *)[inString UTF8String]);
if (theDoc != NULL)
{
......@@ -67,14 +67,14 @@ if ((self = [super init]) != NULL)
else
{
xmlErrorPtr theLastErrorPtr = xmlGetLastError();
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithUTF8String:theLastErrorPtr->message], NSLocalizedDescriptionKey,
NULL];
theError = [NSError errorWithDomain:@"CXMLErrorDomain" code:1 userInfo:theUserInfo];
xmlResetLastError();
}
......@@ -92,7 +92,7 @@ return(self);
- (id)initWithData:(NSData *)inData options:(NSUInteger)inOptions error:(NSError **)outError
{
return [self initWithData:inData encoding:NSUTF8StringEncoding options:inOptions error:outError];
return [self initWithData:inData encoding:NSUTF8StringEncoding options:inOptions error:outError];
}
- (id)initWithData:(NSData *)inData encoding:(NSStringEncoding)encoding options:(NSUInteger)inOptions error:(NSError **)outError
......@@ -100,7 +100,7 @@ return(self);
if ((self = [super init]) != NULL)
{
NSError *theError = NULL;
#if TOUCHXMLUSETIDY
if (inOptions & CXMLDocumentTidyHTML)
{
......@@ -111,7 +111,7 @@ if ((self = [super init]) != NULL)
inData = [[CTidy tidy] tidyData:inData inputFormat:TidyFormat_XML outputFormat:TidyFormat_XML diagnostics:NULL error:&theError];
}
#endif
if (theError == NULL)
{
xmlDocPtr theDoc = NULL;
......@@ -122,7 +122,7 @@ if ((self = [super init]) != NULL)
const char *enc = CFStringGetCStringPtr(cfencstr, 0);
theDoc = xmlReadMemory([inData bytes], [inData length], NULL, enc, XML_PARSE_RECOVER | XML_PARSE_NOWARNING);
}
if (theDoc != NULL)
{
_node = (xmlNodePtr)theDoc;
......@@ -165,7 +165,7 @@ else
{
self = NULL;
}
return(self);
}
......@@ -197,7 +197,7 @@ _node = NULL;
- (CXMLElement *)rootElement
{
xmlNodePtr theLibXMLNode = xmlDocGetRootElement((xmlDocPtr)_node);
return([CXMLNode nodeWithLibXMLNode:theLibXMLNode freeOnDealloc:NO]);
}
......@@ -241,9 +241,33 @@ int buffersize;
xmlDocDumpFormatMemory((xmlDocPtr)(self->_node), &xmlbuff, &buffersize, 1);
NSString *dump = [[[NSString alloc] initWithBytes:xmlbuff length:buffersize encoding:NSUTF8StringEncoding] autorelease];
xmlFree(xmlbuff);
[result appendString:dump];
return result;
}
- (void)insertChild:(CXMLNode *)child atIndex:(NSUInteger)index
{
[self.nodePool addObject:child];
CXMLNode *theCurrentNode = [self.children objectAtIndex:index];
xmlAddPrevSibling(theCurrentNode->_node, child->_node);
}
- (void)addChild:(CXMLNode *)child
{
[self.nodePool addObject:child];
xmlAddChild(self->_node, child->_node);
}
- (NSMutableSet *)nodePool
{
if (nodePool == NULL)
{
nodePool = [[NSMutableSet alloc] init];
}
return(nodePool);
}
@end
......@@ -40,14 +40,6 @@ xmlParseDocument(inContext);
}
*/
- (NSMutableSet *)nodePool
{
if (nodePool == NULL)
{
nodePool = [[NSMutableSet alloc] init];
}
return(nodePool);
}
@end
......@@ -80,4 +80,11 @@ typedef enum {
- (NSString *)XMLStringWithOptions:(NSUInteger)options;
//- (NSString *)canonicalXMLStringPreservingComments:(BOOL)comments;
- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error;
@property (readonly, nonatomic, assign) xmlNodePtr node;
- (id)initWithLibXMLNode:(xmlNodePtr)inLibXMLNode freeOnDealloc:(BOOL)infreeOnDealloc;
+ (id)nodeWithLibXMLNode:(xmlNodePtr)inLibXMLNode freeOnDealloc:(BOOL)infreeOnDealloc;
@end
......@@ -92,7 +92,7 @@ else
NSAssert(_node != NULL, @"CXMLNode does not have attached libxml2 _node.");
xmlChar *theXMLString;
BOOL theFreeReminderFlag = NO;
if (_node->type == XML_TEXT_NODE || _node->type == XML_CDATA_SECTION_NODE)
if (_node->type == XML_TEXT_NODE || _node->type == XML_CDATA_SECTION_NODE)
theXMLString = _node->content;
else
{
......@@ -175,7 +175,7 @@ while (theCurrentNode != NULL)
[theChildren addObject:theNode];
theCurrentNode = theCurrentNode->next;
}
return(theChildren);
return(theChildren);
}
- (CXMLNode *)childAtIndex:(NSUInteger)index
......@@ -305,7 +305,7 @@ else
xmlNodePtr theNode = theXPathObject->nodesetval->nodeTab[N];
[theArray addObject:[CXMLNode nodeWithLibXMLNode:theNode freeOnDealloc:NO]];
}
theResult = theArray;
}
......@@ -318,6 +318,89 @@ return(theResult);
//- (NSArray *)objectsForXQuery:(NSString *)xquery error:(NSError **)error;
@dynamic node;
- (id)initWithLibXMLNode:(xmlNodePtr)inLibXMLNode freeOnDealloc:(BOOL)infreeOnDealloc
{
if ((self = [super init]) != NULL)
{
_node = inLibXMLNode;
_freeNodeOnRelease = infreeOnDealloc;
}
return(self);
}
+ (id)nodeWithLibXMLNode:(xmlNodePtr)inLibXMLNode freeOnDealloc:(BOOL)infreeOnDealloc
{
// TODO more checking.
if (inLibXMLNode->_private)
return(inLibXMLNode->_private);
Class theClass = [CXMLNode class];
switch (inLibXMLNode->type)
{
case XML_ELEMENT_NODE:
theClass = [CXMLElement class];
break;
case XML_DOCUMENT_NODE:
theClass = [CXMLDocument class];
break;
case XML_ATTRIBUTE_NODE:
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_COMMENT_NODE:
break;
default:
NSAssert1(NO, @"TODO Unhandled type (%d).", inLibXMLNode->type);
return(NULL);
}
CXMLNode *theNode = [[[theClass alloc] initWithLibXMLNode:inLibXMLNode freeOnDealloc:infreeOnDealloc] autorelease];
if (inLibXMLNode->doc != NULL)
{
CXMLDocument *theXMLDocument = inLibXMLNode->doc->_private;
if (theXMLDocument != NULL)
{
NSAssert([theXMLDocument isKindOfClass:[CXMLDocument class]] == YES, @"TODO");
[[theXMLDocument nodePool] addObject:theNode];
theNode->_node->_private = theNode;
}
}
return(theNode);
}
- (NSString *)stringValueForXPath:(NSString *)string
{
NSArray *nodes = [self nodesForXPath:string error:nil];
if ([nodes count] == 0)
return nil;
return [[nodes objectAtIndex:0] stringValue];
}
- (NSNumber *)numberValueForXPath:(NSString *)string
{
NSArray *nodes = [self nodesForXPath:string error:nil];
if ([nodes count] == 0)
return nil;
NSScanner *scanner = [NSScanner scannerWithString:[[nodes objectAtIndex:0] stringValue]];
NSInteger i;
if ([scanner scanInteger:&i])
return [NSNumber numberWithInteger:i];
return nil;
}
- (xmlNodePtr)node
{
return(_node);
}
@end
static int MyXmlOutputWriteCallback(void * context, const char * buffer, int len)
......
......@@ -31,10 +31,4 @@
@interface CXMLNode (CXMLNode_PrivateExtensions)
@property (readonly, nonatomic, assign) xmlNodePtr node;
- (id)initWithLibXMLNode:(xmlNodePtr)inLibXMLNode freeOnDealloc:(BOOL)infreeOnDealloc;
+ (id)nodeWithLibXMLNode:(xmlNodePtr)inLibXMLNode freeOnDealloc:(BOOL)infreeOnDealloc;
@end
......@@ -32,68 +32,7 @@
#import "CXMLElement.h"
#import "CXMLDocument_PrivateExtensions.h"
@implementation CXMLNode (CXMLNode_PrivateExtensions)
@implementation CXMLNode (dd)
@dynamic node;
- (id)initWithLibXMLNode:(xmlNodePtr)inLibXMLNode freeOnDealloc:(BOOL)infreeOnDealloc
{
if ((self = [super init]) != NULL)
{
_node = inLibXMLNode;
_freeNodeOnRelease = infreeOnDealloc;
}
return(self);
}
+ (id)nodeWithLibXMLNode:(xmlNodePtr)inLibXMLNode freeOnDealloc:(BOOL)infreeOnDealloc
{
// TODO more checking.
if (inLibXMLNode->_private)
return(inLibXMLNode->_private);
Class theClass = [CXMLNode class];
switch (inLibXMLNode->type)
{
case XML_ELEMENT_NODE:
theClass = [CXMLElement class];
break;
case XML_DOCUMENT_NODE:
theClass = [CXMLDocument class];
break;
case XML_ATTRIBUTE_NODE:
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_COMMENT_NODE:
break;
default:
NSAssert1(NO, @"TODO Unhandled type (%d).", inLibXMLNode->type);
return(NULL);
}
CXMLNode *theNode = [[[theClass alloc] initWithLibXMLNode:inLibXMLNode freeOnDealloc:infreeOnDealloc] autorelease];
if (inLibXMLNode->doc != NULL)
{
CXMLDocument *theXMLDocument = inLibXMLNode->doc->_private;
if (theXMLDocument != NULL)
{
NSAssert([theXMLDocument isKindOfClass:[CXMLDocument class]] == YES, @"TODO");
[[theXMLDocument nodePool] addObject:theNode];
theNode->_node->_private = theNode;
}
}
return(theNode);
}
- (xmlNodePtr)node
{
return(_node);
}
@end
......@@ -33,21 +33,3 @@
#import "CXMLNode_PrivateExtensions.h"
#import "CXMLDocument_PrivateExtensions.h"
@implementation CXMLDocument (CXMLDocument_CreationExtensions)
- (void)insertChild:(CXMLNode *)child atIndex:(NSUInteger)index
{
[self.nodePool addObject:child];
CXMLNode *theCurrentNode = [self.children objectAtIndex:index];
xmlAddPrevSibling(theCurrentNode->_node, child->_node);
}
- (void)addChild:(CXMLNode *)child
{
[self.nodePool addObject:child];
xmlAddChild(self->_node, child->_node);
}
@end
......@@ -5,8 +5,8 @@
// Created by Pierre d'Herbemont on 7/14/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <CoreData/CoreData.h>
#import <CoreData/CoreData.h>
@interface MLMediaLibrary : NSObject {
NSManagedObjectContext *_managedObjectContext;
......
This diff is collapsed.
......@@ -15,10 +15,15 @@
id<MLMovieInfoGrabberDelegate> _delegate;
#if HAVE_BLOCK
void (^_block)(NSError *);
#else
id _userData;
#endif
}
@property (readwrite, assign) id<MLMovieInfoGrabberDelegate> delegate;
#if !HAVE_BLOCK
@property (readwrite, retain) id userData;
#endif
@property (readonly, retain) NSArray *results;
- (void)lookUpForTitle:(NSString *)title;
......
......@@ -28,8 +28,14 @@
@implementation MLMovieInfoGrabber
@synthesize delegate=_delegate;
@synthesize results=_results;
#if !HAVE_BLOCK
@synthesize userData=_userData;
#endif
- (void)dealloc
{
#if !HAVE_BLOCK
[_userData release];
#endif
[_data release];
[_connection release];
[_results release];
......@@ -44,7 +50,6 @@
{
NSString *escapedString = [title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:TMDB_QUERY_SEARCH, TMDB_HOSTNAME, escapedString, TMDB_API_KEY]];
NSLog(@"Accessing %@", url);
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15];
[_connection cancel];
[_connection release];
......@@ -108,12 +113,15 @@
continue;
NSString *title = [node stringValueForXPath:@"./title"];
NSString *release = [node stringValueForXPath:@"./release"];
NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
[inputFormatter setDateFormat:@"yyyy-MM-dd"];
NSDateFormatter *outputFormatter = [[[NSDateFormatter alloc] init] autorelease];
[outputFormatter setDateFormat:@"yyyy"];
NSDate *releaseDate = [inputFormatter dateFromString:release];
NSString *releaseYear = releaseDate ? [outputFormatter stringFromDate:releaseDate] : nil;
NSString *releaseYear = nil;
if (release) {
NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
[inputFormatter setDateFormat:@"yyyy-MM-dd"];
NSDateFormatter *outputFormatter = [[[NSDateFormatter alloc] init] autorelease];
[outputFormatter setDateFormat:@"yyyy"];
NSDate *releaseDate = [inputFormatter dateFromString:release];
releaseYear = releaseDate ? [outputFormatter stringFromDate:releaseDate] : nil;
}
//NSLog(@"%@", title);
......
......@@ -16,11 +16,17 @@
NSArray *_episodesResults;
id<MLTVShowEpisodesInfoGrabberDelegate> _delegate;
void (^_block)();
#if !HAVE_BLOCK
id _userData;
#endif
}
@property (readwrite, assign) id<MLTVShowEpisodesInfoGrabberDelegate> delegate;
@property (readonly, retain) NSArray *episodesResults;
@property (readonly, retain) NSDictionary *results;
#if !HAVE_BLOCK
@property (readwrite, retain) id userData;
#endif
- (void)lookUpForShowID:(NSString *)id;
......
......@@ -9,6 +9,7 @@
#import "MLTVShowEpisodesInfoGrabber.h"
#import "TheTVDBGrabber.h"
#import "MLURLConnection.h"
@interface MLTVShowEpisodesInfoGrabber ()
@property (readwrite, retain) NSDictionary *results;
......@@ -21,8 +22,12 @@
@synthesize delegate=_delegate;
@synthesize episodesResults=_episodesResults;
@synthesize results=_results;
#if !HAVE_BLOCK
@synthesize userData=_userData;
#endif
- (void)dealloc
{
[_userData release];
[_connection release];
[_results release];
[_episodesResults release];
......@@ -33,6 +38,20 @@
[super dealloc];
}
#if !HAVE_BLOCK
- (void)urlConnection:(MLURLConnection *)connection didFinishWithError:(NSError *)error
{
if (error) {
[_connection release];
_connection = nil;
[self autorelease];
return;
}
[self didReceiveData:connection.data];
[self autorelease];
}
#endif
- (void)lookUpForShowID:(NSString *)showId
{
[_connection cancel];
......@@ -55,7 +74,7 @@
[self autorelease];
}] retain];
#else
NSLog(@"%s: Unimplemented", __PRETTY_FUNCTION__);
_connection = [[MLURLConnection runConnectionWithURL:url delegate:self userObject:nil] retain];
#endif
}
......
......@@ -14,10 +14,16 @@
NSArray *_results;
void (^_block)();
id<MLTVShowInfoGrabberDelegate> _delegate;
#if !HAVE_BLOCK
id _userData;
#endif
}
@property (readwrite, assign) id<MLTVShowInfoGrabberDelegate> delegate;
@property (readonly, retain) NSArray *results;
#if !HAVE_BLOCK
@property (readwrite, retain) id userData;
#endif
- (void)lookUpForTitle:(NSString *)title;
......@@ -25,13 +31,22 @@
- (void)lookUpForTitle:(NSString *)title andExecuteBlock:(void (^)())block;
#endif
#if HAVE_BLOCK
+ (void)fetchServerTimeAndExecuteBlock:(void (^)(NSNumber *))block;
+ (void)fetchUpdatesSinceServerTime:(NSNumber *)serverTime andExecuteBlock:(void (^)(NSArray *))block;
#else
- (void)fetchUpdatesSinceServerTime:(NSNumber *)serverTime;
- (void)fetchServerTime;
+ (NSNumber *)serverTime;
#endif
@end
@protocol MLTVShowInfoGrabberDelegate <NSObject>
@optional
- (void)tvShowInfoGrabber:(MLTVShowInfoGrabber *)grabber didFailWithError:(NSError *)error;
@required
- (void)tvShowInfoGrabberDidFinishGrabbing:(MLTVShowInfoGrabber *)grabber;
- (void)tvShowInfoGrabberDidFetchServerTime:(MLTVShowInfoGrabber *)grabber;
- (void)tvShowInfoGrabber:(MLTVShowInfoGrabber *)grabber didFetchUpdates:(NSArray *)updates;
@end
......@@ -8,24 +8,88 @@
#import "MLTVShowInfoGrabber.h"
#import "TheTVDBGrabber.h"
#import "MLURLConnection.h"
@interface MLTVShowInfoGrabber ()
#if !HAVE_BLOCK
<MLURLConnectionDelegate>
#endif
@property (readwrite, retain) NSArray *results;
@end
@implementation MLTVShowInfoGrabber
@synthesize delegate=_delegate;
@synthesize results=_results;
#if !HAVE_BLOCK
@synthesize userData=_userData;
#endif
static NSDate *gLastFetch = nil;
static NSNumber *gServerTime = nil;
- (void)dealloc
{
[_data release];
[_connection release];
[_results release];
[super dealloc];
}
#if !HAVE_BLOCK
- (void)urlConnection:(MLURLConnection *)connection didFinishWithError:(NSError *)error
{
if ([connection.userObject isEqualToString:@"fetchUpdatesSinceServerTime"]) {
// We are coming form -fetchUpdatesSinceServerTime
if (error)
return;
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithData:connection.data options:0 error:nil];
NSNumber *serverTime = [[xmlDoc rootElement] numberValueForXPath:@"./Time"];
[gServerTime release];
[gLastFetch release];
gServerTime = [serverTime retain];
gLastFetch = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
NSArray *nodes = [xmlDoc nodesForXPath:@"./Items/Series" error:&error];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[nodes count]];
for (NSXMLNode *node in nodes) {
NSNumber *id = [node numberValueForXPath:@"."];
[array addObject:id];
}
[_delegate tvShowInfoGrabber:self didFetchUpdates:array];
} else {
NSAssert([connection.userObject isEqualToString:@"fetchServerTime"], @"Unkown callback emitter");
if (error) {
[_delegate tvShowInfoGrabberDidFetchServerTime:self];
return;
}
[gServerTime release];
[gLastFetch release];
NSData *data = connection.data;
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithData:connection.data options:0 error:nil];
NSNumber *serverTime = [[xmlDoc rootElement] numberValueForXPath:@"./Time"];
gServerTime = [serverTime retain];
gLastFetch = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
[_delegate tvShowInfoGrabberDidFetchServerTime:self];
}
}
- (void)fetchUpdatesSinceServerTime:(NSNumber *)serverTime
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:TVDB_QUERY_UPDATES, TVDB_HOSTNAME, serverTime]];
[MLURLConnection runConnectionWithURL:url delegate:self userObject:@"fetchUpdatesSinceServerTime"];
}
#else
+ (void)fetchUpdatesSinceServerTime:(NSNumber *)serverTime andExecuteBlock:(void (^)(NSArray *))block
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:TVDB_QUERY_UPDATES, TVDB_HOSTNAME, serverTime]];
NSLog(@"Accessing %@", url);
#if HAVE_BLOCK
[MLURLConnection runConnectionWithURL:url andBlock:^(MLURLConnection *connection, NSError *error) {
if (error) {
return;
......@@ -46,9 +110,32 @@ static NSNumber *gServerTime = nil;
}
block(array);
}];
}
#endif
#if !HAVE_BLOCK
+ (NSNumber *)serverTime
{
return gServerTime;
}
- (void)fetchServerTime
{
if (gLastFetch && gServerTime) {
// Only fetch the serverTime every hour
// FIXME: Have a default for that?
NSDate *oneHourAgo = [NSDate dateWithTimeIntervalSinceNow:5 * 60 /* Every 5 mins */];
if ([oneHourAgo earlierDate:gLastFetch] == gLastFetch) {
[_delegate tvShowInfoGrabberDidFetchServerTime:self];
return;
}
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:TVDB_QUERY_SERVER_TIME, TVDB_HOSTNAME]];
[MLURLConnection runConnectionWithURL:url delegate:self userObject:@"fetchServerTime"];
}
#else
+ (void)fetchServerTimeAndExecuteBlock:(void (^)(NSNumber *))block
{
......@@ -57,14 +144,13 @@ static NSNumber *gServerTime = nil;
// FIXME: Have a default for that?
NSDate *oneHourAgo = [NSDate dateWithTimeIntervalSinceNow:5 * 60 /* Every 5 mins */];
if ([oneHourAgo earlierDate:gLastFetch] == gLastFetch) {
block(gServerTime);
[_delegate tvShowInfoGrabberDidFetchServerTime:self];
return;
}
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:TVDB_QUERY_SERVER_TIME, TVDB_HOSTNAME]];
#if HAVE_BLOCK
[MLURLConnection runConnectionWithURL:url andBlock:^(MLURLConnection *connection, NSError *error) {
if (error) {
block(nil);
......@@ -79,24 +165,16 @@ static NSNumber *gServerTime = nil;
gServerTime = [serverTime retain];
gLastFetch = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
block(gServerTime);
[_delegate tvShowInfoGrabberDidFetchServerTime:self];
}];
#endif
}
#endif
- (void)dealloc
{
[_data release];
[_connection release];
[_results release];
[super dealloc];
}
- (void)lookUpForTitle:(NSString *)title
{
NSString *escapedString = [title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];