00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "xmmssensor.h"
00011
00012 XMMSSensor::XMMSSensor( int interval, QString encoding, int nw, int nh, karamba* k ) : Sensor( interval )
00013 {
00014 if( encoding != "" )
00015 {
00016 codec = QTextCodec::codecForName( encoding );
00017 if ( codec == 0)
00018 codec = QTextCodec::codecForLocale();
00019 }
00020 else{
00021 codec = QTextCodec::codecForLocale();
00022 }
00023 slotLoadConfig();
00024 w = getW( nw );
00025 h = getH( nh );
00026 current = 0;
00027 pos = 0;
00028 title = "";
00029 stitle = "";
00030 artist = "";
00031 album = "";
00032 track = "";
00033 vol = 0;
00034 currentTime = 0;
00035 setKaramba(k);
00036 connect( this,SIGNAL(songChange()), this,SLOT(slotUpValues()) );
00037 connect( myKaramba,SIGNAL(configChanged()), this,SLOT(slotLoadConfig()) );
00038 #ifdef HAVE_XMMS
00039 if(xmms_remote_is_running(0)){
00040 slotGetPos();
00041 }
00042 #endif // HAVE_XMMS
00043 }
00044
00045 XMMSSensor::~XMMSSensor()
00046 {}
00047
00048 int XMMSSensor::getW( int nw ){
00049 if(nw == 0){
00050 nw = defW;
00051 }
00052 return nw;
00053 }
00054
00055 int XMMSSensor::getH( int nh ){
00056 if(nh == 0){
00057 nh = defH;
00058 }
00059 return nh;
00060 }
00061
00062 void XMMSSensor::slotUpValues(){
00063 #ifdef HAVE_XMMS
00064 freq = 0;
00065 channel = 0;
00066 rate = 0;
00067 kdDebug("XMMS sensor") << "Song change, updating values" << endl;
00068 title = "";
00069 stitle = "";
00070 artist = "";
00071 album = "";
00072 track = "";
00073 cover = "";
00074 rcover = "";
00075 file = codec->toUnicode( QCString( xmms_remote_get_playlist_file( 0, pos ) ) );
00076 Tags* tags = new Tags( file );
00077 if(tags->isMP3() && EnableID3){
00078 tags->ReadId3();
00079 stitle = tags->stitle;
00080 artist = tags->artist;
00081 album = tags->album;
00082 track = QString::number(tags->track);
00083 title = tags->title;
00084 rate = tags->rate;
00085 channel = tags->channel;
00086 freq = tags->freq;
00087 }
00088 else if(tags->isOGG() && EnableID3){
00089 tags->ReadVorbis();
00090 stitle = tags->stitle;
00091 artist = tags->artist;
00092 album = tags->album;
00093 track = QString::number(tags->track);
00094 title = tags->title;
00095 rate = tags->rate;
00096 int tmpfreq;
00097 xmms_remote_get_info(0, &rate, &tmpfreq, &channel);
00098 freq = ((QString::number(tmpfreq).toDouble())/1000);
00099 }
00100 if( title == "" ){
00101 kdDebug("XMMS sensor") << "Getting tags from XMMS" << endl;
00102 title = codec->toUnicode( QCString( xmms_remote_get_playlist_title( 0, pos ) ) );
00103 ConfigFile* xmmsConf = xmms_cfg_open_default_file();
00104 gchar* pat;
00105 xmms_cfg_read_string(xmmsConf, "xmms", "generic_title_format", &pat);
00106 title = tags->upFirst(title);
00107 QString pattern = pat;
00108 int c = pattern.contains("-");
00109 kdDebug("XMMS sensor") << pattern << endl;
00110 int n = 0;
00111 while(n != c){
00112 QString sec = pattern.section("-", n, n).stripWhiteSpace();
00113 kdDebug("XMMS sensor") << sec << endl;
00114 if(sec == "%n"){
00115 track = title.section("-", n, n).stripWhiteSpace();
00116 }
00117 else if(sec == "%p"){
00118 artist = title.section("-", n, n).stripWhiteSpace();
00119 }
00120 else if(sec == "%t"){
00121 stitle = title.section("-", n, n).stripWhiteSpace();
00122 }
00123 else if(sec == "%a"){
00124 album = title.section("-", n, n).stripWhiteSpace();
00125 }
00126 n++;
00127 }
00128 tags->setInfos(track.toInt(), artist, stitle, album, QString::null, 0, QString::null);
00129 if(SaveID3 && QFileInfo(file).isWritable()){
00130 if(tags->isMP3()){
00131 tags->WriteId3();
00132 }
00133 if(tags->isOGG()){
00134 tags->WriteVorbis();
00135 }
00136 }
00137 int tmpfreq;
00138 xmms_remote_get_info(0, &rate, &tmpfreq, &channel);
00139 freq = ((QString::number(tmpfreq).toDouble())/1000);
00140 }
00141 kdDebug("XMMS sensor") << title << endl;
00142 if(channel == 1){
00143 monoster = "mono";
00144 }
00145 else if(channel == 2){
00146 monoster = "stereo";
00147 }
00148 else{
00149 monoster = "nosound";
00150 }
00151 songLength = xmms_remote_get_playlist_time( 0, pos );
00152 if(EnableCover){
00153 slotGetCover( tags );
00154 }
00155 tags = 0;
00156 #endif // HAVE_XMMS
00157 }
00158
00159 QString XMMSSensor::setCover(int w, int h, QString cover){
00160 fp = locateLocal("tmp", "cover.png");
00161 QImage img;
00162 QImage tmpimg;
00163 img.load(cover);
00164 if(img.width() >= w || img.height() >= h){
00165 tmpimg = img.smoothScale(h, w, QImage::ScaleMin);
00166 tmpimg.save(fp, "PNG", 0);
00167 img.reset();
00168 tmpimg.reset();
00169 }
00170 else{
00171 fp = cover;
00172 }
00173 return fp;
00174 }
00175
00176 void XMMSSensor::slotGetCover( Tags* tags ){
00177 bool id3c = false;
00178 if(tags->isMP3() && EnableID3C){
00179 QImage img = tags->cover;
00180 if(tags->haveCover){
00181 QImage img = tags->cover;
00182 QImage tmpimg;
00183 if(img.width() >= w || img.height() >= h){
00184 tmpimg = img.scale(h, w, QImage::ScaleMin);
00185 }
00186 tmpimg.save(fp, "PNG", 0);
00187 img.save( locateLocal("tmp", "rcover.png"), "PNG", 0);
00188 img.reset();
00189 tmpimg.reset();
00190 cover = locateLocal("tmp", "cover.png");
00191 rcover = locateLocal("tmp", "rcover.png");
00192 id3c = true;
00193 }
00194 }
00195 if(!id3c){
00196 QUrl url(file);
00197 dir = url.dirPath ();
00198 if(QFile::exists(dir + "/cover.png")){
00199 rcover = dir + "/cover.png";
00200 cover = setCover(w, h, rcover);
00201 }
00202 else if(QFile::exists(dir + "/cover.jpg")){
00203 rcover = dir + "/cover.jpg";
00204 cover = setCover(w, h, rcover);
00205 }
00206 else if(QFile::exists(dir + "/cover.jpeg")){
00207 rcover = dir + "/cover.jpeg";
00208 cover = setCover(w, h, rcover);
00209 }
00210 else if(QFile::exists(locateLocal("data", "karamba/covers/"+artist+" - "+album+".jpg"))){
00211 rcover = locateLocal("data", "karamba/covers/"+artist+" - "+album+".jpg");
00212 cover = setCover(w, h, rcover);
00213 }
00214 else if(NetCover){
00215 if(QFileInfo(dir).isWritable()){
00216 if(darkcover(dir + "/cover.jpg", tags)){
00217 rcover = dir + "/cover.jpg";
00218 cover = setCover(w, h, rcover);
00219 }
00220 }
00221 else{
00222 if(darkcover(locateLocal("data", "karamba/covers/"+artist+" - "+album+".jpg"), tags)){
00223 rcover = locateLocal("data", "karamba/covers/"+artist+" - "+album+".jpg");
00224 cover = setCover(w, h, rcover);
00225 }
00226 }
00227 }
00228 if(cover == ""){
00229 rcover = "pics/nocover.png";
00230 cover = "pics/nocover.png";
00231 }
00232 }
00233 }
00234
00235 bool XMMSSensor::darkcover(QString out, Tags* tags){
00236 kdDebug("Tags") << "Searching cover on darktown.com" << endl;
00237 QString ar = (artist.lower()).replace(" ", "_");
00238 QString al = (album.lower()).replace(" ", "_");
00239 QString l = ar.left(1);
00240
00241 QString mime = "";
00242 KURL u1("http://img.darktown.com/cover/audio/download/"+l+"/"+ar+"_-_"+al+"_a.jpg");
00243 mime = KIO::NetAccess::mimetype( u1, NULL );
00244 if( mime == "image/jpeg" ){
00245 kdDebug("Tags") << "Cover found, downloading" << endl;
00246 return KIO::NetAccess::download( u1, out, NULL );
00247 }
00248 else{
00249 KURL u2("http://img.darktown.com/cover/audio/download/"+l+"/"+ar+"_"+al+"_a.jpg");
00250 mime = KIO::NetAccess::mimetype( u2, NULL );
00251 if( mime == "image/jpeg" ){
00252 kdDebug("Tags") << "Cover found, downloading" << endl;
00253 return KIO::NetAccess::download( u2, out, NULL );
00254 }
00255 else{
00256 QString ar = (tags->upFirst(artist.lower())).replace(" ", "_");
00257 QString al = (tags->upFirst(album.lower())).replace(" ", "_");
00258 KURL u3("http://img.darktown.com/cover/audio/download/"+l+"/"+ar+"_"+al+"_a.jpg");
00259 mime = KIO::NetAccess::mimetype( u3, NULL );
00260 if( mime == "image/jpeg" ){
00261 kdDebug("Tags") << "Cover found, downloading" << endl;
00262 return KIO::NetAccess::download( u3, out, NULL );
00263 }
00264 else{
00265 kdDebug("Tags") << "Cover not found" << endl;
00266 return false;
00267 }
00268 }
00269 }
00270 }
00271
00272 void XMMSSensor::slotGetPos(){
00273 pos = xmms_remote_get_playlist_pos(0);
00274 if(pos != current && pos != 0){
00275 current = pos;
00276 emit songChange();
00277 }
00278 }
00279
00280 void XMMSSensor::update(){
00281 QString format;
00282 QString imgsize;
00283 SensorParams *sp;
00284 Meter *meter;
00285 QObjectListIt it( *objList );
00286 #ifdef HAVE_XMMS
00287 bool isRunning = xmms_remote_is_running(0);
00288 bool isPlaying = false;
00289 if( isRunning ){
00290 if( xmms_remote_is_main_win(0)){
00291 if( HideMainWin ){
00292 kdDebug("XMMS sensor") << "Hide XMMS main window" << endl;
00293 xmms_remote_main_win_toggle(0, false);
00294 }
00295 }
00296 else{
00297 if( !HideMainWin ){
00298 kdDebug("XMMS sensor") << "Show XMMS main window" << endl;
00299 xmms_remote_main_win_toggle(0, true);
00300 }
00301 }
00302 slotGetPos();
00303 isPlaying = xmms_remote_is_playing(0) || xmms_remote_is_paused(0);
00304 if(isPlaying){
00305 currentTime = xmms_remote_get_output_time( 0 );
00306 }
00307 else{
00308 currentTime = 0;
00309 }
00310 }
00311 #endif // HAVE_XMMS
00312 while (it != 0){
00313 sp = (SensorParams*)(*it);
00314 meter = sp->getMeter();
00315 #ifdef HAVE_XMMS
00316 if( isRunning ){
00317 format = sp->getParam("FORMAT");
00318 if (format.length() == 0 ){
00319 format = "%title %time / %length";
00320 }
00321 if( format == "%ms" ){
00322 meter->setMax( songLength );
00323 meter->setValue( currentTime );
00324 }
00325 else{
00326 if ( format == "%full" ){
00327 meter->setValue( 1 );
00328 }
00329 else{
00330 if(format.contains("%file")){
00331 format.replace( "%file", KURL(file).url() );
00332 }
00333 if(format.contains("%title")){
00334 format.replace( "%title", title );
00335 }
00336 if(EnableCover && format.contains("%cover")){
00337 format.replace( "%cover", cover );
00338 }
00339 if(EnableCover && format.contains("%viewer")){
00340 format.replace( "%viewer", Viewer );
00341 }
00342 if(EnableCover && format.contains("%rcover")){
00343 format.replace( "%rcover", rcover );
00344 }
00345 if(format.contains("%stitle")){
00346 format.replace( "%stitle", stitle );
00347 }
00348 if(format.contains("%artist")){
00349 format.replace( "%artist", artist );
00350 }
00351 if(format.contains("%album")){
00352 format.replace( "%album", album );
00353 }
00354 if(format.contains("%track")){
00355 format.replace( "%track", track );
00356 }
00357 if(format.contains("%rate")){
00358 format.replace( "%rate", QString::number(rate/1000) );
00359 }
00360 if(format.contains("%freq")){
00361 format.replace( "%freq", QString::number(freq) );
00362 }
00363 if(format.contains("%channel")){
00364 format.replace( "%channel", QString::number(channel) );
00365 }
00366 if(format.contains("%monoster")){
00367 format.replace( "%monoster", monoster );
00368 }
00369 if( format.contains("%runquit") ){
00370 format.replace("%runquit", "exit");
00371 meter->setValue( "exit" );
00372 }
00373 if(format == "%onoff"){
00374 format.replace( "%onoff", "xmms-shell -e xmmsexit" );
00375 }
00376 if(format == "%pref"){
00377 format.replace( "%pref", "xmms-shell -e preferences" );
00378 }
00379 if(format == "%play"){
00380 format.replace( "%play", "xmms-shell -e play" );
00381 }
00382 if(format == "%pause"){
00383 format.replace( "%pause", "xmms-shell -e pause" );
00384 }
00385 if(format == "%stop"){
00386 format.replace( "%stop", "xmms-shell -e stop" );
00387 }
00388 if(format == "%next"){
00389 format.replace( "%next", "xmms-shell -e next" );
00390 }
00391 if(format == "%prev"){
00392 format.replace( "%prev", "xmms-shell -e previous" );
00393 }
00394 if(format == "%volup"){
00395 format.replace( "%volup", "xmms-shell -e +" );
00396 }
00397 if(format == "%voldown"){
00398 format.replace( "%voldown", "xmms-shell -e -" );
00399 }
00400 if(format.contains("%vol")){
00401 vol = xmms_remote_get_main_volume( 0 );
00402 format.replace( "%vol", QString::number(vol) );
00403 }
00404 if(format.contains("%list")){
00405 if(xmms_remote_is_pl_win(0)){
00406 format.replace( "%list", "xmms-shell -e 'window playlist hide'" );
00407 }
00408 else{
00409 format.replace( "%list", "xmms-shell -e 'window playlist show'" );
00410 }
00411 }
00412 if(format.contains("%length")){
00413 format.replace( "%length", QTime( 0,0,0 ).addMSecs( songLength ).toString( "h:mm:ss" ) );
00414 }
00415 if(format.contains("%time")){
00416 format.replace( "%time", QTime( 0,0,0 ).addMSecs( currentTime ).toString( "h:mm:ss" ) );
00417 }
00418 if(isPlaying && format.contains("%remain")){
00419 format.replace( "%remain", QTime( 0,0,0 ).addMSecs( songLength ).addMSecs(-currentTime ).toString( "h:mm:ss" ) );
00420 }
00421 else if(format.contains("%remain")){
00422 format.replace( "%remain", QTime( 0,0,0 ).toString("h:mm:ss" ) );
00423 }
00424 meter->setValue(format);
00425 }
00426 }
00427 }
00428 else{
00429 format = sp->getParam("FORMAT");
00430 if(format == "%onoff"){
00431 format.replace( "%onoff", "xmms" );
00432 meter->setValue(format);
00433 }
00434 else if(format == "%title"){
00435 format.replace( "%title", "Karamba XMMS Sensor" );
00436 meter->setValue(format);
00437 }
00438 else if(format == "%cover" && EnableCover){
00439 format.replace( "%cover", "pics/nocover.png" );
00440 meter->setValue(format);
00441 }
00442 else if( format.contains("%runquit") ){
00443 format.replace("%runquit", "launch");
00444 meter->setValue(format);
00445 }
00446 else{
00447 meter->setValue("");
00448 }
00449 }
00450 #endif // HAVE_XMMS
00451 ++it;
00452 }
00453 delete it;
00454 format = "";
00455 }
00456
00457 void XMMSSensor::setMaxValue( SensorParams *sp){
00458 Meter *meter;
00459 meter = sp->getMeter();
00460 QString f;
00461 f = sp->getParam("FORMAT");
00462
00463 if ( f == "%full" ){
00464 meter->setMax( 1 );
00465 }
00466 }
00467
00468 void XMMSSensor::setKaramba(karamba* k)
00469 {
00470 myKaramba = k;
00471 }
00472
00473 void XMMSSensor::slotLoadConfig( ){
00474 KSimpleConfig conf("karambarc",true);
00475 conf.setGroup("karamba");
00476 Viewer = conf.readEntry("ImgViewer", "kuickshow");
00477 conf.setGroup("XMMS");
00478 HideMainWin = conf.readBoolEntry("HideMainWin",false);
00479 EnableCover = conf.readBoolEntry("EnableCover",false);
00480 EnableID3 = conf.readBoolEntry("EnableId3",false);
00481 SaveID3 = conf.readBoolEntry("SaveId3",false);
00482 EnableCover = conf.readBoolEntry("EnableCover",false);
00483 EnableID3C = conf.readBoolEntry("EnableId3Cover",false);
00484 NetCover = conf.readBoolEntry("EnableNetCover",false);
00485 defH = conf.readNumEntry("CoverHeight",64);
00486 defW = conf.readNumEntry("CoverWidth",64);
00487 }
00488