Skip to content

Commit

Permalink
Change Enable/Disable Autostart action according to situation
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianTM committed Aug 10, 2024
1 parent 567ab88 commit 029f2cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
27 changes: 22 additions & 5 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ void MainWindow::createActions()
helpAction = new QAction(QIcon::fromTheme("help-browser"), tr("Help"), this);
listDevicesAction = new QAction(QIcon::fromTheme("drive-removable-media"), tr("List Devices"), this);
quitAction = new QAction(QIcon::fromTheme("gtk-quit"), tr("Quit"), this);
toggleAutostartAction = new QAction(QIcon::fromTheme("preferences-system"), tr("Enable Autostart?"), this);
QString autostartFile = QDir::homePath() + "/.config/autostart/mx-usb-unmounter.desktop";
toggleAutostartAction
= new QAction(QIcon::fromTheme("preferences-system"),
tr(QFile::exists(autostartFile) ? "Disable Autostart" : "Enable Autostart"), this);

connect(aboutAction, &QAction::triggered, this, &MainWindow::about);
connect(helpAction, &QAction::triggered, this, &MainWindow::help);
Expand Down Expand Up @@ -302,12 +305,26 @@ void MainWindow::setPosition()

void MainWindow::toggleAutostart()
{
QString local_file = QDir::homePath() + "/.config/autostart/mx-usb-unmounter.desktop";
if (QMessageBox::Yes == QMessageBox::question(nullptr, tr("Autostart Settings"), tr("Enable Autostart?"))) {
QFile::remove(local_file);
const QString autostartFile = QDir::homePath() + "/.config/autostart/mx-usb-unmounter.desktop";
const bool autostartEnabled = QFile::exists(autostartFile);

QString message;
QString title;
bool success = false;

if (autostartEnabled) {
success = QFile::remove(autostartFile);
message = success ? tr("Autostart has been disabled.") : tr("Failed to disable autostart.");
title = tr("Autostart Disabled");
toggleAutostartAction->setText(tr("Enable Autostart"));
} else {
QFile::copy("/usr/share/mx-usb-unmounter/mx-usb-unmounter.desktop", local_file);
success = QFile::copy("/usr/share/mx-usb-unmounter/mx-usb-unmounter.desktop", autostartFile);
message = success ? tr("Autostart has been enabled.") : tr("Failed to enable autostart.");
title = tr("Autostart Enabled");
toggleAutostartAction->setText(tr("Disable Autostart"));
}

QMessageBox::information(this, title, message);
}

// implement change event that closes app when window loses focus
Expand Down
2 changes: 1 addition & 1 deletion mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ private slots:
QSystemTrayIcon *trayIcon {};

static void help();
static void toggleAutostart();
void createActions();
void createMenu();
void setPosition();
void toggleAutostart();
};

0 comments on commit 029f2cf

Please sign in to comment.