~ubuntu-branches/ubuntu/intrepid/kdesdk/intrepid-updates

« back to all changes in this revision

Viewing changes to kbabel/catalogmanager/validateprogress.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-05-28 10:11:43 UTC
  • mto: This revision was merged to the branch mainline in revision 37.
  • Revision ID: james.westby@ubuntu.com-20080528101143-gzc3styjz1b70zxu
Tags: upstream-4.0.80
ImportĀ upstreamĀ versionĀ 4.0.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ****************************************************************************
2
 
  This file is part of KBabel
3
 
 
4
 
  Copyright (C) 2002-2003 by Stanislav Visnovsky
5
 
                            <visnovsky@kde.org>
6
 
 
7
 
  This program is free software; you can redistribute it and/or modify
8
 
  it under the terms of the GNU General Public License as published by
9
 
  the Free Software Foundation; either version 2 of the License, or
10
 
  (at your option) any later version.
11
 
 
12
 
  This program is distributed in the hope that it will be useful,
13
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
  GNU General Public License for more details.
16
 
 
17
 
  You should have received a copy of the GNU General Public License
18
 
  along with this program; if not, write to the Free Software
19
 
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 
 
21
 
  In addition, as a special exception, the copyright holders give
22
 
  permission to link the code of this program with any edition of
23
 
  the Qt library by Trolltech AS, Norway (or with modified versions
24
 
  of Qt that use the same license as Qt), and distribute linked
25
 
  combinations including the two.  You must obey the GNU General
26
 
  Public License in all respects for all of the code used other than
27
 
  Qt. If you modify this file, you may extend this exception to
28
 
  your version of the file, but you are not obligated to do so.  If
29
 
  you do not wish to do so, delete this exception statement from
30
 
  your version.
31
 
      
32
 
**************************************************************************** */
33
 
#include "validateprogress.h"
34
 
#include "validateprogresswidget.h"
35
 
#include "catmanlistitem.h"
36
 
#include "catmanresource.h"
37
 
 
38
 
#include <kdebug.h>
39
 
#include <kdatatool.h>
40
 
#include <klocale.h>
41
 
#include <kmessagebox.h>
42
 
#include <kpopupmenu.h>
43
 
#include <kprogress.h>
44
 
#include <ksqueezedtextlabel.h>
45
 
 
46
 
#include <qlistbox.h>
47
 
#include <qtimer.h>
48
 
 
49
 
#define ID_ERROR_OPEN   1
50
 
#define ID_ERROR_IGNORE 2
51
 
 
52
 
// version identification for validation ignores
53
 
#define IGNOREFILE_VERSION 0x00
54
 
 
55
 
ValidateProgressDialog::ValidateProgressDialog(const QString& ignoreURL, QWidget *parent,const char *name)
56
 
                : KDialogBase(parent,name,true,i18n("Caption of dialog","Validation")
57
 
                                                , Close, Close)
58
 
                , _ignoreURL(ignoreURL), _tool(0), _stopped(false)
59
 
                , _ignoreFuzzy(false), _setAsFuzzy(false)
60
 
 
61
 
{
62
 
    _mainWidget = new ValidateProgressWidget(this);
63
 
    setMainWidget(_mainWidget);
64
 
    setInitialSize( QSize(400, 300) );
65
 
 
66
 
    _errors.clear();
67
 
    _ignores.clear();
68
 
 
69
 
    readIgnores();
70
 
 
71
 
    _errorMenu = new KPopupMenu(this);
72
 
    _errorMenu->insertItem(i18n("&Open"),ID_ERROR_OPEN);
73
 
    _errorMenu->insertItem(i18n("&Ignore"),ID_ERROR_IGNORE);
74
 
 
75
 
    connect( this, SIGNAL(closeClicked()), this, SLOT(stop()));
76
 
    connect( _mainWidget->_errorList, SIGNAL( doubleClicked(QListBoxItem *)),
77
 
        this, SLOT( errorItemDoubleClicked(QListBoxItem *)));
78
 
 
79
 
    connect( _mainWidget->_errorList, SIGNAL( contextMenuRequested(QListBoxItem *, const QPoint &)),
80
 
        this, SLOT( showContextMenu(QListBoxItem *, const QPoint &)));
81
 
}
82
 
 
83
 
ValidateProgressDialog::~ValidateProgressDialog()
84
 
