Perl applets extend a platform-independent desktop panel

Strictly by the Rules

Listing 2 Ticker.pm contains the applet code and has to follow the PerlPanel's rules. This includes a module in the PerlPanel::Applet::Name name space, a new() constructor, and an initial configure() function, which the panel runs first. The expand() and fill() functions stipulate how the widget changes if more space becomes available in the panel. widget() must return a pointer to the topmost Gtk widget in the applet, and get_default_config() normally returns a structure that configures the applet. However, as the configuration is stored in an external file in our case, to avoid any changes by the user requiring a restart of the applet, the function only returns undef here.

Listing 2

Ticker.pm

 

Enough said about the PerlPanel API – what we need now is the application code. The applet uses configure() to build its GUI, which comprises a label with share price data and a button that handles user clicks. Line 42 defines this using the widget's signal_connect() method, which assigns an anonymous subroutine to the clicked event; the subroutine calls the applet's stocks_update() method. After all the widgets have been defined, show_all() draws them in the panel. Line 56 calls stocks_update() for the first time before the program uses add_timeout() to define an event that reoccurs every five minutes (5 * 60 * 1000 milliseconds), which also calls stocks_update(). The function in turn calls symbols() to parse the ~/.ticker-rc file, thus immediately catching short-term symbol updates by the user.

Comment lines, blanks and anything that does not look like a ticker symbol is simply ditched by the function; later on, these symbols will be sent to the getquotes script, and it's essential to avoid unpleasant surprises with unchecked command-line parameters.

Background

When Perl's open() function is called by stocks_update() in line 110, it creates a pipe to allow getquote to run in the background. Be careful here: If the code were to grab the output of the externally called process right after, it might have to wait a couple of seconds for the results to trickle in from the Internet, which is not a good idea for a GUI that must react swiftly to user input that can happen at any time. Instead, stocks_update() uses the Gtk2::Helper add_watch() function, which accepts a file descriptor (fileno() generates a descriptor from a Perl file handle) and jumps to a callback function when data arrives on it. This means that the script keeps running in the meantime, terminating stocks_update() and jumping to the applet's main event loop, where it can process user input and other external events without any delays.

If get_quote has finally sent some data, but still has more to come, the call to eof(COMMAND) is false and the Else loop in Line 132 appends the data to the existing results. When get_quote finishes, it's the IF branch's turn; the file handle is cleaned up by close(), and remove_watch() stops watching the corresponding descriptor. Line 129 then transforms the column format to a single Symbol Price Symbol Price … line and then calls the set_markup() method to send it to the label widget, which displays the text in the panel.

To make sure that the developer knows what the applet is up to, Ticker.pm first initializes Log4perl to redirect the log statements embedded in the code to the /tmp/ticker.log file. Figure 2 shows a couple of lines from the log. If you do not need this additional output, simply comment out the call to easy_init() in the code.

Figure 2: The Perl applet logs its current activities to the /tmp/ticker.log file.

Installation

On Ubuntu, you can type sudo apt-get install perlpanel at the command line to install PerlPanel along with the Perl modules on which it depends. The files are available from the Ubuntu Package repository, so there is no need for a CPAN Shell. The getquote script then must be installed in /usr/bin and requires the Finance::-YahooQuote module, which is also available as a Ubuntu package (libfinance-yahooquote-perl).

To tell PerlPanel about the new ticker applet, you must copy the Ticker.pm file to the /usr/share/perlpanel/PerlPanel directory, where you will find some other applets that are installed as part of the PerlPanel distribution. Then you need to add the line

Ticker:Stock Ticker:Utilities

to the applet registry file, /usr/share/perlpanel/applet.registry. This specifies that the panel will find the "Ticker" widget in the applet directory under the name of Ticker.pm, that it is a stock ticker, and that it should be located in the Utiltities section.

After it restarts, PerlPanel knows that the applet exists, but still does not display it. To allow this to happen, the user first has to add the applet to the panel. To do so, you need to press the Actions button in the panel, and select the Configure menu item. In the dialog box that follows, click the +Add button. This gives you the selection box (Figure 3), which shows a selection of ready-to-run applets. One of them is your ticker, which you can then select and install by pressing the +Install Applet button. The applet then appears in the container, where you can move it up or down to change its corresponding horizontal display position in the panel.

Figure 3: The newly written Ticker Perl applet is available for selection.

To launch the panel automatically when you log in to a session with your window manager, you must add the /usr/bin/perlpanel program to your session start dialog; on Gnome, this looks something like the dialog box shown in Figure 4, which appears when you click System | Preferences | Sessions in the main menu.

Figure 4: This configuration tells the Gnome Session Manager to launch the PerlPanel when you log in.

For more information on building your own Perl applets, you can read the perlpanel-applet-howto.pod file, which is not part of the Ubuntu package, but is available with the source code from the PerlPanel CVS repository [2].

All of the panel's functions, including the desktop pager, the taskbar, and the dialog boxes for adding new applets and their configurations, were written in Perl and give you a taste of what's possible with PerlPanel.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

072-076_perl.pdf  (802.66 kB)

News