createnetattachjob.cpp 5.25 KB
Newer Older
1 2 3 4 5
/*
 *  SPDX-FileCopyrightText: 2012 Alejandro Fiestas Olivares <afiestas@kde.org>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */
6

7
#include "createnetattachjob.h"
8

9 10
#include <Accounts/Manager>

11
#include <KDirNotify>
Nicolas Fella's avatar
Nicolas Fella committed
12
#include <KWallet>
13
#include <KConfig>
14
#include <KIO/Job>
15
#include <KConfigGroup>
Martin Klapetek's avatar
Martin Klapetek committed
16

Martin Klapetek's avatar
Martin Klapetek committed
17 18 19 20
#include <QApplication>
#include <QWidget>
#include <QUrl>
#include <QDir>
Martin Klapetek's avatar
Martin Klapetek committed
21
#include <QDebug>
22
#include <QRegularExpression>
23 24 25

using namespace KWallet;

Martin Klapetek's avatar
Martin Klapetek committed
26
CreateNetAttachJob::CreateNetAttachJob(QObject *parent)
27 28 29 30 31
 : KJob(parent)
{

}

Àlex Fiestas's avatar
Àlex Fiestas committed
32 33 34 35 36
CreateNetAttachJob::~CreateNetAttachJob()
{
    delete m_wallet;
}

37
void CreateNetAttachJob::start()
38
{
39 40
    QMetaObject::invokeMethod(this, "createNetAttach", Qt::QueuedConnection);
}
41

42
void CreateNetAttachJob::createNetAttach()
43
{
Martin Klapetek's avatar
Martin Klapetek committed
44
    qDebug();
45 46 47 48 49
    WId windowId = 0;
    if (qApp->activeWindow()) {
        windowId = qApp->activeWindow()->winId();
    }
    m_wallet = Wallet::openWallet(Wallet::NetworkWallet(), windowId, Wallet::Asynchronous);
50

Nicolas Fella's avatar
Nicolas Fella committed
51
    connect(m_wallet, &KWallet::Wallet::walletOpened, this, &CreateNetAttachJob::walletOpened);
52 53 54 55
}

void CreateNetAttachJob::walletOpened(bool opened)
{
Martin Klapetek's avatar
Martin Klapetek committed
56
    qDebug();
57 58
    if (!opened) {
        setError(-1);
59
        setErrorText(QStringLiteral("Can't open wallet"));
60 61 62 63
        emitResult();
        return;
    }

64
    getRealm();
65 66
}

67
void CreateNetAttachJob::getRealm()
68
{
Martin Klapetek's avatar
Martin Klapetek committed
69
    qDebug();
Martin Klapetek's avatar
Martin Klapetek committed
70
    QUrl url;
71
    url.setHost(m_host);
Martin Klapetek's avatar
Martin Klapetek committed
72
    url.setUserName(m_username);
Nicolas Fella's avatar
Nicolas Fella committed
73
    url.setScheme(QStringLiteral("webdavs"));
Nicolas Fella's avatar
Nicolas Fella committed
74
    url.setPath(m_path);
75

76 77 78 79 80
    if (!m_realm.isEmpty()) {
        createDesktopFile(url);
        return;
    }

Martin Klapetek's avatar
Martin Klapetek committed
81
    KIO::TransferJob *job = KIO::get(url , KIO::NoReload, KIO::HideProgressInfo);
Nicolas Fella's avatar
Nicolas Fella committed
82
    connect(job, &KIO::TransferJob::finished, this, &CreateNetAttachJob::gotRealm);
83
    KIO::MetaData data;
84
    data.insert(QStringLiteral("PropagateHttpHeader"), QStringLiteral("true"));
85
    job->setMetaData(data);
Laurent Montel's avatar
Laurent Montel committed
86
    job->setUiDelegate(nullptr);
87 88 89
    job->start();
}

Martin Klapetek's avatar
Martin Klapetek committed
90
void CreateNetAttachJob::gotRealm(KJob *job)
91
{
Martin Klapetek's avatar
Martin Klapetek committed
92
    KIO::TransferJob *hJob = qobject_cast<KIO::TransferJob*>(job);
93 94
    QRegularExpression rx(QStringLiteral("www-authenticate: Basic realm=\"([^\"]+)\""));
    Q_ASSERT(rx.isValid());
95
    QString headers = hJob->metaData().value(QStringLiteral("HTTP-Headers"));
96 97 98 99 100

    auto match = rx.match(headers);

    if (match.hasMatch()) {
        m_realm = match.captured(1);
101 102 103 104 105 106
    }

    createDesktopFile(hJob->url());
}