{
85
 
    writeIgnores();
86
 
}
87
 
 
88
 
void ValidateProgressDialog::validate( const KDataToolInfo &tool, const QPtrList<CatManListItem> files )
89
 
{
90
 
    if( files.isEmpty() ) return;
91
 
 
92
 
    _errors.clear();
93
 
 
94
 
    KDataTool* t = tool.createTool();
95
 
 
96
 
    if( !t )
97
 
    {
98
 
        KMessageBox::error( this, i18n("Cannot instantiate a validation tool.\n"
99
 
        "Please check your installation."), i18n("Validation Tool Error") );
100
 
        return;
101
 
    }
102
 
 
103
 
    _tool = t;
104
 
    _toolID = tool.service()->library();
105
 
    _files = files;
106
 
 
107
 
    _mainWidget->_errorList->clear();
108
 
    _mainWidget->_currentTool->setText(*(tool.userCommands().at(0)));
109
 
    _mainWidget->_overallProgress->setTotalSteps(files.count());
110
 
    _mainWidget->_overallProgress->setValue(0);
111
 
 
112
 
    _stopped = false;
113
 
 
114
 
    QTimer::singleShot( 0, this, SLOT(validate_internal()) );
115
 
 
116
 
    exec();
117
 
 
118
 
    _stopped = true;
119
 
}
120
 
 
121
 
void ValidateProgressDialog::validate_internal()
122
 
{
123
 
    uint checked=0;
124
 
    uint errors=0;
125
 
    uint ignorederrors=0;
126
 
 
127
 
    for( CatManListItem* it=_files.first() ; it && !_stopped ; it = _files.next() )
128
 
    {
129
 
        _mainWidget->_currentFile->setText( it->poFile() );
130
 
 
131
 
        checked++;
132
 
 
133
 
        _mainWidget->_currentFileProgress->setTotalSteps(100);
134
 
        _mainWidget->_currentFileProgress->setValue(0);
135
 
 
136
 
        it->checkErrors(_tool,_mainWidget, _ignoreFuzzy, _setAsFuzzy);
137
 
 
138
 
        bool noHeader = true;
139
 
 
140
 
        if( it->hasErrors() )
141
 
        {
142
 
            QValueList<IgnoreItem> err = it->errors();
143
 
 
144
 
            for( QValueList<IgnoreItem>::Iterator errit = err.begin();  errit!=err.end() ; ++errit )
145
 
            {
146
 
                IgnoreItem item = (*errit);
147
 
 
148
 
                QValueList<IgnoreItem>::Iterator ig;
149
 
                for( ig = _ignores.begin() ; ig != _ignores.end() ; ++ig )
150
 
                {
151
 
                    if( (*ig).validationTool == _toolID &&
152
 
                      (*ig).msgid == item.msgid &&
153
 
                      (*ig).msgstr == item.msgstr &&
154
 
                      (*ig).fileURL == item.fileURL ) break;
155
 
                }
156
 
 
157
 
                if( ig != _ignores.end() )
158
 
                {
159
 
                    ++ignorederrors;
160
 
                    continue;
161
 
                } ++errors;
162
 
 
163
 
                if( noHeader )
164
 
                {
165
 
                    _mainWidget->_errorList->insertItem( ICON_ERROR, it->package() );
166
 
                    _errors.insert( it->package(), err.first() );
167
 
                    noHeader = false;
168
 
                }
169
 
 
170
 
                QString errortext=QString::number(item.index+1)+": " + item.msgid.first().left(50);
171
 
                errortext.replace("\n"," ");
172
 
                if( item.msgid.first().length() > 50 ) errortext+="...";
173
 
                _mainWidget->_errorList->insertItem( errortext);
174
 
 
175
 
                _errors.insert( errortext, item );
176
 
            }
177
 
        }
178
 
 
179
 
        _mainWidget->_currentFileProgress->setValue(100);
180
 
 
181
 
        _mainWidget->_overallProgress->advance(1);
182
 
    }
183
 
 
184
 
    if( !_stopped )
185
 
    {
186
 
        KMessageBox::information(this, i18n("Validation done.\n"
187
 
        "\n"
188
 
        "Checked files: %1\n"
189
 
        "Number of errors: %2\n"
190
 
        "Number of ignored errors: %3").arg(checked).arg(errors).arg(ignorederrors),i18n("Validation Done"));
191
 
    }
192
 
 
193
 
    delete _tool;
194
 
    _tool = 0;
195
 
    _files.clear();
196
 
}
197
 
 
198
 
