From 113f13e9d82e0d4a1c23e86877ca623a5a287d28 Mon Sep 17 00:00:00 2001 From: Arno Date: Mon, 19 Feb 2018 04:02:16 +0100 Subject: Add syncing favorites Symlink all favorites to a directory. Don't try to be smart, just ask if we need to delete anything if the target dir is not empty. Be very careful about what we delete. Only remove the item if it's a symlink. --- collectionfavoritesview.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ collectionfavoritesview.h | 1 + 2 files changed, 43 insertions(+) diff --git a/collectionfavoritesview.cpp b/collectionfavoritesview.cpp index 54ce2f5..b948c41 100644 --- a/collectionfavoritesview.cpp +++ b/collectionfavoritesview.cpp @@ -2,6 +2,10 @@ #include #include #include +#include +#include +#include +#include #include "collectionfavoritesview.h" #include "helper.h" @@ -9,7 +13,10 @@ CollectionFavoritesView::CollectionFavoritesView(QWidget *parent) : CollectionWidget(parent) { QAction *removeFromFavoritesA = new QAction(QIcon(":/delete.png"), tr("Remove from Favorites"), this); connect(removeFromFavoritesA, &QAction::triggered, [this] { removeFromFavorites(view()->selectionModel()->selectedRows()); }); + QAction *syncA = new QAction(QIcon(":/sissyd.png"), tr("Sync to dir..."), this); + connect(syncA, &QAction::triggered, this, &CollectionFavoritesView::syncToDir); customActions()->addAction(Helper::createSeparator(this)); + customActions()->addAction(syncA); customActions()->addAction(removeFromFavoritesA); } @@ -67,3 +74,38 @@ void CollectionFavoritesView::removeFromFavorites(const QModelIndexList &idxs){ populate(); } } + +void CollectionFavoritesView::syncToDir(){ + QSettings s; + QString dir = s.value("syncdir", QDir::homePath()).toString(); + QString destDir = QFileDialog::getExistingDirectory(this, tr("Sync to..."), dir); + if(!destDir.isEmpty()){ + s.setValue("syncdir", destDir); + QStandardItemModel *m = model(); + QStandardItem *root = m->invisibleRootItem(); + QDirIterator it(destDir); + if(it.hasNext()){ + int retval = QMessageBox::warning(this, tr("Clear directory"), tr("Target directory is not empty. Delete all files?"), QMessageBox::Yes | QMessageBox::No); + if(retval == QMessageBox::Yes){ + while(it.hasNext()){ + QFileInfo fi = it.fileInfo(); + if(fi.isSymLink()){ + QFile::remove(fi.filePath()); + } + it.next(); + } + } + } + for(int i = 0; i < root->rowCount(); ++i){ + QStandardItem *child = root->child(i, 0); + QString src = child->data(FullPathRole).toString(); + QFileInfo fi(src); + QString artist = child->data(ArtistRole).toString(); + QString title = child->data(TitleRole).toString(); + QString ext = fi.suffix(); + QString linkTo = QString("%1/%2-%3.%4").arg(destDir).arg(artist).arg(title).arg(ext); + QFile f(src); + f.link(linkTo); + } + } +} diff --git a/collectionfavoritesview.h b/collectionfavoritesview.h index d8d34d1..ccc3fc0 100644 --- a/collectionfavoritesview.h +++ b/collectionfavoritesview.h @@ -11,6 +11,7 @@ class CollectionFavoritesView : public CollectionWidget { public slots: virtual void populate(); void removeFromFavorites(const QModelIndexList &idxs); + void syncToDir(); }; #endif // COLLECTIONFAVORITESVIEW_H -- cgit v1.2.3-70-g09d2