summaryrefslogtreecommitdiffstats
path: root/cervisia/cvsservice/cvsloginjob.cpp
blob: 5eb1d9fc00dfababda7a1ad5da3cee76c2fdaafe (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
/*
 * Copyright (c) 2003 Christian Loose <christian.loose@hamburg.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "cvsloginjob.h"

#include <kdebug.h>
#include <tdelocale.h>
#include <kpassdlg.h>

#include <sys/types.h>
#include <signal.h>

static const char LOGIN_PHRASE[]   = "Logging in to";
static const char FAILURE_PHRASE[] = "authorization failed:";
static const char PASS_PHRASE[]    = "CVS password: ";


CvsLoginJob::CvsLoginJob(unsigned jobNum)
    : DCOPObject()
    , m_Proc(0)
{
    TQString objId("CvsLoginJob" + TQString::number(jobNum));
    setObjId(objId.local8Bit());

    m_Proc = new PtyProcess;
}


CvsLoginJob::~CvsLoginJob()
{
    delete m_Proc;
}


void CvsLoginJob::setServer(const TQString& server)
{
    m_Server = server;
}


void CvsLoginJob::setCvsClient(const TQCString& cvsClient)
{
    m_CvsClient = cvsClient;

    m_Arguments.clear();
    m_Arguments += "-f";
}


void CvsLoginJob::setRepository(const TQCString& repository)
{
    m_Arguments += "-d";
    m_Arguments += repository;
    m_Arguments += "login";
}


bool CvsLoginJob::execute()
{
    static TQCString repository;

    int res = m_Proc->exec(m_CvsClient, m_Arguments);
    if( res < 0 )
    {
        kdDebug(8051) << "Couldn't start 'cvs login' process!" << endl;
        return false;
    }

    bool result = false;
    while( true )
    {
        TQCString line = m_Proc->readLine();
        if( line.isNull() )
        {
            return result;
        }

        // add line to output list
        m_output << line;
        kdDebug(8051) << "process output = " << line << endl;

        // retrieve repository from 'Logging in to'-line
        if( line.contains(LOGIN_PHRASE) )
        {
            repository = line.remove(0, line.find(":pserver:"));
            continue;
        }

        // process asks for the password
        // search case insensitive as cvs and cvsnt use different capitalization
        if( line.contains(PASS_PHRASE, false) )
        {
            kdDebug(8051) << "process waits for the password." << endl;

            // show password dialog
            // TODO: We really should display the repository name. Unfortunately
            //       the dialog doesn't show part of the repository name, because
            //       it's too long. :-(
            TQCString password;
            int res = KPasswordDialog::getPassword(password, i18n("Please type "
                        "in your password for the repository below."));
            if( res == KPasswordDialog::Accepted )
            {
                // send password to process
                m_Proc->WaitSlave();
                m_Proc->writeLine(password);

                // wait for the result
                while( !line.contains(FAILURE_PHRASE) )
                {
                    line = m_Proc->readLine();
                    if( line.isNull() )
                        return true;

                    // add line to output list
                    m_output << line;
                    kdDebug(8051) << "process output = " << line << endl;
                }

                result = false;
            }
            else
            {
                // user pressed cancel so kill the process
                kill(m_Proc->pid(), SIGKILL);
                m_Proc->waitForChild();
                result = false;
            }
        }
    }

    return false;
}


TQStringList CvsLoginJob::output()
{
    return m_output;
}