summaryrefslogtreecommitdiffstats
path: root/kbabel/catalogmanager/validateprogress.cpp
blob: acfc36da809ef3e6e2e5b12c74dceaa125a08932 (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
/* ****************************************************************************
  This file is part of KBabel

  Copyright (C) 2002-2003 by Stanislav Visnovsky
                            <visnovsky@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; 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

  In addition, as a special exception, the copyright holders give
  permission to link the code of this program with any edition of
  the TQt library by Trolltech AS, Norway (or with modified versions
  of TQt that use the same license as TQt), and distribute linked
  combinations including the two.  You must obey the GNU General
  Public License in all respects for all of the code used other than
  TQt. If you modify this file, you may extend this exception to
  your version of the file, but you are not obligated to do so.  If
  you do not wish to do so, delete this exception statement from
  your version.
      
**************************************************************************** */
#include "validateprogress.h"
#include "validateprogresswidget.h"
#include "catmanlistitem.h"
#include "catmanresource.h"

#include <kdebug.h>
#include <kdatatool.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpopupmenu.h>
#include <kprogress.h>
#include <ksqueezedtextlabel.h>

#include <tqlistbox.h>
#include <tqtimer.h>

#define ID_ERROR_OPEN   1
#define ID_ERROR_IGNORE 2

// version identification for validation ignores
#define IGNOREFILE_VERSION 0x00

ValidateProgressDialog::ValidateProgressDialog(const TQString& ignoreURL, TQWidget *tqparent,const char *name)
		: KDialogBase(tqparent,name,true,i18n("Caption of dialog","Validation")
						, Close, Close)
		, _ignoreURL(ignoreURL), _tool(0), _stopped(false)
		, _ignoreFuzzy(false), _setAsFuzzy(false)

{
    _mainWidget = new ValidateProgressWidget(this);
    setMainWidget(_mainWidget);
    setInitialSize( TQSize(400, 300) );

    _errors.clear();
    _ignores.clear();

    readIgnores();

    _errorMenu = new KPopupMenu(this);
    _errorMenu->insertItem(i18n("&Open"),ID_ERROR_OPEN);
    _errorMenu->insertItem(i18n("&Ignore"),ID_ERROR_IGNORE);

    connect( this, TQT_SIGNAL(closeClicked()), this, TQT_SLOT(stop()));
    connect( _mainWidget->_errorList, TQT_SIGNAL( doubleClicked(TQListBoxItem *)),
	this, TQT_SLOT( errorItemDoubleClicked(TQListBoxItem *)));

    connect( _mainWidget->_errorList, TQT_SIGNAL( contextMenuRequested(TQListBoxItem *, const TQPoint &)),
	this, TQT_SLOT( showContextMenu(TQListBoxItem *, const TQPoint &)));
}

ValidateProgressDialog::~ValidateProgressDialog()
{
    writeIgnores();
}

void ValidateProgressDialog::validate( const KDataToolInfo &tool, const TQPtrList<CatManListItem> files )
{
    if( files.isEmpty() ) return;

    _errors.clear();

    KDataTool* t = tool.createTool();

    if( !t )
    {
	KMessageBox::error( this, i18n("Cannot instantiate a validation tool.\n"
	"Please check your installation."), i18n("Validation Tool Error") );
	return;
    }

    _tool = t;
    _toolID = tool.service()->library();
    _files = files;

    _mainWidget->_errorList->clear();
    _mainWidget->_currentTool->setText(*(tool.userCommands().at(0)));
    _mainWidget->_overallProgress->setTotalSteps(files.count());
    _mainWidget->_overallProgress->setValue(0);

    _stopped = false;

    TQTimer::singleShot( 0, this, TQT_SLOT(validate_internal()) );

    exec();

    _stopped = true;
}

void ValidateProgressDialog::validate_internal()
{
    uint checked=0;
    uint errors=0;
    uint ignorederrors=0;

    for( CatManListItem* it=_files.first() ; it && !_stopped ; it = _files.next() )
    {
	_mainWidget->_currentFile->setText( it->poFile() );

	checked++;

	_mainWidget->_currentFileProgress->setTotalSteps(100);
	_mainWidget->_currentFileProgress->setValue(0);

	it->checkErrors(_tool, TQT_TQOBJECT(_mainWidget), _ignoreFuzzy, _setAsFuzzy);

	bool noHeader = true;

	if( it->hasErrors() )
	{
	    TQValueList<IgnoreItem> err = it->errors();

	    for( TQValueList<IgnoreItem>::Iterator errit = err.begin();  errit!=err.end() ; ++errit )
	    {
		IgnoreItem item = (*errit);

		TQValueList<IgnoreItem>::Iterator ig;
		for( ig = _ignores.begin() ; ig != _ignores.end() ; ++ig )
		{
		    if( (*ig).validationTool == _toolID &&
		      (*ig).msgid == item.msgid &&
		      (*ig).msgstr == item.msgstr &&
		      (*ig).fileURL == item.fileURL ) break;
		}

		if( ig != _ignores.end() )
		{
		    ++ignorederrors;
		    continue;
		} ++errors;

		if( noHeader )
		{
		    _mainWidget->_errorList->insertItem( ICON_ERROR, it->package() );
		    _errors.insert( it->package(), err.first() );
		    noHeader = false;
		}

		TQString errortext=TQString::number(item.index+1)+": " + item.msgid.first().left(50);
	        errortext.tqreplace("\n"," ");
		if( item.msgid.first().length() > 50 ) errortext+="...";
		_mainWidget->_errorList->insertItem( errortext);

		_errors.insert( errortext, item );
	    }
	}

	_mainWidget->_currentFileProgress->setValue(100);

	_mainWidget->_overallProgress->advance(1);
    }

    if( !_stopped )
    {
	KMessageBox::information(this, i18n("Validation done.\n"
	"\n"
	"Checked files: %1\n"
	"Number of errors: %2\n"
	"Number of ignored errors: %3").tqarg(checked).tqarg(errors).tqarg(ignorederrors),i18n("Validation Done"));
    }

    delete _tool;
    _tool = 0;
    _files.clear();
}

void ValidateProgressDialog::stop()
{
    _stopped = true;
}

void ValidateProgressDialog::errorItemDoubleClicked(TQListBoxItem * item)
{
    TQString it = item->text();

    bool ok =false;
    int offset = it.tqfind(":");

    int num;
    if( offset < -1 ) num = 0;
    else
    {
	num = it.left(offset).toInt(&ok);
	if( !ok ) num = 0;
    }

    TQListBoxItem* package=item;

    while( package && !package->text().startsWith("/") ) package=package->prev();

    if( !package )
    {
	kdWarning() << "Unable to find the package for the error" << endl;
	return;
    }

    emit errorDoubleClicked(package->text(), num-1 );
}

void ValidateProgressDialog::showContextMenu(TQListBoxItem * item, const TQPoint & pos)
{
    // disable ignore for whole package
    _errorMenu->setItemEnabled(ID_ERROR_IGNORE, item->pixmap()==0 );
    int result = _errorMenu->exec(pos);
    switch( result )
    {
	case ID_ERROR_OPEN:
	    errorItemDoubleClicked( item );
	    break;
	case ID_ERROR_IGNORE:
	    IgnoreItem it = _errors.tqfind(item->text()).data();

	    // if there is no pixmap, it's the whole file
	    if( !item->pixmap() )
	    {
		it.validationTool = _toolID;
		_ignores.append( it );
	    }
	    break;
    }
}

void ValidateProgressDialog::readIgnores()
{
    IgnoreItem item;

    TQFile ignoreFile( _ignoreURL );
    if( ignoreFile.open( IO_ReadOnly ) ) {
        TQDataStream s( &ignoreFile );
	TQString url;
	int version;

	s >> version;
	if( version == IGNOREFILE_VERSION ) // Only read if correct versi
	{
	    _ignores.clear();

	    while( !s.atEnd() ) {
		s >> item;
                _ignores.append(item);
            }
	}
	ignoreFile.close();
    }
}

void ValidateProgressDialog::writeIgnores()
{
    TQFile ignoreFile( _ignoreURL );
    if( ignoreFile.open( IO_WriteOnly ) ) {
        TQDataStream s( &ignoreFile );
	TQString url;
	int version = IGNOREFILE_VERSION;

	s << version;

	for( TQValueList<IgnoreItem>::Iterator it = _ignores.begin(); it!=_ignores.end(); ++it)
	{
	    s << (*it);
	}
	ignoreFile.close();
    }
}

TQDataStream & operator<<( TQDataStream & stream, const IgnoreItem & i )
{
    return stream << i.fileURL
                  << i.msgid
		  << i.msgstr
		  << i.index
		  << i.validationTool;
}

TQDataStream & operator>>( TQDataStream & stream, IgnoreItem & i ) {
    return stream >> i.fileURL
		    >> i.msgid
		    >> i.msgstr
		    >> i.index
		    >> i.validationTool;
}

#include "validateprogress.moc"