Wednesday, July 27, 2005

Yummy Kapsules!

As I mentioned yesterday, I was unable to install Konfabulator on my machine. Today, I played a little with Kapsules and had some fun while getting my hands dirty by modifying an existing Widget. Here I describe the changes that I made to a Widget named Bobs LaunchList in the hope that it would benefit other developers who are new to Kapsules.

OK, let's start off with a screenshot of the finished Widget:



While browsing the list of Widgets through Widget Forge, I stumbled upon Bobs LaunchList.

Bobs LaunchList is a simple launcher that allows you to launch files from a specific folder. You can filter any number of document types (i.e., .exe, .doc, .txt), limit the number of files displayed, display a custom icon for the folder, and more.

Well, people often share files through network shares on LANs, and I had heard a friend mention that he would like to monitor the contents of a directory on a network share without having to visit the directory at periodic intervals of time. Well, one of the solutions could be to point Bobs LaunchList to the directory of interest on the networks share and modify the Widget to refresh the list of files displayed in it at fixed intervals of time that can be configured by the user.

I had done some Javascript before (most recently in Search4Code) so I could implement the change in under an hour. See screenshot above.

What I did

Each Kapsules Widget (foo) stores its configuration information in a .config file (foo.config) and the Javascript code in a .kap file (foo.kap).

I introduced a settings node in BobsLaunchList.config file called dirListReloadTime:


<dirListReloadTime>
<type>text</type>
<size>10</size>
<name>Directory Listing Reload Time</name>
<path>reloadTime</path>
<nodetype>node</nodetype>
<description>Sets the delay between automatic reloads of the directory
listing (Default: 10 minutes).</description>
</dirListReloadTime>

The default-value of this element is set by the following in the config file:

<reloadTime>10</reloadTime>

Now onto writing the Javascript code. In BobsLaunchList.kap I declared a variable to store the time in milliseconds between two successive reloads of the directory listing.

var reloadTime;

BobsLaunchList encapsulates all directory listing code in a method called "Init". All the settings of the Widget are also obtained in Init. So I introduced the following additional code there:


reloadTime = 60 * 1000
* Settings.GetSettingNode(
"widgetroot/reloadTime"
);

Here I basically get the value of "reloadTime" attribute from the config file and convert it to milliseconds.

I initialize the timer with the value of reloadTime when the Widget is activated.

function Widget_Activate()
{
Init();
Widget.AddTimer("reloadTimer", reloadTime);
Widget_Paint();
}

Next, I introduced a handler for the timer.

function reloadTimer_Tick()
{
Widget.StopTimer("reloadTimer");

Init();

Widget_Paint();

Widget.ResetTimer("reloadTimer", reloadTime);
}

At each tick of the timer, Init() gets the value of "reloadTime" element from the settings of the Widget, stores it in the variable named reloadTime, and redraws the file listing of the directory being monitored. Before terminating this event handler I set the timer to tick again reloadTime milliseconds later.

Additionaly, I deleted the timer in the Widget_Deactivate method.

function Widget_Deactivate()
{
// delete timers
Widget.DeleteTimer("reloadTimer");
}

That's it!

Thanks to Robert Lausevic who wrote the original Bobs LaunchList Widget.

Downloads

No comments: