diff --git a/src/playlist/playlistcontainer.cpp b/src/playlist/playlistcontainer.cpp index 2da98c6a1..c75c2f843 100644 --- a/src/playlist/playlistcontainer.cpp +++ b/src/playlist/playlistcontainer.cpp @@ -256,9 +256,32 @@ void PlaylistContainer::LoadPlaylist() { void PlaylistContainer::SavePlaylist(int id = -1) { QString filename = settings_.value("last_save_playlist").toString(); + + // We want to use the playlist tab name as a default filename, but in the + // same directory as the last saved file. + + // Strip off filename components until we find something that's a folder + forever { + QFileInfo fileinfo(filename); + if (filename.isEmpty() || fileinfo.isDir()) + break; + + filename = filename.section('/', 0, -2); + } + + // Use the home directory as a fallback in case the path is empty. + if (filename.isEmpty()) + filename = QDir::homePath(); + + // Add the suggested filename based on the tab name + filename += "/" + ui_->tab_bar->tabText(ui_->tab_bar->currentIndex()) + + "." + manager_->parser()->default_extension(); + + QString default_filter = manager_->parser()->default_filter(); + filename = QFileDialog::getSaveFileName( this, tr("Save playlist"), filename, - manager_->parser()->filters()); + manager_->parser()->filters(), &default_filter); if (filename.isNull()) return; diff --git a/src/playlistparsers/playlistparser.cpp b/src/playlistparsers/playlistparser.cpp index 63cd01e15..c38ec7d1c 100644 --- a/src/playlistparsers/playlistparser.cpp +++ b/src/playlistparsers/playlistparser.cpp @@ -28,8 +28,9 @@ const int PlaylistParser::kMagicSize = 512; PlaylistParser::PlaylistParser(QObject *parent) : QObject(parent) { + default_parser_ = new XSPFParser(this); parsers_ << new M3UParser(this); - parsers_ << new XSPFParser(this); + parsers_ << default_parser_; parsers_ << new PLSParser(this); parsers_ << new ASXParser(this); parsers_ << new AsxIniParser(this); @@ -50,12 +51,7 @@ QString PlaylistParser::filters() const { QStringList filters; QStringList all_extensions; foreach (ParserBase* parser, parsers_) { - QStringList extensions; - foreach (const QString& extension, parser->file_extensions()) - extensions << "*." + extension; - all_extensions << extensions; - - filters << tr("%1 playlists (%2)").arg(parser->name(), extensions.join(" ")); + filters << FilterForParser(parser, &all_extensions); } filters.prepend(tr("All playlists (%1)").arg(all_extensions.join(" "))); @@ -63,6 +59,25 @@ QString PlaylistParser::filters() const { return filters.join(";;"); } +QString PlaylistParser::FilterForParser(const ParserBase *parser, QStringList *all_extensions) const { + QStringList extensions; + foreach (const QString& extension, parser->file_extensions()) + extensions << "*." + extension; + + if (all_extensions) + *all_extensions << extensions; + + return tr("%1 playlists (%2)").arg(parser->name(), extensions.join(" ")); +} + +QString PlaylistParser::default_extension() const { + return default_parser_->file_extensions()[0]; +} + +QString PlaylistParser::default_filter() const { + return FilterForParser(default_parser_); +} + ParserBase* PlaylistParser::ParserForExtension(const QString& suffix) const { foreach (ParserBase* p, parsers_) { if (p->file_extensions().contains(suffix)) diff --git a/src/playlistparsers/playlistparser.h b/src/playlistparsers/playlistparser.h index a04a663ab..ad55c8dcc 100644 --- a/src/playlistparsers/playlistparser.h +++ b/src/playlistparsers/playlistparser.h @@ -34,6 +34,9 @@ public: QStringList file_extensions() const; QString filters() const; + QString default_extension() const; + QString default_filter() const; + ParserBase* MaybeGetParserForMagic(const QByteArray& data, const QString& mime_type = QString()) const; ParserBase* ParserForExtension(const QString& suffix) const; @@ -41,8 +44,13 @@ public: SongList Load(const QString& filename, ParserBase* parser = 0) const; void Save(const SongList& songs, const QString& filename) const; +private: + QString FilterForParser(const ParserBase* parser, + QStringList* all_extensions = NULL) const; + private: QList parsers_; + ParserBase* default_parser_; }; #endif // PLAYLISTPARSER_H