]> Some of my projects - anidbudpclient.git/commitdiff
- Make Hash and Client singletons.
authorAPTX <mail@example.com>
Tue, 1 Sep 2009 17:39:45 +0000 (19:39 +0200)
committerAPTX <mail@example.com>
Tue, 1 Sep 2009 17:39:45 +0000 (19:39 +0200)
client.cpp
client.h
hash.cpp
hash.h

index 89c29a5cdf914509cb783a670ea15497b359c14d..fdd8082f6dcd6df43eca313b9b6e0be60c19a071 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 7fa97f9b4f6b28b88f49ca9f733b856d5b0e0e51..728ba491f0f037daed4d4a914e5d176b0a89ec9a 100644 (file)
--- a/client.h
+++ b/client.h
@@ -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 9141bbb18ab497da8a53d5c9959984cb46a8e250..f569ab542f6e292139a86bbf19814a875bf07423 100644 (file)
--- a/hash.cpp
+++ b/hash.cpp
@@ -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
diff --git a/hash.h b/hash.h
index b950162aba918b0b2ff9e65eeaa7ecf06ce2afae..90e1a77b2934334ef210616ba372fdadd6d5d96b 100644 (file)
--- a/hash.h
+++ b/hash.h
@@ -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