From: APTX Date: Tue, 1 Sep 2009 17:39:45 +0000 (+0200) Subject: - Make Hash and Client singletons. X-Git-Url: https://gitweb.tyo.aptx.org/?a=commitdiff_plain;h=b9792919359d1b5fb32268ff9464048000309c18;p=anidbudpclient.git - Make Hash and Client singletons. --- diff --git a/client.cpp b/client.cpp index 89c29a5..fdd8082 100644 --- a/client.cpp +++ b/client.cpp @@ -12,6 +12,7 @@ #include "rawcommand.h" #include "logoutcommand.h" #include "uptimecommand.h" +#include "hash.h" #include @@ -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; diff --git a/client.h b/client.h index 7fa97f9..728ba49 100644 --- 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 -Q_SCRIPT_DECLARE_QMETAOBJECT(AniDBUdpClient::Client, QObject*); +//Q_SCRIPT_DECLARE_QMETAOBJECT(AniDBUdpClient::Client, QObject*); #endif // ANIDBUDPCLIENT_H diff --git a/hash.cpp b/hash.cpp index 9141bbb..f569ab5 100644 --- 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 b950162..90e1a77 100644 --- 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