<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Upthrust &#187; bash</title>
	<atom:link href="http://blog.mpathirage.com/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mpathirage.com</link>
	<description>This is the weblog of Milinda Pathirage</description>
	<lastBuildDate>Mon, 29 Aug 2011 02:00:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Using libnotify in Ubuntu 9.04</title>
		<link>http://blog.mpathirage.com/2009/09/12/using-libnotify-in-ubuntu-9-04/</link>
		<comments>http://blog.mpathirage.com/2009/09/12/using-libnotify-in-ubuntu-9-04/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 14:00:09 +0000</pubDate>
		<dc:creator>Milinda Lakmal</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[libnotify]]></category>
		<category><![CDATA[notify osd]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://blog.mpathirage.com/?p=140</guid>
		<description><![CDATA[There are some applications which need to send notifications to users. Ubuntu 9.04 has new on-screen-display notification agent called Notify OSD which implmeents freedesktop.org Desktop Notifications Specification with semi-transparent click-through bubbles. This post is not about Notify OSD, but about how you can use libnotify to send notifications in you shell scripts, C programs and [...]]]></description>
			<content:encoded><![CDATA[<p>There are some applications which need to send notifications to users. Ubuntu 9.04 has new on-screen-display notification agent called <a href="https://launchpad.net/notify-osd" target="_blank">Notify OSD</a> which implmeents freedesktop.org Desktop Notifications Specification with semi-transparent click-through bubbles. This post is not about Notify OSD, but about how you can use libnotify to send notifications in you shell scripts, C programs and Python programs.</p>
<p><strong>Send notifications from your shell scripts</strong></p>
<p>You have to install <strong><em>libnotify-bin</em></strong> package in Ubuntu and you can use <strong><em>notify-send</em></strong> command to send notifications from your shell script. After installing libnotify-bin you can try notify-send command just like following.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">notify-send <span style="color: #ff0000;">&quot;First Notification&quot;</span></div></div>
<p>By default the message will be displayed for 5 seconds. To change how long a message stays displayed use the &#8220;-t&#8221; switch. You can use &#8220;-t 0&#8243; leave the message up until the user closes it. But in new Ubuntu notification agent will display these notifications as alert box. You can find great explanations about <a href="https://wiki.ubuntu.com/NotificationDevelopmentGuidelines" target="_blank">Notify OSD here</a>.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">notify-send <span style="color: #ff0000;">&quot;This message will be displayed for 3 seconds&quot;</span> <span style="color: #660033;">-t</span> <span style="color: #000000;">3000</span><br />
notify-send <span style="color: #ff0000;">&quot;Click me to close me.&quot;</span> <span style="color: #660033;">-t</span> <span style="color: #000000;">0</span></div></div>
<p>Add title to your notification like following:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">notify-send <span style="color: #ff0000;">&quot;This is the Title&quot;</span> &nbsp;<span style="color: #ff0000;">&quot;This is the message body&quot;</span></div></div>
<p>Add icon with title and message body(You can find <a href="https://wiki.ubuntu.com/NotificationDevelopmentGuidelines#How%20do%20I%20get%20these%20slick%20icons" target="_blank">icon codes here</a>):</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">notify-send <span style="color: #ff0000;">&quot;New Mail&quot;</span> <span style="color: #ff0000;">&quot;Hey buddy, you got a new E-mail&quot;</span> <span style="color: #660033;">-i</span> notification-message-email</div></div>
<p><strong>Send notifications from your Python application</strong></p>
<p>You need to install <strong><em>python-notify</em></strong> package first and then you can use <strong>pynotify</strong> to send notification from your Python script. There are different capabilities in different notification agents. I am ignoring getting those information in this example.</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> pynotify<br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">&quot;__main__&quot;</span>:<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># Check whether your notification agent support</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># incon-summary-body layout.</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> pynotify.<span style="color: black;">init</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;icon-summary-body&quot;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; n <span style="color: #66cc66;">=</span> pynotify.<span style="color: black;">Notification</span><span style="color: black;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;You have a mail&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;You have new e-mail from Milinda&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">&quot;notification-message-email&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; n.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p><strong>Sample code in C</strong><br />
You need to install <strong><em>libnotify-dev</em></strong>, <strong><em>libglib2.0-dev</em></strong> and <em><strong>libgtk2.0-dev</strong></em> packages first. You can compile this sample using following gcc command.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">gcc</span> test_notify.c <span style="color: #000000; font-weight: bold;">`</span>pkg-config <span style="color: #660033;">--cflags</span> glib-<span style="color: #000000;">2.0</span><span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">`</span>pkg-config <span style="color: #660033;">--cflags</span> <span style="color: #660033;">--libs</span> gtk+-<span style="color: #000000;">2.0</span><span style="color: #000000; font-weight: bold;">`</span> <span style="color: #660033;">-lnotify</span></div></div>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:350px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include</span><br />
<span style="color: #339933;">#include</span><br />
<span style="color: #339933;">#include</span><br />
<br />
<span style="color: #993333;">void</span> closed_handler <span style="color: #009900;">&#40;</span>NotifyNotification<span style="color: #339933;">*</span> notification<span style="color: #339933;">,</span> gpointer data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; g_print <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;closed_handler() called&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span><span style="color: #339933;">**</span> argv<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; NotifyNotification<span style="color: #339933;">*</span> notification<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gboolean &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;success<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; GError<span style="color: #339933;">*</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>notify_init <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;icon-summary-body&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; notification <span style="color: #339933;">=</span> notify_notification_new <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;You have a mail&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;You have mail from Milinda&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&quot;notification-message-email&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; error <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; success <span style="color: #339933;">=</span> notify_notification_show <span style="color: #009900;">&#40;</span>notification<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>error<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>success<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; g_print <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;That did not work ... <span style="color: #000099; font-weight: bold;">\&quot;</span>%s<span style="color: #000099; font-weight: bold;">\&quot;</span>.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> error<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; g_signal_connect <span style="color: #009900;">&#40;</span>G_OBJECT <span style="color: #009900;">&#40;</span>notification<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;closed&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; G_CALLBACK <span style="color: #009900;">&#40;</span>closed_handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; notify_uninit <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>For more information please refer <a href="https://wiki.ubuntu.com/NotificationDevelopmentGuidelines#How%20do%20I%20get%20these%20slick%20icons" target="_blank">Ubuntu Notification Development Guidelines</a> document.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mpathirage.com/2009/09/12/using-libnotify-in-ubuntu-9-04/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

