2
* Copyright (C) 2014 Tom Beckmann <tomjonabc@gmail.com>
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.
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.
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.
18
* Authored by Tom Beckmann <tomjonabc@gmail.com>
24
public class ContractorPlugin : Object, Activatable, ActionProvider
26
public bool enabled { get; set; default = true; }
28
public void activate ()
32
public void deactivate ()
36
private class ContractorAction : BaseAction
38
public Granite.Services.Contract contract { get; construct; }
40
public ContractorAction (Granite.Services.Contract contract)
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 (),
49
public override void do_execute (Match? match, Match? target = null)
51
if (match.match_type == MatchType.GENERIC_URI && match is UriMatch) {
52
var uri_match = match as UriMatch;
54
contract.execute_with_file (File.new_for_uri (uri_match.uri));
58
public override bool valid_for_match (Match match)
60
switch (match.match_type) {
61
case MatchType.GENERIC_URI:
62
// TODO local files only
70
public ResultSet? find_for_match (ref Query q, Match match)
72
if (!(match is UriMatch))
76
q.query_string = q.query_string.strip ();
77
bool query_empty = q.query_string == "";
79
var results = new ResultSet ();
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));
89
int rel = actions[0].default_relevancy;
90
foreach (var action in actions)
91
results.add (action, rel);
93
var matchers = Query.get_matchers_for_query (q.query_string, 0,
94
RegexCompileFlags.CASELESS);
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);