Kdialog – Progress Bar

This is not that hard to use in a script, and if the user has to wait for anything, its vital feedback. However, guessing at how to get it to work is a little tiresome, and off putting.

progressbar

Here is the basic method of getting one to appear in PHP Command Line.

#!/usr/bin/php

<?php 

exec( "kdialog --progressbar \"Connecting to Server\" 30 ",$out,$ret);
$qdbus_ref = $out[0];
echo "\$qdbus_ref = $qdbus_ref\n";

$life_span = 30;
$stop = time() + $life_span ;
while( time()<=$stop ) { 
  $percent = $life_span-($stop-time());
  echo "\$percent = $percent \n";
  exec( "qdbus $qdbus_ref Set org.kde.kdialog.ProgressDialog value $percent" );
  sleep(1);
}
exec("qdbus  $qdbus_ref org.kde.kdialog.ProgressDialog.close"); ?>

That will popup a Progress Bar, but most important it gives us the all important handle to the D-BUS. It is the D-BUS reference that we will use to make it work like magic.

Note, that if you comment out the last line, the progress bar will complete to 100% and still stay open… It will not close until you click it to close… This also means you can quickly and easily have a look at the D-BUS methods and properties for your ProgressBar. A quick edit copy paste of the D-Bus reference, unique to that instance of that progress bar, reveals all…

qdbus org.kde.kdialog-19870 /ProgressDialog >progressbar.txt

Before you tell me it does not work, try using your D-Bus reference, not just copy pasting my D-Bus reference… Its unique, if you run the script again, you will get a different reference number. Also, do NOT close the pesky ProgressBar Dialog, or you will destroy the reference.

property readwrite bool org.kde.kdialog.ProgressDialog.autoClose
property readwrite int org.kde.kdialog.ProgressDialog.maximum
property readwrite int org.kde.kdialog.ProgressDialog.value
method void org.kde.kdialog.ProgressDialog.close()
method void org.kde.kdialog.ProgressDialog.setLabelText(QString label)
method void org.kde.kdialog.ProgressDialog.showCancelButton(bool value)
method bool org.kde.kdialog.ProgressDialog.wasCancelled()
method QDBusVariant org.freedesktop.DBus.Properties.Get(QString interface_name, QString property_name)
method QVariantMap org.freedesktop.DBus.Properties.GetAll(QString interface_name)
method void org.freedesktop.DBus.Properties.Set(QString interface_name, QString property_name, QDBusVariant value)
method QString org.freedesktop.DBus.Introspectable.Introspect()

I will just point out the method of adding the all important CANCEL Button. This could be more than useful, it might be really, really, really important….

exec("qdbus $qdbus_ref org.kde.kdialog.ProgressDialog.showCancelButton True",$out,$ret);

Of course, adding it is only half the story, as we would need to find out if the button has been pressed while we are looping around.

exec( "qdbus $qdbus_ref wasCancelled \n", $out, $ret );

Then of course see if $out[] contains true…..

I should also point out that $out[] will just grow and grow if you are not careful. So you should force flush with an assignment such as $out=null, each time you cycle through the main loop.

Also make sure you test for the the text string ===’true’ as qdbus will return the boolean as clear plain old text.

Enjoy.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s