| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/settings/settingsnodejs.h" |
| 4 | |
| 5 | #include "definitions/definitions.h" |
| 6 | #include "exceptions/applicationexception.h" |
| 7 | #include "gui/dialogs/filedialog.h" |
| 8 | #include "miscellaneous/application.h" |
| 9 | #include "miscellaneous/iconfactory.h" |
| 10 | #include "miscellaneous/nodejs.h" |
| 11 | #include "network-web/webfactory.h" |
| 12 | |
| 13 | #include <QDir> |
| 14 | |
| 15 | SettingsNodejs::SettingsNodejs(Settings* settings, QWidget* parent) : SettingsPanel(settings, parent) { |
| 16 | m_ui.setupUi(this); |
| 17 | |
| 18 | m_ui.m_helpInfo->setHelpText(tr("What is Node.js?"), |
| 19 | tr("Node.js is asynchronous event-driven JavaScript runtime, designed to build " |
| 20 | "scalable network applications.\n\n" |
| 21 | "%1 integrates Node.js to bring some modern features like Adblock.\n\n" |
| 22 | "Note that usually all required Node.js tools should be available via your \"PATH\" " |
| 23 | "environment variable, so you do not have to specify full paths.\n\n" |
| 24 | "Also, relaunch \"Settings\" dialog after you install Node.js.") |
| 25 | .arg(APP_NAME), |
| 26 | false); |
| 27 | |
| 28 | m_ui.m_helpPackages |
| 29 | ->setHelpText(tr("%1 automatically installs some Node.js packages so that you do not have to. %1 does not " |
| 30 | "use global package folder because that requires administrator rights, therefore by default " |
| 31 | "it uses subfolder placed in your \"user data\" folder.") |
| 32 | .arg(APP_NAME), |
| 33 | false); |
| 34 | |
| 35 | connect(m_ui.m_btnDownloadNodejs, &QPushButton::clicked, this, [this]() { |
| 36 | qApp->web()->openUrlInExternalBrowser(QSL("https://nodejs.org/en/download/")); |
| 37 | }); |
| 38 | |
| 39 | connect(m_ui.m_tbNodeExecutable->lineEdit(), &BaseLineEdit::textChanged, this, &SettingsNodejs::testNodejs); |
| 40 | connect(m_ui.m_tbNpmExecutable->lineEdit(), &BaseLineEdit::textChanged, this, &SettingsNodejs::testNpm); |
| 41 | connect(m_ui.m_tbPackageFolder->lineEdit(), &BaseLineEdit::textChanged, this, &SettingsNodejs::testPackageFolder); |
| 42 | |
| 43 | connect(m_ui.m_tbNodeExecutable->lineEdit(), &BaseLineEdit::textChanged, this, &SettingsNodejs::dirtifySettings); |
| 44 | connect(m_ui.m_tbNpmExecutable->lineEdit(), &BaseLineEdit::textChanged, this, &SettingsNodejs::dirtifySettings); |
| 45 | connect(m_ui.m_tbPackageFolder->lineEdit(), &BaseLineEdit::textChanged, this, &SettingsNodejs::dirtifySettings); |
| 46 | |
| 47 | connect(m_ui.m_btnPackageFolder, &QPushButton::clicked, this, [this]() { |
| 48 | changeFileFolder(m_ui.m_tbPackageFolder, true); |
| 49 | }); |
| 50 | connect(m_ui.m_btnNodeExecutable, &QPushButton::clicked, this, [this]() { |
| 51 | changeFileFolder(m_ui.m_tbNodeExecutable, false, QSL("Node.js (node*)")); |
| 52 | }); |
| 53 | connect(m_ui.m_btnNpmExecutable, &QPushButton::clicked, this, [this]() { |
| 54 | changeFileFolder(m_ui.m_tbNpmExecutable, false, QSL("NPM (npm*)")); |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | QIcon SettingsNodejs::icon() const { |
| 59 | return qApp->icons()->fromTheme(QSL("node-join"), QSL("node")); |
| 60 | } |
| 61 | |
| 62 | void SettingsNodejs::changeFileFolder(LineEditWithStatus* tb, bool directory_select, const QString& file_filter) { |
| 63 | QString file_dir; |
| 64 | QString current = qApp->replaceUserDataFolderPlaceholder(tb->lineEdit()->text()); |
| 65 | |
| 66 | if (directory_select) { |
| 67 | file_dir = FileDialog::existingDirectory(this, {}, current, GENERAL_REMEMBERED_PATH); |
| 68 | } |
| 69 | else { |
| 70 | file_dir = FileDialog::openFileName(this, {}, current, file_filter, {}, GENERAL_REMEMBERED_PATH); |
| 71 | } |
| 72 | |
| 73 | if (!file_dir.isEmpty()) { |
| 74 | tb->lineEdit()->setText(QDir::toNativeSeparators(file_dir)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | QString SettingsNodejs::title() const { |
| 79 | return QSL("Node.js"); |
| 80 | } |
| 81 | |
| 82 | void SettingsNodejs::loadSettings() { |
| 83 | onBeginLoadSettings(); |
| 84 | |
| 85 | m_ui.m_tbNodeExecutable->lineEdit()->setText(qApp->nodejs()->nodeJsExecutable()); |
| 86 | m_ui.m_tbNpmExecutable->lineEdit()->setText(qApp->nodejs()->npmExecutable()); |
| 87 | m_ui.m_tbPackageFolder->lineEdit()->setText(qApp->nodejs()->packageFolder()); |
| 88 | |
| 89 | onEndLoadSettings(); |
| 90 | } |
| 91 | |
| 92 | void SettingsNodejs::saveSettings() { |
| 93 | onBeginSaveSettings(); |
| 94 | |
| 95 | qApp->nodejs()->setNodeJsExecutable(m_ui.m_tbNodeExecutable->lineEdit()->text()); |
| 96 | qApp->nodejs()->setNpmExecutable(m_ui.m_tbNpmExecutable->lineEdit()->text()); |
| 97 | qApp->nodejs()->setPackageFolder(m_ui.m_tbPackageFolder->lineEdit()->text()); |
| 98 | |
| 99 | onEndSaveSettings(); |
| 100 | } |
| 101 | |
| 102 | void SettingsNodejs::testNodejs() { |
| 103 | try { |
| 104 | QString node_version = qApp->nodejs()->nodeJsVersion(m_ui.m_tbNodeExecutable->lineEdit()->text()); |
| 105 | |
| 106 | m_ui.m_tbNodeExecutable->setStatus(WidgetWithStatus::StatusType::Ok, |
| 107 | tr("Node.js has version %1.").arg(node_version)); |
| 108 | } |
| 109 | catch (const ApplicationException& ex) { |
| 110 | m_ui.m_tbNodeExecutable->setStatus(WidgetWithStatus::StatusType::Error, QSL("Node.js: %1.").arg(ex.message())); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void SettingsNodejs::testNpm() { |
| 115 | try { |
| 116 | QString npm_version = qApp->nodejs()->npmVersion(m_ui.m_tbNpmExecutable->lineEdit()->text()); |
| 117 | |
| 118 | m_ui.m_tbNpmExecutable->setStatus(WidgetWithStatus::StatusType::Ok, tr("NPM has version %1.").arg(npm_version)); |
| 119 | } |
| 120 | catch (const ApplicationException& ex) { |
| 121 | m_ui.m_tbNpmExecutable->setStatus(WidgetWithStatus::StatusType::Error, QSL("NPM: %1.").arg(ex.message())); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void SettingsNodejs::testPackageFolder() { |
| 126 | QString folder = qApp->replaceUserDataFolderPlaceholder(m_ui.m_tbPackageFolder->lineEdit()->text()); |
| 127 | |
| 128 | const auto fi = QFileInfo(folder); |
| 129 | const auto is_file = fi.isFile() && fi.exists(); |
| 130 | QString desc; |
| 131 | WidgetWithStatus::StatusType stat; |
| 132 | |
| 133 | if (is_file) { |
| 134 | stat = WidgetWithStatus::StatusType::Error; |
| 135 | desc = tr("You cannot choose file, you have to choose FOLDER."); |
| 136 | } |
| 137 | else if (QDir().exists(folder)) { |
| 138 | stat = WidgetWithStatus::StatusType::Ok; |
| 139 | desc = tr("Package folder is OK."); |
| 140 | } |
| 141 | else { |
| 142 | stat = WidgetWithStatus::StatusType::Ok; |
| 143 | desc = tr("Package folder will be created!"); |
| 144 | } |
| 145 | |
| 146 | m_ui.m_tbPackageFolder->setStatus(stat, desc); |
| 147 | } |