Main Page   Class Hierarchy   Alphabetical List   Compound List   File List  

xmmssensor.cpp

00001 /***************************************************************************
00002 *   Copyright (C) 2003 by Hans Karlsson                                   *
00003 *   karlsson.h@home.se                                                      *
00004 *                                                                         *
00005 *   This program is free software; you can redistribute it and/or modify  *
00006 *   it under the terms of the GNU General Public License as published by  *
00007 *   the Free Software Foundation; either version 2 of the License, or     *
00008 *   (at your option) any later version.                                   *
00009 ***************************************************************************/
00010 #include "xmmssensor.h"
00011 XMMSSensor::XMMSSensor( int interval, QString encoding ) : Sensor( interval )
00012 {
00013      if( encoding != "" )
00014     {
00015         codec = QTextCodec::codecForName( encoding );
00016         if ( codec == 0)
00017             codec = QTextCodec::codecForLocale();
00018     }
00019     else{
00020         codec = QTextCodec::codecForLocale();
00021     }
00022 }
00023 XMMSSensor::~XMMSSensor()
00024 {}
00025 
00026 int XMMSSensor::getW(){
00027   KSimpleConfig conf("karambarc",true);
00028   conf.setGroup("XMMS");
00029   w = conf.readNumEntry("CoverWidth",64);
00030    return w;
00031 }
00032 
00033 int XMMSSensor::getH(){
00034   KSimpleConfig conf("karambarc",true);
00035   conf.setGroup("XMMS");
00036   h = conf.readNumEntry("CoverHeight",64);
00037   return h;
00038 }
00039 
00040 QString XMMSSensor::setCover(){
00041   int w = getW();
00042   int h = getH();
00043   QImage previmg;
00044   fp = locateLocal("tmp", "cover.png");
00045   previmg.load(fp);
00046   if(previmg.text("Title") != title || previmg.width() != w || previmg.height() != h){ //Verify song change to limit CPU usage
00047     QImage img;
00048     QImage tmpimg;
00049     img.load(rcover);
00050     if(img.width() >= w || img.height() >= h){ //scale image if necessary
00051       tmpimg = img.scale(h, w, QImage::ScaleMin);
00052     }
00053     tmpimg.setText("Title", 0, title); //add Title in the image text for cache usage
00054     tmpimg.save(fp, "PNG", 0);
00055   }
00056   return fp;
00057 }
00058 
00059 bool XMMSSensor::id3cover(QString file){
00060   myTag = new ID3_Tag();
00061   myTag->Link(file, ID3TT_ALL);
00062   ID3_Frame* myFrame = myTag->Find(ID3FID_PICTURE);// Check if id3 have PICTURE field
00063   if (NULL != myFrame){
00064   //qDebug("id3 have picture ");
00065     int w = getW();
00066     int h = getH();
00067     QImage previmg;
00068     fp = locateLocal("tmp", "cover.png");
00069     previmg.load(fp);
00070     if(previmg.text("Title") != title || previmg.width() != w || previmg.height() != h){ //Verify song change to limit CPU usage
00071       ID3_Field* myField = myFrame->GetField(ID3FN_DATA);
00072        const uchar* data = myField->GetRawBinary();
00073        size_t size = myField->Size();
00074       QImage img;
00075       QImage tmpimg;
00076       img.loadFromData (data, size);
00077       if(img.width() >= w || img.height() >= h){ //scale image if necessary
00078         tmpimg = img.scale(h, w, QImage::ScaleMin);
00079       }
00080       tmpimg.setText("Title", 0, title); //add Title in the image text for cache usage
00081       tmpimg.save(fp, "PNG", 0);
00082       img.save( locateLocal("tmp", "rcover.png"), "PNG", 0);
00083     }
00084     return true;
00085   }
00086   else{
00087     //qDebug("id3 don't have picture");
00088     return false;
00089   }
00090 }
00091 
00092 void XMMSSensor::update()
00093 {
00094     QString format;
00095     QString imgsize;
00096     SensorParams *sp;
00097     Meter *meter;
00098     QObjectListIt it( *objList );
00099 
00100 #ifdef HAVE_XMMS
00101 
00102     int pos;
00103     int songLength = 0;
00104     int currentTime = 0;
00105     bool isPlaying = false;
00106     bool isRunning = xmms_remote_is_running(0);
00107     bool id3c = false;
00108     KSimpleConfig conf("karambarc",true);
00109     conf.setGroup("karamba");
00110     QString Viewer =  conf.readEntry("ImgViewer", "");
00111     conf.setGroup("XMMS");
00112     bool EnableCover = conf.readBoolEntry("EnableCover",false);
00113     bool EnableID3C = conf.readBoolEntry("EnableId3Cover",false);
00114     bool HideMainWin = conf.readBoolEntry("HideMainWin",false);
00115 
00116     if( isRunning )
00117     {
00118         isPlaying = xmms_remote_is_playing(0);
00119         pos = xmms_remote_get_playlist_pos(0);
00120         //qDebug("unicode start");
00121         title = codec->toUnicode( QCString( xmms_remote_get_playlist_title( 0, pos ) )  );
00122         file = codec->toUnicode( QCString( xmms_remote_get_playlist_file( 0, pos ) )  ); //get the path and filename
00123         //qDebug("unicode end");
00124         if( title == "" ){
00125           if(file != ""){
00126             title = file;
00127           }
00128           else{
00129             title = "XMMS";
00130           }
00131         }
00132 
00133         if(EnableCover == true){
00134           if((file.section('.', -1) == "mp3" || file.section('.', -1) == "MP3") && EnableID3C == true){ // Check if file is an mp3
00135             id3c = id3cover(file);
00136             if(id3c == true){
00137               cover = locateLocal("tmp", "cover.png");
00138               rcover = locateLocal("tmp", "rcover.png");
00139             }
00140           }
00141           if(id3c == false){ // File is not an mp3 or don't have id3 PICTURE field
00142             QUrl url(file); //make QUrl from QString
00143             dir = url.dirPath (); //resolve the absolute path
00144             if(QFile::exists(dir + "/cover.png")){
00145               rcover = dir + "/cover.png";
00146               cover = setCover();
00147             }
00148             else if(QFile::exists(dir + "/cover.jpg")){
00149               rcover = dir + "/cover.jpg";
00150               cover = setCover();
00151             }
00152             else if(QFile::exists(dir + "/cover.jpeg")){
00153               rcover = dir + "/cover.jpeg";
00154               cover = setCover();
00155             }
00156             else{
00157               rcover = "pics/nocover.png";
00158               cover = "pics/nocover.png";
00159             }
00160          }
00161         }
00162 
00163         songLength = xmms_remote_get_playlist_time( 0, pos );
00164         currentTime = xmms_remote_get_output_time( 0 );
00165     }
00166 #endif // HAVE_XMMS
00167 
00168     while (it != 0)
00169     {
00170         sp = (SensorParams*)(*it);
00171         meter = sp->getMeter();
00172 
00173 #ifdef HAVE_XMMS
00174 
00175         if( isRunning )
00176         {
00177             if( xmms_remote_is_main_win(0) == true && HideMainWin == true){
00178               xmms_remote_main_win_toggle(0, false);
00179             }
00180 
00181             format = sp->getParam("FORMAT");
00182 
00183             if (format.length() == 0 )
00184             {
00185                 format = "%title %time / %length";
00186             }
00187 
00188             if( format == "%ms" )
00189             {
00190                 meter->setMax( songLength );
00191                 meter->setValue( currentTime );
00192             }
00193             else{
00194 
00195                 if ( format == "%full" )
00196                 {
00197                     meter->setValue( 1 );
00198                 }
00199                 else
00200                 {
00201                     format.replace( QRegExp("%title", false), title );
00202                     if(EnableCover == true){
00203                       format.replace( QRegExp("%cover", false), cover );
00204                       format.replace( QRegExp("%rcover", false), Viewer + " '" + rcover + "'" );
00205                     }
00206 
00207                     format.replace( QRegExp("%runquit", false), "xmms-shell -e xmmsexit" );
00208                     format.replace( QRegExp("%play", false), "xmms-shell -e play" );
00209                     format.replace( QRegExp("%pause", false), "xmms-shell -e pause" );
00210                     format.replace( QRegExp("%stop", false), "xmms-shell -e stop" );
00211                     format.replace( QRegExp("%next", false), "xmms-shell -e next" );
00212                     format.replace( QRegExp("%prev", false), "xmms-shell -e previous" );
00213                     if(xmms_remote_is_pl_win(0) == true){
00214                       format.replace( QRegExp("%list", false), "xmms-shell -e 'window playlist hide'" );
00215                     }
00216                     else{
00217                       format.replace( QRegExp("%list", false), "xmms-shell -e 'window playlist show'" );
00218                     }
00219 
00220                     format.replace( QRegExp("%length", false), QTime( 0,0,0 ).addMSecs( songLength ).toString( "h:mm:ss" ) );
00221 
00222                     format.replace( QRegExp("%time", false), QTime( 0,0,0 ).addMSecs( currentTime ).toString( "h:mm:ss" ) );
00223 
00224                     if( isPlaying  ){
00225                         format.replace( QRegExp("%remain", false), QTime( 0,0,0 ).
00226                                         addMSecs( songLength )
00227                                         .addMSecs(-currentTime )
00228                                         .toString( "h:mm:ss" ) );
00229                     }
00230                     else{
00231                         format.replace( QRegExp("%remain", false), QTime( 0,0,0 ).toString("h:mm:ss" ) );
00232                     }
00233 
00234                     meter->setValue(format);
00235                 }
00236             }
00237        }
00238         else
00239         {
00240             format = sp->getParam("FORMAT");
00241             if(format == "%runquit"){
00242               format.replace( QRegExp("%runquit", false), "xmms" );
00243               meter->setValue(format);
00244             }
00245             else{
00246               meter->setValue("");
00247             }
00248         }
00249 #endif // HAVE_XMMS
00250         ++it;
00251 
00252     }
00253 
00254 }
00255 
00256 void XMMSSensor::setMaxValue( SensorParams *sp)
00257 {
00258     Meter *meter;
00259     meter = sp->getMeter();
00260     QString f;
00261     f = sp->getParam("FORMAT");
00262 
00263     if ( f == "%full" )
00264         meter->setMax( 1 );
00265 
00266 }

Generated on Tue May 6 09:52:09 2003 for karamba by doxygen1.2.18