summaryrefslogtreecommitdiffstats
path: root/krename/undodialog.cpp
blob: 793040726a9775d58db2bd6ea480522e9cbed754 (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
    /***************************************************************************
                         undodialog.cpp  -  description
                             -------------------
    begin                : Mon Mai 27 20:08:19 CEST 2002
    copyright            : (C) 2001 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "undodialog.h"

// TQt includes
#include <tqlabel.h>
#include <tqlayout.h>

// KDE includes
#include <tdefiledialog.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kprocess.h>
#include <ktextbrowser.h>
#include <kurlrequester.h>

UndoDialog::UndoDialog( TQWidget* parent )
    : KDialogBase( KDialogBase::Plain, i18n("Undo Renaming"),
      KDialogBase::User1 | KDialogBase::Close, KDialogBase::User1, parent, 0, false, true )
{
    UndoDialogLayout = new TQVBoxLayout( plainPage(), 11, 6, "UndoDialogLayout");

    TextLabel1 = new TQLabel( plainPage(), "TextLabel1" );
    TextLabel1->setText( i18n( "Undo script:" ) );
    UndoDialogLayout->addWidget( TextLabel1 );

    scriptname = new KURLRequester( plainPage(), "KURLRequester1" );
    scriptname->setMode( KFile::File | KFile::LocalOnly  );
    scriptname->fileDialog()->setOperationMode( KFileDialog::Opening );

    scriptname->setFilter( i18n("*.krename|KRename undo scripts (*.krename)\n"
                                   "*|All Files (*)") );
    UndoDialogLayout->addWidget( scriptname );

    TextLabel2 = new TQLabel( plainPage(), "TextLabel2" );
    TextLabel2->setText( i18n( "<qt>Undo Scripts are normal shell scripts which can also be executed manually from the command line.</qt>" ) );
    UndoDialogLayout->addWidget( TextLabel2 );

    browser = new KTextBrowser( plainPage());
    browser->setWordWrap( TQTextEdit::NoWrap );
    browser->setTextFormat( TQt::RichText );

    UndoDialogLayout->addWidget( browser );

    setButtonText( KDialogBase::User1, i18n( "&Start" ) );

    connect( this, TQT_SIGNAL( user1Clicked() ), this, TQT_SLOT( start() ) );
    connect( scriptname, TQT_SIGNAL( textChanged( const TQString & ) ), this, TQT_SLOT( enableControls() ) );

    enableControls();
}

UndoDialog::~UndoDialog()
{ }

void UndoDialog::start()
{
    if( scriptname->url().right( 8 ) != ".krename" ) // EXTENSION
        if( KMessageBox::warningContinueCancel( this, i18n("This script does not seem "
                                        "to be a Krename undo script. Execution of this "
                                        "script can be dangerous. Continue ?") )
            == KMessageBox::Cancel )
            return;

    TDEProcess *proc = new TDEProcess;
    *proc << scriptname->url() << "--krename";

    enableButton( KDialogBase::User1, false );

    if( !proc->start( TDEProcess::NotifyOnExit, TDEProcess::AllOutput ) ) {
        KMessageBox::sorry( this, i18n("Unable to start the given undo script!") );
        enableButton( KDialogBase::User1, true );
        delete proc;
        return;
    }
    proc->resume();
    connect( proc, TQT_SIGNAL( receivedStdout( TDEProcess*, char*, int) ), this, TQT_SLOT( receive( TDEProcess*, char*, int ) ) );
    connect( proc, TQT_SIGNAL( receivedStderr( TDEProcess*, char*, int) ), this, TQT_SLOT( receiveErr( TDEProcess*, char*, int ) ) );
    connect( proc, TQT_SIGNAL( processExited( TDEProcess* ) ), this, TQT_SLOT( finished( TDEProcess* ) ) );
}

void UndoDialog::receive( TDEProcess*, char* buffer, int len )
{
    TQString text;
    for( int i = 0; i < len; i++ )
        text.append( buffer[i] );

    browser->setText( browser->text() + text + "<br>");
}

void UndoDialog::receiveErr( TDEProcess*, char* buffer, int len )
{
    TQString text = "<b>";
    for( int i = 0; i < len; i++ )
        text.append( buffer[i] );

    browser->setText( browser->text() + text + "</b><br>");
}

void UndoDialog::finished( TDEProcess* p )
{
    delete p;
    KMessageBox::information( this, i18n("Finished successfully") );
    enableControls();
}

void UndoDialog::enableControls()
{
    TQFileInfo fi( scriptname->url() );
    bool b = !scriptname->url().isEmpty() && fi.exists() && fi.isExecutable();
    enableButton( KDialogBase::User1, b );
}

void UndoDialog::setUndoScript( const TQString & filename )
{
    scriptname->setURL( filename );
}
    
#include "undodialog.moc"