noatunsensor.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 "noatunsensor.h" 00011 00012 NoatunSensor::NoatunSensor( int interval, DCOPClient *c) 00013 : Sensor( interval ) 00014 { 00015 client = c; 00016 noatunID = "noatun"; 00017 } 00018 00019 00020 NoatunSensor::~NoatunSensor() 00021 {} 00022 00023 00024 void NoatunSensor::update() 00025 { 00026 TQString format; 00027 SensorParams *sp; 00028 Meter *meter; 00029 TQObjectListIt it( *objList ); 00030 00031 TQString title; 00032 int songLength = 0; 00033 int currentTime = 0; 00034 00035 bool running = isRunning(); 00036 00037 if( running ) 00038 { 00039 title = getTitle(); 00040 00041 if( title.isEmpty() ) 00042 title = "Noatun"; 00043 currentTime = getTime(); 00044 if( currentTime == -1 ) 00045 currentTime = 0; 00046 00047 songLength = getLength(); 00048 if( songLength == -1 ) 00049 songLength = 0; 00050 } 00051 00052 00053 while (it != 0) 00054 { 00055 sp = (SensorParams*)(*it); 00056 meter = sp->getMeter(); 00057 00058 if( running ) 00059 { 00060 00061 format = sp->getParam("FORMAT"); 00062 if (format.length() == 0 ) 00063 { 00064 format = "%title %time / %length"; 00065 } 00066 00067 if( format.lower() == "%ms" ) 00068 { 00069 meter->setMax( songLength ); 00070 meter->setValue( currentTime ); 00071 } 00072 else 00073 if ( format.lower() == "%full" ) 00074 { 00075 meter->setValue( 1 ); 00076 } 00077 else 00078 00079 { 00080 format.replace( TQRegExp("%title", false), title ); 00081 format.replace( TQRegExp("%id", false), noatunID ); 00082 00083 format.replace( TQRegExp("%length", false), TQTime( 0,0,0 ). 00084 addMSecs( songLength ) 00085 .toString( "h:mm:ss" ) ); 00086 00087 format.replace( TQRegExp("%time", false), TQTime( 0,0,0 ). 00088 addMSecs( currentTime ) 00089 .toString( "h:mm:ss" ) ); 00090 format.replace( TQRegExp("%remain", false), TQTime( 0,0,0 ). 00091 addMSecs( songLength ) 00092 .addMSecs(-currentTime ) 00093 .toString( "h:mm:ss" ) ); 00094 00095 meter->setValue(format); 00096 } 00097 } 00098 else 00099 00100 { 00101 meter->setValue(""); 00102 } 00103 ++it; 00104 00105 00106 } 00107 00108 } 00109 00110 bool NoatunSensor::isRunning() 00111 { 00112 TQRegExp rx("(noatun)|(noatun-\\d+)"); 00113 QCStringList list = client->registeredApplications(); 00114 TQValueList<TQCString>::iterator it; 00115 it = list.begin(); 00116 bool foundNoatun = false; 00117 noatunID = "noatun"; 00118 while( foundNoatun == false && it != list.end() ) 00119 { 00120 if( rx.search( *it ) != -1 ) 00121 { 00122 foundNoatun = true; 00123 noatunID = *it; 00124 } 00125 ++it; 00126 } 00127 return ( client->isApplicationRegistered ( noatunID ) ); 00128 } 00129 00130 00131 TQString NoatunSensor::getTitle() 00132 { 00133 TQByteArray data, replyData; 00134 TQCString replyType; 00135 TQString result; 00136 TQDataStream arg(data, IO_WriteOnly); 00137 arg << 5; 00138 if (!client->call( noatunID, "Noatun", "title()", 00139 data, replyType, replyData)) 00140 { 00141 result = ""; 00142 tqDebug("there was some error using DCOP."); 00143 } 00144 else 00145 { 00146 TQDataStream reply(replyData, IO_ReadOnly); 00147 if (replyType == TQSTRING_OBJECT_NAME_STRING) 00148 { 00149 reply >> result; 00150 result.replace( TQRegExp("_")," " ); 00151 result.replace( TQRegExp(".mp3$"),"" ); 00152 00153 } 00154 else 00155 { 00156 result = ""; 00157 tqDebug("title returned an unexpected type of reply!"); 00158 } 00159 } 00160 return result; 00161 } 00162 00163 00164 int NoatunSensor::getTime() 00165 { 00166 TQByteArray data, replyData; 00167 TQCString replyType; 00168 int result; 00169 TQDataStream arg(data, IO_WriteOnly); 00170 arg << 5; 00171 if (!client->call( noatunID, "Noatun", "position()", 00172 data, replyType, replyData)) 00173 { 00174 result = 0; 00175 tqDebug("there was some error using DCOP."); 00176 } 00177 else 00178 { 00179 TQDataStream reply(replyData, IO_ReadOnly); 00180 if (replyType == "int") 00181 { 00182 reply >> result; 00183 } 00184 else 00185 { 00186 result = 0; 00187 tqDebug("title returned an unexpected type of reply!"); 00188 } 00189 } 00190 return result; 00191 } 00192 00193 00194 int NoatunSensor::getLength() 00195 { 00196 TQByteArray data, replyData; 00197 TQCString replyType; 00198 int result; 00199 TQDataStream arg(data, IO_WriteOnly); 00200 arg << 5; 00201 if (!client->call( noatunID, "Noatun", "length()", 00202 data, replyType, replyData)) 00203 { 00204 result = 0; 00205 tqDebug("there was some error using DCOP."); 00206 } 00207 else 00208 { 00209 TQDataStream reply(replyData, IO_ReadOnly); 00210 if (replyType == "int") 00211 { 00212 reply >> result; 00213 } 00214 else 00215 { 00216 result = 0; 00217 tqDebug("title returned an unexpected type of reply!"); 00218 } 00219 } 00220 return result; 00221 } 00222 00223 00224 void NoatunSensor::setMaxValue( SensorParams *sp) 00225 { 00226 Meter *meter; 00227 meter = sp->getMeter(); 00228 TQString f; 00229 f = sp->getParam("FORMAT"); 00230 00231 if ( f == "%full" ) 00232 meter->setMax( 1 ); 00233 00234 }