1 /*
2  * Copyright (C) 2020 Damir Porobic <damir.porobic@gmx.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "ScriptUploaderSettings.h"
21 
ScriptUploaderSettings(KsnipConfig * ksnipConfig)22 ScriptUploaderSettings::ScriptUploaderSettings(KsnipConfig *ksnipConfig) :
23 	mConfig(ksnipConfig),
24 	mLayout(new QGridLayout(this)),
25 	mCopyOutputToClipboardCheckbox(new QCheckBox(this)),
26 	mStopOnStdErrCheckbox(new QCheckBox(this)),
27 	mCopyOutputFilterLabel(new QLabel(this)),
28 	mScriptPathLabel(new QLabel(this)),
29 	mCopyOutputFilterLineEdit(new QLineEdit(this)),
30 	mUploadScriptPathLineEdit(new QLineEdit(this)),
31 	mBrowseButton(new QPushButton(this)),
32 	mFileDialog(FileDialogAdapterFactory::create())
33 {
34 	initGui();
35 	loadConfig();
36 }
37 
~ScriptUploaderSettings()38 ScriptUploaderSettings::~ScriptUploaderSettings()
39 {
40 	delete mLayout;
41 	delete mCopyOutputToClipboardCheckbox;
42 	delete mStopOnStdErrCheckbox;
43 	delete mCopyOutputFilterLabel;
44 	delete mScriptPathLabel;
45 	delete mCopyOutputFilterLineEdit;
46 	delete mUploadScriptPathLineEdit;
47 	delete mBrowseButton;
48 }
49 
saveSettings()50 void ScriptUploaderSettings::saveSettings()
51 {
52 	mConfig->setUploadScriptStopOnStdErr(mStopOnStdErrCheckbox->isChecked());
53 	mConfig->setUploadScriptCopyOutputToClipboard(mCopyOutputToClipboardCheckbox->isChecked());
54 	mConfig->setUploadScriptCopyOutputFilter(mCopyOutputFilterLineEdit->text());
55 	mConfig->setUploadScriptPath(mUploadScriptPathLineEdit->text());
56 }
57 
initGui()58 void ScriptUploaderSettings::initGui()
59 {
60 	mStopOnStdErrCheckbox->setText(tr("Stop when upload script writes to StdErr"));
61 	mStopOnStdErrCheckbox->setToolTip(tr("Marks the upload as failed when script writes to StdErr.\n"
62 									        "Without this setting errors in the script will be unnoticed."));
63 
64 	mCopyOutputToClipboardCheckbox->setText(tr("Copy script output to clipboard"));
65 	connect(mCopyOutputToClipboardCheckbox, &QCheckBox::stateChanged, this, &ScriptUploaderSettings::copyToClipboardChanged);
66 
67 	mCopyOutputFilterLabel->setText(tr("Filter:"));
68 	mCopyOutputFilterLabel->setToolTip(tr("RegEx Expression. Only copy to clipboard what matches the RegEx Expression.\n"
69 									         "When omitted, everything is copied."));
70 
71 	mCopyOutputFilterLineEdit->setToolTip(mCopyOutputFilterLabel->toolTip());
72 
73 	mScriptPathLabel->setText(tr("Script:"));
74 	mScriptPathLabel->setToolTip(tr("Path to script that will be called for uploading. During upload the script will be called\n"
75 								 "with the path to a temporary png file as a single argument."));
76 
77 	mUploadScriptPathLineEdit->setToolTip(mScriptPathLabel->toolTip());
78 
79 	mBrowseButton->setText(tr("Browse"));
80 	connect(mBrowseButton, &QPushButton::clicked, this, &ScriptUploaderSettings::ShowScriptSelectionDialog);
81 
82 	mLayout->setAlignment(Qt::AlignTop);
83 	mLayout->setColumnMinimumWidth(0, 10);
84 	mLayout->addWidget(mStopOnStdErrCheckbox, 0, 0, 1, 3);
85 	mLayout->addWidget(mCopyOutputToClipboardCheckbox, 1, 0, 1, 3);
86 	mLayout->addWidget(mCopyOutputFilterLabel, 2, 1, 1, 1);
87 	mLayout->addWidget(mCopyOutputFilterLineEdit, 2, 2, 1, 1);
88 	mLayout->addWidget(mScriptPathLabel, 3, 0, 1, 1);
89 	mLayout->addWidget(mUploadScriptPathLineEdit, 3, 1, 1, 2);
90 	mLayout->addWidget(mBrowseButton, 3, 3, 1, 1);
91 
92 	setTitle(tr("Script Uploader"));
93 	setLayout(mLayout);
94 }
95 
loadConfig()96 void ScriptUploaderSettings::loadConfig()
97 {
98 	mStopOnStdErrCheckbox->setChecked(mConfig->uploadScriptStopOnStdErr());
99 	mCopyOutputToClipboardCheckbox->setChecked(mConfig->uploadScriptCopyOutputToClipboard());
100 	mCopyOutputFilterLineEdit->setText(mConfig->uploadScriptCopyOutputFilter());
101 	mUploadScriptPathLineEdit->setText(mConfig->uploadScriptPath());
102 
103 	copyToClipboardChanged();
104 }
105 
ShowScriptSelectionDialog()106 void ScriptUploaderSettings::ShowScriptSelectionDialog()
107 {
108 	auto path = mFileDialog->getOpenFileName(this, tr("Select Upload Script"), mConfig->uploadScriptPath());
109 	if(PathHelper::isPathValid(path)) {
110 		mUploadScriptPathLineEdit->setText(path);
111 	}
112 }
113 
copyToClipboardChanged()114 void ScriptUploaderSettings::copyToClipboardChanged()
115 {
116 	auto enabled = mCopyOutputToClipboardCheckbox->isChecked();
117 	mCopyOutputFilterLabel->setEnabled(enabled);
118 	mCopyOutputFilterLineEdit->setEnabled(enabled);
119 }
120