-
Notifications
You must be signed in to change notification settings - Fork 3
Add WebSearchPlugin #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JrFernando
wants to merge
6
commits into
leolost2605:main
Choose a base branch
from
JrFernando:feat/add-web-search-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7b44b27
feat: Add WebSearchPlugin with support for multiple search engines.
JrFernando 69ea159
feat: Adds WebSearchPlugin text to i18n files
JrFernando 6e0d662
feat: Added support for opening URLs directly in the browser.
JrFernando 3309bc2
feat: Replace the activation method for opening URLs using Gtk.UriLau…
JrFernando 56c5e2b
Merge branch 'main' of github.com:leolost2605/detective into feat/add…
JrFernando 2c0143d
feat: Update translations for the web search plugin
JrFernando File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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\""); | ||
| } | ||
| } | ||
|
|
||
| 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\""); | ||
|
||
| } | ||
| } | ||
|
|
||
| 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 (); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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