]> Some of my projects - aniplayer-old.git/commitdiff
Should all be working just like before the rewrite. Commands with waitForResult(...
authorAPTX <marek321@gmail.com>
Sun, 30 May 2010 10:33:58 +0000 (12:33 +0200)
committerAPTX <marek321@gmail.com>
Sun, 30 May 2010 10:33:58 +0000 (12:33 +0200)
lib/anidbudpclient/abstractcommand.h
lib/anidbudpclient/client.cpp
lib/anidbudpclient/client.h
lib/anidbudpclient/file.cpp
lib/anidbudpclient/file.h
lib/anidbudpclient/uptimecommand.cpp
lib/anidbudpclient/uptimecommand.h
src/videowindow.cpp
src/videowindow.h

index 4d41abfcf53cc8bce77f0d4ab8daa42cc202a669..62cad5412335838bc12f985c9e41db49f9774e74 100644 (file)
@@ -26,29 +26,28 @@ public:
 
        virtual Command rawCommand() const;
 
+       /**
+         If it's false the client takes ownership of the request!
+        **/
        virtual bool waitForResult() const;
        virtual bool requiresSession() const;
 };
 
+#define REPLY_DEFINITION_HELPER_INTERNAL(name, constructor) \
+friend class Client; \
+public: \
+typedef name##Command CommandType; \
+private: \
+CommandType m_command; \
+Client *m_client; \
+name##Reply(const CommandType command, const QByteArray &id, Client *client, QObject *parent) : m_command(command), m_client(client), AbstractReply(id, client, parent) {constructor} \
+inline const CommandType &command() const { return m_command; }
+
 #define REPLY_DEFINITION_HELPER(name) \
-               friend class Client; \
-       public: \
-               typedef name##Command CommandType; \
-       private: \
-               CommandType m_command; \
-               Client *m_client; \
-               name##Reply(const CommandType command, const QByteArray &id, Client *client, QObject *parent) : m_command(command), m_client(client), AbstractReply(id, client, parent) {m_commandPtr = &m_command;} \
-               inline const CommandType &command() const { return m_command; }
+               REPLY_DEFINITION_HELPER_INTERNAL(name, m_commandPtr = &m_command;)
 
 #define REPLY_DEFINITION_HELPER2(name) \
-               friend class Client; \
-       public: \
-               typedef name##Command CommandType; \
-       private: \
-               CommandType m_command; \
-               Client *m_client; \
-               name##Reply(const CommandType command, const QByteArray &id, Client *client, QObject *parent) : m_command(command), m_client(client), AbstractReply(id, client, parent) {m_commandPtr = &m_command; init();} \
-               inline const CommandType &command() const { return m_command; }
+               REPLY_DEFINITION_HELPER_INTERNAL(name, m_commandPtr = &m_command; init();)
 
 class ANIDBUDPCLIENTSHARED_EXPORT AbstractReply : public QObject
 {
index 1bb5acaadfbdeee69b44867c8d95e58c3f2d6e67..e6705adf7641241075c8ceed6124dc93b195cb86 100644 (file)
@@ -27,6 +27,7 @@ Client::Client(QObject *parent) : QObject(parent)
 qDebug() << "Api instance init!";
 
        authReply = 0;
+       uptimeReply = 0;
 
        m_error = NoError;
        m_idlePolicy = DoNothingIdlePolicy;
@@ -129,27 +130,35 @@ qDebug() << "Api instance init!";
 
 Client::~Client()
 {
-       disconnect();
-       clearCommandQueue();
-
        foreach (CommandData *cmd, sentCommands)
        {
-               if (!cmd->command->command().waitForResult())
-               {
-                       // Send CLIENT_DESTROYED to indicate that no real reply will come.
-                       cmd->command->setRawReply(CLIENT_DESTROYED, "");
-               }
+               // Send CLIENT_DESTROYED to indicate that no real reply will come.
+               cmd->command->setRawReply(CLIENT_DESTROYED, "");
        }
+       sentCommands.clear();
+
+       clearCommandQueue();
 
        if (!m_sessionId.isEmpty())
        {
                while (commandTimer->isActive())
                        QCoreApplication::processEvents();
 
-               sendCommand(createReply(LogoutCommand()), true);
+               logout(true);
                socket->waitForBytesWritten(5);
        }
+       disconnect();
 
+       if (authReply)
+       {
+               delete authReply;
+               authReply = 0;
+       }
+       if (uptimeReply)
+       {
+               delete uptimeReply;
+               uptimeReply = 0;
+       }
 }
 
 QString Client::host() const
@@ -394,7 +403,7 @@ qDebug() << m_idlePolicy;
                        idleTimer->start(UDP_API_INACTIVITY_UPDATE * 1000);
                break;
                case ImmediateLogoutIdlePolicy:
-                       enqueueControlCommand(logoutReply);
+                       logout(true);
                break;
                default:
                break;
@@ -532,7 +541,7 @@ qDebug() << "COMPRESSED DATAGRAM = " << tmp;
                        case LOGIN_FIRST:
                        case INVALID_SESSION:
 qDebug() << "LOGIN FIRST required, authing";
-                               m_sessionId = "";
+                               m_sessionId.clear();
                                if (controlCommand)
                                        enqueueControlCommand(cmd);
                                else
@@ -541,7 +550,7 @@ qDebug() << "LOGIN FIRST required, authing";
                                goto continueLoop;
                        break;
                        case LOGGED_OUT:
-                               m_sessionId = "";
+                               m_sessionId.clear();
                        break;
                        case BANNED:
                                m_error = BannedError;
@@ -578,6 +587,10 @@ qDebug() << "LOGIN FIRST required, authing";
                reply = reply.mid(10);
 
                cmd->setRawReply(replyCode, reply);
+
+               // Delete if command is owned by the client.
+               if (!cmd->command().waitForResult())
+                       delete cmd;
 continueLoop:
                ;
        }
