summaryrefslogtreecommitdiffstats
path: root/adept/libadept/dpkgpm-gui.cpp
blob: fb5e2dc34a409d3f90a4004686696be5d577eaa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/** -*- C++ -*-
	@file adept/dpkgpm-gui.cpp
	@author Peter Rockai <me@mornfall.net>
*/

#include <fcntl.h>
#include <iostream>

#include <tqstrlist.h>
#include <tdeapplication.h>
#include <tdelistbox.h>
#include <tdelocale.h>
#include <tdeparts/part.h>

#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>

#include <apt-front/cache/cache.h>
#include <apt-front/cache/component/packages.h>
#include <apt-front/cache/entity/package.h>

#include <adept/dpkgpm-gui.h>
#include <adept/utils.h>

using namespace aptFront;
using namespace cache;

namespace adept {

PkgSystem::PkgSystem()
    : m_terminalPart( 0 )
{
    std::cerr << "kapture::PkgSystem::PkgSystem()" << std::endl;
    Label = "adeptDPkgSystem";
}

void PkgSystem::setTerminal( KParts::Part *t )
{
    m_terminalPart = t;
}

pkgPackageManager *PkgSystem::CreatePM( pkgDepCache *c ) const
{
    std::cerr << "kapture::PkgSystem::CreatePM()" << std::endl;
    adept::DPkgPM *pm = new adept::DPkgPM( c, m_terminalPart );
    connect( pm, TQT_SIGNAL( statusChanged( int, TQString ) ),
             this, TQT_SIGNAL( statusChanged( int, TQString ) ) );
    return pm;
}

DPkgPM::DPkgPM( pkgDepCache *cache, KParts::Part *t )
    : aptFront::DPkgPM (cache), m_terminalPart (t)
{
}

bool DPkgPM::forkDpkg( char *const argv[] )
{
    bool ok = true;
    std::cerr << "adept::DPkgPM::forkDpkg ()" << std::endl;
    TQStrList l;
    for (int i = 0; argv[i]; i ++)
        l.append( argv[i] );

    m_processRunning = true;
    connect( m_terminalPart, TQT_SIGNAL( processExited( TDEProcess * ) ),
             this, TQT_SLOT( processExit( TDEProcess * ) ) );
    connect( m_terminalPart, TQT_SIGNAL( processExited( const TDEProcess * ) ),
             this, TQT_SLOT( processExitC( const TDEProcess * ) ) );
    connect( m_terminalPart, TQT_SIGNAL( forkedChild() ),
             this, TQT_SLOT( setupDpkgChild() ) );

    terminal()->startProgram( u8( argv[0] ), l );

    ::close( m_dpkgPipe[1] );
    ::fcntl( m_dpkgPipe[0], F_SETFL, O_NONBLOCK );

    while (m_processRunning) {
        dpkgMonitor();
        usleep( 50000 );
    }

    if (m_exitedProcess->normalExit()) {
        if (m_exitedProcess->exitStatus() != 0) {
            ok = _error->Error( "Child for %s exited with error %d",
                                argv [0], m_exitedProcess->exitStatus());
        }
    } else {
        ok = _error->Error( "Child for %s was killed by signal %d",
                            argv [0], m_exitedProcess->exitSignal());
    }

    if (ok) // do we run scripts in case dpkg died???
        ok = runScripts ("DPkg::Post-Invoke", false);

    return ok;
}

ExtTerminalInterface *DPkgPM::terminal() {
    return static_cast<ExtTerminalInterface *>(
        m_terminalPart->tqt_cast( "ExtTerminalInterface" ) );
}

bool DPkgPM::forkScript (const char *cmd, bool fP)
{
    std::cerr << "adept::DPkgPM::forkScript(\"" << cmd << "\")" << std::endl;
    if (fP) {
        if (pipe( m_scriptPipe ) != 0)
            return _error->Errno(
                "pipe","Failed to create IPC pipe to subprocess");
        SetCloseExec (m_scriptPipe[0], true);
        SetCloseExec (m_scriptPipe[1], true);
    }
    TQStrList l;
    l.append ("/bin/sh");
    l.append ("-c");
    l.append (cmd);
    if (fP) {
        connect( m_terminalPart, TQT_SIGNAL( forkedChild() ),
                 this, TQT_SLOT( setupScriptPipe() ) );
    }

    connect( m_terminalPart, TQT_SIGNAL( processExited( TDEProcess * ) ),
             this, TQT_SLOT( processExit( TDEProcess * ) ) );
    connect( m_terminalPart, TQT_SIGNAL( processExited( const TDEProcess * ) ),
             this, TQT_SLOT( processExitC( const TDEProcess * ) ) );

    m_processRunning = true;
    terminal()->startProgram( u8( "/bin/sh" ), l);

    if (fP) {
        if (!feedPackages())
            return _error->Error("Failed feeding packages to script");
    }

    while (m_processRunning) {
        kapp->processEvents();
        usleep(50000);
    }

    std::cerr << "END: adept::DPkgPM::forkScript(\""
              << cmd << "\")" << std::endl;

    if (m_exitedProcess->normalExit()) {
        if (m_exitedProcess->exitStatus() != 0) {
            return _error -> Error("Child for %s exited with error %d",
                                   cmd, m_exitedProcess->exitStatus());
        } else {
            return true;
        }
    } else {
        return _error->Error("Child for %s was killed by signal %d",
                             cmd, m_exitedProcess->exitSignal());
    }
}

void DPkgPM::processExit(TDEProcess *p) {
    processExitC( p );
}

void DPkgPM::processExitC(const TDEProcess *p)
{
    std::cerr << "a process exited!" << std::endl;
    m_processRunning = false;
    m_exitedProcess = p;
}

void DPkgPM::setupScriptPipe()
{
    // setupScript();
    // std::cerr << "setupScriptPipe()" << std::endl;
    dup2 (m_scriptPipe[0], STDIN_FILENO);
}

void DPkgPM::setupDpkgChild()
{
    // setupScript();
    // std::cerr << "setupDpkgChild()" << std::endl;
    setupChild();
}

bool DPkgPM::Go( int )
{
    std::cerr << "kapture::DPkgPM::Go ()" << std::endl;
    statusChanged( 0, i18n( "Preparing..." ) );
    bool ret = aptFront::DPkgPM::Go(-1);
    TQStrList l;
    l.append("echo");
    l.append("dpkg run finished!");
    terminal()->startProgram( u8( "echo" ), l );
    statusChanged( 100, i18n( "Done" ) );
    return ret;
}

void DPkgPM::dpkgMonitor ()
{
    aptFront::DPkgPM::dpkgMonitor();
    kapp->processEvents();
}

void DPkgPM::updateStatus( std::string pkg, std::string ev, std::string r )
{
    std::string op, msg;
    aptFront::DPkgPM::updateStatus( pkg, ev, r );
    entity::Package p = cache::Global::get().packages().packageByName( pkg );

    if ( m_currentOp == OInstall ) {
        if ( p.markedNewInstall() ) {
            if ( ev == "half-installed" )
                msg = u8( i18n( "Preparing installation of %1..." ) );
            else if ( ev == "unpacked" )
                msg = u8( i18n( "Unpacking %1..." ) );
        } else if ( p.markedUpgrade() ) {
            if ( ev == "half-installed" )
                msg = u8( i18n( "Preparing upgrade of %1..." ) );
            else if ( ev == "unpacked" || ev == "half-configured" )
                msg = u8( i18n( "Replacing %1 with new version..." ) );
        }
    } else if ( m_currentOp == OConfigure ) {
        if ( p.markedNewInstall() ) {
            if ( ev == "unpacked" )
                msg = u8( i18n( "Preparing to configure %1..." ) );
            else if ( ev == "half-configured" )
                msg = u8( i18n( "Configuring %1..." ) );
            else if ( ev == "installed" )
                msg = u8( i18n( "Installed %1" ) );
        } else if ( p.markedUpgrade() ) {
            if ( ev == "unpacked" )
                msg = u8( i18n( "Preparing to configure new version of %1..." ) );
            else if ( ev == "half-configured" )
                msg = u8( i18n( "Configuring new version of %1..." ) );
            else if ( ev == "installed" )
                msg = u8( i18n( "Upgraded %1" ) );
        }
    } else if ( m_currentOp == ORemove ) {
        if ( ev == "installed" )
            msg = u8( i18n( "Preparing to remove %1..." ) );
        else if ( ev == "half-configured" || ev == "half-installed" )
            msg = u8( i18n( "Removing %1..." ) );
        else if ( ev == "config-files" || ev == "not-installed" )
            msg = u8( i18n( "Removed %1" ) );
    } else if ( m_currentOp == OPurge ) {
        if ( ev == "config-files" )
            msg = u8( i18n( "Preparing to purge %1..." ) );
        else if ( ev == "not-installed" )
            msg = u8( i18n( "Purged %1" ) );
    }

    std::cerr << "updateStatus( " << pkg << ", " << ev << ", " << r << ")" << std::endl;
    std::cerr << "updateStatus: msg =  " << msg << std::endl;
    std::cerr << "updateStatus: seen = " << m_seenOpCount
              << ", total = " << m_totalOpCount << std::endl;
    statusChanged( ( m_seenOpCount * 100 ) / m_totalOpCount,
                   u8( msg ).arg( pkg ) + ( ( r == "") ? "" : (" (" + r + ")") ) );
}

}

#include "dpkgpm-gui.moc"