Show a dialog with Kdialog (part 1)

Updates

The following updates have been applied:

2021-03-25:
  • Added the --imgbox dialog
2022-01-12:
  • Added the --dontagain option paragraph
2024-01-18:
  • Added the example script for the --imgbox dialog

If you ever needed to write shell scripts to automatize your tasks, but at the same time wanted a better integration with the running Desktop Environment, an application like Gdialog or Kdialog might be a good choice.

Being a KDE user myself, this article will focus on Kdialog, but I guess the same consideration might still be valid for Gdialog or the ncurses version dialog.

A dialog for everything

The first thing I loved since I moved my first steps into the Linux world, is the powerfull shell installed by default and a large number of command-line applications that allow writing fantastic scripts.

Kdialog is one of them and its goal is to provide a wide range of dialog’s windows, for input and output purposes: a simple message box or a file selection dialog, a mere text input dialog or a selection list.

Most of the time these dialogs give substantial help providing a sort of input validation, that’s why shouldn’t be considered decoration only.

Structure of a dialog

A dialog is a window, resizable or not, with a set of elements:

  1. a title to introduce the dialog itself;
  2. a text area intended as a message output or an input request;
  3. a widget area , where the needed components are deployed to perform the request (for input dialogs only);
  4. a set of buttons at the bottom to complete the action.

Later we will see how, reading the standard output and the exit status after a Kdialog call, we will have full control over the choice made.

Let’s start with the output dialogs.

Say something

The simplest use case is a message box with an optional title:

$ kdialog --msgbox "Hello World!" --title "Just saying hello"

The icon in the dialog shows that the kind of message is purely informative and doesn’t deserve particular attention, it can be used to communicate the end of a process or the result of a calculation.

The –title switch is optional and can be used for the whole set of dialogs provided by Kdialog.

We can also warn the user:

$ kdialog --sorry "Ehi, I am here!" --title "Warning you"

Or communicate an error:

$ kdialog --error "You made a mistake" --title "Something went wrong"

Giving more details

In the latest two cases, we can provide more info within a hidden textbox, which can be displayed pressing the Details button.

The detailedsorry:

$ kdialog --detailedsorry "Ehi, I am here" "Behave or I'll tell your mom!" --title "Warning you"

And the detailederror:

$ kdialog --detailederror "You made a mistake" "Didn't consider the other opportunity" --title "Something went wrong"

Notification popup

Another less known output dialog is represented by the Passive Popup which is just a notification message appearing in the bottom right corner, which disappears after nth seconds, and that can display an optional icon:

$ kdialog --passivepopup "I am going to vanish in three seconds" 3 --icon dialog-error --title "Auto-destructive message"

The icon parameter accepts one of the icon names included in the current theme, it defaults to dialog-information, the same icon used by the msgbox dialog.

Later we will see how to get the list without dig into the icon theme folders, but just using Kdialog.

Reading from a file

A useful output dialog is the textbox: it’s intended to show the content of a text file within a resizable window having only a text area and an ok button.

$ kdialog --textbox ~/.bashrc --geometry 640x400+400+300 --title ".bashrc content"

Being the window resizable we can use the optional geometry switch to fix, respectively, the width, height, and the offset from the left and the top border of the screen.

Display a picture

One of the last addition to the set of output dialogs is imgbox: it simply shows a picture and an ok button.

$ kdialog --imgbox "Downloads/Cliparts/OpenSUSE_Logo.svg" --title "openSUSE Logo"

The dialog can’t be resized to scale down the picture, so it’s not comfortable to use it with large images.

Showing progress

Last but not least the progressbar is the most complex among the output dialogs because requires several steps to work properly:

$ kdialog --progressbar "I am progressing" 10 --title "Step by step" --geometry 400x200+400+300

Once the dialog is launch, something is printed in the standard output, leaving the shell free to evaluate other commands.

To interact with that dialog we need the power of qdbus combined with the previous output:

$ qdbus org.kde.kdialog-5433 /ProgressDialog org.freedesktop.DBus.Properties.Set '' 'value' 5

The idea is to use a loop in our script to continously update the progress value, until the max value is reached.

We can also set the autoclose property when the value reaches the max:

$ qdbus org.kde.kdialog-5433 /ProgressDialog org.freedesktop.DBus.Properties.Set '' 'autoClose' 1

Don’t again option

For all the dialogs mentioned within the paragraphs Say something and Giving more details, there is the --dontagain option that adds an interesting feature.

To explain the role of that option let’s start saying that dialogs are very useful to communicate errors, warning or simply info regarding any state of the running script, but sometimes all that messages might result too repetitive especially when the script is used, for example, in a daily basis, and might be useful shut them up.

The dontagain option can be seen as a method to make our script less verbose without much efforces.

To work correctly it requires a file where the choice will be saved and, separated by a colon : char, a string to identify the key as the choice itself:

$ kdialog --msgbox 'This is a boring message' --dontagain 'myapp/configs:boring_message'

Once the checkbox is checked and the Ok button pressed, to save the preference the file ~/.config/myapp/configs will be created with an ini structure like this:

[Notification Messages]
boring_message=false

Typing again the same command above the dialog won’t appear anymore until the boring_message value is removed:

[Notification Messages]
boring_message=

This requires the kwriteconfig utility:

$ kwriteconfig --file "$HOME/.config/myapp/configs" --group 'Notification Messages' --key 'boring_message' ''

Notice that the last value is an empty string '' and it is mandatory to unset the boring_message key, setting the value to true won’t accomplish the dialog block removal.

Final thoughts

In this first part, I have shown the whole set of Kdialog’s output dialogs, next time we will see how to handle the input dialogs and how to use them within our scripts or inside a terminal.


Creative Commons License This work is licensed under a Creative Commons Attribution-NoCommercial-ShareAlike 4.0 International License


Follow-up articles


Leave a Comment