Merge lp:~angeloc/unity-lens-files/fix-for-948086 into lp:unity-lens-files

Proposed by Angelo Compagnucci on 2012-03-06
Status: Superseded
Proposed branch: lp:~angeloc/unity-lens-files/fix-for-948086
Merge into: lp:unity-lens-files
Diff against target: 226 lines (+98/-35)
3 files modified
src/daemon.vala (+33/-15)
src/folder.vala (+1/-1)
src/url-checker.vala (+64/-19)
To merge this branch: bzr merge lp:~angeloc/unity-lens-files/fix-for-948086
Reviewer Review Type Date Requested Status
Michal Hruby (community) 2012-03-06 Resubmit on 2012-03-07
Review via email: mp+96187@code.launchpad.net

This proposal has been superseded by a proposal from 2012-03-07.

Description of the change

More robust url validation.

To post a comment you must log in.
223. By Angelo Compagnucci on 2012-03-06

Fixing Regexp glitches.

Michal Hruby (mhr3) wrote :

Can you please set your branch which fixes the mountable uris as a prerequisite branch for this?

review: Resubmit
224. By Angelo Compagnucci on 2012-03-07

Splitting regex into const string
Use OPTIMIZE flag for regexp

225. By Angelo Compagnucci on 2012-03-07

Fixing codestyle.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/daemon.vala'
--- src/daemon.vala 2012-02-14 13:44:37 +0000
+++ src/daemon.vala 2012-03-06 17:40:28 +0000
@@ -14,6 +14,7 @@
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *15 *
16 * Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>16 * Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
17 * Modified by Angelo Compagnucci <angelo.compagnucci@gmail.com>
17 *18 *
18 */19 */
19using Zeitgeist;20using Zeitgeist;
@@ -33,6 +34,7 @@
3334
34 private Bookmarks bookmarks;35 private Bookmarks bookmarks;
35 private UrlChecker urls;36 private UrlChecker urls;
37 private UrlType url_type;
3638
37 private Unity.Lens lens;39 private Unity.Lens lens;
38 private Unity.Scope scope;40 private Unity.Scope scope;
@@ -376,14 +378,16 @@
376378
377 results_model.clear ();379 results_model.clear ();
378380
379 /* check if the thing typed isn't a url (like facebook.com) */381 /* check if the thing typed isn't a url (like facebook.com)
380 var checked_url = urls.check_url (search.search_string);382 * or isn't a remote mountable url (like ftp://ftp.ubuntu.com)*/
383 var url_type = UrlType.UNKNOWN;
384 var checked_url = urls.check_url (search.search_string, out url_type);
381 if (checked_url != null)385 if (checked_url != null)
382 {386 {
383 results_model.append (checked_url, urls.icon,387 results_model.append (checked_url, urls.get_icon_for_type(url_type),
384 Categories.FILES_AND_FOLDERS,388 Categories.FILES_AND_FOLDERS,
385 "text/html", search.search_string,389 "text/html", search.search_string,
386 checked_url, checked_url);390 checked_url, checked_url);
387 }391 }
388392
389 var category_id = has_search ?393 var category_id = has_search ?
@@ -438,13 +442,16 @@
438442
439 txn.clear ();443 txn.clear ();
440444
441 /* check if the thing typed isn't a url (like facebook.com) */445 /* check if the thing typed isn't a url (like facebook.com)
442 var checked_url = urls.check_url (search.search_string);446 * or isn't a remote mountable url (like ftp://ftp.ubuntu.com)*/
447 var url_type = UrlType.UNKNOWN;
448 var checked_url = urls.check_url (search.search_string, out url_type);
443 if (checked_url != null)449 if (checked_url != null)
444 {450 {
445 txn.append (checked_url, urls.icon, Categories.RECENT,451 txn.append (checked_url, urls.get_icon_for_type(url_type),
446 "text/html", search.search_string,452 Categories.RECENT,
447 checked_url, checked_url);453 "text/html", search.search_string,
454 checked_url, checked_url);
448 }455 }
449456
450 /* apply filters to results found by zeitgeist */457 /* apply filters to results found by zeitgeist */
@@ -757,13 +764,24 @@
757 {764 {
758 debug (@"Activating: $uri");765 debug (@"Activating: $uri");
759 try {766 try {
760 if (!bookmarks.launch_if_bookmark (uri))767 if (bookmarks.launch_if_bookmark (uri))
761 AppInfo.launch_default_for_uri (uri, null);768 return new Unity.ActivationResponse(Unity.HandledType.HIDE_DASH);
762 return new Unity.ActivationResponse(Unity.HandledType.HIDE_DASH);769
770 /* this code ensures that a file manager will be used
771 * * if uri it's a remote location that should be mounted */
772 var url_type = UrlType.UNKNOWN;
773 var checked_url = urls.check_url (uri, out url_type);
774 if (checked_url != null && url_type == UrlType.MOUNTABLE) {
775 var muris = new GLib.List<string>();
776 muris.prepend (uri);
777 var file_manager = AppInfo.get_default_for_type("inode/directory", true);
778 file_manager.launch_uris(muris,null);
779 return new Unity.ActivationResponse(Unity.HandledType.HIDE_DASH);
780 }
763 } catch (GLib.Error error) {781 } catch (GLib.Error error) {
764 warning ("Failed to launch URI %s", uri);782 warning ("Failed to launch URI %s", uri);
765 return new Unity.ActivationResponse(Unity.HandledType.NOT_HANDLED);
766 }783 }
784 return new Unity.ActivationResponse (Unity.HandledType.NOT_HANDLED);
767 }785 }
768786
769 private void on_zeitgeist_changed ()787 private void on_zeitgeist_changed ()
770788
=== modified file 'src/folder.vala'
--- src/folder.vala 2011-09-29 12:06:45 +0000
+++ src/folder.vala 2012-03-06 17:40:28 +0000
@@ -187,7 +187,7 @@
187 var uris = new List<string> ();187 var uris = new List<string> ();
188 uris.append (uri);188 uris.append (uri);
189 189
190 launcher.launch_uris (uris, new AppLaunchContext());190 launcher.launch_uris (uris, null);
191 191
192 return true;192 return true;
193 }193 }
194194
=== modified file 'src/url-checker.vala'
--- src/url-checker.vala 2012-01-30 11:26:40 +0000
+++ src/url-checker.vala 2012-03-06 17:40:28 +0000
@@ -14,51 +14,96 @@
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *15 *
16 * Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>16 * Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
17 * Modified by Angelo Compagnucci <angelo.compagnucci@gmail.com>
17 *18 *
18 */19 */
1920
20namespace Unity.FilesLens {21namespace Unity.FilesLens {
22 public enum UrlType {
23 UNKNOWN,
24 WEB,
25 MOUNTABLE;
26 }
2127
22 public class UrlChecker : Object28 public class UrlChecker : Object
23 {29 {
24 /* A string serialized GIcon */30 /* A string serialized GIcon */
25 public string icon { get; private set; }31 public string web_icon { get; private set; }
32 public string mountable_icon { get; private set; }
26 33
27 /* Regexes URLs must match */34 /* Regexes URLs must match */
28 private List<Regex> regexes;35 private Regex web_regex;
36 private Regex mountable_regex;
29 37
30 public UrlChecker ()38 public UrlChecker ()
31 {39 {
32 icon = new ThemedIcon ("web-browser").to_string ();40 web_icon = new ThemedIcon ("web-browser").to_string ();
33 regexes = new List<Regex> ();41 mountable_icon = new ThemedIcon ("folder-remote").to_string ();
34 42
35 try {43 try {
36 regexes.prepend (new Regex (".+\\...+")); // contains a dot + >= 2 chars44 web_regex = new Regex ("(http[s]{0,1}://.+){0,1}" +
37 regexes.prepend (new Regex ("www.*"));45 "[a-zA-Z0-9\\-\\.]+\\.(ac|ad|ae|aero|af|ag|" +
38 regexes.prepend (new Regex ("http://.+"));46 "ai|al|am|an|ao|aq|ar|arpa|as|asia|at|" +
39 regexes.prepend (new Regex ("https://.+"));47 "au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|" +
48 "biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|" +
49 "ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|"+
50 "com|coop|cr|cu|cv|cw|cx|cy|cz|de|dj|dk|" +
51 "dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|"+
52 "fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|" +
53 "gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|" +
54 "hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|" +
55 "io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|" +
56 "kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|" +
57 "lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|" +
58 "mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|" +
59 "mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|" +
60 "net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|" +
61 "pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|"+
62 "pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|" +
63 "sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|" +
64 "sx|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|" +
65 "tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|" +
66 "ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|" +
67 "ws|xn|xxx|ye|yt|za|zm|zw|local|[0-9]{1,3})$");
68 mountable_regex = new Regex ("(\\\\|(ftp|ssh|sftp|smb|dav)://).+");
40 } catch (RegexError e) {69 } catch (RegexError e) {
41 warning ("Error compiling regular expressions for URL matching. URL launching will not work: %s", e.message);70 warning ("Error compiling regular expressions for URL matching. URL launching will not work: %s", e.message);
42 }71 }
43 }72 }
44 73
45 /* Returns a valid HTTP/HTTPS URL if the input looks like it,74 /* Returns a valid URL if the input looks like it or null otherwise,
46 * or null otherwise */75 * returns also url type which can after be used
47 public string? check_url (string sample)76 * to retrive corresponding icon
77 */
78 public string? check_url (string sample, out UrlType url_type)
48 {79 {
49 if (sample.strip () == "") return null;80 if (sample.strip () == "") return null;
50
51 foreach (var regex in regexes)
52 {
53 if (regex.match (sample))
54 {
55 return sample.has_prefix ("http") ? sample : ("http://" + sample);
56 }
57 }
58 81
82 if (mountable_regex.match (sample))
83 {
84 url_type = UrlType.MOUNTABLE;
85 return sample.replace("\\\\","smb://");
86 }
87 else if (web_regex.match (sample))
88 {
89 url_type = UrlType.WEB;
90 return sample.has_prefix ("http") ? sample : ("http://" + sample);
91 }
92 url_type = UrlType.UNKNOWN;
59 return null;93 return null;
60 }94 }
61 95
96 public string get_icon_for_type (UrlType url_type)
97 {
98 switch (url_type){
99 case UrlType.WEB:
100 return web_icon;
101 case UrlType.MOUNTABLE:
102 return mountable_icon;
103 }
104 return web_icon;
105 }
106
62 } /* end: class UrlChecker */107 } /* end: class UrlChecker */
63108
64} /* end: namespace */109} /* end: namespace */

Subscribers

People subscribed via source and target branches