PageRenderTime 66ms CodeModel.GetById 45ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/GDataEntryMailItem.m

http://google-email-uploader-mac.googlecode.com/
Objective C | 175 lines | 115 code | 43 blank | 17 comment | 2 complexity | 40f0bfc36e7aff6aa73e1e909e3baa43 MD5 | raw file
  1/* Copyright (c) 2009 Google Inc.
  2 *
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 *
  7 *     http://www.apache.org/licenses/LICENSE-2.0
  8 *
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS,
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License.
 14 */
 15
 16//
 17//  GDataEntryMailItem.m
 18//
 19
 20#define GDATAMAILITEM_DEFINE_GLOBALS 1
 21
 22#import "GDataEntryMailItem.h"
 23#import "GDataEntryLink.h"
 24
 25static NSString* const kEncodingAttr = @"encoding";
 26
 27@implementation GDataMailItemProperty
 28+ (NSString *)extensionElementURI       { return kGDataNamespaceGoogleApps; }
 29+ (NSString *)extensionElementPrefix    { return kGDataNamespaceGoogleAppsPrefix; }
 30+ (NSString *)extensionElementLocalName { return @"mailItemProperty"; }
 31@end
 32
 33@implementation GDataMailItemLabel
 34+ (NSString *)extensionElementURI       { return kGDataNamespaceGoogleApps; }
 35+ (NSString *)extensionElementPrefix    { return kGDataNamespaceGoogleAppsPrefix; }
 36+ (NSString *)extensionElementLocalName { return @"label"; }
 37
 38- (NSString *)attributeName {
 39  return @"labelName";
 40}
 41@end
 42
 43@implementation GDataMailItemSpamSetting
 44+ (NSString *)extensionElementURI       { return kGDataNamespaceGoogleApps; }
 45+ (NSString *)extensionElementPrefix    { return kGDataNamespaceGoogleAppsPrefix; }
 46+ (NSString *)extensionElementLocalName { return @"spamSettings"; }
 47@end
 48
 49@implementation GDataEntryMailItem
 50
 51+ (NSDictionary *)appsNamespaces {
 52  NSMutableDictionary *namespaces;
 53  
 54  namespaces = [NSMutableDictionary dictionaryWithObject:kGDataNamespaceGoogleApps
 55                                                  forKey:kGDataNamespaceGoogleAppsPrefix];
 56  
 57  [namespaces addEntriesFromDictionary:[GDataEntryBase baseGDataNamespaces]];
 58  
 59  return namespaces;
 60}
 61
 62+ (GDataEntryMailItem *)mailItem {
 63  GDataEntryMailItem *obj;
 64  obj = [[[GDataEntryMailItem alloc] init] autorelease];
 65
 66  [obj setNamespaces:[GDataEntryMailItem appsNamespaces]];
 67
 68  return obj;
 69}
 70
 71+ (GDataEntryMailItem *)mailItemWithRFC822String:(NSString *)str {
 72
 73  GDataEntryMailItem *entry = [self mailItem];
 74
 75  NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
 76
 77  [entry setUploadData:data];
 78  [entry setUploadMIMEType:@"message/rfc822"];
 79  return entry;
 80}
 81
 82#pragma mark -
 83
 84+ (NSString *)standardEntryKind {
 85  return kGDataCategoryMailItem;
 86}
 87
 88+ (void)load {
 89  [self registerEntryClass];
 90}
 91
 92- (void)addExtensionDeclarations {
 93  
 94  [super addExtensionDeclarations];
 95  
 96  [self addExtensionDeclarationForParentClass:[self class]
 97                                   childClasses:
 98   [GDataMailItemProperty class],
 99   [GDataMailItemLabel class],
100   [GDataMailItemSpamSetting class],
101   nil];
102}
103
104- (NSMutableArray *)itemsForDescription {
105  
106  NSData *uploadData = [self uploadData];
107  NSString *msgStr = [[[NSString alloc] initWithData:uploadData
108                                            encoding:NSUTF8StringEncoding] autorelease];
109  if (msgStr == nil) {
110   msgStr = @"<Unknown upload data>"; 
111  }
112
113  NSArray *labels = [self valueForKeyPath:@"mailItemLabels.stringValue"];
114  NSString *labelStr = [labels componentsJoinedByString:@","];
115  
116  struct GDataDescriptionRecord descRecs[] = {
117    { @"properties", @"mailItemProperties", kGDataDescValueLabeled     },
118    { @"labels",     labelStr,              kGDataDescValueIsKeyPath   },
119    { @"msg",        msgStr,                kGDataDescValueIsKeyPath   },
120    { @"filterSpam", @"shouldFilterSpam",   kGDataDescBooleanPresent   },
121    { nil, nil, 0 }
122  };
123  
124  NSMutableArray *items = [super itemsForDescription];
125  [self addDescriptionRecords:descRecs toItems:items];
126  return items;
127}
128
129#pragma mark -
130
131- (NSArray *)mailItemLabels {
132  return [self objectsForExtensionClass:[GDataMailItemLabel class]];
133}
134
135- (void)setMailItemLabels:(NSArray *)array {
136  [self setObjects:array forExtensionClass:[GDataMailItemLabel class]];
137}
138
139- (void)addMailItemLabel:(GDataMailItemLabel *)obj {
140  [self addObject:obj forExtensionClass:[GDataMailItemLabel class]];
141}
142
143- (void)addMailItemLabelWithString:(NSString *)str {
144  GDataMailItemLabel *label = [GDataMailItemLabel valueWithString:str];
145  [self addMailItemLabel:label];
146}
147
148- (NSArray *)mailItemProperties {
149  return [self objectsForExtensionClass:[GDataMailItemProperty class]];
150}
151
152- (void)setMailItemProperties:(NSArray *)array {
153  [self setObjects:array forExtensionClass:[GDataMailItemProperty class]];
154}
155
156- (void)addMailItemProperty:(GDataMailItemProperty *)obj {
157  [self addObject:obj forExtensionClass:[GDataMailItemProperty class]];
158}
159
160- (void)addMailItemPropertyWithString:(NSString *)str {
161  GDataMailItemProperty *prop = [GDataMailItemProperty valueWithString:str];
162  [self addMailItemProperty:prop];
163}
164
165- (BOOL)shouldFilterSpam {
166  id obj = [self objectForExtensionClass:[GDataMailItemSpamSetting class]]; 
167  return [obj boolValue];
168}
169
170- (void)setShouldFilterSpam:(BOOL)flag {
171  GDataMailItemSpamSetting *obj = [GDataMailItemSpamSetting valueWithBool:flag];
172  [self setObject:obj forExtensionClass:[GDataMailItemSpamSetting class]];
173}
174@end
175