OSDN Git Service

245220b1bad89aaa53508ff090a63c13ed0ba029
[lamexp/LameXP.git] / src / Thread_MessageHandler.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_MessageHandler.h"
24
25 //Internal
26 #include "Global.h"
27
28 //MUtils
29 #include <MUtils/IPCChannel.h>
30
31 //Qt
32 #include <QSharedMemory>
33 #include <QSystemSemaphore>
34 #include <QMessageBox>
35
36 //CRL
37 #include <limits.h>
38
39 ////////////////////////////////////////////////////////////
40 // Constructor
41 ////////////////////////////////////////////////////////////
42
43 MessageHandlerThread::MessageHandlerThread(MUtils::IPCChannel *const ipcChannel)
44 :
45         m_ipcChannel(ipcChannel)
46 {
47         m_aborted = false;
48         m_parameter = new char[4096];
49 }
50
51 MessageHandlerThread::~MessageHandlerThread(void)
52 {
53         delete [] m_parameter;
54 }
55
56 void MessageHandlerThread::run()
57 {
58         m_aborted = false;
59         setTerminationEnabled(true);
60
61         while(!m_aborted)
62         {
63                 unsigned int command = 0;
64                 m_ipcChannel->read(command, m_parameter, 4096);
65                 if(!command) continue;
66
67                 switch(command)
68                 {
69                 case 1:
70                         emit fileReceived(QString::fromUtf8(m_parameter));
71                         break;
72                 case 2:
73                         emit folderReceived(QString::fromUtf8(m_parameter), false);
74                         break;
75                 case 3:
76                         emit folderReceived(QString::fromUtf8(m_parameter), true);
77                         break;
78                 case 666:
79                         if(!_stricmp(m_parameter, "Force!"))
80                         {
81                                 _exit(-2);
82                         }
83                         else
84                         {
85                                 emit killSignalReceived();
86                         }
87                         break;
88                 case UINT_MAX:
89                         emit otherInstanceDetected();
90                         break;
91                 default:
92                         qWarning("Received an unknown IPC message! (command=%u)", command);
93                         break;
94                 }
95         }
96 }
97
98 void MessageHandlerThread::stop(void)
99 {
100         if(!m_aborted)
101         {
102                 m_aborted = true;
103                 m_ipcChannel->send(0, NULL);
104         }
105 }
106
107 ////////////////////////////////////////////////////////////
108 // EVENTS
109 ////////////////////////////////////////////////////////////
110
111 /*NONE*/