File indexing completed on 2024-04-28 17:06:07

0001 /*
0002     SPDX-FileCopyrightText: 2004 Csaba Karai <krusader@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "kgdependencies.h"
0009 #include "../compat.h"
0010 #include "../krglobal.h"
0011 #include "../krservices.h"
0012 
0013 // QtCore
0014 #include <QUrl>
0015 // QtWidgets
0016 #include <QGridLayout>
0017 #include <QScrollArea>
0018 #include <QTabWidget>
0019 
0020 #include <KConfigCore/KSharedConfig>
0021 #include <KI18n/KLocalizedString>
0022 #include <KWidgetsAddons/KMessageBox>
0023 
0024 #define PAGE_GENERAL 0
0025 #define PAGE_PACKERS 1
0026 #define PAGE_CHECKSUM 2
0027 
0028 KgDependencies::KgDependencies(bool first, QWidget *parent)
0029     : KonfiguratorPage(first, parent)
0030 {
0031     auto *kgDependenciesLayout = new QGridLayout(this);
0032     kgDependenciesLayout->setSpacing(6);
0033 
0034     //  ---------------------------- GENERAL TAB -------------------------------------
0035     tabWidget = new QTabWidget(this);
0036 
0037     QWidget *general_tab = new QWidget(tabWidget);
0038     auto *general_scroll = new QScrollArea(tabWidget);
0039     general_scroll->setFrameStyle(QFrame::NoFrame);
0040     general_scroll->setWidget(general_tab); // this also sets scrollacrea as the new parent for widget
0041     general_scroll->setWidgetResizable(true); // let the widget use every space available
0042     tabWidget->addTab(general_scroll, i18n("General"));
0043 
0044     auto *pathsGrid = new QGridLayout(general_tab);
0045     pathsGrid->setSpacing(6);
0046     pathsGrid->setContentsMargins(11, 11, 11, 11);
0047     pathsGrid->setAlignment(Qt::AlignTop);
0048 
0049     addApplication("kget", pathsGrid, 0, general_tab, PAGE_GENERAL);
0050     addApplication("mailer", pathsGrid, 1, general_tab, PAGE_GENERAL);
0051     addApplication("diff utility", pathsGrid, 2, general_tab, PAGE_GENERAL);
0052     addApplication("krename", pathsGrid, 3, general_tab, PAGE_GENERAL);
0053     addApplication("locate", pathsGrid, 4, general_tab, PAGE_GENERAL);
0054     addApplication("mount", pathsGrid, 5, general_tab, PAGE_GENERAL);
0055     addApplication("umount", pathsGrid, 6, general_tab, PAGE_GENERAL);
0056     addApplication("updatedb", pathsGrid, 7, general_tab, PAGE_GENERAL);
0057 
0058     //  ---------------------------- PACKERS TAB -------------------------------------
0059     QWidget *packers_tab = new QWidget(tabWidget);
0060     auto *packers_scroll = new QScrollArea(tabWidget);
0061     packers_scroll->setFrameStyle(QFrame::NoFrame);
0062     packers_scroll->setWidget(packers_tab); // this also sets scrollacrea as the new parent for widget
0063     packers_scroll->setWidgetResizable(true); // let the widget use every space available
0064     tabWidget->addTab(packers_scroll, i18n("Packers"));
0065 
0066     auto *archGrid1 = new QGridLayout(packers_tab);
0067     archGrid1->setSpacing(6);
0068     archGrid1->setContentsMargins(11, 11, 11, 11);
0069     archGrid1->setAlignment(Qt::AlignTop);
0070 
0071     addApplication("7z", archGrid1, 0, packers_tab, PAGE_PACKERS, "7za");
0072     addApplication("arj", archGrid1, 1, packers_tab, PAGE_PACKERS);
0073     addApplication("bzip2", archGrid1, 2, packers_tab, PAGE_PACKERS);
0074     addApplication("cpio", archGrid1, 3, packers_tab, PAGE_PACKERS);
0075     addApplication("dpkg", archGrid1, 4, packers_tab, PAGE_PACKERS);
0076     addApplication("gzip", archGrid1, 5, packers_tab, PAGE_PACKERS);
0077     addApplication("lha", archGrid1, 6, packers_tab, PAGE_PACKERS);
0078     addApplication("lzma", archGrid1, 7, packers_tab, PAGE_PACKERS);
0079     addApplication("rar", archGrid1, 8, packers_tab, PAGE_PACKERS);
0080     addApplication("tar", archGrid1, 9, packers_tab, PAGE_PACKERS);
0081     addApplication("unace", archGrid1, 10, packers_tab, PAGE_PACKERS);
0082     addApplication("unarj", archGrid1, 11, packers_tab, PAGE_PACKERS);
0083     addApplication("unrar", archGrid1, 12, packers_tab, PAGE_PACKERS);
0084     addApplication("unzip", archGrid1, 13, packers_tab, PAGE_PACKERS);
0085     addApplication("zip", archGrid1, 14, packers_tab, PAGE_PACKERS);
0086     addApplication("xz", archGrid1, 15, packers_tab, PAGE_PACKERS);
0087 
0088     //  ---------------------------- CHECKSUM TAB -------------------------------------
0089     QWidget *checksum_tab = new QWidget(tabWidget);
0090     auto *checksum_scroll = new QScrollArea(tabWidget);
0091     checksum_scroll->setFrameStyle(QFrame::NoFrame);
0092     checksum_scroll->setWidget(checksum_tab); // this also sets scrollacrea as the new parent for widget
0093     checksum_scroll->setWidgetResizable(true); // let the widget use every space available
0094     tabWidget->addTab(checksum_scroll, i18n("Checksum Utilities"));
0095 
0096     auto *archGrid2 = new QGridLayout(checksum_tab);
0097     archGrid2->setSpacing(6);
0098     archGrid2->setContentsMargins(11, 11, 11, 11);
0099     archGrid2->setAlignment(Qt::AlignTop);
0100 
0101     addApplication("md5sum", archGrid2, 0, checksum_tab, PAGE_CHECKSUM);
0102     addApplication("sha1sum", archGrid2, 1, checksum_tab, PAGE_CHECKSUM);
0103     addApplication("sha224sum", archGrid2, 2, checksum_tab, PAGE_CHECKSUM);
0104     addApplication("sha256sum", archGrid2, 3, checksum_tab, PAGE_CHECKSUM);
0105     addApplication("sha384sum", archGrid2, 4, checksum_tab, PAGE_CHECKSUM);
0106     addApplication("sha512sum", archGrid2, 5, checksum_tab, PAGE_CHECKSUM);
0107 
0108     kgDependenciesLayout->addWidget(tabWidget, 0, 0);
0109 }
0110 
0111 void KgDependencies::addApplication(const QString &name, QGridLayout *grid, int row, QWidget *parent, int page, const QString &additionalList)
0112 {
0113     // try to autodetect the full path name
0114     QString defaultValue = KrServices::fullPathName(name);
0115 
0116     if (defaultValue.isEmpty()) {
0117         QStringList list = additionalList.split(',', SKIP_EMPTY_PARTS);
0118         for (int i = 0; i != list.count(); i++)
0119             if (!KrServices::fullPathName(list[i]).isEmpty()) {
0120                 defaultValue = KrServices::fullPathName(list[i]);
0121                 break;
0122             }
0123     }
0124 
0125     QLabel *labelPath = addLabel(grid, row, 0, name, parent);
0126 
0127     KonfiguratorURLRequester *fullPath = createURLRequester("Dependencies", name, defaultValue, labelPath, parent, false, QString(), page);
0128     connect(fullPath->extension(), &KonfiguratorExtension::applyManually, this, &KgDependencies::slotApply);
0129     grid->addWidget(fullPath, row, 1);
0130 }
0131 
0132 void KgDependencies::slotApply(QObject *obj, const QString &configGroup, const QString &name)
0133 {
0134     auto *urlRequester = qobject_cast<KonfiguratorURLRequester *>(obj);
0135 
0136     KConfigGroup group(krConfig, configGroup);
0137     group.writeEntry(name, urlRequester->url().toDisplayString(QUrl::PreferLocalFile));
0138 
0139     QString usedPath = KrServices::fullPathName(name);
0140 
0141     if (urlRequester->url().toDisplayString(QUrl::PreferLocalFile) != usedPath) {
0142         group.writeEntry(name, usedPath);
0143         if (usedPath.isEmpty())
0144             KMessageBox::error(this, i18n("The %1 path is incorrect, no valid path found.", urlRequester->url().toDisplayString(QUrl::PreferLocalFile)));
0145         else
0146             KMessageBox::error(this, i18n("The %1 path is incorrect, %2 used instead.", urlRequester->url().toDisplayString(QUrl::PreferLocalFile), usedPath));
0147         urlRequester->setUrl(QUrl::fromLocalFile(usedPath));
0148     }
0149 }
0150 
0151 int KgDependencies::activeSubPage()
0152 {
0153     return tabWidget->currentIndex();
0154 }