CatNiP prefinal
Sähköinen nuottikirja, HY-TKTKL-OHTUPROJ KESÄ11
|
00001 00009 #import "MediaWikiQuery.h" 00010 00011 00012 @implementation MediaWikiQuery 00013 @synthesize mediaWikiURLString; 00025 -(NSInteger)expectedSize { 00026 return expectedSize; 00027 } 00033 -(NSInteger)currentBytes { 00034 return currentBytes; 00035 } 00036 00040 -(id)init { 00041 if((self = [super init])) { 00042 expectedSize = NSIntegerMin; // MAAAGIC NUMBER 00043 currentBytes = 0; 00044 progressListeners = [[NSMutableSet alloc] init]; 00045 } 00046 return self; 00047 } 00048 00054 -(id)initWithDelegate:(id <MediaWikiQueryDelegate>)mediaWikiQueryDelegate { 00055 if((self = [self init])) { 00056 // Won't work without a delegate 00057 if(mediaWikiQueryDelegate == nil) { 00058 return nil; 00059 } 00060 queryDelegate = mediaWikiQueryDelegate; 00061 working = NO; 00062 } 00063 return self; 00064 } 00071 -(id)initWithDelegate:(id <MediaWikiQueryDelegate>)mediaWikiQueryDelegate mediaWikiURL:(NSString *)mwURLString { 00072 if(!mwURLString) { 00073 return nil; 00074 } 00075 self = [self initWithDelegate:mediaWikiQueryDelegate]; 00076 if(self != nil) { 00077 self.mediaWikiURLString = [mwURLString copy]; 00078 } 00079 return self; 00080 } 00087 -(NSString*)buildQueryURL { 00088 return self.mediaWikiURLString; 00089 } 00099 -(BOOL)startQuery { 00100 NSString* queryURL = [self buildQueryURL]; 00101 if(queryURL == nil || [queryURL length] < 3) { 00102 return NO; 00103 } 00104 if(working) { 00105 return NO; 00106 } 00107 // We probably need to ensure that we're not in the middle 00108 // of a request! 00109 // queryURL = [queryURL stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]; 00110 queryURL = [queryURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 00111 mediaWikiRequest = [[[NSMutableURLRequest alloc] initWithURL:[[[NSURL alloc] initWithString:queryURL] autorelease]] autorelease]; 00112 //NSLog(@"urlstring: %@ requestobj: %@", queryURL, mediaWikiRequest); 00113 //Is this the right spot for a retain? 00114 if(![NSURLConnection canHandleRequest:mediaWikiRequest]) { 00115 //[mediaWikiRequest release]; 00116 // TODO: Add an error message or something! 00117 return NO; 00118 } 00119 mediaWikiConnection = [[NSURLConnection alloc] initWithRequest:mediaWikiRequest delegate:self]; 00120 if(mediaWikiConnection) { 00121 00122 receivedData = [[NSMutableData data] retain]; 00123 [mediaWikiConnection start]; 00124 //[mediaWikiRequest release]; 00125 working = YES; 00126 } else { 00127 NSDictionary *errordata = 00128 [NSDictionary dictionaryWithObjectsAndKeys: 00129 @"Unknown error while opening HTTP connection", NSLocalizedFailureReasonErrorKey, 00130 [NSString stringWithFormat:@"Could not establish a connection to \"%@\".",queryURL], 00131 NSLocalizedDescriptionKey, nil]; 00132 [[CatNiPErrorManager sharedManager] reportError:[NSError errorWithDomain:NSPOSIXErrorDomain code:EIO userInfo:errordata]]; 00133 NSLog(@"NSURLConnection failed for some reason."); 00134 //[mediaWikiRequest release]; 00135 return NO; 00136 } 00137 return YES; 00138 00139 } 00143 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 00144 // This method is called when the server has determined that it 00145 // has enough information to create the NSURLResponse. 00146 if([response expectedContentLength]) { 00147 expectedSize = [response expectedContentLength]; 00148 } else { 00149 expectedSize = -1; // Maaaagic number 2. 00150 } 00151 // It can be called multiple times, for example in the case of a 00152 // redirect, so each time we reset the data. 00153 [receivedData setLength:0]; 00154 00155 00156 } 00159 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 00160 // Append the new data to receivedData. 00161 // receivedData is an instance variable declared in the interface. 00162 [receivedData appendData:data]; 00163 currentBytes += [data length]; 00164 for (id<MediaWikiProgressListener> listener in progressListeners) { 00165 [listener progressUpdate:self]; 00166 } 00167 00168 00169 } 00172 - (void)connection:(NSURLConnection *)connection 00173 didFailWithError:(NSError *)error { 00174 // release the connection, and the data object 00175 [mediaWikiConnection release]; 00176 [receivedData release]; 00177 working = NO; 00178 [[CatNiPErrorManager sharedManager] reportError:error]; 00179 // inform the user 00180 00181 NSLog(@"Connection failed! Error - %@ %@", 00182 [error localizedDescription], 00183 [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 00184 } 00187 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { 00188 // do something with the data 00189 // receivedData is declared as a method instance elsewhere 00190 NSData* tmp = [NSData dataWithData:receivedData]; 00191 [mediaWikiConnection release]; 00192 [receivedData release]; 00193 working = NO; 00194 [queryDelegate didReceiveMediaWikiData:tmp]; 00195 } 00196 00204 -(void)addProgressListener:(id <MediaWikiProgressListener>)listener { 00205 if([progressListeners containsObject:listener]) { 00206 return; 00207 } 00208 else { 00209 [progressListeners addObject:listener]; 00210 } 00211 } 00212 00217 -(void)removeProgressListener:(id <MediaWikiProgressListener>)listener { 00218 if([progressListeners containsObject:listener]) { 00219 [progressListeners removeObject:listener]; 00220 } 00221 } 00222 00223 -(void)dealloc { 00224 queryDelegate = nil; 00225 [mediaWikiURLString release]; 00226 mediaWikiURLString = nil; 00227 mediaWikiConnection = nil; 00228 receivedData = nil; 00229 [progressListeners removeAllObjects]; 00230 [progressListeners release]; 00231 progressListeners = nil; 00232 [super dealloc]; 00233 } 00234 @end