summaryrefslogtreecommitdiffstats
path: root/kmrml/kmrml/kcontrol/mainpage.cpp
blob: ca491ead12ea15088c202cf35f7672a384b054e9 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/* This file is part of the KDE project
   Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>

   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, version 2.

   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
    General Public License for more details.

   You should have received a copy of the GNU 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 <tqcheckbox.h>
#include <tqlabel.h>
#include <tqsizepolicy.h>
#include <tqtooltip.h>
#include <tqwidget.h>
#include <tqvgroupbox.h>

#include <kcombobox.h>
#include <kdialog.h>
#include <keditlistbox.h>
#include <kglobalsettings.h>
#include <klineedit.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <knuminput.h>
#include <kprogress.h>
#include <kurlrequester.h>

#include <tdeversion.h>
#include <kdebug.h>
#include <kio/slaveconfig.h>
#include <kio/ioslave_defaults.h> // MAX_PORT_VALUE

#include "serverconfigwidget.h"
#include "mainpage.h"
#include "indexer.h"
#include "indexcleaner.h"

#include <limits.h>
#include <string.h>

using namespace KMrmlConfig;


MainPage::MainPage( TQWidget *parent, const char *name )
    : TQVBox( parent, name ),
      m_indexer( 0L ),
      m_indexCleaner( 0L ),
      m_progressDialog( 0L ),
      m_performIndexing( false ),
      m_locked( false )
{
    m_config = new KMrml::Config();
    setSpacing( KDialog::spacingHint() );

    TQVGroupBox *gBox = new TQVGroupBox( i18n("Indexing Server Configuration"),
                                       this );
    m_serverWidget = new ServerConfigWidget( gBox, "server config widget" );
    TQString tip = i18n("Hostname of the Indexing Server");
    TQToolTip::add( m_serverWidget->m_hostLabel, tip );
    TQToolTip::add( m_serverWidget->m_hostCombo, tip );

    m_serverWidget->m_portInput->setRange( 0, MAX_PORT_VALUE );

#if TDE_VERSION >= 306
    KURLRequester *requester = new KURLRequester( this, "dir requester" );
    requester->setMode( KFile::Directory | KFile::ExistingOnly | KFile::LocalOnly );
    requester->setURL( KGlobalSettings::documentPath() );
    connect( requester, TQT_SIGNAL( openFileDialog( KURLRequester * )),
             TQT_SLOT( slotRequesterClicked( KURLRequester * )));

    m_listBox = new KEditListBox( i18n("Folders to Be Indexed" ),
                                  requester->customEditor(), this, "listbox",
                                  false,
                                  KEditListBox::Add |  KEditListBox::Remove );
#else
    m_listBox = new KEditListBox( i18n("Folders to Be Indexed" ),
                                  this, "listbox", false,
                                  KEditListBox::Add |  KEditListBox::Remove );
#endif

    connect( m_listBox, TQT_SIGNAL( changed() ), TQT_SLOT( slotDirectoriesChanged() ));
    connect( m_serverWidget->m_hostCombo, TQT_SIGNAL( textChanged(const TQString&)),
             TQT_SLOT( slotHostChanged() ));
    connect( m_serverWidget->m_portInput, TQT_SIGNAL( valueChanged( int )),
             TQT_SLOT( slotPortChanged( int ) ));
    connect ( m_serverWidget->m_useAuth, TQT_SIGNAL( toggled(bool) ),
              TQT_SLOT( slotUseAuthChanged( bool ) ));
    connect( m_serverWidget->m_userEdit, TQT_SIGNAL( textChanged( const TQString&)),
             TQT_SLOT( slotUserChanged( const TQString& ) ));
    connect( m_serverWidget->m_passEdit, TQT_SIGNAL( textChanged( const TQString&)),
             TQT_SLOT( slotPassChanged( const TQString& ) ));

    connect( m_serverWidget->m_addButton, TQT_SIGNAL( clicked() ),
             TQT_SLOT( slotAddClicked() ));
    connect( m_serverWidget->m_removeButton, TQT_SIGNAL( clicked() ),
             TQT_SLOT( slotRemoveClicked() ));

    connect( m_serverWidget->m_hostCombo, TQT_SIGNAL( activated( const TQString& )),
             TQT_SLOT( slotHostActivated( const TQString& )));
    connect( m_serverWidget->m_hostCombo, TQT_SIGNAL( returnPressed() ),
             TQT_SLOT( slotAddClicked() ));

    connect( m_serverWidget->m_autoPort, TQT_SIGNAL( toggled( bool ) ),
             TQT_SLOT( slotAutoPortChanged( bool ) ));

    m_serverWidget->m_hostCombo->setTrapReturnKey( true );
    m_serverWidget->m_hostCombo->setFocus();
}

MainPage::~MainPage()
{
    delete m_config;
}

void MainPage::resetDefaults()
{
    blockSignals( true );

    initFromSettings( KMrml::ServerSettings::defaults() );

    m_serverWidget->m_hostCombo->clear();
    m_serverWidget->m_hostCombo->insertItem( m_settings.host );

    m_listBox->clear();

    // slotHostChanged(); not necessary, will be called by TQt signals
    slotUseAuthChanged( m_serverWidget->m_useAuth->isChecked() );

    blockSignals( false );
}

void MainPage::load()
{
    blockSignals( true );

    initFromSettings( m_config->defaultSettings() );

    m_serverWidget->m_hostCombo->clear();
    m_serverWidget->m_hostCombo->insertStringList( m_config->hosts() );
    m_serverWidget->m_hostCombo->setCurrentItem( m_settings.host );

    m_listBox->clear();
    m_listBox->insertStringList( m_config->indexableDirectories() );

    // slotHostChanged(); not necessary, will be called by TQt signals
    slotUseAuthChanged( m_serverWidget->m_useAuth->isChecked() );

    blockSignals( false );
}

void MainPage::save()
{
    m_config->addSettings( m_settings );
    m_config->setDefaultHost( m_settings.host );

    TQStringList indexDirs = m_listBox->items();
    TQStringList oldIndexDirs = m_config->indexableDirectories();
    TQStringList removedDirs = difference( oldIndexDirs, indexDirs );

    m_config->setIndexableDirectories( indexDirs );
    if ( indexDirs.isEmpty() )
        KMessageBox::information( this,
                                  i18n("You did not specify any folders to "
                                       "be indexed. This means you will be "
                                       "unable to perform queries on your "
                                       "computer."),
                                  "kcmkmrml_no_directories_specified" );

    if ( m_config->sync() )
        KIO::SlaveConfig::self()->reset();

    processIndexDirs( removedDirs );
}

TQStringList MainPage::difference( const TQStringList& oldIndexDirs,
                                  const TQStringList& newIndexDirs ) const
{
    TQStringList result;

    TQString slash = TQString::fromLatin1("/");
    TQStringList::ConstIterator oldIt = oldIndexDirs.begin();
    TQString oldDir, newDir;

    for ( ; oldIt != oldIndexDirs.end(); oldIt++ )
    {
        bool removed = true;
        oldDir = *oldIt;

        while ( oldDir.endsWith( slash ) ) // remove slashes
            oldDir.remove( oldDir.length() - 1, 1 );

        TQStringList::ConstIterator newIt = newIndexDirs.begin();
        for ( ; newIt != newIndexDirs.end(); newIt++ )
        {
            newDir = *newIt;
            while ( newDir.endsWith( slash ) ) // remove slashes
                newDir.remove( newDir.length() - 1, 1 );

            if ( oldDir == newDir )
            {
                removed = false;
                break;
            }
        }

        if ( removed )
            result.append( *oldIt ); // not oldDir -- maybe gift needs slashes
    }

    return result;
}

void MainPage::initFromSettings( const KMrml::ServerSettings& settings )
{
    m_settings = settings;

    m_locked = true;

    m_serverWidget->m_portInput->setValue( settings.configuredPort );
    m_serverWidget->m_autoPort->setChecked( settings.autoPort );
    m_serverWidget->m_useAuth->setChecked( settings.useAuth );
    m_serverWidget->m_userEdit->setText( settings.user );
    m_serverWidget->m_passEdit->setText( settings.pass );

    m_locked = false;
}

void MainPage::slotHostActivated( const TQString& host )
{
    // implicitly save the current settings when another host was chosen
    m_config->addSettings( m_settings );

    initFromSettings( m_config->settingsForHost( host ) );
}

void MainPage::slotHostChanged()
{
    TQString host = m_serverWidget->m_hostCombo->currentText();
    m_listBox->setEnabled( (host == "localhost") );

    KMrml::ServerSettings settings = m_config->settingsForHost( host );
    enableWidgetsFor( settings );
}

void MainPage::slotUseAuthChanged( bool enable )
{
    m_settings.useAuth = enable;
    m_serverWidget->m_userEdit->setEnabled( enable );
    m_serverWidget->m_passEdit->setEnabled( enable );

    if ( enable )
        m_serverWidget->m_userEdit->setFocus();

    if ( !m_locked )
        changed();
}

void MainPage::slotUserChanged( const TQString& user )
{
    if ( m_locked )
        return;

    m_settings.user = user;
    changed();
}

void MainPage::slotPassChanged( const TQString& pass )
{
    if ( m_locked )
        return;

    m_settings.pass = pass;
    changed();
}

void MainPage::slotPortChanged( int port )
{
    if ( m_locked )
        return;

    m_settings.configuredPort = (unsigned short int) port;
    changed();
}

void MainPage::slotAutoPortChanged( bool on )
{
    if ( m_locked )
        return;

    m_settings.autoPort = on;
    m_serverWidget->m_portInput->setEnabled( !on );
    changed();
}

void MainPage::slotRequesterClicked( KURLRequester *requester )
{
    static bool init = true;
    if ( !init )
        return;

    init = false;

    requester->setCaption(i18n("Select Folder You Want to Index"));
}

void MainPage::slotAddClicked()
{
    TQString host = m_serverWidget->m_hostCombo->currentText();
    m_settings.host = host;

    m_config->addSettings( m_settings );
    m_serverWidget->m_hostCombo->insertItem( host );
    m_serverWidget->m_hostCombo->setCurrentItem( host );

    enableWidgetsFor( m_settings );
}

void MainPage::slotRemoveClicked()
{
    TQString host = m_serverWidget->m_hostCombo->currentText();
    if ( host.isEmpty() ) // should never happen
        return;

    m_config->removeSettings( host );
    m_serverWidget->m_hostCombo->removeItem( m_serverWidget->m_hostCombo->currentItem() );
    m_serverWidget->m_hostCombo->setCurrentItem( 0 );

    host = m_serverWidget->m_hostCombo->currentText();
    initFromSettings( m_config->settingsForHost( host ) );
}

void MainPage::enableWidgetsFor( const KMrml::ServerSettings& settings )
{
    TQString host = settings.host;
    bool enableWidgets = (m_config->hosts().findIndex( host ) > -1);
    m_serverWidget->m_addButton->setEnabled(!enableWidgets && !host.isEmpty());
    m_serverWidget->m_removeButton->setEnabled( enableWidgets &&
                                                !host.isEmpty() &&
                                                host != "localhost" );

    m_serverWidget->m_autoPort->setEnabled( host == "localhost" );
    bool portEnable = enableWidgets && (settings.autoPort ||
                                        !m_serverWidget->m_autoPort->isEnabled());
    m_serverWidget->m_portLabel->setEnabled( portEnable && !m_serverWidget->m_autoPort->isChecked());
    m_serverWidget->m_portInput->setEnabled( portEnable && !m_serverWidget->m_autoPort->isChecked());

    m_serverWidget->m_useAuth->setEnabled( enableWidgets );
    m_serverWidget->m_userLabel->setEnabled( enableWidgets );
    m_serverWidget->m_passLabel->setEnabled( enableWidgets );
    m_serverWidget->m_userEdit->setEnabled( enableWidgets );
    m_serverWidget->m_passEdit->setEnabled( enableWidgets );

    bool useAuth = m_serverWidget->m_useAuth->isChecked();
    m_serverWidget->m_userEdit->setEnabled( useAuth );
    m_serverWidget->m_passEdit->setEnabled( useAuth );
}

void MainPage::slotDirectoriesChanged()
{
    m_performIndexing = true;
    changed();
}

void MainPage::processIndexDirs( const TQStringList& removeDirs )
{
    // ### how to remove indexed directories?
    if ( !m_performIndexing ||
         (removeDirs.isEmpty() && m_config->indexableDirectories().isEmpty()) )
        return;

    delete m_progressDialog;
    delete m_indexCleaner;
    m_indexCleaner = 0L;
    delete m_indexer;
    m_indexer = 0L;

    m_progressDialog = new KProgressDialog( this, "indexing dialog",
                                            i18n("Removing old Index Files"),
                                            i18n("Processing..."),
                                            true );
    m_progressDialog->setAutoClose( false );
    m_progressDialog->setMinimumWidth( 300 );
    connect( m_progressDialog, TQT_SIGNAL( cancelClicked() ),
             TQT_SLOT( slotCancelIndexing() ));

    // argh -- don't automatically show the dialog
    m_progressDialog->setMinimumDuration( INT_MAX );

    if ( !removeDirs.isEmpty() )
    {
        m_indexCleaner = new IndexCleaner( removeDirs, m_config, TQT_TQOBJECT(this) );
        connect( m_indexCleaner, TQT_SIGNAL( advance( int ) ),
                 m_progressDialog->progressBar(), TQT_SLOT( advance( int ) ));
        connect( m_indexCleaner, TQT_SIGNAL( finished() ),
                 TQT_SLOT( slotMaybeIndex() ) );
        m_indexCleaner->start();
    }
    else
    {
        slotMaybeIndex();
    }
    if ( m_progressDialog )
        m_progressDialog->exec();
}

void MainPage::slotMaybeIndex()
{
    delete m_indexCleaner; // Stop in the name of the law!
    m_indexCleaner = 0L;

    m_progressDialog->setLabel( i18n("Finished.") );

    if ( m_config->indexableDirectories().isEmpty() )
        return;

    if ( KMessageBox::questionYesNo( this,
                                     i18n("The settings have been saved. Now, "
                                          "the configured directories need to "
                                          "be indexed. This may take a while. "
                                          "Do you want to do this now?"),
                                     i18n("Start Indexing Now?"),
                                     i18n("Index"), i18n("Do Not Index"),
                                     "ask_startIndexing"
             ) != KMessageBox::Yes )
        return;
    m_progressDialog->setCaption( i18n("Indexing Folders") );
    m_progressDialog->setLabel( i18n("Processing...") );
    m_progressDialog->progressBar()->setProgress( 0 );

    // do the indexing
    m_indexer = new Indexer( m_config, TQT_TQOBJECT(this), "Indexer" );
    connect( m_indexer, TQT_SIGNAL( progress( int, const TQString& )),
             TQT_SLOT( slotIndexingProgress( int, const TQString& ) ));
    connect( m_indexer, TQT_SIGNAL( finished( int )),
             TQT_SLOT( slotIndexingFinished( int ) ));
    m_indexer->startIndexing( m_config->indexableDirectories() );
}


void MainPage::slotIndexingProgress( int percent, const TQString& message )
{
    m_progressDialog->progressBar()->setValue( percent );
    m_progressDialog->setLabel( message );
}

void MainPage::slotIndexingFinished( int returnCode )
{
    if ( returnCode != 0 )
    {
        TQString syserr;
        if ( returnCode == 127 )
            syserr = i18n("Is the \"GNU Image Finding Tool\" properly installed?");
        else
	{
            char *err = strerror( returnCode );
            if ( err )
                syserr = TQString::fromLocal8Bit( err );
            else
                syserr = i18n("Unknown error: %1").arg( returnCode );
        }

        KMessageBox::detailedError( this, i18n("An error occurred during indexing. The index might be invalid."),
                                    syserr, i18n("Indexing Aborted") );
    }
    else
        m_performIndexing = false;

    delete m_indexer;
    m_indexer = 0L;
    if ( m_progressDialog )
    {
        m_progressDialog->deleteLater();
        m_progressDialog = 0L;
    }
}

void MainPage::slotCancelIndexing()
{
    delete m_indexCleaner;
    m_indexCleaner = 0L;

    delete m_indexer;
    m_indexer = 0L;
    if ( m_progressDialog )
    {
        m_progressDialog->deleteLater();
        m_progressDialog = 0L;
    }
}


#include "mainpage.moc"