Aucune description

EmailComposerProxyImpl.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. Copyright 2013-2016 appPlant UG
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. var proxy = require('de.appplant.cordova.plugin.email-composer.EmailComposerProxy'),
  19. impl = proxy.impl = {},
  20. WinMail = Windows.ApplicationModel.Email;
  21. /**
  22. * The Email with the containing properties.
  23. *
  24. * @param {Object} props
  25. * The email properties like subject or body
  26. * @return {Windows.ApplicationModel.Email.EmailMessage}
  27. * The resulting email draft
  28. */
  29. impl.getDraftWithProperties = function (props) {
  30. var me = this;
  31. return new WinJS.Promise(function (complete) {
  32. var mail = new WinMail.EmailMessage();
  33. // subject
  34. me.setSubject(props.subject, mail);
  35. // body
  36. me.setBody(props.body, props.isHtml, mail);
  37. // To recipients
  38. me.setRecipients(props.to, mail.to);
  39. // CC recipients
  40. me.setRecipients(props.cc, mail.cc);
  41. // BCC recipients
  42. me.setRecipients(props.bcc, mail.bcc);
  43. // attachments
  44. me.setAttachments(props.attachments, mail)
  45. .then(function () {
  46. complete(mail);
  47. });
  48. });
  49. };
  50. impl.getMailTo = function (props) {
  51. // The URI to launch
  52. var uriToLaunch = "mailto:" + props.to;
  53. var options = '';
  54. if (props.subject !== '') {
  55. options = options + '&subject=' + props.subject;
  56. }
  57. if (props.body !== '') {
  58. options = options + '&body=' + props.body;
  59. }
  60. if (props.cc !== '') {
  61. options = options + '&cc=' + props.cc;
  62. }
  63. if (props.bcc !== '') {
  64. options = options + '&bcc=' + props.bcc;
  65. }
  66. if (options !== '') {
  67. options = '?' + options.substring(1);
  68. uriToLaunch = uriToLaunch + options;
  69. }
  70. // Create a Uri object from a URI string
  71. var uri = new Windows.Foundation.Uri(uriToLaunch);
  72. return uri;
  73. };
  74. /**
  75. * Setter for the subject.
  76. *
  77. * @param {String} subject
  78. * The subject
  79. * @param {Windows.ApplicationModel.Email.EmailMessage} draft
  80. * The draft
  81. */
  82. impl.setSubject = function (subject, draft) {
  83. draft.subject = subject;
  84. };
  85. /**
  86. * Setter for the body.
  87. *
  88. * @param {String} body
  89. * The body
  90. * @param isHTML
  91. * Indicates the encoding
  92. * (HTML or plain text)
  93. * @param {Windows.ApplicationModel.Email.EmailMessage} draft
  94. * The draft
  95. */
  96. impl.setBody = function (body, isHTML, draft) {
  97. draft.body = body;
  98. };
  99. /**
  100. * Setter for the recipients.
  101. *
  102. * @param {String[]} recipients
  103. * List of mail addresses
  104. * @param {Windows.ApplicationModel.Email.EmailMessage} draft
  105. * The draft.to / *.cc / *.bcc
  106. */
  107. impl.setRecipients = function (recipients, draft) {
  108. recipients.forEach(function (address) {
  109. draft.push(new WinMail.EmailRecipient(address));
  110. });
  111. };
  112. /**
  113. * Setter for the attachments.
  114. *
  115. * @param {String[]} attachments
  116. * List of URIs
  117. * @param {Windows.ApplicationModel.Email.EmailMessage} draft
  118. * The draft
  119. */
  120. impl.setAttachments = function (attachments, draft) {
  121. var promises = [], me = this;
  122. return new WinJS.Promise(function (complete) {
  123. attachments.forEach(function (path) {
  124. promises.push(me.getUriForPath(path));
  125. });
  126. WinJS.Promise.thenEach(promises, function (uri) {
  127. draft.attachments.push(
  128. new WinMail.EmailAttachment(
  129. uri.path.split('/').reverse()[0],
  130. Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(uri)
  131. )
  132. );
  133. }).done(complete);
  134. });
  135. };
  136. /**
  137. * The URI for an attachment path.
  138. *
  139. * @param {String} path
  140. * The given path to the attachment
  141. * @return
  142. * The URI pointing to the given path
  143. */
  144. impl.getUriForPath = function (path) {
  145. var me = this;
  146. return new WinJS.Promise(function (complete) {
  147. if (path.match(/^res:/)) {
  148. complete(me.getUriForResourcePath(path));
  149. } else if (path.match(/^file:\/{3}/)) {
  150. complete(me.getUriForAbsolutePath(path));
  151. } else if (path.match(/^file:/)) {
  152. complete(me.getUriForAssetPath(path));
  153. } else if (path.match(/^base64:/)) {
  154. me.getUriFromBase64(path).then(complete);
  155. } else {
  156. complete(new Windows.Foundation.Uri(path));
  157. }
  158. });
  159. };
  160. /**
  161. * The URI for a file.
  162. *
  163. * @param {String} path
  164. * The given absolute path
  165. * @return
  166. * The URI pointing to the given path
  167. */
  168. impl.getUriForAbsolutePath = function (path) {
  169. return new Windows.Foundation.Uri(path);
  170. };
  171. /**
  172. * The URI for an asset.
  173. *
  174. * @param {String} path
  175. * The given asset path
  176. * @return
  177. * The URI pointing to the given path
  178. */
  179. impl.getUriForAssetPath = function (path) {
  180. var resPath = path.replace('file:/', '/www');
  181. return this.getUriForPathUtil(resPath);
  182. };
  183. /**
  184. * The URI for a resource.
  185. *
  186. * @param {String} path
  187. * The given relative path
  188. * @return
  189. * The URI pointing to the given path
  190. */
  191. impl.getUriForResourcePath = function (path) {
  192. var resPath = path.replace('res:/', '/images');
  193. return this.getUriForPathUtil(resPath);
  194. };
  195. /**
  196. * The URI for a path.
  197. *
  198. * @param {String} resPath
  199. * The given relative path
  200. * @return
  201. * The URI pointing to the given path
  202. */
  203. impl.getUriForPathUtil = function (resPath) {
  204. var rawUri = 'ms-appx:' + '//' + resPath;
  205. return new Windows.Foundation.Uri(rawUri);
  206. };
  207. /**
  208. * The URI for a base64 encoded content.
  209. *
  210. * @param {String} content
  211. * The given base64 encoded content
  212. * @return
  213. * The URI including the given content
  214. */
  215. impl.getUriFromBase64 = function (content) {
  216. return new WinJS.Promise(function (complete) {
  217. var match = content.match(/^base64:([^\/]+)\/\/(.*)/),
  218. base64 = match[2],
  219. name = match[1],
  220. buffer = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(base64),
  221. rwplus = Windows.Storage.CreationCollisionOption.openIfExists,
  222. folder = Windows.Storage.ApplicationData.current.temporaryFolder,
  223. uri = new Windows.Foundation.Uri('ms-appdata:///temp/' + name);
  224. folder.createFileAsync(name, rwplus).done(function (file) {
  225. Windows.Storage.FileIO.writeBufferAsync(file, buffer).then(function () {
  226. complete(uri);
  227. });
  228. });
  229. });
  230. };