~ubuntu-branches/ubuntu/utopic/kpty/utopic-proposed

« back to all changes in this revision

Viewing changes to src/kptyprocess.cpp

  • Committer: Package Import Robot
  • Author(s): Scarlett Clark
  • Date: 2014-07-14 17:50:19 UTC
  • Revision ID: package-import@ubuntu.com-20140714175019-6lqoahk5g43drljq
Tags: upstream-5.0.0
ImportĀ upstreamĀ versionĀ 5.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This file is part of the KDE libraries
 
3
 
 
4
    Copyright (C) 2007 Oswald Buddenhagen <ossi@kde.org>
 
5
 
 
6
    This library is free software; you can redistribute it and/or
 
7
    modify it under the terms of the GNU Library General Public
 
8
    License as published by the Free Software Foundation; either
 
9
    version 2 of the License, or (at your option) any later version.
 
10
 
 
11
    This library 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 GNU
 
14
    Library General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU Library General Public License
 
17
    along with this library; see the file COPYING.LIB.  If not, write to
 
18
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
    Boston, MA 02110-1301, USA.
 
20
*/
 
21
 
 
22
#include "kptyprocess.h"
 
23
 
 
24
#include <kuser.h>
 
25
#include <kptydevice.h>
 
26
 
 
27
#include <stdlib.h>
 
28
#include <unistd.h>
 
29
 
 
30
//////////////////
 
31
// private data //
 
32
//////////////////
 
33
 
 
34
struct KPtyProcessPrivate {
 
35
    KPtyProcessPrivate() :
 
36
        ptyChannels(KPtyProcess::NoChannels),
 
37
        addUtmp(false)
 
38
    {
 
39
    }
 
40
 
 
41
    void _k_onStateChanged(QProcess::ProcessState newState)
 
42
    {
 
43
        if (newState == QProcess::NotRunning && addUtmp) {
 
44
            pty->logout();
 
45
        }
 
46
    }
 
47
 
 
48
    KPtyDevice *pty;
 
49
    KPtyProcess::PtyChannels ptyChannels;
 
50
    bool addUtmp : 1;
 
51
};
 
52
 
 
53
KPtyProcess::KPtyProcess(QObject *parent) :
 
54
    KProcess(parent), d_ptr(new KPtyProcessPrivate)
 
55
{
 
56
    Q_D(KPtyProcess);
 
57
 
 
58
    d->pty = new KPtyDevice(this);
 
59
    d->pty->open();
 
60
    connect(this, SIGNAL(stateChanged(QProcess::ProcessState)),
 
61
            SLOT(_k_onStateChanged(QProcess::ProcessState)));
 
62
}
 
63
 
 
64
KPtyProcess::KPtyProcess(int ptyMasterFd, QObject *parent) :
 
65
    KProcess(parent), d_ptr(new KPtyProcessPrivate)
 
66
{
 
67
    Q_D(KPtyProcess);
 
68
 
 
69
    d->pty = new KPtyDevice(this);
 
70
    d->pty->open(ptyMasterFd);
 
71
    connect(this, SIGNAL(stateChanged(QProcess::ProcessState)),
 
72
            SLOT(_k_onStateChanged(QProcess::ProcessState)));
 
73
}
 
74
 
 
75
KPtyProcess::~KPtyProcess()
 
76
{
 
77
    Q_D(KPtyProcess);
 
78
 
 
79
    if (state() != QProcess::NotRunning && d->addUtmp) {
 
80
        d->pty->logout();
 
81
        disconnect(SIGNAL(stateChanged(QProcess::ProcessState)),
 
82
                   this, SLOT(_k_onStateChanged(QProcess::ProcessState)));
 
83
    }
 
84
    delete d->pty;
 
85
    delete d_ptr;
 
86
}
 
87
 
 
88
void KPtyProcess::setPtyChannels(PtyChannels channels)
 
89
{
 
90
    Q_D(KPtyProcess);
 
91
 
 
92
    d->ptyChannels = channels;
 
93
}
 
94
 
 
95
KPtyProcess::PtyChannels KPtyProcess::ptyChannels() const
 
96
{
 
97
    Q_D(const KPtyProcess);
 
98
 
 
99
    return d->ptyChannels;
 
100
}
 
101
 
 
102
void KPtyProcess::setUseUtmp(bool value)
 
103
{
 
104
    Q_D(KPtyProcess);
 
105
 
 
106
    d->addUtmp = value;
 
107
}
 
108
 
 
109
bool KPtyProcess::isUseUtmp() const
 
110
{
 
111
    Q_D(const KPtyProcess);
 
112
 
 
113
    return d->addUtmp;
 
114
}
 
115
 
 
116
KPtyDevice *KPtyProcess::pty() const
 
117
{
 
118
    Q_D(const KPtyProcess);
 
119
 
 
120
    return d->pty;
 
121
}
 
122
 
 
123
void KPtyProcess::setupChildProcess()
 
124
{
 
125
    Q_D(KPtyProcess);
 
126
 
 
127
    d->pty->setCTty();
 
128
    if (d->addUtmp) {
 
129
        d->pty->login(KUser(KUser::UseRealUserID).loginName().toLocal8Bit().constData(), qgetenv("DISPLAY").constData());
 
130
    }
 
131
    if (d->ptyChannels & StdinChannel) {
 
132
        dup2(d->pty->slaveFd(), 0);
 
133
    }
 
134
    if (d->ptyChannels & StdoutChannel) {
 
135
        dup2(d->pty->slaveFd(), 1);
 
136
    }
 
137
    if (d->ptyChannels & StderrChannel) {
 
138
        dup2(d->pty->slaveFd(), 2);
 
139
    }
 
140
 
 
141
    KProcess::setupChildProcess();
 
142
}
 
143
 
 
144
#include "moc_kptyprocess.cpp"