File indexing completed on 2024-05-19 04:50:21
0001 /**************************************************************************************** 0002 * Copyright (c) 2008 Casey Link <unnamedrambler@gmail.com> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify it under * 0005 * the terms of the GNU General Public License as published by the Free Software * 0006 * Foundation; either version 2 of the License, or (at your option) any later * 0007 * version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0011 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0012 * * 0013 * You should have received a copy of the GNU General Public License along with * 0014 * this program. If not, see <http://www.gnu.org/licenses/>. * 0015 ****************************************************************************************/ 0016 0017 #include "Mp3tunesHarmonyHandler.h" 0018 #include "mp3tunesharmonyhandleradaptor.h" 0019 #include "App.h" 0020 #include "core/support/Debug.h" 0021 0022 #include <QList> 0023 #include <QVariant> 0024 0025 #include <unistd.h> 0026 0027 0028 Mp3tunesHarmonyHandler::Mp3tunesHarmonyHandler( QString identifier, 0029 QString email, 0030 QString pin ) 0031 : QObject( qApp ) 0032 , m_daemon( 0 ) 0033 , m_identifier( identifier ) 0034 , m_email( email ) 0035 , m_pin( pin ) 0036 { 0037 new Mp3tunesHarmonyHandlerAdaptor( this ); 0038 QDBusConnection::sessionBus().registerObject("/Mp3tunesHarmonyHandler", this); 0039 debug() << "All aboard the DBUS!"; 0040 } 0041 Mp3tunesHarmonyHandler::~Mp3tunesHarmonyHandler() 0042 { 0043 stopDaemon(); 0044 if( m_daemon ) 0045 delete m_daemon; 0046 } 0047 0048 bool Mp3tunesHarmonyHandler::startDaemon() 0049 { 0050 m_daemon = new AmarokProcess( this ); 0051 if( m_email.isEmpty() && m_pin.isEmpty() ) 0052 *m_daemon << "amarokmp3tunesharmonydaemon" << m_identifier; 0053 else if( !m_email.isEmpty() && !m_pin.isEmpty() ) 0054 *m_daemon << "amarokmp3tunesharmonydaemon" << m_identifier << m_email << m_pin; 0055 m_daemon->setOutputChannelMode( KProcess::OnlyStdoutChannel ); 0056 connect( m_daemon, QOverload<int>::of( &QProcess::finished ), this, &Mp3tunesHarmonyHandler::slotFinished ); 0057 connect( m_daemon, QOverload<QProcess::ProcessError>::of( &AmarokProcess::error ), this, &Mp3tunesHarmonyHandler::slotError ); 0058 m_daemon->start(); 0059 sleep(3); // sleep for 3 seconds to allow the process to start and register. 0060 return m_daemon->waitForStarted( -1 ); 0061 } 0062 0063 void Mp3tunesHarmonyHandler::stopDaemon() 0064 { 0065 if( daemonRunning() ) 0066 m_daemon->close(); 0067 } 0068 0069 void 0070 Mp3tunesHarmonyHandler::slotFinished( ) 0071 { 0072 m_daemon->deleteLater(); 0073 m_daemon = 0; 0074 } 0075 0076 void 0077 Mp3tunesHarmonyHandler::slotError( QProcess::ProcessError error ) 0078 { 0079 if( error == QProcess::Crashed ) 0080 { 0081 //handleRestart(); 0082 } 0083 } 0084 0085 bool Mp3tunesHarmonyHandler::daemonRunning() 0086 { 0087 if( !m_daemon ) 0088 return false; 0089 debug() << "Daemon process is running"; 0090 return true; 0091 } 0092 0093 bool Mp3tunesHarmonyHandler::daemonConnected() 0094 { 0095 DEBUG_BLOCK 0096 if( !daemonRunning() ) 0097 return false; 0098 QString name = "org.kde.amarok.Mp3tunesHarmonyDaemon-" + QString::number( m_daemon->pid() ); 0099 debug() << "Making Dbus call about daemonConnected to: " << name; 0100 QDBusMessage m = QDBusMessage::createMethodCall( name, 0101 "/Mp3tunesHarmonyDaemon", 0102 "", 0103 "daemonConnected" ); 0104 QDBusMessage response = QDBusConnection::sessionBus().call( m ); 0105 if( response.type() == QDBusMessage::ErrorMessage ) 0106 { 0107 debug() << "Got ERROR response daemonConnected"; 0108 debug() << response.errorName() << ": " << response.errorMessage(); 0109 } 0110 QList<QVariant> args = response.arguments(); 0111 if( args.count() == 1) 0112 { 0113 if( args[0].toString() == "true" ) { 0114 debug() << "Daemon Connected"; 0115 return true; 0116 } else if( args[0].toString() == "false" ) { 0117 debug() << "Daemon Not Connected"; 0118 return false; 0119 } 0120 } 0121 debug() << "Unexpected DBUS return. " << args.count(); 0122 return false; 0123 } 0124 0125 void Mp3tunesHarmonyHandler::makeConnection() 0126 { 0127 DEBUG_BLOCK 0128 if( !daemonRunning() ) 0129 return; 0130 QString name = "org.kde.amarok.Mp3tunesHarmonyDaemon-" + QString::number( m_daemon->pid() ); 0131 debug() << "Making Dbus call about makeConnection to: " << name; 0132 QDBusMessage m = QDBusMessage::createMethodCall( name, 0133 "/Mp3tunesHarmonyDaemon", 0134 "", 0135 "makeConnection" ); 0136 QDBusMessage response = QDBusConnection::sessionBus().call( m ); 0137 if( response.type() == QDBusMessage::ErrorMessage ) 0138 { 0139 debug() << "Got ERROR response makeConnection"; 0140 debug() << response.errorName() << ": " << response.errorMessage(); 0141 } 0142 } 0143 0144 void Mp3tunesHarmonyHandler::breakConnection() 0145 { 0146 DEBUG_BLOCK 0147 if( !daemonRunning() ) 0148 return; 0149 QString name = "org.kde.amarok.Mp3tunesHarmonyDaemon-" + QString::number( m_daemon->pid() ); 0150 //QString name = "org.kde.amarok.Mp3tunesHarmonyDaemon"; 0151 debug() << "Making Dbus call about breakConnection to: " << name; 0152 QDBusMessage m = QDBusMessage::createMethodCall( name, 0153 "/Mp3tunesHarmonyDaemon", 0154 "", 0155 "breakConnection" ); 0156 QDBusMessage response = QDBusConnection::sessionBus().call( m ); 0157 if( response.type() == QDBusMessage::ErrorMessage ) 0158 { 0159 debug() << "Got ERROR response "; 0160 debug() << response.errorName() << ": " << response.errorMessage(); 0161 } 0162 } 0163 0164 QString Mp3tunesHarmonyHandler::pin() 0165 { 0166 DEBUG_BLOCK 0167 if( !daemonRunning() ) 0168 return QString(); 0169 QString name = "org.kde.amarok.Mp3tunesHarmonyDaemon-" + QString::number( m_daemon->pid() ); 0170 //QString name = "org.kde.amarok.Mp3tunesHarmonyDaemon"; 0171 debug() << "Making Dbus call about pin to: " << name; 0172 QDBusMessage m = QDBusMessage::createMethodCall( name, 0173 "/Mp3tunesHarmonyDaemon", 0174 "", 0175 "pin" ); 0176 QDBusMessage response = QDBusConnection::sessionBus().call( m ); 0177 if( response.type() == QDBusMessage::ErrorMessage ) 0178 { 0179 debug() << "Got ERROR response pin"; 0180 debug() << response.errorName() << ": " << response.errorMessage(); 0181 } 0182 QList<QVariant> args = response.arguments(); 0183 if( args.count() == 1) 0184 { 0185 return args[0].toString(); 0186 } 0187 return QString(); 0188 } 0189 0190 QString Mp3tunesHarmonyHandler::email() 0191 { 0192 DEBUG_BLOCK 0193 if( !daemonRunning() ) 0194 return QString(); 0195 QString name = "org.kde.amarok.Mp3tunesHarmonyDaemon-" + QString::number( m_daemon->pid() ); 0196 //QString name = "org.kde.amarok.Mp3tunesHarmonyDaemon"; 0197 debug() << "Making Dbus call about email to: " << name; 0198 QDBusMessage m = QDBusMessage::createMethodCall( name, 0199 "/Mp3tunesHarmonyDaemon", 0200 "", 0201 "email" ); 0202 QDBusMessage response = QDBusConnection::sessionBus().call( m ); 0203 if( response.type() == QDBusMessage::ErrorMessage ) 0204 { 0205 debug() << "Got ERROR response email"; 0206 debug() << response.errorName() << ": " << response.errorMessage(); 0207 } 0208 QList<QVariant> args = response.arguments(); 0209 if( args.count() == 1) 0210 { 0211 return args[0].toString(); 0212 } 0213 return QString(); 0214 } 0215 0216 void 0217 Mp3tunesHarmonyHandler::emitError( const QString &error ) 0218 { 0219 emit( signalError( error ) ); 0220 } 0221 0222 void 0223 Mp3tunesHarmonyHandler::emitWaitingForEmail( const QString &pin ) 0224 { 0225 emit( waitingForEmail( pin ) ); 0226 } 0227 0228 void 0229 Mp3tunesHarmonyHandler::emitWaitingForPin() 0230 { 0231 emit( waitingForPin() ); 0232 } 0233 0234 void 0235 Mp3tunesHarmonyHandler::emitConnected() 0236 { 0237 emit( connected() ); 0238 } 0239 0240 void 0241 Mp3tunesHarmonyHandler::emitDisconnected() 0242 { 0243 emit( disconnected() ); 0244 } 0245 0246 void 0247 Mp3tunesHarmonyHandler::emitDownloadReady( const QVariantMap &download ) 0248 { 0249 emit( downloadReady( download ) ); 0250 } 0251 0252 void 0253 Mp3tunesHarmonyHandler::emitDownloadPending( const QVariantMap &download ) 0254 { 0255 emit( downloadReady( download ) ); 0256 }