root/tags/2D14/Importer/GetMetadataForFile.m

Revision 2584, 3.6 kB (checked in by timothy, 4 years ago)

Should be properly testing if we are building on 10.4 now.
Added an autorelease pool, the docs said not to assume.

Line 
1 #include <AvailabilityMacros.h>
2
3 #ifdef MAC_OS_X_VERSION_10_4
4
5 #include <CoreFoundation/CoreFoundation.h>
6 #include <CoreServices/CoreServices.h>
7 #import <Foundation/Foundation.h>
8
9 Boolean GetMetadataForFile( void *thisInterface, CFMutableDictionaryRef attributes, CFStringRef contentTypeUTI, CFStringRef pathToFile ) {
10         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
11
12         NSMutableDictionary *returnDict = (NSMutableDictionary *) attributes;
13         NSURL *file = [NSURL fileURLWithPath:(NSString *) pathToFile];
14         NSString *title = [[(NSString *) pathToFile lastPathComponent] stringByDeletingPathExtension];
15
16         // Set title
17         [returnDict setObject:title forKey:(NSString *) kMDItemTitle];
18
19         NSError *error = nil;
20     NSXMLDocument *transcript = [[[NSXMLDocument alloc] initWithContentsOfURL:file options:NSXMLDocumentTidyXML error:&error] autorelease];
21
22         // Get date started
23     NSDate *dateStarted = nil;
24         NSArray *xpathResult = [transcript nodesForXPath:@"/log[1]/@began" error:&error];
25
26         if( ! [xpathResult count] )
27                 xpathResult = [transcript nodesForXPath:@"/log[1]/envelope[1]/message[1]/@received" error:&error];
28
29         if( [xpathResult count] ) {
30                 dateStarted = [NSDate dateWithString:[[xpathResult objectAtIndex:0] stringValue]];
31                 [returnDict setObject:dateStarted forKey:(NSString *) kMDItemContentCreationDate];
32         }
33
34         // Get date of last message
35         NSDate *lastMessageDate = nil;
36
37         xpathResult = [transcript nodesForXPath:@"/log[1]/envelope[last()]/message[last()]/@received" error:&error];
38         if( [xpathResult count] ) {
39                 lastMessageDate = [NSDate dateWithString:[[xpathResult objectAtIndex:0] stringValue]];
40                 [returnDict setObject:lastMessageDate forKey:(NSString *) kMDItemContentModificationDate];
41                 [returnDict setObject:lastMessageDate forKey:(NSString *) kMDItemLastUsedDate];
42         }
43
44         if( lastMessageDate && dateStarted ) {
45                 // Set Duration
46                 NSTimeInterval logDuration = [lastMessageDate timeIntervalSinceDate:dateStarted];
47                 [returnDict setObject:[NSNumber numberWithInt:logDuration] forKey:(NSString *) kMDItemDurationSeconds];
48
49                 // Set Coverage
50                 NSDateFormatter *formatter = [[[NSDateFormatter alloc] initWithDateFormat:@"" allowNaturalLanguage:NO] autorelease];
51                 [formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
52                 [formatter setDateStyle:NSDateFormatterShortStyle];
53                 [formatter setTimeStyle:NSDateFormatterShortStyle];
54
55                 NSString *coverageWording = [NSString stringWithFormat:@"%@ - %@", [formatter stringFromDate:dateStarted], [formatter stringFromDate:lastMessageDate]];
56                 [returnDict setObject:coverageWording forKey:(NSString *) kMDItemCoverage];
57         }
58
59         // Get Where From
60         xpathResult = [transcript nodesForXPath:@"/log[1]/@source" error:&error];
61         NSString *fromURL = nil;
62         if( [xpathResult count] ) {
63                 fromURL = [[xpathResult objectAtIndex:0] stringValue];
64                 [returnDict setObject:fromURL forKey:(NSString *) kMDItemWhereFroms];
65         }
66
67         // Get Participants
68         NSMutableArray *participants = [NSMutableArray array];
69         NSArray *participantNodes = [transcript nodesForXPath:@"/log[1]/envelope/sender" error:&error];
70         NSEnumerator *enumerator = [participantNodes objectEnumerator];
71         NSXMLNode *participantNode = nil;
72
73         while( ( participantNode = [enumerator nextObject] ) ) {
74                 NSString *nickname = [participantNode stringValue];
75                 if( ! [participants containsObject:nickname] )
76                         [participants addObject:nickname];
77         }
78
79         [returnDict setObject:participants forKey:(NSString *) kMDItemContributors];
80
81         // Now the stuff that doesn't change
82         [returnDict setObject:[NSArray arrayWithObject:@"Chat transcript"] forKey:(NSString *) kMDItemKind];
83         [returnDict setObject:[NSArray arrayWithObject:@"Colloquy"] forKey:(NSString *) kMDItemCreator];
84
85         [pool release];
86
87     return TRUE;
88 }
89
90 #endif
Note: See TracBrowser for help on using the browser.