File indexing completed on 2024-05-05 04:48:27

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Daniel Dewald <Daniel.Dewald@time-shift.de>                       *
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 #define DEBUG_PREFIX "TagGuesser"
0018 
0019 #include "TagGuesser.h"
0020 
0021 #include "core/support/Amarok.h"
0022 #include "core/support/Debug.h"
0023 #include "TagsFromFileNameGuesser.h"
0024 
0025 TagGuesser::TagGuesser()
0026           : m_guessed( false )
0027           , m_caseOptions( 0 )
0028           , m_cutTrailingSpaces( false )
0029           , m_convertUnderscores( false )
0030 {
0031 }
0032 
0033 // sets filename to guess from
0034 void
0035 TagGuesser::setFilename( const QString &fileName )
0036 {
0037     m_fileName = fileName;
0038 }
0039 
0040 // sets schema to guess with
0041 void
0042 TagGuesser::setSchema( const QString &schema )
0043 {
0044     m_schema = schema;
0045 }
0046 
0047 // sets case type to convert tags into
0048 void
0049 TagGuesser::setCaseType( const int caseOptions )
0050 {
0051     m_caseOptions = caseOptions;
0052 }
0053 
0054 // sets whether trailing spaces should be removes
0055 void
0056 TagGuesser::setCutTrailingSpaces( const bool cutTrailingSpaces )
0057 {
0058     m_cutTrailingSpaces = cutTrailingSpaces;
0059 }
0060 
0061 // sets whether underscore should be converted
0062 void
0063 TagGuesser::setConvertUnderscores( const bool convertUnderscores )
0064 {
0065     m_convertUnderscores = convertUnderscores;
0066 }
0067 
0068 // guesses tags depending on the schema
0069 bool
0070 TagGuesser::guess()
0071 {
0072     m_guessed = false;
0073     if( !m_fileName.isEmpty() && !m_schema.isEmpty() )
0074     {
0075         debug() << "Guessing tags from file name '" << m_fileName << "', using schema '" << m_schema << "'.";
0076         Meta::FieldHash tags = Meta::Tag::TagGuesser::guessTagsByScheme( m_fileName, m_schema,
0077                                                                          m_cutTrailingSpaces,
0078                                                                          m_convertUnderscores );
0079         foreach( qint64 key, tags.keys() )
0080         {
0081             if( !key )
0082                 continue;
0083 
0084             m_tags.insert( key, convertTagCaseType( tags[key].toString(), m_caseOptions ) );
0085         }
0086 
0087         m_guessed = !m_tags.isEmpty();
0088     }
0089 
0090     return m_guessed;
0091 }
0092 
0093 // Converts a tag to its case type version
0094 QString
0095 TagGuesser::convertTagCaseType( const QString &tag, int type )
0096 {
0097     if( tag.isEmpty() )
0098         return tag;
0099 
0100     switch( type )
0101     {
0102         case 0:
0103             return tag;
0104         case 1:
0105             return tag.toLower().replace( 0, 1, tag.left( 1 ).toUpper() );
0106         case 2:
0107         {
0108             QString complete;
0109             QStringList splitList = tag.toLower().split( ' ', Qt::SkipEmptyParts );
0110 
0111             foreach( QString word, splitList )
0112             {
0113                 if( word.length() > 1 )
0114                     word.replace( 0, 1, word.left( 1 ).toUpper() );
0115                 complete += word + ' ';
0116             }
0117             complete.truncate( complete.length() - 1 );
0118 
0119             return complete;
0120         }
0121         case 3:
0122             return tag.toUpper();
0123         case 4:
0124             return tag.toLower();
0125         default:
0126             return tag;
0127     }
0128 }
0129