udisks2job.cpp 4.65 KB
Newer Older
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
/*
 * Copyright (C) 2018 Jolla Ltd. <raine.makelainen@jolla.com>
 *
 * You may use this file under the terms of the BSD license as follows:
 *
 * "Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Nemo Mobile nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 */

#include "udisks2job_p.h"
#include "udisks2monitor_p.h"
#include "udisks2defines.h"
35
#include "logging_p.h"
36 37 38 39 40 41 42 43 44 45 46 47 48 49

#include <QDBusConnection>

#include <nemo-dbus/dbus.h>

UDisks2::Job::Job(const QString &path, const QVariantMap &data, QObject *parent)
    : QObject(parent)
    , m_path(path)
    , m_data(data)
    , m_status(Added)
    , m_completed(false)
    , m_success(false)
    , m_connection(QDBusConnection::systemBus())
{
50
    if (!m_path.isEmpty() && !m_connection.connect(
51 52 53 54 55 56
                UDISKS2_SERVICE,
                m_path,
                UDISKS2_JOB_INTERFACE,
                QStringLiteral("Completed"),
                this,
                SLOT(updateCompleted(bool, QString)))) {
57
        qCWarning(lcMemoryCardLog) << "Failed to connect to Job's at path" << qPrintable(m_path) << "completed signal" << qPrintable(m_connection.lastError().message());
58 59 60
    }

    connect(Monitor::instance(), &Monitor::errorMessage, this, [this](const QString &objectPath, const QString &errorName) {
61
        if (objects().contains(objectPath) && errorName == UDISKS2_ERROR_DEVICE_BUSY) {
62 63 64 65 66 67 68 69 70 71 72 73
            m_message = errorName;
            if (!isCompleted() && deviceBusy()) {
                updateCompleted(false, m_message);
            }
        }
    });
}

UDisks2::Job::~Job()
{
}

74 75 76 77 78 79 80 81
void UDisks2::Job::complete(bool success)
{
    m_completed = true;
    m_success = success;
    m_status = UDisks2::Job::Completed;
    emit completed(success);
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
bool UDisks2::Job::isCompleted() const
{
    return m_completed;
}

bool UDisks2::Job::success() const
{
    return m_success;
}

QString UDisks2::Job::message() const
{
    return m_message;
}

bool UDisks2::Job::deviceBusy() const
{
    return m_message == UDISKS2_ERROR_TARGET_BUSY || m_message == UDISKS2_ERROR_DEVICE_BUSY;
}

102 103 104 105 106
QStringList UDisks2::Job::objects() const
{
    return value(UDISKS2_JOB_KEY_OBJECTS).toStringList();
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
QString UDisks2::Job::path() const
{
    return m_path;
}

QVariant UDisks2::Job::value(const QString &key) const
{
    return NemoDBus::demarshallDBusArgument(m_data.value(key));
}

UDisks2::Job::Status UDisks2::Job::status() const
{
    return m_status;
}

UDisks2::Job::Operation UDisks2::Job::operation() const
{
    QString operation = value(UDISKS2_JOB_KEY_OPERATION).toString();
    if (operation == UDISKS2_JOB_OP_FS_MOUNT) {
        return Mount;
    } else if (operation == UDISKS2_JOB_OP_FS_UNMOUNT) {
        return Unmount;
129 130
    } else if (operation == UDISKS2_JOB_OF_FS_FORMAT) {
        return Format;
131 132 133 134
    } else if (operation == UDISKS2_JOB_OP_ENC_LOCK) {
        return Lock;
    } else if (operation == UDISKS2_JOB_OP_ENC_UNLOCK) {
        return Unlock;
135 136 137 138 139
    } else {
        return Unknown;
    }
}

140 141 142 143 144 145 146 147
void UDisks2::Job::dumpInfo() const
{
    qCInfo(lcMemoryCardLog) << "Job" << path() << ((status() == Added) ? "added" : "completed");
    for (const QString &key : m_data.keys()) {
        qCInfo(lcMemoryCardLog) << "- " << qPrintable(key) << value(key);
    }
}

148 149 150
void UDisks2::Job::updateCompleted(bool success, const QString &message)
{
    m_message = message;
151
    complete(success);
152
}