OSDN Git Service

Manual: Completed the "Tutorial" section.
[lamexp/LameXP.git] / src / Thread_MessageProducer.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Thread_MessageProducer.h"
24
25 //Internal
26 #include "Global.h"
27 #include "IPCCommands.h"
28
29 //MUtils
30 #include <MUtils/Global.h>
31 #include <MUtils/IPCChannel.h>
32 #include <MUtils/OSSupport.h>
33
34 //Qt
35 #include <QStringList>
36 #include <QApplication>
37 #include <QFileInfo>
38 #include <QDir>
39
40 //CRT
41 #include <limits.h>
42
43 ////////////////////////////////////////////////////////////
44 // Constructor
45 ////////////////////////////////////////////////////////////
46
47 MessageProducerThread::MessageProducerThread(MUtils::IPCChannel *const ipcChannel)
48 :
49         m_ipcChannel(ipcChannel)
50 {
51 }
52
53 MessageProducerThread::~MessageProducerThread(void)
54 {
55 }
56
57 void MessageProducerThread::run()
58 {
59         setTerminationEnabled(true);
60         bool bSentFiles = false;
61         const QStringList &arguments = MUtils::OS::arguments();
62
63         for(int i = 0; i < arguments.count(); i++)
64         {
65                 if(!arguments[i].compare("--kill", Qt::CaseInsensitive))
66                 {
67                         if(!m_ipcChannel->send(IPC_CMD_TERMINATE, IPC_FLAG_NONE, NULL))
68                         {
69                                 qWarning("Failed to send IPC message!");
70                         }
71                         return;
72                 }
73                 if(!arguments[i].compare("--force-kill", Qt::CaseInsensitive))
74                 {
75                         if(!m_ipcChannel->send(IPC_CMD_TERMINATE, IPC_FLAG_FORCE, NULL))
76                         {
77                                 qWarning("Failed to send IPC message!");
78                         }
79                         return;
80                 }
81         }
82
83         for(int i = 0; i < arguments.count() - 1; i++)
84         {
85                 if(!arguments[i].compare("--add", Qt::CaseInsensitive))
86                 {
87                         QFileInfo file = QFileInfo(arguments[++i]);
88                         if(file.exists() && file.isFile())
89                         {
90                                 if(!m_ipcChannel->send(IPC_CMD_ADD_FILE, IPC_FLAG_NONE, MUTILS_UTF8(file.canonicalFilePath())))
91                                 {
92                                         qWarning("Failed to send IPC message!");
93                                 }
94                         }
95                         bSentFiles = true;
96                 }
97                 if(!arguments[i].compare("--add-folder", Qt::CaseInsensitive))
98                 {
99                         QDir dir = QDir(arguments[++i]);
100                         if(dir.exists())
101                         {
102                                 if(!m_ipcChannel->send(IPC_CMD_ADD_FOLDER, IPC_FLAG_NONE, MUTILS_UTF8(dir.canonicalPath())))
103                                 {
104                                         qWarning("Failed to send IPC message!");
105                                 }
106                         }
107                         bSentFiles = true;
108                 }
109                 if(!arguments[i].compare("--add-recursive", Qt::CaseInsensitive))
110                 {
111                         QDir dir = QDir(arguments[++i]);
112                         if(dir.exists())
113                         {
114                                 if(!m_ipcChannel->send(IPC_CMD_ADD_FOLDER, IPC_FLAG_ADD_RECURSIVE, MUTILS_UTF8(dir.canonicalPath())))
115                                 {
116                                         qWarning("Failed to send IPC message!");
117                                 }
118                         }
119                         bSentFiles = true;
120                 }
121         }
122
123         if(!bSentFiles)
124         {
125                 if(!m_ipcChannel->send(IPC_CMD_PING, IPC_FLAG_NONE, "Use running instance!"))
126                 {
127                         qWarning("Failed to send IPC message!");
128                 }
129         }
130 }
131
132 ////////////////////////////////////////////////////////////
133 // EVENTS
134 ////////////////////////////////////////////////////////////
135
136 /*NONE*/