File indexing completed on 2025-01-05 04:25:26

0001 /**************************************************************************
0002 *   Encoding Fixer Script for Amarok 2.0                                  *
0003 *   Last Modified:  22/11/2008                                            *
0004 *                                                                         *
0005 *   Copyright                                                             *
0006 *   (C) 2008 Peter ZHOU  <peterzhoulei@gmail.com>                         *
0007 *                                                                         *
0008 *   This program is free software; you can redistribute it and/or modify  *
0009 *   it under the terms of the GNU General Public License as published by  *
0010 *   the Free Software Foundation; either version 2 of the License, or     *
0011 *   (at your option) any later version.                                   *
0012 *                                                                         *
0013 *   This program is distributed in the hope that it will be useful,       *
0014 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0015 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0016 *   GNU General Public License for more details.                          *
0017 *                                                                         *
0018 *   You should have received a copy of the GNU General Public License     *
0019 *   along with this program; if not, write to the                         *
0020 *   Free Software Foundation, Inc.,                                       *
0021 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0022 **************************************************************************/
0023 
0024 Importer.loadQtBinding( "qt.core" );
0025 Importer.loadQtBinding( "qt.gui" );
0026 Importer.loadQtBinding( "qt.uitools" );
0027 
0028 function checkerURL( encoding, webencoding, url, querystr )
0029 {
0030     this.encoding = encoding;
0031     this.webencoding = webencoding;
0032     this.url = url;
0033     this.querystr = querystr;
0034 }
0035 
0036 function fixencoding( encoding )
0037 {
0038 //Yes = 3, No = 4
0039     if ( Amarok.alert( "<p>the track might be encoded in " + encoding + ".</p>"
0040                        + "<p>Title: " + Amarok.Lyrics.QStringtoUtf8( Amarok.Engine.currentTrack().title, encoding ) + "</p>"
0041                        + "<p>Artist: " + Amarok.Lyrics.QStringtoUtf8( Amarok.Engine.currentTrack().artist, encoding ) + "</p>"
0042 +                      + "<p>Album: " + Amarok.Lyrics.QStringtoUtf8( Amarok.Engine.currentTrack().album, encoding ) + "</p>"
0043                        + "<p>Comment: " + Amarok.Lyrics.QStringtoUtf8( Amarok.Engine.currentTrack().comment, encoding ) + "</p>"
0044                        + "<p>Do you want to convert it to UTF-8?</p>", "questionYesNo" ) == 3 )
0045     {
0046         Amarok.debug( "converting the track tag to " + encoding + "..." );
0047         Amarok.Engine.currentTrack().title = Amarok.Lyrics.QStringtoUtf8( Amarok.Engine.currentTrack().title, encoding );
0048         Amarok.Engine.currentTrack().artist = Amarok.Lyrics.QStringtoUtf8( Amarok.Engine.currentTrack().artist, encoding );
0049         Amarok.Engine.currentTrack().album = Amarok.Lyrics.QStringtoUtf8( Amarok.Engine.currentTrack().album, encoding );
0050         Amarok.Engine.currentTrack().comment = Amarok.Lyrics.QStringtoUtf8( Amarok.Engine.currentTrack().comment, encoding );
0051     }
0052 }
0053 
0054 function checkGB18030( gb18030reply )
0055 {
0056     try
0057     {
0058         this.pageresult = gb18030reply;
0059 //        Amarok.debug( reply );
0060         Amarok.debug( "searching..." );
0061         this.resultPos = this.pageresult.search( /<tr><td rowspan="2" style="line-height:21px"><font class=mr>*/ );
0062 
0063         if( this.resultPos > 0 )
0064         {
0065             Amarok.debug( "found info at pos " + this.resultPos );
0066             Amarok.debug( "the encoding of the track might be: GB18030" );
0067             fixencoding( "GB18030" );
0068         }
0069         else
0070         {
0071             Amarok.debug( "cannot find info using encoding: GB18030" );
0072         }
0073     }
0074     catch( err )
0075     {
0076         Amarok.debug ( err );
0077     }
0078 }
0079 
0080 function checkBig5( big5reply )
0081 {
0082     try
0083     {
0084         this.pageresult = big5reply;
0085 //        Amarok.debug( reply );
0086         Amarok.debug( "searching..." );
0087         this.resultPos = this.pageresult.search( /<tr><td rowspan="2" style="line-height:21px"><font class=mr>*/ );
0088 
0089         if( this.resultPos > 0 )
0090         {
0091             Amarok.debug( "found info at pos " + this.resultPos );
0092             Amarok.debug( "the encoding of the track might be: Big5" );
0093             fixencoding( "Big5" );
0094         }
0095         else
0096         {
0097             Amarok.debug( "cannot find info using encoding: Big5" );
0098         }
0099     }
0100     catch( err )
0101     {
0102         Amarok.debug ( err );
0103     }
0104 }
0105 
0106 function checkKOI8R( koi8rreply )
0107 {
0108     try
0109     {
0110         this.pageresult = koi8rreply;
0111 //        Amarok.debug( reply );
0112         Amarok.debug( "searching..." );
0113         this.resultPos = this.pageresult.search( /style="margin-right: 30px;">*/ );
0114 
0115         if( this.resultPos == 0 )
0116         {
0117             Amarok.debug( "found info at pos " + this.resultPos );
0118             Amarok.debug( "the encoding of the track might be: KOI8-R" );
0119             fixencoding( "KOI8-R" );
0120         }
0121         else
0122         {
0123             Amarok.debug( "cannot find info using encoding: KOI8-R" );
0124         }
0125     }
0126     catch( err )
0127     {
0128         Amarok.debug ( err );
0129     }
0130 }
0131 
0132 function urlGenerator( title, urlInfo )
0133 {
0134     try
0135     {
0136         Amarok.debug( "checking for track title: " + title );
0137 
0138         Amarok.debug( "checking encoding candidate: " + urlInfo.encoding );
0139         this.info = Amarok.Lyrics.QStringtoUtf8( title, urlInfo.encoding );
0140         Amarok.debug( "trying: " + this.info );
0141 
0142         this.encodedTitleKey = Amarok.Lyrics.fromUtf8( urlInfo.querystr, urlInfo.webencoding );
0143         this.encodedTitle = Amarok.Lyrics.fromUtf8( this.info, urlInfo.webencoding );
0144         this.url = new QUrl( urlInfo.url );
0145         this.url.addEncodedQueryItem( this.encodedTitleKey, this.encodedTitle );
0146         return this.url;
0147     }
0148     catch( err )
0149     {
0150         Amarok.debug ( err );
0151     }
0152 
0153 }
0154 
0155 var urlInfo = new Array (
0156     new checkerURL( "GB18030", "GB2312", "http://mp3.sogou.com/gecisearch.so", "query" ),
0157     new checkerURL( "Big5", "GB2312", "http://mp3.sogou.com/gecisearch.so", "query" ),
0158     new checkerURL( "KOI8-R", "KOI8-R", "http://www.zvuki.ru/search/?what=song", "query" )
0159 //    new checkerURL( "UTF-8", "", "", "" )
0160 );
0161 
0162 function doubleChecker()
0163 {
0164     this.title = Amarok.Engine.currentTrack().title;
0165     //checking gb18030
0166     if ( simpleChinese_Module )
0167     {
0168         this.gb18030url = urlGenerator( this.title, this.urlInfo[0] );
0169         this.gb18030d = new Downloader( this.gb18030url, checkGB18030, urlInfo[0].webencoding );
0170     }
0171     //checking big5
0172     if ( triditionalChinese_Module )
0173     {
0174         this.big5url = urlGenerator( this.title, this.urlInfo[1] );
0175         this.big5d = new Downloader( this.big5url, checkBig5, urlInfo[1].webencoding );
0176     }
0177     //checking KOI8-R
0178     if ( Russian_Module )
0179     {
0180         this.koi8rurl = urlGenerator( this.title, this.urlInfo[2] );
0181         this.koi8rd = new Downloader( this.koi8rurl, checkKOI8R, urlInfo[2].webencoding );
0182     }
0183 }
0184 
0185 function saveConfiguration()
0186 {
0187 //Pretty messy :S
0188     if ( mainWindow.children()[1].children()[1].checked )
0189     {
0190         Amarok.Script.writeConfig( "simple_Chinese", "true" );
0191         simpleChinese_Module = true;
0192     }
0193     else
0194     {
0195         Amarok.Script.writeConfig( "simple_Chinese", "false" );
0196         simpleChinese_Module = false;
0197     }
0198     if ( mainWindow.children()[1].children()[2].checked )
0199     {
0200         Amarok.Script.writeConfig( "triditional_Chinese", "true" );
0201         triditionalChinese_Module = true;
0202     }
0203     else
0204     {
0205         Amarok.Script.writeConfig( "triditional_Chinese", "false" );
0206         triditionalChinese_Module = false;
0207     }
0208     if ( mainWindow.children()[1].children()[3].checked )
0209     {
0210         Amarok.Script.writeConfig( "Russian", "true" );
0211         Russian_Module = true;
0212     }
0213     else
0214     {
0215         Amarok.Script.writeConfig( "Russian", "false" );
0216         Russian_Module = false;
0217     }
0218 }
0219 
0220 function readConfiguration()
0221 {
0222     if ( Amarok.Script.readConfig( "simple_Chinese", "false" ) == "false" )
0223         mainWindow.children()[1].children()[1].checked = false;
0224     else
0225         mainWindow.children()[1].children()[1].checked = true;
0226     if ( Amarok.Script.readConfig( "triditional_Chinese", "false" ) == "false" )
0227         mainWindow.children()[1].children()[2].checked = false;
0228     else
0229         mainWindow.children()[1].children()[2].checked = true;
0230     if ( Amarok.Script.readConfig( "Russian", "false" ) == "false" )
0231         mainWindow.children()[1].children()[3].checked = false;
0232     else
0233         mainWindow.children()[1].children()[3].checked = true;
0234 }
0235 
0236 function onConfigure()
0237 {
0238     mainWindow.show();
0239 }
0240 
0241 function init()
0242 {
0243     try
0244     {
0245         var UIloader = new QUiLoader( this );
0246         var uifile = new QFile ( Amarok.Info.scriptPath() + "/main.ui" );
0247         uifile.open( QIODevice.ReadOnly );
0248         mainWindow = UIloader.load( uifile, this);
0249         uifile.close();
0250         readConfiguration();
0251         mainWindow.children()[0].accepted.connect( saveConfiguration );
0252         mainWindow.children()[0].rejected.connect( readConfiguration );
0253 
0254         Amarok.Window.addToolsSeparator();
0255         Amarok.Window.addToolsMenu( "checkencoding", "Check Encodings" );
0256         Amarok.Window.addSettingsSeparator();
0257         Amarok.Window.addSettingsMenu( "configencoding", "Encoding Fixer Settings..." );
0258         Amarok.Window.ToolsMenu.checkencoding.triggered.connect( doubleChecker );
0259         Amarok.Window.SettingsMenu.configencoding.triggered.connect( onConfigure );
0260     }
0261     catch( err )
0262     {
0263         Amarok.debug ( err );
0264     }
0265 }
0266 
0267 var simpleChinese_Module = false;
0268 var triditionalChinese_Module = false;
0269 var Russian_Module = false;
0270 init();