File: driveselection.cpp

package info (click to toggle)
kup-backup 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,576 kB
  • sloc: cpp: 8,422; xml: 311; makefile: 6; sh: 3
file content (328 lines) | stat: -rw-r--r-- 13,548 bytes parent folder | download
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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// SPDX-FileCopyrightText: 2020 Simon Persson <simon.persson@mykolab.com>
//
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL

#include "driveselection.h"
#include "backupplan.h"
#include "driveselectiondelegate.h"

#include <QItemSelectionModel>
#include <QList>
#include <QPainter>
#include <QStandardItemModel>
#include <QTimer>

#include <KConfigDialogManager>
#include <KLocalizedString>
#include <QStorageInfo>

#include <Solid/Device>
#include <Solid/DeviceNotifier>
#include <Solid/StorageAccess>
#include <Solid/StorageDrive>
#include <Solid/StorageVolume>

bool deviceLessThan(const Solid::Device &a, const Solid::Device &b)
{
    return a.udi() < b.udi();
}

DriveSelection::DriveSelection(BackupPlan *pBackupPlan, QWidget *parent)
    : QListView(parent)
    , mBackupPlan(pBackupPlan)
    , mSelectedAndAccessible(false)
    , mSyncedBackupType(false)
{
    mDrivesModel = new QStandardItemModel(this);
    setModel(mDrivesModel);
    setItemDelegate(new DriveSelectionDelegate(this));
    setSelectionMode(QAbstractItemView::SingleSelection);
    setWordWrap(true);

    if (!mBackupPlan->mExternalUUID.isEmpty()) {
        auto *lItem = new QStandardItem();
        lItem->setEditable(false);
        lItem->setData(QString(), DriveSelection::UDI);
        lItem->setData(mBackupPlan->mExternalUUID, DriveSelection::UUID);
        lItem->setData(0, DriveSelection::UsedSpace);
        lItem->setData(mBackupPlan->mExternalPartitionNumber, DriveSelection::PartitionNumber);
        lItem->setData(mBackupPlan->mExternalPartitionsOnDrive, DriveSelection::PartitionsOnDrive);
        lItem->setData(mBackupPlan->mExternalDeviceDescription, DriveSelection::DeviceDescription);
        lItem->setData(mBackupPlan->mExternalVolumeCapacity, DriveSelection::TotalSpace);
        lItem->setData(mBackupPlan->mExternalVolumeLabel, DriveSelection::Label);
        mDrivesModel->appendRow(lItem);
    }

    QList<Solid::Device> lDeviceList = Solid::Device::listFromType(Solid::DeviceInterface::StorageDrive);
    foreach (const Solid::Device &lDevice, lDeviceList) {
        deviceAdded(lDevice.udi());
    }
    connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded, this, &DriveSelection::deviceAdded);
    connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceRemoved, this, &DriveSelection::deviceRemoved);
    connect(selectionModel(), &QItemSelectionModel::selectionChanged, this, &DriveSelection::updateSelection);
}

QString DriveSelection::mountPathOfSelectedDrive() const
{
    if (mSelectedAndAccessible) {
        QStandardItem *lItem;
        findItem(DriveSelection::UUID, mSelectedUuid, &lItem);
        if (lItem != nullptr) {
            Solid::Device lDevice(lItem->data(DriveSelection::UDI).toString());
            auto *lAccess = lDevice.as<Solid::StorageAccess>();
            if (lAccess) {
                return lAccess->filePath();
            }
        }
    }
    return QString();
}

void DriveSelection::deviceAdded(const QString &pUdi)
{
    Solid::Device lDevice(pUdi);
    if (!lDevice.is<Solid::StorageDrive>()) {
        return;
    }
    auto *lDrive = lDevice.as<Solid::StorageDrive>();
    if (!lDrive->isHotpluggable() && !lDrive->isRemovable()) {
        return;
    }
    if (mDrivesToAdd.contains(pUdi)) {
        return;
    }
    mDrivesToAdd.append(pUdi);
    QTimer::singleShot(2000, this, SLOT(delayedDeviceAdded()));
}

