summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/shared-modules/AttachmentHelpers.jsm
blob: 971b25fa7766c946c233cc854d55b374600de313 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const EXPORTED_SYMBOLS = [
  "create_body_part",
  "create_deleted_attachment",
  "create_detached_attachment",
  "create_enclosure_attachment",
  "gMockFilePicker",
  "gMockFilePickReg",
  "select_attachments",
];

var { MockObjectReplacer } = ChromeUtils.import(
  "resource://testing-common/mozmill/MockObjectHelpers.jsm"
);

var gMockFilePickReg = new MockObjectReplacer(
  "@mozilla.org/filepicker;1",
  MockFilePickerConstructor
);

function MockFilePickerConstructor() {
  return gMockFilePicker;
}

var gMockFilePicker = {
  QueryInterface: ChromeUtils.generateQI(["nsIFilePicker"]),
  defaultExtension: "",
  filterIndex: null,
  displayDirectory: null,
  returnFiles: [],
  addToRecentDocs: false,

  get defaultString() {
    throw Components.Exception("", Cr.NS_ERROR_FAILURE);
  },

  get fileURL() {
    return null;
  },

  get file() {
    if (this.returnFiles.length >= 1) {
      return this.returnFiles[0];
    }
    return null;
  },

  get files() {
    let self = this;
    return {
      index: 0,
      QueryInterface: ChromeUtils.generateQI(["nsISimpleEnumerator"]),
      hasMoreElements() {
        return this.index < self.returnFiles.length;
      },
      getNext() {
        return self.returnFiles[this.index++];
      },
      [Symbol.iterator]() {
        return self.returnFiles.values();
      },
    };
  },

  init(aParent, aTitle, aMode) {},

  appendFilters(aFilterMask) {},

  appendFilter(aTitle, aFilter) {},

  open(aFilePickerShownCallback) {
    aFilePickerShownCallback.done(Ci.nsIFilePicker.returnOK);
  },

  set defaultString(aVal) {},
};

/**
 * Create a body part with attachments for the message generator
 *
 * @param body the text of the main body of the message
 * @param attachments an array of attachment objects (as strings)
 * @param boundary an optional string defining the boundary of the parts
 * @returns an object suitable for passing as the |bodyPart| for create_message
 */
function create_body_part(body, attachments, boundary) {
  if (!boundary) {
    boundary = "------------CHOPCHOP";
  }

  return {
    contentTypeHeaderValue: 'multipart/mixed;\r\n boundary="' + boundary + '"',
    toMessageString() {
      let str =
        "This is a multi-part message in MIME format.\r\n" +
        "--" +
        boundary +
        "\r\n" +
        "Content-Type: text/plain; charset=ISO-8859-1; " +
        "format=flowed\r\n" +
        "Content-Transfer-Encoding: 7bit\r\n\r\n" +
        body +
        "\r\n\r\n";

      for (let i = 0; i < attachments.length; i++) {
        str += "--" + boundary + "\r\n" + attachments[i] + "\r\n";
      }

      str += "--" + boundary + "--";
      return str;
    },
  };
}

function help_create_detached_deleted_attachment(filename, type) {
  return (
    "You deleted an attachment from this message. The original MIME " +
    "headers for the attachment were:\r\n" +
    "Content-Type: " +
    type +
    ";\r\n" +
    ' name="' +
    filename +
    '"\r\n' +
    "Content-Transfer-Encoding: 7bit\r\n" +
    "Content-Disposition: attachment;\r\n" +
    ' filename="' +
    filename +
    '"\r\n\r\n'
  );
}

/**
 * Create the raw data for a detached attachment
 *
 * @param file an nsIFile for the external file for the attachment
 * @param type the content type
 * @returns a string representing the attachment
 */
function create_detached_attachment(file, type) {
  let fileHandler = Services.io
    .getProtocolHandler("file")
    .QueryInterface(Ci.nsIFileProtocolHandler);
  let url = fileHandler.getURLSpecFromActualFile(file);
  let filename = file.leafName;

  let str =
    'Content-Type: text/plain;\r\n name="' +
    filename +
    '"\r\n' +
    'Content-Disposition: attachment; filename="' +
    filename +
    '"\r\n' +
    "X-Mozilla-External-Attachment-URL: " +
    url +
    "\r\n" +
    'X-Mozilla-Altered: AttachmentDetached; date="' +
    'Wed Oct 06 17:28:24 2010"\r\n\r\n';

  str += help_create_detached_deleted_attachment(filename, type);
  return str;
}

/**
 * Create the raw data for a deleted attachment
 *
 * @param filename the "original" filename
 * @param type the content type
 * @returns a string representing the attachment
 */
function create_deleted_attachment(filename, type) {
  let str =
    'Content-Type: text/x-moz-deleted; name="Deleted: ' +
    filename +
    '"\r\n' +
    "Content-Transfer-Encoding: 8bit\r\n" +
    'Content-Disposition: inline; filename="Deleted: ' +
    filename +
    '"\r\n' +
    'X-Mozilla-Altered: AttachmentDeleted; date="' +
    'Wed Oct 06 17:28:24 2010"\r\n\r\n';
  str += help_create_detached_deleted_attachment(filename, type);
  return str;
}

/**
 * Create the raw data for a feed enclosure attachment.
 *
 * @param filename the filename
 * @param type the content type
 * @param url the remote link url
 * @param size the optional size (use > 1 for real size)
 * @returns a string representing the attachment
 */
function create_enclosure_attachment(filename, type, url, size) {
  return (
    "Content-Type: " +
    type +
    '; name="' +
    filename +
    (size ? '"; size=' + size : '"') +
    "\r\n" +
    "X-Mozilla-External-Attachment-URL: " +
    url +
    "\r\n" +
    'Content-Disposition: attachment; filename="' +
    filename +
    '"\r\n\r\n' +
    "This MIME attachment is stored separately from the message."
  );
}

/**
 * A helper function that selects either one, or a continuous range
 * of items in the attachment list.
 *
 * @param aController a composer window controller
 * @param aIndexStart the index of the first item to select
 * @param aIndexEnd (optional) the index of the last item to select
 */
function select_attachments(aController, aIndexStart, aIndexEnd) {
  let bucket = aController.window.document.getElementById("attachmentBucket");
  bucket.clearSelection();

  if (aIndexEnd !== undefined) {
    let startItem = bucket.getItemAtIndex(aIndexStart);
    let endItem = bucket.getItemAtIndex(aIndexEnd);
    bucket.selectItemRange(startItem, endItem);
  } else {
    bucket.selectedIndex = aIndexStart;
  }

  bucket.focus();
  return [...bucket.selectedItems];
}