udisks2monitor_p.h 4.85 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 35 36 37
/*
 * 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."
 */

#ifndef UDISKS2_MONITOR_H
#define UDISKS2_MONITOR_H

#include <QObject>
#include <QDBusObjectPath>
#include <QExplicitlySharedDataPointer>
38
#include <QRegularExpression>
39
#include <QQueue>
40
#include <QVariantList>
41 42 43

#include "partitionmodel.h"
#include "partitionmanager_p.h"
44
#include "udisks2defines.h"
45 46 47

class PartitionManagerPrivate;

48 49
static const QRegularExpression deviceRoot(QStringLiteral("^mmcblk\\d+$"));

50 51
namespace UDisks2 {

52
class Block;
53
class BlockDevices;
54 55
class Job;

56 57
struct Operation
{
58
    Operation(const QString &command, const QString &devicePath, const QString &dbusObjectPath = QString(), const QString &filesystemType = QString(), const QVariantMap &arguments  = QVariantMap())
59
        : command(command)
60
        , devicePath(devicePath)
61
        , dbusObjectPath(dbusObjectPath)
62
        , filesystemType(filesystemType)
63 64 65 66
        , arguments(arguments)
    {}

    QString command;
67
    QString devicePath;
68
    QString dbusObjectPath;
69
    QString filesystemType;
70
    QVariantMap arguments;
71 72
};

73 74 75 76 77 78 79 80 81
class Monitor : public QObject
{
    Q_OBJECT
public:
    explicit Monitor(PartitionManagerPrivate *manager, QObject *parent = nullptr);
    ~Monitor();

    static Monitor *instance();

82 83
    void lock(const QString &devicePath);
    void unlock(const QString &devicePath, const QString &passphrase);
84

85 86
    void mount(const QString &devicePath);
    void unmount(const QString &devicePath);
87

88
    void format(const QString &devicePath, const QString &filesystemType, const QVariantMap &arguments);
89

90
signals:
91
    void status(const QString &devicePath, Partition::Status);
92
    void errorMessage(const QString &objectPath, const QString &errorName);
93 94
    void lockError(Partition::Error error);
    void unlockError(Partition::Error error);
95 96 97
    void mountError(Partition::Error error);
    void unmountError(Partition::Error error);
    void formatError(Partition::Error error);
98 99

private slots:
100
    void interfacesAdded(const QDBusObjectPath &objectPath, const UDisks2::InterfacePropertyMap &interfaces);
101
    void interfacesRemoved(const QDBusObjectPath &objectPath, const QStringList &interfaces);
102
    void doFormat(const QString &devicePath, const QString &dbusObjectPath, const QString &filesystemType, const QVariantMap &arguments);
103
    void handleNewBlock(UDisks2::Block *block);
104 105

private:
106 107 108
    void setPartitionProperties(QExplicitlySharedDataPointer<PartitionPrivate> &partition, const Block *blockDevice);
    void updatePartitionProperties(const Block *blockDevice);
    void updatePartitionStatus(const Job *job, bool success);
109

110 111
    void startLuksOperation(const QString &devicePath, const QString &dbusMethod, const QString &dbusObjectPath, const QVariantList &arguments);
    void startMountOperation(const QString &devicePath, const QString &dbusMethod, const QString &dbusObjectPath, const QVariantList &arguments);
112
    void lookupPartitions(PartitionManagerPrivate::Partitions &affectedPartitions, const QStringList &objects);
113

114 115
    void createPartition(const Block *block);
    void getBlockDevices();
116
    void connectSignals(UDisks2::Block *block);
117

118 119 120 121
private:
    static Monitor *sharedInstance;

    QExplicitlySharedDataPointer<PartitionManagerPrivate> m_manager;
122
    QMap<QString, Job *> m_jobsToWait;
123 124

    QQueue<Operation> m_operationQueue;
125 126

    BlockDevices *m_blockDevices;
127 128 129 130 131
};

}

#endif