~elementary-pantheon/contractor/master

« back to all changes in this revision

Viewing changes to src/DBusService.vala

  • Committer: RabbitBot
  • Author(s): Florian R. A. Angermeier
  • Date: 2015-09-21 13:31:07 UTC
  • mfrom: (136.1.11)
  • Revision ID: git-v1:4a5e95794024146bc89ee39053530c11e4977103
Implement filter functions based on file size:
* Get the max file size (int64, size in bytes) from a .contract file (optional key MaxFileSize)
* Add methods to the D-Bus service:
  - get_contracts_by_file_size
  - get_contracts_by_mime_and_file_size
  - get_contracts_by_mimelist_and_file_size

Add documentation

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
namespace Contractor {
26
26
    [DBus (name = "org.elementary.ContractorError")]
 
27
    /**
 
28
     * Errors specific to the Contractor D-Bus service.
 
29
     */
27
30
    public errordomain ContractorError {
 
31
        /**
 
32
         * Error if no MIME type was passed where mandatory.
 
33
         */
28
34
        NO_MIMETYPES_GIVEN
29
35
    }
30
36
 
 
37
    /**
 
38
     * A D-Bus service handling requests for Contracts defined by .contract
 
39
     * files.
 
40
     */
31
41
    [DBus (name = "org.elementary.Contractor")]
32
42
    public class DBusService : Object {
 
43
        /**
 
44
         * Signal which gets sent when Contracts change.
 
45
         */
33
46
        public signal void contracts_changed ();
34
47
 
 
48
        /**
 
49
         * Source from where .Contracts are loaded.
 
50
         */
35
51
        private ContractSource contract_source;
36
52
 
 
53
        /**
 
54
         * Constructor to create a DBusService object and setup a
 
55
         * ContractSource.
 
56
         */
37
57
        public DBusService () {
38
58
            contract_source = new ContractSource ();
39
59
            contract_source.changed.connect (() => contracts_changed ());
40
60
        }
41
61
 
 
62
        /**
 
63
         * This method gets an array of GenericContracts filtered by a
 
64
         * MIME type.
 
65
         *
 
66
         * @param mime_type a MIME type string, e.g. text, image
 
67
         *
 
68
         * @return an array of GenericContracts
 
69
         */
42
70
        public GenericContract[] get_contracts_by_mime (string mime_type) throws Error {
43
71
            string[] mime_types = { mime_type };
44
72
            return get_contracts_by_mimelist (mime_types);
45
73
        }
46
74
 
 
75
        /**
 
76
         * This method gets an array of GenericContracts filtered by an array of
 
77
         * MIME types.
 
78
         *
 
79
         * @param mime_types an array of MIME type strings, e.g. text, image
 
80
         *
 
81
         * @return an array of GenericContracts
 
82
         */
47
83
        public GenericContract[] get_contracts_by_mimelist (string[] mime_types) throws Error {
48
84
            var all_contracts = contract_source.get_contracts ();
49
85
            var contracts = ContractMatcher.get_contracts_for_types (mime_types, all_contracts);
50
86
            return convert_to_generic_contracts (contracts);
51
87
        }
52
88
 
 
89
        /**
 
90
         * This method gets an array of GenericContracts filtered by the file
 
91
         * size in bytes.
 
92
         *
 
93
         * @param file_size the file size in bytes, e.g. from FileInfo.get_size ()
 
94
         *
 
95
         * @return an array of GenericContracts
 
96
         */
 
97
        public GenericContract[] get_contracts_by_file_size (int64 file_size) throws Error {
 
98
            var all_contracts = contract_source.get_contracts ();
 
99
            var contracts = ContractMatcher.get_contracts_for_file_size (file_size, all_contracts);
 
100
            return convert_to_generic_contracts (contracts);
 
101
        }
 
102
 
 
103
        /**
 
104
         * This method gets an array of GenericContracts filtered by a MIME type
 
105
         * and the file size in bytes.
 
106
         *
 
107
         * @param mime_type a MIME type string, e.g. text, image
 
108
         * @param file_size the file size in bytes, e.g. from FileInfo.get_size ()
 
109
         *
 
110
         * @return an array of GenericContracts
 
111
         */
 
112
        public GenericContract[] get_contracts_by_mime_and_file_size (string mime_type, int64 file_size) throws Error {
 
113
            string[] mime_types = { mime_type };
 
114
            return get_contracts_by_mimelist_and_file_size (mime_types, file_size);
 
115
        }
 
116
 
 
117
        /**
 
118
         * This method gets an array of GenericContracts filtered by an array of
 
119
         * MIME types and the file size in bytes.
 
120
         *
 
121
         * The file size should probably be the sum of all files file size.
 
122
         *
 
123
         * @param mime_types an array of MIME type strings, e.g. text, image
 
124
         * @param file_size the file size in bytes, e.g. from FileInfo.get_size ()
 
125
         *
 
126
         * @return an array of GenericContracts
 
127
         */
 
128
        public GenericContract[] get_contracts_by_mimelist_and_file_size (string[] mime_types, int64 file_size) throws Error {
 
129
            var all_contracts = contract_source.get_contracts ();
 
130
            var contracts = ContractMatcher.get_contracts_for_types_and_file_size (mime_types, file_size, all_contracts);
 
131
            return convert_to_generic_contracts (contracts);
 
132
        }
 
133
 
53
134
        public void execute_with_uri (string id, string uri) throws Error {
54
135
            string[] uris = { uri };
55
136
            execute_with_uri_list (id, uris);
60
141
            contract.launch_uris (uris);
61
142
        }
62
143
 
 
144
        /**
 
145
         * This method gets an array of GenericContracts containing all
 
146
         * contracts that exist.
 
147
         *
 
148
         * @return an array of GenericContracts containing all contracts
 
149
         */
63
150
        public GenericContract[] list_all_contracts () {
64
151
            var contracts = contract_source.get_contracts ();
65
152
            return convert_to_generic_contracts (contracts);
66
153
        }
67
154
 
 
155
        /**
 
156
         * Converts a Collection of Contracts into an array of
 
157
         * GenericContracts.
 
158
         *
 
159
         * @return an array of GenericContracts
 
160
         */
68
161
        private static GenericContract[] convert_to_generic_contracts (Gee.Collection<Contract> contracts) {
69
162
            var generic_contracts = new GenericContract[0];
70
163
 
71
 
            foreach (var contract in contracts)
 
164
            foreach (var contract in contracts) {
72
165
                generic_contracts += contract.get_generic ();
 
166
            }
73
167
 
74
168
            return generic_contracts;
75
169
        }