driveselection.cpp 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/***************************************************************************
 *   Copyright Simon Persson                                               *
 *   simonpersson1@gmail.com                                               *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

21 22
#include "driveselection.h"
#include "driveselectiondelegate.h"
23
#include "backupplan.h"
24 25

#include <QItemSelectionModel>
26
#include <QList>
27
#include <QPainter>
28
#include <QStandardItemModel>
29
#include <QTimer>
30 31 32 33

#include <KConfigDialogManager>
#include <KDiskFreeSpaceInfo>
#include <KLocale>
34 35 36 37 38 39 40 41 42
#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();
}
43

44
DriveSelection::DriveSelection(BackupPlan *pBackupPlan, QWidget *parent)
45
   : QListView(parent), mBackupPlan(pBackupPlan), mSelectedAndAccessible(false), mSyncedBackupType(false)
46
{
47 48
	KConfigDialogManager::changedMap()->insert(QLatin1String("DriveSelection"),
	                                           SIGNAL(selectedDriveChanged(QString)));
49

50 51 52
	mDrivesModel = new QStandardItemModel(this);
	setModel(mDrivesModel);
	setItemDelegate(new DriveSelectionDelegate(this));
53
	setSelectionMode(QAbstractItemView::SingleSelection);
54
	setWordWrap(true);
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69
	if(!mBackupPlan->mExternalUUID.isEmpty()) {
		QStandardItem *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);
	}

70 71 72 73 74 75
	QList<Solid::Device> lDeviceList = Solid::Device::listFromType(Solid::DeviceInterface::StorageDrive);
	foreach (const Solid::Device &lDevice, lDeviceList) {
		deviceAdded(lDevice.udi());
	}
	connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceAdded(QString)), SLOT(deviceAdded(QString)));
	connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceRemoved(QString)), SLOT(deviceRemoved(QString)));
76
	connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
77
	        this, SLOT(updateSelection(QItemSelection,QItemSelection)));
78 79
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93
QString DriveSelection::mountPathOfSelectedDrive() const {
	if(mSelectedAndAccessible) {
		QStandardItem *lItem;
		findItem(DriveSelection::UUID, mSelectedUuid, &lItem);
		if(lItem != NULL) {
			Solid::Device lDevice(lItem->data(DriveSelection::UDI).toString());
			Solid::StorageAccess *lAccess = lDevice.as<Solid::StorageAccess>();
			if(lAccess) {
				return lAccess->filePath();
			}
		}
	}
	return QString();
}
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

void DriveSelection::deviceAdded(const QString &pUdi) {
	Solid::Device lDevice(pUdi);
	if(!lDevice.is<Solid::StorageDrive>()) {
		return;
	}
	Solid::StorageDrive *lDrive = lDevice.as<Solid::StorageDrive>();
	if(!lDrive->isHotpluggable()) {
		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) {
		Solid::StorageVolume *lVolume = lVolumeDevice.as<Solid::StorageVolume>();
127 128 129
		if(lVolume && !lVolume->isIgnored() && (
		         lVolume->usage() == Solid::StorageVolume::FileSystem ||
		         lVolume->usage() == Solid::StorageVolume::Encrypted)) {
130
			lVolumeDeviceList.append(lVolumeDevice);
131
		}
132 133 134 135 136 137 138 139
	}

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

	int lPartitionNumber = 1;
	foreach(Solid::Device lVolumeDevice, lVolumeDeviceList) {
		Solid::StorageVolume *lVolume = lVolumeDevice.as<Solid::StorageVolume>();
140 141
		QString lUuid = lVolume->uuid();
		if(lUuid.isEmpty()) { //seems to happen for vfat partitions
142 143 144
			lUuid += lParentDevice.description();
			lUuid += QLatin1String("|");
			lUuid += lVolume->label();
145 146 147 148 149 150 151 152 153 154 155
		}
		QStandardItem *lItem;
		bool lNeedsToBeAdded = false;
		findItem(DriveSelection::UUID, lUuid, &lItem);
		if(lItem == NULL) {
			lItem = new QStandardItem();
			lItem->setEditable(false);
			lItem->setData(lUuid, DriveSelection::UUID);
			lItem->setData(0, DriveSelection::TotalSpace);
			lItem->setData(0, DriveSelection::UsedSpace);
			lNeedsToBeAdded = true;
156 157 158 159 160 161
		}
		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);
162 163 164 165 166 167
		lItem->setData(lVolume->fsType(), DriveSelection::FileSystem);
		lItem->setData(mSyncedBackupType && (lVolume->fsType() == QLatin1String("vfat") ||
		                                     lVolume->fsType() == QLatin1String("ntfs")),
		               DriveSelection::PermissionLossWarning);
		lItem->setData(mSyncedBackupType && lVolume->fsType() == QLatin1String("vfat"),
		               DriveSelection::SymlinkLossWarning);
168 169

		Solid::StorageAccess *lAccess = lVolumeDevice.as<Solid::StorageAccess>();
170
		connect(lAccess, SIGNAL(accessibilityChanged(bool,QString)), SLOT(accessabilityChanged(bool,QString)));
171 172 173 174 175 176 177
		if(!lAccess->isAccessible()) {
			lAccess->setup();
		} else {
			KDiskFreeSpaceInfo lInfo = KDiskFreeSpaceInfo::freeSpaceInfo(lAccess->filePath());
			if(lInfo.isValid()) {
				lItem->setData(lInfo.size(), DriveSelection::TotalSpace);
				lItem->setData(lInfo.used(), DriveSelection::UsedSpace);
178
			}
179 180 181 182 183
			if(lUuid == mSelectedUuid) {
				// Selected volume was just added, could not have been accessible before.
				mSelectedAndAccessible = true;
				emit selectedDriveIsAccessibleChanged(true);
			}
184
		}
185 186
		if(lNeedsToBeAdded) {
			mDrivesModel->appendRow(lItem);
187 188 189
			if(mDrivesModel->rowCount() == 1) {
				selectionModel()->select(mDrivesModel->index(0, 0), QItemSelectionModel::ClearAndSelect);
			}
190 191 192 193 194 195
		}
		lPartitionNumber++;
	}
}

void DriveSelection::deviceRemoved(const QString &pUdi) {
196 197 198 199 200 201 202 203 204
	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 {
205
			mDrivesModel->removeRow(lRow);
206 207 208 209
		}
		if(lUuid == mSelectedUuid && mSelectedAndAccessible) {
			mSelectedAndAccessible = false;
			emit selectedDriveIsAccessibleChanged(false);
210 211
		}
	}
212 213 214
}

void DriveSelection::accessabilityChanged(bool pAccessible, const QString &pUdi) {
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	QStandardItem *lItem;
	findItem(DriveSelection::UDI, pUdi, &lItem);
	if(lItem != NULL) {
		if(pAccessible) {
			Solid::Device lDevice(pUdi);
			Solid::StorageAccess *lAccess = lDevice.as<Solid::StorageAccess>();
			if(lAccess) {
				KDiskFreeSpaceInfo lInfo = KDiskFreeSpaceInfo::freeSpaceInfo(lAccess->filePath());
				if(lInfo.isValid()) {
					lItem->setData(lInfo.size(), DriveSelection::TotalSpace);
					lItem->setData(lInfo.used(), DriveSelection::UsedSpace);
				}
			}
		}
		bool lSelectedAndAccessible = (lItem->data(DriveSelection::UUID).toString() == mSelectedUuid && pAccessible);
		if(lSelectedAndAccessible != mSelectedAndAccessible) {
			mSelectedAndAccessible = lSelectedAndAccessible;
			emit selectedDriveIsAccessibleChanged(lSelectedAndAccessible);
233
		}
234 235 236
	}
}

237
void DriveSelection::updateSelection(const QItemSelection &pSelected, const QItemSelection &pDeselected) {
238
	Q_UNUSED(pDeselected)
239
	if(!pSelected.indexes().isEmpty()) {
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
		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);
			Solid::StorageAccess *lAccess = lDevice.as<Solid::StorageAccess>();
			if(lAccess != NULL) {
				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,
		                      i18nc("@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);
276
	}
277 278 279
}

void DriveSelection::setSelectedDrive(const QString &pUuid) {
280 281
	if(pUuid == mSelectedUuid) {
		return;
282 283 284 285 286 287 288
	} else if(pUuid.isEmpty()) {
		clearSelection();
	} else {
		QStandardItem *lItem;
		findItem(DriveSelection::UUID, pUuid, &lItem);
		if(lItem != NULL) {
			setCurrentIndex(mDrivesModel->indexFromItem(lItem));
289 290 291 292 293
		}
	}
}

void DriveSelection::saveExtraData() {
294 295 296 297 298 299 300 301
	QStandardItem *lItem;
	findItem(DriveSelection::UUID, mSelectedUuid, &lItem);
	if(lItem != NULL) {
		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();
302
	}
303 304
}

305 306 307 308 309 310 311 312 313 314 315 316
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 == QLatin1String("vfat") ||
		                                                     lFsType == QLatin1String("ntfs")),
		                               DriveSelection::PermissionLossWarning);
		mDrivesModel->item(i)->setData(mSyncedBackupType && lFsType == QLatin1String("vfat"),
		                               DriveSelection::SymlinkLossWarning);
	}
}

317 318
int DriveSelection::findItem(const DriveSelection::DataType pField, const QString &pSearchString,
                             QStandardItem **pReturnedItem) const {
319 320
	for(int lRow = 0; lRow < mDrivesModel->rowCount(); ++lRow) {
		QStandardItem *lItem = mDrivesModel->item(lRow);
321 322 323 324 325
		if(lItem->data(pField).toString() == pSearchString) {
			if(pReturnedItem != NULL) {
				*pReturnedItem = lItem;
			}
			return lRow;
326 327
		}
	}
328 329 330 331
	if(pReturnedItem != NULL) {
		*pReturnedItem = NULL;
	}
	return -1;
332
}