source: smplayer/vendor/current/src/playlist.cpp@ 168

Last change on this file since 168 was 168, checked in by Silvan Scherrer, 11 years ago

SMPlayer: update vendor to 14.9.0

File size: 37.8 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2014 Ricardo Villalba <[email protected]>
3
4 This program 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 2 of the License, or
7 (at your option) any later version.
8
9 This program 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 this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#include "playlist.h"
20
21#include <QToolBar>
22#include <QFile>
23#include <QTextStream>
24#include <QDir>
25#include <QFileInfo>
26#include <QMessageBox>
27#include <QPushButton>
28#include <QRegExp>
29#include <QMenu>
30#include <QDateTime>
31#include <QSettings>
32#include <QInputDialog>
33#include <QToolButton>
34#include <QTimer>
35#include <QVBoxLayout>
36#include <QUrl>
37#include <QDragEnterEvent>
38#include <QDropEvent>
39#include <QHeaderView>
40#include <QTextCodec>
41#include <QApplication>
42#include <QMimeData>
43
44#include "mytablewidget.h"
45#include "myaction.h"
46#include "filedialog.h"
47#include "helper.h"
48#include "images.h"
49#include "preferences.h"
50#include "multilineinputdialog.h"
51#include "version.h"
52#include "global.h"
53#include "core.h"
54#include "extensions.h"
55#include "guiconfig.h"
56
57#include <stdlib.h>
58
59#if USE_INFOPROVIDER
60#include "infoprovider.h"
61#endif
62
63#define DRAG_ITEMS 0
64
65#define COL_PLAY 0
66#define COL_NAME 1
67#define COL_TIME 2
68
69using namespace Global;
70
71
72Playlist::Playlist( Core *c, QWidget * parent, Qt::WindowFlags f)
73 : QWidget(parent,f)
74{
75 save_playlist_in_config = true;
76 recursive_add_directory = false;
77 automatically_get_info = false;
78 play_files_from_start = true;
79
80 automatically_play_next = true;
81
82 row_spacing = -1; // Default height
83
84 modified = false;
85
86 core = c;
87 playlist_path = "";
88 latest_dir = "";
89
90 createTable();
91 createActions();
92 createToolbar();
93
94 connect( core, SIGNAL(mediaFinished()), this, SLOT(playNext()), Qt::QueuedConnection );
95 connect( core, SIGNAL(mediaLoaded()), this, SLOT(getMediaInfo()) );
96
97 QVBoxLayout *layout = new QVBoxLayout;
98 layout->addWidget( listView );
99 layout->addWidget( toolbar );
100 setLayout(layout);
101
102 clear();
103
104 retranslateStrings();
105
106#if !DOCK_PLAYLIST
107 setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Expanding );
108 adjustSize();
109#else
110 //setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Expanding );
111 //setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
112 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
113#endif
114
115 setAcceptDrops(true);
116 setAttribute(Qt::WA_NoMousePropagation);
117
118 // Random seed
119 QTime t;
120 t.start();
121 srand( t.hour() * 3600 + t.minute() * 60 + t.second() );
122
123 loadSettings();
124
125 // Ugly hack to avoid to play next item automatically
126 if (!automatically_play_next) {
127 disconnect( core, SIGNAL(mediaFinished()), this, SLOT(playNext()) );
128 }
129
130 // Save config every 5 minutes.
131 save_timer = new QTimer(this);
132 connect( save_timer, SIGNAL(timeout()), this, SLOT(maybeSaveSettings()) );
133 save_timer->start( 5 * 60000 );
134}
135
136Playlist::~Playlist() {
137 saveSettings();
138}
139
140void Playlist::setModified(bool mod) {
141 qDebug("Playlist::setModified: %d", mod);
142
143 modified = mod;
144 emit modifiedChanged(modified);
145}
146
147void Playlist::createTable() {
148 listView = new MyTableWidget( 0, COL_TIME + 1, this);
149 listView->setObjectName("playlist_table");
150 listView->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
151 listView->setSelectionBehavior(QAbstractItemView::SelectRows);
152 listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
153 listView->setContextMenuPolicy( Qt::CustomContextMenu );
154 listView->setShowGrid(false);
155 listView->setSortingEnabled(false);
156 //listView->setAlternatingRowColors(true);
157
158#if QT_VERSION >= 0x050000
159 listView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
160 listView->horizontalHeader()->setSectionResizeMode(COL_NAME, QHeaderView::Stretch);
161#else
162 listView->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
163 listView->horizontalHeader()->setResizeMode(COL_NAME, QHeaderView::Stretch);
164#endif
165 /*
166 listView->horizontalHeader()->setResizeMode(COL_TIME, QHeaderView::ResizeToContents);
167 listView->horizontalHeader()->setResizeMode(COL_PLAY, QHeaderView::ResizeToContents);
168 */
169 listView->setIconSize( Images::icon("ok").size() );
170
171#if DRAG_ITEMS
172 listView->setSelectionMode(QAbstractItemView::SingleSelection);
173 listView->setDragEnabled(true);
174 listView->setAcceptDrops(true);
175 listView->setDropIndicatorShown(true);
176 listView->setDragDropMode(QAbstractItemView::InternalMove);
177#endif
178
179 connect( listView, SIGNAL(cellActivated(int,int)),
180 this, SLOT(itemDoubleClicked(int)) );
181
182 // EDIT BY NEO -->
183 connect( listView->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(sortBy(int)));
184 // <--
185}
186
187void Playlist::createActions() {
188 openAct = new MyAction(this, "pl_open", false);
189 connect( openAct, SIGNAL(triggered()), this, SLOT(load()) );
190
191 saveAct = new MyAction(this, "pl_save", false);
192 connect( saveAct, SIGNAL(triggered()), this, SLOT(save()) );
193
194 playAct = new MyAction(this, "pl_play", false);
195 connect( playAct, SIGNAL(triggered()), this, SLOT(playCurrent()) );
196
197 nextAct = new MyAction(Qt::Key_N /*Qt::Key_Greater*/, this, "pl_next", false);
198 connect( nextAct, SIGNAL(triggered()), this, SLOT(playNext()) );
199
200 prevAct = new MyAction(Qt::Key_P /*Qt::Key_Less*/, this, "pl_prev", false);
201 connect( prevAct, SIGNAL(triggered()), this, SLOT(playPrev()) );
202
203 moveUpAct = new MyAction(this, "pl_move_up", false);
204 connect( moveUpAct, SIGNAL(triggered()), this, SLOT(upItem()) );
205
206 moveDownAct = new MyAction(this, "pl_move_down", false);
207 connect( moveDownAct, SIGNAL(triggered()), this, SLOT(downItem()) );
208
209 repeatAct = new MyAction(this, "pl_repeat", false);
210 repeatAct->setCheckable(true);
211
212 shuffleAct = new MyAction(this, "pl_shuffle", false);
213 shuffleAct->setCheckable(true);
214
215 // Add actions
216 addCurrentAct = new MyAction(this, "pl_add_current", false);
217 connect( addCurrentAct, SIGNAL(triggered()), this, SLOT(addCurrentFile()) );
218
219 addFilesAct = new MyAction(this, "pl_add_files", false);
220 connect( addFilesAct, SIGNAL(triggered()), this, SLOT(addFiles()) );
221
222 addDirectoryAct = new MyAction(this, "pl_add_directory", false);
223 connect( addDirectoryAct, SIGNAL(triggered()), this, SLOT(addDirectory()) );
224
225 addUrlsAct = new MyAction(this, "pl_add_urls", false);
226 connect( addUrlsAct, SIGNAL(triggered()), this, SLOT(addUrls()) );
227
228 // Remove actions
229 removeSelectedAct = new MyAction(this, "pl_remove_selected", false);
230 connect( removeSelectedAct, SIGNAL(triggered()), this, SLOT(removeSelected()) );
231
232 removeAllAct = new MyAction(this, "pl_remove_all", false);
233 connect( removeAllAct, SIGNAL(triggered()), this, SLOT(removeAll()) );
234
235 // Edit
236 editAct = new MyAction(this, "pl_edit", false);
237 connect( editAct, SIGNAL(triggered()), this, SLOT(editCurrentItem()) );
238}
239
240void Playlist::createToolbar() {
241 toolbar = new QToolBar(this);
242 toolbar->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
243
244 toolbar->addAction(openAct);
245 toolbar->addAction(saveAct);;
246 toolbar->addSeparator();
247
248 add_menu = new QMenu( this );
249 add_menu->addAction(addCurrentAct);
250 add_menu->addAction(addFilesAct );
251 add_menu->addAction(addDirectoryAct);
252 add_menu->addAction(addUrlsAct);
253
254 add_button = new QToolButton( this );
255 add_button->setMenu( add_menu );
256 add_button->setPopupMode(QToolButton::InstantPopup);
257
258 remove_menu = new QMenu( this );
259 remove_menu->addAction(removeSelectedAct);
260 remove_menu->addAction(removeAllAct);
261
262 remove_button = new QToolButton( this );
263 remove_button->setMenu( remove_menu );
264 remove_button->setPopupMode(QToolButton::InstantPopup);
265
266 toolbar->addWidget(add_button);
267 toolbar->addWidget(remove_button);
268
269 toolbar->addSeparator();
270 toolbar->addAction(playAct);
271 toolbar->addSeparator();
272 toolbar->addAction(prevAct);
273 toolbar->addAction(nextAct);
274 toolbar->addSeparator();
275 toolbar->addAction(repeatAct);
276 toolbar->addAction(shuffleAct);
277 toolbar->addSeparator();
278 toolbar->addAction(moveUpAct);
279 toolbar->addAction(moveDownAct);
280
281 // Popup menu
282 popup = new QMenu(this);
283 popup->addAction(playAct);
284 popup->addAction(removeSelectedAct);
285 popup->addAction(editAct);
286
287 connect( listView, SIGNAL(customContextMenuRequested(const QPoint &)),
288 this, SLOT(showPopup(const QPoint &)) );
289}
290
291void Playlist::retranslateStrings() {
292 listView->setHorizontalHeaderLabels( QStringList() << " " <<
293 tr("Name") << tr("Length") );
294
295 openAct->change( Images::icon("open"), tr("&Load") );
296 saveAct->change( Images::icon("save"), tr("&Save") );
297
298 playAct->change( tr("&Play") );
299
300 nextAct->change( tr("&Next") );
301 prevAct->change( tr("Pre&vious") );
302
303 playAct->setIcon( Images::icon("play") );
304 nextAct->setIcon( Images::icon("next") );
305 prevAct->setIcon( Images::icon("previous") );
306
307 moveUpAct->change( Images::icon("up"), tr("Move &up") );
308 moveDownAct->change( Images::icon("down"), tr("Move &down") );
309
310 repeatAct->change( Images::icon("repeat"), tr("&Repeat") );
311 shuffleAct->change( Images::icon("shuffle"), tr("S&huffle") );
312
313 // Add actions
314 addCurrentAct->change( tr("Add &current file") );
315 addFilesAct->change( tr("Add &file(s)") );
316 addDirectoryAct->change( tr("Add &directory") );
317 addUrlsAct->change( tr("Add &URL(s)") );
318
319 // Remove actions
320 removeSelectedAct->change( tr("Remove &selected") );
321 removeAllAct->change( tr("Remove &all") );
322
323 // Edit
324 editAct->change( tr("&Edit") );
325
326 // Tool buttons
327 add_button->setIcon( Images::icon("plus") );
328 add_button->setToolTip( tr("Add...") );
329 remove_button->setIcon( Images::icon("minus") );
330 remove_button->setToolTip( tr("Remove...") );
331
332 // Icon
333 setWindowIcon( Images::icon("logo", 64) );
334 setWindowTitle( tr( "SMPlayer - Playlist" ) );
335}
336
337void Playlist::list() {
338 qDebug("Playlist::list");
339
340 PlaylistItemList::iterator it;
341 for ( it = pl.begin(); it != pl.end(); ++it ) {
342 qDebug( "filename: '%s', name: '%s' duration: %f",
343 (*it).filename().toUtf8().data(), (*it).name().toUtf8().data(),
344 (*it).duration() );
345 }
346}
347
348
349QString Playlist::print(QString seperator){
350 qDebug("Playlist::print");
351 QString output = "";
352
353 PlaylistItemList::iterator it;
354 for ( it = pl.begin(); it != pl.end(); ++it ) {
355 output += it->filename() + seperator + it->name() + seperator + QString::number(it->duration()) + "\r\n";
356 }
357
358 return output;
359}
360
361void Playlist::updateView() {
362 qDebug("Playlist::updateView");
363
364 listView->setRowCount( pl.count() );
365
366 //QString number;
367 QString name;
368 QString time;
369
370 for (int n=0; n < pl.count(); n++) {
371 name = pl[n].name();
372 if (name.isEmpty()) name = pl[n].filename();
373 time = Helper::formatTime( (int) pl[n].duration() );
374
375 //listView->setText(n, COL_POS, number);
376 qDebug("Playlist::updateView: name: '%s'", name.toUtf8().data());
377 listView->setText(n, COL_NAME, name);
378 listView->setText(n, COL_TIME, time);
379
380 if (pl[n].played()) {
381 listView->setIcon(n, COL_PLAY, Images::icon("ok") );
382 } else {
383 listView->setIcon(n, COL_PLAY, QPixmap() );
384 }
385
386 if (row_spacing > -1) listView->setRowHeight(n, listView->font().pointSize() + row_spacing);
387 }
388 //listView->resizeColumnsToContents();
389 listView->resizeColumnToContents(COL_PLAY);
390 listView->resizeColumnToContents(COL_TIME);
391
392 setCurrentItem(current_item);
393
394 //adjustSize();
395}
396
397void Playlist::setCurrentItem(int current) {
398 QIcon play_icon;
399 play_icon = Images::icon("play");
400
401 int old_current = current_item;
402 current_item = current;
403
404 if ((current_item > -1) && (current_item < pl.count())) {
405 pl[current_item].setPlayed(true);
406 }
407
408 if ( (old_current >= 0) && (old_current < listView->rowCount()) ) {
409 listView->setIcon(old_current, COL_PLAY, QPixmap() );
410 }
411
412 if ( (current_item >= 0) && (current_item < listView->rowCount()) ) {
413 listView->setIcon(current_item, COL_PLAY, play_icon );
414 }
415 //if (current_item >= 0) listView->selectRow(current_item);
416 if (current_item >= 0) {
417 listView->clearSelection();
418 listView->setCurrentCell( current_item, 0);
419 }
420}
421
422void Playlist::clear() {
423 pl.clear();
424
425 listView->clearContents();
426 listView->setRowCount(0);
427
428 setCurrentItem(0);
429
430 setModified( false );
431}
432
433void Playlist::remove(int i){
434 if(i > -1 && i < pl.count()){
435 pl.removeAt(i);
436 if(current_item == i && i == (pl.count() - 1))
437 setCurrentItem(i - 1);
438 setModified(false);
439 updateView();
440 } //end if
441}
442
443int Playlist::count() {
444 return pl.count();
445}
446
447bool Playlist::isEmpty() {
448 return pl.isEmpty();
449}
450
451void Playlist::addItem(QString filename, QString name, double duration) {
452 qDebug("Playlist::addItem: '%s'", filename.toUtf8().data());
453
454 #if defined(Q_OS_WIN) || defined(Q_OS_OS2)
455 filename = Helper::changeSlashes(filename);
456 #endif
457
458 // Test if already is in the list
459 bool exists = false;
460 for ( int n = 0; n < pl.count(); n++) {
461 if ( pl[n].filename() == filename ) {
462 exists = true;
463 int last_item = pl.count()-1;
464 pl.move(n, last_item);
465 qDebug("Playlist::addItem: item already in list (%d), moved to %d", n, last_item);
466 if (current_item > -1) {
467 if (current_item > n) current_item--;
468 else
469 if (current_item == n) current_item = last_item;
470 }
471 break;
472 }
473 }
474
475 if (!exists) {
476 if (name.isEmpty()) {
477 QFileInfo fi(filename);
478 // Let's see if it looks like a file (no dvd://1 or something)
479 if (filename.indexOf(QRegExp("^.*://.*")) == -1) {
480 // Local file
481 name = fi.fileName(); //fi.baseName(true);
482 } else {
483 // Stream
484 name = filename;
485 }
486 }
487 pl.append( PlaylistItem(filename, name, duration) );
488 //setModified( true ); // Better set the modified on a higher level
489 } else {
490 qDebug("Playlist::addItem: item not added, already in the list");
491 }
492}
493
494// EDIT BY NEO -->
495void Playlist::sortBy(int section) {
496 qDebug("Playlist::sortBy");
497
498 sortBy(section, false, 0);
499}
500
501void Playlist::sortBy(int section, bool revert, int count) {
502 // Bubble sort
503 bool swaped = false;
504
505 for ( int n = 0; n < (pl.count() - count); n++) {
506
507 int last = n - 1;
508 int current = n;
509
510 // Revert the sort
511 if (revert) {
512 last = n;
513 current = n - 1;
514 }
515
516 if (n > 0) {
517 int compare = 0;
518
519 if (section == 0) {
520 // Sort by played
521 bool lastItem = pl[last].played();
522 bool currentItem = pl[current].played();
523
524 if (!lastItem && currentItem) {
525 compare = 1;
526 } else if (lastItem && currentItem) {
527 if (last == current_item) {
528 compare = 1;
529 } else {
530 compare = -1;
531 }
532 } else {
533 compare = -1;
534 }
535 }
536 else if (section == 1) {
537 // Sort alphabetically
538 QString lastItem = pl[last].name();
539 QString currentItem = pl[current].name();
540 compare = lastItem.compare(currentItem);
541 } else if (section == 2) {
542 // Sort by duration
543 double lastItem = pl[last].duration();
544 double currentItem = pl[current].duration();
545
546 if (lastItem == currentItem) {
547 compare = 0;
548 } else if (lastItem > currentItem) {
549 compare = 1;
550 } else {
551 compare = -1;
552 }
553 }
554
555 // Swap items
556 if(compare > 0) {
557 swapItems(n, n - 1);
558
559 if (current_item == (n - 1)) {
560 current_item = n;
561 } else if (current_item == n) {
562 current_item = n - 1;
563 }
564
565 listView->clearSelection();
566 listView->setCurrentCell(n - 1, 0);
567
568 swaped = true;
569 }
570 }
571 }
572
573 if ((count == 0) && !swaped && !revert) {
574 // Revert sort
575 sortBy(section, true, 0);
576 }else if(swaped) {
577 // Sort until there is nothing to sort
578 sortBy(section, revert, ++count);
579 } else {
580 updateView();
581 }
582}
583// <--
584
585void Playlist::load_m3u(QString file) {
586 qDebug("Playlist::load_m3u");
587
588 bool utf8 = (QFileInfo(file).suffix().toLower() == "m3u8");
589
590 QRegExp m3u_id("^#EXTM3U|^#M3U");
591 QRegExp info("^#EXTINF:(.*),(.*)");
592
593 QFile f( file );
594 if ( f.open( QIODevice::ReadOnly ) ) {
595 playlist_path = QFileInfo(file).path();
596
597 clear();
598 QString filename="";
599 QString name="";
600 double duration=0;
601
602 QTextStream stream( &f );
603
604 if (utf8)
605 stream.setCodec("UTF-8");
606 else
607 stream.setCodec(QTextCodec::codecForLocale());
608
609 QString line;
610 while ( !stream.atEnd() ) {
611 line = stream.readLine(); // line of text excluding '\n'
612 qDebug( " * line: '%s'", line.toUtf8().data() );
613 if (m3u_id.indexIn(line)!=-1) {
614 //#EXTM3U
615 // Ignore line
616 }
617 else
618 if (info.indexIn(line)!=-1) {
619 duration = info.cap(1).toDouble();
620 name = info.cap(2);
621 qDebug(" * name: '%s', duration: %f", name.toUtf8().data(), duration );
622 }
623 else
624 if (line.startsWith("#")) {
625 // Comment
626 // Ignore
627 } else {
628 filename = line;
629 QFileInfo fi(filename);
630 if (fi.exists()) {
631 filename = fi.absoluteFilePath();
632 }
633 if (!fi.exists()) {
634 if (QFileInfo( playlist_path + "/" + filename).exists() ) {
635 filename = playlist_path + "/" + filename;
636 }
637 }
638 addItem( filename, name, duration );
639 name="";
640 duration = 0;
641 }
642 }
643 f.close();
644 list();
645 updateView();
646
647 setModified( false );
648
649 startPlay();
650 }
651}
652
653void Playlist::load_pls(QString file) {
654 qDebug("Playlist::load_pls");
655
656 if (!QFile::exists(file)) {
657 qDebug("Playlist::load_pls: '%s' doesn't exist, doing nothing", file.toUtf8().constData());
658 return;
659 }
660
661 playlist_path = QFileInfo(file).path();
662
663 QSettings set(file, QSettings::IniFormat);
664 set.beginGroup("playlist");
665
666 if (set.status() == QSettings::NoError) {
667 clear();
668 QString filename;
669 QString name;
670 double duration;
671
672 int num_items = set.value("NumberOfEntries", 0).toInt();
673
674 for (int n=0; n < num_items; n++) {
675 filename = set.value("File"+QString::number(n+1), "").toString();
676 name = set.value("Title"+QString::number(n+1), "").toString();
677 duration = (double) set.value("Length"+QString::number(n+1), 0).toInt();
678
679 QFileInfo fi(filename);
680 if (fi.exists()) {
681 filename = fi.absoluteFilePath();
682 }
683 if (!fi.exists()) {
684 if (QFileInfo( playlist_path + "/" + filename).exists() ) {
685 filename = playlist_path + "/" + filename;
686 }
687 }
688 addItem( filename, name, duration );
689 }
690 }
691
692 set.endGroup();
693
694 list();
695 updateView();
696
697 setModified( false );
698
699 if (set.status() == QSettings::NoError) startPlay();
700}
701
702bool Playlist::save_m3u(QString file) {
703 qDebug("Playlist::save_m3u: '%s'", file.toUtf8().data());
704
705 QString dir_path = QFileInfo(file).path();
706 if (!dir_path.endsWith("/")) dir_path += "/";
707
708 #if defined(Q_OS_WIN) || defined(Q_OS_OS2)
709 dir_path = Helper::changeSlashes(dir_path);
710 #endif
711
712 qDebug(" * dirPath: '%s'", dir_path.toUtf8().data());
713
714 bool utf8 = (QFileInfo(file).suffix().toLower() == "m3u8");
715
716 QFile f( file );
717 if ( f.open( QIODevice::WriteOnly ) ) {
718 QTextStream stream( &f );
719
720 if (utf8)
721 stream.setCodec("UTF-8");
722 else
723 stream.setCodec(QTextCodec::codecForLocale());
724
725 QString filename;
726
727 stream << "#EXTM3U" << "\n";
728 stream << "# Playlist created by SMPlayer " << Version::printable() << " \n";
729
730 PlaylistItemList::iterator it;
731 for ( it = pl.begin(); it != pl.end(); ++it ) {
732 filename = (*it).filename();
733 #if defined(Q_OS_WIN) || defined(Q_OS_OS2)
734 filename = Helper::changeSlashes(filename);
735 #endif
736 stream << "#EXTINF:";
737 stream << (*it).duration() << ",";
738 stream << (*it).name() << "\n";
739 // Try to save the filename as relative instead of absolute
740 if (filename.startsWith( dir_path )) {
741 filename = filename.mid( dir_path.length() );
742 }
743 stream << filename << "\n";
744 }
745 f.close();
746
747 setModified( false );
748 return true;
749 } else {
750 return false;
751 }
752}
753
754
755bool Playlist::save_pls(QString file) {
756 qDebug("Playlist::save_pls: '%s'", file.toUtf8().data());
757
758 QString dir_path = QFileInfo(file).path();
759 if (!dir_path.endsWith("/")) dir_path += "/";
760
761 #if defined(Q_OS_WIN) || defined(Q_OS_OS2)
762 dir_path = Helper::changeSlashes(dir_path);
763 #endif
764
765 qDebug(" * dirPath: '%s'", dir_path.toUtf8().data());
766
767 QSettings set(file, QSettings::IniFormat);
768 set.beginGroup( "playlist");
769
770 QString filename;
771
772 PlaylistItemList::iterator it;
773 for ( int n=0; n < pl.count(); n++ ) {
774 filename = pl[n].filename();
775 #if defined(Q_OS_WIN) || defined(Q_OS_OS2)
776 filename = Helper::changeSlashes(filename);
777 #endif
778
779 // Try to save the filename as relative instead of absolute
780 if (filename.startsWith( dir_path )) {
781 filename = filename.mid( dir_path.length() );
782 }
783
784 set.setValue("File"+QString::number(n+1), filename);
785 set.setValue("Title"+QString::number(n+1), pl[n].name());
786 set.setValue("Length"+QString::number(n+1), (int) pl[n].duration());
787 }
788
789 set.setValue("NumberOfEntries", pl.count());
790 set.setValue("Version", 2);
791
792 set.endGroup();
793
794 set.sync();
795
796 bool ok = (set.status() == QSettings::NoError);
797 if (ok) setModified( false );
798
799 return ok;
800}
801
802
803void Playlist::load() {
804 if (maybeSave()) {
805 Extensions e;
806 QString s = MyFileDialog::getOpenFileName(
807 this, tr("Choose a file"),
808 lastDir(),
809 tr("Playlists") + e.playlist().forFilter());
810
811 if (!s.isEmpty()) {
812 latest_dir = QFileInfo(s).absolutePath();
813
814 if (QFileInfo(s).suffix().toLower() == "pls")
815 load_pls(s);
816 else
817 load_m3u(s);
818 }
819 }
820}
821
822bool Playlist::save() {
823 Extensions e;
824 QString s = MyFileDialog::getSaveFileName(
825 this, tr("Choose a filename"),
826 lastDir(),
827 tr("Playlists") + e.playlist().forFilter());
828
829 if (!s.isEmpty()) {
830 // If filename has no extension, add it
831 if (QFileInfo(s).suffix().isEmpty()) {
832 s = s + ".m3u";
833 }
834 if (QFileInfo(s).exists()) {
835 int res = QMessageBox::question( this,
836 tr("Confirm overwrite?"),
837 tr("The file %1 already exists.\n"
838 "Do you want to overwrite?").arg(s),
839 QMessageBox::Yes,
840 QMessageBox::No,
841 QMessageBox::NoButton);
842 if (res == QMessageBox::No ) {
843 return false;
844 }
845 }
846 latest_dir = QFileInfo(s).absolutePath();
847
848 if (QFileInfo(s).suffix().toLower() == "pls")
849 return save_pls(s);
850 else
851 return save_m3u(s);
852
853 } else {
854 return false;
855 }
856}
857
858bool Playlist::maybeSave() {
859 if (!isModified()) return true;
860
861 int res = QMessageBox::question( this,
862 tr("Playlist modified"),
863 tr("There are unsaved changes, do you want to save the playlist?"),
864 QMessageBox::Yes,
865 QMessageBox::No,
866 QMessageBox::Cancel);
867
868 switch (res) {
869 case QMessageBox::No : return true; // Discard changes
870 case QMessageBox::Cancel : return false; // Cancel operation
871 default : return save();
872 }
873}
874
875void Playlist::playCurrent() {
876 int current = listView->currentRow();
877 if (current > -1) {
878 playItem(current);
879 }
880}
881
882void Playlist::itemDoubleClicked(int row) {
883 qDebug("Playlist::itemDoubleClicked: row: %d", row );
884 playItem(row);
885}
886
887void Playlist::showPopup(const QPoint & pos) {
888 qDebug("Playlist::showPopup: x: %d y: %d", pos.x(), pos.y() );
889
890 if (!popup->isVisible()) {
891 popup->move( listView->viewport()->mapToGlobal(pos) );
892 popup->show();
893 }
894}
895
896void Playlist::startPlay() {
897 // Start to play
898 if ( shuffleAct->isChecked() )
899 playItem( chooseRandomItem() );
900 else
901 playItem(0);
902}
903
904void Playlist::playItem( int n ) {
905 qDebug("Playlist::playItem: %d (count:%d)", n, pl.count());
906
907 if ( (n >= pl.count()) || (n < 0) ) {
908 qDebug("Playlist::playItem: out of range");
909 emit playlistEnded();
910 return;
911 }
912
913 qDebug(" playlist_path: '%s'", playlist_path.toUtf8().data() );
914
915 QString filename = pl[n].filename();
916 QString filename_with_path = playlist_path + "/" + filename;
917
918 if (!filename.isEmpty()) {
919 //pl[n].setPlayed(true);
920 setCurrentItem(n);
921 if (play_files_from_start)
922 core->open(filename, 0);
923 else
924 core->open(filename);
925 }
926
927}
928
929void Playlist::playNext() {
930 qDebug("Playlist::playNext");
931
932 if (shuffleAct->isChecked()) {
933 // Shuffle
934 int chosen_item = chooseRandomItem();
935 if (chosen_item == -1) {
936 clearPlayedTag();
937 if (repeatAct->isChecked()) chosen_item = chooseRandomItem();
938 }
939 playItem( chosen_item );
940 } else {
941 bool finished_list = (current_item+1 >= pl.count());
942 if (finished_list) clearPlayedTag();
943
944 if ( (repeatAct->isChecked()) && (finished_list) ) {
945 playItem(0);
946 } else {
947 playItem( current_item+1 );
948 }
949 }
950}
951
952void Playlist::playPrev() {
953 qDebug("Playlist::playPrev");
954 if (current_item > 0) {
955 playItem( current_item-1 );
956 } else {
957 if (pl.count() > 1) playItem( pl.count() -1 );
958 }
959}
960
961
962void Playlist::resumePlay() {
963 if (pl.count() > 0) {
964 if (current_item < 0) current_item = 0;
965 playItem(current_item);
966 }
967}
968
969void Playlist::getMediaInfo() {
970 qDebug("Playlist:: getMediaInfo");
971
972 QString filename = core->mdat.filename;
973 double duration = core->mdat.duration;
974 QString artist = core->mdat.clip_artist;
975
976 QString name = core->mdat.clip_name;
977 if (name.isEmpty()) name = core->mdat.stream_title;
978
979 #if defined(Q_OS_WIN) || defined(Q_OS_OS2)
980 filename = Helper::changeSlashes(filename);
981 #endif
982
983 if (name.isEmpty()) {
984 QFileInfo fi(filename);
985 if (fi.exists()) {
986 // Local file
987 name = fi.fileName();
988 } else {
989 // Stream
990 name = filename;
991 }
992 }
993 if (!artist.isEmpty()) name = artist + " - " + name;
994
995 int pos=0;
996 PlaylistItemList::iterator it;
997 for ( it = pl.begin(); it != pl.end(); ++it ) {
998 /* qDebug("Playlist:: getMediaInfo: f1: %s f2: %s", (*it).filename().toUtf8().constData(), filename.toUtf8().constData()); */
999 if ( (*it).filename() == filename ) {
1000 /* qDebug("Playlist:: getMediaInfo: duration: %f", (*it).duration()); */
1001 if ((*it).duration()<1) {
1002 if (!name.isEmpty()) {
1003 (*it).setName(name);
1004 }
1005 (*it).setDuration(duration);
1006 //setModified( true );
1007 }
1008 else
1009 // Edited name (sets duration to 1)
1010 if ((*it).duration()==1) {
1011 (*it).setDuration(duration);
1012 //setModified( true );
1013 }
1014 setCurrentItem(pos);
1015 }
1016 pos++;
1017 }
1018 updateView();
1019}
1020
1021// Add current file to playlist
1022void Playlist::addCurrentFile() {
1023 qDebug("Playlist::addCurrentFile");
1024 if (!core->mdat.filename.isEmpty()) {
1025 addItem( core->mdat.filename, "", 0 );
1026 getMediaInfo();
1027 }
1028}
1029
1030void Playlist::addFiles() {
1031 Extensions e;
1032 QStringList files = MyFileDialog::getOpenFileNames(
1033 this, tr("Select one or more files to open"),
1034 lastDir(),
1035 tr("Multimedia") + e.multimedia().forFilter() + ";;" +
1036 tr("All files") +" (*.*)" );
1037
1038 if (files.count()!=0) addFiles(files);
1039}
1040
1041void Playlist::addFiles(QStringList files, AutoGetInfo auto_get_info) {
1042 qDebug("Playlist::addFiles");
1043
1044#if USE_INFOPROVIDER
1045 bool get_info = (auto_get_info == GetInfo);
1046 if (auto_get_info == UserDefined) {
1047 get_info = automatically_get_info;
1048 }
1049
1050 MediaData data;
1051 setCursor(Qt::WaitCursor);
1052#endif
1053
1054 QStringList::Iterator it = files.begin();
1055 while( it != files.end() ) {
1056#if USE_INFOPROVIDER
1057 if ( (get_info) && (QFile::exists((*it))) ) {
1058 data = InfoProvider::getInfo( (*it) );
1059 addItem( (*it), data.displayName(), data.duration );
1060 //updateView();
1061 //qApp->processEvents();
1062 } else {
1063 addItem( (*it), "", 0 );
1064 }
1065#else
1066 addItem( (*it), "", 0 );
1067#endif
1068
1069 if (QFile::exists(*it)) {
1070 latest_dir = QFileInfo((*it)).absolutePath();
1071 }
1072
1073 ++it;
1074 }
1075#if USE_INFOPROVIDER
1076 unsetCursor();
1077#endif
1078 updateView();
1079
1080 qDebug( "Playlist::addFiles: latest_dir: '%s'", latest_dir.toUtf8().constData() );
1081}
1082
1083void Playlist::addFile(QString file, AutoGetInfo auto_get_info) {
1084 addFiles( QStringList() << file, auto_get_info );
1085}
1086
1087void Playlist::addDirectory() {
1088 QString s = MyFileDialog::getExistingDirectory(
1089 this, tr("Choose a directory"),
1090 lastDir() );
1091
1092 if (!s.isEmpty()) {
1093 addDirectory(s);
1094 latest_dir = s;
1095 }
1096}
1097
1098void Playlist::addUrls() {
1099 MultilineInputDialog d(this);
1100 if (d.exec() == QDialog::Accepted) {
1101 QStringList urls = d.lines();
1102 foreach(QString u, urls) {
1103 if (!u.isEmpty()) addItem( u, "", 0 );
1104 }
1105 updateView();
1106 }
1107}
1108
1109void Playlist::addOneDirectory(QString dir) {
1110 QStringList filelist;
1111
1112 Extensions e;
1113 QRegExp rx_ext(e.multimedia().forRegExp());
1114 rx_ext.setCaseSensitivity(Qt::CaseInsensitive);
1115
1116 QStringList dir_list = QDir(dir).entryList();
1117
1118 QString filename;
1119 QStringList::Iterator it = dir_list.begin();
1120 while( it != dir_list.end() ) {
1121 filename = dir;
1122 if (filename.right(1)!="/") filename += "/";
1123 filename += (*it);
1124 QFileInfo fi(filename);
1125 if (!fi.isDir()) {
1126 if (rx_ext.indexIn(fi.suffix()) > -1) {
1127 filelist << filename;
1128 }
1129 }
1130 ++it;
1131 }
1132 addFiles(filelist);
1133}
1134
1135void Playlist::addDirectory(QString dir) {
1136 addOneDirectory(dir);
1137
1138 if (recursive_add_directory) {
1139 QFileInfoList dir_list = QDir(dir).entryInfoList(QStringList() << "*", QDir::AllDirs | QDir::NoDotAndDotDot);
1140 for (int n=0; n < dir_list.count(); n++) {
1141 if (dir_list[n].isDir()) {
1142 qDebug("Playlist::addDirectory: adding directory: %s", dir_list[n].filePath().toUtf8().data());
1143 addDirectory(dir_list[n].filePath());
1144 }
1145 }
1146 }
1147}
1148
1149// Remove selected items
1150void Playlist::removeSelected() {
1151 qDebug("Playlist::removeSelected");
1152
1153 int first_selected = -1;
1154 int number_previous_item = 0;
1155
1156 for (int n=0; n < listView->rowCount(); n++) {
1157 if (listView->isSelected(n, 0)) {
1158 qDebug(" row %d selected", n);
1159 pl[n].setMarkForDeletion(true);
1160 number_previous_item++;
1161 if (first_selected == -1) first_selected = n;
1162 }
1163 }
1164
1165 PlaylistItemList::iterator it;
1166 for ( it = pl.begin(); it != pl.end(); ++it ) {
1167 if ( (*it).markedForDeletion() ) {
1168 qDebug("Remove '%s'", (*it).filename().toUtf8().data());
1169 it = pl.erase(it);
1170 it--;
1171 setModified( true );
1172 }
1173 }
1174
1175
1176 if (first_selected < current_item) {
1177 current_item -= number_previous_item;
1178 }
1179
1180 if (isEmpty()) setModified(false);
1181 updateView();
1182
1183 if (first_selected >= listView->rowCount())
1184 first_selected = listView->rowCount() - 1;
1185
1186 if ( ( first_selected > -1) && ( first_selected < listView->rowCount() ) ) {
1187 listView->clearSelection();
1188 listView->setCurrentCell( first_selected, 0);
1189 //listView->selectRow( first_selected );
1190 }
1191}
1192
1193void Playlist::removeAll() {
1194 /*
1195 pl.clear();
1196 updateView();
1197 setModified( false );
1198 */
1199 clear();
1200}
1201
1202void Playlist::clearPlayedTag() {
1203 PlaylistItemList::iterator it;
1204 for ( it = pl.begin(); it != pl.end(); ++it ) {
1205 (*it).setPlayed(false);
1206 }
1207 updateView();
1208}
1209
1210int Playlist::chooseRandomItem() {
1211 qDebug( "Playlist::chooseRandomItem");
1212 QList <int> fi; //List of not played items (free items)
1213
1214 int n=0;
1215 PlaylistItemList::iterator it;
1216 for ( it = pl.begin(); it != pl.end(); ++it ) {
1217 if (! (*it).played() ) fi.append(n);
1218 n++;
1219 }
1220
1221 qDebug(" * free items: %d", fi.count() );
1222
1223 if (fi.count()==0) return -1; // none free
1224
1225 qDebug(" * items: ");
1226 for (int i=0; i < fi.count(); i++) {
1227 qDebug(" * item: %d", fi[i]);
1228 }
1229
1230 int selected = (int) ((double) fi.count() * rand()/(RAND_MAX+1.0));
1231 qDebug(" * selected item: %d (%d)", selected, fi[selected]);
1232 return fi[selected];
1233}
1234
1235void Playlist::swapItems(int item1, int item2 ) {
1236 PlaylistItem it1 = pl[item1];
1237 pl[item1] = pl[item2];
1238 pl[item2] = it1;
1239 setModified( true );
1240}
1241
1242
1243void Playlist::upItem() {
1244 qDebug("Playlist::upItem");
1245
1246 int current = listView->currentRow();
1247 qDebug(" currentRow: %d", current );
1248
1249 moveItemUp(current);
1250
1251}
1252
1253void Playlist::downItem() {
1254 qDebug("Playlist::downItem");
1255
1256 int current = listView->currentRow();
1257 qDebug(" currentRow: %d", current );
1258
1259 moveItemDown(current);
1260}
1261
1262void Playlist::moveItemUp(int current){
1263 qDebug("Playlist::moveItemUp");
1264
1265 if (current >= 1) {
1266 swapItems( current, current-1 );
1267 if (current_item == (current-1)) current_item = current;
1268 else
1269 if (current_item == current) current_item = current-1;
1270 updateView();
1271 listView->clearSelection();
1272 listView->setCurrentCell( current-1, 0);
1273 }
1274}
1275void Playlist::moveItemDown(int current ){
1276 qDebug("Playlist::moveItemDown");
1277
1278 if ( (current > -1) && (current < (pl.count()-1)) ) {
1279 swapItems( current, current+1 );
1280 if (current_item == (current+1)) current_item = current;
1281 else
1282 if (current_item == current) current_item = current+1;
1283 updateView();
1284 listView->clearSelection();
1285 listView->setCurrentCell( current+1, 0);
1286 }
1287}
1288
1289void Playlist::editCurrentItem() {
1290 int current = listView->currentRow();
1291 if (current > -1) editItem(current);
1292}
1293
1294void Playlist::editItem(int item) {
1295 QString current_name = pl[item].name();
1296 if (current_name.isEmpty()) current_name = pl[item].filename();
1297
1298 bool ok;
1299 QString text = QInputDialog::getText( this,
1300 tr("Edit name"),
1301 tr("Type the name that will be displayed in the playlist for this file:"),
1302 QLineEdit::Normal,
1303 current_name, &ok );
1304 if ( ok && !text.isEmpty() ) {
1305 // user entered something and pressed OK
1306 pl[item].setName(text);
1307
1308 // If duration == 0 the name will be overwritten!
1309 if (pl[item].duration()<1) pl[item].setDuration(1);
1310 updateView();
1311
1312 setModified( true );
1313 }
1314}
1315
1316// Drag&drop
1317void Playlist::dragEnterEvent( QDragEnterEvent *e ) {
1318 qDebug("Playlist::dragEnterEvent");
1319
1320 if (e->mimeData()->hasUrls()) {
1321 e->acceptProposedAction();
1322 }
1323}
1324
1325void Playlist::dropEvent( QDropEvent *e ) {
1326 qDebug("Playlist::dropEvent");
1327
1328 QStringList files;
1329
1330 if (e->mimeData()->hasUrls()) {
1331 QList <QUrl> l = e->mimeData()->urls();
1332 QString s;
1333 for (int n=0; n < l.count(); n++) {
1334 if (l[n].isValid()) {
1335 qDebug("Playlist::dropEvent: scheme: '%s'", l[n].scheme().toUtf8().data());
1336 if (l[n].scheme() == "file")
1337 s = l[n].toLocalFile();
1338 else
1339 s = l[n].toString();
1340 /*
1341 qDebug(" * '%s'", l[n].toString().toUtf8().data());
1342 qDebug(" * '%s'", l[n].toLocalFile().toUtf8().data());
1343 */
1344 qDebug("Playlist::dropEvent: file: '%s'", s.toUtf8().data());
1345 files.append(s);
1346 }
1347 }
1348 }
1349
1350 #ifdef Q_OS_WIN
1351 files = Helper::resolveSymlinks(files); // Check for Windows shortcuts
1352 #endif
1353 files.sort();
1354
1355 QStringList only_files;
1356 for (int n = 0; n < files.count(); n++) {
1357 if ( QFileInfo( files[n] ).isDir() ) {
1358 addDirectory( files[n] );
1359 } else {
1360 only_files.append( files[n] );
1361 }
1362 }
1363 addFiles( only_files );
1364}
1365
1366
1367void Playlist::hideEvent( QHideEvent * ) {
1368 emit visibilityChanged(false);
1369}
1370
1371void Playlist::showEvent( QShowEvent * ) {
1372 emit visibilityChanged(true);
1373}
1374
1375void Playlist::closeEvent( QCloseEvent * e ) {
1376 saveSettings();
1377 e->accept();
1378}
1379
1380
1381void Playlist::maybeSaveSettings() {
1382 qDebug("Playlist::maybeSaveSettings");
1383 if (isModified()) saveSettings();
1384}
1385
1386void Playlist::saveSettings() {
1387 qDebug("Playlist::saveSettings");
1388
1389 QSettings * set = settings;
1390
1391 set->beginGroup( "directories");
1392 bool save_dirs = set->value("save_dirs", false).toBool();
1393 set->endGroup();
1394
1395 set->beginGroup( "playlist");
1396
1397 set->setValue( "repeat", repeatAct->isChecked() );
1398 set->setValue( "shuffle", shuffleAct->isChecked() );
1399
1400 set->setValue( "auto_get_info", automatically_get_info );
1401 set->setValue( "recursive_add_directory", recursive_add_directory );
1402 set->setValue( "save_playlist_in_config", save_playlist_in_config );
1403 set->setValue( "play_files_from_start", play_files_from_start );
1404 set->setValue( "automatically_play_next", automatically_play_next );
1405
1406 set->setValue( "row_spacing", row_spacing );
1407
1408#if !DOCK_PLAYLIST
1409 set->setValue( "size", size() );
1410#endif
1411 if (save_dirs) {
1412 set->setValue( "latest_dir", latest_dir );
1413 } else {
1414 set->setValue( "latest_dir", "" );
1415 }
1416
1417 set->endGroup();
1418
1419 if (save_playlist_in_config) {
1420 //Save current list
1421 set->beginGroup( "playlist_contents");
1422
1423 set->setValue( "count", (int) pl.count() );
1424 for ( int n=0; n < pl.count(); n++ ) {
1425 set->setValue( QString("item_%1_filename").arg(n), pl[n].filename() );
1426 set->setValue( QString("item_%1_duration").arg(n), pl[n].duration() );
1427 set->setValue( QString("item_%1_name").arg(n), pl[n].name() );
1428 }
1429 set->setValue( "current_item", current_item );
1430 set->setValue( "modified", modified );
1431
1432 set->endGroup();
1433 }
1434}
1435
1436void Playlist::loadSettings() {
1437 qDebug("Playlist::loadSettings");
1438
1439 QSettings * set = settings;
1440
1441 set->beginGroup( "playlist");
1442
1443 repeatAct->setChecked( set->value( "repeat", repeatAct->isChecked() ).toBool() );
1444 shuffleAct->setChecked( set->value( "shuffle", shuffleAct->isChecked() ).toBool() );
1445
1446 automatically_get_info = set->value( "auto_get_info", automatically_get_info ).toBool();
1447 recursive_add_directory = set->value( "recursive_add_directory", recursive_add_directory ).toBool();
1448 save_playlist_in_config = set->value( "save_playlist_in_config", save_playlist_in_config ).toBool();
1449 play_files_from_start = set->value( "play_files_from_start", play_files_from_start ).toBool();
1450 automatically_play_next = set->value( "automatically_play_next", automatically_play_next ).toBool();
1451
1452 row_spacing = set->value( "row_spacing", row_spacing ).toInt();
1453
1454#if !DOCK_PLAYLIST
1455 resize( set->value("size", size()).toSize() );
1456#endif
1457
1458 latest_dir = set->value( "latest_dir", latest_dir ).toString();
1459
1460 set->endGroup();
1461
1462 if (save_playlist_in_config) {
1463 //Load latest list
1464 set->beginGroup( "playlist_contents");
1465
1466 int count = set->value( "count", 0 ).toInt();
1467 QString filename, name;
1468 double duration;
1469 for ( int n=0; n < count; n++ ) {
1470 filename = set->value( QString("item_%1_filename").arg(n), "" ).toString();
1471 duration = set->value( QString("item_%1_duration").arg(n), -1 ).toDouble();
1472 name = set->value( QString("item_%1_name").arg(n), "" ).toString();
1473 addItem( filename, name, duration );
1474 }
1475 setCurrentItem( set->value( "current_item", -1 ).toInt() );
1476 setModified( set->value( "modified", false ).toBool() );
1477 updateView();
1478
1479 set->endGroup();
1480 }
1481}
1482
1483QString Playlist::lastDir() {
1484 QString last_dir = latest_dir;
1485 if (last_dir.isEmpty()) last_dir = pref->latest_dir;
1486 return last_dir;
1487}
1488
1489// Language change stuff
1490void Playlist::changeEvent(QEvent *e) {
1491 if (e->type() == QEvent::LanguageChange) {
1492 retranslateStrings();
1493 } else {
1494 QWidget::changeEvent(e);
1495 }
1496}
1497
1498#include "moc_playlist.cpp"
Note: See TracBrowser for help on using the repository browser.