revolution: update tag creation accordingly to the new tag API
[awesome.git] / lib / telak.lua.in
blobeb755caa0c67e6c23b8b9192ecf2768e5149fbf7
1 ----------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2008 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ----------------------------------------------------------------------------
7 -- Grab environment
8 local os = os
9 local io = io
10 local http = require("socket.http")
11 local ltn12 = require("ltn12")
12 local otable = otable
13 local setmetatable = setmetatable
14 local util = require("awful.util")
15 local hooks = require("awful.hooks")
16 local capi =
18 wibox = wibox,
19 widget = widget,
20 image = image
23 --- Root window image display library
24 module("telak")
26 local data = otable()
28 -- Update a telak wibox.
29 -- @param w The wibox to update.
30 local function update(w)
31 local tmp = os.tmpname()
32 http.request{url = data[w].image, sink = ltn12.sink.file(io.open(tmp, "w"))}
33 local img = capi.image(tmp)
34 if img then
35 w:geometry({ width = img.width, height = img.height })
36 w.widgets.image = img
37 else
38 w.visible = false
39 end
40 os.remove(tmp)
41 end
43 --- Create a new telak wibox. This will be automagically update to show a
44 -- local or remote image.
45 -- @param args A table with arguments: image is the local or remote image.
46 -- Timer can be specified to set the time in seconds between two update.
47 -- Default is 300 seconds.
48 -- @return The wibox. You need to attach it to a screen and to set its
49 -- coordinates as you want.
50 local function new(_, args)
51 if not args or not args.image then return end
53 -- Create wibox
54 local w = capi.wibox({ position = "floating" })
55 data[w] = { image = args.image }
56 local wimg = capi.widget({ type = "imagebox" })
57 w.widgets = wimg
59 data[w].cb = function () update(w) end
60 hooks.timer.register(args.timer or 3, data[w].cb)
62 update(w)
64 return w
65 end
67 --- Delete a telak wibox.
68 -- @param w The wibox.
69 function delete(w)
70 if data[w] then
71 hooks.timer.unregister(data[w].cb)
72 data[w] = nil
73 end
74 end
76 setmetatable(_M, { __call = new })
78 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80