From def3e3d868ff532366ee6879a4b01fd9aa847cb5 Mon Sep 17 00:00:00 2001
From: Dominik Schmidt <dev@dominik-schmidt.de>
Date: Wed, 20 Apr 2011 20:22:06 +0200
Subject: [PATCH] Move SipHandler from application to library

---
 src/CMakeLists.txt                       |  3 -
 src/libtomahawk/CMakeLists.txt           |  2 +
 src/{ => libtomahawk}/sip/SipHandler.cpp | 71 +++++++++++++++++++++++-
 src/{ => libtomahawk}/sip/SipHandler.h   | 16 +++++-
 4 files changed, 84 insertions(+), 8 deletions(-)
 rename src/{ => libtomahawk}/sip/SipHandler.cpp (83%)
 rename src/{ => libtomahawk}/sip/SipHandler.h (85%)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 067558b19..1c3f3a8e7 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -32,8 +32,6 @@ ENDIF()
 #ENDFOREACH( moddir )
 
 SET( tomahawkSources ${tomahawkSources}
-     sip/SipHandler.cpp
-
      web/api_v1.cpp
 
      resolvers/scriptresolver.cpp
@@ -71,7 +69,6 @@ SET( tomahawkSourcesGui ${tomahawkSourcesGui}
 SET( tomahawkHeaders ${tomahawkHeaders}
      "${TOMAHAWK_INC_DIR}/tomahawk/tomahawkapp.h"
 
-     sip/SipHandler.h
 
      web/api_v1.h
 
diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt
index 33aaff411..01a18a12a 100644
--- a/src/libtomahawk/CMakeLists.txt
+++ b/src/libtomahawk/CMakeLists.txt
@@ -29,6 +29,7 @@ set( libSources
     viewpage.cpp
 
     sip/SipPlugin.cpp
+    sip/SipHandler.cpp
 
     audio/madtranscode.cpp
     audio/dummytranscode.cpp
@@ -187,6 +188,7 @@ set( libHeaders
     playlist.h
 
     sip/SipPlugin.h
+    sip/SipHandler.h
 
     audio/transcodeinterface.h
     audio/madtranscode.h
diff --git a/src/sip/SipHandler.cpp b/src/libtomahawk/sip/SipHandler.cpp
similarity index 83%
rename from src/sip/SipHandler.cpp
rename to src/libtomahawk/sip/SipHandler.cpp
index 59aa8ea12..0170bdf64 100644
--- a/src/sip/SipHandler.cpp
+++ b/src/libtomahawk/sip/SipHandler.cpp
@@ -1,5 +1,5 @@
 /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
- * 
+ *
  *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  *
  *   Tomahawk is free software: you can redistribute it and/or modify
@@ -24,19 +24,33 @@
 #include <QPluginLoader>
 #include <QMessageBox>
 
+#include "functimeout.h"
+
 #include "database/database.h"
 #include "network/controlconnection.h"
 #include "sourcelist.h"
 #include "tomahawksettings.h"
-#include "tomahawk/tomahawkapp.h"
+//#include "tomahawk/tomahawkapp.h"
 
 #include "config.h"
 
 
+//remove
+#include <QLabel>
+
+SipHandler* SipHandler::s_instance = 0;
+
+SipHandler* SipHandler::instance()
+{
+    return s_instance;
+}
+
 SipHandler::SipHandler( QObject* parent )
     : QObject( parent )
     , m_connected( false )
 {
+    s_instance = this;
+
     loadPlugins( findPlugins() );
 
     connect( TomahawkSettings::instance(), SIGNAL( changed() ), SLOT( onSettingsChanged() ) );
@@ -55,6 +69,23 @@ SipHandler::plugins() const
     return m_plugins;
 }
 
+const QPixmap SipHandler::avatar( const QString& name ) const
+{
+    qDebug() << Q_FUNC_INFO << "Getting avatar" << name << m_usernameAvatars.keys();
+    if( m_usernameAvatars.keys().contains( name ) )
+    {
+        qDebug() << Q_FUNC_INFO << "Getting avatar and avatar != null ";
+        Q_ASSERT(!m_usernameAvatars.value( name ).isNull());
+        return m_usernameAvatars.value( name );
+    }
+    else
+    {
+        qDebug() << Q_FUNC_INFO << "Getting avatar and avatar == null, GAAAAAH ";
+        return QPixmap();
+    }
+}
+
+
 
 void
 SipHandler::onSettingsChanged()
@@ -147,6 +178,7 @@ SipHandler::loadPlugin( const QString& path )
         QObject::connect( sip, SIGNAL( disconnected() ), SIGNAL( disconnected() ) );
         QObject::connect( sip, SIGNAL( error( int, QString ) ), SLOT( onError( int, QString ) ) );
 
+        QObject::connect( sip, SIGNAL( avatarReceived( QString, QPixmap ) ), SLOT( onAvatarReceived( QString, QPixmap ) ) );
         m_plugins << sip;
     }
 }
@@ -182,7 +214,8 @@ SipHandler::connectPlugins( bool startup, const QString &pluginName )
     if ( !TomahawkSettings::instance()->acceptedLegalWarning() )
     {
         int result = QMessageBox::question(
-            TomahawkApp::instance()->mainWindow(), tr( "Legal Warning" ),
+            //TomahawkApp::instance()->mainWindow(),
+            0, tr( "Legal Warning" ),
             tr( "By pressing OK below, you agree that your use of Tomahawk will be in accordance with any applicable laws, including copyright and intellectual property laws, in effect in your country of residence, and indemnify the Tomahawk developers and project from liability should you choose to break those laws.\n\nFor more information, please see http://gettomahawk.com/legal" ),
             tr( "I Do Not Agree" ), tr( "I Agree" )
         );
@@ -346,3 +379,35 @@ SipHandler::onError( int code, const QString& msg )
         QTimer::singleShot( 10000, sip, SLOT( connectPlugin() ) );
     }
 }
+
+void SipHandler::onAvatarReceived( const QString& from, const QPixmap& avatar )
+{
+    qDebug() << Q_FUNC_INFO << "Set avatar on source for " << from;
+    Q_ASSERT(!avatar.isNull());
+
+    m_usernameAvatars.insert( from, avatar );
+
+    //
+
+    //Tomahawk::source_ptr source = ->source();
+    ControlConnection *conn = Servent::instance()->lookupControlConnection( from );
+    if( conn )
+    {
+        qDebug() << Q_FUNC_INFO << from << "got control connection";
+        Tomahawk::source_ptr source = conn->source();
+        if( source )
+        {
+
+            qDebug() << Q_FUNC_INFO << from << "got source, setting avatar";
+            source->setAvatar( avatar );
+        }
+        else
+        {
+            qDebug() << Q_FUNC_INFO << from << "no source found, not setting avatar";
+        }
+    }
+    else
+    {
+        qDebug() << Q_FUNC_INFO << from << "no control connection setup yet";
+    }
+}
diff --git a/src/sip/SipHandler.h b/src/libtomahawk/sip/SipHandler.h
similarity index 85%
rename from src/sip/SipHandler.h
rename to src/libtomahawk/sip/SipHandler.h
index 0d3dcfa63..d394ea5d1 100644
--- a/src/sip/SipHandler.h
+++ b/src/libtomahawk/sip/SipHandler.h
@@ -1,5 +1,5 @@
 /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
- * 
+ *
  *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  *
  *   Tomahawk is free software: you can redistribute it and/or modify
@@ -23,19 +23,24 @@
 
 #include <QDebug>
 #include <QObject>
+#include <QHash>
+#include <QPixmap>
+#include <QString>
 
 class SipHandler : public QObject
 {
     Q_OBJECT
 
 public:
-//    static SipHandler* instance() { return s_instance ? s_instance : new SipHandler(); }
+    static SipHandler* instance();
 
     SipHandler( QObject* parent );
     ~SipHandler();
 
     QList< SipPlugin* > plugins() const;
 
+    const QPixmap avatar( const QString& name ) const;
+
 public slots:
     void addContact( const QString& id ) { qDebug() << Q_FUNC_INFO << id; }
 
@@ -57,7 +62,11 @@ private slots:
 
     void onSettingsChanged();
 
+    void onAvatarReceived( const QString& from, const QPixmap& avatar = QPixmap());
+
 private:
+    static SipHandler *s_instance;
+
     QStringList findPlugins();
     bool pluginLoaded( const QString& name ) const;
 
@@ -66,6 +75,9 @@ private:
 
     QList< SipPlugin* > m_plugins;
     bool m_connected;
+
+
+    QHash<QString, QPixmap> m_usernameAvatars;
 };
 
 #endif