Revert previous commit, was incorrect
[amarok.git] / src / Process.cpp
blob93771fae696c558efde1d7449c7918c191a686ca
1 /***************************************************************************
2 * copyright : (C) 2007 Shane King <kde@dontletsstart.com> *
3 **************************************************************************/
5 /***************************************************************************
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 ***************************************************************************/
14 #include "Process.h"
15 #include "debug.h"
17 #include <QTextCodec>
19 #include <fcntl.h>
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <unistd.h>
24 Process::Process(QObject *parent)
25 : KProcess(parent), lowPriority(false)
27 connect( this, SIGNAL( finished( int ) ), this, SLOT( finished() ) );
28 connect( this, SIGNAL( readyReadStandardOutput() ), this, SLOT( readyReadStandardOutput() ) );
29 connect( this, SIGNAL( readyReadStandardError() ), this, SLOT( readyReadStandardError() ) );
32 /**
33 * Due to xine-lib, we have to make KProcess close all fds, otherwise we get "device is busy" messages
34 * exploiting setupChildProcess(), a virtual method that
35 * happens to be called in the forked process
36 * See bug #103750 for more information.
38 void
39 Process::setupChildProcess()
41 KProcess::setupChildProcess();
43 #ifdef Q_OS_UNIX
44 // can't get at the fds that QProcess needs to keep around to do its status
45 // tracking , but fortunately it sets them to close on exec anyway, so if
46 // we do likewise to everything then we should be ok.
47 for(int i = sysconf(_SC_OPEN_MAX) - 1; i > 2; i--)
48 fcntl(i, F_SETFD, FD_CLOEXEC);
50 if( lowPriority )
51 setpriority( PRIO_PROCESS, 0, 19 );
52 #endif
55 void
56 Process::start()
58 KProcess::start();
60 #ifdef Q_OS_WIN32
61 if( lowPriority )
62 SetPriorityClass( QProcess::pid()->hProcess, IDLE_PRIORITY_CLASS );
63 #endif
66 void
67 Process::finished() // SLOT
69 emit processExited( this );
72 void
73 Process::readyReadStandardOutput() // SLOT
75 emit receivedStdout( this );
78 void
79 Process::readyReadStandardError() // SLOT
81 emit receivedStderr( this );
84 // ProcIO
85 ProcIO::ProcIO ( )
86 : codec(QTextCodec::codecForName( "UTF-8" ))
90 bool
91 ProcIO::writeStdin (const QString &line)
93 return write( codec->fromUnicode( line + "\n" ) ) > 0;
96 int
97 ProcIO::readln (QString &line)
99 QByteArray bytes = readLine();
100 if (bytes.length() == 0)
102 return -1;
104 else
106 // convert and remove \n
107 line = codec->toUnicode( bytes.data(), bytes.length() - 1);
108 return line.length();
112 void
113 ProcIO::start()
115 connect (this, SIGNAL (readyReadStandardOutput()), this, SLOT (readyReadStandardOutput()));
117 KProcess::start ();
120 void
121 ProcIO::readyReadStandardOutput()
123 if( canReadLine() )
124 emit readReady( this );
127 ShellProcess &
128 ShellProcess::operator<<(const QString& arg)
130 if( program().isEmpty() )
131 setShellCommand( arg );
132 else
133 Process::operator<<( arg );
134 return *this;
137 ShellProcess &
138 ShellProcess::operator<<(const QStringList& args)
140 foreach( QString arg, args )
141 *this << arg;
142 return *this;