+ const PlacesItem* item = placesItem(index);
+ if (!item) {
+ return 0;
+ }
+
+ Solid::Device device = item->device();
+ const bool providesTearDown = device.is<Solid::StorageAccess>() &&
+ device.as<Solid::StorageAccess>()->isAccessible();
+ if (!providesTearDown) {
+ return 0;
+ }
+
+ Solid::StorageDrive* drive = device.as<Solid::StorageDrive>();
+ if (!drive) {
+ drive = device.parent().as<Solid::StorageDrive>();
+ }
+
+ bool hotPluggable = false;
+ bool removable = false;
+ if (drive) {
+ hotPluggable = drive->isHotpluggable();
+ removable = drive->isRemovable();
+ }
+
+ QString iconName;
+ QString text;
+ const QString label = item->text();
+ if (device.is<Solid::OpticalDisc>()) {
+ text = i18nc("@item", "Release '%1'", label);
+ } else if (removable || hotPluggable) {
+ text = i18nc("@item", "Safely Remove '%1'", label);
+ iconName = "media-eject";
+ } else {
+ text = i18nc("@item", "Unmount '%1'", label);
+ iconName = "media-eject";
+ }
+
+ 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<Solid::OpticalDrive>();
+ if (drive) {
+ connect(drive, SIGNAL(ejectDone(Solid::ErrorType,QVariant,QString)),
+ this, SLOT(slotStorageTeardownDone(Solid::ErrorType,QVariant)),
+ Qt::UniqueConnection);
+ drive->eject();
+ } 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::requestTeardown(int index)
+{
+ const PlacesItem* item = placesItem(index);
+ if (item) {
+ Solid::StorageAccess* access = item->device().as<Solid::StorageAccess>();
+ if (access) {
+ connect(access, SIGNAL(teardownDone(Solid::ErrorType,QVariant,QString)),
+ this, SLOT(slotStorageTeardownDone(Solid::ErrorType,QVariant)),
+ Qt::UniqueConnection);
+ access->teardown();
+ }
+ }
+}
+
+bool PlacesItemModel::storageSetupNeeded(int index) const
+{
+ const PlacesItem* item = placesItem(index);
+ return item ? item->storageSetupNeeded() : false;
+}
+
+void PlacesItemModel::requestStorageSetup(int index)
+{
+ const PlacesItem* item = placesItem(index);
+ if (!item) {
+ return;
+ }
+
+ Solid::Device device = item->device();
+ const bool setup = device.is<Solid::StorageAccess>()
+ && !m_storageSetupInProgress.contains(device.as<Solid::StorageAccess>())
+ && !device.as<Solid::StorageAccess>()->isAccessible();
+ if (setup) {
+ Solid::StorageAccess* access = device.as<Solid::StorageAccess>();
+
+ m_storageSetupInProgress[access] = index;
+
+ connect(access, SIGNAL(setupDone(Solid::ErrorType,QVariant,QString)),
+ this, SLOT(slotStorageSetupDone(Solid::ErrorType,QVariant,QString)),
+ Qt::UniqueConnection);
+
+ access->setup();
+ }
+}
+
+QMimeData* PlacesItemModel::createMimeData(const KItemSet& indexes) const
+{
+ KUrl::List urls;
+ QByteArray itemData;
+
+ QDataStream stream(&itemData, QIODevice::WriteOnly);
+
+ foreach (int index, indexes) {
+ const KUrl itemUrl = placesItem(index)->url();
+ if (itemUrl.isValid()) {
+ urls << itemUrl;
+ }
+ stream << index;
+ }
+
+ QMimeData* mimeData = new QMimeData();
+ if (!urls.isEmpty()) {
+ urls.populateMimeData(mimeData);
+ }
+ mimeData->setData(internalMimeType(), itemData);
+
+ return mimeData;
+}
+
+bool PlacesItemModel::supportsDropping(int index) const
+{
+ return index >= 0 && index < count();
+}
+
+void PlacesItemModel::dropMimeDataBefore(int index, const QMimeData* mimeData)
+{
+ if (mimeData->hasFormat(internalMimeType())) {
+ // The item has been moved inside the view
+ QByteArray itemData = mimeData->data(internalMimeType());
+ QDataStream stream(&itemData, QIODevice::ReadOnly);
+ int oldIndex;
+ stream >> oldIndex;
+ if (oldIndex == index || oldIndex == index - 1) {
+ // No moving has been done
+ return;
+ }
+
+ PlacesItem* oldItem = placesItem(oldIndex);
+ if (!oldItem) {
+ return;
+ }
+
+ PlacesItem* newItem = new PlacesItem(oldItem->bookmark());
+ removeItem(oldIndex);
+
+ if (oldIndex < index) {
+ --index;
+ }
+
+ const int dropIndex = groupedDropIndex(index, newItem);
+ insertItem(dropIndex, newItem);
+ } else if (mimeData->hasFormat("text/uri-list")) {
+ // One or more items must be added to the model
+ const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
+ for (int i = urls.count() - 1; i >= 0; --i) {
+ const KUrl& url = urls[i];
+
+ QString text = url.fileName();
+ if (text.isEmpty()) {
+ text = url.host();
+ }
+
+ if ((url.isLocalFile() && !QFileInfo(url.toLocalFile()).isDir())
+ || url.protocol() == "trash") {
+ // Only directories outside the trash are allowed
+ continue;
+ }
+
+ PlacesItem* newItem = createPlacesItem(text, url);
+ const int dropIndex = groupedDropIndex(index, newItem);
+ insertItem(dropIndex, newItem);
+ }
+ }
+}
+
+KUrl PlacesItemModel::convertedUrl(const KUrl& url)
+{
+ KUrl newUrl = url;
+ if (url.protocol() == QLatin1String("timeline")) {
+ newUrl = createTimelineUrl(url);
+ } else if (url.protocol() == QLatin1String("search")) {
+ newUrl = createSearchUrl(url);
+ }
+
+ return newUrl;
+}
+
+void PlacesItemModel::onItemInserted(int index)
+{
+ const PlacesItem* insertedItem = placesItem(index);
+ if (insertedItem) {
+ // Take care to apply the PlacesItemModel-order of the inserted item
+ // also to the bookmark-manager.
+ const KBookmark insertedBookmark = insertedItem->bookmark();
+
+ const PlacesItem* previousItem = placesItem(index - 1);
+ KBookmark previousBookmark;
+ if (previousItem) {
+ previousBookmark = previousItem->bookmark();
+ }
+
+ m_bookmarkManager->root().moveBookmark(insertedBookmark, previousBookmark);
+ }
+
+ if (index == count() - 1) {
+ // The item has been appended as last item to the list. In this
+ // case assure that it is also appended after the hidden items and
+ // not before (like done otherwise).
+ m_bookmarkedItems.append(0);
+ } else {
+
+ int modelIndex = -1;
+ int bookmarkIndex = 0;
+ while (bookmarkIndex < m_bookmarkedItems.count()) {
+ if (!m_bookmarkedItems[bookmarkIndex]) {
+ ++modelIndex;
+ if (modelIndex + 1 == index) {
+ break;
+ }
+ }
+ ++bookmarkIndex;
+ }
+ m_bookmarkedItems.insert(bookmarkIndex, 0);
+ }
+
+ triggerBookmarksSaving();
+
+#ifdef PLACESITEMMODEL_DEBUG
+ kDebug() << "Inserted item" << index;
+ showModelState();
+#endif
+}
+
+void PlacesItemModel::onItemRemoved(int index, KStandardItem* removedItem)
+{
+ PlacesItem* placesItem = dynamic_cast<PlacesItem*>(removedItem);
+ if (placesItem) {
+ const KBookmark bookmark = placesItem->bookmark();
+ m_bookmarkManager->root().deleteBookmark(bookmark);
+ }
+
+ const int boomarkIndex = bookmarkIndex(index);
+ Q_ASSERT(!m_bookmarkedItems[boomarkIndex]);
+ m_bookmarkedItems.removeAt(boomarkIndex);
+
+ triggerBookmarksSaving();
+
+#ifdef PLACESITEMMODEL_DEBUG
+ kDebug() << "Removed item" << index;
+ showModelState();
+#endif
+}
+
+void PlacesItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRoles)
+{
+ const PlacesItem* changedItem = placesItem(index);
+ if (changedItem) {
+ // Take care to apply the PlacesItemModel-order of the changed item
+ // also to the bookmark-manager.
+ const KBookmark insertedBookmark = changedItem->bookmark();
+
+ const PlacesItem* previousItem = placesItem(index - 1);
+ KBookmark previousBookmark;
+ if (previousItem) {
+ previousBookmark = previousItem->bookmark();
+ }
+
+ m_bookmarkManager->root().moveBookmark(insertedBookmark, previousBookmark);
+ }
+
+ if (changedRoles.contains("isHidden")) {
+ if (!m_hiddenItemsShown && changedItem->isHidden()) {
+ m_hiddenItemToRemove = index;
+ QTimer::singleShot(0, this, SLOT(hideItem()));
+ }
+ }
+
+ triggerBookmarksSaving();
+}
+
+void PlacesItemModel::slotDeviceAdded(const QString& udi)
+{
+ const Solid::Device device(udi);
+
+ if (!m_predicate.matches(device)) {
+ return;
+ }
+
+ m_availableDevices << udi;
+ const KBookmark bookmark = PlacesItem::createDeviceBookmark(m_bookmarkManager, udi);
+ appendItem(new PlacesItem(bookmark));
+}
+
+void PlacesItemModel::slotDeviceRemoved(const QString& udi)
+{
+ if (!m_availableDevices.contains(udi)) {
+ return;
+ }
+
+ for (int i = 0; i < m_bookmarkedItems.count(); ++i) {
+ PlacesItem* item = m_bookmarkedItems[i];
+ if (item && item->udi() == udi) {
+ m_bookmarkedItems.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::slotStorageSetupDone(Solid::ErrorType error,
+ const QVariant& errorData,
+ const QString& udi)
+{
+ Q_UNUSED(udi);
+
+ Q_ASSERT(!m_storageSetupInProgress.isEmpty());
+ const int index = m_storageSetupInProgress.take(sender());
+ const PlacesItem* item = placesItem(index);
+ if (!item) {
+ return;
+ }
+
+ if (error) {
+ if (errorData.isValid()) {
+ emit errorMessage(i18nc("@info", "An error occurred while accessing '%1', the system responded: %2",
+ item->text(),
+ errorData.toString()));
+ } else {
+ emit errorMessage(i18nc("@info", "An error occurred while accessing '%1'",
+ item->text()));
+ }
+ emit storageSetupDone(index, false);
+ } else {
+ emit storageSetupDone(index, true);
+ }
+}
+
+void PlacesItemModel::hideItem()
+{
+ hideItem(m_hiddenItemToRemove);
+ m_hiddenItemToRemove = -1;
+}
+
+void PlacesItemModel::updateBookmarks()
+{
+ // Verify whether new bookmarks have been added or existing
+ // bookmarks have been changed.
+ KBookmarkGroup root = m_bookmarkManager->root();
+ KBookmark newBookmark = root.first();
+ while (!newBookmark.isNull()) {
+ if (acceptBookmark(newBookmark, m_availableDevices)) {
+ bool found = false;
+ int modelIndex = 0;
+ for (int i = 0; i < m_bookmarkedItems.count(); ++i) {
+ PlacesItem* item = m_bookmarkedItems[i];
+ if (!item) {
+ item = placesItem(modelIndex);
+ ++modelIndex;
+ }
+
+ const KBookmark oldBookmark = item->bookmark();
+ if (equalBookmarkIdentifiers(newBookmark, oldBookmark)) {
+ // The bookmark has been found in the model or as
+ // a hidden item. The content of the bookmark might
+ // have been changed, so an update is done.
+ found = true;
+ if (newBookmark.metaDataItem("UDI").isEmpty()) {
+ item->setBookmark(newBookmark);
+ item->setText(i18nc("KFile System Bookmarks", newBookmark.text().toUtf8().data()));
+ }
+ break;
+ }
+ }
+
+ if (!found) {
+ const QString udi = newBookmark.metaDataItem("UDI");
+
+ /*
+ * See Bug 304878
+ * Only add a new places item, if the item text is not empty
+ * and if the device is available. Fixes the strange behaviour -
+ * add a places item without text in the Places section - when you
+ * remove a device (e.g. a usb stick) without unmounting.
+ */
+ if (udi.isEmpty() || Solid::Device(udi).isValid()) {
+ PlacesItem* item = new PlacesItem(newBookmark);
+ if (item->isHidden() && !m_hiddenItemsShown) {
+ m_bookmarkedItems.append(item);
+ } else {
+ appendItemToGroup(item);
+ }
+ }
+ }
+ }
+
+ newBookmark = root.next(newBookmark);
+ }
+
+ // Remove items that are not part of the bookmark-manager anymore
+ int modelIndex = 0;
+ for (int i = m_bookmarkedItems.count() - 1; i >= 0; --i) {
+ PlacesItem* item = m_bookmarkedItems[i];
+ const bool itemIsPartOfModel = (item == 0);
+ if (itemIsPartOfModel) {
+ item = placesItem(modelIndex);
+ }
+
+ bool hasBeenRemoved = true;
+ const KBookmark oldBookmark = item->bookmark();
+ KBookmark newBookmark = root.first();
+ while (!newBookmark.isNull()) {
+ if (equalBookmarkIdentifiers(newBookmark, oldBookmark)) {
+ hasBeenRemoved = false;
+ break;
+ }
+ newBookmark = root.next(newBookmark);
+ }
+
+ if (hasBeenRemoved) {
+ if (m_bookmarkedItems[i]) {
+ delete m_bookmarkedItems[i];
+ m_bookmarkedItems.removeAt(i);
+ } else {
+ removeItem(modelIndex);
+ --modelIndex;
+ }
+ }
+
+ if (itemIsPartOfModel) {
+ ++modelIndex;
+ }
+ }
+}
+
+void PlacesItemModel::saveBookmarks()
+{
+ m_bookmarkManager->emitChanged(m_bookmarkManager->root());