owncloud.cpp 6.59 KB
Newer Older
Martin Klapetek's avatar
Martin Klapetek committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*************************************************************************************
 *  Copyright (C) 2012 by Alejandro Fiestas Olivares <afiestas@kde.org>              *
 *  Copyright (C) 2015 by Martin Klapetek <mklapetek@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 "owncloud.h"
#include "pages/basicinfo.h"
#include "pages/connecting.h"
#include "pages/oservices.h"
24
#include "qmlhelper.h"
Martin Klapetek's avatar
Martin Klapetek committed
25 26 27

#include <klocalizedstring.h>
#include <kstandardguiitem.h>
28 29 30 31
#include <KDeclarative/QmlObject>

#include <QQmlEngine>
#include <QQmlContext>
Martin Klapetek's avatar
Martin Klapetek committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

#include <QPushButton>
#include <QWizard>

// #include <KWallet/Wallet>

// using namespace KWallet;

OwnCloudWizard::OwnCloudWizard(QWidget *parent)
    : KAccountsUiPlugin(parent)
{

}

OwnCloudWizard::~OwnCloudWizard()
{

}

void OwnCloudWizard::init(KAccountsUiPlugin::UiType type)
{
    if (type == KAccountsUiPlugin::NewAccountDialog) {
54 55 56 57 58 59 60 61

        const QString packagePath("org.kde.kaccounts.owncloud");

        m_object = new KDeclarative::QmlObject();
        m_object->setTranslationDomain(packagePath);
        m_object->setInitializationDelayed(true);
        m_object->loadPackage(packagePath);

62
        QmlHelper *helper = new QmlHelper(m_object);
63
        connect(helper, &QmlHelper::wizardFinished, this, &OwnCloudWizard::success);
64 65 66 67 68 69 70
        connect(helper, &QmlHelper::wizardFinished, [=] {
            QWindow *window = qobject_cast<QWindow *>(m_object->rootObject());
            if (window) {
                window->close();
            }
            m_object->deleteLater();
        });
71 72 73 74 75 76 77 78
        m_object->engine()->rootContext()->setContextProperty("helper", helper);

        m_object->completeInitialization();

        if (!m_object->package().metadata().isValid()) {
            return;
        }
        /*
Martin Klapetek's avatar
Martin Klapetek committed
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
        m_wizard = new QWizard();
        m_wizard->setWindowTitle(i18n("Add ownCloud Account"));

        BasicInfo *basicInfo = new BasicInfo(m_wizard);
        Connecting *connecting = new Connecting(m_wizard);
        OServices *services = new OServices(m_wizard);

        m_wizard->addPage(basicInfo);
        m_wizard->addPage(connecting);
        m_wizard->addPage(services);

        QPushButton *backButton = new QPushButton(m_wizard);
        QPushButton *forwardButton = new QPushButton(m_wizard);
        QPushButton *applyButton = new QPushButton(m_wizard);
        QPushButton *cancelButton = new QPushButton(m_wizard);

        KGuiItem::assign(backButton, KStandardGuiItem::back());
        KGuiItem::assign(forwardButton, KStandardGuiItem::forward());
        KGuiItem::assign(applyButton, KStandardGuiItem::apply());
        KGuiItem::assign(cancelButton, KStandardGuiItem::cancel());

        m_wizard->setButton(QWizard::BackButton, backButton);
        m_wizard->setButton(QWizard::NextButton, forwardButton);
        m_wizard->setButton(QWizard::FinishButton, applyButton);
        m_wizard->setButton(QWizard::CancelButton, cancelButton);

        // We do not want "Forward" as text
        m_wizard->setButtonText(QWizard::NextButton, i18nc("Action to go to the next page on the wizard", "Next"));
        m_wizard->setButtonText(QWizard::FinishButton, i18nc("Action to finish the wizard", "Finish"));

        m_wizard->setOption(QWizard::NoDefaultButton, false);


112
        connect(m_wizard, &QWizard::accepted, this, &OwnCloudWizard::done);*/
Martin Klapetek's avatar
Martin Klapetek committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126

        Q_EMIT uiReady();
    }

}

void OwnCloudWizard::setProviderName(const QString &providerName)
{
    //TODO: unused?
    m_providerName = providerName;
}

void OwnCloudWizard::showNewAccountDialog()
{
127 128 129 130 131
//     if (m_wizard) {
//         m_wizard->exec();
//     }
    QWindow *window = qobject_cast<QWindow *>(m_object->rootObject());
    if (window) {
132
        window->setTransientParent(transientParent());
133 134 135 136
        window->show();
        window->requestActivate();
        window->setTitle(m_object->package().metadata().name());
        window->setIcon(QIcon::fromTheme(m_object->package().metadata().iconName()));
Martin Klapetek's avatar
Martin Klapetek committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    }
}

void OwnCloudWizard::showConfigureAccountDialog(const quint32 accountId)
{

}

QStringList OwnCloudWizard::supportedServicesForConfig() const
{
    return QStringList();
}

void OwnCloudWizard::done()
{
    QVariantMap data;
    data.insert("server", m_wizard->property("server"));

155 156 157 158
    Q_FOREACH (const QString &disabledService, m_wizard->property("disabledServices").toStringList()) {
        data.insert("__service/" + disabledService, false);
    }

Martin Klapetek's avatar
Martin Klapetek committed
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
    const QString username = m_wizard->property("username").toString();
    QUrl carddavUrl = m_wizard->property("server").toUrl();
    carddavUrl.setPath(carddavUrl.path() + QString("/remote.php/carddav/addressbooks/%1").arg(username));

    data.insert("carddavUrl", carddavUrl);
    Q_EMIT success(username, m_wizard->property("password").toString(), data);
}

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

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

void OwnCloudWizard::setServer(const QUrl &server)
{
    m_server = server;
}

const QString OwnCloudWizard::username() const
{
    return m_username;
}

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

const QUrl OwnCloudWizard::server() const
{
    return m_server;
}

void OwnCloudWizard::activateOption(const QString &name, bool checked)
{
    if (!checked) {
        m_services[name] = 0;
        return;
    }

    m_services[name] = 2;
}