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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// This file was generated by gir (https://github.com/gtk-rs/gir @ fe7a6ff+)
// from gir-files (https://github.com/gtk-rs/gir-files @ b215ee8)
// DO NOT EDIT

use ContentEncoding;
use DataWrapper;
use DecryptFlags;
use DecryptResult;
use EncodingConstraint;
use Error;
use Object;
use OpenPGPData;
use SignatureList;
use VerifyFlags;
use ffi;
use glib::object::IsA;
use glib::translate::*;
use glib_ffi;
use gobject_ffi;
use std::mem;
use std::ptr;

glib_wrapper! {
    /// A leaf-node MIME part object.
    ///
    /// # Implements
    ///
    /// [`PartExt`](trait.PartExt.html), [`ObjectExt`](trait.ObjectExt.html)
    pub struct Part(Object<ffi::GMimePart, ffi::GMimePartClass>): Object;

    match fn {
        get_type => || ffi::g_mime_part_get_type(),
    }
}

impl Part {
    /// Creates a new MIME Part object with a default content-type of
    /// application/octet-stream.
    ///
    /// # Returns
    ///
    /// an empty MIME Part object with a default content-type of
    /// application/octet-stream.
    pub fn new() -> Part {
        unsafe {
            from_glib_full(ffi::g_mime_part_new())
        }
    }

    /// Creates a new MIME Part with a sepcified type.
    /// ## `type_`
    /// content-type string
    /// ## `subtype`
    /// content-subtype string
    ///
    /// # Returns
    ///
    /// an empty MIME Part object with the specified content-type.
    pub fn new_with_type(type_: &str, subtype: &str) -> Part {
        unsafe {
            from_glib_full(ffi::g_mime_part_new_with_type(type_.to_glib_none().0, subtype.to_glib_none().0))
        }
    }
}

impl Default for Part {
    fn default() -> Self {
        Self::new()
    }
}

/// Trait containing all `Part` methods.
///
/// # Implementors
///
/// [`ApplicationPkcs7Mime`](struct.ApplicationPkcs7Mime.html), [`MessagePartial`](struct.MessagePartial.html), [`Part`](struct.Part.html), [`TextPart`](struct.TextPart.html)
pub trait PartExt {
    /// Calculates the most efficient content encoding for the `self`
    /// given the `constraint`.
    /// ## `constraint`
    /// a `EncodingConstraint`
    ///
    /// # Returns
    ///
    /// the best content encoding for the specified mime part.
    fn get_best_content_encoding(&self, constraint: EncodingConstraint) -> ContentEncoding;

    /// Gets the internal data-wrapper of the specified mime part, or `None`
    /// on error.
    ///
    /// # Returns
    ///
    /// the data-wrapper for the mime part's
    /// contents.
    fn get_content(&self) -> Option<DataWrapper>;

    /// Gets the value of the Content-Description for the specified mime
    /// part if it exists or `None` otherwise.
    ///
    /// # Returns
    ///
    /// the content description for the specified mime part.
    fn get_content_description(&self) -> Option<String>;

    /// Gets the content encoding of the mime part.
    ///
    /// # Returns
    ///
    /// the content encoding for the specified mime part.
    fn get_content_encoding(&self) -> ContentEncoding;

    /// Gets the value of the Content-Location header if it exists, or
    /// `None` otherwise.
    ///
    /// # Returns
    ///
    /// the content location for the specified mime part.
    fn get_content_location(&self) -> Option<String>;

    /// Gets the md5sum contained in the Content-Md5 header of the
    /// specified mime part if it exists, or `None` otherwise.
    ///
    /// # Returns
    ///
    /// the content md5 for the specified mime part.
    fn get_content_md5(&self) -> Option<String>;

    /// Gets the filename of the specificed mime part, or `None` if the
    /// `self` does not have the filename or name parameter set.
    ///
    /// # Returns
    ///
    /// the filename of the specified `self` or `None` if
    /// neither of the parameters is set. If a file name is set, the
    /// returned string will be in UTF-8.
    fn get_filename(&self) -> Option<String>;