@@ -600,7 +613,7 @@ void Client::clearCommandQueue()
                AbstractReply *reply = commandQueue.dequeue();
                if (!reply->command().waitForResult())
                {
-                       // These would be deleted anyway
+                       // These are owned by the client
                        delete reply;
                }
                else
@@ -615,7 +628,7 @@ void Client::clearCommandQueue()
                AbstractReply *reply = commandQueue.dequeue();
                if (!reply->command().waitForResult())
                {
-                       // These would be deleted anyway
+                       // These are owned by the client
                        delete reply;
                }
                else
@@ -680,8 +693,14 @@ void Client::cancel(AbstractReply *reply)
 
 void Client::logout()
 {
-       if (!m_sessionId.isEmpty())
-               enqueueControlCommand(logoutReply);
+       logout(false);
+}
+
+void Client::logout(bool force)
+{
+       if (m_sessionId.isEmpty())
+               return;
+       enqueueControlCommand(createReply(LogoutCommand()), force);
 }
 
 void Client::commandTimeout(const QByteArray &commandId)
index f836c018042d55d845edeedcd964b7e576d5b396..1d197ffb4a23cfba4c70d3ee6a0f2cefba788e4b 100644 (file)
@@ -173,6 +173,7 @@ private:
        void enqueueControlCommand(AbstractReply *command, bool first = false);
        void sendCommand(AbstractReply *command, bool controlCommand = false);
 
+       void logout(bool force);
 
        QByteArray buildCmd(const QString &cmd, const QVariantMap &args);
        QByteArray nextCommandId(int len = 5);
@@ -209,7 +210,6 @@ private:
 
        AuthCommand authCommand;
        AuthReply *authReply;
-       LogoutReply *logoutReply;
        UptimeReply *uptimeReply;
 
        static Client *m_instance;
index a8532cefb5108324714621f7a44fba1a0266eed6..c8acde73c83c0717e67b1f3632cc61a1d414fdef 100644 (file)
@@ -21,6 +21,11 @@ File::File(const QFileInfo &file, QObject *parent) : QObject(parent)
 
 File::~File()
 {
+       if (markReply)
+       {
+               delete markReply;
+               markReply = 0;
+       }
        if (fileReply)
        {
                delete fileReply;
@@ -117,16 +122,21 @@ bool File::addToMyList()
        return true;
 }
 
-bool File::markWatched(bool watched)
+bool File::markWatched()
 {
-       if (m_markingState != Success)
+       if (m_markingState == Success)
                return true;
 
        if (m_hashingState != Success)
                actionsQueue.enqueue(Hashing);
 
+       if (m_addingState != Success)
+               actionsQueue.enqueue(Adding);
+
+       actionsQueue.enqueue(MarkingWatched);
+
        if (notWorking) work();
-       return watched;
+       return true;
 }
 
 
@@ -201,7 +211,12 @@ void File::finishAdding(bool success)
 
 void File::finishMarking(bool success)
 {
-       Q_UNUSED(success);
+       if (!success)
+       {
+               updateStatus(MarkingWatched, Failure);
+               return;
+       }
+       updateStatus(MarkingWatched, Success);
 }
 
 void File::work()
@@ -214,6 +229,7 @@ qDebug() << actionsQueue;
        if (actionsQueue.isEmpty())
        {
                emit statusUpdate(All, Finished);
+               emit finished();
                notWorking = true;
                return;
        }
@@ -333,6 +349,18 @@ void File::startMarking()
                work();
                return;
        }
