1/*
2 * Copyright (C) 2011 Michal Hruby <michal.mhr@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 Michal Hruby <michal.mhr@gmail.com>
19 *
20 */
21
22namespace Synapse
23{
24  // There are two basic plugin interfaces - ItemProvider and ActionProvider
25  //
26  // Plugins implementing ItemProvider have the ability to add items as a result for particular search query.
27  // ActionProvider plugins on the other hand define actions that can be performed on items returned
28  // by other ItemProviders ie. a "Home directory" is an item that gets added by a particular ItemProvider plugin
29  // as a possible match when user searches for "home". ActionProvider will inspect this item, see that it's a file URI,
30  // and will add an action for the item, for example "Open".
31  //
32  // Please note that for example a "Pause" action (for a music player), is still implemented by an ItemProvider and
33  // it gets matched to the default "Run" action.
34  //
35  // Also note that a plugin can implement both of these interfaces if it's necessary.
36  public class HelloWorldPlugin : Object, Activatable, ItemProvider
37  {
38    // a mandatory property
39    public bool enabled { get; set; default = true; }
40
41    // this method is called when a plugin is enabled
42    // use it to initialize your plugin
43    public void activate ()
44    {
45    }
46
47    // this method is called when a plugin is disabled
48    // use it to free the resources you're using
49    public void deactivate ()
50    {
51    }
52
53    // register your plugin in the UI
54    static void register_plugin ()
55    {
56      PluginRegistry.get_default ().register_plugin (
57        typeof (HelloWorldPlugin),
58        _("Hello world"), // plugin title
59        _("An example plugin."), // description
60        "system-run", // icon name
61        register_plugin, // reference to this function
62        Environment.find_program_in_path ("ls") != null, // true if user's system has all required components which the plugin needs
63        _("ls is not installed") // error message
64      );
65    }
66
67    static construct
68    {
69      // register the plugin when the class is constructed
70      register_plugin ();
71    }
72
73    // an optional method to improve the speed of searches,
74    // if you return false here, the search method won't be called
75    // for this query
76    public bool handles_query (Query query)
77    {
78      // we will only search in the "Actions" category (that includes "All" as well)
79      return (QueryFlags.ACTIONS in query.query_type);
80    }
81
82    public async ResultSet? search (Query query) throws SearchError
83    {
84      if (query.query_string.has_prefix ("hello"))
85      {
86        // if the user searches for "hello" + anything, we'll add our result
87        var results = new ResultSet ();
88        results.add (new WorldMatch (), MatchScore.AVERAGE);
89
90        // make sure this method is called before returning any results
91        query.check_cancellable ();
92        return results;
93      }
94
95      // make sure this method is called before returning any results
96      query.check_cancellable ();
97      return null;
98    }
99
100    // define our Match object
101    private class WorldMatch : UnknownMatch
102    {
103      public WorldMatch ()
104      {
105        Object (title: "HelloWorld",
106                description: "Result from HelloWorldPlugin",
107                has_thumbnail: false, icon_name: "system-run");
108      }
109    }
110  }
111}
112