--- LOAD PLAYLIST --- Clear current playlist and load new playlist contents.
| 1582 | // --- LOAD PLAYLIST --- |
| 1583 | // Clear current playlist and load new playlist contents. |
| 1584 | void MainWindow::loadPlaylist() { |
| 1585 | // Select playlist from filesystem, parse and replace old playlist view. |
| 1586 | // Use stored directory location, if any. |
| 1587 | QSettings settings; |
| 1588 | QString dir = settings.value("openPlaylistDir").toString(); |
| 1589 | QString filename = QFileDialog::getOpenFileName(this, tr("Open playlist"), dir, tr("Playlists (*.m3u *.m3u8)")); |
| 1590 | if (filename.isEmpty()) { return; } |
| 1591 | |
| 1592 | // Update current folder. |
| 1593 | settings.setValue("openPlaylistDir", filename); |
| 1594 | |
| 1595 | // Parse file. Discard Extended M3U (#-prefixed) lines for now. |
| 1596 | QFile playlist; |
| 1597 | playlist.setFileName(filename); |
| 1598 | if (!playlist.exists()) { |
| 1599 | QMessageBox::warning(this, tr("Failed to open file"), tr("The selected file could not be opened.")); |
| 1600 | return; |
| 1601 | } |
| 1602 | |
| 1603 | playlist.open(QIODevice::ReadOnly); |
| 1604 | QTextStream textStream(&playlist); |
| 1605 | textStream.setCodec("UTF-8"); |
| 1606 | ui->mediaListWidget->clear(); |
| 1607 | |
| 1608 | // Overwrite local playlist with the new playlist too. |
| 1609 | QFile filepaths; |
| 1610 | filepaths.setFileName(appDataLocation + "/filepaths.conf"); |
| 1611 | filepaths.open(QIODevice::WriteOnly | QIODevice::Truncate); |
| 1612 | QTextStream outStream(&filepaths); |
| 1613 | outStream.setCodec("UTF-8"); |
| 1614 | |
| 1615 | QString line; |
| 1616 | bool clean = true; |
| 1617 | while (!(line = textStream.readLine()).isNull()) { |
| 1618 | if (line.startsWith("#")) { continue; } |
| 1619 | |
| 1620 | // We should have a file path now. |
| 1621 | QFileInfo finf(line); |
| 1622 | if (!finf.isFile()) { clean = false; continue; } |
| 1623 | |
| 1624 | QListWidgetItem *newItem = new QListWidgetItem; |
| 1625 | newItem->setText(finf.fileName()); |
| 1626 | newItem->setData(Qt::UserRole, QVariant(line)); |
| 1627 | ui->mediaListWidget->addItem(newItem); |
| 1628 | |
| 1629 | outStream << line << "\n"; |
| 1630 | } |
| 1631 | |
| 1632 | if (!clean) { |
| 1633 | QMessageBox::warning(this, tr("Playlist issue"), |
| 1634 | tr("There was an issue with some of the playlist entries")); |
| 1635 | } |
| 1636 | |
| 1637 | playlist.close(); |
| 1638 | filepaths.close(); |
| 1639 | } |
| 1640 | |
| 1641 |