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

0001 /****************************************************************************************
0002  * Copyright (c) 2009 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 "AmarokUrl.h"
0018 
0019 #include "AmarokUrlHandler.h"
0020 #include "BookmarkGroup.h"
0021 #include "core-impl/storage/StorageManager.h"
0022 #include "core/support/Debug.h"
0023 #include <core/storage/SqlStorage.h>
0024 
0025 #include <QUrl>
0026 #include <QUrlQuery>
0027 
0028 AmarokUrl::AmarokUrl()
0029     : m_id( -1 )
0030     , m_parent( nullptr )
0031 {}
0032 
0033 AmarokUrl::AmarokUrl( const QString & urlString, const BookmarkGroupPtr &parent )
0034     : m_id( -1 )
0035     , m_parent( parent )
0036 {
0037     initFromString( urlString );
0038 }
0039 
0040 AmarokUrl::AmarokUrl( const QStringList & resultRow, const BookmarkGroupPtr &parent )
0041     : m_parent( parent )
0042 {
0043     m_id = resultRow[0].toInt();
0044     m_name = resultRow[2];
0045     const QString urlString = resultRow[3];
0046     m_description = resultRow[4];
0047     m_customValue = resultRow[5];
0048 
0049     initFromString( urlString );
0050 }
0051 
0052 AmarokUrl::~AmarokUrl()
0053 {}
0054 
0055 void AmarokUrl::initFromString( const QString & urlString )
0056 {
0057     //first, strip amarok://
0058     QString strippedUrlString = urlString;
0059     strippedUrlString = strippedUrlString.replace( QLatin1String("amarok://"), QLatin1String("") );
0060 
0061     //separate path from arguments
0062     QStringList parts = strippedUrlString.split( QLatin1Char('?') );
0063 
0064     QString commandAndPath = parts.at( 0 );
0065 
0066     QString argumentsString;
0067     if ( parts.size() == 2 )
0068         argumentsString = parts.at( 1 );
0069 
0070     if ( !argumentsString.isEmpty() )
0071     {
0072         parts = argumentsString.split( '&' );
0073         
0074         foreach( const QString &argument, parts )
0075         {
0076             
0077             QStringList argParts = argument.split( '=' );
0078             debug() << "argument: " << argument << " unescaped: " << unescape( argParts.at( 1 ) );
0079             setArg( argParts.at( 0 ), unescape( argParts.at( 1 ) ) );
0080         }
0081     }
0082 
0083     //get the command
0084 
0085     parts = commandAndPath.split( QLatin1Char('/') );
0086     m_command = parts.takeFirst();
0087 
0088     m_path = parts.join( QLatin1Char('/') );
0089 
0090     m_path = unescape( m_path );
0091 }
0092 
0093 void AmarokUrl::setCommand( const QString & command )
0094 {
0095     m_command = command;
0096 }
0097 
0098 QString AmarokUrl::command() const
0099 {
0100         return m_command;
0101 }
0102 
0103 QString
0104 AmarokUrl::prettyCommand() const
0105 {
0106     return The::amarokUrlHandler()->prettyCommand( command() );
0107 }
0108 
0109 QMap<QString, QString> AmarokUrl::args() const
0110 {
0111     return m_arguments;
0112 }
0113 
0114 void AmarokUrl::setArg( const QString &name, const QString &value )
0115 {
0116     m_arguments.insert( name, value );
0117 }
0118 
0119 bool AmarokUrl::run()
0120 {
0121     DEBUG_BLOCK
0122     return The::amarokUrlHandler()->run( *this );
0123 }
0124 
0125 QString AmarokUrl::url() const
0126 {
0127     QUrl url;
0128     url.setScheme( QStringLiteral("amarok") );
0129     url.setHost( m_command );
0130     url.setPath( '/' + m_path ); // the path must begin by /
0131     QUrlQuery query;
0132 
0133     foreach( const QString &argName, m_arguments.keys() )
0134         query.addQueryItem( argName, m_arguments[argName] );
0135 
0136     url.setQuery( query );
0137     return url.toEncoded();
0138 }
0139 
0140 bool AmarokUrl::saveToDb()
0141 {
0142     DEBUG_BLOCK
0143 
0144     if ( isNull() )
0145         return false;
0146 
0147     const int parentId = m_parent ? m_parent->id() : -1;
0148 
0149     auto sql =  StorageManager::instance()->sqlStorage();
0150 
0151     if( m_id != -1 )
0152     {
0153         //update existing
0154         debug() << "Updating bookmark";
0155         QString query = QStringLiteral("UPDATE bookmarks SET parent_id=%1, name='%2', url='%3', description='%4', custom='%5' WHERE id=%6;");
0156         query = query.arg( QString::number( parentId ), sql->escape( m_name ), sql->escape( url() ),
0157                            sql->escape( m_description ), sql->escape( m_customValue ), QString::number( m_id ) );
0158         StorageManager::instance()->sqlStorage()->query( query );
0159     }
0160     else
0161     {
0162         //insert new
0163         debug() << "Creating new bookmark in the db";
0164         QString query = QStringLiteral("INSERT INTO bookmarks ( parent_id, name, url, description, custom ) VALUES ( %1, '%2', '%3', '%4', '%5' );");
0165         query = query.arg( QString::number( parentId ), sql->escape( m_name ), sql->escape( url() ),
0166                            sql->escape( m_description ), sql->escape( m_customValue ) );
0167         m_id = StorageManager::instance()->sqlStorage()->insert( query, nullptr );
0168     }
0169 
0170     return true;
0171 }
0172 
0173 void AmarokUrl::setName( const QString & name )
0174 {
0175     m_name = name;
0176 }
0177 
0178 QString AmarokUrl::name() const
0179 {
0180     return m_name;
0181 }
0182 
0183 void AmarokUrl::setDescription( const QString & description )
0184 {
0185     m_description = description;
0186 }
0187 
0188 QString AmarokUrl::description() const
0189 {
0190     return m_description;
0191 }
0192 
0193 void AmarokUrl::removeFromDb()
0194 {
0195     QString query = QStringLiteral("DELETE FROM bookmarks WHERE id=%1");
0196     query = query.arg( QString::number( m_id ) );
0197     StorageManager::instance()->sqlStorage()->query( query );
0198 }
0199 
0200 void AmarokUrl::rename( const QString &name )
0201 {
0202     m_name = name;
0203     if ( m_id != -1 )
0204         saveToDb();
0205 }
0206 
0207 void AmarokUrl::reparent( const BookmarkGroupPtr &parent )
0208 {
0209     m_parent = parent;
0210     saveToDb();
0211 }
0212 
0213 void AmarokUrl::setCustomValue( const QString & custom )
0214 {
0215     m_customValue = custom;
0216 }
0217 
0218 QString AmarokUrl::customValue() const
0219 {
0220     return m_customValue;
0221 }
0222 
0223 QString AmarokUrl::escape( const QString & in )
0224 {
0225     return QUrl::toPercentEncoding( in.toUtf8() );
0226 }
0227 
0228 QString AmarokUrl::unescape( const QString & in )
0229 {
0230     return QUrl::fromPercentEncoding( in.toUtf8() );
0231 }
0232 
0233 bool AmarokUrl::isNull() const
0234 {
0235     return m_command.isEmpty();
0236 }
0237 
0238 QString AmarokUrl::path() const
0239 {
0240     return m_path;
0241 }
0242 
0243 void AmarokUrl::setPath( const QString &path )
0244 {
0245     m_path = path;
0246 }
0247 
0248 
0249