Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions plugins/WebSearchPlugin/Plugin.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2025 Fernando Júnior Gomes da Silva <[email protected]>
*/

public class Detective.WebSearchMatch : Match {
public string search_engine { get; construct; }
public string search_query { get; construct; }
public string url { get; construct; }

public WebSearchMatch (string search_engine, string search_query, string url) {
var icon = new ThemedIcon ("system-search");

// Translators: %s is the search query, %s is the search engine name
string match_title = _("Search \"%s\" on %s").printf (search_query, search_engine);

Object (
relevancy: Relevancy.LOWEST,
search_engine: search_engine,
search_query: search_query,
url: url,
title: match_title,
description: url,
icon: icon
);
}

public override async void activate () throws Error {
Process.spawn_command_line_async (@"flatpak-spawn --host xdg-open \"$url\"");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Gtk.UriLauncher here which uses portals and doesn't need the extra permission for the flatpak-spawn --host

}
}

public class Detective.OpenUrlMatch : Match {
public string url { get; construct; }

public OpenUrlMatch (string url) {
var icon = new ThemedIcon ("applications-internet");

// Translators: %s is the URL to open
string match_title = _("Open %s").printf (url);

Object (
relevancy: Relevancy.HIGHEST,
url: url,
title: match_title,
description: _("Open URL in browser"),
icon: icon
);
}

public override async void activate () throws Error {
Process.spawn_command_line_async (@"flatpak-spawn --host xdg-open \"$url\"");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

}
}

public class Detective.WebSearchProvider : SearchProvider {
private const SearchEngine[] SEARCH_ENGINES = {
{ "Google", "https://www.google.com/search?q=%s" },
{ "DuckDuckGo", "https://duckduckgo.com/?q=%s" },
{ "Bing", "https://www.bing.com/search?q=%s" },
};

private struct SearchEngine {
string name;
string url_template;
}

private ListStore matches_internal;

construct {
matches_internal = new ListStore (typeof (Match));

var match_types = new ListStore (typeof (MatchType));
match_types.append (new MatchType (_("Web Search"), matches_internal));
this.match_types = match_types;
}

private bool is_url (string text) {
if (text.has_prefix ("http://") || text.has_prefix ("https://")) {
return true;
}

if (text.contains (" ")) {
return false;
}

if (text.has_prefix ("www.")) {
return true;
}

// Check generic domain pattern: contains at least one dot
// followed by 2-6 alphabetic characters (typical TLD: .com, .br, .museum)
if (text.contains (".")) {
var parts = text.split (".");
if (parts.length >= 2) {
// Get the last part (TLD)
string tld = parts[parts.length - 1].down ();

// TLDs usually have 2-6 characters and are alphabetic
if (tld.length >= 2 && tld.length <= 6) {
// Check if it's alphabetic (a-z)
for (int i = 0; i < tld.length; i++) {
unichar c = tld.get_char (i);
if (c < 'a' || c > 'z') {
return false;
}
}
return true;
}
}
}

return false;
}

private string normalize_url (string text) {
if (text.has_prefix ("http://") || text.has_prefix ("https://")) {
return text;
}

return "https://" + text;
}

public override void search (Query query) {
matches_internal.remove_all ();

if (query.search_term.length < 2) {
return;
}

if (is_url (query.search_term)) {
string normalized_url = normalize_url (query.search_term);
var open_url_match = new OpenUrlMatch (normalized_url);
matches_internal.append (open_url_match);
return;
}

string encoded_query = Uri.escape_string (query.search_term, null, true);

foreach (var engine in SEARCH_ENGINES) {
string url = engine.url_template.printf (encoded_query);
var match = new WebSearchMatch (engine.name, query.search_term, url);
matches_internal.append (match);
}
}

public override void clear () {
matches_internal.remove_all ();
}
}

public Detective.WebSearchProvider get_provider () {
return new Detective.WebSearchProvider ();
}
7 changes: 7 additions & 0 deletions plugins/WebSearchPlugin/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
shared_library (
'WebSearchPlugin',
'Plugin.vala',
dependencies: lib_dep,
install: true,
install_dir: plugin_dir
)
1 change: 1 addition & 0 deletions plugins/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ subdir('AppsPlugin')
subdir('CalculatorPlugin')
subdir('FilePlugin')
subdir('LocationPlugin')
subdir('WebSearchPlugin')
1 change: 1 addition & 0 deletions po/POTFILES
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ src/Application.vala
src/MatchRow.vala
src/SearchWindow.vala
src/ShellKeyGrabber.vala
plugins/WebSearchPlugin/Plugin.vala
20 changes: 20 additions & 0 deletions po/io.github.leolost2605.detective.pot
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,23 @@ msgid ""
"Detective needs to run in the background to be easily invokable via keyboard "
"shortcuts."
msgstr ""

#. Translators: %s is the search query, %s is the search engine name
#: plugins/WebSearchPlugin/Plugin.vala:15
#, c-format
msgid "Search \"%s\" on %s"
msgstr ""

#. Translators: %s is the URL to open
#: plugins/WebSearchPlugin/Plugin.vala:40
#, c-format
msgid "Open %s"
msgstr ""

#: plugins/WebSearchPlugin/Plugin.vala:46
msgid "Open URL in browser"
msgstr ""

#: plugins/WebSearchPlugin/Plugin.vala:74
msgid "Web Search"
msgstr ""