On Mon, 26 Oct 2009, Dominik Bruhn wrote:

My third question remains:
I'm searching for a widget which uses inotify to check whether there are new mails and doesnt scan the folder all the time, does such a thing exist? Would save many cpu cycles.

Yes. I will attach one to this message, I probably got it from the wiki "User contributed widgets" page, but there is no copyright so I don't know the author (and "home" page).

Most users of vicious (for now) said that they would not install additional libraries like inotify, filesystem, socket... for some insignificant little widgets. So we keep it simple, and for me personally mdir.lua is ok if we execute every 60+ seconds.

--
Adrian C. (anrxc) | anrxc..sysphere.org | PGP ID: D20A0618
PGP FP: 02A5 628A D8EE 2A93 996E  929F D5CB 31B7 D20A 0618
-- Awesome 3.x Mailchecker Widget
-- Checks for new mail in a Maildir mail folder
--
-- 0.1:
--   initial release
--
-- 0.2:
--   use lua inotify library instead of constantly pulling the mailbox
--   display subject of new mail using naughty.notify

local lfs = lfs
local assert = assert
local pairs = pairs
local ipairs = ipairs
local awful = awful
local inotify = inotify
local io = io
local naughty = naughty
local string = string

module("mailcheck")

local widget
local mailbox
local notify
local popup

--- Register mailbox to textbox widget
-- @param w widget to register mailbox to
-- @param m path to maildir to watch
function register (w,m)
   widget = w
   notify = inotify.init(true)

   mailbox = { 
      maildir = m,
      wd = notify:add_watch( m .. "/new", { "IN_CREATE", "IN_MOVED_FROM" }),
      info = ""
   }

   widget.mouse_enter = function ()
                           popup = naughty.notify({ text = mailbox.info, width 
= 600 })
                        end
   widget.mouse_leave = function ()
                           naughty.destroy(popup)
                        end
   awful.hooks.timer.register(1, check)
   read_maildir()
end

--- Check for events in maildir folder
function check ()

   something_happened = false

   events = notify:nbread()
   if events then
      for i, event in ipairs(events) do
         if event.wd == mailbox.wd then something_happened = true end
      end
   end

   if something_happened then
      read_maildir()
   end
end

-- Count number of messages in Maildir/new and update the infobox
function read_maildir ()

      mailcount = 0
      mailbox.info = ""

      -- count message files
      for msgfile in lfs.dir(mailbox.maildir .. "/new") do
         if lfs.attributes(mailbox.maildir .. "/new/" .. msgfile, "mode") == 
"file" then
            mailcount = mailcount + 1
            f = io.open( mailbox.maildir .. "/new/" .. msgfile)

            subject = ""

            for line in f:lines() do
               if line:find("^Subject:") then subject = string.match(line, 
"^Subject: (.*)") end
            end
            f:close()
            mailbox.info = mailbox.info .. awful.util.escape("\n" .. 
string.format("%4d %-80.80s", mailcount, subject))
         end
      end

      if mailcount ~= 0 then
         widget.text = "<span color='green'>mail: " .. mailcount .. "</span>"
      else
         widget.text = "mail: 0"
      end

   end

Reply via email to