Merge lp:~elementary-apps/slingshot/synapse-calculator into lp:~elementary-pantheon/slingshot/trunk

Proposed by Tom Beckmann
Status: Merged
Approved by: Cody Garver
Approved revision: 423
Merged at revision: 423
Proposed branch: lp:~elementary-apps/slingshot/synapse-calculator
Merge into: lp:~elementary-pantheon/slingshot/trunk
Diff against target: 179 lines (+153/-0)
3 files modified
lib/synapse-plugins/CMakeLists.txt (+1/-0)
lib/synapse-plugins/calculator-plugin.vala (+151/-0)
src/Backend/SynapseSearch.vala (+1/-0)
To merge this branch: bzr merge lp:~elementary-apps/slingshot/synapse-calculator
Reviewer Review Type Date Requested Status
elementary Pantheon team Pending
Review via email: mp+224930@code.launchpad.net

Commit message

Add Synapse calculator plugin to fix bug #1335526

Description of the change

adds the calculator plugin back in, as proposed in https://bugs.launchpad.net/slingshot/+bug/1335526

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/synapse-plugins/CMakeLists.txt'
--- lib/synapse-plugins/CMakeLists.txt 2014-06-13 21:09:06 +0000
+++ lib/synapse-plugins/CMakeLists.txt 2014-06-29 14:57:08 +0000
@@ -11,6 +11,7 @@
11pkg_check_modules(PLUGINS_DEPS REQUIRED ${PLUGINS_PKG})11pkg_check_modules(PLUGINS_DEPS REQUIRED ${PLUGINS_PKG})
1212
13set(PLUGINS_SOURCE13set(PLUGINS_SOURCE
14 calculator-plugin.vala
14 command-plugin.vala15 command-plugin.vala
15 desktop-file-plugin.vala16 desktop-file-plugin.vala
16)17)
1718
=== added file 'lib/synapse-plugins/calculator-plugin.vala'
--- lib/synapse-plugins/calculator-plugin.vala 1970-01-01 00:00:00 +0000
+++ lib/synapse-plugins/calculator-plugin.vala 2014-06-29 14:57:08 +0000
@@ -0,0 +1,151 @@
1/*
2 * Copyright (C) 2010 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 Magnus Kulke <mkulke@gmail.com>
19 *
20 */
21
22namespace Synapse
23{
24 public class CalculatorPlugin: Object, Activatable, ItemProvider
25 {
26 public bool enabled { get; set; default = true; }
27
28 public void activate ()
29 {
30
31 }
32
33 public void deactivate ()
34 {
35
36 }
37
38 private class Result: Object, Match
39 {
40 // from Match interface
41 public string title { get; construct set; }
42 public string description { get; set; }
43 public string icon_name { get; construct set; }
44 public bool has_thumbnail { get; construct set; }
45 public string thumbnail_path { get; construct set; }
46 public MatchType match_type { get; construct set; }
47
48 public int default_relevancy { get; set; default = 0; }
49
50 public Result (double result, string match_string)
51 {
52 Object (match_type: MatchType.TEXT,
53 title: "%g".printf (result),
54 description: "%s = %g".printf (match_string, result),
55 has_thumbnail: false, icon_name: "accessories-calculator");
56 }
57 }
58
59 static void register_plugin ()
60 {
61 DataSink.PluginRegistry.get_default ().register_plugin (
62 typeof (CalculatorPlugin),
63 _ ("Calculator"),
64 _ ("Calculate basic expressions."),
65 "accessories-calculator",
66 register_plugin,
67 Environment.find_program_in_path ("bc") != null,
68 _ ("bc is not installed")
69 );
70 }
71
72 static construct
73 {
74 register_plugin ();
75 }
76
77 private Regex regex;
78
79 construct
80 {
81 /* The regex describes a string which *resembles* a mathematical expression. It does not
82 check for pairs of parantheses to be used correctly and only whitespace-stripped strings
83 will match. Basically it matches strings of the form:
84 "paratheses_open* number (operator paratheses_open* number paratheses_close*)+"
85 */
86 try
87 {
88 regex = new Regex ("^\\(*(-?\\d+([.,]\\d+)?)([*/+-^]\\(*(-?\\d+([.,]\\d+)?)\\)*)+$",
89 RegexCompileFlags.OPTIMIZE);
90 } catch (Error e) {
91 Utils.Logger.error (this, "Error creating regexp.");
92 }
93 }
94
95 public bool handles_query (Query query)
96 {
97 return (QueryFlags.ACTIONS in query.query_type);
98 }
99
100 public async ResultSet? search (Query query) throws SearchError
101 {
102 string input = query.query_string.replace (" ", "").replace (",", ".");
103 bool matched = regex.match (input);
104 if (!matched && input.length > 1)
105 {
106 input = input[0 : input.length - 1];
107 matched = regex.match (input);
108 }
109 if (matched)
110 {
111 Pid pid;
112 int read_fd, write_fd;
113 string[] argv = {"bc", "-l"};
114 string? solution = null;
115
116 try
117 {
118 Process.spawn_async_with_pipes (null, argv, null,
119 SpawnFlags.SEARCH_PATH,
120 null, out pid, out write_fd, out read_fd);
121 UnixInputStream read_stream = new UnixInputStream (read_fd, true);
122 DataInputStream bc_output = new DataInputStream (read_stream);
123
124 UnixOutputStream write_stream = new UnixOutputStream (write_fd, true);
125 DataOutputStream bc_input = new DataOutputStream (write_stream);
126
127 bc_input.put_string (input + "\n", query.cancellable);
128 yield bc_input.close_async (Priority.DEFAULT, query.cancellable);
129 solution = yield bc_output.read_line_async (Priority.DEFAULT_IDLE, query.cancellable);
130
131 if (solution != null)
132 {
133 double d = double.parse (solution);
134 Result result = new Result (d, query.query_string);
135 ResultSet results = new ResultSet ();
136 results.add (result, Match.Score.AVERAGE);
137 query.check_cancellable ();
138 return results;
139 }
140 }
141 catch (Error err)
142 {
143 if (!query.is_cancelled ()) warning ("%s", err.message);
144 }
145 }
146
147 query.check_cancellable ();
148 return null;
149 }
150 }
151}
0152
=== modified file 'src/Backend/SynapseSearch.vala'
--- src/Backend/SynapseSearch.vala 2014-06-12 09:14:57 +0000
+++ src/Backend/SynapseSearch.vala 2014-06-29 14:57:08 +0000
@@ -21,6 +21,7 @@
21 public class SynapseSearch : Object {21 public class SynapseSearch : Object {
2222
23 private static Type[] plugins = {23 private static Type[] plugins = {
24 typeof (Synapse.CalculatorPlugin),
24 typeof (Synapse.CommandPlugin),25 typeof (Synapse.CommandPlugin),
25 typeof (Synapse.DesktopFilePlugin)26 typeof (Synapse.DesktopFilePlugin)
26 };27 };

Subscribers

People subscribed via source and target branches