]> Some of my projects - aniplayer-old.git/commitdiff
- Make Hash and Client singletons.
authorAPTX <redacted>
Tue, 1 Sep 2009 17:39:45 +0000 (19:39 +0200)
committerAPTX <redacted>
Tue, 1 Sep 2009 17:39:45 +0000 (19:39 +0200)
lib/anidbudpclient/client.cpp
lib/anidbudpclient/client.h
lib/anidbudpclient/hash.cpp
lib/anidbudpclient/hash.h
src/videowindow.cpp

index 89c29a5..fdd8082 100644 (file)
@@ -12,6 +12,7 @@
 #include "rawcommand.h"
 #include "logoutcommand.h"
 #include "uptimecommand.h"
+#include "hash.h"
 
 #include <QtDebug>
 
@@ -154,6 +155,9 @@ Client::~Client()
                sendCommand(new LogoutCommand, true);
                socket->waitForBytesWritten(5);
        }
+
+       if (m_hashInstance)
+               delete m_hashInstance;
 }
 
 QString Client::host() const
@@ -591,6 +595,28 @@ void Client::clearCommandQueue()
        }
 }
 
+Client *Client::instance()
+{
+       if (!m_instance)
+               m_instance = new Client;
+
+       return m_instance;
+}
+
+void Client::destroy()
+{
+       delete m_instance;
+       m_instance = 0;
+}
+
+Hash *Client::hashInstance()
+{
+       if (!m_hashInstance)
+               m_hashInstance = new Hash;
+
+       return m_hashInstance;
+}
+
 void Client::connect()
 {
 qDebug() << "Conneting";
@@ -755,6 +781,8 @@ qDebug() << QString("Generated id %1").arg(result.constData());
        return result;
 }
 
+Client *Client::m_instance = 0;
+
 CommandData::CommandData(AbstractCommand *command, const QByteArray &commandId, bool controlCommand) : QObject()
 {
        this->command = command;
index 7fa97f9..728ba49 100644 (file)
@@ -55,9 +55,11 @@ public:
        static const int clientVersion;
        static const int protocolVersion;
 
+protected:
        Client(QObject *parent = 0);
        virtual ~Client();
 
+public:
        // ------------------ Properties ------------------
        QString host() const;
        void setHost(const QString &host, quint16 port = 0);
@@ -87,6 +89,9 @@ public:
 
        void clearCommandQueue();
 
+       static Client *instance();
+       static void destroy();
+
 public slots:
        void connect();
 
@@ -193,6 +198,7 @@ private:
        LogoutCommand *logoutCommand;
        UptimeCommand *uptimeCommand;
 
+       static Client *m_instance;
 
        static const int UDP_DATAGRAM_MAXIMUM_SIZE = 1400;
 
@@ -241,6 +247,6 @@ private slots:
 } // namespace AniDBUdpClient
 
 #include <QScriptEngine>
-Q_SCRIPT_DECLARE_QMETAOBJECT(AniDBUdpClient::Client, QObject*);
+//Q_SCRIPT_DECLARE_QMETAOBJECT(AniDBUdpClient::Client, QObject*);
 
 #endif // ANIDBUDPCLIENT_H
index 9141bbb..f569ab5 100644 (file)
@@ -18,6 +18,20 @@ Hash::~Hash()
        tearDown();
 }
 
+static Hash *Hash::instance()
+{
+       if (!m_instance)
+               m_instance = new Hash();
+
+       return m_instance;
+}
+
+static void Hash::destroy()
+{
+       delete m_instance;
+       m_instance = 0;
+}
+
 void Hash::hashFile(const QFileInfo &file)
 {
 qDebug() << "Hash::hashFile";
@@ -100,4 +114,6 @@ void Hash::tearDown()
        buffer = 0;
 }
 
+Hash *Hash::m_instance = 0;
+
 } // namesapce AniDBUdpClient
index b950162..90e1a77 100644 (file)
@@ -18,12 +18,16 @@ class ANIDBUDPCLIENTSHARED_EXPORT Hash : public QObject
 
        Q_OBJECT
 
-public:
+protected:
        Hash(QObject *parent = 0);
        ~Hash();
 
+public:
        void hashFile(const QFileInfo &file);
 
+       static Hash *instance();
+       static void destroy();
+
 signals:
        void fileHashed(const QFileInfo &file, const QByteArray &hash);
        void fileProgress(int percent);
@@ -54,6 +58,8 @@ private:
 
        QTime fileTime;
        QTime totalTime;
+
+       static Hash *m_instance;
 };
 
 } // namesapce AniDBUdpClient
index 23695f9..b57e746 100644 (file)
@@ -47,7 +47,7 @@ VideoWindow::VideoWindow(QWidget *parent) : QMainWindow(parent)
        destroyed = menuMoving = windowMoving = m_closeOnStop = false;
 
 #ifndef NO_ANIDBUDPCLIENT
-       anidb = new AniDBUdpClient::Client(this);
+       anidb = AniDBUdpClient::Client::instance();
        addCommand = 0;
        m_marked = true;
        m_automark = 0;
@@ -184,6 +184,9 @@ VideoWindow::~VideoWindow()
 
        hide();
        menu->hide();
+
+       anidb->destroy();
+       anidb = 0;
 }
 
 QString VideoWindow::currentFile() const