diff --git a/YACReaderLibrary/comics_remover.cpp b/YACReaderLibrary/comics_remover.cpp index d5d5676d..ef3fd009 100644 --- a/YACReaderLibrary/comics_remover.cpp +++ b/YACReaderLibrary/comics_remover.cpp @@ -1,13 +1,16 @@ #include "comics_remover.h" #include +#include -ComicsRemover::ComicsRemover(QModelIndexList & il, QList & ps, QObject *parent) : - QThread(parent),indexList(il), paths(ps) +#include "QsLog.h" + +ComicsRemover::ComicsRemover(QModelIndexList & il, QList & ps, QObject *parent) + :QObject(parent),indexList(il), paths(ps) { } -void ComicsRemover::run() +void ComicsRemover::process() { QString currentComicPath; QListIterator i(indexList); @@ -27,3 +30,34 @@ void ComicsRemover::run() emit finished(); } + + +FoldersRemover::FoldersRemover(QModelIndexList &il, QList &ps, QObject *parent) + :QObject(parent),indexList(il), paths(ps) +{ + +} + +void FoldersRemover::process() +{ + QString currentFolderPath; + QListIterator i(indexList); + QListIterator i2(paths); + i.toBack(); + i2.toBack(); + + QLOG_DEBUG() << "Deleting folders" << paths.at(0); + + while (i.hasPrevious() && i2.hasPrevious()) + { + QModelIndex mi = i.previous(); + currentFolderPath = i2.previous(); + QDir d(currentFolderPath); + if(d.removeRecursively() || !d.exists()) //the folder is in the DB but no in the drive... + emit remove(mi); + else + emit removeError(); + } + + emit finished(); +} diff --git a/YACReaderLibrary/comics_remover.h b/YACReaderLibrary/comics_remover.h index 5a77a84b..ff9d0a21 100644 --- a/YACReaderLibrary/comics_remover.h +++ b/YACReaderLibrary/comics_remover.h @@ -6,7 +6,7 @@ #include #include -class ComicsRemover : public QThread +class ComicsRemover : public QObject { Q_OBJECT public: @@ -17,12 +17,31 @@ signals: void removeError(); void finished(); -private: - void run(); +public slots: + void process(); private: QModelIndexList indexList; QList paths; }; +class FoldersRemover : public QObject +{ + Q_OBJECT +public: + explicit FoldersRemover(QModelIndexList & indexList, QList & paths, QObject *parent = 0); + +signals: + void remove(QModelIndex); + void removeError(); + void finished(); + +public slots: + void process(); + +private: + QModelIndexList indexList; + QList paths; +}; + #endif // COMICS_REMOVER_H diff --git a/YACReaderLibrary/db/treeitem.cpp b/YACReaderLibrary/db/treeitem.cpp index ec31049e..2cbeec9f 100644 --- a/YACReaderLibrary/db/treeitem.cpp +++ b/YACReaderLibrary/db/treeitem.cpp @@ -68,6 +68,11 @@ void TreeItem::setData(int column, const QVariant & value) itemData[column] = value; } +void TreeItem::removeChild(int childIndex) +{ + childItems.removeAt(childIndex); +} + void TreeItem::clearChildren() { qDeleteAll(childItems); diff --git a/YACReaderLibrary/db/treeitem.h b/YACReaderLibrary/db/treeitem.h index 833f4a94..cb21dbcb 100644 --- a/YACReaderLibrary/db/treeitem.h +++ b/YACReaderLibrary/db/treeitem.h @@ -65,6 +65,7 @@ public: QList comicNames; TreeItem * originalItem; void setData(int column, const QVariant &value); + void removeChild(int childIndex); void clearChildren(); QList children(); private: diff --git a/YACReaderLibrary/db/treemodel.cpp b/YACReaderLibrary/db/treemodel.cpp index 02a0e383..01aeb7a9 100644 --- a/YACReaderLibrary/db/treemodel.cpp +++ b/YACReaderLibrary/db/treemodel.cpp @@ -621,3 +621,22 @@ void TreeModel::fetchMoreFromDB(const QModelIndex &parent) db.close(); QSqlDatabase::removeDatabase(_databasePath); } + +void TreeModel::deleteFolder(const QModelIndex &mi) +{ + beginRemoveRows(mi.parent(),mi.row(),mi.row()); + + TreeItem * item = static_cast(mi.internalPointer()); + + TreeItem * parent = item->parent(); + parent->removeChild(mi.row()); + + Folder f; + f.setId(item->id); + + QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath); + DBHelper::removeFromDB(&f,db); + QSqlDatabase::removeDatabase(_databasePath); + + endRemoveRows(); +} diff --git a/YACReaderLibrary/db/treemodel.h b/YACReaderLibrary/db/treemodel.h index 8a5e2f4b..c164a536 100644 --- a/YACReaderLibrary/db/treemodel.h +++ b/YACReaderLibrary/db/treemodel.h @@ -96,6 +96,9 @@ public: Completed = 3 };//id INTEGER PRIMARY KEY, parentId INTEGER NOT NULL, name TEXT NOT NULL, path TEXT NOT NULL +public slots: + void deleteFolder(const QModelIndex & mi); + private: void setupModelData( QSqlQuery &sqlquery, TreeItem *parent); void updateFolderModelData( QSqlQuery &sqlquery, TreeItem *parent); diff --git a/YACReaderLibrary/images_win.qrc b/YACReaderLibrary/images_win.qrc index b41ecd8d..99e83640 100644 --- a/YACReaderLibrary/images_win.qrc +++ b/YACReaderLibrary/images_win.qrc @@ -20,6 +20,8 @@ ../images/grid_to_flow.gif ../images/empty_folder.png ../images/empty_search.png + ../images/addNew_sidebar.png + ../images/delete_sidebar.png ../images/iconSearchNew.png ../images/clearSearchNew.png diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index 60094585..4d416638 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -189,6 +189,9 @@ void LibraryWindow::doLayout() librariesTitle->addAction(openLibraryAction); librariesTitle->addSpacing(3); + foldersTitle->addAction(addFolderAction); + foldersTitle->addAction(deleteFolderAction); + foldersTitle->addSepartor(); foldersTitle->addAction(setRootIndexAction); foldersTitle->addAction(expandAllNodesAction); foldersTitle->addAction(colapseAllNodesAction); @@ -319,6 +322,8 @@ void LibraryWindow::setUpShortcutsManagement() editShortcutsDialog->addActionsGroup("Folders",QIcon(":/images/shortcuts_group_folders.png"), tmpList = QList() + << addFolderAction + << deleteFolderAction << setRootIndexAction << expandAllNodesAction << colapseAllNodesAction @@ -524,6 +529,18 @@ void LibraryWindow::createActions() icoHelpButton.addPixmap(QPixmap(":/images/main_toolbar/help.png"), QIcon::Normal); helpAboutAction->setIcon(icoHelpButton); + addFolderAction = new QAction(tr("Add new folder"), this); + addFolderAction->setData(ADD_FOLDER_ACTION_YL); + addFolderAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(ADD_FOLDER_ACTION_YL)); + addFolderAction->setToolTip(tr("Add new folder to the current library")); + addFolderAction->setIcon(QIcon(":/images/addNew_sidebar.png")); + + deleteFolderAction = new QAction(tr("Delete folder"), this); + deleteFolderAction->setData(REMOVE_FOLDER_ACTION_YL); + deleteFolderAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(REMOVE_FOLDER_ACTION_YL)); + deleteFolderAction->setToolTip(tr("Delete current folder from disk")); + deleteFolderAction->setIcon(QIcon(":/images/delete_sidebar.png")); + setRootIndexAction = new QAction(this); setRootIndexAction->setData(SET_ROOT_INDEX_ACTION_YL); setRootIndexAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(SET_ROOT_INDEX_ACTION_YL)); @@ -858,6 +875,10 @@ void LibraryWindow::createMenus() comicsView->setItemActions(itemActions); comicsView->setViewActions(viewActions); + foldersView->addAction(addFolderAction); + foldersView->addAction(deleteFolderAction); + YACReader::addSperator(foldersView); + foldersView->addAction(openContainingFolderAction); foldersView->addAction(updateFolderAction); YACReader::addSperator(foldersView); @@ -1016,6 +1037,8 @@ void LibraryWindow::createConnections() connect(removeLibraryAction,SIGNAL(triggered()),this,SLOT(removeLibrary())); connect(openComicAction,SIGNAL(triggered()),this,SLOT(openComic())); connect(helpAboutAction,SIGNAL(triggered()),had,SLOT(show())); + connect(addFolderAction,SIGNAL(triggered()),this,SLOT(addFolderToCurrentIndex())); + connect(deleteFolderAction,SIGNAL(triggered()),this,SLOT(deleteSelectedFolder())); connect(setRootIndexAction,SIGNAL(triggered()),this,SLOT(setRootIndex())); connect(expandAllNodesAction,SIGNAL(triggered()),foldersView,SLOT(expandAll())); connect(colapseAllNodesAction,SIGNAL(triggered()),foldersView,SLOT(collapseAll())); @@ -1446,6 +1469,65 @@ void LibraryWindow::enableNeededActions() } +void LibraryWindow::addFolderToCurrentIndex() +{ + QModelIndex currentIndex = getCurrentFolderIndex(); + + bool ok; + QString text = QInputDialog::getText(this, tr("Add new folder"), + tr("Folder name:"), QLineEdit::Normal, + "", &ok); + if (ok && !text.isEmpty()) + QLOG_INFO() << text; + + +} + +void LibraryWindow::deleteSelectedFolder() +{ + QModelIndex currentIndex = getCurrentFolderIndex(); + + if(!currentIndex.isValid()) + QMessageBox::information(this,tr("No folder selected"), tr("Please, select a folder first")); + else + { + int ret = QMessageBox::question(this,tr("Delete folder"),tr("The selected folder and all its contents will be deleted from your disk. Are you sure?"),QMessageBox::Yes,QMessageBox::No); + + if(ret == QMessageBox::Yes) + { + //no folders multiselection by now + QModelIndexList indexList; + indexList << currentIndex; + + QList paths; + paths << QDir::cleanPath(currentPath()+foldersModel->getFolderPath(currentIndex)); + + FoldersRemover * remover = new FoldersRemover(indexList,paths); + + 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()),this,SLOT(reloadCovers())); + connect(remover, SIGNAL(finished()), remover, SLOT(deleteLater())); + connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); + + if(thread != NULL) + thread->start(); + } + } +} + +void LibraryWindow::errorDeletingFolder() +{ + QMessageBox::critical(this,tr("Unable to delete"),tr("There was an issue trying to delete the selected folders. Please, check for write permissions and be sure that any applications are using these folders or any of the contained files.")); +} + void LibraryWindow::selectSubfolder(const QModelIndex &mi, int child) { QModelIndex dest = foldersModel->index(child,0,mi); @@ -2236,20 +2318,26 @@ void LibraryWindow::deleteComics() } ComicsRemover * remover = new ComicsRemover(indexList,paths); + QThread * thread = NULL; + + thread = new QThread(this); + + remover->moveToThread(thread); - //comicsView->showDeleteProgress(); 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(removeError()),this,SLOT(setRemoveError())); connect(remover, SIGNAL(finished()), comicsModel, SLOT(finishTransaction())); - //connect(remover, SIGNAL(finished()), comicsView, SLOT(hideDeleteProgress())); - connect(remover, SIGNAL(finished()),this,SLOT(checkEmptyFolder())); - connect(remover, SIGNAL(finished()),this,SLOT(checkRemoveError())); + + connect(remover, SIGNAL(finished()),this,SLOT(checkEmptyFolder())); + connect(remover, SIGNAL(finished()),this,SLOT(checkRemoveError())); connect(remover, SIGNAL(finished()), remover, SLOT(deleteLater())); - //connect(remover, SIGNAL(finished()), thread, SLOT(deleteLater())); + connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); - remover->start(); + if(thread != NULL) + thread->start(); } } diff --git a/YACReaderLibrary/library_window.h b/YACReaderLibrary/library_window.h index b0fa49db..d61af10e 100644 --- a/YACReaderLibrary/library_window.h +++ b/YACReaderLibrary/library_window.h @@ -147,6 +147,9 @@ private: //QAction * socialAction; //tree actions + QAction * addFolderAction; + QAction * deleteFolderAction; + //-- QAction * setRootIndexAction; QAction * expandAllNodesAction; QAction * colapseAllNodesAction; @@ -339,6 +342,9 @@ public slots: void reloadAfterCopyMove(); QModelIndex getCurrentFolderIndex(); void enableNeededActions(); + void addFolderToCurrentIndex(); + void deleteSelectedFolder(); + void errorDeletingFolder(); }; #endif diff --git a/custom_widgets/yacreader_titled_toolbar.cpp b/custom_widgets/yacreader_titled_toolbar.cpp index 9015ff29..79b54d3d 100644 --- a/custom_widgets/yacreader_titled_toolbar.cpp +++ b/custom_widgets/yacreader_titled_toolbar.cpp @@ -65,7 +65,7 @@ YACReaderTitledToolBar::YACReaderTitledToolBar(const QString & title, QWidget *p mainLayout->setSpacing(0); QString styleSheet = "QWidget {border:0px;}"; - setStyleSheet(styleSheet); + setStyleSheet(styleSheet); nameLabel = new DropShadowLabel(this); nameLabel->setText(title); @@ -108,5 +108,18 @@ void YACReaderTitledToolBar::addSpacing(int spacing) { QHBoxLayout * mainLayout = dynamic_cast(layout()); - mainLayout->addSpacing(spacing); + mainLayout->addSpacing(spacing); +} + +void YACReaderTitledToolBar::addSepartor() +{ + QHBoxLayout * mainLayout = dynamic_cast(layout()); + + QWidget * w = new QWidget(this); + w->setFixedSize(1,14); + w->setStyleSheet("QWidget {background-color:#6F6F6F;}"); + + mainLayout->addSpacing(10); + mainLayout->addWidget(w); + mainLayout->addSpacing(10); } diff --git a/custom_widgets/yacreader_titled_toolbar.h b/custom_widgets/yacreader_titled_toolbar.h index 0b4a03c1..21b9b75c 100644 --- a/custom_widgets/yacreader_titled_toolbar.h +++ b/custom_widgets/yacreader_titled_toolbar.h @@ -38,6 +38,7 @@ signals: public slots: void addAction(QAction * action); void addSpacing(int space); + void addSepartor(); private: DropShadowLabel * nameLabel; }; diff --git a/images/addNew_sidebar.png b/images/addNew_sidebar.png new file mode 100644 index 00000000..f23c0676 Binary files /dev/null and b/images/addNew_sidebar.png differ diff --git a/images/delete_sidebar.png b/images/delete_sidebar.png new file mode 100644 index 00000000..76120100 Binary files /dev/null and b/images/delete_sidebar.png differ diff --git a/shortcuts_management/shortcuts_manager.h b/shortcuts_management/shortcuts_manager.h index 8e6f0cd3..a5f52a49 100644 --- a/shortcuts_management/shortcuts_manager.h +++ b/shortcuts_management/shortcuts_manager.h @@ -73,6 +73,8 @@ public: #define GET_INFO_ACTION_YL "GET_INFO_ACTION_YL" #define SHOW_EDIT_SHORTCUTS_ACTION_YL "SHOW_EDIT_SHORTCUTS_ACTION_YL" #define UPDATE_CURRENT_FOLDER_ACTION_YL "UPDATE_CURRENT_FOLDER_ACTION_YL" +#define ADD_FOLDER_ACTION_YL "ADD_FOLDER_ACTION_YL" +#define REMOVE_FOLDER_ACTION_YL "REMOVE_FOLDER_ACTION_YL" //COMMANDS YACReaderLibrary