diff --git a/app/fileoperations.cpp b/app/fileoperations.cpp index 4c6f26bb..160a2009 100644 --- a/app/fileoperations.cpp +++ b/app/fileoperations.cpp @@ -1,248 +1,248 @@ // vim: set tabstop=4 shiftwidth=4 expandtab: /* Gwenview: an image viewer Copyright 2007 Aurélien Gâteau 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., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA. */ // Self #include "fileoperations.h" // Qt #include #include #include #include // KDE #include #include #include #include #include #include #include // Local #include #include #include namespace Gwenview { namespace FileOperations { static void copyMoveOrLink(Operation operation, const QList& urlList, QWidget* parent, ContextManager* contextManager) { Q_ASSERT(!urlList.isEmpty()); const int numberOfImages = urlList.count(); QFileDialog dialog(parent->nativeParentWidget(), QString()); dialog.setAcceptMode(QFileDialog::AcceptSave); // Figure out what the window title and buttons should say, // depending on the operation and how many images are selected switch (operation) { case COPY: if (numberOfImages == 1) { dialog.setWindowTitle(i18nc("@title:window %1 file name", "Copy %1", urlList.constFirst().fileName())); } else { dialog.setWindowTitle(i18ncp("@title:window %1 number of images", "Copy %1 image", "Copy %1 images", numberOfImages)); } dialog.setLabelText(QFileDialog::DialogLabel::Accept, i18nc("@action:button", "Copy")); break; case MOVE: if (numberOfImages == 1) { dialog.setWindowTitle(i18nc("@title:window %1 file name", "Move %1", urlList.constFirst().fileName())); } else { dialog.setWindowTitle(i18ncp("@title:window %1 number of images", "Move %1 image", "Move %1 images", numberOfImages)); } dialog.setLabelText(QFileDialog::DialogLabel::Accept, i18nc("@action:button", "Move")); break; case LINK: if (numberOfImages == 1) { dialog.setWindowTitle(i18nc("@title:window %1 file name", "Link %1", urlList.constFirst().fileName())); } else { dialog.setWindowTitle(i18ncp("@title:window %1 number of images", "Link %1 image", "Link %1 images", numberOfImages)); } dialog.setLabelText(QFileDialog::DialogLabel::Accept, i18nc("@action:button", "Link")); break; default: Q_ASSERT(0); } if (numberOfImages == 1) { dialog.setFileMode(QFileDialog::AnyFile); dialog.selectUrl(urlList.constFirst()); } else { dialog.setFileMode(QFileDialog::Directory); dialog.setOption(QFileDialog::ShowDirsOnly, true); } - dialog.setDirectoryUrl(contextManager->targetUrl()); + dialog.setDirectoryUrl(contextManager->targetDirUrl()); if (!dialog.exec()) { return; } QUrl destUrl = dialog.selectedUrls().first(); KIO::CopyJob* job = 0; switch (operation) { case COPY: job = KIO::copy(urlList, destUrl); break; case MOVE: job = KIO::move(urlList, destUrl); break; case LINK: job = KIO::link(urlList, destUrl); break; default: Q_ASSERT(0); } KJobWidgets::setWindow(job, parent); job->uiDelegate()->setAutoErrorHandlingEnabled(true); if (numberOfImages == 1) { destUrl = destUrl.adjusted(QUrl::RemoveFilename|QUrl::StripTrailingSlash); } - contextManager->setTargetUrl(destUrl); + contextManager->setTargetDirUrl(destUrl); } static void delOrTrash(KIO::JobUiDelegate::DeletionType deletionType, const QList& urlList, QWidget* parent) { Q_ASSERT(urlList.count() > 0); KIO::JobUiDelegate uiDelegate; uiDelegate.setWindow(parent); if (!uiDelegate.askDeleteConfirmation(urlList, deletionType, KIO::JobUiDelegate::DefaultConfirmation)) { return; } KIO::Job* job = 0; switch (deletionType) { case KIO::JobUiDelegate::Trash: job = KIO::trash(urlList); break; case KIO::JobUiDelegate::Delete: job = KIO::del(urlList); break; default: // e.g. EmptyTrash return; } Q_ASSERT(job); KJobWidgets::setWindow(job,parent); Q_FOREACH(const QUrl &url, urlList) { DocumentFactory::instance()->forget(url); } } void copyTo(const QList& urlList, QWidget* parent, ContextManager* contextManager) { copyMoveOrLink(COPY, urlList, parent, contextManager); } void moveTo(const QList& urlList, QWidget* parent, ContextManager* contextManager) { copyMoveOrLink(MOVE, urlList, parent, contextManager); } void linkTo(const QList& urlList, QWidget* parent, ContextManager* contextManager) { copyMoveOrLink(LINK, urlList, parent, contextManager); } void trash(const QList& urlList, QWidget* parent) { delOrTrash(KIO::JobUiDelegate::Trash, urlList, parent); } void del(const QList& urlList, QWidget* parent) { delOrTrash(KIO::JobUiDelegate::Delete, urlList, parent); } void showMenuForDroppedUrls(QWidget* parent, const QList& urlList, const QUrl &destUrl) { if (urlList.isEmpty()) { qWarning() << "urlList is empty!"; return; } if (!destUrl.isValid()) { qWarning() << "destUrl is not valid!"; return; } QMenu menu(parent); QAction* moveAction = menu.addAction( QIcon::fromTheme("go-jump"), i18n("Move Here")); QAction* copyAction = menu.addAction( QIcon::fromTheme("edit-copy"), i18n("Copy Here")); QAction* linkAction = menu.addAction( QIcon::fromTheme("edit-link"), i18n("Link Here")); menu.addSeparator(); menu.addAction( QIcon::fromTheme("process-stop"), i18n("Cancel")); QAction* action = menu.exec(QCursor::pos()); KIO::Job* job = 0; if (action == moveAction) { job = KIO::move(urlList, destUrl); } else if (action == copyAction) { job = KIO::copy(urlList, destUrl); } else if (action == linkAction) { job = KIO::link(urlList, destUrl); } else { return; } Q_ASSERT(job); KJobWidgets::setWindow(job, parent); } void rename(const QUrl &oldUrl, QWidget* parent) { QString name = QInputDialog::getText(parent, i18nc("@title:window", "Rename") /* caption */, xi18n("Rename %1 to:", oldUrl.fileName()) /* label */, QLineEdit::Normal, oldUrl.fileName() /* value */ ); if (name.isEmpty() || name == oldUrl.fileName()) { return; } QUrl newUrl = oldUrl; newUrl = newUrl.adjusted(QUrl::RemoveFilename); newUrl.setPath(newUrl.path() + name); KIO::SimpleJob* job = KIO::rename(oldUrl, newUrl, KIO::HideProgressInfo); KJobWidgets::setWindow(job, parent); if (!job->exec()) { job->uiDelegate()->showErrorMessage(); return; } ThumbnailProvider::moveThumbnail(oldUrl, newUrl); } } // namespace } // namespace diff --git a/lib/contextmanager.cpp b/lib/contextmanager.cpp index ba44a414..9e8fb352 100644 --- a/lib/contextmanager.cpp +++ b/lib/contextmanager.cpp @@ -1,364 +1,364 @@ /* Gwenview: an image viewer Copyright 2007 Aurélien Gâteau 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "contextmanager.h" #include "contextmanager.h" // Qt #include #include #include #include // KDE #include #include #include // Local #include #include #include #include namespace Gwenview { struct ContextManagerPrivate { SortedDirModel* mDirModel; QItemSelectionModel* mSelectionModel; QUrl mCurrentDirUrl; QUrl mCurrentUrl; QUrl mUrlToSelect; - QUrl mTargetUrl; + QUrl mTargetDirUrl; bool mSelectedFileItemListNeedsUpdate; QSet mQueuedSignals; KFileItemList mSelectedFileItemList; bool mDirListerFinished = false; QTimer* mQueuedSignalsTimer; void queueSignal(const QByteArray& signal) { mQueuedSignals << signal; mQueuedSignalsTimer->start(); } void updateSelectedFileItemList() { if (!mSelectedFileItemListNeedsUpdate) { return; } mSelectedFileItemList.clear(); QItemSelection selection = mSelectionModel->selection(); Q_FOREACH(const QModelIndex & index, selection.indexes()) { mSelectedFileItemList << mDirModel->itemForIndex(index); } // At least add current url if it's valid (it may not be in // the list if we are viewing a non-browsable url, for example // using http protocol) if (mSelectedFileItemList.isEmpty() && mCurrentUrl.isValid()) { KFileItem item(mCurrentUrl); mSelectedFileItemList << item; } mSelectedFileItemListNeedsUpdate = false; } }; ContextManager::ContextManager(SortedDirModel* dirModel, QObject* parent) : QObject(parent) , d(new ContextManagerPrivate) { d->mQueuedSignalsTimer = new QTimer(this); d->mQueuedSignalsTimer->setInterval(100); d->mQueuedSignalsTimer->setSingleShot(true); connect(d->mQueuedSignalsTimer, &QTimer::timeout, this, &ContextManager::emitQueuedSignals); d->mDirModel = dirModel; connect(d->mDirModel, &SortedDirModel::dataChanged, this, &ContextManager::slotDirModelDataChanged); /* HACK! In extended-selection mode, when the current index is removed, * QItemSelectionModel selects the previous index if there is one, if not it * selects the next index. This is not what we want: when the user removes * an image, he expects to go to the next one, not the previous one. * * To overcome this, we must connect to the mDirModel.rowsAboutToBeRemoved() * signal *before* QItemSelectionModel connects to it, so that our slot is * called before QItemSelectionModel slot. This allows us to pick a new * current index ourself, leaving QItemSelectionModel slot with nothing to * do. * * This is the reason ContextManager creates a QItemSelectionModel itself: * doing so ensures QItemSelectionModel cannot be connected to the * mDirModel.rowsAboutToBeRemoved() signal before us. */ connect(d->mDirModel, &SortedDirModel::rowsAboutToBeRemoved, this, &ContextManager::slotRowsAboutToBeRemoved); connect(d->mDirModel, &SortedDirModel::rowsInserted, this, &ContextManager::slotRowsInserted); connect(d->mDirModel->dirLister(), SIGNAL(redirection(QUrl)), SLOT(slotDirListerRedirection(QUrl))); connect(d->mDirModel->dirLister(), static_cast(&KDirLister::completed), this, &ContextManager::slotDirListerCompleted); d->mSelectionModel = new QItemSelectionModel(d->mDirModel); connect(d->mSelectionModel, &QItemSelectionModel::selectionChanged, this, &ContextManager::slotSelectionChanged); connect(d->mSelectionModel, &QItemSelectionModel::currentChanged, this, &ContextManager::slotCurrentChanged); d->mSelectedFileItemListNeedsUpdate = false; } ContextManager::~ContextManager() { delete d; } void ContextManager::loadConfig() { - setTargetUrl(QUrl(GwenviewConfig::lastTargetDir())); + setTargetDirUrl(QUrl(GwenviewConfig::lastTargetDir())); } void ContextManager::saveConfig() const { - GwenviewConfig::setLastTargetDir(targetUrl().toString()); + GwenviewConfig::setLastTargetDir(targetDirUrl().toString()); } QItemSelectionModel* ContextManager::selectionModel() const { return d->mSelectionModel; } void ContextManager::setCurrentUrl(const QUrl ¤tUrl) { if (d->mCurrentUrl == currentUrl) { return; } d->mCurrentUrl = currentUrl; if (!d->mCurrentUrl.isEmpty()) { Document::Ptr doc = DocumentFactory::instance()->load(currentUrl); QUndoGroup* undoGroup = DocumentFactory::instance()->undoGroup(); undoGroup->addStack(doc->undoStack()); undoGroup->setActiveStack(doc->undoStack()); } d->mSelectedFileItemListNeedsUpdate = true; currentUrlChanged(currentUrl); } KFileItemList ContextManager::selectedFileItemList() const { d->updateSelectedFileItemList(); return d->mSelectedFileItemList; } void ContextManager::setCurrentDirUrl(const QUrl &url) { if (url == d->mCurrentDirUrl) { return; } if (url.isValid() && KProtocolManager::supportsListing(url)) { d->mCurrentDirUrl = url; d->mDirModel->dirLister()->openUrl(url); d->mDirListerFinished = false; } else { d->mCurrentDirUrl.clear(); d->mDirModel->dirLister()->clear(); } currentDirUrlChanged(url); } QUrl ContextManager::currentDirUrl() const { return d->mCurrentDirUrl; } QUrl ContextManager::currentUrl() const { return d->mCurrentUrl; } SortedDirModel* ContextManager::dirModel() const { return d->mDirModel; } void ContextManager::slotDirModelDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight) { // Data change can happen in the following cases: // - items have been renamed // - item bytes have been modified // - item meta info has been retrieved or modified // // If a selected item is affected, schedule emission of a // selectionDataChanged() signal. Don't emit it directly to avoid spamming // the context items in case of a mass change. QModelIndexList selectionList = d->mSelectionModel->selectedIndexes(); if (selectionList.isEmpty()) { return; } QModelIndexList changedList; for (int row = topLeft.row(); row <= bottomRight.row(); ++row) { changedList << d->mDirModel->index(row, 0); } QModelIndexList& shortList = selectionList; QModelIndexList& longList = changedList; if (shortList.length() > longList.length()) { qSwap(shortList, longList); } Q_FOREACH(const QModelIndex & index, shortList) { if (longList.contains(index)) { d->mSelectedFileItemListNeedsUpdate = true; d->queueSignal("selectionDataChanged"); return; } } } void ContextManager::slotSelectionChanged() { d->mSelectedFileItemListNeedsUpdate = true; if (!d->mSelectionModel->hasSelection()) { setCurrentUrl(QUrl()); } d->queueSignal("selectionChanged"); } void Gwenview::ContextManager::slotCurrentChanged(const QModelIndex& index) { QUrl url = d->mDirModel->urlForIndex(index); setCurrentUrl(url); } void ContextManager::emitQueuedSignals() { Q_FOREACH(const QByteArray & signal, d->mQueuedSignals) { QMetaObject::invokeMethod(this, signal.data()); } d->mQueuedSignals.clear(); } void Gwenview::ContextManager::slotRowsAboutToBeRemoved(const QModelIndex& /*parent*/, int start, int end) { QModelIndex oldCurrent = d->mSelectionModel->currentIndex(); if (oldCurrent.row() < start || oldCurrent.row() > end) { // currentIndex has not been removed return; } QModelIndex newCurrent; if (end + 1 < d->mDirModel->rowCount()) { newCurrent = d->mDirModel->index(end + 1, 0); } else if (start > 0) { newCurrent = d->mDirModel->index(start - 1, 0); } else { // No index we can select, nothing to do return; } d->mSelectionModel->select(oldCurrent, QItemSelectionModel::Deselect); d->mSelectionModel->setCurrentIndex(newCurrent, QItemSelectionModel::Select); } bool ContextManager::currentUrlIsRasterImage() const { return MimeTypeUtils::urlKind(currentUrl()) == MimeTypeUtils::KIND_RASTER_IMAGE; } QUrl ContextManager::urlToSelect() const { return d->mUrlToSelect; } void ContextManager::setUrlToSelect(const QUrl &url) { GV_RETURN_IF_FAIL(url.isValid()); d->mUrlToSelect = url; setCurrentDirUrl(url.adjusted(QUrl::RemoveFilename)); setCurrentUrl(url); selectUrlToSelect(); } -QUrl ContextManager::targetUrl() const +QUrl ContextManager::targetDirUrl() const { - return d->mTargetUrl; + return d->mTargetDirUrl; } -void ContextManager::setTargetUrl(const QUrl &url) +void ContextManager::setTargetDirUrl(const QUrl &url) { GV_RETURN_IF_FAIL(url.isValid()); - d->mTargetUrl = url; + d->mTargetDirUrl = url; } void ContextManager::slotRowsInserted() { // We reach this method when rows have been inserted in the model, but views // may not have been updated yet and thus do not have the matching items. // Delay the selection of mUrlToSelect so that the view items exist. // // Without this, when Gwenview is started with an image as argument and the // thumbnail bar is visible, the image will not be selected in the thumbnail // bar. if (d->mUrlToSelect.isValid()) { QMetaObject::invokeMethod(this, "selectUrlToSelect", Qt::QueuedConnection); } } void ContextManager::selectUrlToSelect() { // Because of the queued connection above we might be called several times in a row // In this case we don't want the warning below if (d->mUrlToSelect.isEmpty()) { return; } GV_RETURN_IF_FAIL(d->mUrlToSelect.isValid()); QModelIndex index = d->mDirModel->indexForUrl(d->mUrlToSelect); if (index.isValid()) { d->mSelectionModel->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect); d->mUrlToSelect = QUrl(); } else if (d->mDirListerFinished) { // Desired URL cannot be found in the directory // Clear the selection to avoid dragging any local files into context // and manually set current URL d->mSelectionModel->clearSelection(); setCurrentUrl(d->mUrlToSelect); d->mUrlToSelect.clear(); } } void ContextManager::slotDirListerRedirection(const QUrl &newUrl) { setCurrentDirUrl(newUrl); } void ContextManager::slotDirListerCompleted() { d->mDirListerFinished = true; } } // namespace diff --git a/lib/contextmanager.h b/lib/contextmanager.h index ac1c37fa..8d6aae1f 100644 --- a/lib/contextmanager.h +++ b/lib/contextmanager.h @@ -1,106 +1,106 @@ /* Gwenview: an image viewer Copyright 2007 Aurélien Gâteau 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef CONTEXTMANAGER_H #define CONTEXTMANAGER_H #include // Qt #include // KDE #include #include class QItemSelectionModel; class QModelIndex; namespace Gwenview { class SortedDirModel; struct ContextManagerPrivate; /** * Manages the state of the application. * TODO: Most of GvCore should be merged in this class */ class GWENVIEWLIB_EXPORT ContextManager : public QObject { Q_OBJECT public: ContextManager(SortedDirModel*, QObject* parent); ~ContextManager(); void loadConfig(); void saveConfig() const; QUrl currentUrl() const; void setCurrentDirUrl(const QUrl&); QUrl currentDirUrl() const; void setCurrentUrl(const QUrl ¤tUrl); KFileItemList selectedFileItemList() const; SortedDirModel* dirModel() const; QItemSelectionModel* selectionModel() const; bool currentUrlIsRasterImage() const; QUrl urlToSelect() const; void setUrlToSelect(const QUrl&); - QUrl targetUrl() const; + QUrl targetDirUrl() const; - void setTargetUrl(const QUrl&); + void setTargetDirUrl(const QUrl&); Q_SIGNALS: void currentDirUrlChanged(const QUrl&); void currentUrlChanged(const QUrl&); void selectionChanged(); void selectionDataChanged(); public Q_SLOTS: void slotSelectionChanged(); private Q_SLOTS: void slotDirModelDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight); void slotCurrentChanged(const QModelIndex&); void emitQueuedSignals(); void slotRowsAboutToBeRemoved(const QModelIndex& /*parent*/, int start, int end); void slotRowsInserted(); void selectUrlToSelect(); void slotDirListerRedirection(const QUrl&); void slotDirListerCompleted(); private: ContextManagerPrivate* const d; }; } // namespace #endif /* CONTEXTMANAGER_H */