CatNiP prefinal
Sähköinen nuottikirja, HY-TKTKL-OHTUPROJ KESÄ11
/Users/awniemel/Notepad-SVN/svn/trunk/CatNiP/CatNiP/LocalDataHandler.m
Go to the documentation of this file.
00001 
00012 #import "LocalDataHandler.h"
00013 
00014 @implementation LocalDataHandler
00015 
00016 
00017 -(id)init
00018 {
00019     self = [super init];
00020     return self;
00021 }
00022 
00027 +(void)deleteFile:(NSString *)filepath 
00028 
00029 {
00030     NSFileManager *fileMgr = [NSFileManager defaultManager];
00031     NSError *error;
00032     
00033     if ([fileMgr removeItemAtPath:filepath error:&error] != YES) 
00034     {
00035         NSLog(@"Unable to delete file at filepath: %@, error:%@", filepath, error);
00036     }
00037     else NSLog(@"Deleted file at filepath: %@", filepath);
00038 }
00039 
00044 +(void)deleteFiles:(NSArray *)filepaths 
00045 {
00046     NSFileManager *fileMgr = [NSFileManager defaultManager];
00047     NSError *error;
00048     
00049     for (NSString *file in filepaths) {
00050         if ([fileMgr removeItemAtPath:file error:&error] != YES) 
00051         {
00052             NSLog(@"Unable to delete file at filepath: %@, error:%@", file, error);
00053         }
00054         else NSLog(@"Deleted file at filepath: %@", file);
00055     }
00056 }
00057 
00064 +(void)saveScoresToDefaultFile:(NSMutableArray *)scores 
00065 {
00066     NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
00067         NSString *documentDirectory = [documentDirectories objectAtIndex:0];
00068         NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"catnip_local.data"];
00069     
00070     NSLog(@"Saving %d scores", [scores count]);
00071         [NSKeyedArchiver archiveRootObject:scores toFile:filePath];
00072 }
00073 
00074 //+(void)saveDictionaryToDefaultFile:(NSMutableDictionary *)dict
00075 //{
00076 //    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
00077 //      NSString *documentDirectory = [documentDirectories objectAtIndex:0];
00078 //      NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"catnip_dictionary.data"];
00079 //    
00080 //    NSLog(@"Saving %d composers and %d compositions", [[dict allKeys] count], [[dict allValues] count]);
00081 //      [NSKeyedArchiver archiveRootObject:dict toFile:filePath];
00082 //}
00083 
00089 +(void)savePlaylistsToDefaultFile:(NSMutableArray *)playlists
00090 {
00091     NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
00092         NSString *documentDirectory = [documentDirectories objectAtIndex:0];
00093         NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"catnip_playlists.data"];
00094     NSMutableArray *playlistsToSave = [[NSMutableArray alloc] init];
00095     
00096     for (Playlist *p in playlists) 
00097     {
00098         Playlist *s = [[Playlist alloc] initWithName:p.name andArray:nil];
00099         NSMutableArray *a = [[NSMutableArray alloc] init];
00100         for (LocalScoreData *l in p.scorelist) 
00101         {
00102             [a addObject:[NSNumber numberWithInt:l.index]];
00103         }
00104         s.scorelist = a;
00105         [playlistsToSave addObject:s];
00106         [a release];
00107         [s release]; 
00108     }
00109     
00110     NSLog(@"Saving %d playlists from original %d", [playlistsToSave count], [playlists count]);
00111         [NSKeyedArchiver archiveRootObject:playlistsToSave toFile:filePath];
00112     [playlistsToSave release];
00113 }
00114 
00121 +(NSMutableArray *)loadLocalScoresFromDefaultFile 
00122 {
00123     NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
00124         NSString *documentDirectory = [documentDirectories objectAtIndex:0];
00125         NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"catnip_local.data"];
00126     
00127         NSMutableArray *scores = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
00128         if (!scores)
00129                 scores = [[NSMutableArray alloc] init ];        
00130     else NSLog(@"Read %d scores from memory", [scores count]);
00131 
00132     return scores;
00133 }
00134 
00135 //+(NSMutableDictionary *)loadDictionaryFromDefaultFile 
00136 //{
00137 //    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
00138 //      NSString *documentDirectory = [documentDirectories objectAtIndex:0];
00139 //      NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"catnip_dictionary.data"];
00140 //    
00141 //      NSMutableDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
00142 //      if (!dict)
00143 //              dict = [[NSMutableArray alloc] init ];  
00144 //    else NSLog(@"Read %d composers and %d compositions from memory", [[dict allKeys] count], [[dict allValues] count]);
00145 //    
00146 //    return dict;
00147 //}
00148 
00163 +(NSMutableArray *)loadPlaylistsFromDefaultFile:(NSArray *)allScores 
00164 {
00165     NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
00166         NSString *documentDirectory = [documentDirectories objectAtIndex:0];
00167         NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"catnip_playlists.data"];
00168     NSMutableArray *playlists = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
00169         if ([playlists count] == 0) 
00170     {
00171         return [[NSMutableArray alloc] init];
00172     }
00173     else 
00174     {
00175         NSLog(@"Read %d playlists from memory", [playlists count]);
00176         
00177         // create temporary dictionary for index - score pairs from allLocalScores
00178         NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
00179         for (LocalScoreData *l in allScores) [tempDict setObject:l forKey:[NSNumber numberWithInt:l.index]];
00180         // create playlist array to be used on application lifetime
00181         NSMutableArray *playlistsToUse = [[NSMutableArray alloc] init];
00182         
00183         // and add all saved index-playlists to it with references to real LocalScoreData objects
00184         for (Playlist *p in playlists)
00185         {
00186             Playlist *s = [[Playlist alloc] initWithName:p.name andArray:nil];
00187             NSMutableArray *a = [[NSMutableArray alloc] init];
00188             
00189             // get LocalScoreDatas in this playlist from allLocalScores as references
00190             for (NSNumber *n in p.scorelist)
00191             {
00192                 NSNumber *temp = n;
00193                 if ([tempDict objectForKey:temp] != NULL) 
00194                 {
00195                     [a addObject:[tempDict objectForKey:temp]];
00196                 }
00197             }
00198             
00199             s.scorelist = a;
00200             [playlistsToUse addObject:s];
00201             [s release];
00202             [a release];
00203         }
00204         [tempDict release];
00205         NSLog(@"Returning %d playlists.", [playlistsToUse count]);
00206         return playlistsToUse;
00207     }
00208 }
00209 
00217 +(LocalScoreData *)createLocalScoreData:(NSArray *)fileNameParams ofComposition:(CompositionData *)comp
00218 localScores:(NSArray *)scores
00219 {
00220     ScoreData *thisData; 
00221     for (int i = 0; i< [comp.musicScores count]; i++) 
00222     {
00223         if ([[[comp.musicScores objectAtIndex:i] pdf] compare:[fileNameParams objectAtIndex:0]] == 0) 
00224         {
00225             thisData = [comp.musicScores objectAtIndex:i];
00226         }
00227     }
00228     if (thisData == NULL) {
00229         NSLog(@"Could not find score data for %@", [fileNameParams objectAtIndex:0]);
00230         return NULL;
00231     }
00232     
00233     LocalScoreData *thisScore = [[LocalScoreData alloc] init];
00234     thisScore.fullMetaData = thisData.fullMetaData;
00235     thisScore.metaDataDict = thisData.metaDataDict;
00236     thisScore.description = thisData.description;
00237     thisScore.pdf = [fileNameParams objectAtIndex:1];
00238     thisScore.composer = comp.composerName;
00239     thisScore.composition = comp.compositionName;
00240     thisScore.userNotes = @"";
00241     thisScore.index = [self searchFreeIndexForScore:scores];
00242     NSLog(@"Giving index %d", thisScore.index);
00243     
00244     return thisScore;
00245 }
00246 
00247 int int_cmp(const void *a, const void *b) 
00248 { 
00249     const int *ia = (const int *)a; // casting pointer types 
00250     const int *ib = (const int *)b;
00251     return *ia  - *ib; 
00252         /* integer comparison: returns negative if b > a 
00253      and positive if a > b */ 
00254 } 
00255 
00265 +(NSInteger)searchFreeIndexForScore:(NSArray *)scores 
00266 {
00267     if (!scores) return 1;
00268     if ([scores count] == 0) return 1;
00269     else {
00270         /* takes all indeces from local scores and sort them */
00271         int indeces[[scores count]];
00272         for (int i = 0; i < [scores count]; i++) 
00273         {
00274             indeces[i] = [[scores objectAtIndex:i] index];
00275         }
00276         size_t len = sizeof(indeces)/sizeof(int);
00277         qsort(indeces, len, sizeof(int), int_cmp);
00278         
00279         /* return first positive integer which is not assigned to any score. */ 
00280         int lastIndex = 0;
00281         for (int i = 0; i < len; i++) 
00282         {
00283             if (indeces[i] - lastIndex > 1)
00284                 return lastIndex +1;
00285             else lastIndex = indeces[i];
00286         }
00287         return lastIndex +1;
00288     }
00289     return -1;
00290 }
00291 
00302 +(NSMutableDictionary *)createComposerDictionary:(NSArray *)scores
00303 {
00304     NSMutableDictionary *compDict = [[NSMutableDictionary alloc] init];
00305     for (int i = 0; i < [scores count]; i++) 
00306     {
00307         LocalScoreData *score = [scores objectAtIndex:i];
00308         if ([compDict objectForKey:score.composer] == NULL) 
00309         {
00310             NSLog(@"No entry for %@", score.composer);
00311             CompositionData *entry = [[CompositionData alloc] init];
00312             entry.composerName = score.composer;
00313             entry.compositionName = score.composition;
00314             entry.musicScores = [[NSMutableArray alloc] initWithObjects:score, nil];
00315             NSMutableArray *entries = [[NSMutableArray alloc] initWithObjects:entry, nil];
00316             [compDict setObject:entries forKey:score.composer]; 
00317             [entry release];
00318             [entries release];
00319         }
00320         else 
00321         {
00322             NSLog(@"Found entry for %@", score.composer);
00323             NSMutableArray *entries = [compDict valueForKey:score.composer];
00324             for (int j = 0; j < [entries count]; j++) 
00325             {
00326                 CompositionData *entry = [entries objectAtIndex:j];
00327                 
00328                 if ([[entry compositionName] compare:score.composition] == 0) 
00329                 // composition already found, check for possible duplicate score
00330                 {
00331                     for (int k = 0; k < [[entry musicScores] count]; k++) 
00332                     {
00333                         LocalScoreData *s = [[entry musicScores] objectAtIndex:k];
00334                         if ([s.pdf compare:score.pdf] == 0) break; // score already found, don't add it
00335                         if (k == [[entry musicScores] count] -1) 
00336                         {
00337                             [[entry musicScores] addObject:score];
00338                         }
00339                     }
00340                 }
00341                 else 
00342                 {
00343                     if (j == [entries count] -1) 
00344                     // create new CompositionData object and add it to entries 
00345                     {
00346                         CompositionData *entry = [[CompositionData alloc] init];
00347                         entry.composerName = score.composer;
00348                         entry.compositionName = score.composition;
00349                         entry.musicScores = [[NSMutableArray alloc] initWithObjects:score, nil];
00350                         [entries addObject:entry];
00351                         [entry release];
00352                     }
00353                 }
00354             }
00355         }
00356     }
00357     return compDict;
00358 }
00359 
00360 
00371 +(void)removeLocalScoreData:(LocalScoreData *)score fromScoreArray:(NSMutableArray *)sarray
00372 {
00373     NSMutableArray *scoresToRemove = [[NSMutableArray alloc] init];
00374     
00375     for (int i = 0; i < [sarray count]; i++) 
00376     {
00377         LocalScoreData *s = [sarray objectAtIndex:i];
00378         if ([s.pdf compare:score.pdf] == 0) 
00379         {    
00380             [scoresToRemove addObject:s];
00381         }
00382     }
00383     NSLog(@"local scores count %d, scores to remove count %d", [sarray count], [scoresToRemove count]);
00384     [sarray removeObjectsInArray:scoresToRemove];
00385     NSLog(@"local scores count after removing %d", [sarray count]);
00386     [scoresToRemove release];
00387 }
00388 
00402 +(void)removeLocalScoreData:(LocalScoreData *)score fromDictionary:(NSMutableDictionary *)dict
00403 {
00404     [score retain];
00405     NSMutableArray *scoresToRemove = [[NSMutableArray alloc] init];
00406     NSMutableArray *composersCompositions = [dict objectForKey:score.composer];
00407     NSMutableArray *compositionsToRemove = [[NSMutableArray alloc] init];
00408     
00409     for (int i = 0; i < [composersCompositions count]; i++) 
00410     {
00411         CompositionData *c = [composersCompositions objectAtIndex:i];
00412         if ([c.compositionName compare:score.composition] == 0) 
00413         {    
00414             for (LocalScoreData *s in c.musicScores) 
00415             {
00416                 if ([s.pdf compare:score.pdf] == 0) // remove all scores with same pdf filepath
00417                 {
00418                     [scoresToRemove addObject:s];
00419                 }
00420             }
00421             [c.musicScores removeObjectsInArray:scoresToRemove];
00422             [scoresToRemove removeAllObjects];
00423         }
00424         if ([c.musicScores count] == 0) // if all scores are removed compositionData can be removed also
00425         {
00426             NSLog(@"Adding compositionData of %@ - %@ to remove array", c.composerName, c.compositionName);
00427             [compositionsToRemove addObject:c];
00428         }    
00429     }
00430     [composersCompositions removeObjectsInArray:compositionsToRemove];
00431     
00432     if ([composersCompositions count] == 0) // if all compositions from a composer are removed, composer can be removed too
00433     {
00434         NSLog(@"Removing composer from dictionary");
00435         [dict removeObjectForKey:score.composer];
00436     }
00437     [scoresToRemove release];
00438     [compositionsToRemove release];
00439     [score release];
00440 }
00441 
00450 +(void)removeLocalScoreData:(LocalScoreData *)score fromPlaylistArray:(NSMutableArray *)parray
00451 {
00452     for (int i = 0; i < [parray count]; i++) 
00453     {
00454         [LocalDataHandler removeLocalScoreData:score fromScoreArray:[[parray objectAtIndex:i] scorelist]];
00455     }
00456 }
00457 
00466 +(void)removeAndDeleteLocalScoreData:(LocalScoreData*)score fromScoreArray:(NSMutableArray *)sarray andDictionary:(NSMutableDictionary *)dict andPlaylistArray:(NSMutableArray *)parray
00467 {
00468     [score retain];
00469     [LocalDataHandler removeLocalScoreData:score fromScoreArray:sarray];
00470     [LocalDataHandler removeLocalScoreData:score fromDictionary:dict];
00471     [LocalDataHandler removeLocalScoreData:score fromPlaylistArray:parray];
00472     [LocalDataHandler deleteFile:score.pdf];
00473     [score release];
00474 }
00475 
00481 +(void)removeScoreDataArray:(NSArray *)sdarray fromScoreArray:(NSMutableArray *)sarray 
00482 {
00483     for (int i = 0; i < [sdarray count]; i++) 
00484     {
00485         LocalScoreData *s = [sdarray objectAtIndex:i];
00486         [LocalDataHandler removeLocalScoreData:s fromScoreArray:sarray];
00487     }
00488 }
00489 
00495 +(void)removeScoreDataArray:(NSArray *)sdarray fromDictionary:(NSMutableDictionary *)dict
00496 {
00497     for (int i = 0; i < [sdarray count]; i++) 
00498     {
00499         LocalScoreData *s = [sdarray objectAtIndex:i];
00500         [LocalDataHandler removeLocalScoreData:s fromDictionary:dict];
00501     }
00502 }
00503 
00509 +(void)removeScoreDataArray:(NSArray *)sdarray fromPlaylistArray:(NSMutableArray *)parray 
00510 {
00511     for (Playlist *p in parray)
00512     {
00513         NSMutableArray *scoresToRemove = [[NSMutableArray alloc] init];
00514         for (LocalScoreData *s in sdarray) 
00515         {
00516             for (LocalScoreData *ps in p.scorelist)
00517             {
00518                 if ([ps.pdf compare:s.pdf] == 0)
00519                 {
00520                     [scoresToRemove addObject:ps];
00521                     //NSLog(@"removing %@ from %@", ps.pdf, p.name);
00522                 }
00523                     
00524             }
00525         }
00526         NSLog(@"removing %d scores from %@", [scoresToRemove count], p.name);
00527         [p.scorelist removeObjectsInArray:scoresToRemove];
00528         [scoresToRemove release];
00529     }
00530 }
00531 
00540 +(void)removeAndDeleteScoreDataArray:(NSArray *)sdarray fromScoreArray:(NSMutableArray *)sarray andDictionary:(NSMutableDictionary *)dict andPlaylistArray:(NSMutableArray *)parray
00541 {
00542     for (LocalScoreData *s in sdarray) 
00543     {
00544         [LocalDataHandler removeAndDeleteLocalScoreData:s fromScoreArray:sarray andDictionary:dict andPlaylistArray:parray];
00545     }
00546 }
00547 
00553 +(void)removeCompositionDataArray:(NSArray *)cdarray fromScoreArray:(NSMutableArray *)sarray
00554 {
00555     for (int i = 0; i < [cdarray count]; i++) 
00556     {
00557         CompositionData *cd = [cdarray objectAtIndex:i];
00558         NSArray *sa = [cd musicScores];
00559         [LocalDataHandler removeScoreDataArray:sa fromScoreArray:sarray];
00560     }
00561 }
00562 
00568 +(void)removeCompositionDataArray:(NSArray *)cdarray fromDictionary:(NSMutableDictionary *)dict
00569 {
00570     for (int i = 0; i < [cdarray count]; i++) 
00571     {
00572         CompositionData *cd = [cdarray objectAtIndex:i];
00573         NSArray *sa = [cd musicScores];
00574         [LocalDataHandler removeScoreDataArray:sa fromDictionary:dict];
00575     }
00576 }
00577 
00583 +(void)removeCompositionDataArray:(NSArray *)cdarray fromPlaylistArray:(NSMutableArray *)parray
00584 {
00585     for (int i = 0; i < [cdarray count]; i++) 
00586     {
00587         CompositionData *cd = [cdarray objectAtIndex:i];
00588         NSArray *sa = [cd musicScores];
00589         [LocalDataHandler removeScoreDataArray:sa fromPlaylistArray:parray];
00590     }
00591 }
00592 
00601 +(void)removeAndDeleteCompositionDataArray:(NSArray *)cdarray fromScoreArray:(NSMutableArray *)sarray andDictionary:(NSMutableDictionary *)dict andPlaylistArray:(NSMutableArray *)parray 
00602 {
00603     [cdarray retain];
00604     for (int i = 0; i < [cdarray count]; i++) 
00605     {
00606         CompositionData *cd = [cdarray objectAtIndex:i]; 
00607         NSArray *sa = [[NSArray alloc] initWithArray:cd.musicScores copyItems:NO];
00608         [sa retain];
00609         [LocalDataHandler removeScoreDataArray:sa fromScoreArray:sarray];
00610         [LocalDataHandler removeScoreDataArray:sa fromPlaylistArray:parray];
00611         NSMutableArray *filepaths = [[NSMutableArray alloc] init];
00612         for (LocalScoreData *s in sa) [filepaths addObject:s.pdf];
00613         [LocalDataHandler deleteFiles:filepaths];
00614         [filepaths release];
00615         [sa release];
00616     }
00617     [LocalDataHandler removeCompositionDataArray:cdarray fromDictionary:dict];
00618     [cdarray release];
00619 }
00620 
00627 +(BOOL)scoreArray:(NSArray *)sarray hasScoreWithPdfFileName:(NSString *)filename 
00628 {
00629     for (LocalScoreData *s in sarray)
00630     {
00631        if ([[s.pdf lastPathComponent] compare:filename] == 0)
00632            return YES;
00633     }
00634     return NO;
00635 }
00636 
00643 +(BOOL)scoreArray:(NSArray *)sarray hasScoreWithPdfFilePath:(NSString *)filepath 
00644 {
00645     for (LocalScoreData *s in sarray)
00646     {
00647         if ([s.pdf compare:filepath] == 0)
00648             return YES;
00649     }
00650     return NO;
00651 }
00652 
00660 +(BOOL)compositionDataArray:(NSArray *)cdarray hasScoreWithPdfInScoreArray:(NSArray *)sarray
00661 {
00662     for (LocalScoreData *s in sarray)
00663     {
00664         NSString *filepath = s.pdf;
00665         
00666         for (int i = 0 ; i < [cdarray count] ; i++) 
00667         {
00668             CompositionData *cd = [cdarray objectAtIndex:i];
00669             if ([LocalDataHandler scoreArray:cd.musicScores hasScoreWithPdfFilePath:filepath])
00670                 return YES;
00671         }
00672     }
00673     return NO;
00674 }
00675 
00676 @end
 All Classes Files Functions Variables Enumerations Enumerator Properties Defines