diff options
author | Christian Dywan <christian@twotoasts.de> | 2018-09-10 14:47:17 +0200 |
---|---|---|
committer | Christian Dywan <christian@twotoasts.de> | 2018-09-12 23:56:23 +0200 |
commit | 4bb2254c42593ef26b519521636088212341b67d (patch) | |
tree | b0ecb7f25b463685f4eec31aa2c6b45ab5d5cbad | |
parent | 1245e9cd5603d0fe6e27c84d4a386bd5c47c9e0f (diff) | |
download | midori-git-4bb2254c42593ef26b519521636088212341b67d.tar.gz |
Introduce DatabaseItem.id to expose row_idexpose-row-id
-rw-r--r-- | core/database.vala | 49 | ||||
-rw-r--r-- | core/tab.vala | 32 |
2 files changed, 59 insertions, 22 deletions
diff --git a/core/database.vala b/core/database.vala index 748f54c3..925d01ee 100644 --- a/core/database.vala +++ b/core/database.vala @@ -147,6 +147,7 @@ namespace Midori { public class DatabaseItem : Object { public Database? database { get; set; } + public int64 id { get; set; } public string uri { get; set; } public string? title { get; set; } public int64 date { get; set; } @@ -367,16 +368,16 @@ namespace Midori { } /* - * Delete an item from the database, where the URI matches. + * Delete an item from the database. */ public async bool delete (DatabaseItem item) throws DatabaseError { string sqlcmd = """ - DELETE FROM %s WHERE uri = :uri + DELETE FROM %s WHERE rowid = :id """.printf (table); DatabaseStatement statement; try { statement = prepare (sqlcmd, - ":uri", typeof (string), item.uri); + ":id", typeof (int64), item.id); if (statement.exec ()) { if (_items != null) { int index = _items.index (item); @@ -392,11 +393,31 @@ namespace Midori { } /* + * Lookup a specific item by its URI. + */ + public async virtual DatabaseItem? lookup (string uri) throws DatabaseError { + string sqlcmd = """ + SELECT rowid, title, date FROM %s WHERE uri = :uri LIMIT 1 + """.printf (table); + var statement = prepare (sqlcmd, + ":uri", typeof (string), uri); + if (statement.step ()) { + string title = statement.get_string ("title"); + int64 date = statement.get_int64 ("date"); + var item = new DatabaseItem (uri, title, date); + item.database = this; + item.id = statement.get_int64 ("rowid"); + return item; + } + return null; + } + + /* * Determine if the item is in the database, where the URI matches. */ public bool contains (DatabaseItem item) throws DatabaseError { string sqlcmd = """ - SELECT uri FROM %s WHERE uri = :uri + SELECT uri FROM %s WHERE uri = :uri LIMIT 1 """.printf (table); DatabaseStatement statement; try { @@ -413,18 +434,21 @@ namespace Midori { * Query items from the database, matching filter if given. */ public async virtual List<DatabaseItem>? query (string? filter=null, int64 max_items=15, Cancellable? cancellable=null) throws DatabaseError { + string where = filter != null ? "WHERE uri LIKE :filter OR title LIKE :filter" : ""; string sqlcmd = """ - SELECT uri, title, date, count () AS ct FROM %s - WHERE uri LIKE :filter OR title LIKE :filter + SELECT rowid, uri, title, date, count () AS ct FROM %s + %s GROUP BY uri ORDER BY ct DESC LIMIT :limit - """.printf (table); + """.printf (table, where); DatabaseStatement statement; try { - string real_filter = "%" + (filter ?? "").replace (" ", "%") + "%"; statement = prepare (sqlcmd, - ":filter", typeof (string), real_filter, ":limit", typeof (int64), max_items); + if (filter != null) { + string real_filter = "%" + filter.replace (" ", "%") + "%"; + statement.bind (":filter", typeof (string), real_filter); + } } catch (Error error) { critical (_("Failed to select from %s: %s"), table, error.message); return null; @@ -438,6 +462,7 @@ namespace Midori { int64 date = statement.get_int64 ("date"); var item = new DatabaseItem (uri, title, date); item.database = this; + item.id = statement.get_int64 ("rowid"); items.append (item); uint src = Idle.add (query.callback); @@ -457,15 +482,16 @@ namespace Midori { } /* - * Update an existing item, where URI and date match. + * Update an existing item. */ public async bool update (DatabaseItem item) throws DatabaseError { string sqlcmd = """ - UPDATE %s SET title=:title WHERE uri = :uri AND date=:date + UPDATE %s SET uri=:uri, title=:title, date=:date WHERE rowid = :id """.printf (table); DatabaseStatement statement; try { statement = prepare (sqlcmd, + ":id", typeof (int64), item.id, ":uri", typeof (string), item.uri, ":title", typeof (string), item.title, ":date", typeof (int64), item.date); @@ -495,6 +521,7 @@ namespace Midori { ":title", typeof (string), item.title, ":date", typeof (int64), item.date); if (statement.exec ()) { + item.id = statement.row_id (); if (_items != null) { _items.append (item); items_changed (_items.index (item), 0, 1); diff --git a/core/tab.vala b/core/tab.vala index 9bdf87e0..d291cf0a 100644 --- a/core/tab.vala +++ b/core/tab.vala @@ -46,8 +46,10 @@ namespace Midori { can_go_forward = base.can_go_forward (); }); notify["title"].connect ((pspec) => { - display_title = (title != null && title != "") ? title : display_uri; - item.title = display_title; + if (title != null && title != "") { + display_title = title; + item.title = display_title; + } }); } @@ -61,26 +63,30 @@ namespace Midori { settings.user_agent = settings.user_agent.replace ("Version/11.0", "Chrome/55.0.2876.0"); settings.enable_developer_extras = true; + if (uri != null) { + display_uri = uri; + display_title = (title != null && title != "") ? title : uri; + } else { + display_uri = "internal:speed-dial"; + display_title = _("Speed Dial"); + } + item = new DatabaseItem (display_uri, null, 0); + if (pinned) { var extensions = Plugins.get_default ().plug<TabActivatable> ("tab", this); extensions.extension_added.connect ((info, extension) => ((TabActivatable)extension).activate ()); extensions.foreach ((extensions, info, extension) => { extensions.extension_added (info, extension); }); - load_uri (uri ?? "internal:speed-dial"); + load_uri (display_uri); } else { load_uri_delayed.begin (uri, title); } } async void load_uri_delayed (string? uri, string? title) { - display_uri = uri ?? "internal:speed-dial"; - display_title = title ?? display_uri; - item = new DatabaseItem (display_uri, display_title, 0); - // Get title from history try { var history = HistoryDatabase.get_default (); - var items = yield history.query (display_title, 1); - var item = items.nth_data (0); + var item = yield history.lookup (display_uri); if (item != null) { display_title = item.title; this.item = item; @@ -117,14 +123,18 @@ namespace Midori { public override void load_changed (WebKit.LoadEvent load_event) { if (load_event == WebKit.LoadEvent.COMMITTED) { - item = new DatabaseItem (uri, display_title, new DateTime.now_local ().to_unix ()); + secure = get_tls_info (null, null); + item = new DatabaseItem (uri, null, new DateTime.now_local ().to_unix ()); + // Don't add internal or blank pages to history + if (uri.has_prefix ("internal:") || uri.has_prefix ("about:")) { + return; + } try { var history = HistoryDatabase.get_default (); history.insert.begin (item); } catch (DatabaseError error) { debug ("Failed to insert history item: %s", error.message); } - secure = get_tls_info (null, null); } } |