Files
sovereign_browser/src/history.c

114 lines
3.3 KiB
C

/*
* history.c — URL history tracking for sovereign_browser
*
* History is stored in the SQLite database (history table) with timestamps
* and visit counts. No flat file, no entry cap.
*/
#include "history.h"
#include "db.h"
#include <string.h>
#include <stdlib.h>
/* ── Public API ────────────────────────────────────────────────────── */
void history_add(const char *url) {
history_add_titled(url, NULL);
}
void history_add_titled(const char *url, const char *title) {
if (url == NULL || url[0] == '\0') return;
/* Skip sovereign:// API endpoints (not pages the user would want
* in their recents). The actual pages — sovereign://settings,
* sovereign://profile, sovereign://bookmarks, sovereign://security —
* are kept in history so the user can quickly return to them. */
if (strncmp(url, "sovereign://settings/set", 24) == 0 ||
strncmp(url, "sovereign://bookmarks/add", 25) == 0 ||
strncmp(url, "sovereign://bookmarks/delete", 28) == 0 ||
strncmp(url, "sovereign://bookmarks/createdir", 31) == 0 ||
strncmp(url, "sovereign://bookmarks/deletedir", 31) == 0 ||
strncmp(url, "sovereign://bookmarks/move", 26) == 0 ||
strncmp(url, "sovereign://bookmarks/renamedir", 31) == 0 ||
strncmp(url, "sovereign://qr", 14) == 0 ||
strncmp(url, "sovereign://nostr/", 18) == 0) {
return;
}
db_history_add(url, title);
}
int history_count(void) {
/* Query the count from the database via db_history_get.
* For the Recents submenu we only show 20, but we need the
* total count to decide whether to show the submenu. */
char **urls = NULL;
int count = 0;
int rc = db_history_get(&urls, NULL, &count, 10000);
if (rc < 0) return 0;
if (urls) {
for (int i = 0; i < count; i++) g_free(urls[i]);
g_free(urls);
}
return count;
}
char *history_get(int index) {
if (index < 0) return NULL;
char **urls = NULL;
int count = 0;
db_history_get(&urls, NULL, &count, index + 1);
if (urls == NULL || count <= index) {
if (urls) {
for (int i = 0; i < count; i++) g_free(urls[i]);
g_free(urls);
}
return NULL;
}
char *result = urls[index];
/* Free the other entries. */
for (int i = 0; i < count; i++) {
if (i != index) g_free(urls[i]);
}
g_free(urls);
return result;
}
char *history_get_title(int index) {
if (index < 0) return NULL;
char **urls = NULL;
char **titles = NULL;
int count = 0;
db_history_get(&urls, &titles, &count, index + 1);
if (urls == NULL || count <= index) {
if (urls) {
for (int i = 0; i < count; i++) g_free(urls[i]);
g_free(urls);
}
if (titles) {
for (int i = 0; i < count; i++) g_free(titles[i]);
g_free(titles);
}
return NULL;
}
char *result = titles[index];
/* Free everything except the result. */
for (int i = 0; i < count; i++) {
if (i != index) g_free(titles[i]);
g_free(urls[i]);
}
g_free(urls);
g_free(titles);
return result;
}
void history_clear(void) {
db_history_clear();
g_print("[history] cleared\n");
}