File: cgpgproc.cpp

package info (click to toggle)
gpgkeys 0.3.1-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 276 kB
  • ctags: 241
  • sloc: cpp: 2,007; makefile: 53
file content (110 lines) | stat: -rw-r--r-- 2,845 bytes parent folder | download | duplicates (2)
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
/*
 * GPG Keys 
 * cgpgproc.cpp
 * (c) 2001 Peter Mathiasson <peter@mathiasson.nu>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation
 */

#include <qapp.h>
#include <qobject.h>
#include <qstring.h>
#include <qstringlist.h>
#include <qprocess.h>
#include "cgpgproc.h"
#include "functions.h"
#include "config.h"

/* --------------------------------------------------------------------------------- */

CGPGProc::CGPGProc( QObject *parent ) : QObject( parent, "CGPGProc" )
{
    proc = new QProcess( this );    
}

/* --------------------------------------------------------------------------------- */

bool CGPGProc::import( const QString importData )
{    
    /* Check for running GPG */
    if ( proc->isRunning() )
        return ALREADYRUNNING;

    /* Setup Arguments */
    proc->clearArguments();
    proc->addArgument( readConfigString( "gpgpath", GPGPATH ) );
    proc->addArgument( "--import" );

    /* Run GPG */
    emit message( tr( "Importing key(s)" ), 0 );
    if ( !proc->launch( importData ) ) {
        emit message( tr( "Run failed"), 2000 );
        return RUNFAILED;
    }

    /* Wait */
    while ( proc->isRunning() ) {
        qApp->processEvents();
    }

    /* Check Exit Status */
    if ( proc->normalExit() && !proc->exitStatus() ) {
        emit message( tr( "Key(s) sucessfully imported" ), 2000 );
        return OK;
    } else {
        emit message( tr( "Import failed" ), 2000 );
        return ERROR;
    }
    
}

/* --------------------------------------------------------------------------------- */

int CGPGProc::run( QObject *parent, const QStringList args, const QString stdin,
                   bool terminal )
{

    /* Create Process */
    QProcess *gpgProc = new QProcess( parent );
    if ( terminal ) {
        QStringList term = QStringList::split( ' ', readConfigString( "terminal", TERMINAL ) );
        for ( unsigned int i = 0; i < term.size(); i++ ) {
            gpgProc->addArgument( term[i] );
        }
    }
    gpgProc->addArgument( readConfigString( "gpgpath", GPGPATH ) );
    for ( unsigned int i = 0; i < args.size(); i++ ) {
        gpgProc->addArgument( args[i] );
    }
    
    /* Run GPG */
    int retval;
    if ( stdin == QString::null ) {
        retval = gpgProc->start();
    } else {
        retval = gpgProc->launch( stdin );
    }

    if ( !retval ) {
        return RUNFAILED;
    }

    /* Wait */
    while ( gpgProc->isRunning() ) {
        qApp->processEvents();
    }

    /* Check Exit Status */
    if ( gpgProc->normalExit() && !gpgProc->exitStatus() ) {
        return OK;
    } else {
        return ERROR;
    }

}

/* --------------------------------------------------------------------------------- */