Merge lp:~donadigo/slingshot/open-link-browser into lp:~elementary-pantheon/slingshot/trunk

Proposed by Adam Bieńkowski on 2016-02-20
Status: Merged
Approved by: Daniel Fore on 2016-02-21
Approved revision: 635
Merged at revision: 634
Proposed branch: lp:~donadigo/slingshot/open-link-browser
Merge into: lp:~elementary-pantheon/slingshot/trunk
Diff against target: 216 lines (+150/-4)
5 files modified
lib/synapse-plugins/CMakeLists.txt (+1/-0)
lib/synapse-plugins/link-plugin.vala (+140/-0)
src/Backend/SynapseSearch.vala (+2/-1)
src/Widgets/SearchItem.vala (+2/-1)
src/Widgets/SearchView.vala (+5/-2)
To merge this branch: bzr merge lp:~donadigo/slingshot/open-link-browser
Reviewer Review Type Date Requested Status
Daniel Fore 2016-02-20 Needs Fixing on 2016-02-21
Review via email: mp+286738@code.launchpad.net

Commit message

Fix bug #1487786 "When entering web address, offer to open default browser [$15]".

Description of the change

Fix bug #1487786 "When entering web address, offer to open default browser [$15]".

I tried to keep the diff as small as possible, regex allows to open links with "https" and without it, plugin gets default browser by what's displayed in the applications plug. Tested with Midori, Chrome and Firefox.

To post a comment you must log in.
Daniel Fore (danrabbit) wrote :

Hrm I'm not sure how terribly useful this is when the regex requires x.y.z format or http(s). I would expect that most users write urls like foo.bar as a common case

review: Needs Fixing
635. By Adam Bieńkowski on 2016-02-21

