Skip to content

Commit

Permalink
added queueScript functionality
Browse files Browse the repository at this point in the history
after last queued task is finished user defined script (in preferences) is executed - can be used for e.g. hibernating computer when transfers are done
  • Loading branch information
kapitainsky committed Mar 10, 2020
1 parent d04dadd commit e7bd07d
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 87 deletions.
12 changes: 12 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ int main(int argc, char *argv[]) {
settings->setValue("Settings/remoteType", "main");
};

// during first run the queueScript key might not exist
if (!(settings->contains("Settings/queueScript"))) {
// if queueScript does not exist create new key
settings->setValue("Settings/queueScript", "");
};

// during first run the queueScriptRun key might not exist
if (!(settings->contains("Settings/queueScriptRun"))) {
// if queueScriptRun does not exist create new key
settings->setValue("Settings/queueScriptRun", "false");
};

// set application font size
int fontsize = 0;

Expand Down
62 changes: 62 additions & 0 deletions src/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ MainWindow::MainWindow() {
}

auto settings = GetSettings();
ui.queueScriptRun->setChecked(
(settings->value("Settings/queueScriptRun").toBool()));

#if defined(Q_OS_WIN)
// disable "?" WindowContextHelpButton
Expand Down Expand Up @@ -404,6 +406,8 @@ MainWindow::MainWindow() {
dialog.getDefaultUploadOptions().trimmed());
settings->setValue("Settings/defaultRcloneOptions",
dialog.getDefaultRcloneOptions().trimmed());
settings->setValue("Settings/queueScript",
dialog.getQueueScript().trimmed());

settings->setValue("Settings/checkRcloneBrowserUpdates",
dialog.getCheckRcloneBrowserUpdates());
Expand Down Expand Up @@ -441,6 +445,16 @@ MainWindow::MainWindow() {
dialog.getHttpsProxy().trimmed());
settings->setValue("Settings/no_proxy", dialog.getNoProxy().trimmed());

// set queueScriptRun tooltip
QString queueScriptRunToolTip =
QString("Run script defined in preferences after all queue "
"processing finishes.\n\n"
"Can be used for example to hibernate your computer.\n\n") +
QString("Script set in preferences: ") + QString("\"") +
QString(settings->value("Settings/queueScript", false).toString()) +
QString("\"");
ui.queueScriptRun->setToolTip(queueScriptRunToolTip);

SetRclone(dialog.getRclone());
SetRcloneConf(dialog.getRcloneConf());
mFirstTime = true;
Expand All @@ -454,6 +468,13 @@ MainWindow::MainWindow() {
}
});

QObject::connect(ui.actionQueueScriptRun, &QAction::triggered, this, [=]() {
// remember in settings queueScriptRun checkbox state
auto settings = GetSettings();
settings->setValue("Settings/queueScriptRun",
ui.queueScriptRun->isChecked());
});