+       MyListAddCommand markCommand(0);
+       if (addReply->lid())
+               markCommand = MyListAddCommand(addReply->lid());
+       else
+               markCommand = MyListAddCommand(m_ed2k, size(), true);
+       markCommand.setViewed(true);
+
+       if (markReply) markReply->deleteLater();
+       markReply = Client::instance()->send(markCommand);
+       connect(markReply, SIGNAL(replyReady(bool)), this, SLOT(finishMarking(bool)));
+
+       updateStatus(MarkingWatched, InProgress);
 }
 
 void File::init()
@@ -340,6 +368,7 @@ void File::init()
        hashResult = 0;
        fileReply = 0;
        addReply = 0;
+       markReply = 0;
        m_hashingState = m_renamingState = m_addingState = m_markingState = NotStarted;
        notWorking = true;
 
index 9118a0d8e2519fda8fcc18fd0dc15a37aecf5dd1..0d83544a835747323eb47cfa3902795cc0909be4 100644 (file)
@@ -73,10 +73,11 @@ public slots:
        void hash();
        bool rename();
        bool addToMyList();
-       bool markWatched(bool watched = true);
+       bool markWatched();
 
 signals:
        void statusUpdate(AniDBUdpClient::File::Action action, AniDBUdpClient::File::ActionState state);
+       void finished();
 
 private slots:
        void finishHashing();
@@ -114,6 +115,7 @@ private:
        HashResult *hashResult;
        FileReply *fileReply;
        MyListAddReply *addReply;
+       MyListAddReply *markReply;
 
 };
 
index b4c05f6e00c41e6cf5f69665b98ded5ae3a8f34c..f45ebaa234f5475fd588cf1580ee6162415eb7ca 100644 (file)
@@ -6,6 +6,11 @@ UptimeCommand::UptimeCommand() : AbstractCommand()
 {
 }
 
+bool UptimeCommand::waitForResult() const
+{
+       return true;
+}
+
 Command UptimeCommand::rawCommand() const
 {
        Command command;
index 7b3aaa2cbd8b560919cac8dafaee0744a5563385..4a01766cc2504dc0890e886be24a2578081d7a09 100644 (file)
@@ -13,6 +13,7 @@ public:
        typedef UptimeReply ReplyType;
        UptimeCommand();
 
+       bool waitForResult() const;
        Command rawCommand() const;
 };
 
index 2945a546324ac584b2e5247ab98f4abd47a08618..9e0ff5f4a174706e1c852e6c68e02bffa1d37e16 100644 (file)
@@ -25,9 +25,6 @@
 
 #ifndef NO_ANIDBUDPCLIENT
 #      include <AniDBUdpClient/Client>
-#      include <AniDBUdpClient/RawCommand>
-#      include <AniDBUdpClient/MyListAddCommand>
-#      include <AniDBUdpClient/Hash>
 #endif
 
 #include <QDebug>
@@ -48,8 +45,6 @@ VideoWindow::VideoWindow(QWidget *parent) : QMainWindow(parent)
        destroyed = menuMoving = windowMoving = m_closeOnStop = false;
 
 #ifndef NO_ANIDBUDPCLIENT
-       anidb = AniDBUdpClient::Client::instance();
-       addReply = 0;
        m_marked = true;
        m_automark = 0;
 #endif
@@ -171,8 +166,8 @@ VideoWindow::VideoWindow(QWidget *parent) : QMainWindow(parent)
 #endif
 
 #ifndef NO_ANIDBUDPCLIENT
-       anidb->setCompression(true);
-       anidb->setIdlePolicy(AniDBUdpClient::ImmediateLogoutIdlePolicy);
+       AniDBUdpClient::Client::instance()->setCompression(true);
+       AniDBUdpClient::Client::instance()->setIdlePolicy(AniDBUdpClient::ImmediateLogoutIdlePolicy);
 #endif
 
 }
@@ -187,8 +182,6 @@ VideoWindow::~VideoWindow()
 
        hide();
        menu->hide();
-
-       anidb = 0;
 }
 
 QString VideoWindow::currentFile() const
