File indexing completed on 2024-04-28 17:05:54

0001 /*
0002     SPDX-FileCopyrightText: 2000 Shie Erlich <krusader@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2000 Rafi Yanai <krusader@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "packgui.h"
0010 #include "../defaults.h"
0011 #include "../krglobal.h"
0012 
0013 // QtCore
0014 #include <QStringList>
0015 // QtWidgets
0016 #include <QCheckBox>
0017 #include <QComboBox>
0018 #include <QFileDialog>
0019 #include <QLabel>
0020 #include <QLineEdit>
0021 #include <QPushButton>
0022 
0023 #include <KConfigCore/KSharedConfig>
0024 #include <KI18n/KLocalizedString>
0025 
0026 #include "../GUI/krhistorycombobox.h"
0027 
0028 #define PS(x) (lst.contains(x))
0029 
0030 // clear the statics first
0031 QString PackGUI::filename = nullptr;
0032 QString PackGUI::destination = nullptr;
0033 QString PackGUI::type = nullptr;
0034 QMap<QString, QString> PackGUI::extraProps;
0035 
0036 PackGUI::PackGUI(const QString &defaultName, const QString &defaultPath, int noOfFiles, const QString &filename)
0037     : PackGUIBase(nullptr)
0038 {
0039     // first, fill the WhatToPack textfield with information
0040     if (noOfFiles == 1)
0041         TextLabel1->setText(i18n("Pack %1", filename));
0042     else
0043         TextLabel1->setText(i18np("Pack %1 file", "Pack %1 files", noOfFiles));
0044 
0045     // now, according to the Konfigurator, fill the combobox with the information
0046     // about what kind of packing we can do
0047     KConfigGroup group(krConfig, "Archives");
0048     QStringList lst = group.readEntry("Supported Packers", QStringList());
0049     // now, clear the type combo and begin...
0050     typeData->clear();
0051     if (PS("tar"))
0052         typeData->addItem("tar");
0053     if (PS("tar") && PS("gzip"))
0054         typeData->addItem("tar.gz");
0055     if (PS("tar") && PS("bzip2"))
0056         typeData->addItem("tar.bz2");
0057     if (PS("tar") && PS("lzma"))
0058         typeData->addItem("tar.lzma");
0059     if (PS("tar") && PS("xz"))
0060         typeData->addItem("tar.xz");
0061     if (PS("zip"))
0062         typeData->addItem("zip");
0063     if (PS("zip"))
0064         typeData->addItem("cbz");
0065     if (PS("rar"))
0066         typeData->addItem("rar");
0067     if (PS("rar"))
0068         typeData->addItem("cbr");
0069     if (PS("lha"))
0070         typeData->addItem("lha");
0071     if (PS("arj"))
0072         typeData->addItem("arj");
0073     if (PS("7z"))
0074         typeData->addItem("7z");
0075     // set the last used packer as the top one
0076     QString tmp = group.readEntry("lastUsedPacker", QString());
0077     if (!tmp.isEmpty()) {
0078         for (int i = 0; i < typeData->count(); ++i)
0079             if (typeData->itemText(i) == tmp) {
0080                 typeData->removeItem(i);
0081                 typeData->insertItem(0, tmp);
0082                 typeData->setCurrentIndex(0);
0083                 break;
0084             }
0085     }
0086     checkConsistency();
0087 
0088     // and go on with the normal stuff
0089     dirData->setText(defaultPath);
0090     nameData->setText(defaultName);
0091     nameData->setFocus();
0092     if (typeData->count() == 0) // if no packers are available
0093         okButton->setEnabled(false);
0094     setGeometry(krMainWindow->x() + krMainWindow->width() / 2 - width() / 2, krMainWindow->y() + krMainWindow->height() / 2 - height() / 2, width(), height());
0095     exec();
0096 }
0097 
0098 void PackGUI::browse()
0099 {
0100     QString temp = QFileDialog::getExistingDirectory(nullptr, i18n("Please select a folder"), dirData->text());
0101     if (!temp.isEmpty()) {
0102         dirData->setText(temp);
0103     }
0104 }
0105 
0106 void PackGUI::accept()
0107 {
0108     if (!extraProperties(extraProps))
0109         return;
0110 
0111     filename = nameData->text();
0112     destination = dirData->text();
0113     type = typeData->currentText();
0114     // write down the packer chosen, to be lastUsedPacker
0115     KConfigGroup group(krConfig, "Archives");
0116     group.writeEntry("lastUsedPacker", type);
0117 
0118     group.writeEntry("Command Line Switches", commandLineSwitches->historyItems());
0119     krConfig->sync();
0120     PackGUIBase::accept();
0121 }
0122 
0123 void PackGUI::reject()
0124 {
0125     filename.clear();
0126     destination.clear();
0127     type.clear();
0128     // If e.g. the user has deleted a command line switch from the list, that's
0129     // taken into account even if a file is not packed afterwards
0130     KConfigGroup group(krConfig, "Archives");
0131     group.writeEntry("Command Line Switches", commandLineSwitches->historyItems());
0132 
0133     PackGUIBase::reject();
0134 }