summaryrefslogtreecommitdiffstats
path: root/krename/commandplugin.cpp
blob: 3c790281d3699ddd0834a8319da88a20fae3dc1a (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
/***************************************************************************
                          commandplugin.cpp  -  description
                             -------------------
    begin                : Son Jan 5 2003
    copyright            : (C) 2003 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 "commandplugin.h"

// QT includes
#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqlayout.h>

// KDE includes
#include <tdeapplication.h>
#include <tdeconfig.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <klineedit.h>
#include <tdelistbox.h>
#include <tdemessagebox.h>
#include <kpushbutton.h>
#include <kprocess.h>

const TQString CommandPlugin::getName() const
{
    return i18n("Command Plugin");
}

const TQString CommandPlugin::getAccelName() const
{
    return i18n("&Command Plugin");
}

const int CommandPlugin::type() const
{
    return TYPE_FINAL_FILE;
}

bool CommandPlugin::checkError()
{
    if( commandline->text().isEmpty() ) {
        KMessageBox::error( 0, i18n("You did not specify a command to execute.") );
        return false;
    }

    return true;
}

void CommandPlugin::drawInterface( TQWidget* w, TQVBoxLayout* l )
{
    m_widget = w;

    TQHBoxLayout* hb = new TQHBoxLayout( 0, 0, 6 );
    TQVBoxLayout* vb = new TQVBoxLayout( 0, 0, 6 );

    TQLabel* la = new TQLabel( w );
    la->setText( i18n("<b>Command Plugin</b>") );
    l->addWidget( la );

    la = new TQLabel( w );
    la->setText( i18n( "<qt>Executes a shell command on every file after it has been renamed. "
                       "Add %1 to the command line arguments to get the filename of the renamed file.</qt>") );
    l->addWidget( la );
    l->addWidget( new TQLabel( i18n("Command:"), w ) );

    commandline = new KLineEdit( w );
    l->addWidget( commandline );

    checkNoBlock = new TQCheckBox( i18n("&Execute without blocking (not recommended)"), w );
    l->addWidget( checkNoBlock );

    buttonAdd = new KPushButton( i18n("&Add"), w );
    buttonRemove = new KPushButton( i18n("&Remove"), w );
    hb->addWidget( buttonAdd );
    hb->addWidget( buttonRemove );

    vb->addLayout( hb );

    list = new TDEListBox( w );
    vb->addWidget( list );
    vb->setStretchFactor( list, 2 );
    l->addLayout( vb );

    connect( buttonAdd, TQT_SIGNAL( clicked() ), this, TQT_SLOT( add() ) );
    connect( buttonRemove, TQT_SIGNAL( clicked() ), this, TQT_SLOT( remove() ) );
    connect( list, TQT_SIGNAL( executed( TQListBoxItem* ) ), this, TQT_SLOT( exec() ) );

    TDEConfig* conf = kapp->config();
    conf->setGroup("CommandPlugin");
    list->insertStringList( conf->readListEntry("commandlines" ) );
    
    TQStringList examples;
    examples.append( "chmod 0444 %1" );
    examples.append( "convert %1 %1.png" );
    examples.append( "echo %1 >> $HOME/file.list" );
//    examples.append( ")    
    for( unsigned int i = 0; i < examples.count(); i++ )
        if( !list->findItem( examples[i] ) )
            list->insertItem( examples[i] );
}

void CommandPlugin::fillStructure()
{
    command = commandline->text();
    noblock = checkNoBlock->isChecked();
}

TQString CommandPlugin::processFile( BatchRenamer* b, int i, TQString, int )
{
    TQString filename = b->files()[i].dst.name;
    

    TQString c = command;
       
    KShellProcess proc;
    c = c.replace( "%1", KShellProcess::quote( filename ) );

    proc << c;
    
    if( noblock )
        proc.start( TDEProcess::DontCare, TDEProcess::NoCommunication );
    else
        proc.start( TDEProcess::Block, TDEProcess::NoCommunication );

    proc.resume();

    if( !noblock && proc.exitStatus() )
        return command.arg( filename ) + i18n(" exited with error: %1").arg( proc.exitStatus() );

    return TQString();
}

void CommandPlugin::finished()
{
    TDEConfig* conf = kapp->config();
    conf->setGroup("CommandPlugin");
    TQStringList slist;
    for( unsigned int i = 0; i < list->count(); i++ )
        slist.append( list->text( i ) );

    conf->writeEntry("commandlines", slist );
    conf->sync();
    return;
}

void CommandPlugin::add()
{
    if( !commandline->text().isEmpty() ) {
        for( unsigned int i = 0; i < list->count(); i++ )
            if( list->text( i ) == commandline->text() )
                return;

        list->insertItem( commandline->text() );
    }
}

void CommandPlugin::remove()
{
    unsigned int i = 0;
    do {
        if(list->isSelected( i ))
            list->removeItem( i );
        else
            i++;
    } while( i < list->count() );
}

void CommandPlugin::exec()
{
    for( unsigned int i = 0; i < list->count(); i++ )
        if( list->isSelected( i ) )
            commandline->setText( list->text( i ) );
}

const TQPixmap CommandPlugin::getIcon() const
{
    return kapp->iconLoader()->loadIcon( "konsole", TDEIcon::Small );
}

#include "commandplugin.moc"