@@ -538,59 +531,61 @@ void VideoWindow::moveWithMenu()
 #ifndef NO_ANIDBUDPCLIENT
 void VideoWindow::markWatched()
 {
-       if (addReply != 0)
+       if (m_marked)
        {
-               menu->showMessage("File already Marked");
+               menu->showMessage(tr("File already marked"));
                return;
        }
-       menu->showMessage("Hashing file");
-       AniDBUdpClient::HashRequest hashRequest(videoPlayer->currentFile());
-
-       AniDBUdpClient::HashResult *hashResult = AniDBUdpClient::Hash::instance()->hashFile(hashRequest);
-       qDebug() << "hashing file...";
-       connect(hashResult, SIGNAL(resultReady()), this, SLOT(doMarkWatched()));
-}
-
-void VideoWindow::doMarkWatched()
-{
-       AniDBUdpClient::HashResult *hashResult = qobject_cast<AniDBUdpClient::HashResult *>(sender());
-       if (!hashResult)
-               return;
-       AniDBUdpClient::MyListAddCommand addCommand(hashResult->hash(), QFileInfo(videoPlayer->currentFile()).size(), false);
-       AniDBUdpClient::MyListAddReply *addReply = anidb->send(addCommand);
-       connect(addReply, SIGNAL(replyReady(bool)), this, SLOT(showMarkResult(bool)));
-       connect(addReply, SIGNAL(replyReady(bool)), this, SLOT(deleteLater()));
+       menu->showMessage(tr("Starting marking file as watched."));
 
-       menu->showMessage(tr("Marking file %1").arg(hashResult->fileInfo().fileName()));
-       hashResult->deleteLater();
+       AniDBUdpClient::File *file = new AniDBUdpClient::File(videoPlayer->currentFile());
+       file->markWatched();
+       connect(file, SIGNAL(statusUpdate(AniDBUdpClient::File::Action,AniDBUdpClient::File::ActionState)), this, SLOT(markingStatus(AniDBUdpClient::File::Action,AniDBUdpClient::File::ActionState)));
+       connect(file, SIGNAL(finished()), file, SLOT(deleteLater()));
 }
 
-void VideoWindow::showMarkResult(bool success)
+void VideoWindow::markingStatus(AniDBUdpClient::File::Action action, AniDBUdpClient::File::ActionState actionState)
 {
-       AniDBUdpClient::MyListAddReply *cmd = qobject_cast<AniDBUdpClient::MyListAddReply *>(sender());
-       if (!cmd)
-               return;
-
-       if (success)
-       {
-               menu->showMessage(tr("File marked watched"));
-       }
-       else
+       AniDBUdpClient::File *file = (AniDBUdpClient::File *) sender();
+       switch (action)
        {
-               menu->showMessage(tr("Failed to mark file"));
+               case AniDBUdpClient::File::Hashing:
+                       switch (actionState)
+                       {
+                               case AniDBUdpClient::File::InProgress:
+                                       menu->showMessage(tr("Hashing File"));
+                               break;
+                               default: ;
+                       }
+               break;
+               case AniDBUdpClient::File::MarkingWatched:
+                       switch (actionState)
+                       {
+                               case AniDBUdpClient::File::InProgress:
+                                       menu->showMessage(tr("Marking File"));
+                               break;
+                               case AniDBUdpClient::File::Success:
+                                       menu->showMessage(tr("File %1 marked watched").arg(file->file().fileName()));
+                               break;
+                               case AniDBUdpClient::File::Failure:
+                                       menu->showMessage(tr("Failed to mark file %1 as watched").arg(file->file().fileName()));
+                               break;
+                               default: ;
+                       }
+               break;
+               default: ;
        }
-       cmd->deleteLater();
 }
 
 void VideoWindow::anidbSettings()
 {
        AniDBConfigDialog dialog(this);
 
-       dialog.setHost(anidb->host());
-       dialog.setHostPort(anidb->hostPort());
-       dialog.setLocalPort(anidb->localPort());
-       dialog.setUser(anidb->user());
-       dialog.setPass(anidb->pass());
+       dialog.setHost(AniDBUdpClient::Client::instance()->host());
+       dialog.setHostPort(AniDBUdpClient::Client::instance()->hostPort());
+       dialog.setLocalPort(AniDBUdpClient::Client::instance()->localPort());
+       dialog.setUser(AniDBUdpClient::Client::instance()->user());
+       dialog.setPass(AniDBUdpClient::Client::instance()->pass());
        dialog.setAutomark(m_automark);
        dialog.setPaths(m_automarkPaths);
 
@@ -599,13 +594,13 @@ void VideoWindow::anidbSettings()
                return;
        }
 
