Home > Mobile >  How to check system tray availability programmatically?
How to check system tray availability programmatically?

Time:07-05

Upon launching QSyncthingTray without any panel/trayer running, it displays an error dialog:

I couldn't detect any system tray on this system.
                                         [ OK ]

and exits upon pressing [ OK ].

Curiously though, if I have QSyncthingTray already running as a tray icon and kill my panel, QSyncthingTray doesn't quit, and upon restarting the panel QSyncthingTray is still there...

For my ~/.xsession & ~/.xinitrc startup, I'd like to add a looped check whether a tray is present, pseudocode:

loop:
  if system-tray.is-available:
    run QSyncthingTray
    done
  else sleep 1

How do I do the if system-tray.is-available?
The preferable language is Perl, also interested in Raku and POSIX sh. Other languages are welcome as well.

CodePudding user response:

QSyncthingTray uses isSystemTrayAvailable() from Qt c lib.

You can do the same.

is_tray_available.cpp:

// program that exits with zero code when tray is available
// when not available (or program crashes) it should return non-zero

#include <QApplication>
#include <QSystemTrayIcon>

int main(int argc, char** argv)
{
  QApplication app(argc, argv);
  if(QSystemTrayIcon::isSystemTrayAvailable()) return 0;
  else return 1;
}

QT is retarded and you need to use fullblown build system to build programs using it

is_tray_available.pro:

QT  = widgets
TARGET = is_tray_available
SOURCES  = is_tray_available.cpp

save both files in the same dir, execute qmake, make and voilà, you have executable that you can use in shellscript

  • Related