~jose-exposito89/ubuntuone-ios-files/social-networking

« back to all changes in this revision

Viewing changes to Dependencies/ShareKit/Classes/ShareKit/Sharers/Actions/Email/SHKMail.m

  • Committer: José Expósito
  • Date: 2012-10-23 15:22:36 UTC
  • Revision ID: jose.exposito89@gmail.com-20121023152236-f2rfafq000ex0vgz
Added support to share links in social networks

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//  SHKMail.m
 
3
//  ShareKit
 
4
//
 
5
//  Created by Nathan Weiner on 6/17/10.
 
6
 
 
7
//
 
8
//  Permission is hereby granted, free of charge, to any person obtaining a copy
 
9
//  of this software and associated documentation files (the "Software"), to deal
 
10
//  in the Software without restriction, including without limitation the rights
 
11
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
12
//  copies of the Software, and to permit persons to whom the Software is
 
13
//  furnished to do so, subject to the following conditions:
 
14
//
 
15
//  The above copyright notice and this permission notice shall be included in
 
16
//  all copies or substantial portions of the Software.
 
17
//
 
18
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
19
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
20
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
21
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
22
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
23
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
24
//  THE SOFTWARE.
 
25
//
 
26
//
 
27
 
 
28
#import "SHKConfiguration.h"
 
29
#import "SHKMail.h"
 
30
 
 
31
 
 
32
@implementation MFMailComposeViewController (SHK)
 
33
 
 
34
- (void)SHKviewDidDisappear:(BOOL)animated
 
35
{       
 
36
        [super viewDidDisappear:animated];
 
37
        
 
38
        // Remove the SHK view wrapper from the window (but only if the view doesn't have another modal over it)
 
39
        if (self.modalViewController == nil)
 
40
                [[SHK currentHelper] viewWasDismissed];
 
41
}
 
42
 
 
43
@end
 
44
 
 
45
 
 
46
 
 
47
@implementation SHKMail
 
48
 
 
49
#pragma mark -
 
50
#pragma mark Configuration : Service Defination
 
51
 
 
52
+ (NSString *)sharerTitle
 
53
{
 
54
        return SHKLocalizedString(@"Email");
 
55
}
 
56
 
 
57
+ (BOOL)canShareText
 
58
{
 
59
        return YES;
 
60
}
 
61
 
 
62
+ (BOOL)canShareURL
 
63
{
 
64
        return YES;
 
65
}
 
66
 
 
67
+ (BOOL)canShareImage
 
68
{
 
69
        return YES;
 
70
}
 
71
 
 
72
+ (BOOL)canShareFile
 
73
{
 
74
        return YES;
 
75
}
 
76
 
 
77
+ (BOOL)shareRequiresInternetConnection
 
78
{
 
79
        return NO;
 
80
}
 
81
 
 
82
+ (BOOL)requiresAuthentication
 
83
{
 
84
        return NO;
 
85
}
 
86
 
 
87
 
 
88
#pragma mark -
 
89
#pragma mark Configuration : Dynamic Enable
 
90
 
 
91
+ (BOOL)canShare
 
92
{
 
93
        return [MFMailComposeViewController canSendMail];
 
94
}
 
95
 
 
96
- (BOOL)shouldAutoShare
 
97
{
 
98
        return YES;
 
99
}
 
100
 
 
101
 
 
102
 
 
103
#pragma mark -
 
104
#pragma mark Share API Methods
 
105
 
 
106
- (BOOL)send
 
107
{
 
108
        self.quiet = YES;
 
109
        
 
110
        if (![self validateItem])
 
111
                return NO;
 
112
        
 
113
        return [self sendMail]; // Put the actual sending action in another method to make subclassing SHKMail easier
 
114
}
 
115
 
 
116
- (BOOL)sendMail
 
117
{       
 
118
        MFMailComposeViewController *mailController = [[[MFMailComposeViewController alloc] init] autorelease];
 
119
        if (!mailController) {
 
120
                // e.g. no mail account registered (will show alert)
 
121
                [[SHK currentHelper] hideCurrentViewControllerAnimated:YES];
 
122
                return YES;
 
123
        }
 
124
        
 
125
    [self retain]; //must retain, because mailController does not retain its delegates. Released in callback.
 
126
        mailController.mailComposeDelegate = self;
 
127
        mailController.navigationBar.tintColor = SHKCONFIG_WITH_ARGUMENT(barTintForView:,mailController);
 
128
        
 
129
        NSString *body = item.text;
 
130
        BOOL isHTML = self.item.isMailHTML;
 
131
        NSString *separator = (isHTML ? @"<br/><br/>" : @"\n\n");
 
132
    
 
133
        if (body == nil)
 
134
        {
 
135
                body = @"";
 
136
                
 
137
                if (item.URL != nil)
 
138
                {
 
139
                        NSString *urlStr = [item.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
140
                        
 
141
                        if (isHTML)
 
142
                                body = [body stringByAppendingFormat:@"%@%@", separator, urlStr];
 
143
                        else
 
144
                                body = urlStr;
 
145
                }
 
146
                
 
147
                if (item.data)
 
148
                {
 
149
                        NSString *attachedStr = SHKLocalizedString(@"Attached: %@", item.title ? item.title : item.filename);
 
150
                        
 
151
                        if (isHTML)
 
152
                                body = [body stringByAppendingFormat:@"%@%@", separator, attachedStr];
 
153
                        else
 
154
                                body = attachedStr;
 
155
                }
 
156
                
 
157
                // fallback
 
158
                if (body == nil)
 
159
                        body = @"";
 
160
                
 
161
                // sig
 
162
                if (self.item.mailShareWithAppSignature)
 
163
                {
 
164
                        body = [body stringByAppendingString:separator];
 
165
                        body = [body stringByAppendingString:SHKLocalizedString(@"Sent from %@", SHKCONFIG(appName))];
 
166
                }
 
167
        }
 
168
        
 
169
        if (item.data)          
 
170
                [mailController addAttachmentData:item.data mimeType:item.mimeType fileName:item.filename];
 
171
        
 
172
        NSArray *toRecipients = self.item.mailToRecipients;
 
173
    if (toRecipients)
 
174
                [mailController setToRecipients:toRecipients];
 
175
    
 
176
        if (item.image){
 
177
        
 
178
        CGFloat jpgQuality = self.item.mailJPGQuality;
 
179
        [mailController addAttachmentData:UIImageJPEGRepresentation(item.image, jpgQuality) mimeType:@"image/jpeg" fileName:@"Image.jpg"];
 
180
        }
 
181
        
 
182
        [mailController setSubject:item.title];
 
183
        [mailController setMessageBody:body isHTML:isHTML];
 
184
                        
 
185
        [[SHK currentHelper] showViewController:mailController];
 
186
        
 
187
        return YES;
 
188
}
 
189
 
 
190
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
 
191
{
 
192
        [[SHK currentHelper] hideCurrentViewControllerAnimated:YES];
 
193
        
 
194
        switch (result) 
 
195
        {
 
196
                case MFMailComposeResultSent:
 
197
                        [self sendDidFinish];
 
198
                        break;
 
199
                case MFMailComposeResultSaved:
 
200
                        [self sendDidFinish];
 
201
                        break;
 
202
                case MFMailComposeResultCancelled:
 
203
                        [self sendDidCancel];
 
204
                        break;
 
205
                case MFMailComposeResultFailed:
 
206
                        [self sendDidFailWithError:nil];
 
207
                        break;
 
208
        }
 
209
        [self autorelease];
 
210
}
 
211
 
 
212
 
 
213
@end