OSDN Git Service

Bump version.
[lamexp/LameXP.git] / src / Thread_MessageProducer.cpp
index cf50ad7..b8b3838 100644 (file)
@@ -1,12 +1,12 @@
 ///////////////////////////////////////////////////////////////////////////////
 // LameXP - Audio Encoder Front-End
-// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2020 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
+// it under the terms of the GNU GENERAL PUBLIC LICENSE as published by
 // the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version, but always including the *additional*
-// restrictions defined in the "License.txt" file.
+// (at your option) any later version; always including the non-optional
+// LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
 //
 // This program is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 //Internal
 #include "Global.h"
+#include "IPCCommands.h"
 
 //MUtils
 #include <MUtils/Global.h>
+#include <MUtils/IPCChannel.h>
+#include <MUtils/OSSupport.h>
 
 //Qt
 #include <QStringList>
@@ -41,7 +44,9 @@
 // Constructor
 ////////////////////////////////////////////////////////////
 
-MessageProducerThread::MessageProducerThread(void)
+MessageProducerThread::MessageProducerThread(MUtils::IPCChannel *const ipcChannel)
+:
+       m_ipcChannel(ipcChannel)
 {
 }
 
@@ -53,48 +58,68 @@ void MessageProducerThread::run()
 {
        setTerminationEnabled(true);
        bool bSentFiles = false;
-       const QStringList &arguments = lamexp_arguments(); //QApplication::arguments();
+       const MUtils::OS::ArgumentMap &arguments = MUtils::OS::arguments();
 
-       for(int i = 0; i < arguments.count(); i++)
+       //Kill application?
+       if(arguments.contains("kill"))
        {
-               if(!arguments[i].compare("--kill", Qt::CaseInsensitive))
+               if(!m_ipcChannel->send(IPC_CMD_TERMINATE, IPC_FLAG_NONE))
                {
-                       lamexp_ipc_send(666, NULL);
-                       return;
+                       qWarning("Failed to send IPC message!");
                }
-               if(!arguments[i].compare("--force-kill", Qt::CaseInsensitive))
+               return;
+       }
+       if(arguments.contains("force-kill"))
+       {
+               if(!m_ipcChannel->send(IPC_CMD_TERMINATE, IPC_FLAG_FORCE))
                {
-                       lamexp_ipc_send(666, "Force!");
-                       return;
+                       qWarning("Failed to send IPC message!");
                }
+               return;
        }
 
-       for(int i = 0; i < arguments.count() - 1; i++)
+       //Send file to "matser" instance
+       foreach(const QString &value, arguments.values("add"))
        {
-               if(!arguments[i].compare("--add", Qt::CaseInsensitive))
+               if(!value.isEmpty())
                {
-                       QFileInfo file = QFileInfo(arguments[++i]);
+                       const QFileInfo file = QFileInfo(value);
                        if(file.exists() && file.isFile())
                        {
-                               lamexp_ipc_send(1, MUTILS_UTF8(file.canonicalFilePath()));
+                               if(!m_ipcChannel->send(IPC_CMD_ADD_FILE, IPC_FLAG_NONE, QStringList() << file.canonicalFilePath()))
+                               {
+                                       qWarning("Failed to send IPC message!");
+                               }
                        }
                        bSentFiles = true;
                }
-               if(!arguments[i].compare("--add-folder", Qt::CaseInsensitive))
+       }
+       foreach(const QString &value, arguments.values("add-folder"))
+       {
+               if(!value.isEmpty())
                {
-                       QDir dir = QDir(arguments[++i]);
+                       const QDir dir = QDir(value);
                        if(dir.exists())
                        {
-                               lamexp_ipc_send(2, MUTILS_UTF8(dir.canonicalPath()));
+                               if(!m_ipcChannel->send(IPC_CMD_ADD_FOLDER, IPC_FLAG_NONE, QStringList() << dir.canonicalPath()))
+                               {
+                                       qWarning("Failed to send IPC message!");
+                               }
                        }
                        bSentFiles = true;
                }
-               if(!arguments[i].compare("--add-recursive", Qt::CaseInsensitive))
+       }
+       foreach(const QString &value, arguments.values("add-recursive"))
+       {
+               if(!value.isEmpty())
                {
-                       QDir dir = QDir(arguments[++i]);
+                       const QDir dir = QDir(value);
                        if(dir.exists())
                        {
-                               lamexp_ipc_send(3, MUTILS_UTF8(dir.canonicalPath()));
+                               if(!m_ipcChannel->send(IPC_CMD_ADD_FOLDER, IPC_FLAG_ADD_RECURSIVE, QStringList() << dir.canonicalPath()))
+                               {
+                                       qWarning("Failed to send IPC message!");
+                               }
                        }
                        bSentFiles = true;
                }
@@ -102,7 +127,10 @@ void MessageProducerThread::run()
 
        if(!bSentFiles)
        {
-               lamexp_ipc_send(UINT_MAX, "Use running instance!");
+               if(!m_ipcChannel->send(IPC_CMD_PING, IPC_FLAG_NONE, QStringList() << QLatin1String("Use running instance!")))
+               {
+                       qWarning("Failed to send IPC message!");
+               }
        }
 }