~blue-shell/accounts/stable

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
/*************************************************************************************
 *  Copyright (C) 2012 by Alejandro Fiestas Olivares <afiestas@kde.org>              *
 *                                                                                   *
 *  This program is free software; you can redistribute it and/or                    *
 *  modify it under the terms of the GNU General Public License                      *
 *  as published by the Free Software Foundation; either version 2                   *
 *  of the License, or (at your option) any later version.                           *
 *                                                                                   *
 *  This program is distributed in the hope that it will be useful,                  *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
 *  GNU General Public License for more details.                                     *
 *                                                                                   *
 *  You should have received a copy of the GNU General Public License                *
 *  along with this program; if not, write to the Free Software                      *
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
 *************************************************************************************/

#include "ocreatefile.h"

#include <QWidget>
#include <QApplication>
#include <QtGui/QDesktopServices>

#include <KGlobal>
#include <KConfig>
#include <KDirNotify>
#include <KStandardDirs>
#include <KWallet/Wallet>
#include <kio/job.h>
#include <KDebug>

using namespace KWallet;

OCreateFile::OCreateFile(KConfigGroup group, QObject* parent)
 : KJob(parent)
 , m_config(group)
{

}

OCreateFile::~OCreateFile()
{

}

void OCreateFile::start()
{
    QMetaObject::invokeMethod(this, "getRealm", Qt::QueuedConnection);
}

void OCreateFile::getRealm()
{
    kDebug();
    KUrl url(m_config.readEntry("server", ""));
    url.setUser(m_config.name());
    url.setScheme("webdav");
    url.addPath("files/webdav.php/");

    KIO::TransferJob* job = KIO::get(url , KIO::NoReload, KIO::HideProgressInfo);
    connect(job, SIGNAL(finished(KJob*)), SLOT(gotRealm(KJob*)));
    KIO::MetaData data;
    data.insert("PropagateHttpHeader", "true");
    job->setMetaData(data);
    job->setUiDelegate(0);
    job->start();
}

void OCreateFile::gotRealm(KJob* job)
{
    KIO::TransferJob* hJob = qobject_cast<KIO::TransferJob*>(job);
    QRegExp rx("www-authenticate: Basic realm=\"(\\S+)\"\n");
    QString headers = hJob->metaData().value("HTTP-Headers");
    if (rx.indexIn(headers) != -1) {
        m_realm = rx.cap(1);
    }

    createNetAttach();
}

void OCreateFile::createNetAttach()
{
    KGlobal::dirs()->addResourceType("remote_entries", "data", "remoteview");

    KUrl url(m_config.readEntry("server", ""));
    url.setUser(m_config.name());
    url.setScheme("webdav");
    url.addPath("files/webdav.php/");

    QString path = KGlobal::dirs()->saveLocation("remote_entries");
    path +=  m_config.name() + "ownCloud.desktop";

    kDebug() << "Creating knetAttach place";
    kDebug() << path;
    kDebug() << url.prettyUrl();

    m_config.group("private").writeEntry("fileDesktop", path);

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

    if (m_config.readEntry("type", "") == "runnerid") {
        desktopFile.writeEntry("Icon", "netrunnerplace");
        desktopFile.writeEntry("Name", "Runners-ID-Storage");
    } else {
        desktopFile.writeEntry("Icon", "owncloud");
        desktopFile.writeEntry("Name", url.host());
    }
    desktopFile.writeEntry("Type", "Link");
    desktopFile.writeEntry("URL", url.prettyUrl());
    desktopFile.writeEntry("Charset", url.fileEncoding());
    desktopFile.sync();
    _desktopFile.sync();

    org::kde::KDirNotify::emitFilesAdded( "remote:/" );

    if (m_config.readEntry("type", "") == "runnerid") {
        QString networkPath = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
        networkPath.append("/Network/");
        if (QDir().exists(networkPath)) {
            QDir().mkdir(networkPath);
        }
        networkPath.append("Runners-ID-Storage");
        qDebug() << path << networkPath;
        qDebug() << QFile::copy(path, networkPath);
    }

    QString walletUrl(url.scheme());
    walletUrl.append("-");
    walletUrl.append(m_config.name());
    walletUrl.append("@");
    walletUrl.append(url.host());
    walletUrl.append(":-1");//Overwrite the first option
    if (!m_realm.isEmpty()) {
        walletUrl.append(m_realm);
    } else {
        walletUrl.append("webdav");
    }

    WId windowId = 0;
    if (qApp->activeWindow()) {
        windowId = qApp->activeWindow()->winId();
    }
    Wallet *wallet = Wallet::openWallet(Wallet::NetworkWallet(), windowId, Wallet::Synchronous);

    if (!wallet->isOpen() || !wallet->isEnabled()) {
        return;
    }

    QString password;
    wallet->setFolder("WebAccounts");
    wallet->readPassword("owncloud-" + m_config.name(), password);

    QMap<QString, QString> info;
    info["login"] = m_config.name();
    info["password"] = password;

    wallet->setFolder("Passwords");
    wallet->writeMap(walletUrl, info);
    wallet->sync();

    m_config.group("services").writeEntry("File", 1);
    emitResult();
}