source: trunk/SapphirePopulateDataMenu.m @ 123

Last change on this file since 123 was 123, checked in by pmerrill, 10 years ago
  • changed MyCompany? name to www.nanopi.net
  • added xml importing to the file import tool
File size: 5.5 KB
Line 
1//
2//  SapphirePopulateDataMenu.m
3//  Sapphire
4//
5//  Created by pnmerrill on 6/24/07.
6//  Copyright 2007 __www.nanopi.net__. All rights reserved.
7//
8
9#import "SapphirePopulateDataMenu.h"
10#import "SapphireMetaData.h"
11
12//Single Attributes
13#define MEDIA_TVSHOW_XML_QUERY          @"/media[@type='TV Show']/text()"
14#define MEDIA_MOVIE_XML_QUERY           @"/media[@type='Movie']/text()"
15#define SUMMARY_XML_QUERY                       @"/media/summary/text()"
16#define DESCRIPTION_XML_QUERY           @"/media/description/text()"
17#define PUBLISHER_XML_QUERY                     @"/media/publisher/text()"
18#define COPYRIGHT_XML_QUERY                     @"/media/copyright/text()"
19#define USER_RATING_XML_QUERY           @"/media/userStarRating/text()"
20#define RATING_XML_QUERY                        @"/media/rating/text()"
21#define SERIES_NAME_XML_QUERY           @"/media/seriesName/text()"
22#define BROADCASTER_XML_QUERY           @"/media/broadcaster/text()"
23#define EPISODE_NUMBER_XML_QUERY        @"/media/episodeNumber/text()"
24#define EPISODE_XML_QUERY                       @"/media/episode/text()"
25#define SEASON_XML_QUERY                        @"/media/season/text()"
26#define PUBLISHED_XML_QUERY                     @"/media/published/text()"
27//Multi Attributes             
28#define TITLE_XML_QUERY                         @"/media/title/text()"
29#define GENRES_XML_QUERY                        @"/media/genres/genre/text()"
30#define CAST_XML_QUERY                          @"/media/cast/name/text()"
31#define DIRECTORS_XML_QUERY                     @"/media/directors/name/text()"
32#define PRODUCERS_XML_QUERY                     @"/media/producers/name/text()"
33
34@interface SapphireImporterDataMenu (private)
35- (void)setText:(NSString *)theText;
36- (void)setFileProgress:(NSString *)updateFileProgress;
37- (void)resetUIElements;
38- (void)importNextItem:(NSTimer *)timer;
39- (void)setCurrentFile:(NSString *)theCurrentFile;
40@end
41
42
43@implementation SapphirePopulateDataMenu
44
45static NSDictionary *xmlSingleAttributes = nil;
46static NSDictionary *xmlMultiAttributes = nil;
47
48+(void) initialize
49{
50        xmlSingleAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
51                META_TITLE_KEY,                                 TITLE_XML_QUERY,
52                META_SUMMARY_KEY,                               SUMMARY_XML_QUERY,
53                META_DESCRIPTION_KEY,                   DESCRIPTION_XML_QUERY,
54                @"Publisher",                                   PUBLISHER_XML_QUERY,
55                META_COPYRIGHT_KEY,                             COPYRIGHT_XML_QUERY,
56                META_SHOW_FAVORITE_RATING_KEY,  USER_RATING_XML_QUERY,
57                META_SHOW_RATING_KEY,                   RATING_XML_QUERY,
58                META_SHOW_NAME_KEY,                             SERIES_NAME_XML_QUERY,
59                META_SHOW_BROADCASTER_KEY,              BROADCASTER_XML_QUERY,
60                @"Episode Number",                              EPISODE_NUMBER_XML_QUERY,
61                META_EPISODE_NUMBER_KEY,                EPISODE_XML_QUERY,
62                META_SEASON_NUMBER_KEY,                 SEASON_XML_QUERY,
63                META_SHOW_PUBLISHED_DATE_KEY,   PUBLISHED_XML_QUERY,nil] ;
64        xmlMultiAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
65                @"Genres",                              GENRES_XML_QUERY,
66                @"Cast",                                CAST_XML_QUERY,
67                @"Producers" ,                  PRODUCERS_XML_QUERY,
68                @"Directors",                   DIRECTORS_XML_QUERY,nil];
69}
70
71
72
73- (void)getItems
74{
75        importItems = [[meta subFileMetas] mutableCopy];
76        xmlFileCount=0 ;
77}
78
79- (BOOL)doImport
80{
81        SapphireFileMetaData *fileMeta = [importItems objectAtIndex:0];
82        return [fileMeta updateMetaData];
83}
84
85- (void)setCompletionText
86{
87        [self setText:@"Sapphire will continue to import new files as it encounters them.  You may initiate this import again at any time, and any new or changed files will be imported"];
88}
89
90- (void)importXMLFile:(NSString *)xmlFileName forMeta: (SapphireFileMetaData *) fileMeta
91{
92        NSURL *url = [NSURL fileURLWithPath:xmlFileName];
93        NSError *error = nil;
94        NSMutableDictionary * metaData=[NSMutableDictionary dictionary];
95        NSXMLDocument *document = [[NSXMLDocument alloc] initWithContentsOfURL:url options:NSXMLDocumentTidyXML error:&error];
96        NSXMLElement *root = [document rootElement];
97        if(!root)
98                return;
99/*
100        NSString *type = [[root attributeForName:@"type"] stringValue];
101        //Need to catch the media type {"TV Show" , "Movie"}
102        if(type!=nil)metaData
103*/
104        NSEnumerator *keyEnum = [xmlSingleAttributes keyEnumerator];
105        NSString *key = nil;
106       
107        while((key = [keyEnum nextObject]) != nil)
108        {
109                NSArray *objects = [root objectsForXQuery:key error:&error];
110                if([objects count])
111                {
112                        [metaData setObject:[[objects objectAtIndex:0] stringValue] forKey:[xmlSingleAttributes objectForKey:key]] ;
113                }
114    }
115        keyEnum = [xmlMultiAttributes keyEnumerator];
116        while((key = [keyEnum nextObject]) != nil)
117        {
118                NSArray *objects = [root objectsForXQuery:key error:&error];
119                int count = [objects count];
120                NSMutableArray *newData= nil;
121                if(!count)
122                        continue;
123                newData = [NSMutableArray arrayWithCapacity:count];
124                NSEnumerator *objectsEnum = [objects objectEnumerator];
125                NSXMLNode *node = nil;
126                while((node = [objectsEnum nextObject]) != nil)
127                {
128                        [newData addObject:[node stringValue]];
129                }
130                [metaData setObject:newData forKey:[xmlMultiAttributes objectForKey:key]] ;
131
132        }
133        [fileMeta importInfo: metaData fromSource:META_XML_IMPORT_KEY];
134}
135
136
137- (void)importNextItem:(NSTimer *)timer
138{
139        NSFileManager *fm = [NSFileManager defaultManager];
140        SapphireFileMetaData *fileMeta = [importItems objectAtIndex:0];
141        NSString * fileName=[[fileMeta path] lastPathComponent] ;
142        NSString * xmlFilePath=[fileMeta path] ;
143        xmlPathIsDir = NO;
144        xmlFilePath=[[xmlFilePath stringByDeletingPathExtension] stringByAppendingPathExtension:@"xml"];
145        if([fm fileExistsAtPath:xmlFilePath isDirectory:&xmlPathIsDir] && !xmlPathIsDir)
146        {
147                [self importXMLFile:xmlFilePath forMeta:fileMeta] ;
148                xmlFileCount++ ;
149        }
150        xmlPathIsDir = NO;
151        [self setCurrentFile:[NSString stringWithFormat:@"Current File: %@ XML=%d",fileName,xmlFileCount]];
152        [super importNextItem:timer];
153}
154
155
156
157- (void)resetUIElements
158{
159        [super resetUIElements];
160        [title setTitle: @"Import File Data"];
161        [self setText:@"This will populate Sapphire's File data.  This proceedure may take a while, but you may cancel at any time"];
162        [button setTitle: @"Import File Data"];
163}
164@end
Note: See TracBrowser for help on using the repository browser.