i wrote a qt application for linux. The application is supposed to run at startup- which i did with a desktop entry.
but i need it to be more complicated: there is a checkbox that the user supposed to check in order to choose whether the application will run at startup or not.
how do i suppose to save his prefference?
the application was wriiten for windows before and this was saved in the registry. i got from googling that i should save it in /etc.
what file should it be? how do i write it in my code? and could i add a condition to the desktop entry, or should i run some script?
i'm quite new in all this, so i will appriciate a detailed answer.
thank u.
For this particular case, saving a preference setting controlling whether the app should run at startup or not is completely pointless. The very existence of the autorun entry desktop file reflects the state of that preference. If that file exists, you check the checkbox. If the user unchecks the checkbox, you delete the file. If the user checks the checkbox, you create the file. That's it. Duplicating the setting in a preference store will only lead to bugs since now you have to keep the setting and the presence of the file in the file system in sync and you have to handle all sorts of corner cases.
Additionally, please keep in mind that /etc/xdg/autostart
is for system-wide autorun entries. If it's supposed to be a per-user setting, you should create the .desktop file in the user's autostart directory. To determine its location, please follow the Desktop Application Autostart Specification, which mandates that the location be $XDG_CONFIG_DIRS/autostart
, which typically resolves to the .config/autostart
directory in the user's home (however, if the XDG_CONFIG_DIRS
environment variable exists, you should resolve it by reading that value first then appending /autostart
to it.)
Here is an example program that will print out what you want:
#include <cstdlib>
#include <iostream>
#include <QtCore/QString>
#include <QtCore/QDir>
#ifndef Q_OS_UNIX
#error This method only makes sense on Unix, use OS-specific handling for other OSes.
#endif
QString getUserXdgConfigDir()
{
QString result(std::getenv("XDG_CONFIG_DIRS"));
if (result.isEmpty()) {
// XDG_CONFIG_DIRS is not defined, we'll use the default value
// as mandated by http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
result = QDir::homePath() + QDir::separator() + ".config";
}
return result;
}
QString getUserAutostartDir()
{
return getUserXdgConfigDir() + QDir::separator() + "autostart";
}
int main(int argc, char *argv[])
{
std::cout << "User config dir is " << getUserXdgConfigDir().toStdString() << std::endl;
std::cout << "User autostart dir is " << getUserAutostartDir().toStdString() << std::endl;
return 0;
}