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 "krspwidgets.h"
0010 #include "../Filter/filtertabs.h"
0011 #include "../GUI/krlistwidget.h"
0012 #include "../icon.h"
0013 #include "../krglobal.h"
0014 
0015 // QtCore
0016 #include <QEvent>
0017 // QtGui
0018 #include <QBitmap>
0019 // QtWidgets
0020 #include <QCheckBox>
0021 #include <QComboBox>
0022 #include <QLabel>
0023 #include <QLineEdit>
0024 #include <QSpinBox>
0025 #include <qnamespace.h> // missing ?
0026 
0027 #include <KCompletion/KComboBox>
0028 #include <KConfigCore/KSharedConfig>
0029 #include <KI18n/KLocalizedString>
0030 #include <KWidgetsAddons/KCursor>
0031 
0032 ///////////////////// initiation of the static members ////////////////////////
0033 QStringList KrSpWidgets::maskList;
0034 
0035 ///////////////////////////////////////////////////////////////////////////////
0036 
0037 KrSpWidgets::KrSpWidgets() = default;
0038 
0039 KrQuery KrSpWidgets::getMask(const QString &caption, bool nameOnly, QWidget *parent)
0040 {
0041     if (!nameOnly) {
0042         return FilterTabs::getQuery(parent);
0043     } else {
0044         QPointer<KrMaskChoiceSub> p = new KrMaskChoiceSub(parent);
0045         p->setWindowTitle(caption);
0046         p->exec();
0047         QString selection = p->selection->currentText();
0048         delete p;
0049         if (selection.isEmpty()) {
0050             return KrQuery();
0051         } else {
0052             return KrQuery(selection);
0053         }
0054     }
0055 }
0056 
0057 /////////////////////////// newFTP ////////////////////////////////////////
0058 QUrl KrSpWidgets::newFTP()
0059 {
0060     QPointer<newFTPSub> p = new newFTPSub();
0061     p->exec();
0062     QString uri = p->url->currentText();
0063     if (uri.isEmpty()) {
0064         delete p;
0065         return QUrl(); // empty url
0066     }
0067 
0068     QString protocol = p->prefix->currentText();
0069     protocol.truncate(protocol.length() - 3); // remove the trailing ://
0070 
0071     QString username = p->username->text().simplified();
0072     QString password = p->password->text().simplified();
0073 
0074     int uriStart = uri.lastIndexOf('@'); /* lets the user enter user and password in the URI field */
0075     if (uriStart != -1) {
0076         QString uriUser = uri.left(uriStart);
0077         QString uriPsw;
0078         uri = uri.mid(uriStart + 1);
0079 
0080         int pswStart = uriUser.indexOf(':'); /* getting the password name from the URL */
0081         if (pswStart != -1) {
0082             uriPsw = uriUser.mid(pswStart + 1);
0083             uriUser = uriUser.left(pswStart);
0084         }
0085 
0086         if (!uriUser.isEmpty()) { /* handling the ftp proxy username and password also */
0087             username = username.isEmpty() ? uriUser : username + '@' + uriUser;
0088         }
0089 
0090         if (!uriPsw.isEmpty()) { /* handling the ftp proxy username and password also */
0091             password = password.isEmpty() ? uriPsw : password + '@' + uriPsw;
0092         }
0093     }
0094 
0095     QString host = uri; /* separating the hostname and path from the uri */
0096     QString path;
0097     int pathStart = uri.indexOf("/");
0098     if (pathStart != -1) {
0099         path = host.mid(pathStart);
0100         host = host.left(pathStart);
0101     }
0102 
0103     /* setting the parameters of the URL */
0104     QUrl url;
0105     url.setScheme(protocol);
0106     url.setHost(host);
0107     url.setPath(path);
0108     if (protocol == "ftp" || protocol == "fish" || protocol == "sftp") {
0109         url.setPort(p->port->cleanText().toInt());
0110     }
0111     if (!username.isEmpty()) {
0112         url.setUserName(username);
0113     }
0114     if (!password.isEmpty()) {
0115         url.setPassword(password);
0116     }
0117 
0118     delete p;
0119     return url;
0120 }
0121 
0122 newFTPSub::newFTPSub()
0123     : newFTPGUI(nullptr)
0124 {
0125     url->setFocus();
0126     setGeometry(krMainWindow->x() + krMainWindow->width() / 2 - width() / 2, krMainWindow->y() + krMainWindow->height() / 2 - height() / 2, width(), height());
0127 }
0128 
0129 void newFTPSub::accept()
0130 {
0131     url->addToHistory(url->currentText());
0132     newFTPGUI::accept();
0133 }
0134 
0135 void newFTPSub::reject()
0136 {
0137     url->lineEdit()->setText("");
0138     newFTPGUI::reject();
0139 }
0140 
0141 newFTPSub::~newFTPSub()
0142 {
0143     // Save the history and the completion list of the url comboBox
0144     KConfigGroup group(krConfig, "Private");
0145     QStringList list = url->completionObject()->items();
0146     group.writeEntry("newFTP Completion list", list);
0147     list = url->historyItems();
0148     group.writeEntry("newFTP History list", list);
0149     QString protocol = prefix->currentText();
0150     group.writeEntry("newFTP Protocol", protocol);
0151 }
0152 
0153 /////////////////////////// KrMaskChoiceSub ///////////////////////////////
0154 KrMaskChoiceSub::KrMaskChoiceSub(QWidget *parent)
0155     : KrMaskChoice(parent)
0156 {
0157     PixmapLabel1->setPixmap(Icon("edit-select").pixmap(32));
0158     label->setText(i18n("Enter a selection:"));
0159     // the predefined selections list
0160     KConfigGroup group(krConfig, "Private");
0161     QStringList lst = group.readEntry("Predefined Selections", QStringList());
0162     if (lst.size() > 0)
0163         preSelections->addItems(lst);
0164     // the combo-box tweaks
0165     selection->setDuplicatesEnabled(false);
0166     selection->addItems(KrSpWidgets::maskList);
0167     selection->lineEdit()->setText("*");
0168     selection->lineEdit()->selectAll();
0169     selection->setFocus();
0170 }
0171 
0172 void KrMaskChoiceSub::reject()
0173 {
0174     selection->clear();
0175     KrMaskChoice::reject();
0176 }
0177 
0178 void KrMaskChoiceSub::accept()
0179 {
0180     bool add = true;
0181     // make sure we don't have that already
0182     for (int i = 0; i != KrSpWidgets::maskList.count(); i++)
0183         if (KrSpWidgets::maskList[i].simplified() == selection->currentText().simplified()) {
0184             // break if we found one such as this
0185             add = false;
0186             break;
0187         }
0188 
0189     if (add)
0190         KrSpWidgets::maskList.insert(0, selection->currentText().toLocal8Bit());
0191     // write down the predefined selections list
0192     QStringList list;
0193 
0194     for (int j = 0; j != preSelections->count(); j++) {
0195         QListWidgetItem *i = preSelections->item(j);
0196         list.append(i->text());
0197     }
0198 
0199     KConfigGroup group(krConfig, "Private");
0200     group.writeEntry("Predefined Selections", list);
0201     KrMaskChoice::accept();
0202 }
0203 
0204 void KrMaskChoiceSub::addSelection()
0205 {
0206     QString temp = selection->currentText();
0207     bool itemExists = false;
0208 
0209     // check if the selection already exists
0210     for (int j = 0; j != preSelections->count(); j++) {
0211         QListWidgetItem *i = preSelections->item(j);
0212 
0213         if (i->text() == temp) {
0214             itemExists = true;
0215             break;
0216         }
0217     }
0218 
0219     if (!temp.isEmpty() && !itemExists) {
0220         preSelections->addItem(selection->currentText());
0221         preSelections->update();
0222     }
0223 }
0224 
0225 void KrMaskChoiceSub::deleteSelection()
0226 {
0227     delete preSelections->currentItem();
0228     preSelections->update();
0229 }
0230 
0231 void KrMaskChoiceSub::clearSelections()
0232 {
0233     preSelections->clear();
0234     preSelections->update();
0235 }
0236 
0237 void KrMaskChoiceSub::acceptFromList(QListWidgetItem *i)
0238 {
0239     selection->addItem(i->text(), 0);
0240     accept();
0241 }
0242 
0243 void KrMaskChoiceSub::currentItemChanged(QListWidgetItem *i)
0244 {
0245     if (i)
0246         selection->setEditText(i->text());
0247 }