I have been fiddling with the Python programming language over the past few days. And even though it has just been few weeks and I hardly have covered every aspect of the language in detail, I am already producing working code in python. In short, I am flying! Python really rocks!
So yesterday I thought it would be nice to be able to check my Gmail inbox for new incoming mails from the command line. So a little searching gave me this link.
This is a python script that does exactly what I wanted. But it runs from the command line and is interactive in that it asks for the user name and password from the user interactively. I went a step ahead and thought what if I can make this process automated and for every new mail, show up a notification. Speaking of notifications, you must be aware of the new OSD notification that is creating much buzz these days, especially in Ubuntu circles. This new notification system is designed to replace the existing notification system with a cooler UI and better usability. At least that’s what the canonical folks claim.
Anyway, I set out to create this cool notifier and in a few hours it was ready. Being a first timer to Python, it took some time as every now and then I would stop to see some documentation and then once I understood the concept, carried on hacking. Once complete, the next few minutes I spent in fine tuning the notifier, like add an image (the gmail logo of course!!).
Eureka!! It worked, Yippee !! That’s what I like most about Python. Even a complete newbie like me can create working code in such a short time! Having written the it, now I wanted to make it more user friendly. So the next day, I ended up writing a setup.py script to provide ease of installation. Believe me, writing this setup was far more difficult than the original notification script ! But because of this script, installation is super-easy. Just run the setup.py file, that’s it!
So finally, it was done. I did some minimal testing but it should work well. You may download this from here.
Steps to install:
0. First make sure you have the python bindings for notify-osd, the new notification system for Ubuntu. If not, then install it with the below command:
$ sudo apt-get install python-notify
1. Download, gunzip and untar the gmailnotifier.tar.gz file from the above link.
2. Go to the gmailnotifier folder so created and run the setup.py file.
$ ./setup.py
It will ask a few questions, like your gmail username, password, where you want to install the script and what should be the time interval in seconds to periodically check your mailbox for new mails. Make sure you answer those questions carefully and correctly. I have not added error checking for those questions yet :-|.
But if you feel anything is not correct, you may simply press Ctrl-C, abort the script and then run it from the beginning.
3. Now simply log off from gnome and then log back in. You are all set! That’s it. And don’t worry my setup script makes sure that the configuration file is not world-readable, since your Gmail password is there in plain text.
Enjoy ! In the event that you want to uninstall my script, you can do it manually(since uninstall option is not present in the setup.py script yet). Just open the file ~/.config/autostart/gmailnotifier.desktop in your favorite text editor. Note down the line which has an ‘Exec’ in it as the first word. That line would be something like:
Exec=/path/given/while/running/setup.py/gmailnotifier/gmailnotifier.py
Note down the path, excluding the gmailnotifier part. Here it would be
/path/given/while/running/setup.py
Go to that path and delete the gmailnotifier folder altogether. After that, close the ~/.config/autostart/gmailnotifier.desktop file and delete that also. That’s it.
And in case you are wondering how this looks like, here is a screenshot of it in my Ubuntu Jaunty installation. Look at the top right hand corner. It says I have 3 new mails.

Also, if you have not noticed by now, once installed my notifier script installs itself as a gnome startup program. If you go to ‘System->Preferences->Startup Applications’ you can see a new entry named ‘gmailnotifier’. Here is a screenshot of that too, just so that you don’t get surprised later when you see this new thing there all by itself.

Read on if you want to know the technical details of my notifier. Basically my notifier depends on chiefly two main libraries - glib and pynotify. Glib is a general purpose library that is the foundation for GTK+ but more than that, it is a very high quality general purpose library that you can use freely in your other programs. Pynotify, on the other hand gives python bindings to the libnotify library.
In my script I first create a glib main loop. A main loop is an event loop that waits for events waiting indefinitely. Yeah, you guessed it, a glib main loop is at the heart of all GTK+ GUI applications which are fundamentally event driven and respond to mouse clicks, drags/drops and key events. But before I start the loop, I register for a timer callback every n seconds. This n is the interval the script would check your inbox for new mails.
This is one of the values you supply while running the setup script. Anyway, whenever this timer expires, the timer callback is called and this callback logs in, checks the Gmail RSS feed and informs you if there are any new mails. The point to be noted here is that new mails here mean mails that are ‘new’ to the script not to your inbox. Since this script does not mark new mails as read after displaying them, it maintains its own 2 buffers of old mails and new mails respectively. Any mail that is present in the new mail buffer but not in the old mail buffer is flagged as new and this count is what is shown in the the pop up.
As usual, comments/suggestions/bug reports are welcome if you liked my gmailnotifier!
admin Linux, Ubuntu Gmail, notify-osd, Python, Ubuntu
Recent Comments