// intercept tab closure
MainWindow::connect(ui.tabs, SIGNAL(tabCloseRequested(int)), this,
SLOT(slotCloseTab(int)));
Expand Down Expand Up @@ -2195,6 +2216,17 @@ void MainWindow::addTasksToQueue() {

ui.queueListWidget->clear();

// set queueScriptRun tooltip
auto settings = GetSettings();
QString queueScriptRunToolTip =
QString("Run script defined in preferences after all queue processing "
"finishes.\n\n"
"Can be used for example to hibernate your computer.\n\n") +
QString("Script set in preferences: ") + QString("\"") +
QString(settings->value("Settings/queueScript", false).toString()) +
QString("\"");
ui.queueScriptRun->setToolTip(queueScriptRunToolTip);

auto items = ui.tasksListWidget->selectedItems();

ListOfJobOptions *ljo = ListOfJobOptions::getInstance();
Expand Down Expand Up @@ -2560,6 +2592,19 @@ void MainWindow::addTransfer(const QString &message, const QString &source,
if (mQueueCount == 0) {
ui.tabs->setTabText(3,
QString("Queue (%1)>>(0)").arg(mQueueCount));
// run queueScript
auto settings = GetSettings();
bool queueScriptRun =
settings->value("Settings/queueScriptRun", false).toBool();

if (queueScriptRun) {
QString queueScript =
settings->value("Settings/queueScript", false).toString();
if (!queueScript.isEmpty()) {
runQueueScript(queueScript);
}
}

} else {
ui.tabs->setTabText(
3, QString("Queue (%1)>>(1)").arg(mQueueCount - 1));
Expand Down Expand Up @@ -2635,6 +2680,23 @@ void MainWindow::addTransfer(const QString &message, const QString &source,
transfer->start(GetRclone(), GetRcloneConf() + args, QIODevice::ReadOnly);
}

// runs queueScript
void MainWindow::runQueueScript(const QString &script) {

QProcess *p = new QProcess();

QObject::connect(p,
static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(
&QProcess::finished),
this, [=](int code, QProcess::ExitStatus) {
if (code == 0) {
}
p->deleteLater();
});

p->start(script);
}

void MainWindow::addMount(const QString &remote, const QString &folder,
const QString &remoteType) {
QProcess *mount = new QProcess(this);
Expand Down
2 changes: 2 additions & 0 deletions src/main_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ private slots:
void addStream(const QString &remote, const QString &stream,
const QString &remoteType);

void runQueueScript(const QString &script);

void slotCloseTab(int index);

void saveQueueFile(void);
Expand Down
28 changes: 28 additions & 0 deletions src/main_window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,13 @@
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="queueScriptRun">
<property name="text">
<string>Run script when finished</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down Expand Up @@ -811,6 +818,11 @@
<string>Move task down in the queue</string>
</property>
</action>
<action name="actionQueueScriptRun">
<property name="text">
<string>queueScriptRun</string>
</property>
</action>
</widget>
<tabstops>
<tabstop>tabs</tabstop>
Expand All @@ -835,5 +847,21 @@
</hint>
</hints>
</connection>
<connection>
<sender>queueScriptRun</sender>
<signal>toggled(bool)</signal>
<receiver>actionQueueScriptRun</receiver>
<slot>trigger()</slot>
<hints>
<hint type="sourcelabel">
<x>567</x>
<y>73</y>
</hint>
<hint type="destinationlabel">
<x>-1</x>
<y>-1</y>
</hint>
</hints>
</connection>
</connections>
</ui>
29 changes: 29 additions & 0 deletions src/preferences_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ PreferencesDialog::PreferencesDialog(QWidget *parent) : QDialog(parent) {
ui.defaultUploadDir->setText(defaultUploadDir);
});

QObject::connect(ui.queueScriptBrowse, &QPushButton::clicked, this, [=]() {
QString queueScript = QFileDialog::getOpenFileName(this, "Select script",
ui.queueScript->text());
if (queueScript.isEmpty()) {
return;
}

if (!QFileInfo(queueScript).isExecutable()) {
QMessageBox::critical(
this, "Error", QString("File %1 is not executable").arg(queueScript));
return;
}

if (QFileInfo(queueScript) == QFileInfo(qApp->applicationFilePath())) {
QMessageBox::critical(this, "Error",
"You selected RcloneBrowser executable!\nPlease "
"select your script executable instead.");
return;
}

ui.queueScript->setText(queueScript);
});

auto settings = GetSettings();
ui.rclone->setText(
QDir::toNativeSeparators(settings->value("Settings/rclone").toString()));
Expand Down Expand Up @@ -119,6 +142,8 @@ PreferencesDialog::PreferencesDialog(QWidget *parent) : QDialog(parent) {
settings->value("Settings/defaultUploadOptions").toString());
ui.defaultRcloneOptions->setText(
settings->value("Settings/defaultRcloneOptions").toString());
ui.queueScript->setText(QDir::toNativeSeparators(
settings->value("Settings/queueScript").toString()));

ui.checkRcloneBrowserUpdates->setChecked(
settings->value("Settings/checkRcloneBrowserUpdates", true).toBool());
Expand Down Expand Up @@ -305,6 +330,10 @@ QString PreferencesDialog::getDefaultRcloneOptions() const {
return ui.defaultRcloneOptions->text();
}

QString PreferencesDialog::getQueueScript() const {
return ui.queueScript->text();
}

bool PreferencesDialog::getCheckRcloneBrowserUpdates() const {
return ui.checkRcloneBrowserUpdates->isChecked();
}
Expand Down
1 change: 1 addition & 0 deletions src/preferences_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PreferencesDialog : public QDialog {
QString getDefaultDownloadOptions() const;
QString getDefaultUploadOptions() const;
QString getDefaultRcloneOptions() const;
QString getQueueScript() const;

bool getCheckRcloneBrowserUpdates() const;
bool getCheckRcloneUpdates() const;
Expand Down
Loading

0 comments on commit e7bd07d

Please sign in to comment.