1 /*****************************************************************************
2  * Copyright (C) 2000 Shie Erlich <krusader@users.sourceforge.net>           *
3  * Copyright (C) 2000 Rafi Yanai <krusader@users.sourceforge.net>            *
4  * Copyright (C) 2004-2019 Krusader Krew [https://krusader.org]              *
5  *                                                                           *
6  * This file is part of Krusader [https://krusader.org].                     *
7  *                                                                           *
8  * Krusader is free software: you can redistribute it and/or modify          *
9  * it under the terms of the GNU General Public License as published by      *
10  * the Free Software Foundation, either version 2 of the License, or         *
11  * (at your option) any later version.                                       *
12  *                                                                           *
13  * Krusader is distributed in the hope that it will be useful,               *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
16  * GNU General Public License for more details.                              *
17  *                                                                           *
18  * You should have received a copy of the GNU General Public License         *
19  * along with Krusader.  If not, see [http://www.gnu.org/licenses/].         *
20  *****************************************************************************/
21 
22 #include "krspwidgets.h"
23 #include "../krglobal.h"
24 #include "../icon.h"
25 #include "../Filter/filtertabs.h"
26 #include "../GUI/krlistwidget.h"
27 
28 // QtCore
29 #include <QEvent>
30 // QtGui
31 #include <QBitmap>
32 // QtWidgets
33 #include <QComboBox>
34 #include <QLabel>
35 #include <QLineEdit>
36 #include <QCheckBox>
37 #include <QSpinBox>
38 #include <qnamespace.h>		// missing ?
39 
40 #include <KCompletion/KComboBox>
41 #include <KCompletion/KHistoryComboBox>
42 #include <KConfigCore/KSharedConfig>
43 #include <KI18n/KLocalizedString>
44 #include <KWidgetsAddons/KCursor>
45 
46 ///////////////////// initiation of the static members ////////////////////////
47 QStringList KRSpWidgets::maskList;
48 
49 ///////////////////////////////////////////////////////////////////////////////
50 
KRSpWidgets()51 KRSpWidgets::KRSpWidgets()
52 {
53 }
54 
getMask(QString caption,bool nameOnly,QWidget * parent)55 KRQuery KRSpWidgets::getMask(QString caption, bool nameOnly, QWidget * parent)
56 {
57     if (!nameOnly) {
58         return FilterTabs::getQuery(parent);
59     } else {
60         QPointer<KRMaskChoiceSub> p = new KRMaskChoiceSub(parent);
61         p->setWindowTitle(caption);
62         p->exec();
63         QString selection = p->selection->currentText();
64         delete p;
65         if (selection.isEmpty()) {
66             return KRQuery();
67         } else {
68             return KRQuery(selection);
69         }
70     }
71 }
72 
73 /////////////////////////// newFTP ////////////////////////////////////////
newFTP()74 QUrl KRSpWidgets::newFTP()
75 {
76     QPointer<newFTPSub> p = new newFTPSub();
77     p->exec();
78     QString uri = p->url->currentText();
79     if (uri.isEmpty()) {
80         delete p;
81         return QUrl(); // empty url
82     }
83 
84     QString protocol = p->prefix->currentText();
85     protocol.truncate(protocol.length() - 3); // remove the trailing ://
86 
87     QString username = p->username->text().simplified();
88     QString password = p->password->text().simplified();
89 
90     int uriStart = uri.lastIndexOf('@'); /* lets the user enter user and password in the URI field */
91     if (uriStart != -1) {
92         QString uriUser = uri.left(uriStart);
93         QString uriPsw;
94         uri = uri.mid(uriStart + 1);
95 
96         int pswStart = uriUser.indexOf(':'); /* getting the password name from the URL */
97         if (pswStart != -1) {
98             uriPsw = uriUser.mid(pswStart + 1);
99             uriUser = uriUser.left(pswStart);
100         }
101 
102         if (!uriUser.isEmpty()) { /* handling the ftp proxy username and password also */
103             username = username.isEmpty() ? uriUser : username + '@' + uriUser;
104         }
105 
106         if (!uriPsw.isEmpty()) { /* handling the ftp proxy username and password also */
107             password = password.isEmpty() ? uriPsw : password + '@' + uriPsw;
108         }
109     }
110 
111     QString host = uri; /* separating the hostname and path from the uri */
112     QString path;
113     int pathStart = uri.indexOf("/");
114     if (pathStart != -1) {
115         path = host.mid(pathStart);
116         host = host.left(pathStart);
117     }
118 
119     /* setting the parameters of the URL */
120     QUrl url;
121     url.setScheme(protocol);
122     url.setHost(host);
123     url.setPath(path);
124     if (protocol == "ftp" || protocol == "fish" || protocol == "sftp") {
125         url.setPort(p->port->cleanText().toInt());
126     }
127     if (!username.isEmpty()) {
128         url.setUserName(username);
129     }
130     if (!password.isEmpty()) {
131         url.setPassword(password);
132     }
133 
134     delete p;
135     return url;
136 }
137 
newFTPSub()138 newFTPSub::newFTPSub() : newFTPGUI(0)
139 {
140     url->setFocus();
141     setGeometry(krMainWindow->x() + krMainWindow->width() / 2 - width() / 2,
142                 krMainWindow->y() + krMainWindow->height() / 2 - height() / 2, width(), height());
143 }
144 
accept()145 void newFTPSub::accept()
146 {
147     url->addToHistory(url->currentText());
148     // save the history and completion list when the history combo is
149     // destroyed
150     KConfigGroup group(krConfig, "Private");
151     QStringList list = url->completionObject()->items();
152     group.writeEntry("newFTP Completion list", list);
153     list = url->historyItems();
154     group.writeEntry("newFTP History list", list);
155     QString protocol = prefix->currentText();
156     group.writeEntry("newFTP Protocol", protocol);
157 
158     newFTPGUI::accept();
159 }
160 
reject()161 void newFTPSub::reject()
162 {
163     url->lineEdit()->setText("");
164     newFTPGUI::reject();
165 }
166 
167 /////////////////////////// KRMaskChoiceSub ///////////////////////////////
KRMaskChoiceSub(QWidget * parent)168 KRMaskChoiceSub::KRMaskChoiceSub(QWidget * parent) : KRMaskChoice(parent)
169 {
170     PixmapLabel1->setPixmap(Icon("edit-select").pixmap(32));
171     label->setText(i18n("Enter a selection:"));
172     // the predefined selections list
173     KConfigGroup group(krConfig, "Private");
174     QStringList lst = group.readEntry("Predefined Selections", QStringList());
175     if (lst.size() > 0) preSelections->addItems(lst);
176     // the combo-box tweaks
177     selection->setDuplicatesEnabled(false);
178     selection->addItems(KRSpWidgets::maskList);
179     selection->lineEdit()->setText("*");
180     selection->lineEdit()->selectAll();
181     selection->setFocus();
182 }
183 
reject()184 void KRMaskChoiceSub::reject()
185 {
186     selection->clear();
187     KRMaskChoice::reject();
188 }
189 
accept()190 void KRMaskChoiceSub::accept()
191 {
192     bool add = true;
193     // make sure we don't have that already
194     for (int i = 0; i != KRSpWidgets::maskList.count(); i++)
195         if (KRSpWidgets::maskList[ i ].simplified() == selection->currentText().simplified()) {
196             // break if we found one such as this
197             add = false;
198             break;
199         }
200 
201     if (add)
202         KRSpWidgets::maskList.insert(0, selection->currentText().toLocal8Bit());
203     // write down the predefined selections list
204     QStringList list;
205 
206     for (int j = 0; j != preSelections->count(); j++) {
207         QListWidgetItem *i = preSelections->item(j);
208         list.append(i->text());
209     }
210 
211     KConfigGroup group(krConfig, "Private");
212     group.writeEntry("Predefined Selections", list);
213     KRMaskChoice::accept();
214 }
215 
addSelection()216 void KRMaskChoiceSub::addSelection()
217 {
218     QString temp = selection->currentText();
219     bool itemExists = false;
220 
221     // check if the selection already exists
222     for (int j = 0; j != preSelections->count(); j++) {
223         QListWidgetItem *i = preSelections->item(j);
224 
225         if (i->text() == temp) {
226             itemExists = true;
227             break;
228         }
229     }
230 
231     if (!temp.isEmpty() && !itemExists) {
232         preSelections->addItem(selection->currentText());
233         preSelections->update();
234     }
235 }
236 
deleteSelection()237 void KRMaskChoiceSub::deleteSelection()
238 {
239     delete preSelections->currentItem();
240     preSelections->update();
241 }
242 
clearSelections()243 void KRMaskChoiceSub::clearSelections()
244 {
245     preSelections->clear();
246     preSelections->update();
247 }
248 
acceptFromList(QListWidgetItem * i)249 void KRMaskChoiceSub::acceptFromList(QListWidgetItem *i)
250 {
251     selection->addItem(i->text(), 0);
252     accept();
253 }
254 
currentItemChanged(QListWidgetItem * i)255 void KRMaskChoiceSub::currentItemChanged(QListWidgetItem *i)
256 {
257     if (i)
258         selection->setEditText(i->text());
259 }
260