PageRenderTime 85ms CodeModel.GetById 61ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/SpotlightPlugins/XcodeProject/GetMetadataForFile.m

http://macfuse.googlecode.com/
Objective C | 85 lines | 63 code | 2 blank | 20 comment | 16 complexity | 5d8af3fae5eb59a2ede590c4be38069d MD5 | raw file
 1//
 2//  GetMetadataForFile.m
 3//
 4//  Copyright 2008 Google Inc.
 5//
 6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
 7//  use this file except in compliance with the License.  You may obtain a copy
 8//  of the License at
 9// 
10//  http://www.apache.org/licenses/LICENSE-2.0
11// 
12//  Unless required by applicable law or agreed to in writing, software
13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15//  License for the specific language governing permissions and limitations under
16//  the License.
17//
18
19#import <Foundation/Foundation.h>
20static BOOL ImportProjectFile(NSMutableDictionary *attributes, 
21                              NSString *pathToFile) {
22  pathToFile = [pathToFile stringByAppendingPathComponent:@"project.pbxproj"];
23  NSMutableSet *filenames = [[[NSMutableSet alloc] init] autorelease];
24  NSMutableSet *comments = [[[NSMutableSet alloc] init] autorelease];
25  BOOL wasGood = NO;
26  NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:pathToFile];
27  if (dict) {
28    NSDictionary *objects = [dict objectForKey:@"objects"];
29    if (objects) {
30      NSEnumerator *objEnumerator = [objects objectEnumerator];
31      NSDictionary *object;
32      while ((object = [objEnumerator nextObject])) {
33        NSString *isaType = [object objectForKey:@"isa"];
34        if ([isaType caseInsensitiveCompare:@"PBXFileReference"] == NSOrderedSame) {
35          NSString *path = [object objectForKey:@"path"];
36          if (path) {
37            [filenames addObject:[path lastPathComponent]];
38          }
39        } else if ([isaType caseInsensitiveCompare:@"PBXNativeTarget"] == NSOrderedSame) {
40          NSString *name = [object objectForKey:@"name"];
41          if (name) {
42            [filenames addObject:name];
43          }
44          name = [object objectForKey:@"productName"];
45          if (name) {
46            [filenames addObject:name];
47          }
48        }
49        NSString *comment = [object objectForKey:@"comments"]; 
50        if (comment) {
51          [comments addObject:comment];
52        }
53      }
54    }
55  }
56  if ([filenames count]) {
57    NSString *description = [[filenames allObjects] componentsJoinedByString:@"\n"];
58    [attributes setObject:description forKey:(NSString*)kMDItemDescription];
59    wasGood = YES;
60  }
61  if ([comments count]) {
62    NSString *comment = [[comments allObjects] componentsJoinedByString:@"\n"];
63    [attributes setObject:comment forKey:(NSString*)kMDItemComment];
64    wasGood = YES;
65  }
66  return wasGood;
67}  
68
69// Currently grabs all the filenames, target names, and product names
70// and sticks them into kMDItemDescription.
71// It also grabs all of the comments and sticks them into kMDItemComment.
72Boolean GetMetadataForFile(void* interface, 
73                           CFMutableDictionaryRef cfAttributes, 
74                           CFStringRef contentTypeUTI,
75                           CFStringRef cfPathToFile) {
76  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
77  NSMutableDictionary *attributes = (NSMutableDictionary*)cfAttributes;
78  NSString *pathToFile = (NSString*)cfPathToFile;
79  BOOL wasGood = NO;
80  if (UTTypeConformsTo(contentTypeUTI, CFSTR("com.apple.xcode.project"))) {
81    wasGood = ImportProjectFile(attributes, pathToFile);
82  }
83  [pool release];
84  return wasGood == NO ? FALSE : TRUE;
85}