1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/*
Copyright 2013-2015 appPlant UG
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
/**
* Verifies if sending emails is supported on the device.
*
* @param {Function} success
* Success callback function
* @param {Function} error
* Error callback function
* @param {Array} args
* Interface arguments
*/
exports.isAvailable = function (success, error, args) {
success(true);
};
/**
* Displays the email composer pre-filled with data.
*
* @param {Function} success
* Success callback function
* @param {Function} error
* Error callback function
* @param {Array} args
* Interface arguments
*/
exports.open = function (success, error, args) {
var props = args[0],
email = exports.getDraftWithProperties(props);
Windows.ApplicationModel.Email.EmailManager
.showComposeNewEmailAsync(email)
.done(success());
};
/**
* The Email with the containing properties.
*
* @param {Object} props
* The email properties like subject or body
* @return {Windows.ApplicationModel.Email.EmailMessage}
* The resulting email draft
*/
exports.getDraftWithProperties = function (props) {
var mail = new Windows.ApplicationModel.Email.EmailMessage();
// subject
exports.setSubject(props.subject, mail);
// body
exports.setBody(props.body, props.isHtml, mail);
// To recipients
exports.setRecipients(props.to, mail);
// CC recipients
exports.setCcRecipients(props.cc, mail);
// BCC recipients
exports.setBccRecipients(props.bcc, mail);
// attachments
exports.setAttachments(props.attachments, mail);
return mail;
};
/**
* Setter for the subject.
*
* @param {String} subject
* The subject
* @param {Windows.ApplicationModel.Email.EmailMessage} draft
* The draft
*/
exports.setSubject = function (subject, draft) {
draft.subject = subject;
};
/**
* Setter for the body.
*
* @param {String} body
* The body
* @param isHTML
* Indicates the encoding
* (HTML or plain text)
* @param {Windows.ApplicationModel.Email.EmailMessage} draft
* The draft
*/
exports.setBody = function (body, isHTML, draft) {
draft.body = body;
};
/**
* Setter for the recipients.
*
* @param {String[]} recipients
* List of mail addresses
* @param {Windows.ApplicationModel.Email.EmailMessage} draft
* The draft
*/
exports.setRecipients = function (recipients, draft) {
recipients.forEach(function (address) {
draft.to.push(
new Windows.ApplicationModel.Email.EmailRecipient(address));
});
};
/**
* Setter for the cc recipients.
*
* @param {String[]} recipients
* List of mail addresses
* @param {Windows.ApplicationModel.Email.EmailMessage} draft
* The draft
*/
exports.setCcRecipients = function (recipients, draft) {
recipients.forEach(function (address) {
draft.cc.push(
new Windows.ApplicationModel.Email.EmailRecipient(address));
});
};
/**
* Setter for the bcc recipients.
*
* @param {String[]} recipients
* List of mail addresses
* @param {Windows.ApplicationModel.Email.EmailMessage} draft
* The draft
*/
exports.setBccRecipients = function (recipients, draft) {
recipients.forEach(function (address) {
draft.bcc.push(
new Windows.ApplicationModel.Email.EmailRecipient(address));
});
};
/**
* Setter for the attachments.
*
* @param {String[]} attachments
* List of URIs
* @param {Windows.ApplicationModel.Email.EmailMessage} draft
* The draft
*/
exports.setAttachments = function (attachments, draft) {
attachments.forEach(function (path) {
var uri = exports.getUriForPath(path),
name = uri.path.split('/').reverse()[0],
stream = Windows.Storage.Streams.RandomAccessStreamReference
.createFromUri(uri);
draft.attachments.push(
new Windows.ApplicationModel.Email.
EmailAttachment(name, stream)
);
});
};
/**
* The URI for an attachment path.
*
* @param {String} path
* The given path to the attachment
*
* @return
* The URI pointing to the given path
*/
exports.getUriForPath = function (path) {
if (path.match(/^res:/)) {
return exports.getUriForResourcePath(path);
} else if (path.match(/^file:\/{3}/)) {
return exports.getUriForAbsolutePath(path);
} else if (path.match(/^file:/)) {
return exports.getUriForAssetPath(path);
} else if (path.match(/^base64:/)) {
return exports.getUriForBase64Content(path);
}
return new Windows.Foundation.Uri(path);
};
/**
* The URI for a file.
*
* @param {String} path
* The given absolute path
*
* @return
* The URI pointing to the given path
*/
exports.getUriForAbsolutePath = function (path) {
return new Windows.Foundation.Uri(path);
};
/**
* The URI for an asset.
*
* @param {String} path
* The given asset path
*
* @return
* The URI pointing to the given path
*/
exports.getUriForAssetPath = function (path) {
var host = document.location.host,
protocol = document.location.protocol,
resPath = path.replace('file:/', '/www'),
rawUri = protocol + '//' + host + resPath;
return new Windows.Foundation.Uri(rawUri);
};
/**
* The URI for a resource.
*
* @param {String} path
* The given relative path
*
* @return
* The URI pointing to the given path
*/
exports.getUriForResourcePath = function (path) {
var host = document.location.host,
protocol = document.location.protocol,
resPath = path.replace('res:/', '/images'),
rawUri = protocol + '//' + host + resPath;
return new Windows.Foundation.Uri(rawUri);
};
/**
* The URI for a base64 encoded content.
*
* @param {String} content
* The given base64 encoded content
*
* @return
* The URI including the given content
*/
exports.getUriForBase64Content = function (content) {
var match = content.match(/^base64:([^\/]+)\/\/(.*)/),
base64 = match[2],
name = match[1],
buffer = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(base64),
rwplus = Windows.Storage.CreationCollisionOption.openIfExists,
folder = Windows.Storage.ApplicationData.current.temporaryFolder,
uri = new Windows.Foundation.Uri('ms-appdata:///temp/' + name);
folder.createFileAsync(name, rwplus).done(function (file) {
Windows.Storage.FileIO.writeBufferAsync(file, buffer);
});
return uri;
};
require('cordova/exec/proxy').add('EmailComposer', exports);
|