-       anidb->disconnect();
+       AniDBUdpClient::Client::instance()->disconnect();
 
-       anidb->setHost(dialog.host());
-       anidb->setHostPort(dialog.hostPort());
-       anidb->setLocalPort(dialog.localPort());
-       anidb->setUser(dialog.user());
-       anidb->setPass(dialog.pass());
+       AniDBUdpClient::Client::instance()->setHost(dialog.host());
+       AniDBUdpClient::Client::instance()->setHostPort(dialog.hostPort());
+       AniDBUdpClient::Client::instance()->setLocalPort(dialog.localPort());
+       AniDBUdpClient::Client::instance()->setUser(dialog.user());
+       AniDBUdpClient::Client::instance()->setPass(dialog.pass());
        m_automark = dialog.automark();
        m_automarkPaths = dialog.paths();
 
@@ -695,11 +690,11 @@ void VideoWindow::saveSettings()
        settings.endGroup();
 #  ifndef NO_ANIDBUDPCLIENT
        settings.beginGroup("anidbudpapiclient");
-               settings.setValue("host", anidb->host());
-               settings.setValue("hostPort", anidb->hostPort());
-               settings.setValue("localPort", anidb->localPort());
-               settings.setValue("user", anidb->user());
-               settings.setValue("pass", anidb->pass());
+               settings.setValue("host", AniDBUdpClient::Client::instance()->host());
+               settings.setValue("hostPort", AniDBUdpClient::Client::instance()->hostPort());
+               settings.setValue("localPort", AniDBUdpClient::Client::instance()->localPort());
+               settings.setValue("user", AniDBUdpClient::Client::instance()->user());
+               settings.setValue("pass", AniDBUdpClient::Client::instance()->pass());
                settings.setValue("automark", m_automark);
                settings.setValue("paths", m_automarkPaths);
        settings.endGroup();
@@ -729,11 +724,11 @@ void VideoWindow::loadSettings()
        settings.endGroup();
 #  ifndef NO_ANIDBUDPCLIENT
        settings.beginGroup("anidbudpapiclient");
-               anidb->setHost(settings.value("host", "api.anidb.info").toString());
-               anidb->setHostPort(settings.value("hostPort", 9000).toInt());
-               anidb->setLocalPort(settings.value("localPort", 9001).toInt());
-               anidb->setUser(settings.value("user").toString());
-               anidb->setPass(settings.value("pass").toString());
+               AniDBUdpClient::Client::instance()->setHost(settings.value("host", "api.anidb.info").toString());
+               AniDBUdpClient::Client::instance()->setHostPort(settings.value("hostPort", 9000).toInt());
+               AniDBUdpClient::Client::instance()->setLocalPort(settings.value("localPort", 9001).toInt());
+               AniDBUdpClient::Client::instance()->setUser(settings.value("user").toString());
+               AniDBUdpClient::Client::instance()->setPass(settings.value("pass").toString());
                m_automark = settings.value("automark", 0).toInt();
                m_automarkPaths = settings.value("paths", QStringList()).toStringList();
        settings.endGroup();
index 7fa0052275d64ec53626dbf3985a2c66c7e4e6da..4a494a88de8524a3e1c18bb0b16b37e63c421cdf 100644 (file)
 #      endif
 #endif
 
+#ifndef NO_ANIDBUDPCLIENT
+#      include <AniDBUdpClient/File>
+#endif
+
 class VideoPlayer;
 class VideoWidget;
 class Menu;
 class DirectoryPlaylist;
 
-#ifndef NO_ANIDBUDPCLIENT
-namespace AniDBUdpClient {
-       class Client;
-       class MyListAddReply;
-}
-#endif
-
 class VideoWindow : public QMainWindow
 #ifdef BROWSERPLUGIN_BUILD
                , public QtNPBindable
@@ -102,8 +99,7 @@ private slots:
 
 #ifndef NO_ANIDBUDPCLIENT
        void markWatched();
-       void doMarkWatched();
-       void showMarkResult(bool success);
+       void markingStatus(AniDBUdpClient::File::Action action, AniDBUdpClient::File::ActionState actionState);
 
        void anidbSettings();
        void updateAutomarkable();
@@ -155,8 +151,6 @@ private:
        QStringList m_automarkPaths;
        bool m_marked;
        bool m_automarkable;
-       AniDBUdpClient::Client *anidb;
-       AniDBUdpClient::MyListAddReply *addReply;
 #endif
 
        DirectoryPlaylist *playlist;