New relase with auto-rotaion and fixed setting dialog in portrait mode.
[quick-widgets] / ProcessObject.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29
30 #include <signal.h>
31 #include <sys/types.h>
32
33 #include <QtCore/QTime>
34 #include <QtDeclarative/qdeclarative.h>
35 #include <QtCore/QDebug>
36 #include <QtCore/QTextCodec>
37
38 #include "ProcessObject.h"
39
40 ProcessObject::ProcessObject(QObject *parent):
41         QObject(parent),
42         m_suspend(false), m_started(false)
43 {
44     m_process = new QProcess(this);
45     connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
46             this,
47             SLOT(processFinished(int, QProcess::ExitStatus)));
48     connect(m_process, SIGNAL(error(QProcess::ProcessError)),this,
49             SLOT(processErrored(QProcess::ProcessError)));
50 }
51
52 ProcessObject::~ProcessObject()
53 {
54     if (m_started)
55     {
56         terminate();
57     }
58     delete m_process;
59 }
60
61 void ProcessObject::processErrored(QProcess::ProcessError error) {
62     QString err(m_process->readAllStandardError());
63     qDebug() << "failed with exitCode" << m_process->exitCode();
64     emit failed(m_process->exitCode(), err, error);
65     m_started = false;
66 }
67
68 void ProcessObject::processFinished(int exitCode,
69                                     QProcess::ExitStatus exitStatus)
70 {
71     QTextCodec* codec = QTextCodec::codecForName("utf-8");
72     if (exitStatus == QProcess::NormalExit)
73     {
74         if (exitCode == 0)
75         {        
76           QString out = codec->toUnicode(m_process->readAllStandardOutput());
77           qDebug() << "normal";
78           emit completed(out.trimmed());
79         }
80         else
81         {
82           QString err = codec->toUnicode(m_process->readAllStandardError());
83           qDebug() << "failed";
84           emit failed(exitCode, err.trimmed(), m_process->error());
85         }
86     }
87     else
88     {
89         QString err = codec->toUnicode(m_process->readAllStandardError());
90         qDebug() << "failed";
91         emit failed(exitCode, err, m_process->error());
92     }
93     m_started = false;
94 }
95
96 QString ProcessObject::command() const
97 {
98     return m_command;
99 }
100
101 void ProcessObject::setCommand(const QString &command)
102 {
103     if (m_command != command) {
104         m_command = command;
105     }
106 }
107
108 void ProcessObject::run()
109 {
110     if (m_started)
111     {
112         return;
113         qDebug() << "already started";
114     }
115     qDebug() << m_command;
116     m_process->start(m_command);
117     qDebug() << "started";
118     m_started = true;
119 }
120
121 bool ProcessObject::isSuspended() const
122 {
123     return m_suspend;
124 }
125
126 void ProcessObject::setSuspend(bool suspend)
127 {
128     if (!m_started || m_process->pid() == 0)
129     {
130         qDebug() << "No process running";
131         return;
132     }
133     if (m_suspend != suspend)
134     {
135         if (m_suspend)
136         {
137             ::kill(m_process->pid(), SIGCONT);
138             qDebug() << "Process resumed";
139
140         } else {
141             ::kill(m_process->pid(), SIGSTOP);
142             qDebug() << "Process suspended";
143
144         }
145         emit suspendChanged(suspend);
146         m_suspend = suspend;
147     }
148 }
149
150 void ProcessObject::terminate()
151 {
152     if (m_started && m_process->pid() != 0)
153     {
154         m_process->kill();
155     }
156 }
157
158
159 QML_DECLARE_TYPE(ProcessObject);