void DriveSelection::delayedDeviceAdded()
{
    if (mDrivesToAdd.isEmpty()) {
        return;
    }
    Solid::Device lParentDevice(mDrivesToAdd.takeFirst());
    QList<Solid::Device> lDeviceList = Solid::Device::listFromType(Solid::DeviceInterface::StorageVolume, lParentDevice.udi());
    // check for when there is no partitioning scheme, then the drive is also the storage volume
    if (lParentDevice.is<Solid::StorageVolume>()) {
        lDeviceList.append(lParentDevice);
    }

    // filter out some volumes that should not be visible
    QList<Solid::Device> lVolumeDeviceList;
    foreach (Solid::Device lVolumeDevice, lDeviceList) {
        auto *lVolume = lVolumeDevice.as<Solid::StorageVolume>();
        if (lVolume && !lVolume->isIgnored() && (lVolume->usage() == Solid::StorageVolume::FileSystem || lVolume->usage() == Solid::StorageVolume::Encrypted)) {
            lVolumeDeviceList.append(lVolumeDevice);
        }
    }

    // simplest attempt at getting the same partition numbering every time a device is plugged in
    std::sort(lVolumeDeviceList.begin(), lVolumeDeviceList.end(), deviceLessThan);

    int lPartitionNumber = 1;
    foreach (Solid::Device lVolumeDevice, lVolumeDeviceList) {
        auto *lVolume = lVolumeDevice.as<Solid::StorageVolume>();
        QString lUuid = lVolume->uuid();
        if (lUuid.isEmpty()) { // seems to happen for vfat partitions
            lUuid += lParentDevice.description();
            lUuid += QStringLiteral("|");
            lUuid += lVolume->label();
        }
        QStandardItem *lItem;
        bool lNeedsToBeAdded = false;
        findItem(DriveSelection::UUID, lUuid, &lItem);
        if (lItem == nullptr) {
            lItem = new QStandardItem();
            lItem->setEditable(false);
            lItem->setData(lUuid, DriveSelection::UUID);
            lItem->setData(0, DriveSelection::TotalSpace);
            lItem->setData(0, DriveSelection::UsedSpace);
            lNeedsToBeAdded = true;
        }
        lItem->setData(lParentDevice.description(), DriveSelection::DeviceDescription);
        lItem->setData(lVolume->label(), DriveSelection::Label);
        lItem->setData(lVolumeDevice.udi(), DriveSelection::UDI);
        lItem->setData(lPartitionNumber, DriveSelection::PartitionNumber);
        lItem->setData(lVolumeDeviceList.count(), DriveSelection::PartitionsOnDrive);
        lItem->setData(lVolume->fsType(), DriveSelection::FileSystem);
        lItem->setData(mSyncedBackupType && (lVolume->fsType() == QStringLiteral("vfat") || lVolume->fsType() == QStringLiteral("ntfs")),
                       DriveSelection::PermissionLossWarning);
        lItem->setData(mSyncedBackupType && lVolume->fsType() == QStringLiteral("vfat"), DriveSelection::SymlinkLossWarning);

        auto *lAccess = lVolumeDevice.as<Solid::StorageAccess>();
        connect(lAccess, &Solid::StorageAccess::accessibilityChanged, this, &DriveSelection::accessabilityChanged);
        if (lAccess->isAccessible()) {
            QStorageInfo storageInfo(lAccess->filePath());
            if (storageInfo.isValid()) {
                lItem->setData(storageInfo.bytesTotal(), DriveSelection::TotalSpace);
                lItem->setData(storageInfo.bytesTotal() - storageInfo.bytesFree(), DriveSelection::UsedSpace);
            }
            if (lUuid == mSelectedUuid) {
                // Selected volume was just added, could not have been accessible before.
                mSelectedAndAccessible = true;
                emit selectedDriveIsAccessibleChanged(true);
            }
        } else if (lVolume->usage() != Solid::StorageVolume::Encrypted) {
            // Don't bother the user with password prompt just for sake of storage volume space info
            lAccess->setup();
        }
        if (lNeedsToBeAdded) {
            mDrivesModel->appendRow(lItem);
            if (mDrivesModel->rowCount() == 1) {
                selectionModel()->select(mDrivesModel->index(0, 0), QItemSelectionModel::ClearAndSelect);
            }
        }
        lPartitionNumber++;
    }
}

void DriveSelection::deviceRemoved(const QString &pUdi)
{
    QStandardItem *lItem;
    int lRow = findItem(DriveSelection::UDI, pUdi, &lItem);
    if (lRow >= 0) {
        QString lUuid = lItem->data(DriveSelection::UUID).toString();
        if (lUuid == mBackupPlan->mExternalUUID) {
            // let the selected and saved item stay in the list
            // just clear the UDI so that it will be shown as disconnected.
            lItem->setData(QString(), DriveSelection::UDI);
        } else {
            mDrivesModel->removeRow(lRow);
        }
        if (lUuid == mSelectedUuid && mSelectedAndAccessible) {
            mSelectedAndAccessible = false;
            emit selectedDriveIsAccessibleChanged(false);
        }
    }
}

void DriveSelection::accessabilityChanged(bool pAccessible, const QString &pUdi)
{
    QStandardItem *lItem;
    findItem(DriveSelection::UDI, pUdi, &lItem);
    if (lItem != nullptr) {
        if (pAccessible) {
            Solid::Device lDevice(pUdi);
            auto *lAccess = lDevice.as<Solid::StorageAccess>();
            if (lAccess) {
                QStorageInfo storageInfo(lAccess->filePath());
                if (storageInfo.isValid()) {
                    lItem->setData(storageInfo.bytesTotal(), DriveSelection::TotalSpace);
                    lItem->setData(storageInfo.bytesTotal() - storageInfo.bytesFree(), DriveSelection::UsedSpace);
                }
            }
        }
        bool lSelectedAndAccessible = (lItem->data(DriveSelection::UUID).toString() == mSelectedUuid && pAccessible);
        if (lSelectedAndAccessible != mSelectedAndAccessible) {
            mSelectedAndAccessible = lSelectedAndAccessible;
            emit selectedDriveIsAccessibleChanged(lSelectedAndAccessible);
        }
    }
}

