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 #include <qurl.h>
00012 #include <qfile.h>
00013 #include <qimage.h>
00014 #include <kstandarddirs.h>
00015 #ifdef HAVE_XMMS
00016 #include <xmmsctrl.h>
00017 #endif // HAVE_XMMS
00018 
00019 XMMSSensor::XMMSSensor( int interval, QString encoding ) : Sensor( interval )
00020 {
00021      if( encoding != "" )
00022     {
00023         codec = QTextCodec::codecForName( encoding );
00024         if ( codec == 0)
00025             codec = QTextCodec::codecForLocale();
00026     }
00027     else{
00028         codec = QTextCodec::codecForLocale();
00029     }
00030 
00031 }
00032 XMMSSensor::~XMMSSensor()
00033 {}
00034 
00035 int XMMSSensor::getW(){
00036   KSimpleConfig conf("karambarc",true);
00037   conf.setGroup("XMMS");
00038   w = conf.readNumEntry("CoverWidth",64);
00039    return w;
00040 }
00041 
00042 int XMMSSensor::getH(){
00043   KSimpleConfig conf("karambarc",true);
00044   conf.setGroup("XMMS");
00045   h = conf.readNumEntry("CoverHeight",64);
00046   return h;
00047 }
00048 
00049 QString XMMSSensor::setCover(){
00050   int w = getW();
00051   int h = getH();
00052   QImage previmg;
00053   fp = locateLocal("tmp", "cover.png");
00054   previmg.load(fp);
00055   if(previmg.text("Title") != title || previmg.width() != w || previmg.height() != h){ //Verify song change to limit CPU usage
00056     QImage img;
00057     QImage tmpimg;
00058     img.load(rcover);
00059     if(img.width() >= w || img.height() >= h){ //scale image if necessary
00060       tmpimg = img.scale(h, w, QImage::ScaleMin);
00061        tmpimg.setText("Title", 0, title); //add Title in the image text for cache usage
00062     }
00063     tmpimg.save(fp, "PNG", 0);
00064   }
00065   return fp;
00066 }
00067 
00068 void XMMSSensor::update()
00069 {
00070     QString format;
00071     QString imgsize;
00072     SensorParams *sp;
00073     Meter *meter;
00074     QObjectListIt it( *objList );
00075 
00076 #ifdef HAVE_XMMS
00077 
00078     int pos;
00079     int songLength = 0;
00080     int currentTime = 0;
00081     bool isPlaying = false;
00082     bool isRunning = xmms_remote_is_running(0);
00083 
00084     if( isRunning )
00085     {
00086         isPlaying = xmms_remote_is_playing(0);
00087         pos = xmms_remote_get_playlist_pos(0);
00088         //qDebug("unicode start");
00089         title = codec->toUnicode( QCString( xmms_remote_get_playlist_title( 0, pos ) )  );
00090         file = codec->toUnicode( QCString( xmms_remote_get_playlist_file( 0, pos ) )  ); //get the path and filename
00091         //qDebug("unicode end");
00092         if( title == "" ){
00093           if(file != ""){
00094             title = file;
00095           }
00096           else{
00097             title = "XMMS";
00098           }
00099         }
00100         QUrl url(file); //make QUrl from QString
00101         dir = url.dirPath (); //resolve the absolute path
00102         if(QFile::exists (dir + "/cover.png")){
00103           rcover = dir + "/cover.png";
00104           cover = setCover();
00105         }
00106         else if(QFile::exists (dir + "/cover.jpg")){
00107           rcover = dir + "/cover.jpg";
00108           cover = setCover();
00109         }
00110         else if(QFile::exists (dir + "/cover.jpeg")){
00111           rcover = dir + "/cover.jpeg";
00112            cover = setCover();
00113        }
00114         else{
00115           cover = codec->toUnicode("pics/nocover.png");
00116         }
00117 
00118         //qDebug(cover);
00119         //qDebug(title);
00120         songLength = xmms_remote_get_playlist_time( 0, pos );
00121         currentTime = xmms_remote_get_output_time( 0 );
00122     }
00123 #endif // HAVE_XMMS
00124 
00125     while (it != 0)
00126     {
00127         sp = (SensorParams*)(*it);
00128         meter = sp->getMeter();
00129 
00130 #ifdef HAVE_XMMS
00131 
00132         if( isRunning )
00133         {
00134 
00135             format = sp->getParam("FORMAT");
00136             QString click = sp->getParam("ONCLICK");
00137             QString tx = sp->getParam("X");
00138             QString ty = sp->getParam("Y");
00139 
00140             if (format.length() == 0 )
00141             {
00142                 format = "%title %time / %length";
00143             }
00144 
00145             if( format == "%ms" )
00146             {
00147                 meter->setMax( songLength );
00148                 meter->setValue( currentTime );
00149             }
00150             else
00151 
00152                 if ( format == "%full" )
00153                 {
00154                     meter->setValue( 1 );
00155                 }
00156                 else
00157                 {
00158 
00159 
00160                     format.replace( QRegExp("%title", false), title );
00161                     if(format == "%cover"){
00162                       click.replace( QRegExp("%cover", false), cover );
00163                       meter->setValue(click);
00164                       format.replace( QRegExp("%cover", false), cover );
00165                    }
00166 
00167                     format.replace( QRegExp("%length", false), QTime( 0,0,0 ).
00168                                     addMSecs( songLength )
00169                                     .toString( "h:mm:ss" ) );
00170 
00171                     format.replace( QRegExp("%time", false), QTime( 0,0,0 ).
00172                                     addMSecs( currentTime )
00173                                     .toString( "h:mm:ss" ) );
00174 
00175                     if( isPlaying  )
00176                     {
00177                         format.replace( QRegExp("%remain", false), QTime( 0,0,0 ).
00178                                         addMSecs( songLength )
00179                                         .addMSecs(-currentTime )
00180                                         .toString( "h:mm:ss" ) );
00181                     }
00182 
00183                     else
00184                     {
00185                         format.replace( QRegExp("%remain", false), QTime( 0,0,0 ).toString("h:mm:ss" ) );
00186                     }
00187                     meter->setValue(format);
00188                 }
00189        }
00190         else
00191 #endif // HAVE_XMMS
00192 
00193         {
00194             meter->setValue("");
00195         }
00196         ++it;
00197 
00198     }
00199 
00200 }
00201 
00202 void XMMSSensor::setMaxValue( SensorParams *sp)
00203 {
00204     Meter *meter;
00205     meter = sp->getMeter();
00206     QString f;
00207     f = sp->getParam("FORMAT");
00208 
00209     if ( f == "%full" )
00210         meter->setMax( 1 );
00211 
00212 }

Generated on Tue Apr 29 19:56:58 2003 for karamba by doxygen1.2.18