From 28daa45a44bc172747bad6d948de2a58c8586845 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Fri, 11 May 2012 23:00:26 +0200 Subject: Places Panel: Implement eject and teardown actions Further fixes: - Add/remove item when device has been added/removed - Update emblem if the accessibility-state has been changed --- src/panels/places/placesitemmodel.cpp | 110 ++++++++++++++++++++++++++++------ 1 file changed, 93 insertions(+), 17 deletions(-) (limited to 'src/panels/places/placesitemmodel.cpp') diff --git a/src/panels/places/placesitemmodel.cpp b/src/panels/places/placesitemmodel.cpp index 48f54b276..815338763 100644 --- a/src/panels/places/placesitemmodel.cpp +++ b/src/panels/places/placesitemmodel.cpp @@ -48,6 +48,10 @@ #include #include +#include +#include +#include +#include PlacesItemModel::PlacesItemModel(QObject* parent) : KStandardItemModel(parent), @@ -221,31 +225,82 @@ QAction* PlacesItemModel::ejectAction(int index) const return 0; } -QAction* PlacesItemModel::tearDownAction(int index) const +QAction* PlacesItemModel::teardownAction(int index) const { - // TODO: This is a dummy-implementation to have at least all - // translation-strings as part of the code before the freeze + const PlacesItem* item = placesItem(index); + if (!item) { + return 0; + } + + Solid::Device device = item->device(); + const bool providesTearDown = device.is() && + device.as()->isAccessible(); + if (!providesTearDown) { + return 0; + } + + Solid::StorageDrive* drive = device.as(); + if (!drive) { + drive = device.parent().as(); + } + + bool hotPluggable = false; + bool removable = false; + if (drive) { + hotPluggable = drive->isHotpluggable(); + removable = drive->isRemovable(); + } + QString iconName; QString text; - QString label; - switch (index) { - case 0: + const QString label = item->text(); + if (device.is()) { text = i18nc("@item", "Release '%1'", label); - break; - case 1: + } else if (removable || hotPluggable) { text = i18nc("@item", "Safely Remove '%1'", label); iconName = "media-eject"; - break; - case 2: + } else { text = i18nc("@item", "Unmount '%1'", label); iconName = "media-eject"; - break; - default: - break; } - //return new QAction(KIcon(iconName), text, 0); - return 0; + if (iconName.isEmpty()) { + return new QAction(text, 0); + } + + return new QAction(KIcon(iconName), text, 0); +} + +void PlacesItemModel::requestEject(int index) +{ + const PlacesItem* item = placesItem(index); + if (item) { + Solid::OpticalDrive* drive = item->device().parent().as(); + if (drive) { + connect(drive, SIGNAL(ejectDone(Solid::ErrorType,QVariant,QString)), + this, SLOT(slotStorageTeardownDone(Solid::ErrorType,QVariant))); + drive->eject(); + } else { + + } + } +} + +void PlacesItemModel::requestTeardown(int index) +{ + const PlacesItem* item = placesItem(index); + if (item) { + Solid::StorageAccess* access = item->device().as(); + if (access) { + connect(access, SIGNAL(teardownDone(Solid::ErrorType,QVariant,QString)), + this, SLOT(slotStorageTeardownDone(Solid::ErrorType,QVariant))); + access->teardown(); + } else { + const QString label = item->text(); + const QString message = i18nc("@info", "The device '%1' is not a disk and cannot be ejected.", label); + emit errorMessage(message); + } + } } void PlacesItemModel::onItemInserted(int index) @@ -283,12 +338,33 @@ void PlacesItemModel::onItemRemoved(int index) void PlacesItemModel::slotDeviceAdded(const QString& udi) { - Q_UNUSED(udi); + appendItem(new PlacesItem(udi)); } void PlacesItemModel::slotDeviceRemoved(const QString& udi) { - Q_UNUSED(udi); + for (int i = 0; i < m_hiddenItems.count(); ++i) { + PlacesItem* item = m_hiddenItems[i]; + if (item && item->udi() == udi) { + m_hiddenItems.removeAt(i); + delete item; + return; + } + } + + for (int i = 0; i < count(); ++i) { + if (placesItem(i)->udi() == udi) { + removeItem(i); + return; + } + } +} + +void PlacesItemModel::slotStorageTeardownDone(Solid::ErrorType error, const QVariant& errorData) +{ + if (error && errorData.isValid()) { + emit errorMessage(errorData.toString()); + } } void PlacesItemModel::loadBookmarks() -- cgit v1.3.1