File indexing completed on 2025-02-23 04:27:08

0001 /**************************************************************************
0002 *   Copy Cover Script for Amarok 2.0                                      *
0003 *                                                                         *
0004 *   Copyright                                                             *
0005 *   (C) 2009 Sven Krohlas <sven@asbest-online.de>                         *
0006 *   (C) 2008 Simon Esneault  <simon.esneault@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 
0025 Importer.loadQtBinding( "qt.core" );
0026 Importer.loadQtBinding( "qt.gui" );
0027 Importer.loadQtBinding( "qt.uitools" );
0028 
0029 function copyCover()
0030 {
0031         var TrackInfo = Amarok.Engine.currentTrack();
0032         if ( TrackInfo.isValid ) // if track is valid
0033         {
0034                 var path_image=TrackInfo.imageUrl.substring( 7 );
0035                 var image=new QImage( path_image );
0036                 if ( !image.isNull() ) // if we have a cover
0037                 {
0038                         // Make a the correct path
0039                         var path=TrackInfo.path.substring( 0, TrackInfo.path.lastIndexOf( "/" ) + 1 );
0040             
0041             if ( writeCover == true )
0042             {
0043                 var patha=path+"cover.png";
0044                 
0045                 // try to load the image first, to prevent rewritting
0046                 var image2 = new QImage ( patha );
0047                 if ( !image2.isNull() ) // if already an image
0048                 {
0049                     if ( image2 != image ) // and image are different
0050                     {
0051                         if( image.save( patha ) )
0052                             Amarok.Window.Statusbar.shortMessage( "Copy-Cover has written " + patha );
0053                         else
0054                             Amarok.Window.Statusbar.shortMessage( "Copy-Cover can not write " + patha );
0055                     }
0056                 }
0057                 else
0058                 {
0059                     if( image.save( patha ) )
0060                         Amarok.Window.Statusbar.shortMessage( "Copy-Cover has written " + patha );
0061                     else
0062                         Amarok.Window.Statusbar.shortMessage( "Copy-Cover can not write " + patha );
0063                 }
0064             }
0065             
0066             if ( writeArtistAlbum == true )
0067             {
0068                 path+=TrackInfo.artist+"-"+TrackInfo.album+".png";
0069                 
0070                 // try to load the image first, to prevent rewritting
0071                 var image2 = new QImage( path );
0072                 if ( !image2.isNull() ) // if already an image
0073                 {
0074                     if ( image2 != image ) // and image are different
0075                     {
0076                         if( image.save(path ) )
0077                             Amarok.Window.Statusbar.shortMessage( "Copy-Cover has written " + path );
0078                         else
0079                             Amarok.Window.Statusbar.shortMessage( "Copy-Cover can not write " + path );
0080                     }
0081                 }
0082                 else
0083                 {
0084                 
0085                     if( image.save( path ) )
0086                         Amarok.Window.Statusbar.shortMessage( "Copy-Cover has written " + path );
0087                     else
0088                         Amarok.Window.Statusbar.shortMessage( "Copy-Cover can not write " + path );
0089                 }
0090             }            
0091                 }
0092                 else
0093                         Amarok.Window.Statusbar.shortMessage( "Copy-Cover can not read the image" );
0094                 
0095         }       
0096         else
0097         {
0098                 Amarok.debug( "COPY-COVER -> Track not valid " );
0099         }  
0100 }
0101 
0102 
0103 function saveConfiguration()
0104 {
0105     //Pretty messy :S
0106     if ( mainWindow.widget.checkBox.checked )
0107     {
0108         Amarok.Script.writeConfig( "writeCover", "true" );
0109         writeCover = true;
0110     }
0111     else
0112     {
0113         Amarok.Script.writeConfig( "writeCover", "false" );
0114         writeCover = false;
0115     }
0116     if ( mainWindow.widget.checkBox_2.checked )
0117     {
0118         Amarok.Script.writeConfig( "writeArtistAlbum", "true" );
0119         writeArtistAlbum = true;
0120     }
0121     else
0122     {
0123         Amarok.Script.writeConfig( "writeArtistAlbum", "false" );
0124         writeArtistAlbum = false;
0125     }
0126 }
0127 
0128 function readConfiguration()
0129 {
0130     if ( Amarok.Script.readConfig( "writeCover", "true" ) == "true" )
0131         mainWindow.widget.checkBox.setChecked( true );
0132     else
0133         mainWindow.widget.checkBox.setChecked( false );
0134 
0135     if ( Amarok.Script.readConfig( "writeArtistAlbum", "false" ) == "false" )
0136         mainWindow.widget.checkBox_2.setChecked( false );
0137     else
0138         mainWindow.widget.checkBox_2.setChecked( true );
0139 
0140 }
0141 
0142 function openSettings()
0143 {
0144     mainWindow.show();
0145     Amarok.debug( "COPY-COVER -> Show configuration" );
0146 }
0147 
0148 function init()
0149 {
0150     try
0151     {
0152         // Ui stuff
0153         var UIloader = new QUiLoader( this );
0154         var uifile = new QFile ( Amarok.Info.scriptPath() + "/copycover.ui" );
0155         uifile.open( QIODevice.ReadOnly );
0156         mainWindow = UIloader.load( uifile, this ); //load the ui file
0157         uifile.close();
0158 
0159         // read configuration
0160         readConfiguration();
0161 
0162         // connect the button ok/cancel to save/read config.
0163         mainWindow.buttonBox.accepted.connect( saveConfiguration );
0164         mainWindow.buttonBox.rejected.connect( readConfiguration );
0165         
0166         // Add tool menu, and a callback
0167         Amarok.Window.addSettingsMenu( "copycov", "Copy Cover Settings", "amarok" );
0168         Amarok.Window.SettingsMenu.copycov['triggered()'].connect(openSettings );
0169         
0170         // call every track changed
0171         Amarok.Engine.trackChanged.connect( copyCover ) ;
0172     }
0173     catch( err )
0174     {
0175         Amarok.debug( err );
0176     }
0177 }
0178 
0179 var writeCover = true;
0180 var writeArtistAlbum = false;
0181 init();
0182 
0183 
0184 
0185 
0186