diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index ae50be94..a8d6aa31 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -94,6 +94,20 @@ #include #endif +namespace { +template +void moveAndConnectRemoverToThread(Remover *remover, QThread *thread) +{ + Q_ASSERT(remover); + Q_ASSERT(thread); + remover->moveToThread(thread); + QObject::connect(thread, &QThread::started, remover, &Remover::process); + QObject::connect(remover, &Remover::finished, remover, &QObject::deleteLater); + QObject::connect(remover, &Remover::finished, thread, &QThread::quit); + QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater); +} +} + using namespace YACReader; LibraryWindow::LibraryWindow() @@ -1576,22 +1590,14 @@ void LibraryWindow::deleteSelectedFolder() paths << folderPath; auto remover = new FoldersRemover(indexList, paths); + const auto thread = new QThread(this); + moveAndConnectRemoverToThread(remover, thread); - QThread *thread = NULL; - - thread = new QThread(this); - - remover->moveToThread(thread); - - connect(thread, SIGNAL(started()), remover, SLOT(process())); connect(remover, SIGNAL(remove(QModelIndex)), foldersModel, SLOT(deleteFolder(QModelIndex))); connect(remover, SIGNAL(removeError()), this, SLOT(errorDeletingFolder())); connect(remover, SIGNAL(finished()), navigationController, SLOT(reselectCurrentFolder())); - connect(remover, SIGNAL(finished()), remover, SLOT(deleteLater())); - connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); - if (thread != NULL) - thread->start(); + thread->start(); } } } @@ -2562,28 +2568,20 @@ void LibraryWindow::deleteComicsFromDisk() } auto remover = new ComicsRemover(indexList, paths, comics.at(0).parentId); - QThread *thread = NULL; - - thread = new QThread(this); - - remover->moveToThread(thread); + const auto thread = new QThread(this); + moveAndConnectRemoverToThread(remover, thread); comicsModel->startTransaction(); - connect(thread, SIGNAL(started()), remover, SLOT(process())); connect(remover, SIGNAL(remove(int)), comicsModel, SLOT(remove(int))); connect(remover, SIGNAL(removeError()), this, SLOT(setRemoveError())); connect(remover, SIGNAL(finished()), comicsModel, SLOT(finishTransaction())); - connect(remover, SIGNAL(finished()), comicsModel, SLOT(finishTransaction())); connect(remover, SIGNAL(removedItemsFromFolder(qulonglong)), foldersModel, SLOT(updateFolderChildrenInfo(qulonglong))); connect(remover, SIGNAL(finished()), this, SLOT(checkEmptyFolder())); connect(remover, SIGNAL(finished()), this, SLOT(checkRemoveError())); - connect(remover, SIGNAL(finished()), remover, SLOT(deleteLater())); - connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); - if (thread != NULL) - thread->start(); + thread->start(); } }