Change regex to accept urls without www

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 2015-09-23 22:17:18 +0000
3+++ lib/synapse-plugins/CMakeLists.txt 2016-02-21 20:36:00 +0000
4@@ -20,6 +20,7 @@
5 desktop-file-plugin.vala
6 switchboard-plugin.vala
7 system-managment.vala
8+ link-plugin.vala
9 )
10
11 # Compile
12
13=== added file 'lib/synapse-plugins/link-plugin.vala'
14--- lib/synapse-plugins/link-plugin.vala 1970-01-01 00:00:00 +0000
15+++ lib/synapse-plugins/link-plugin.vala 2016-02-21 20:36:00 +0000
16@@ -0,0 +1,140 @@
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 LinkPlugin: 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+ public 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+ private string uri;
67+ private AppInfo? appinfo;
68+
69+ public Result (string link)
70+ {
71+ uri = link;
72+ string _title = _("Open %s in default web browser".printf (uri));
73+ string _icon_name = "web-browser";
74+
75+ appinfo = AppInfo.get_default_for_type ("x-scheme-handler/http", false);
76+ if (appinfo != null)
77+ {
78+ _title = _("Open %s in %s").printf (uri, appinfo.get_display_name ());
79+ _icon_name = appinfo.get_icon ().to_string ();
80+ }
81+
82+ this.title = _title;
83+ this.icon_name = _icon_name;
84+ this.description = _("Open this link in default browser");
85+ this.has_thumbnail = false;
86+ this.match_type = MatchType.ACTION;
87+ }
88+
89+ public void execute (Match? match)
90+ {
91+ if (appinfo == null) {
92+ return;
93+ }
94+
95+ var list = new List<string> ();
96+ list.append (uri);
97+ try
98+ {
99+ appinfo.launch_uris (list, null);
100+ } catch (Error e)
101+ {
102+ warning ("%s\n", e.message);
103+ }
104+ }
105+ }
106+
107+ static void register_plugin ()
108+ {
109+ DataSink.PluginRegistry.get_default ().register_plugin (
110+ typeof (LinkPlugin),
111+ _("Link"),
112+ _("Open link in default browser"),
113+ "web-browser",
114+ register_plugin
115+ );
116+ }
117+
118+ static construct
119+ {
120+ register_plugin ();
121+ }
122+
123+ private Regex regex;
124+
125+ construct
126+ {
127+ try
128+ {
129+ regex = new Regex ("[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,4}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)",
130+ RegexCompileFlags.OPTIMIZE);
131+ } catch (Error e) {
132+ Utils.Logger.error (this, "Error creating regexp.");
133+ }
134+ }
135+
136+ public bool handles_query (Query query)
137+ {
138+ return QueryFlags.TEXT in query.query_type;
139+ }
140+
141+ public async ResultSet? search (Query query) throws SearchError
142+ {
143+ bool matched = regex.match (query.query_string);
144+ if (matched)
145+ {
146+ Result result = new Result (query.query_string);
147+ ResultSet results = new ResultSet ();
148+ results.add (result, Match.Score.AVERAGE);
149+ query.check_cancellable ();
150+ return results;
151+ }
152+
153+ return null;
154+ }
155+ }
156+}
157
158=== modified file 'src/Backend/SynapseSearch.vala'
159--- src/Backend/SynapseSearch.vala 2015-06-03 19:10:24 +0000
160+++ src/Backend/SynapseSearch.vala 2016-02-21 20:36:00 +0000
161@@ -25,7 +25,8 @@
162 typeof (Synapse.CommandPlugin),
163 typeof (Synapse.DesktopFilePlugin),
164 typeof (Synapse.SwitchboardPlugin),
165- typeof (Synapse.SystemManagementPlugin)
166+ typeof (Synapse.SystemManagementPlugin),
167+ typeof (Synapse.LinkPlugin)
168 };
169
170 private static Synapse.DataSink? sink = null;
171
172=== modified file 'src/Widgets/SearchItem.vala'
173--- src/Widgets/SearchItem.vala 2015-12-22 17:07:27 +0000
174+++ src/Widgets/SearchItem.vala 2016-02-21 20:36:00 +0000
175@@ -28,7 +28,8 @@
176 CONTACT,
177 INTERNET,
178 SETTINGS,
179- APP_ACTIONS
180+ APP_ACTIONS,
181+ LINK
182 }
183
184 const int ICON_SIZE = 32;
185
186=== modified file 'src/Widgets/SearchView.vala'
187--- src/Widgets/SearchView.vala 2015-12-19 14:15:03 +0000
188+++ src/Widgets/SearchView.vala 2016-02-21 20:36:00 +0000
189@@ -44,8 +44,8 @@
190 list_box.set_header_func ((Gtk.ListBoxUpdateHeaderFunc) update_header);
191 list_box.row_activated.connect ((row) => {
192 var search_item = row as SearchItem;
193- if (search_item.result_type == SearchItem.ResultType.APP_ACTIONS) {
194- ((Synapse.DesktopFilePlugin.ActionMatch) search_item.app.match).execute (null);
195+ if (search_item.result_type == SearchItem.ResultType.APP_ACTIONS || search_item.result_type == SearchItem.ResultType.LINK) {
196+ search_item.app.match.execute (null);
197 } else {
198 search_item.app.launch ();
199 }
200@@ -70,6 +70,8 @@
201 if (uri.has_prefix ("http://") || uri.has_prefix ("ftp://") || uri.has_prefix ("https://")) {
202 result_type = SearchItem.ResultType.INTERNET;
203 }
204+ } else if (match is Synapse.LinkPlugin.Result) {
205+ result_type = SearchItem.ResultType.LINK;
206 }
207
208 if (result_type == SearchItem.ResultType.UNKNOWN) {
209@@ -153,6 +155,7 @@
210 case SearchItem.ResultType.GENERIC_URI:
211 label = _("Files");
212 break;
213+ case SearchItem.ResultType.LINK:
214 case SearchItem.ResultType.ACTION:
215 label = _("Actions");
216 break;

Subscribers

People subscribed via source and target branches