1 /*
2 * Cantata
3 *
4 * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5 *
6 * ----
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include "searchpage.h"
25 #include "mpd-interface/mpdconnection.h"
26 #include "settings.h"
27 #include "stdactions.h"
28 #include "customactions.h"
29 #include "support/utils.h"
30 #include "support/icon.h"
31 #include "support/messagebox.h"
32 #include "widgets/tableview.h"
33 #include "widgets/menubutton.h"
34 #include "widgets/icons.h"
35
36 class SearchTableView : public TableView
37 {
38 public:
SearchTableView(QWidget * p)39 SearchTableView(QWidget *p)
40 : TableView(QLatin1String("search"), p)
41 {
42 setUseSimpleDelegate();
43 setIndentation(0);
44 }
45
~SearchTableView()46 ~SearchTableView() override { }
47 };
48
SearchPage(QWidget * p)49 SearchPage::SearchPage(QWidget *p)
50 : SinglePageWidget(p)
51 , state(-1)
52 , model(this)
53 , proxy(this)
54 {
55 statsLabel=new SqueezedTextLabel(this);
56 locateAction=new Action(Icons::self()->searchIcon, tr("Locate In Library"), this);
57 view->allowTableView(new SearchTableView(view));
58
59 connect(&model, SIGNAL(searching()), view, SLOT(showSpinner()));
60 connect(&model, SIGNAL(searched()), view, SLOT(hideSpinner()));
61 connect(&model, SIGNAL(statsUpdated(int, quint32)), this, SLOT(statsUpdated(int, quint32)));
62 connect(view, SIGNAL(itemsSelected(bool)), this, SLOT(controlActions()));
63 connect(view, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(itemDoubleClicked(const QModelIndex &)));
64 connect(MPDConnection::self(), SIGNAL(stateChanged(bool)), this, SLOT(setSearchCategories()));
65 connect(locateAction, SIGNAL(triggered()), SLOT(locateSongs()));
66 proxy.setSourceModel(&model);
67 view->setModel(&proxy);
68 view->setPermanentSearch();
69 setSearchCategories();
70 view->setSearchCategory(Settings::self()->searchCategory());
71 statsUpdated(0, 0);
72 view->setMode(ItemView::Mode_List);
73 Configuration config(metaObject()->className());
74 view->load(config);
75 MenuButton *menu=new MenuButton(this);
76 menu->addActions(createViewActions(QList<ItemView::Mode>() << ItemView::Mode_List << ItemView::Mode_Table));
77 statsLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
78 init(ReplacePlayQueue|AppendToPlayQueue, QList<QWidget *>() << menu << statsLabel);
79
80 view->addAction(StdActions::self()->addToStoredPlaylistAction);
81 view->addAction(CustomActions::self());
82 #ifdef TAGLIB_FOUND
83 #ifdef ENABLE_DEVICES_SUPPORT
84 view->addAction(StdActions::self()->copyToDeviceAction);
85 #endif
86 view->addAction(StdActions::self()->organiseFilesAction);
87 view->addAction(StdActions::self()->editTagsAction);
88 #ifdef ENABLE_REPLAYGAIN_SUPPORT
89 view->addAction(StdActions::self()->replaygainAction);
90 #endif
91 #ifdef ENABLE_DEVICES_SUPPORT
92 view->addSeparator();
93 view->addAction(StdActions::self()->deleteSongsAction);
94 #endif
95 #endif // TAGLIB_FOUND
96 view->addAction(locateAction);
97 view->setInfoText(tr("Use the text field above to search for music in your library via MPD's search mechanism. "
98 "Clicking the label next to the field will produce a menu allowing you to control the search area."));
99 }
100
~SearchPage()101 SearchPage::~SearchPage()
102 {
103 Configuration config(metaObject()->className());
104 view->save(config);
105 }
106
refresh()107 void SearchPage::refresh()
108 {
109 model.refresh();
110 }
111
clear()112 void SearchPage::clear()
113 {
114 model.clear();
115 }
116
setView(int mode)117 void SearchPage::setView(int mode)
118 {
119 view->setMode((ItemView::Mode)mode);
120 model.setMultiColumn(ItemView::Mode_Table==mode);
121 }
122
showEvent(QShowEvent * e)123 void SearchPage::showEvent(QShowEvent *e)
124 {
125 SinglePageWidget::showEvent(e);
126 view->focusSearch();
127 }
128
selectedFiles(bool allowPlaylists) const129 QStringList SearchPage::selectedFiles(bool allowPlaylists) const
130 {
131 QModelIndexList selected = view->selectedIndexes();
132 if (selected.isEmpty()) {
133 return QStringList();
134 }
135
136 QModelIndexList mapped;
137 for (const QModelIndex &idx: selected) {
138 mapped.append(proxy.mapToSource(idx));
139 }
140
141 return model.filenames(mapped, allowPlaylists);
142 }
143
selectedSongs(bool allowPlaylists) const144 QList<Song> SearchPage::selectedSongs(bool allowPlaylists) const
145 {
146 QModelIndexList selected = view->selectedIndexes();
147 if (selected.isEmpty()) {
148 return QList<Song>();
149 }
150
151 QModelIndexList mapped;
152 for (const QModelIndex &idx: selected) {
153 mapped.append(proxy.mapToSource(idx));
154 }
155
156 return model.songs(mapped, allowPlaylists);
157 }
158
159 #ifdef ENABLE_DEVICES_SUPPORT
addSelectionToDevice(const QString & udi)160 void SearchPage::addSelectionToDevice(const QString &udi)
161 {
162 QList<Song> songs=selectedSongs();
163
164 if (!songs.isEmpty()) {
165 emit addToDevice(QString(), udi, songs);
166 view->clearSelection();
167 }
168 }
169
deleteSongs()170 void SearchPage::deleteSongs()
171 {
172 QList<Song> songs=selectedSongs();
173
174 if (!songs.isEmpty()) {
175 if (MessageBox::Yes==MessageBox::warningYesNo(this, tr("Are you sure you wish to delete the selected songs?\n\nThis cannot be undone."),
176 tr("Delete Songs"), StdGuiItem::del(), StdGuiItem::cancel())) {
177 emit deleteSongs(QString(), songs);
178 }
179 view->clearSelection();
180 }
181 }
182 #endif
183
doSearch()184 void SearchPage::doSearch()
185 {
186 QString text=view->searchText().trimmed();
187
188 if (text.isEmpty()) {
189 model.clear();
190 } else {
191 model.search(view->searchCategory(), text);
192 }
193 }
194
itemDoubleClicked(const QModelIndex &)195 void SearchPage::itemDoubleClicked(const QModelIndex &)
196 {
197 const QModelIndexList selected = view->selectedIndexes(false); // Dont need sorted selection here...
198 if (1!=selected.size()) {
199 return; //doubleclick should only have one selected item
200 }
201 addSelectionToPlaylist();
202 }
203
controlActions()204 void SearchPage::controlActions()
205 {
206 QModelIndexList selected=view->selectedIndexes(false); // Dont need sorted selection here...
207 bool enable=selected.count()>0;
208
209 StdActions::self()->enableAddToPlayQueue(enable);
210 CustomActions::self()->setEnabled(enable);
211 locateAction->setEnabled(enable);
212
213 StdActions::self()->addToStoredPlaylistAction->setEnabled(enable);
214 #ifdef TAGLIB_FOUND
215 StdActions::self()->organiseFilesAction->setEnabled(enable && MPDConnection::self()->getDetails().dirReadable);
216 StdActions::self()->editTagsAction->setEnabled(StdActions::self()->organiseFilesAction->isEnabled());
217 #ifdef ENABLE_REPLAYGAIN_SUPPORT
218 StdActions::self()->replaygainAction->setEnabled(StdActions::self()->organiseFilesAction->isEnabled());
219 #endif
220 #ifdef ENABLE_DEVICES_SUPPORT
221 StdActions::self()->deleteSongsAction->setEnabled(StdActions::self()->organiseFilesAction->isEnabled());
222 StdActions::self()->copyToDeviceAction->setEnabled(StdActions::self()->organiseFilesAction->isEnabled());
223 #endif
224 #endif // TAGLIB_FOUND
225 }
226
setSearchCategory(const QString & cat)227 void SearchPage::setSearchCategory(const QString &cat)
228 {
229 view->setSearchCategory(cat);
230 }
231
setSearchCategories()232 void SearchPage::setSearchCategories()
233 {
234 int newState=(MPDConnection::self()->composerTagSupported() ? State_ComposerSupported : 0)|
235 (MPDConnection::self()->commentTagSupported() ? State_CommmentSupported : 0)|
236 (MPDConnection::self()->performerTagSupported() ? State_PerformerSupported : 0)|
237 (MPDConnection::self()->modifiedFindSupported() ? State_ModifiedSupported : 0)|
238 (MPDConnection::self()->originalDateTagSupported() ? State_OrigDateSupported : 0);
239
240 if (state==newState) {
241 // No changes to be made!
242 return;
243 }
244
245 state=newState;
246 QList<SearchWidget::Category> categories;
247
248 categories << SearchWidget::Category(tr("Artist:"), QLatin1String("artist"), tr("Search for songs by artist."));
249
250 if (state&State_ComposerSupported) {
251 categories << SearchWidget::Category(tr("Composer:"), QLatin1String("composer"), tr("Search for songs by composer."));
252 }
253 if (state&State_PerformerSupported) {
254 categories << SearchWidget::Category(tr("Performer:"), QLatin1String("performer"), tr("Search for songs by performer."));
255 }
256 categories << SearchWidget::Category(tr("Album:"), QLatin1String("album"), tr("Search for songs by album."))
257 << SearchWidget::Category(tr("Title:"), QLatin1String("title"), tr("Search for songs by title."))
258 << SearchWidget::Category(tr("Genre:"), QLatin1String("genre"), tr("Search for songs by genre."));
259 if (state&State_CommmentSupported) {
260 categories << SearchWidget::Category(tr("Comment:"), QLatin1String("comment"), tr("Search for songs containing comment."));
261 }
262 categories << SearchWidget::Category(tr("Date:"), QLatin1String("date"),
263 tr("Find songs be searching the 'Date' tag.<br/><br/>Usually just entering the year should suffice."));
264 if (state&State_OrigDateSupported) {
265 categories << SearchWidget::Category(tr("Original Date:"), QLatin1String("originaldate"),
266 tr("Find songs be searching the 'Original Date' tag.<br/><br/>Usually just entering the year should suffice."));
267 }
268 if (state&State_ModifiedSupported) {
269 categories << SearchWidget::Category(tr("Modified:"), MPDConnection::constModifiedSince,
270 tr("Enter date (YYYY/MM/DD - e.g. 2015/01/31) to search for files modified since that date.<br/><br>"
271 "Or enter a number of days to find files that were modified in the previous number of days."));
272 }
273 categories << SearchWidget::Category(tr("File:"), QLatin1String("file"), tr("Search for songs by filename or path."))
274 << SearchWidget::Category(tr("Any:"), QLatin1String("any"), tr("Search for songs by any matching metadata."));
275 view->setSearchCategories(categories);
276 }
277
statsUpdated(int songs,quint32 time)278 void SearchPage::statsUpdated(int songs, quint32 time)
279 {
280 statsLabel->setText(0==songs ? tr("No tracks found.") : tr("%n Tracks (%1)", "", songs).arg(Utils::formatDuration(time)));
281 }
282
locateSongs()283 void SearchPage::locateSongs()
284 {
285 QList<Song> songs=selectedSongs();
286
287 if (!songs.isEmpty()) {
288 emit locate(songs);
289 }
290 }
291
292 #include "moc_searchpage.cpp"
293