File: krspwidgets.cpp

package info (click to toggle)
krusader 2%3A2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,448 kB
  • sloc: cpp: 56,112; ansic: 1,187; xml: 811; sh: 23; makefile: 3
file content (247 lines) | stat: -rw-r--r-- 6,941 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
    SPDX-FileCopyrightText: 2000 Shie Erlich <krusader@users.sourceforge.net>
    SPDX-FileCopyrightText: 2000 Rafi Yanai <krusader@users.sourceforge.net>
    SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "krspwidgets.h"
#include "../Filter/filtertabs.h"
#include "../GUI/krlistwidget.h"
#include "../icon.h"
#include "../krglobal.h"

// QtCore
#include <QEvent>
// QtGui
#include <QBitmap>
// QtWidgets
#include <QCheckBox>
#include <QComboBox>
#include <QLabel>
#include <QLineEdit>
#include <QSpinBox>
#include <qnamespace.h> // missing ?

#include <KComboBox>
#include <KCursor>
#include <KLocalizedString>
#include <KSharedConfig>

///////////////////// initiation of the static members ////////////////////////
QStringList KrSpWidgets::maskList;

///////////////////////////////////////////////////////////////////////////////

KrSpWidgets::KrSpWidgets() = default;

KrQuery KrSpWidgets::getMask(const QString &caption, bool nameOnly, QWidget *parent)
{
    if (!nameOnly) {
        return FilterTabs::getQuery(parent);
    } else {
        QPointer<KrMaskChoiceSub> p = new KrMaskChoiceSub(parent);
        p->setWindowTitle(caption);
        p->exec();
        QString selection = p->selection->currentText();
        delete p;
        if (selection.isEmpty()) {
            return KrQuery();
        } else {
            return KrQuery(selection);
        }
    }
}

/////////////////////////// newFTP ////////////////////////////////////////
QUrl KrSpWidgets::newFTP()
{
    QPointer<newFTPSub> p = new newFTPSub();
    p->exec();
    QString uri = p->url->currentText();
    if (uri.isEmpty()) {
        delete p;
        return QUrl(); // empty url
    }

    QString protocol = p->prefix->currentText();
    protocol.truncate(protocol.length() - 3); // remove the trailing ://

    QString username = p->username->text().simplified();
    QString password = p->password->text().simplified();

    qsizetype uriStart = uri.lastIndexOf('@'); /* lets the user enter user and password in the URI field */
    if (uriStart != -1) {
        QString uriUser = uri.left(uriStart);
        QString uriPsw;
        uri = uri.mid(uriStart + 1);

        qsizetype pswStart = uriUser.indexOf(':'); /* getting the password name from the URL */
        if (pswStart != -1) {
            uriPsw = uriUser.mid(pswStart + 1);
            uriUser = uriUser.left(pswStart);
        }

        if (!uriUser.isEmpty()) { /* handling the ftp proxy username and password also */
            username = username.isEmpty() ? uriUser : username + '@' + uriUser;
        }

        if (!uriPsw.isEmpty()) { /* handling the ftp proxy username and password also */
            password = password.isEmpty() ? uriPsw : password + '@' + uriPsw;
        }
    }

    QString host = uri; /* separating the hostname and path from the uri */
    QString path;
    qsizetype pathStart = uri.indexOf("/");
    if (pathStart != -1) {
        path = host.mid(pathStart);
        host = host.left(pathStart);
    }

    /* setting the parameters of the URL */
    QUrl url;
    url.setScheme(protocol);
    url.setHost(host);
    url.setPath(path);
    if (protocol == "ftp" || protocol == "fish" || protocol == "sftp") {
        url.setPort(p->port->cleanText().toInt());
    }
    if (!username.isEmpty()) {
        url.setUserName(username);
    }
    if (!password.isEmpty()) {
        url.setPassword(password);
    }

    delete p;
    return url;
}

newFTPSub::newFTPSub()
    : newFTPGUI(nullptr)
{
    url->setFocus();
    setGeometry(krMainWindow->x() + krMainWindow->width() / 2 - width() / 2, krMainWindow->y() + krMainWindow->height() / 2 - height() / 2, width(), height());
}

void newFTPSub::accept()
{
    url->addToHistory(url->currentText());
    newFTPGUI::accept();
}

void newFTPSub::reject()
{
    url->lineEdit()->setText("");
    newFTPGUI::reject();
}

newFTPSub::~newFTPSub()
{
    // Save the history and the completion list of the url comboBox
    KConfigGroup group(krConfig, "Private");
    QStringList list = url->completionObject()->items();
    group.writeEntry("newFTP Completion list", list);
    list = url->historyItems();
    group.writeEntry("newFTP History list", list);
    QString protocol = prefix->currentText();
    group.writeEntry("newFTP Protocol", protocol);
}

/////////////////////////// KrMaskChoiceSub ///////////////////////////////
KrMaskChoiceSub::KrMaskChoiceSub(QWidget *parent)
    : KrMaskChoice(parent)
{
    PixmapLabel1->setPixmap(Icon("edit-select").pixmap(32));
    label->setText(i18n("Enter a selection:"));
    // the predefined selections list
    KConfigGroup group(krConfig, "Private");
    QStringList lst = group.readEntry("Predefined Selections", QStringList());
    if (lst.size() > 0)
        preSelections->addItems(lst);
    // the combo-box tweaks
    selection->setDuplicatesEnabled(false);
    selection->addItems(KrSpWidgets::maskList);
    selection->lineEdit()->setText("*");
    selection->lineEdit()->selectAll();
    selection->setFocus();
}

void KrMaskChoiceSub::reject()
{
    selection->clear();
    KrMaskChoice::reject();
}

void KrMaskChoiceSub::accept()
{
    bool add = true;
    // make sure we don't have that already
    for (int i = 0; i != KrSpWidgets::maskList.count(); i++)
        if (KrSpWidgets::maskList[i].simplified() == selection->currentText().simplified()) {
            // break if we found one such as this
            add = false;
            break;
        }

    if (add)
        KrSpWidgets::maskList.insert(0, selection->currentText().toLocal8Bit());
    // write down the predefined selections list
    QStringList list;

    for (int j = 0; j != preSelections->count(); j++) {
        QListWidgetItem *i = preSelections->item(j);
        list.append(i->text());
    }

    KConfigGroup group(krConfig, "Private");
    group.writeEntry("Predefined Selections", list);
    KrMaskChoice::accept();
}

void KrMaskChoiceSub::addSelection()
{
    QString temp = selection->currentText();
    bool itemExists = false;

    // check if the selection already exists
    for (int j = 0; j != preSelections->count(); j++) {
        QListWidgetItem *i = preSelections->item(j);

        if (i->text() == temp) {
            itemExists = true;
            break;
        }
    }

    if (!temp.isEmpty() && !itemExists) {
        preSelections->addItem(selection->currentText());
        preSelections->update();
    }
}

void KrMaskChoiceSub::deleteSelection()
{
    delete preSelections->currentItem();
    preSelections->update();
}

void KrMaskChoiceSub::clearSelections()
{
    preSelections->clear();
    preSelections->update();
}

void KrMaskChoiceSub::acceptFromList(QListWidgetItem *i)
{
    selection->addItem(i->text(), 0);
    accept();
}

void KrMaskChoiceSub::currentItemChanged(QListWidgetItem *i)
{
    if (i)
        selection->setEditText(i->text());
}