File: history.lua

package info (click to toggle)
luakit 2012.09.13-r1-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,160 kB
  • ctags: 1,276
  • sloc: ansic: 6,086; makefile: 153; ruby: 79; sh: 38
file content (111 lines) | stat: -rw-r--r-- 2,913 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
-----------------------------------------------------------
-- Save history in sqlite3 database                      --
-- © 2010-2011 Mason Larobina <mason.larobina@gmail.com> --
-----------------------------------------------------------

local os = require "os"
local webview = webview
local table = table
local string = string
local lousy = require "lousy"
local capi = { luakit = luakit, sqlite3 = sqlite3 }

module "history"

-- Path of history sqlite database to open/create/update
db_path = capi.luakit.data_dir .. "/history.db"

-- Setup signals on history module
lousy.signal.setup(_M, true)

function init()
    -- Return if database handle already open
    if db then return end

    db = capi.sqlite3{ filename = _M.db_path }
    db:exec [[
        PRAGMA synchronous = OFF;
        PRAGMA secure_delete = 1;

        CREATE TABLE IF NOT EXISTS history (
            id INTEGER PRIMARY KEY,
            uri TEXT,
            title TEXT,
            visits INTEGER,
            last_visit INTEGER
        );
    ]]

    query_find_last = db:compile [[
        SELECT id
        FROM history
        WHERE uri = ?
        ORDER BY last_visit DESC
        LIMIT 1
    ]]

    query_insert = db:compile [[
        INSERT INTO history
        VALUES (NULL, ?, ?, ?, ?)
    ]]

    query_update_visits = db:compile [[
        UPDATE history
        SET visits = visits + 1, last_visit = ?
        WHERE id = ?
    ]]

    query_update_title = db:compile [[
        UPDATE history
        SET title = ?
        WHERE id = ?
    ]]
end

capi.luakit.idle_add(init)

function add(uri, title, update_visits)
    if not db then init() end

    -- Ignore blank uris
    if not uri or uri == "" or uri == "about:blank" then return end
    -- Ask user if we should ignore uri
    if _M.emit_signal("add", uri, title) == false then return end

    -- Find existing item
    local item = (query_find_last:exec{uri})[1]
    if item then
        if update_visits ~= false then
            query_update_visits:exec{os.time(), item.id}
        end
        if title then
            query_update_title:exec{title, item.id}
        end
    else
        query_insert:exec{uri, title, 1, os.time()}
    end
end

webview.init_funcs.save_hist = function (view)
    -- Add items & update visit count
    view:add_signal("load-status", function (_, status)
        -- Don't add history items when in private browsing mode
        if view.enable_private_browsing then return end

        if status == "committed" then
            add(view.uri)
        end
    end)
    -- Update titles
    view:add_signal("property::title", function ()
        -- Don't add history items when in private browsing mode
        if view.enable_private_browsing then return end

        local title = view.title
        if title and title ~= "" then
            add(view.uri, title, false)
        end
    end)
end

-- vim: et:sw=4:ts=8:sts=4:tw=80