summaryrefslogtreecommitdiffstats
path: root/kmrml/kmrml/kcontrol/indexcleaner.cpp
blob: c184c10c7afae77a12106026bf3480c3438d486c (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
#include <kdebug.h>
#include <kprocess.h>

#include <kmrml_config.h>
#include "indexcleaner.h"

#include <tdeversion.h>
#if TDE_VERSION < 306
  #define TQUOTE( x ) x
#else
  #define TQUOTE( x ) KProcess::quote( x )
#endif

using namespace KMrmlConfig;

IndexCleaner::IndexCleaner( const TQStringList& dirs,
                            const KMrml::Config *config,
                            TQObject *parent, const char *name )
    : TQObject( parent, name ),
      m_dirs( dirs ),
      m_config( config ),
      m_process( 0L )
{
    m_stepSize = 100 / dirs.count();
}

IndexCleaner::~IndexCleaner()
{
    if ( m_process )
    {
        m_process->kill();
        delete m_process;
        m_process = 0L;
    }
}

void IndexCleaner::start()
{
    startNext();
}

void IndexCleaner::slotExited( KProcess *proc )
{
    emit advance( m_stepSize );

    if ( !proc->normalExit() )
        kdWarning() << "Error removing old indexed directory" << endl;

    m_process = 0L;

    startNext();
}

void IndexCleaner::startNext()
{
    if ( m_dirs.isEmpty() )
    {
        emit advance( 100 );
        emit finished();
        return;
    }

#if TDE_VERSION < 306
    m_process = new KShellProcess();
#else
    m_process = new KProcess();
    m_process->setUseShell( true );
#endif
    connect( m_process, TQT_SIGNAL( processExited( KProcess * )),
             TQT_SLOT( slotExited( KProcess * ) ));

    TQString cmd = m_config->removeCollectionCommandLine();

    TQString dir = m_dirs.first();
    m_dirs.pop_front();

    int index = cmd.find( "%d" );
    if ( index != -1 )
        cmd.replace( index, 2, TQUOTE( dir ) );
    else // no %d? What else can we do?
        cmd.append( TQString::fromLatin1(" ") + TQUOTE( dir ) );

    *m_process << cmd;

    if ( !m_process->start() )
    {
        kdWarning() << "Error starting: " << cmd << endl;

        delete m_process;
        m_process = 0L;

        startNext();
    }
}

#include "indexcleaner.moc"