    /// Gets whether or not (and what type) of OpenPGP data is contained
    /// within the `Part`.
    ///
    /// # Returns
    ///
    /// a `OpenPGPData`.
    fn get_openpgp_data(&self) -> OpenPGPData;

    /// Determines whether or not the part is an attachment based on the
    /// value of the Content-Disposition header.
    ///
    /// # Returns
    ///
    /// `true` if the part is an attachment, otherwise `false`.
    fn is_attachment(&self) -> bool;

    /// Decrypts the content of the `self` and then replaces the content with
    /// the new, decrypted, content.
    /// ## `flags`
    /// a set of `DecryptFlags`
    /// ## `session_key`
    /// the session key to use or `None`
    ///
    /// # Returns
    ///
    /// a `DecryptResult` on success or `None` on error.
    fn openpgp_decrypt<'a, P: Into<Option<&'a str>>>(&self, flags: DecryptFlags, session_key: P) -> Result<Option<DecryptResult>, Error>;

    //fn openpgp_encrypt<'a, P: Into<Option<&'a str>>>(&self, sign: bool, userid: P, flags: EncryptFlags, recipients: /*Unknown conversion*//*Unimplemented*/PtrArray TypeId { ns_id: 0, id: 28 }) -> Result<(), Error>;

    /// Signs the content of the `self` and then replaces the content with
    /// the new, signed, content.
    /// ## `userid`
    /// the key id (or email address) to use for signing
    ///
    /// # Returns
    ///
    /// `true` on success or `false` on error.
    fn openpgp_sign(&self, userid: &str) -> Result<(), Error>;

    /// Verifies the OpenPGP signature of the `self` and then replaces the content
    /// with the original, raw, content.
    /// ## `flags`
    /// a set of `VerifyFlags`
    ///
    /// # Returns
    ///
    /// a `SignatureList` on success or `None` on error.
    fn openpgp_verify(&self, flags: VerifyFlags) -> Result<Option<SignatureList>, Error>;

    /// Sets the content on the mime part.
    /// ## `content`
    /// a `DataWrapper` content object
    fn set_content(&self, content: &DataWrapper);

    /// Set the content description for the specified mime part.
    /// ## `description`
    /// content description
    fn set_content_description(&self, description: &str);

    /// Set the content encoding for the specified mime part.
    /// ## `encoding`
    /// a `ContentEncoding`
    fn set_content_encoding(&self, encoding: ContentEncoding);

    /// Set the content location for the specified mime part.
    /// ## `content_location`
    /// content location
    fn set_content_location(&self, content_location: &str);

    /// Set the content md5 for the specified mime part.
    /// ## `content_md5`
    /// content md5 or `None` to generate the md5 digest.
    fn set_content_md5(&self, content_md5: &str);

    /// Sets the "filename" parameter on the Content-Disposition and also sets the
    /// "name" parameter on the Content-Type.
    ///
    /// Note: The `filename` string should be in UTF-8.
    /// ## `filename`
    /// the file name
    fn set_filename(&self, filename: &str);

    /// Sets whether or not (and what type) of OpenPGP data is contained
    /// within the `Part`.
    /// ## `data`
    /// a `OpenPGPData`
    fn set_openpgp_data(&self, data: OpenPGPData);

    /// Verify the content md5 for the specified mime part.
    ///
    /// # Returns
    ///
    /// `true` if the md5 is valid or `false` otherwise. Note: will
    /// return `false` if the mime part does not contain a Content-MD5.
    fn verify_content_md5(&self) -> bool;
}

impl<O: IsA<Part>> PartExt for O {
    fn get_best_content_encoding(&self, constraint: EncodingConstraint) -> ContentEncoding {
        unsafe {
            from_glib(ffi::g_mime_part_get_best_content_encoding(self.to_glib_none().0, constraint.to_glib()))
        }
    }

    fn get_content(&self) -> Option<DataWrapper> {
        unsafe {
            from_glib_none(ffi::g_mime_part_get_content(self.to_glib_none().0))
        }
    }

