138 lines
4.8 KiB
C
138 lines
4.8 KiB
C
/*
|
|
* search.h — search engine integration for sovereign_browser
|
|
*
|
|
* Provides search engine configuration (DuckDuckGo default, with Google,
|
|
* Brave, Startpage, and Searx as alternatives), URL building from query
|
|
* strings, async autocomplete suggestion fetching, and a URL-vs-query
|
|
* heuristic for the URL bar.
|
|
*
|
|
* The active engine name is stored in settings ("search_engine" key) and
|
|
* synced across devices via NIP-78.
|
|
*/
|
|
|
|
#ifndef SEARCH_H
|
|
#define SEARCH_H
|
|
|
|
#include <glib.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/* ── Search engine definitions ──────────────────────────────────────── */
|
|
|
|
#define SEARCH_ENGINE_NAME_MAX 64
|
|
#define SEARCH_ENGINE_URL_MAX 512
|
|
#define SEARCH_SUGGESTION_MAX 128 /* max suggestions per request */
|
|
#define SEARCH_QUERY_MAX 1024
|
|
|
|
typedef struct {
|
|
const char *id; /* short identifier, e.g. "duckduckgo" */
|
|
const char *name; /* display name, e.g. "DuckDuckGo" */
|
|
const char *search_url; /* URL template with %s for the query */
|
|
const char *suggest_url; /* autocomplete API URL template, or NULL */
|
|
} search_engine_t;
|
|
|
|
/*
|
|
* Get the list of built-in search engines (NULL-terminated array).
|
|
* The array is static and valid for the lifetime of the program.
|
|
*/
|
|
const search_engine_t *search_engines_get(void);
|
|
|
|
/*
|
|
* Get the number of built-in search engines.
|
|
*/
|
|
int search_engines_count(void);
|
|
|
|
/*
|
|
* Look up a search engine by its id string.
|
|
* Returns the engine, or NULL if not found.
|
|
*/
|
|
const search_engine_t *search_engine_by_id(const char *id);
|
|
|
|
/*
|
|
* Get the currently active search engine (from settings).
|
|
* Always returns a valid engine — falls back to DuckDuckGo if the
|
|
* configured engine is unknown or unset.
|
|
*/
|
|
const search_engine_t *search_engine_get_active(void);
|
|
|
|
/*
|
|
* Set the active search engine by id. Persists to settings.
|
|
* Returns 0 on success, -1 if the id is unknown.
|
|
*/
|
|
int search_engine_set_active(const char *id);
|
|
|
|
/* ── URL building ──────────────────────────────────────────────────── */
|
|
|
|
/*
|
|
* Build a search results URL for the given query using the active engine.
|
|
* query — the search query string (raw, will be URL-encoded)
|
|
* Returns a newly allocated string (caller must g_free).
|
|
*/
|
|
char *search_build_search_url(const char *query);
|
|
|
|
/*
|
|
* Build a search results URL for the given query using a specific engine.
|
|
* Returns a newly allocated string (caller must g_free).
|
|
*/
|
|
char *search_build_search_url_for(const search_engine_t *engine,
|
|
const char *query);
|
|
|
|
/* ── URL heuristic ─────────────────────────────────────────────────── */
|
|
|
|
/*
|
|
* Determine whether the given input string looks like a URL rather than
|
|
* a search query.
|
|
*
|
|
* Returns TRUE if:
|
|
* - It contains "://" (has a scheme)
|
|
* - It starts with "about:" (internal pages)
|
|
* - It has no spaces AND contains a dot (looks like a domain)
|
|
* - It's a valid-looking IPv4 address
|
|
* - It's "localhost" or starts with "localhost:"
|
|
*/
|
|
gboolean search_is_url(const char *input);
|
|
|
|
/* ── Async suggestion fetch ────────────────────────────────────────── */
|
|
|
|
/*
|
|
* Callback invoked when autocomplete suggestions have been fetched.
|
|
* suggestions — NULL-terminated array of newly allocated strings,
|
|
* or NULL on error / no suggestions
|
|
* user_data — the pointer passed to search_suggest_fetch_async()
|
|
*/
|
|
typedef void (*search_suggest_callback)(char **suggestions,
|
|
gpointer user_data);
|
|
|
|
/*
|
|
* Asynchronously fetch autocomplete suggestions for the given query
|
|
* from the active search engine's suggestion API.
|
|
*
|
|
* If the active engine has no suggestion URL, the callback is called
|
|
* immediately with NULL. The callback is always invoked on the GTK
|
|
* main thread (via g_idle_add), so it's safe to touch GTK widgets.
|
|
*
|
|
* query — the partial search query
|
|
* callback — called with the results (or NULL on error)
|
|
* user_data — passed to the callback
|
|
*
|
|
* Returns a guint request ID (can be used with search_suggest_cancel()),
|
|
* or 0 if the request could not be started.
|
|
*/
|
|
guint search_suggest_fetch_async(const char *query,
|
|
search_suggest_callback callback,
|
|
gpointer user_data);
|
|
|
|
/*
|
|
* Cancel a pending suggestion request by its ID.
|
|
* Safe to call with 0 (no-op) or an already-completed request ID.
|
|
*/
|
|
void search_suggest_cancel(guint request_id);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* SEARCH_H */
|