Parent: [ecec38] (diff)

Child: [4fc016] (diff)

Download this file

dirbrowser.cpp    397 lines (357 with data), 12.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* Copyright (C) 2014 J.F.Dockes
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <QToolButton>
#include <QTabBar>
#include <QIcon>
#include <QTimer>
#include <QShortcut>
#include <QLineEdit>
#include "dirbrowser.h"
#include "HelperStructs/Helper.h"
#include "upadapt/upputils.h"
DirBrowser::DirBrowser(QWidget *parent, Playlist *pl)
: QWidget(parent), ui(new Ui::DirBrowser), m_pl(pl), m_insertactive(false)
{
QKeySequence seq;
QShortcut *sc;
ui->setupUi(this);
ui->searchFrame->hide();
ui->tabs->setTabsClosable(true);
QToolButton *plusbut = new QToolButton(this);
QString icon_path = Helper::getIconPath();
QIcon icon = QIcon(icon_path + "/addtab.png");
plusbut->setIcon(icon);
ui->tabs->setCornerWidget(plusbut, Qt::TopRightCorner);
plusbut->setCursor(Qt::ArrowCursor);
plusbut->setAutoRaise(true);
plusbut->setToolTip(tr("Add tab"));
connect(plusbut, SIGNAL(clicked()), this, SLOT(addTab()));
seq = QKeySequence("Ctrl+t");
sc = new QShortcut(seq, this);
connect(sc, SIGNAL (activated()), this, SLOT(addTab()));
seq = QKeySequence("Ctrl+w");
sc = new QShortcut(seq, this);
connect(sc, SIGNAL (activated()), this, SLOT(closeCurrentTab()));
connect(ui->tabs, SIGNAL(currentChanged(int)),
this, SLOT(onCurrentTabChanged(int)));
connect(ui->tabs, SIGNAL(tabCloseRequested(int)),
this, SLOT(closeTab(int)));
icon = QIcon(icon_path + "/cross.png");
ui->closeSearchTB->setIcon(icon);
connect(ui->closeSearchTB, SIGNAL(clicked()),
this, SLOT(closeSearchPanel()));
seq = QKeySequence("Ctrl+f");
sc = new QShortcut(seq, this);
connect(sc, SIGNAL (activated()), this, SLOT(openSearchPanel()));
seq = QKeySequence("/");
sc = new QShortcut(seq, this);
connect(sc, SIGNAL (activated()), this, SLOT(openSearchPanel()));
seq = QKeySequence("Esc");
sc = new QShortcut(seq, this);
connect(sc, SIGNAL (activated()), this, SLOT(closeSearchPanel()));
seq = QKeySequence(Qt::Key_F3);
sc = new QShortcut(seq, this);
connect(sc, SIGNAL(activated()), this, SLOT(searchNext()));
seq = QKeySequence(Qt::SHIFT + Qt::Key_F3);
sc = new QShortcut(seq, this);
connect(sc, SIGNAL(activated()), this, SLOT(searchPrev()));
connect(ui->searchCMB->lineEdit(), SIGNAL(textChanged(const QString&)),
this, SLOT(onSearchTextChanged(const QString&)));
connect(ui->nextTB, SIGNAL(clicked()), this, SLOT(searchNext()));
connect(ui->prevTB, SIGNAL(clicked()), this, SLOT(searchPrev()));
connect(ui->searchCMB->lineEdit(), SIGNAL(returnPressed()),
this, SLOT(returnPressedInSearch()));
seq = QKeySequence("Ctrl+s");
sc = new QShortcut(seq, this);
connect(sc, SIGNAL(activated()), this, SLOT(toggleSearchKind()));
connect(ui->serverSearchCB, SIGNAL(stateChanged(int)),
this, SLOT(onSearchKindChanged(int)));
connect(ui->execSearchPB, SIGNAL(clicked()), this, SLOT(serverSearch()));
onSearchKindChanged(int(ui->serverSearchCB->checkState()));
ui->execSearchPB->hide();
setPlaylist(pl);
}
void DirBrowser::setPlaylist(Playlist *pl)
{
m_pl = pl;
for (int i = 0; i < ui->tabs->count(); i++) {
setupTabConnections(i);
}
connect(m_pl, SIGNAL(insertDone()), this, SLOT(onInsertDone()));
}
void DirBrowser::setStyleSheet(bool dark)
{
for (int i = 0; i < ui->tabs->count(); i++) {
QWidget *tw = ui->tabs->widget(i);
if (tw) {
CDBrowser *cdb = tw->findChild<CDBrowser*>();
if (cdb) {
cdb->setStyleSheet(dark);
}
}
}
}
void DirBrowser::addTab()
{
QWidget *tab = new QWidget(this);
tab->setObjectName(QString::fromUtf8("tab"));
QVBoxLayout* vl = new QVBoxLayout(tab);
CDBrowser *cdb = new CDBrowser(tab);
cdb->setObjectName(QString::fromUtf8("cdBrowser"));
setupTabConnections(cdb);
vl->addWidget(cdb);
ui->tabs->addTab(tab, QString());
ui->tabs->setCurrentIndex(ui->tabs->count() -1);
}
void DirBrowser::closeCurrentTab()
{
closeTab(ui->tabs->currentIndex());
}
void DirBrowser::closeTab(int i)
{
if (m_insertactive || ui->tabs->count() <= 1)
return;
QWidget *w = ui->tabs->widget(i);
ui->tabs->removeTab(i);
delete w;
}
// Adjust the search panel according the the server caps for the
// active tab
void DirBrowser::onCurrentTabChanged(int)
{
CDBrowser *cdb = currentBrowser();
set<string> caps;
if (cdb)
cdb->getSearchCaps(caps);
while(ui->propsCMB->count())
ui->propsCMB->removeItem(0);
if (caps.empty()) {
ui->serverSearchCB->setCheckState(Qt::Unchecked);
ui->serverSearchCB->setEnabled(false);
} else {
ui->serverSearchCB->setEnabled(true);
QString scs;
vector<pair<string, string> > props;
props.push_back(pair<string,string>("upnp:artist", "Artist"));
props.push_back(pair<string,string>("upnp:album", "Album"));
props.push_back(pair<string,string>("dc:title", "Title"));
props.push_back(pair<string,string>("upnp:genre", "Genre"));
for (auto it = props.begin(); it != props.end(); it++) {
if (caps.find("*") != caps.end() ||
caps.find(it->first) != caps.end()) {
ui->propsCMB->addItem(u8s2qs(it->second),
QVariant(u8s2qs(it->first)));
}
scs += u8s2qs(it->first.c_str()) + " ";
}
qDebug() << "Search caps: " << scs;
onSearchKindChanged(ui->serverSearchCB->checkState());
}
}
void DirBrowser::changeTabTitle(QWidget *w, const QString& tt)
{
int i = ui->tabs->indexOf((QWidget*)w->parent());
if (i >= 0) {
ui->tabs->setTabText(i, tt);
} else {
qDebug() << "changeTabTitle: Widget not found in tabs: " << w;
}
}
// Any of the tabs searchcaps changed. Update the current one just in case.
void DirBrowser::onSearchcapsChanged()
{
onCurrentTabChanged(-1);
}
void DirBrowser::openSearchPanel()
{
onCurrentTabChanged(-1);
ui->searchFrame->show();
ui->searchCMB->lineEdit()->setFocus();
ui->searchCMB->lineEdit()->selectAll();
}
void DirBrowser::closeSearchPanel()
{
QString text = ui->searchCMB->lineEdit()->text();
if (ui->searchCMB->findText(text))
ui->searchCMB->insertItem(0, text);
ui->searchFrame->hide();
}
void DirBrowser::showSearchPanel(bool yes)
{
if (yes)
openSearchPanel();
else
closeSearchPanel();
}
void DirBrowser::toggleSearchKind()
{
if (!ui->serverSearchCB->isEnabled())
return;
if (ui->serverSearchCB->checkState()) {
ui->serverSearchCB->setCheckState(Qt::Unchecked);
} else {
ui->serverSearchCB->setCheckState(Qt::Checked);
}
onSearchKindChanged(ui->serverSearchCB->checkState());
}
void DirBrowser::onSearchKindChanged(int state)
{
if (state == Qt::Unchecked) {
// ui->propsCMB->hide();
ui->propsCMB->setEnabled(false);
ui->execSearchPB->hide();
ui->execSearchPB->setEnabled(false);
ui->nextTB->show();
ui->nextTB->setEnabled(true);
ui->prevTB->show();
ui->prevTB->setEnabled(true);
} else {
// ui->propsCMB->show();
ui->propsCMB->setEnabled(true);
ui->execSearchPB->show();
ui->execSearchPB->setEnabled(true);
ui->nextTB->hide();
ui->nextTB->setEnabled(false);
ui->prevTB->hide();
ui->prevTB->setEnabled(false);
}
}
void DirBrowser::onSearchTextChanged(const QString& text)
{
if (!ui->propsCMB->isEnabled()) {
if (!text.isEmpty()) {
doSearch(text, false);
}
}
}
void DirBrowser::searchNext()
{
doSearch(ui->searchCMB->currentText(), false);
}
void DirBrowser::searchPrev()
{
doSearch(ui->searchCMB->currentText(), true);
}
void DirBrowser::returnPressedInSearch()
{
if (ui->propsCMB->isEnabled()) {
serverSearch();
} else {
QString text = ui->searchCMB->lineEdit()->text();
if (ui->searchCMB->findText(text))
ui->searchCMB->insertItem(0, text);
searchNext();
}
}
void DirBrowser::serverSearch()
{
QString text = ui->searchCMB->lineEdit()->text();
int i = ui->propsCMB->currentIndex();
string prop = qs2utf8s(ui->propsCMB->itemData(i).toString());
if (text != "") {
string iss = qs2utf8s(text);
string ss(prop);
ss += " contains \"";
for (unsigned i = 0; i < iss.size(); i++) {
if (iss[i] == '"' || iss[i] == '\\')
ss += string("\\");
ss += iss[i];
}
ss += '"';
CDBrowser *cdb = currentBrowser();
if (cdb)
cdb->search(ss);
}
}
// Perform text search in current tab.
// @param next if true, we look for the next match of the
// current search, trying to advance and possibly wrapping around. Else is
// false, the search string has been modified, we search for the new string,
// starting from the current position
void DirBrowser::doSearch(const QString& text, bool reverse)
{
qDebug() << "DirBrowser::doSearch: text " << text << " reverse " << reverse;
CDBrowser *cdb = currentBrowser();
if (cdb == 0) {
return;
}
QWebPage::FindFlags options = QWebPage::FindWrapsAroundDocument;
if (reverse)
options |= QWebPage::FindBackward;
cdb->findText(text, options);
}
void DirBrowser::setInsertActive(bool onoff)
{
m_insertactive = onoff;
}
bool DirBrowser::insertActive()
{
return m_insertactive;
}
CDBrowser *DirBrowser::currentBrowser()
{
QWidget *tw = ui->tabs->currentWidget();
if (tw == 0)
return 0;
return tw->findChild<CDBrowser*>();
}
void DirBrowser::setupTabConnections(int i)
{
QWidget *tw = ui->tabs->widget(i);
CDBrowser *cdb;
if (tw) {
cdb = tw->findChild<CDBrowser*>();
} else {
qDebug() << "Tab " << i << " not found";
}
if (!cdb) {
qDebug() << "Tab " << i << " has no browser child";
return;
}
setupTabConnections(cdb);
}
void DirBrowser::setupTabConnections(CDBrowser *cdb)
{
cdb->setDirBrowser(this);
disconnect(cdb, SIGNAL(sig_now_in(QWidget *, const QString&)), 0, 0);
connect(cdb, SIGNAL(sig_now_in(QWidget *, const QString&)),
this, SLOT(changeTabTitle(QWidget *, const QString&)));
disconnect(cdb, SIGNAL(sig_tracks_to_playlist(const MetaDataList&)), 0, 0);
connect(cdb, SIGNAL(sig_tracks_to_playlist(const MetaDataList&)),
m_pl, SLOT(psl_add_tracks(const MetaDataList&)));
disconnect(cdb, SIGNAL(sig_searchcaps_changed()), 0, 0);
connect(cdb, SIGNAL(sig_searchcaps_changed()),
this, SLOT(onSearchcapsChanged()));
disconnect(cdb,
SIGNAL(sig_browse_in_new_tab(QString,
std::vector<CDBrowser::CtPathElt>)),
0, 0);
connect(cdb,
SIGNAL(sig_browse_in_new_tab(QString,
std::vector<CDBrowser::CtPathElt>)),
this, SLOT(onBrowseInNewTab(QString,
std::vector<CDBrowser::CtPathElt>)));
}
void DirBrowser::onBrowseInNewTab(QString UDN,
vector<CDBrowser::CtPathElt> path)
{
//qDebug() << "onBrowseInNewTab(): " << UDN;
addTab();
CDBrowser *cdb = currentBrowser();
if (cdb)
cdb->browseIn(UDN, path);
}