    fn get_content_description(&self) -> Option<String> {
        unsafe {
            from_glib_none(ffi::g_mime_part_get_content_description(self.to_glib_none().0))
        }
    }

    fn get_content_encoding(&self) -> ContentEncoding {
        unsafe {
            from_glib(ffi::g_mime_part_get_content_encoding(self.to_glib_none().0))
        }
    }

    fn get_content_location(&self) -> Option<String> {
        unsafe {
            from_glib_none(ffi::g_mime_part_get_content_location(self.to_glib_none().0))
        }
    }

    fn get_content_md5(&self) -> Option<String> {
        unsafe {
            from_glib_none(ffi::g_mime_part_get_content_md5(self.to_glib_none().0))
        }
    }

    fn get_filename(&self) -> Option<String> {
        unsafe {
            from_glib_none(ffi::g_mime_part_get_filename(self.to_glib_none().0))
        }
    }

    fn get_openpgp_data(&self) -> OpenPGPData {
        unsafe {
            from_glib(ffi::g_mime_part_get_openpgp_data(self.to_glib_none().0))
        }
    }

    fn is_attachment(&self) -> bool {
        unsafe {
            from_glib(ffi::g_mime_part_is_attachment(self.to_glib_none().0))
        }
    }

    fn openpgp_decrypt<'a, P: Into<Option<&'a str>>>(&self, flags: DecryptFlags, session_key: P) -> Result<Option<DecryptResult>, Error> {
        let session_key = session_key.into();
        let session_key = session_key.to_glib_none();
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_mime_part_openpgp_decrypt(self.to_glib_none().0, flags.to_glib(), session_key.0, &mut error);
            if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
        }
    }

    //fn openpgp_encrypt<'a, P: Into<Option<&'a str>>>(&self, sign: bool, userid: P, flags: EncryptFlags, recipients: /*Unknown conversion*//*Unimplemented*/PtrArray TypeId { ns_id: 0, id: 28 }) -> Result<(), Error> {
    //    unsafe { TODO: call ffi::g_mime_part_openpgp_encrypt() }
    //}

    fn openpgp_sign(&self, userid: &str) -> Result<(), Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = ffi::g_mime_part_openpgp_sign(self.to_glib_none().0, userid.to_glib_none().0, &mut error);
            if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) }
        }
    }

    fn openpgp_verify(&self, flags: VerifyFlags) -> Result<Option<SignatureList>, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_mime_part_openpgp_verify(self.to_glib_none().0, flags.to_glib(), &mut error);
            if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
        }
    }

    fn set_content(&self, content: &DataWrapper) {
        unsafe {
            ffi::g_mime_part_set_content(self.to_glib_none().0, content.to_glib_none().0);
        }
    }

    fn set_content_description(&self, description: &str) {
        unsafe {
            ffi::g_mime_part_set_content_description(self.to_glib_none().0, description.to_glib_none().0);
        }
    }

    fn set_content_encoding(&self, encoding: ContentEncoding) {
        unsafe {
            ffi::g_mime_part_set_content_encoding(self.to_glib_none().0, encoding.to_glib());
        }
    }

    fn set_content_location(&self, content_location: &str) {
        unsafe {
            ffi::g_mime_part_set_content_location(self.to_glib_none().0, content_location.to_glib_none().0);
        }
    }

    fn set_content_md5(&self, content_md5: &str) {
        unsafe {
            ffi::g_mime_part_set_content_md5(self.to_glib_none().0, content_md5.to_glib_none().0);
        }
    }

    fn set_filename(&self, filename: &str) {
        unsafe {
            ffi::g_mime_part_set_filename(self.to_glib_none().0, filename.to_glib_none().0);
        }
    }

    fn set_openpgp_data(&self, data: OpenPGPData) {
        unsafe {
            ffi::g_mime_part_set_openpgp_data(self.to_glib_none().0, data.to_glib());
        }
    }

    fn verify_content_md5(&self) -> bool {
        unsafe {
            from_glib(ffi::g_mime_part_verify_content_md5(self.to_glib_none().0))
        }
    }
}