2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
*
|
|
|
|
* Strawberry 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:10:03 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
2020-05-30 21:59:55 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QWidget>
|
|
|
|
#include <QAbstractItemModel>
|
|
|
|
#include <QFileInfo>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QFileSystemModel>
|
2020-03-15 01:56:48 +01:00
|
|
|
#include <QDir>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QMenu>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QUrl>
|
2022-06-14 17:21:59 +02:00
|
|
|
#include <QCollator>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtEvents>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "core/iconloader.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "core/mimedata.h"
|
2022-12-28 03:12:00 +01:00
|
|
|
#include "utilities/filemanagerutils.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "fileviewlist.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
FileViewList::FileViewList(QWidget *parent)
|
|
|
|
: QListView(parent),
|
2021-01-26 16:48:04 +01:00
|
|
|
menu_(new QMenu(this)) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
menu_->addAction(IconLoader::Load("media-playback-start"), tr("Append to current playlist"), this, &FileViewList::AddToPlaylistSlot);
|
|
|
|
menu_->addAction(IconLoader::Load("media-playback-start"), tr("Replace current playlist"), this, &FileViewList::LoadSlot);
|
|
|
|
menu_->addAction(IconLoader::Load("document-new"), tr("Open in new playlist"), this, &FileViewList::OpenInNewPlaylistSlot);
|
2018-02-27 18:06:05 +01:00
|
|
|
menu_->addSeparator();
|
2021-01-26 16:48:04 +01:00
|
|
|
menu_->addAction(IconLoader::Load("edit-copy"), tr("Copy to collection..."), this, &FileViewList::CopyToCollectionSlot);
|
|
|
|
menu_->addAction(IconLoader::Load("go-jump"), tr("Move to collection..."), this, &FileViewList::MoveToCollectionSlot);
|
|
|
|
menu_->addAction(IconLoader::Load("device"), tr("Copy to device..."), this, &FileViewList::CopyToDeviceSlot);
|
|
|
|
menu_->addAction(IconLoader::Load("edit-delete"), tr("Delete from disk..."), this, &FileViewList::DeleteSlot);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
menu_->addSeparator();
|
2021-01-26 16:48:04 +01:00
|
|
|
menu_->addAction(IconLoader::Load("edit-rename"), tr("Edit track information..."), this, &FileViewList::EditTagsSlot);
|
|
|
|
menu_->addAction(IconLoader::Load("document-open-folder"), tr("Show in file browser..."), this, &FileViewList::ShowInBrowser);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
setAttribute(Qt::WA_MacShowFocusRect, false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileViewList::contextMenuEvent(QContextMenuEvent *e) {
|
|
|
|
|
|
|
|
menu_selection_ = selectionModel()->selection();
|
|
|
|
|
|
|
|
menu_->popup(e->globalPos());
|
|
|
|
e->accept();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QUrl> FileViewList::UrlListFromSelection() const {
|
|
|
|
|
2022-06-14 17:21:59 +02:00
|
|
|
QStringList filenames;
|
2021-06-12 20:53:23 +02:00
|
|
|
const QModelIndexList indexes = menu_selection_.indexes();
|
|
|
|
for (const QModelIndex &index : indexes) {
|
2021-08-23 21:21:08 +02:00
|
|
|
if (index.column() == 0) {
|
2022-06-14 17:21:59 +02:00
|
|
|
filenames << qobject_cast<QFileSystemModel*>(model())->fileInfo(index).canonicalFilePath();
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2022-06-14 17:21:59 +02:00
|
|
|
|
|
|
|
QCollator collator;
|
|
|
|
collator.setNumericMode(true);
|
|
|
|
std::sort(filenames.begin(), filenames.end(), collator);
|
|
|
|
|
|
|
|
QList<QUrl> urls;
|
2023-03-19 20:32:06 +01:00
|
|
|
urls.reserve(filenames.count());
|
2022-06-14 17:21:59 +02:00
|
|
|
for (const QString &filename : filenames) {
|
|
|
|
urls << QUrl::fromLocalFile(filename);
|
|
|
|
}
|
2020-05-30 21:59:55 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
return urls;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
MimeData *FileViewList::MimeDataFromSelection() const {
|
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
MimeData *mimedata = new MimeData;
|
|
|
|
mimedata->setUrls(UrlListFromSelection());
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QList<QString> filenames = FilenamesFromSelection();
|
2020-05-30 21:59:55 +02:00
|
|
|
|
2022-08-28 02:44:37 +02:00
|
|
|
// if just one folder selected - use its path as the new playlist's name
|
2018-02-27 18:06:05 +01:00
|
|
|
if (filenames.size() == 1 && QFileInfo(filenames.first()).isDir()) {
|
2020-03-15 01:56:48 +01:00
|
|
|
if (filenames.first().length() > 20) {
|
2020-04-23 21:08:28 +02:00
|
|
|
mimedata->name_for_new_playlist_ = QDir(filenames.first()).dirName();
|
2020-03-15 01:56:48 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-04-23 21:08:28 +02:00
|
|
|
mimedata->name_for_new_playlist_ = filenames.first();
|
2020-03-15 01:56:48 +01:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2020-03-15 01:56:48 +01:00
|
|
|
// otherwise, use the current root path
|
2018-02-27 18:06:05 +01:00
|
|
|
else {
|
2020-07-17 01:32:07 +02:00
|
|
|
QString path = qobject_cast<QFileSystemModel*>(model())->rootPath();
|
2020-03-15 01:56:48 +01:00
|
|
|
if (path.length() > 20) {
|
|
|
|
QFileInfo info(path);
|
|
|
|
if (info.isDir()) {
|
2020-04-23 21:08:28 +02:00
|
|
|
mimedata->name_for_new_playlist_ = QDir(info.filePath()).dirName();
|
2020-03-15 01:56:48 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-09-29 17:30:21 +02:00
|
|
|
mimedata->name_for_new_playlist_ = info.completeBaseName();
|
2020-03-15 01:56:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-04-23 21:08:28 +02:00
|
|
|
mimedata->name_for_new_playlist_ = path;
|
2020-03-15 01:56:48 +01:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
return mimedata;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList FileViewList::FilenamesFromSelection() const {
|
|
|
|
|
|
|
|
QStringList filenames;
|
2021-06-12 20:53:23 +02:00
|
|
|
const QModelIndexList indexes = menu_selection_.indexes();
|
|
|
|
for (const QModelIndex &index : indexes) {
|
2021-08-23 21:21:08 +02:00
|
|
|
if (index.column() == 0) {
|
2020-07-17 01:32:07 +02:00
|
|
|
filenames << qobject_cast<QFileSystemModel*>(model())->filePath(index);
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2022-06-14 17:21:59 +02:00
|
|
|
|
|
|
|
QCollator collator;
|
|
|
|
collator.setNumericMode(true);
|
|
|
|
std::sort(filenames.begin(), filenames.end(), collator);
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
return filenames;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileViewList::LoadSlot() {
|
2020-04-23 21:08:28 +02:00
|
|
|
|
|
|
|
MimeData *mimedata = MimeDataFromSelection();
|
|
|
|
mimedata->clear_first_ = true;
|
|
|
|
emit AddToPlaylist(mimedata);
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileViewList::AddToPlaylistSlot() {
|
|
|
|
emit AddToPlaylist(MimeDataFromSelection());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileViewList::OpenInNewPlaylistSlot() {
|
2020-04-23 21:08:28 +02:00
|
|
|
|
|
|
|
MimeData *mimedata = MimeDataFromSelection();
|
|
|
|
mimedata->open_in_new_playlist_ = true;
|
|
|
|
emit AddToPlaylist(mimedata);
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileViewList::CopyToCollectionSlot() {
|
|
|
|
emit CopyToCollection(UrlListFromSelection());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileViewList::MoveToCollectionSlot() {
|
|
|
|
emit MoveToCollection(UrlListFromSelection());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileViewList::CopyToDeviceSlot() {
|
|
|
|
emit CopyToDevice(UrlListFromSelection());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileViewList::DeleteSlot() {
|
|
|
|
emit Delete(FilenamesFromSelection());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileViewList::EditTagsSlot() {
|
|
|
|
emit EditTags(UrlListFromSelection());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileViewList::mousePressEvent(QMouseEvent *e) {
|
|
|
|
|
|
|
|
switch (e->button()) {
|
|
|
|
case Qt::XButton1:
|
|
|
|
emit Back();
|
|
|
|
break;
|
|
|
|
case Qt::XButton2:
|
|
|
|
emit Forward();
|
|
|
|
break;
|
|
|
|
// enqueue to playlist with middleClick
|
2020-08-23 03:27:24 +02:00
|
|
|
case Qt::MiddleButton: {
|
2018-02-27 18:06:05 +01:00
|
|
|
QListView::mousePressEvent(e);
|
|
|
|
|
|
|
|
// we need to update the menu selection
|
|
|
|
menu_selection_ = selectionModel()->selection();
|
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
MimeData *mimedata = new MimeData;
|
|
|
|
mimedata->setUrls(UrlListFromSelection());
|
|
|
|
mimedata->enqueue_now_ = true;
|
|
|
|
emit AddToPlaylist(mimedata);
|
2018-02-27 18:06:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
QListView::mousePressEvent(e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileViewList::ShowInBrowser() {
|
|
|
|
Utilities::OpenInFileBrowser(UrlListFromSelection());
|
|
|
|
}
|