File indexing completed on 2024-05-05 04:47:22

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016  
0017 #include "NavigationUrlGenerator.h"
0018 
0019 #include "MainWindow.h"
0020 #include "amarokconfig.h"
0021 #include "amarokurls/AmarokUrl.h"
0022 #include "amarokurls/AmarokUrlHandler.h"
0023 #include "browsers/BrowserDock.h"
0024 #include "browsers/CollectionTreeItemModelBase.h"
0025 #include "browsers/collectionbrowser/CollectionWidget.h"
0026 #include "browsers/filebrowser/FileBrowser.h"
0027 #include "browsers/playlistbrowser/PlaylistBrowser.h"
0028 #include "browsers/servicebrowser/ServiceBrowser.h"
0029 #include "core/support/Debug.h"
0030 #include "core/capabilities/SourceInfoCapability.h"
0031 #include "core-impl/collections/db/sql/SqlMeta.h"
0032 #include "playlistmanager/PlaylistManager.h"
0033 
0034 NavigationUrlGenerator * NavigationUrlGenerator::s_instance = nullptr;
0035 
0036 NavigationUrlGenerator * NavigationUrlGenerator::instance()
0037 {
0038     if( s_instance == nullptr )
0039         s_instance = new NavigationUrlGenerator();
0040 
0041     return s_instance;
0042 }
0043 
0044 NavigationUrlGenerator::NavigationUrlGenerator()
0045 {
0046 }
0047 
0048 NavigationUrlGenerator::~NavigationUrlGenerator()
0049 {
0050     The::amarokUrlHandler()->unRegisterGenerator( this );
0051 }
0052 
0053 AmarokUrl NavigationUrlGenerator::CreateAmarokUrl()
0054 {
0055     DEBUG_BLOCK
0056 
0057     AmarokUrl url;
0058     url.setCommand( QStringLiteral("navigate") );
0059 
0060     //get the path
0061     QString path = The::mainWindow()->browserDock()->list()->path();
0062 
0063     QStringList pathParts = path.split( QLatin1Char('/') );
0064 
0065     //we don't use the "Home" part in navigation urls
0066     if ( pathParts.at( 0 ) == QLatin1String("root list") )
0067         pathParts.removeFirst();
0068     
0069     url.setPath( pathParts.join( QLatin1Char('/') ) );
0070 
0071 
0072     QString filter = The::mainWindow()->browserDock()->list()->activeCategoryRecursive()->filter();
0073 
0074     if ( !filter.isEmpty() )
0075         url.setArg( QStringLiteral("filter"), filter );
0076 
0077     QList<CategoryId::CatMenuId> levels = The::mainWindow()->browserDock()->list()->activeCategoryRecursive()->levels();
0078     QString sortMode;
0079 
0080     foreach( CategoryId::CatMenuId level, levels ) {
0081         switch( level ) {
0082             case CategoryId::Genre:
0083                 sortMode += QLatin1String("genre-");
0084                 break;
0085             case CategoryId::Artist:
0086                 sortMode += QLatin1String("artist-");
0087                 break;
0088             case CategoryId::Album:
0089                 sortMode += QLatin1String("album-");
0090                 break;
0091             case CategoryId::AlbumArtist:
0092                 sortMode += QLatin1String("albumartist-");
0093                 break;
0094             case CategoryId::Composer:
0095                 sortMode += QLatin1String("composer-");
0096                 break;
0097             case CategoryId::Year:
0098                 sortMode += QLatin1String("year-");
0099                 break;
0100             default:
0101                 break;
0102         }
0103     }
0104 
0105     //we have left a trailing '-' in there, get rid of it!
0106     if ( sortMode.size() > 0 )
0107         sortMode = sortMode.left( sortMode.size() - 1 );
0108     
0109     if ( !sortMode.isEmpty() )
0110         url.setArg( QStringLiteral("levels"), sortMode );
0111 
0112 
0113     //if in the local collection view, also store "show covers" and "show years"
0114     if( url.path().endsWith( QLatin1String("collections"), Qt::CaseInsensitive ) )
0115     {
0116         debug() << "bookmarking in local collection";
0117 
0118         if( AmarokConfig::showAlbumArt() )
0119             url.setArg( QStringLiteral("show_cover"), QStringLiteral("true") );
0120         else
0121             url.setArg( QStringLiteral("show_cover"), QStringLiteral("false") );
0122 
0123         if(  AmarokConfig::showYears() )
0124             url.setArg( QStringLiteral("show_years"), QStringLiteral("true") );
0125         else
0126             url.setArg( QStringLiteral("show_years"), QStringLiteral("false") );
0127     }
0128 
0129     //come up with a default name for this url..
0130     QString name = The::mainWindow()->browserDock()->list()->activeCategoryRecursive()->prettyName();
0131 
0132     //if in the file browser, also store the file path
0133     if( url.path().endsWith( QLatin1String("files"), Qt::CaseInsensitive ) )
0134     {
0135 
0136         //Give a proper name since it will return "/" as that is what is used in the breadcrumb.
0137         name = i18n( "Files" );
0138 
0139         FileBrowser * fileBrowser = dynamic_cast<FileBrowser *>( The::mainWindow()->browserDock()->list()->activeCategory() );
0140         if( fileBrowser )
0141         {
0142             url.setArg( QStringLiteral("path"), fileBrowser->currentDir() );
0143             name = i18n( "Files (%1)", fileBrowser->currentDir() );
0144         }
0145     }
0146 
0147     url.setName( name );
0148     
0149     return url;
0150 
0151 }
0152 
0153 AmarokUrl NavigationUrlGenerator::urlFromAlbum( Meta::AlbumPtr album )
0154 {
0155     AmarokUrl url;
0156 
0157     QScopedPointer<Capabilities::BookmarkThisCapability> btc( album->create<Capabilities::BookmarkThisCapability>() );
0158     if( btc )
0159     {
0160         if( btc->isBookmarkable() ) {
0161 
0162             QString albumName = album->prettyName();
0163 
0164             url.setCommand( QStringLiteral("navigate") );
0165 
0166             QString path = btc->browserName();
0167             if ( !btc->collectionName().isEmpty() )
0168                 path += ( '/' + btc->collectionName() );
0169             url.setPath( path );
0170 
0171             QString filter;
0172             if ( btc->simpleFiltering() ) {
0173                 filter = "\"" + albumName + "\"";
0174             }
0175             else
0176             {
0177                 url.setArg( QStringLiteral("levels"), QStringLiteral("album") );
0178 
0179                 QString artistName;
0180                 if ( album->albumArtist() )
0181                     artistName = album->albumArtist()->prettyName();
0182 
0183                 filter = "album:\"" + albumName + "\"";
0184                 if ( !artistName.isEmpty() )
0185                     filter += ( " AND artist:\"" + artistName + "\"" );
0186             }
0187 
0188             url.setArg( QStringLiteral("filter"), filter );
0189 
0190             if ( !btc->collectionName().isEmpty() )
0191                 url.setName( i18n( "Album \"%1\" from %2", albumName, btc->collectionName() ) );
0192             else
0193                 url.setName( i18n( "Album \"%1\"", albumName ) );
0194 
0195         }
0196     }
0197 
0198     //debug() << "got url: " << url.url();
0199     return url;
0200 }
0201 
0202 AmarokUrl NavigationUrlGenerator::urlFromArtist( Meta::ArtistPtr artist )
0203 {
0204     DEBUG_BLOCK
0205 
0206     AmarokUrl url;
0207 
0208     QScopedPointer<Capabilities::BookmarkThisCapability> btc( artist->create<Capabilities::BookmarkThisCapability>() );
0209     if( btc )
0210     {
0211         if( btc->isBookmarkable() ) {
0212 
0213             QString artistName = artist->prettyName();
0214 
0215             url.setCommand( QStringLiteral("navigate") );
0216             
0217             QString path = btc->browserName();
0218             if ( !btc->collectionName().isEmpty() )
0219                 path += ( '/' + btc->collectionName() );
0220             url.setPath( path );
0221 
0222             //debug() << "Path: " << url.path();
0223 
0224             QString filter;
0225             if ( btc->simpleFiltering() ) {
0226                 //for services only supporting simple filtering, do not try to set the sorting mode
0227                 filter = "\"" + artistName + "\"";
0228             }
0229             else
0230             {
0231                 url.setArg( QStringLiteral("levels"), QStringLiteral("artist-album") );
0232                 filter = ( "artist:\"" + artistName + "\"" );
0233             }
0234 
0235             url.setArg( QStringLiteral("filter"), filter );
0236 
0237             if ( !btc->collectionName().isEmpty() )
0238                 url.setName( i18n( "Artist \"%1\" from %2", artistName, btc->collectionName() ) );
0239             else
0240                 url.setName( i18n( "Artist \"%1\"", artistName ) );
0241 
0242         }
0243     }
0244 
0245     return url;
0246 
0247 }
0248 
0249 QString
0250 NavigationUrlGenerator::description()
0251 {
0252     return i18n( "Bookmark Media Sources View" );
0253 }
0254 
0255 QIcon NavigationUrlGenerator::icon()
0256 {
0257     return QIcon::fromTheme( QStringLiteral("flag-amarok") );
0258 }
0259 
0260 AmarokUrl
0261 NavigationUrlGenerator::createUrl()
0262 {
0263     return CreateAmarokUrl();
0264 }
0265