void DriveSelection::updateSelection(const QItemSelection &pSelected, const QItemSelection &pDeselected)
{
    Q_UNUSED(pDeselected)
    if (!pSelected.indexes().isEmpty()) {
        QModelIndex lIndex = pSelected.indexes().first();
        if (mSelectedUuid.isEmpty()) {
            emit driveIsSelectedChanged(true);
        }
        mSelectedUuid = lIndex.data(DriveSelection::UUID).toString();
        emit selectedDriveChanged(mSelectedUuid);
        // check if the newly selected volume is accessible, compare to previous selection
        bool lIsAccessible = false;
        QString lUdiOfSelected = lIndex.data(DriveSelection::UDI).toString();
        if (!lUdiOfSelected.isEmpty()) {
            Solid::Device lDevice(lUdiOfSelected);
            auto *lAccess = lDevice.as<Solid::StorageAccess>();
            if (lAccess != nullptr) {
                lIsAccessible = lAccess->isAccessible();
            }
        }
        if (mSelectedAndAccessible != lIsAccessible) {
            mSelectedAndAccessible = lIsAccessible;
            emit selectedDriveIsAccessibleChanged(mSelectedAndAccessible);
        }
    } else {
        mSelectedUuid.clear();
        emit selectedDriveChanged(mSelectedUuid);
        emit driveIsSelectedChanged(false);
        mSelectedAndAccessible = false;
        emit selectedDriveIsAccessibleChanged(false);
    }
}

void DriveSelection::paintEvent(QPaintEvent *pPaintEvent)
{
    QListView::paintEvent(pPaintEvent);
    if (mDrivesModel->rowCount() == 0) {
        QPainter lPainter(viewport());
        style()->drawItemText(&lPainter,
                              rect(),
                              Qt::AlignCenter,
                              palette(),
                              false,
                              xi18nc("@label Only shown if no drives are detected",
                                     "Plug in the external "
                                     "storage you wish to use, then select it in this list."),
                              QPalette::Text);
    }
}

void DriveSelection::setSelectedDrive(const QString &pUuid)
{
    if (pUuid == mSelectedUuid) {
        return;
    }
    if (pUuid.isEmpty()) {
        clearSelection();
    } else {
        QStandardItem *lItem;
        findItem(DriveSelection::UUID, pUuid, &lItem);
        if (lItem != nullptr) {
            setCurrentIndex(mDrivesModel->indexFromItem(lItem));
        }
    }
}

void DriveSelection::saveExtraData()
{
    QStandardItem *lItem;
    findItem(DriveSelection::UUID, mSelectedUuid, &lItem);
    if (lItem != nullptr) {
        mBackupPlan->mExternalDeviceDescription = lItem->data(DriveSelection::DeviceDescription).toString();
        mBackupPlan->mExternalPartitionNumber = lItem->data(DriveSelection::PartitionNumber).toInt();
        mBackupPlan->mExternalPartitionsOnDrive = lItem->data(DriveSelection::PartitionsOnDrive).toInt();
        mBackupPlan->mExternalVolumeCapacity = lItem->data(DriveSelection::TotalSpace).toULongLong();
        mBackupPlan->mExternalVolumeLabel = lItem->data(DriveSelection::Label).toString();
    }
}

void DriveSelection::updateSyncWarning(bool pSyncBackupSelected)
{
    mSyncedBackupType = pSyncBackupSelected;
    for (int i = 0; i < mDrivesModel->rowCount(); ++i) {
        QString lFsType = mDrivesModel->item(i)->data(DriveSelection::FileSystem).toString();
        mDrivesModel->item(i)->setData(mSyncedBackupType && (lFsType == QStringLiteral("vfat") || lFsType == QStringLiteral("ntfs")),
                                       DriveSelection::PermissionLossWarning);
        mDrivesModel->item(i)->setData(mSyncedBackupType && lFsType == QStringLiteral("vfat"), DriveSelection::SymlinkLossWarning);
    }
}

int DriveSelection::findItem(const DriveSelection::DataType pField, const QString &pSearchString, QStandardItem **pReturnedItem) const
{
    for (int lRow = 0; lRow < mDrivesModel->rowCount(); ++lRow) {
        QStandardItem *lItem = mDrivesModel->item(lRow);
        if (lItem->data(pField).toString() == pSearchString) {
            if (pReturnedItem != nullptr) {
                *pReturnedItem = lItem;
            }
            return lRow;
        }
    }
    if (pReturnedItem != nullptr) {
        *pReturnedItem = nullptr;
    }
    return -1;
}