Martin Klapetek's avatar
Martin Klapetek committed
107
void CreateNetAttachJob::createDesktopFile(const QUrl &url)
108
{
Martin Klapetek's avatar
Martin Klapetek committed
109
    qDebug();
110

Nicolas Fella's avatar
Nicolas Fella committed
111
    const QString folderPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/remoteview/");
112

Nicolas Fella's avatar
Nicolas Fella committed
113
    QDir saveDir(folderPath);
114
    if (!saveDir.exists()) {
Nicolas Fella's avatar
Nicolas Fella committed
115 116
        if (!saveDir.mkpath(folderPath)) {
            qWarning() << "Directory" << folderPath << "for storage couldn't be created!";
117 118
        }
    }
Nicolas Fella's avatar
Nicolas Fella committed
119
    const QString path = folderPath + m_uniqueId + QStringLiteral(".desktop");
120

121 122
    qDebug() << "Creating knetAttach place";
    qDebug() << path;
123
    qDebug() << url.host();
Martin Klapetek's avatar
Martin Klapetek committed
124
    qDebug() << url.toString();
Àlex Fiestas's avatar
Àlex Fiestas committed
125

126 127 128
    KConfig _desktopFile( path, KConfig::SimpleConfig );
    KConfigGroup desktopFile(&_desktopFile, "Desktop Entry");

129
    desktopFile.writeEntry("Icon", m_icon);
130
    desktopFile.writeEntry("Name", m_name);
131
    desktopFile.writeEntry("Type", "Link");
Martin Klapetek's avatar
Martin Klapetek committed
132 133
    desktopFile.writeEntry("URL", url.toString());
//     desktopFile.writeEntry("Charset", url.fileEncoding());
134 135 136
    desktopFile.sync();

    QString walletUrl(url.scheme());
137
    walletUrl.append(QStringLiteral("-"));
138
    walletUrl.append(m_username);
139
    walletUrl.append(QStringLiteral("@"));
140
    walletUrl.append(url.host());
141
    walletUrl.append(QStringLiteral(":-1-"));//Overwrite the first option
142 143

    QMap<QString, QString> info;
144 145
    info[QStringLiteral("login")] = m_username;
    info[QStringLiteral("password")] = m_password;
Àlex Fiestas's avatar
Àlex Fiestas committed
146

147
    m_wallet->setFolder(QStringLiteral("Passwords"));
148 149 150 151

    if (!m_realm.isEmpty()) {
        m_wallet->writeMap(walletUrl + m_realm, info);
    }
152
    m_wallet->writeMap(walletUrl + QStringLiteral("webdav"), info);
153 154
    m_wallet->sync();

155
    org::kde::KDirNotify::emitFilesAdded(QUrl(QStringLiteral("remote:/")));
156 157 158

    emitResult();
}
159 160 161 162 163 164 165 166 167 168

QString CreateNetAttachJob::host() const
{
    return m_host;
}
void CreateNetAttachJob::setHost(const QString &host)
{
    m_host = host;
}

169 170 171 172 173 174 175 176 177 178
QString CreateNetAttachJob::path() const
{
    return m_path;
}

void CreateNetAttachJob::setPath(const QString& path)
{
    m_path = path;
}

179 180 181 182 183 184 185 186 187 188
QString CreateNetAttachJob::realm() const
{
    return m_realm;
}

void CreateNetAttachJob::setRealm(const QString &realm)
{
    m_realm = realm;
}

189 190 191 192 193 194 195 196 197 198
QString CreateNetAttachJob::name() const
{
    return m_name;
}

void CreateNetAttachJob::setName(const QString& name)
{
    m_name = name;
}

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
QString CreateNetAttachJob::username() const
{
    return m_username;
}

void CreateNetAttachJob::setUsername(const QString &username)
{
    m_username = username;
}

QString CreateNetAttachJob::password() const
{
    return m_password;
}

void CreateNetAttachJob::setPassword(const QString &password)
{
    m_password = password;
}

219 220 221 222 223 224 225 226 227 228
QString CreateNetAttachJob::uniqueId() const
{
    return m_uniqueId;
}

void CreateNetAttachJob::setUniqueId(const QString& uniqueId)
{
    m_uniqueId = uniqueId;
}

229 230 231 232 233 234 235 236 237
QString CreateNetAttachJob::icon() const
{
    return m_icon;
}

void CreateNetAttachJob::setIcon(const QString &icon)
{
    m_icon = icon;
}