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
1=== modified file 'lib/synapse-plugins/CMakeLists.txt'
2--- lib/synapse-plugins/CMakeLists.txt 2014-06-13 21:09:06 +0000
3+++ lib/synapse-plugins/CMakeLists.txt 2014-06-29 14:57:08 +0000
4@@ -11,6 +11,7 @@
5 pkg_check_modules(PLUGINS_DEPS REQUIRED ${PLUGINS_PKG})
6
7 set(PLUGINS_SOURCE
8+ calculator-plugin.vala
9 command-plugin.vala
10 desktop-file-plugin.vala
11 )
12
13=== added file 'lib/synapse-plugins/calculator-plugin.vala'
14--- lib/synapse-plugins/calculator-plugin.vala 1970-01-01 00:00:00 +0000
15+++ lib/synapse-plugins/calculator-plugin.vala 2014-06-29 14:57:08 +0000
16@@ -0,0 +1,151 @@
17+/*
18+ * Copyright (C) 2010 Michal Hruby <michal.mhr@gmail.com>
19+ *
20+ * This program is free software; you can redistribute it and/or modify
21+ * it under the terms of the GNU General Public License as published by
22+ * the Free Software Foundation; either version 2 of the License, or
23+ * (at your option) any later version.
24+ *
25+ * This program is distributed in the hope that it will be useful,
26+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28+ * GNU General Public License for more details.
29+ *
30+ * You should have received a copy of the GNU General Public License
31+ * along with this program; if not, write to the Free Software
32+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
33+ *
34+ * Authored by Magnus Kulke <mkulke@gmail.com>
35+ *
36+ */
37+
38+namespace Synapse
39+{
40+ public class CalculatorPlugin: Object, Activatable, ItemProvider
41+ {
42+ public bool enabled { get; set; default = true; }
43+
44+ public void activate ()
45+ {
46+
47+ }
48+
49+ public void deactivate ()
50+ {
51+
52+ }
53+
54+ private class Result: Object, Match
55+ {
56+ // from Match interface
57+ public string title { get; construct set; }
58+ public string description { get; set; }
59+ public string icon_name { get; construct set; }
60+ public bool has_thumbnail { get; construct set; }
61+ public string thumbnail_path { get; construct set; }
62+ public MatchType match_type { get; construct set; }
63+
64+ public int default_relevancy { get; set; default = 0; }
65+
66+ public Result (double result, string match_string)
67+ {
68+ Object (match_type: MatchType.TEXT,
69+ title: "%g".printf (result),
70+ description: "%s = %g".printf (match_string, result),
71+ has_thumbnail: false, icon_name: "accessories-calculator");
72+ }
73+ }
74+
75+ static void register_plugin ()
76+ {
77+ DataSink.PluginRegistry.get_default ().register_plugin (
78+ typeof (CalculatorPlugin),
79+ _ ("Calculator"),
80+ _ ("Calculate basic expressions."),
81+ "accessories-calculator",
82+ register_plugin,
83+ Environment.find_program_in_path ("bc") != null,
84+ _ ("bc is not installed")
85+ );
86+ }
87+
88+ static construct
89+ {
90+ register_plugin ();
91+ }
92+
93+ private Regex regex;
94+
95+ construct
96+ {
97+ /* The regex describes a string which *resembles* a mathematical expression. It does not
98+ check for pairs of parantheses to be used correctly and only whitespace-stripped strings
99+ will match. Basically it matches strings of the form:
100+ "paratheses_open* number (operator paratheses_open* number paratheses_close*)+"
101+ */
102+ try
103+ {
104+ regex = new Regex ("^\\(*(-?\\d+([.,]\\d+)?)([*/+-^]\\(*(-?\\d+([.,]\\d+)?)\\)*)+$",
105+ RegexCompileFlags.OPTIMIZE);
106+ } catch (Error e) {
107+ Utils.Logger.error (this, "Error creating regexp.");
108+ }
109+ }
110+
111+ public bool handles_query (Query query)
112+ {
113+ return (QueryFlags.ACTIONS in query.query_type);
114+ }
115+
116+ public async ResultSet? search (Query query) throws SearchError
117+ {
118+ string input = query.query_string.replace (" ", "").replace (",", ".");
119+ bool matched = regex.match (input);
120+ if (!matched && input.length > 1)
121+ {
122+ input = input[0 : input.length - 1];
123+ matched = regex.match (input);
124+ }
125+ if (matched)
126+ {
127+ Pid pid;
128+ int read_fd, write_fd;
129+ string[] argv = {"bc", "-l"};
130+ string? solution = null;
131+
132+ try
133+ {
134+ Process.spawn_async_with_pipes (null, argv, null,
135+ SpawnFlags.SEARCH_PATH,
136+ null, out pid, out write_fd, out read_fd);
137+ UnixInputStream read_stream = new UnixInputStream (read_fd, true);
138+ DataInputStream bc_output = new DataInputStream (read_stream);
139+
140+ UnixOutputStream write_stream = new UnixOutputStream (write_fd, true);
141+ DataOutputStream bc_input = new DataOutputStream (write_stream);
142+
143+ bc_input.put_string (input + "\n", query.cancellable);
144+ yield bc_input.close_async (Priority.DEFAULT, query.cancellable);
145+ solution = yield bc_output.read_line_async (Priority.DEFAULT_IDLE, query.cancellable);
146+
147+ if (solution != null)
148+ {
149+ double d = double.parse (solution);
150+ Result result = new Result (d, query.query_string);
151+ ResultSet results = new ResultSet ();
152+ results.add (result, Match.Score.AVERAGE);
153+ query.check_cancellable ();
154+ return results;
155+ }
156+ }
157+ catch (Error err)
158+ {
159+ if (!query.is_cancelled ()) warning ("%s", err.message);
160+ }
161+ }
162+
163+ query.check_cancellable ();
164+ return null;
165+ }
166+ }
167+}
168
169=== modified file 'src/Backend/SynapseSearch.vala'
170--- src/Backend/SynapseSearch.vala 2014-06-12 09:14:57 +0000
171+++ src/Backend/SynapseSearch.vala 2014-06-29 14:57:08 +0000
172@@ -21,6 +21,7 @@
173 public class SynapseSearch : Object {
174
175 private static Type[] plugins = {
176+ typeof (Synapse.CalculatorPlugin),
177 typeof (Synapse.CommandPlugin),
178 typeof (Synapse.DesktopFilePlugin)
179 };

Subscribers

People subscribed via source and target branches