addresseslocationengineviewer.cpp 4.26 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
    This file is part of Contact Editor.
Laurent Montel's avatar
Laurent Montel committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

    Copyright (C) 2016-2017 Laurent Montel <montel@kde.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library 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 Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "addresseslocationengineviewer.h"
#include "addresseslocationenginepage.h"
Laurent Montel's avatar
Laurent Montel committed
24
#include "contacteditor_debug.h"
Laurent Montel's avatar
Laurent Montel committed
25 26 27 28 29 30
#include "../addresseslocationgrantleeformater.h"
#include <KMessageBox>
#include <KLocalizedString>
#include <QUrlQuery>
#include <QWebEngineSettings>

Laurent Montel's avatar
Laurent Montel committed
31
using namespace ContactEditor;
Laurent Montel's avatar
Laurent Montel committed
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

AddressesLocationEngineViewer::AddressesLocationEngineViewer(QWidget *parent)
    : QWebEngineView(parent),
      mAddressesLocationGrantleeFormatter(new AddressesLocationGrantleeFormater(this)),
      mEditMode(false)
{

    AddressesLocationEnginePage *pageEngine = new AddressesLocationEnginePage(this);
    setPage(pageEngine);

    setFocusPolicy(Qt::WheelFocus);
    connect(pageEngine, &AddressesLocationEnginePage::urlClicked, this, &AddressesLocationEngineViewer::slotLinkClicked);

    setContextMenuPolicy(Qt::CustomContextMenu);
}

AddressesLocationEngineViewer::~AddressesLocationEngineViewer()
{

}

void AddressesLocationEngineViewer::setReadOnly(bool readOnly)
{
    mAddressesLocationGrantleeFormatter->setReadOnly(readOnly);
    updateView();
}

void AddressesLocationEngineViewer::slotLinkClicked(const QUrl &url)
{
    if (url.scheme() == QLatin1String("addresslocationaction")) {
        const QString urlPath(url.path());
        if (url.hasQuery()) {
            const QUrlQuery urlQuery(url);
            const int addressId = urlQuery.queryItemValue(QStringLiteral("id")).toInt();
            if (urlPath == QStringLiteral("removeaddress")) {
                if (!mEditMode) {
                    removeAddress(addressId);
                }
            } else if (urlPath == QStringLiteral("editaddress")) {
                if (!mEditMode) {
                    editAddress(addressId);
                }
            } else {
Laurent Montel's avatar
Laurent Montel committed
75
                qCDebug(CONTACTEDITOR_LOG) << "Unknown url" << url;
Laurent Montel's avatar
Laurent Montel committed
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
            }
        }
    }
}

void AddressesLocationEngineViewer::removeAddress(int index)
{
    if (index < 0) {
        return;
    } else if (index < mAddresses.count()) {
        const int result = KMessageBox::questionYesNo(this, i18n("Do you really want to delete this address?"));

        if (result != KMessageBox::Yes) {
            return;
        }
        mAddresses.remove(index);
        updateView();
    }
}

void AddressesLocationEngineViewer::editAddress(int index)
{
    if (index < 0) {
        return;
    } else if (index < mAddresses.count()) {
        mEditMode = true;
        Q_EMIT modifyAddress(mAddresses.at(index), index);
    }
}

void AddressesLocationEngineViewer::replaceAddress(const KContacts::Address &address, int index)
{
    if (index < 0) {
        return;
    } else if (index < mAddresses.count()) {
        mAddresses[index] = address;
        updateView();
    }
    mEditMode = false;
}

void AddressesLocationEngineViewer::updateAddressCanceled()
{
    mEditMode = false;
}

void AddressesLocationEngineViewer::updateView()
{
    const QString html = mAddressesLocationGrantleeFormatter->formatAddresses(mAddresses);
    setHtml(html, QUrl(QStringLiteral("file://")));
}

void AddressesLocationEngineViewer::addAddress(const KContacts::Address &address)
{
    if (!address.isEmpty()) {
        mAddresses.append(address);
        updateView();
    }
}

KContacts::Address::List AddressesLocationEngineViewer::addresses() const
{
    return mAddresses;
}

void AddressesLocationEngineViewer::setAddresses(const KContacts::Address::List &addresses)
{
    mAddresses = addresses;
    updateView();
}