void ValidateProgressDialog::stop()
199
 
{
200
 
    _stopped = true;
201
 
}
202
 
 
203
 
void ValidateProgressDialog::errorItemDoubleClicked(QListBoxItem * item)
204
 
{
205
 
    QString it = item->text();
206
 
 
207
 
    bool ok =false;
208
 
    int offset = it.find(":");
209
 
 
210
 
    int num;
211
 
    if( offset < -1 ) num = 0;
212
 
    else
213
 
    {
214
 
        num = it.left(offset).toInt(&ok);
215
 
        if( !ok ) num = 0;
216
 
    }
217
 
 
218
 
    QListBoxItem* package=item;
219
 
 
220
 
    while( package && !package->text().startsWith("/") ) package=package->prev();
221
 
 
222
 
    if( !package )
223
 
    {
224
 
        kdWarning() << "Unable to find the package for the error" << endl;
225
 
        return;
226
 
    }
227
 
 
228
 
    emit errorDoubleClicked(package->text(), num-1 );
229
 
}
230
 
 
231
 
void ValidateProgressDialog::showContextMenu(QListBoxItem * item, const QPoint & pos)
232
 
{
233
 
    // disable ignore for whole package
234
 
    _errorMenu->setItemEnabled(ID_ERROR_IGNORE, item->pixmap()==0 );
235
 
    int result = _errorMenu->exec(pos);
236
 
    switch( result )
237
 
    {
238
 
        case ID_ERROR_OPEN:
239
 
            errorItemDoubleClicked( item );
240
 
            break;
241
 
        case ID_ERROR_IGNORE:
242
 
            IgnoreItem it = _errors.find(item->text()).data();
243
 
 
244
 
            // if there is no pixmap, it's the whole file
245
 
            if( !item->pixmap() )
246
 
            {
247
 
                it.validationTool = _toolID;
248
 
                _ignores.append( it );
249
 
            }
250
 
            break;
251
 
    }
252
 
}
253
 
 
254
 
void ValidateProgressDialog::readIgnores()
255
 
{
256
 
    IgnoreItem item;
257
 
 
258
 
    QFile ignoreFile( _ignoreURL );
259
 
    if( ignoreFile.open( IO_ReadOnly ) ) {
260
 
        QDataStream s( &ignoreFile );
261
 
        QString url;
262
 
        int version;
263
 
 
264
 
        s >> version;
265
 
        if( version == IGNOREFILE_VERSION ) // Only read if correct versi
266
 
        {
267
 
            _ignores.clear();
268
 
 
269
 
            while( !s.atEnd() ) {
270
 
                s >> item;
271
 
                _ignores.append(item);
272
 
            }
273
 
        }
274
 
        ignoreFile.close();
275
 
    }
276
 
}
277
 
 
278
 
void ValidateProgressDialog::writeIgnores()
279
 
{
280
 
    QFile ignoreFile( _ignoreURL );
281
 
    if( ignoreFile.open( IO_WriteOnly ) ) {
282
 
        QDataStream s( &ignoreFile );
283
 
        QString url;
284
 
        int version = IGNOREFILE_VERSION;
285
 
 
286
 
        s << version;
287
 
 
288
 
        for( QValueList<IgnoreItem>::Iterator it = _ignores.begin(); it!=_ignores.end(); ++it)
289
 
        {
290
 
            s << (*it);
291
 
        }
292
 
        ignoreFile.close();
293
 
    }
294
 
}
295
 
 
296
 
QDataStream & operator<<( QDataStream & stream, const IgnoreItem & i )
297
 
{
298
 
    return stream << i.fileURL
299
 
                  << i.msgid
300
 
                  << i.msgstr
301
 
                  << i.index
302
 
                  << i.validationTool;
303
 
}
304
 
 
305
 
QDataStream & operator>>( QDataStream & stream, IgnoreItem & i ) {
306
 
    return stream >> i.fileURL
307
 
                    >> i.msgid
308
 
                    >> i.msgstr
309
 
                    >> i.index
310
 
                    >> i.validationTool;
311
 
}
312
 
 
313
 
#include "validateprogress.moc"