~elementary-apps/slingshot/synapse-desktop-only

« back to all changes in this revision

Viewing changes to lib/synapse-plugins/contractor-plugin.vala

  • Committer: Tom Beckmann
  • Date: 2014-06-12 09:14:57 UTC
  • Revision ID: tomjonabc@gmail.com-20140612091457-1spb2qtytxybnvkg
Remove all plugins but desktop-file and command one, fix code style

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2014 Tom Beckmann <tomjonabc@gmail.com>
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 2 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, write to the Free Software
16
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
17
 
 *
18
 
 * Authored by Tom Beckmann <tomjonabc@gmail.com>
19
 
 *
20
 
 */
21
 
 
22
 
namespace Synapse
23
 
{
24
 
        public class ContractorPlugin : Object, Activatable, ActionProvider
25
 
        {
26
 
                public bool enabled { get; set; default = true; }
27
 
 
28
 
                public void activate ()
29
 
                {
30
 
                }
31
 
 
32
 
                public void deactivate ()
33
 
                {
34
 
                }
35
 
 
36
 
                private class ContractorAction : BaseAction
37
 
                {
38
 
                        public Granite.Services.Contract contract { get; construct; }
39
 
 
40
 
                        public ContractorAction (Granite.Services.Contract contract)
41
 
                        {
42
 
                                Object (title: contract.get_display_name (),
43
 
                                                description: contract.get_description (),
44
 
                                                match_type: MatchType.ACTION,
45
 
                                                icon_name: contract.get_icon ().to_string (),
46
 
                                                contract: contract);
47
 
                        }
48
 
 
49
 
                        public override void do_execute (Match? match, Match? target = null)
50
 
                        {
51
 
                                if (match.match_type == MatchType.GENERIC_URI && match is UriMatch) {
52
 
                                        var uri_match = match as UriMatch;
53
 
 
54
 
                                        contract.execute_with_file (File.new_for_uri (uri_match.uri));
55
 
                                }
56
 
                        }
57
 
 
58
 
                        public override bool valid_for_match (Match match)
59
 
                        {
60
 
                                switch (match.match_type) {
61
 
                                        case MatchType.GENERIC_URI:
62
 
                                                // TODO local files only
63
 
                                                return true;
64
 
                                        default:
65
 
                                                return false;
66
 
                                }
67
 
                        }
68
 
                }
69
 
 
70
 
                public ResultSet? find_for_match (ref Query q, Match match)
71
 
                {
72
 
                        if (!(match is UriMatch))
73
 
                                return null;
74
 
 
75
 
                        // strip query
76
 
                        q.query_string = q.query_string.strip ();
77
 
                        bool query_empty = q.query_string == "";
78
 
 
79
 
                        var results = new ResultSet ();
80
 
 
81
 
                        var file = File.new_for_uri ((match as UriMatch).uri);
82
 
                        var contracts = Granite.Services.ContractorProxy.get_contracts_for_file (file);
83
 
                        var actions = new Gee.LinkedList<ContractorAction> ();
84
 
                        foreach (var contract in contracts) {
85
 
                                actions.add (new ContractorAction (contract));
86
 
                        }
87
 
 
88
 
                        if (query_empty) {
89
 
                                int rel = actions[0].default_relevancy;
90
 
                                foreach (var action in actions)
91
 
                                        results.add (action, rel);
92
 
                        } else {
93
 
                                var matchers = Query.get_matchers_for_query (q.query_string, 0,
94
 
                                        RegexCompileFlags.CASELESS);
95
 
 
96
 
                                foreach (var action in actions) {
97
 
                                        foreach (var matcher in matchers) {
98
 
                                                if (matcher.key.match (action.title)) {
99
 
                                                        results.add (action, matcher.value);
100
 
                                                        break;
101
 
                                                }
102
 
                                        }
103
 
                                }
104
 
                        }
105
 
 
106
 
                        return results;
107
 
                }
108
 
        }
109
 
}
110