1 /* This file is part of Clementine.
2    Copyright 2010, David Sansome <me@davidsansome.com>
3 
4    Clementine is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8 
9    Clementine is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "deviceview.h"
19 
20 #include <memory>
21 
22 #include <QApplication>
23 #include <QContextMenuEvent>
24 #include <QMenu>
25 #include <QMessageBox>
26 #include <QPainter>
27 #include <QPushButton>
28 #include <QSortFilterProxyModel>
29 
30 #include "connecteddevice.h"
31 #include "devicelister.h"
32 #include "devicemanager.h"
33 #include "deviceproperties.h"
34 #include "core/application.h"
35 #include "core/deletefiles.h"
36 #include "core/mergedproxymodel.h"
37 #include "core/mimedata.h"
38 #include "library/librarydirectorymodel.h"
39 #include "library/librarymodel.h"
40 #include "library/libraryview.h"
41 #include "ui/iconloader.h"
42 #include "ui/organisedialog.h"
43 #include "ui/organiseerrordialog.h"
44 
45 const int DeviceItemDelegate::kIconPadding = 6;
46 
DeviceItemDelegate(QObject * parent)47 DeviceItemDelegate::DeviceItemDelegate(QObject* parent)
48     : LibraryItemDelegate(parent) {}
49 
paint(QPainter * p,const QStyleOptionViewItem & opt,const QModelIndex & index) const50 void DeviceItemDelegate::paint(QPainter* p, const QStyleOptionViewItem& opt,
51                                const QModelIndex& index) const {
52   // Is it a device or a library item?
53   if (index.data(DeviceManager::Role_State).isNull()) {
54     LibraryItemDelegate::paint(p, opt, index);
55     return;
56   }
57 
58   // Draw the background
59   const QWidget* widget = opt.widget;
60   QStyle* style = widget->style() ? widget->style() : QApplication::style();
61   style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, p, widget);
62 
63   p->save();
64 
65   // Font for the status line
66   QFont status_font(opt.font);
67 
68 #ifdef Q_OS_WIN32
69   status_font.setPointSize(status_font.pointSize() - 1);
70 #else
71   status_font.setPointSize(status_font.pointSize() - 2);
72 #endif
73 
74   const int text_height =
75       QFontMetrics(opt.font).height() + QFontMetrics(status_font).height();
76 
77   QRect line1(opt.rect);
78   QRect line2(opt.rect);
79   line1.setTop(line1.top() + (opt.rect.height() - text_height) / 2);
80   line2.setTop(line1.top() + QFontMetrics(opt.font).height());
81   line1.setLeft(line1.left() + DeviceManager::kDeviceIconSize + kIconPadding);
82   line2.setLeft(line2.left() + DeviceManager::kDeviceIconSize + kIconPadding);
83 
84   // Change the color for selected items
85   if (opt.state & QStyle::State_Selected) {
86     p->setPen(opt.palette.color(QPalette::HighlightedText));
87   }
88 
89   // Draw the icon
90   p->drawPixmap(opt.rect.topLeft(),
91                 index.data(Qt::DecorationRole).value<QPixmap>());
92 
93   // Draw the first line (device name)
94   p->drawText(line1, Qt::AlignLeft | Qt::AlignTop, index.data().toString());
95 
96   // Draw the second line (status)
97   DeviceManager::State state = static_cast<DeviceManager::State>(
98       index.data(DeviceManager::Role_State).toInt());
99   QVariant progress = index.data(DeviceManager::Role_UpdatingPercentage);
100   QString status_text;
101 
102   if (progress.isValid()) {
103     status_text = tr("Updating %1%...").arg(progress.toInt());
104   } else {
105     switch (state) {
106       case DeviceManager::State_Remembered:
107         status_text = tr("Not connected");
108         break;
109 
110       case DeviceManager::State_NotMounted:
111         status_text = tr("Not mounted - double click to mount");
112         break;
113 
114       case DeviceManager::State_NotConnected:
115         status_text = tr("Double click to open");
116         break;
117 
118       case DeviceManager::State_Connected: {
119         QVariant song_count = index.data(DeviceManager::Role_SongCount);
120         if (song_count.isValid()) {
121           int count = song_count.toInt();
122           if (count == 1)  // TODO: Fix this properly
123             status_text = tr("%1 song").arg(count);
124           else
125             status_text = tr("%1 songs").arg(count);
126         } else {
127           status_text = index.data(DeviceManager::Role_MountPath).toString();
128         }
129         break;
130       }
131     }
132   }
133 
134   if (opt.state & QStyle::State_Selected)
135     p->setPen(opt.palette.color(QPalette::HighlightedText));
136   else
137     p->setPen(opt.palette.color(QPalette::Dark));
138   p->setFont(status_font);
139   p->drawText(line2, Qt::AlignLeft | Qt::AlignTop, status_text);
140 
141   p->restore();
142 }
143 
DeviceView(QWidget * parent)144 DeviceView::DeviceView(QWidget* parent)
145     : AutoExpandingTreeView(parent),
146       app_(nullptr),
147       merged_model_(nullptr),
148       sort_model_(nullptr),
149       properties_dialog_(new DeviceProperties),
150       device_menu_(nullptr),
151       library_menu_(nullptr) {
152   setItemDelegate(new DeviceItemDelegate(this));
153   SetExpandOnReset(false);
154   setAttribute(Qt::WA_MacShowFocusRect, false);
155   setHeaderHidden(true);
156   setAllColumnsShowFocus(true);
157   setDragEnabled(true);
158   setDragDropMode(QAbstractItemView::DragOnly);
159   setSelectionMode(QAbstractItemView::ExtendedSelection);
160 }
161 
~DeviceView()162 DeviceView::~DeviceView() {}
163 
SetApplication(Application * app)164 void DeviceView::SetApplication(Application* app) {
165   Q_ASSERT(app_ == nullptr);
166   app_ = app;
167 
168   connect(app_->device_manager(), SIGNAL(DeviceConnected(QModelIndex)),
169           SLOT(DeviceConnected(QModelIndex)));
170   connect(app_->device_manager(), SIGNAL(DeviceDisconnected(QModelIndex)),
171           SLOT(DeviceDisconnected(QModelIndex)));
172 
173   sort_model_ = new QSortFilterProxyModel(this);
174   sort_model_->setSourceModel(app_->device_manager());
175   sort_model_->setDynamicSortFilter(true);
176   sort_model_->setSortCaseSensitivity(Qt::CaseInsensitive);
177   sort_model_->sort(0);
178 
179   merged_model_ = new MergedProxyModel(this);
180   merged_model_->setSourceModel(sort_model_);
181 
182   connect(merged_model_,
183           SIGNAL(SubModelReset(QModelIndex, QAbstractItemModel*)),
184           SLOT(RecursivelyExpand(QModelIndex)));
185 
186   setModel(merged_model_);
187   properties_dialog_->SetDeviceManager(app_->device_manager());
188 
189   organise_dialog_.reset(new OrganiseDialog(app_->task_manager()));
190   organise_dialog_->SetDestinationModel(
191       app_->library_model()->directory_model());
192 }
193 
contextMenuEvent(QContextMenuEvent * e)194 void DeviceView::contextMenuEvent(QContextMenuEvent* e) {
195   if (!device_menu_) {
196     device_menu_ = new QMenu(this);
197     library_menu_ = new QMenu(this);
198 
199     // Device menu
200     eject_action_ = device_menu_->addAction(IconLoader::Load("media-eject",
201                                             IconLoader::Base),
202                                             tr("Safely remove device"), this,
203                                             SLOT(Unmount()));
204     forget_action_ =
205         device_menu_->addAction(IconLoader::Load("list-remove", IconLoader::Base),
206                                 tr("Forget device"), this, SLOT(Forget()));
207     device_menu_->addSeparator();
208     properties_action_ = device_menu_->addAction(IconLoader::Load("configure",
209                                                  IconLoader::Base),
210                                                  tr("Device properties..."),
211                                                  this, SLOT(Properties()));
212 
213     // Library menu
214     add_to_playlist_action_ = library_menu_->addAction(
215         IconLoader::Load("media-playback-start", IconLoader::Base),
216         tr("Append to current playlist"), this, SLOT(AddToPlaylist()));
217     load_action_ = library_menu_->addAction(
218         IconLoader::Load("media-playback-start", IconLoader::Base),
219         tr("Replace current playlist"), this, SLOT(Load()));
220     open_in_new_playlist_ = library_menu_->addAction(
221         IconLoader::Load("document-new", IconLoader::Base),
222         tr("Open in new playlist"), this, SLOT(OpenInNewPlaylist()));
223     library_menu_->addSeparator();
224     organise_action_ = library_menu_->addAction(IconLoader::Load("edit-copy",
225                                                 IconLoader::Base),
226                                                 tr("Copy to library..."), this,
227                                                 SLOT(Organise()));
228     delete_action_ = library_menu_->addAction(IconLoader::Load("edit-delete",
229                                               IconLoader::Base),
230                                               tr("Delete from device..."), this,
231                                               SLOT(Delete()));
232   }
233 
234   menu_index_ = currentIndex();
235 
236   const QModelIndex device_index = MapToDevice(menu_index_);
237   const QModelIndex library_index = MapToLibrary(menu_index_);
238 
239   if (device_index.isValid()) {
240     const bool is_plugged_in = app_->device_manager()->GetLister(device_index);
241     const bool is_remembered =
242         app_->device_manager()->GetDatabaseId(device_index) != -1;
243 
244     forget_action_->setEnabled(is_remembered);
245     eject_action_->setEnabled(is_plugged_in);
246 
247     device_menu_->popup(e->globalPos());
248   } else if (library_index.isValid()) {
249     const QModelIndex parent_device_index = FindParentDevice(menu_index_);
250 
251     bool is_filesystem_device = false;
252     if (parent_device_index.isValid()) {
253       std::shared_ptr<ConnectedDevice> device =
254           app_->device_manager()->GetConnectedDevice(parent_device_index);
255       if (device && !device->LocalPath().isEmpty()) is_filesystem_device = true;
256     }
257 
258     organise_action_->setEnabled(is_filesystem_device);
259 
260     library_menu_->popup(e->globalPos());
261   }
262 }
263 
MapToDevice(const QModelIndex & merged_model_index) const264 QModelIndex DeviceView::MapToDevice(const QModelIndex& merged_model_index)
265     const {
266   QModelIndex sort_model_index = merged_model_->mapToSource(merged_model_index);
267   if (sort_model_index.model() != sort_model_) return QModelIndex();
268 
269   return sort_model_->mapToSource(sort_model_index);
270 }
271 
FindParentDevice(const QModelIndex & merged_model_index) const272 QModelIndex DeviceView::FindParentDevice(const QModelIndex& merged_model_index)
273     const {
274   QModelIndex index = merged_model_->FindSourceParent(merged_model_index);
275   if (index.model() != sort_model_) return QModelIndex();
276 
277   return sort_model_->mapToSource(index);
278 }
279 
MapToLibrary(const QModelIndex & merged_model_index) const280 QModelIndex DeviceView::MapToLibrary(const QModelIndex& merged_model_index)
281     const {
282   QModelIndex sort_model_index = merged_model_->mapToSource(merged_model_index);
283   if (const QSortFilterProxyModel* sort_model =
284           qobject_cast<const QSortFilterProxyModel*>(
285               sort_model_index.model())) {
286     return sort_model->mapToSource(sort_model_index);
287   }
288 
289   return QModelIndex();
290 }
291 
Connect()292 void DeviceView::Connect() {
293   QModelIndex device_idx = MapToDevice(menu_index_);
294   app_->device_manager()->data(device_idx,
295                                MusicStorage::Role_StorageForceConnect);
296 }
297 
DeviceConnected(QModelIndex idx)298 void DeviceView::DeviceConnected(QModelIndex idx) {
299   std::shared_ptr<ConnectedDevice> device =
300       app_->device_manager()->GetConnectedDevice(idx);
301   if (!device) return;
302 
303   QModelIndex sort_idx = sort_model_->mapFromSource(idx);
304 
305   QSortFilterProxyModel* sort_model =
306       new QSortFilterProxyModel(device->model());
307   sort_model->setSourceModel(device->model());
308   sort_model->setSortRole(LibraryModel::Role_SortText);
309   sort_model->setDynamicSortFilter(true);
310   sort_model->sort(0);
311   merged_model_->AddSubModel(sort_idx, sort_model);
312 
313   expand(menu_index_);
314 }
315 
DeviceDisconnected(QModelIndex idx)316 void DeviceView::DeviceDisconnected(QModelIndex idx) {
317   merged_model_->RemoveSubModel(sort_model_->mapFromSource(idx));
318 }
319 
Forget()320 void DeviceView::Forget() {
321   QModelIndex device_idx = MapToDevice(menu_index_);
322   QString unique_id = app_->device_manager()
323                           ->data(device_idx, DeviceManager::Role_UniqueId)
324                           .toString();
325   if (app_->device_manager()->GetLister(device_idx) &&
326       app_->device_manager()->GetLister(device_idx)->AskForScan(unique_id)) {
327     std::unique_ptr<QMessageBox> dialog(new QMessageBox(
328         QMessageBox::Question, tr("Forget device"),
329         tr("Forgetting a device will remove it from this list and Clementine "
330            "will have to rescan all the songs again next time you connect it."),
331         QMessageBox::Cancel, this));
332     QPushButton* forget =
333         dialog->addButton(tr("Forget device"), QMessageBox::DestructiveRole);
334     dialog->exec();
335 
336     if (dialog->clickedButton() != forget) return;
337   }
338 
339   app_->device_manager()->Forget(device_idx);
340 }
341 
Properties()342 void DeviceView::Properties() {
343   properties_dialog_->ShowDevice(MapToDevice(menu_index_));
344 }
345 
mouseDoubleClickEvent(QMouseEvent * event)346 void DeviceView::mouseDoubleClickEvent(QMouseEvent* event) {
347   AutoExpandingTreeView::mouseDoubleClickEvent(event);
348 
349   QModelIndex merged_index = indexAt(event->pos());
350   QModelIndex device_index = MapToDevice(merged_index);
351   if (device_index.isValid()) {
352     if (!app_->device_manager()->GetConnectedDevice(device_index)) {
353       menu_index_ = merged_index;
354       Connect();
355     }
356   }
357 }
358 
GetSelectedSongs() const359 SongList DeviceView::GetSelectedSongs() const {
360   QModelIndexList selected_merged_indexes = selectionModel()->selectedRows();
361   SongList songs;
362   for (const QModelIndex& merged_index : selected_merged_indexes) {
363     QModelIndex library_index = MapToLibrary(merged_index);
364     if (!library_index.isValid()) continue;
365 
366     const LibraryModel* library =
367         qobject_cast<const LibraryModel*>(library_index.model());
368     if (!library) continue;
369 
370     songs << library->GetChildSongs(library_index);
371   }
372   return songs;
373 }
374 
Load()375 void DeviceView::Load() {
376   QMimeData* data = model()->mimeData(selectedIndexes());
377   if (MimeData* mime_data = qobject_cast<MimeData*>(data)) {
378     mime_data->clear_first_ = true;
379   }
380   emit AddToPlaylistSignal(data);
381 }
382 
AddToPlaylist()383 void DeviceView::AddToPlaylist() {
384   emit AddToPlaylistSignal(model()->mimeData(selectedIndexes()));
385 }
386 
OpenInNewPlaylist()387 void DeviceView::OpenInNewPlaylist() {
388   QMimeData* data = model()->mimeData(selectedIndexes());
389   if (MimeData* mime_data = qobject_cast<MimeData*>(data)) {
390     mime_data->open_in_new_playlist_ = true;
391   }
392   emit AddToPlaylistSignal(data);
393 }
394 
Delete()395 void DeviceView::Delete() {
396   if (selectedIndexes().isEmpty()) return;
397 
398   // Take the device of the first selected item
399   QModelIndex device_index = FindParentDevice(selectedIndexes()[0]);
400   if (!device_index.isValid()) return;
401 
402   if (QMessageBox::question(this, tr("Delete files"),
403                             tr("These files will be deleted from the device, "
404                                "are you sure you want to continue?"),
405                             QMessageBox::Yes,
406                             QMessageBox::Cancel) != QMessageBox::Yes)
407     return;
408 
409   std::shared_ptr<MusicStorage> storage =
410       device_index.data(MusicStorage::Role_Storage)
411           .value<std::shared_ptr<MusicStorage>>();
412 
413   DeleteFiles* delete_files = new DeleteFiles(app_->task_manager(), storage);
414   connect(delete_files, SIGNAL(Finished(SongList)),
415           SLOT(DeleteFinished(SongList)));
416   delete_files->Start(GetSelectedSongs());
417 }
418 
Organise()419 void DeviceView::Organise() {
420   SongList songs = GetSelectedSongs();
421   QStringList filenames;
422   for (const Song& song : songs) {
423     filenames << song.url().toLocalFile();
424   }
425 
426   organise_dialog_->SetCopy(true);
427   organise_dialog_->SetFilenames(filenames);
428   organise_dialog_->show();
429 }
430 
Unmount()431 void DeviceView::Unmount() {
432   QModelIndex device_idx = MapToDevice(menu_index_);
433   app_->device_manager()->Unmount(device_idx);
434 }
435 
DeleteFinished(const SongList & songs_with_errors)436 void DeviceView::DeleteFinished(const SongList& songs_with_errors) {
437   if (songs_with_errors.isEmpty()) return;
438 
439   OrganiseErrorDialog* dialog = new OrganiseErrorDialog(this);
440   dialog->Show(OrganiseErrorDialog::Type_Delete, songs_with_errors);
441   // It deletes itself when the user closes it
442 }
443 
CanRecursivelyExpand(const QModelIndex & index) const444 bool DeviceView::CanRecursivelyExpand(const QModelIndex& index) const {
445   // Never expand devices
446   return index.parent().isValid();
447 }
448