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

Last change on this file since 163 was 163, checked in by Silvan Scherrer, 12 years ago

SMPlayer: update vendor to 0.8.7

File size: 37.5 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 name = core->mdat.clip_name;
975 QString artist = core->mdat.clip_artist;
976
977 #if defined(Q_OS_WIN) || defined(Q_OS_OS2)
978 filename = Helper::changeSlashes(filename);
979 #endif
980
981 if (name.isEmpty()) {
982 QFileInfo fi(filename);
983 if (fi.exists()) {
984 // Local file
985 name = fi.fileName();
986 } else {
987 // Stream
988 name = filename;
989 }
990 }
991 if (!artist.isEmpty()) name = artist + " - " + name;
992
993 int pos=0;
994 PlaylistItemList::iterator it;
995 for ( it = pl.begin(); it != pl.end(); ++it ) {
996 if ( (*it).filename() == filename ) {
997 if ((*it).duration()<1) {
998 if (!name.isEmpty()) {
999 (*it).setName(name);
1000 }
1001 (*it).setDuration(duration);
1002 //setModified( true );
1003 }
1004 else
1005 // Edited name (sets duration to 1)
1006 if ((*it).duration()==1) {
1007 (*it).setDuration(duration);
1008 //setModified( true );
1009 }
1010 setCurrentItem(pos);
1011 }
1012 pos++;
1013 }
1014 updateView();
1015}
1016
1017// Add current file to playlist
1018void Playlist::addCurrentFile() {
1019 qDebug("Playlist::addCurrentFile");
1020 if (!core->mdat.filename.isEmpty()) {
1021 addItem( core->mdat.filename, "", 0 );
1022 getMediaInfo();
1023 }
1024}
1025
1026void Playlist::addFiles() {
1027 Extensions e;
1028 QStringList files = MyFileDialog::getOpenFileNames(
1029 this, tr("Select one or more files to open"),
1030 lastDir(),
1031 tr("Multimedia") + e.multimedia().forFilter() + ";;" +
1032 tr("All files") +" (*.*)" );
1033
1034 if (files.count()!=0) addFiles(files);
1035}
1036
1037void Playlist::addFiles(QStringList files, AutoGetInfo auto_get_info) {
1038 qDebug("Playlist::addFiles");
1039
1040#if USE_INFOPROVIDER
1041 bool get_info = (auto_get_info == GetInfo);
1042 if (auto_get_info == UserDefined) {
1043 get_info = automatically_get_info;
1044 }
1045
1046 MediaData data;
1047 setCursor(Qt::WaitCursor);
1048#endif
1049
1050 QStringList::Iterator it = files.begin();
1051 while( it != files.end() ) {
1052#if USE_INFOPROVIDER
1053 if ( (get_info) && (QFile::exists((*it))) ) {
1054 data = InfoProvider::getInfo( (*it) );
1055 addItem( (*it), data.displayName(), data.duration );
1056 //updateView();
1057 //qApp->processEvents();
1058 } else {
1059 addItem( (*it), "", 0 );
1060 }
1061#else
1062 addItem( (*it), "", 0 );
1063#endif
1064
1065 if (QFile::exists(*it)) {
1066 latest_dir = QFileInfo((*it)).absolutePath();
1067 }
1068
1069 ++it;
1070 }
1071#if USE_INFOPROVIDER
1072 unsetCursor();
1073#endif
1074 updateView();
1075
1076 qDebug( "Playlist::addFiles: latest_dir: '%s'", latest_dir.toUtf8().constData() );
1077}
1078
1079void Playlist::addFile(QString file, AutoGetInfo auto_get_info) {
1080 addFiles( QStringList() << file, auto_get_info );
1081}
1082
1083void Playlist::addDirectory() {
1084 QString s = MyFileDialog::getExistingDirectory(
1085 this, tr("Choose a directory"),
1086 lastDir() );
1087
1088 if (!s.isEmpty()) {
1089 addDirectory(s);
1090 latest_dir = s;
1091 }
1092}
1093
1094void Playlist::addUrls() {
1095 MultilineInputDialog d(this);
1096 if (d.exec() == QDialog::Accepted) {
1097 QStringList urls = d.lines();
1098 foreach(QString u, urls) {
1099 if (!u.isEmpty()) addItem( u, "", 0 );
1100 }
1101 updateView();
1102 }
1103}
1104
1105void Playlist::addOneDirectory(QString dir) {
1106 QStringList filelist;
1107
1108 Extensions e;
1109 QRegExp rx_ext(e.multimedia().forRegExp());
1110 rx_ext.setCaseSensitivity(Qt::CaseInsensitive);
1111
1112 QStringList dir_list = QDir(dir).entryList();
1113
1114 QString filename;
1115 QStringList::Iterator it = dir_list.begin();
1116 while( it != dir_list.end() ) {
1117 filename = dir;
1118 if (filename.right(1)!="/") filename += "/";
1119 filename += (*it);
1120 QFileInfo fi(filename);
1121 if (!fi.isDir()) {
1122 if (rx_ext.indexIn(fi.suffix()) > -1) {
1123 filelist << filename;
1124 }
1125 }
1126 ++it;
1127 }
1128 addFiles(filelist);
1129}
1130
1131void Playlist::addDirectory(QString dir) {
1132 addOneDirectory(dir);
1133
1134 if (recursive_add_directory) {
1135 QFileInfoList dir_list = QDir(dir).entryInfoList(QStringList() << "*", QDir::AllDirs | QDir::NoDotAndDotDot);
1136 for (int n=0; n < dir_list.count(); n++) {
1137 if (dir_list[n].isDir()) {
1138 qDebug("Playlist::addDirectory: adding directory: %s", dir_list[n].filePath().toUtf8().data());
1139 addDirectory(dir_list[n].filePath());
1140 }
1141 }
1142 }
1143}
1144
1145// Remove selected items
1146void Playlist::removeSelected() {
1147 qDebug("Playlist::removeSelected");
1148
1149 int first_selected = -1;
1150 int number_previous_item = 0;
1151
1152 for (int n=0; n < listView->rowCount(); n++) {
1153 if (listView->isSelected(n, 0)) {
1154 qDebug(" row %d selected", n);
1155 pl[n].setMarkForDeletion(true);
1156 number_previous_item++;
1157 if (first_selected == -1) first_selected = n;
1158 }
1159 }
1160
1161 PlaylistItemList::iterator it;
1162 for ( it = pl.begin(); it != pl.end(); ++it ) {
1163 if ( (*it).markedForDeletion() ) {
1164 qDebug("Remove '%s'", (*it).filename().toUtf8().data());
1165 it = pl.erase(it);
1166 it--;
1167 setModified( true );
1168 }
1169 }
1170
1171
1172 if (first_selected < current_item) {
1173 current_item -= number_previous_item;
1174 }
1175
1176 if (isEmpty()) setModified(false);
1177 updateView();
1178
1179 if (first_selected >= listView->rowCount())
1180 first_selected = listView->rowCount() - 1;
1181
1182 if ( ( first_selected > -1) && ( first_selected < listView->rowCount() ) ) {
1183 listView->clearSelection();
1184 listView->setCurrentCell( first_selected, 0);
1185 //listView->selectRow( first_selected );
1186 }
1187}
1188
1189void Playlist::removeAll() {
1190 /*
1191 pl.clear();
1192 updateView();
1193 setModified( false );
1194 */
1195 clear();
1196}
1197
1198void Playlist::clearPlayedTag() {
1199 PlaylistItemList::iterator it;
1200 for ( it = pl.begin(); it != pl.end(); ++it ) {
1201 (*it).setPlayed(false);
1202 }
1203 updateView();
1204}
1205
1206int Playlist::chooseRandomItem() {
1207 qDebug( "Playlist::chooseRandomItem");
1208 QList <int> fi; //List of not played items (free items)
1209
1210 int n=0;
1211 PlaylistItemList::iterator it;
1212 for ( it = pl.begin(); it != pl.end(); ++it ) {
1213 if (! (*it).played() ) fi.append(n);
1214 n++;
1215 }
1216
1217 qDebug(" * free items: %d", fi.count() );
1218
1219 if (fi.count()==0) return -1; // none free
1220
1221 qDebug(" * items: ");
1222 for (int i=0; i < fi.count(); i++) {
1223 qDebug(" * item: %d", fi[i]);
1224 }
1225
1226 int selected = (int) ((double) fi.count() * rand()/(RAND_MAX+1.0));
1227 qDebug(" * selected item: %d (%d)", selected, fi[selected]);
1228 return fi[selected];
1229}
1230
1231void Playlist::swapItems(int item1, int item2 ) {
1232 PlaylistItem it1 = pl[item1];
1233 pl[item1] = pl[item2];
1234 pl[item2] = it1;
1235 setModified( true );
1236}
1237
1238
1239void Playlist::upItem() {
1240 qDebug("Playlist::upItem");
1241
1242 int current = listView->currentRow();
1243 qDebug(" currentRow: %d", current );
1244
1245 moveItemUp(current);
1246
1247}
1248
1249void Playlist::downItem() {
1250 qDebug("Playlist::downItem");
1251
1252 int current = listView->currentRow();
1253 qDebug(" currentRow: %d", current );
1254
1255 moveItemDown(current);
1256}
1257
1258void Playlist::moveItemUp(int current){
1259 qDebug("Playlist::moveItemUp");
1260
1261 if (current >= 1) {
1262 swapItems( current, current-1 );
1263 if (current_item == (current-1)) current_item = current;
1264 else
1265 if (current_item == current) current_item = current-1;
1266 updateView();
1267 listView->clearSelection();
1268 listView->setCurrentCell( current-1, 0);
1269 }
1270}
1271void Playlist::moveItemDown(int current ){
1272 qDebug("Playlist::moveItemDown");
1273
1274 if ( (current > -1) && (current < (pl.count()-1)) ) {
1275 swapItems( current, current+1 );
1276 if (current_item == (current+1)) current_item = current;
1277 else
1278 if (current_item == current) current_item = current+1;
1279 updateView();
1280 listView->clearSelection();
1281 listView->setCurrentCell( current+1, 0);
1282 }
1283}
1284
1285void Playlist::editCurrentItem() {
1286 int current = listView->currentRow();
1287 if (current > -1) editItem(current);
1288}
1289
1290void Playlist::editItem(int item) {
1291 QString current_name = pl[item].name();
1292 if (current_name.isEmpty()) current_name = pl[item].filename();
1293
1294 bool ok;
1295 QString text = QInputDialog::getText( this,
1296 tr("Edit name"),
1297 tr("Type the name that will be displayed in the playlist for this file:"),
1298 QLineEdit::Normal,
1299 current_name, &ok );
1300 if ( ok && !text.isEmpty() ) {
1301 // user entered something and pressed OK
1302 pl[item].setName(text);
1303
1304 // If duration == 0 the name will be overwritten!
1305 if (pl[item].duration()<1) pl[item].setDuration(1);
1306 updateView();
1307
1308 setModified( true );
1309 }
1310}
1311
1312// Drag&drop
1313void Playlist::dragEnterEvent( QDragEnterEvent *e ) {
1314 qDebug("Playlist::dragEnterEvent");
1315
1316 if (e->mimeData()->hasUrls()) {
1317 e->acceptProposedAction();
1318 }
1319}
1320
1321void Playlist::dropEvent( QDropEvent *e ) {
1322 qDebug("Playlist::dropEvent");
1323
1324 QStringList files;
1325
1326 if (e->mimeData()->hasUrls()) {
1327 QList <QUrl> l = e->mimeData()->urls();
1328 QString s;
1329 for (int n=0; n < l.count(); n++) {
1330 if (l[n].isValid()) {
1331 qDebug("Playlist::dropEvent: scheme: '%s'", l[n].scheme().toUtf8().data());
1332 if (l[n].scheme() == "file")
1333 s = l[n].toLocalFile();
1334 else
1335 s = l[n].toString();
1336 /*
1337 qDebug(" * '%s'", l[n].toString().toUtf8().data());
1338 qDebug(" * '%s'", l[n].toLocalFile().toUtf8().data());
1339 */
1340 qDebug("Playlist::dropEvent: file: '%s'", s.toUtf8().data());
1341 files.append(s);
1342 }
1343 }
1344 }
1345
1346 #ifdef Q_OS_WIN
1347 files = Helper::resolveSymlinks(files); // Check for Windows shortcuts
1348 #endif
1349
1350 QStringList only_files;
1351 for (int n = 0; n < files.count(); n++) {
1352 if ( QFileInfo( files[n] ).isDir() ) {
1353 addDirectory( files[n] );
1354 } else {
1355 only_files.append( files[n] );
1356 }
1357 }
1358 addFiles( only_files );
1359}
1360
1361
1362void Playlist::hideEvent( QHideEvent * ) {
1363 emit visibilityChanged(false);
1364}
1365
1366void Playlist::showEvent( QShowEvent * ) {
1367 emit visibilityChanged(true);
1368}
1369
1370void Playlist::closeEvent( QCloseEvent * e ) {
1371 saveSettings();
1372 e->accept();
1373}
1374
1375
1376void Playlist::maybeSaveSettings() {
1377 qDebug("Playlist::maybeSaveSettings");
1378 if (isModified()) saveSettings();
1379}
1380
1381void Playlist::saveSettings() {
1382 qDebug("Playlist::saveSettings");
1383
1384 QSettings * set = settings;
1385
1386 set->beginGroup( "directories");
1387 bool save_dirs = set->value("save_dirs", false).toBool();
1388 set->endGroup();
1389
1390 set->beginGroup( "playlist");
1391
1392 set->setValue( "repeat", repeatAct->isChecked() );
1393 set->setValue( "shuffle", shuffleAct->isChecked() );
1394
1395 set->setValue( "auto_get_info", automatically_get_info );
1396 set->setValue( "recursive_add_directory", recursive_add_directory );
1397 set->setValue( "save_playlist_in_config", save_playlist_in_config );
1398 set->setValue( "play_files_from_start", play_files_from_start );
1399 set->setValue( "automatically_play_next", automatically_play_next );
1400
1401 set->setValue( "row_spacing", row_spacing );
1402
1403#if !DOCK_PLAYLIST
1404 set->setValue( "size", size() );
1405#endif
1406 if (save_dirs) {
1407 set->setValue( "latest_dir", latest_dir );
1408 } else {
1409 set->setValue( "latest_dir", "" );
1410 }
1411
1412 set->endGroup();
1413
1414 if (save_playlist_in_config) {
1415 //Save current list
1416 set->beginGroup( "playlist_contents");
1417
1418 set->setValue( "count", (int) pl.count() );
1419 for ( int n=0; n < pl.count(); n++ ) {
1420 set->setValue( QString("item_%1_filename").arg(n), pl[n].filename() );
1421 set->setValue( QString("item_%1_duration").arg(n), pl[n].duration() );
1422 set->setValue( QString("item_%1_name").arg(n), pl[n].name() );
1423 }
1424 set->setValue( "current_item", current_item );
1425 set->setValue( "modified", modified );
1426
1427 set->endGroup();
1428 }
1429}
1430
1431void Playlist::loadSettings() {
1432 qDebug("Playlist::loadSettings");
1433
1434 QSettings * set = settings;
1435
1436 set->beginGroup( "playlist");
1437
1438 repeatAct->setChecked( set->value( "repeat", repeatAct->isChecked() ).toBool() );
1439 shuffleAct->setChecked( set->value( "shuffle", shuffleAct->isChecked() ).toBool() );
1440
1441 automatically_get_info = set->value( "auto_get_info", automatically_get_info ).toBool();
1442 recursive_add_directory = set->value( "recursive_add_directory", recursive_add_directory ).toBool();
1443 save_playlist_in_config = set->value( "save_playlist_in_config", save_playlist_in_config ).toBool();
1444 play_files_from_start = set->value( "play_files_from_start", play_files_from_start ).toBool();
1445 automatically_play_next = set->value( "automatically_play_next", automatically_play_next ).toBool();
1446
1447 row_spacing = set->value( "row_spacing", row_spacing ).toInt();
1448
1449#if !DOCK_PLAYLIST
1450 resize( set->value("size", size()).toSize() );
1451#endif
1452
1453 latest_dir = set->value( "latest_dir", latest_dir ).toString();
1454
1455 set->endGroup();
1456
1457 if (save_playlist_in_config) {
1458 //Load latest list
1459 set->beginGroup( "playlist_contents");
1460
1461 int count = set->value( "count", 0 ).toInt();
1462 QString filename, name;
1463 double duration;
1464 for ( int n=0; n < count; n++ ) {
1465 filename = set->value( QString("item_%1_filename").arg(n), "" ).toString();
1466 duration = set->value( QString("item_%1_duration").arg(n), -1 ).toDouble();
1467 name = set->value( QString("item_%1_name").arg(n), "" ).toString();
1468 addItem( filename, name, duration );
1469 }
1470 setCurrentItem( set->value( "current_item", -1 ).toInt() );
1471 setModified( set->value( "modified", false ).toBool() );
1472 updateView();
1473
1474 set->endGroup();
1475 }
1476}
1477
1478QString Playlist::lastDir() {
1479 QString last_dir = latest_dir;
1480 if (last_dir.isEmpty()) last_dir = pref->latest_dir;
1481 return last_dir;
1482}
1483
1484// Language change stuff
1485void Playlist::changeEvent(QEvent *e) {
1486 if (e->type() == QEvent::LanguageChange) {
1487 retranslateStrings();
1488 } else {
1489 QWidget::changeEvent(e);
1490 }
1491}
1492
1493#include "moc_playlist.cpp"
Note: See TracBrowser for help on using the repository browser.