~elementary-pantheon/contractor/master

113.1.2 by Victor
- Remove god classes and introduce smaller classes with fewer responsibilities.
1
/*
140.1.1 by Leonardo Lemos
Bump copyright years
2
 * Copyright (C) 2013-2017 elementary Developers
113.1.2 by Victor
- Remove god classes and introduce smaller classes with fewer responsibilities.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
public class Contractor.MimeTypeManager : Object {
19
    private string[] values;
20
    private bool is_conditional = false;
21
113.1.14 by Victor
Check if mandatory contract key values are valid while loading a contract.
22
    public MimeTypeManager (string[] mimetypes) throws Error {
23
        if ("!" in mimetypes[0]) { // See if we have a conditional mimetype
113.1.8 by Victor
No need to guard the code against multiple conditional mimetypes since support for them has landed already.
24
            is_conditional = true;
113.1.14 by Victor
Check if mandatory contract key values are valid while loading a contract.
25
            mimetypes[0] = mimetypes[0].replace ("!", ""); // remove the '!'
113.1.2 by Victor
- Remove god classes and introduce smaller classes with fewer responsibilities.
26
        }
113.1.6 by Victor
[MimeTypeManager] Don't be fooled when an empty string is passed as a mimetype.
27
113.4.6 by Victor
Code refactoring
28
        values = String.clean_array (mimetypes);
113.1.14 by Victor
Check if mandatory contract key values are valid while loading a contract.
29
136.1.6 by Florian R. A. Angermeier
Code style fixes. http://elementary.io/de/docs/code/reference#indentation
30
        if (values.length == 0) {
113.1.14 by Victor
Check if mandatory contract key values are valid while loading a contract.
31
            throw new KeyFileError.INVALID_VALUE ("No values specified for MimeType.");
136.1.6 by Florian R. A. Angermeier
Code style fixes. http://elementary.io/de/docs/code/reference#indentation
32
        }
113.1.2 by Victor
- Remove god classes and introduce smaller classes with fewer responsibilities.
33
    }
34
35
    public bool is_type_supported (string mime_type) {
113.1.6 by Victor
[MimeTypeManager] Don't be fooled when an empty string is passed as a mimetype.
36
        bool has_mimetype = contains_mimetype (mime_type);
37
        return is_conditional ? !has_mimetype : has_mimetype;
38
    }
39
40
    private bool contains_mimetype (string mime_type) {
113.1.2 by Victor
- Remove god classes and introduce smaller classes with fewer responsibilities.
41
        foreach (string local_mime_type in values) {
136.1.6 by Florian R. A. Angermeier
Code style fixes. http://elementary.io/de/docs/code/reference#indentation
42
            if (compare (mime_type, local_mime_type)) {
113.1.2 by Victor
- Remove god classes and introduce smaller classes with fewer responsibilities.
43
                return true;
136.1.6 by Florian R. A. Angermeier
Code style fixes. http://elementary.io/de/docs/code/reference#indentation
44
            }
113.1.2 by Victor
- Remove god classes and introduce smaller classes with fewer responsibilities.
45
        }
46
47
        return false;
48
    }
49
113.1.14 by Victor
Check if mandatory contract key values are valid while loading a contract.
50
    private static bool compare (string mime_type, string ref_mime_type) {
128.1.2 by Victor
When matching mimetypes that are incomplete, only compare strings from the beginning.
51
        return mime_type.has_prefix (ref_mime_type)
113.1.3 by Victor Eduardo
Coding style fix
52
            || ContentType.equals (mime_type, ref_mime_type)
53
            || ContentType.is_a (mime_type, ref_mime_type);
113.1.2 by Victor
- Remove god classes and introduce smaller classes with fewer responsibilities.
54
    }
113.1.10 by Victor
Improve mimetype list validator
55
}