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

« back to all changes in this revision

Viewing changes to kbabel/kbabel/kbabelview2.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) 1999-2000 by Matthias Kiefer
5
 
                            <matthias.kiefer@gmx.de>
6
 
                2002-2004 by Stanislav Visnovsky
7
 
                            <visnovsky@kde.org>
8
 
 
9
 
  This program is free software; you can redistribute it and/or modify
10
 
  it under the terms of the GNU General Public License as published by
11
 
  the Free Software Foundation; either version 2 of the License, or
12
 
  (at your option) any later version.
13
 
 
14
 
  This program is distributed in the hope that it will be useful,
15
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
  GNU General Public License for more details.
18
 
 
19
 
  You should have received a copy of the GNU General Public License
20
 
  along with this program; if not, write to the Free Software
21
 
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22
 
 
23
 
  In addition, as a special exception, the copyright holders give
24
 
  permission to link the code of this program with any edition of
25
 
  the Qt library by Trolltech AS, Norway (or with modified versions
26
 
  of Qt that use the same license as Qt), and distribute linked
27
 
  combinations including the two.  You must obey the GNU General
28
 
  Public License in all respects for all of the code used other than
29
 
  Qt. If you modify this file, you may extend this exception to
30
 
  your version of the file, but you are not obligated to do so.  If
31
 
  you do not wish to do so, delete this exception statement from
32
 
  your version.
33
 
 
34
 
**************************************************************************** */
35
 
 
36
 
#include <kdatatool.h>
37
 
#include <kdebug.h>
38
 
#include <kfiledialog.h>
39
 
#include <kinputdialog.h>
40
 
#include <klocale.h>
41
 
#include <kmessagebox.h>
42
 
#include <knotifyclient.h>
43
 
#include <kurl.h>
44
 
#include <kio/netaccess.h>
45
 
 
46
 
#include <qcheckbox.h>
47
 
#include <qlabel.h>
48
 
#include <qlayout.h>
49
 
#include <qmessagebox.h>
50
 
#include <qpopupmenu.h>
51
 
#include <qvbox.h>
52
 
 
53
 
#include "catalogsettings.h"
54
 
#include "editcmd.h"
55
 
#include "tagextractor.h"
56
 
#include "kbabelview.h"
57
 
#include "kbabeldictbox.h"
58
 
#include "mymultilineedit.h"
59
 
#include "hidingmsgedit.h"
60
 
#include "roughtransdlg.h"
61
 
#include "kbabelsettings.h"
62
 
#include "kbprojectsettings.h"
63
 
 
64
 
#include "resources.h"
65
 
 
66
 
using namespace KBabel;
67
 
 
68
 
QPtrList<ModuleInfo> KBabelView::dictionaries()
69
 
{
70
 
   QPtrList<ModuleInfo> list = dictBox->moduleInfos();
71
 
 
72
 
   return list;
73
 
}
74
 
 
75
 
void KBabelView::configureDictionary(const QString id)
76
 
{
77
 
   dictBox->configure(id);
78
 
}
79
 
 
80
 
void KBabelView::editDictionary(const QString id)
81
 
{
82
 
   dictBox->edit(id);
83
 
}
84
 
 
85
 
 
86
 
void KBabelView::aboutDictionary(const QString id)
87
 
{
88
 
   dictBox->aboutModule(id);
89
 
}
90
 
 
91
 
void KBabelView::informDictionary()
92
 
{
93
 
    if( isSearching () )
94
 
        stopSearch ();
95
 
 
96
 
    if( msgstrEdit->isModified() )
97
 
        dictBox->setTextChanged(_catalog->msgid(_currentIndex,true)
98
 
            ,*(_catalog->msgstr(_currentIndex).at(msgstrEdit->currentForm()))
99
 
            ,msgstrEdit->currentForm()
100
 
            ,_catalog->comment(_currentIndex));
101
 
}
102
 
 
103
 
void KBabelView::setNewLanguage()
104
 
{
105
 
    IdentitySettings s = _catalog->identitySettings();
106
 
 
107
 
    dictBox->setLanguage(s.languageCode, s.languageName);
108
 
 
109
 
    // setup new plural form number
110
 
    int form = msgstrEdit->currentForm();
111
 
    if( form >= s.numberOfPluralForms )
112
 
        form = s.numberOfPluralForms-1;
113
 
 
114
 
    msgstrEdit->setNumberOfPlurals (s.numberOfPluralForms);
115
 
    updateSettings();
116
 
 
117
 
    if (! _catalog->currentURL().isEmpty())
118
 
    {
119
 
        updateEditor( form );
120
 
    }
121
 
}
122
 
 
123
 
 
124
 
void KBabelView::wheelEvent(QWheelEvent *e)
125
 
{
126
 
    if( _catalog->numberOfEntries() == 0 ) return;
127
 
 
128
 
    if( (e->state() & ControlButton) && (e->state() & AltButton))
129
 
    {
130
 
        if(e->delta() > 0)
131
 
        {
132
 
            gotoPrevFuzzyOrUntrans();
133
 
        }
134
 
        else
135
 
        {
136
 
            gotoNextFuzzyOrUntrans();
137
 
        }
138
 
    }
139
 
    else if(e->state() & ControlButton)
140
 
    {
141
 
        if(e->delta() > 0)
142
 
        {
143
 
            gotoPrevFuzzy();
144
 
        }
145
 
        else
146
 
        {
147
 
            gotoNextFuzzy();
148
 
        }
149
 
    }
150
 
    else if(e->state() & AltButton)
151
 
    {
152
 
        if(e->delta() > 0)
153
 
        {
154
 
            gotoPrevUntranslated();
155
 
        }
156
 
        else
157
 
        {
158
 
            gotoNextUntranslated();
159
 
        }
160
 
    }
161
 
    else
162
 
    {
163
 
        if(e->delta() > 0)
164
 
        {
165
 
            gotoPrev();
166
 
        }
167
 
        else
168
 
        {
169
 
            gotoNext();
170
 
        }
171
 
    }
172
 
 
173
 
    e->accept();
174
 
}
175
 
 
176
 
 
177
 
 
178
 
void KBabelView::roughTranslation()
179
 
{
180
 
    RoughTransDlg *dlg = new RoughTransDlg(dictBox, _catalog, this
181
 
            , "roughtransDlg");
182
 
 
183
 
    dlg->exec();
184
 
 
185
 
    delete dlg;
186
 
}
187
 
 
188
 
 
189
 
void KBabelView::updateTags()
190
 
{
191
 
    bool hadTags = _tags.count() > 0;
192
 
 
193
 
    _tags = _catalog->tagList(_currentIndex);
194
 
 
195
 
    if(_tagsMenu)
196
 
    {
197
 
        _tagsMenu->clear();
198
 
 
199
 
        QStringList tList;
200
 
        QStringList::Iterator it;
201
 
        int counter=0;
202
 
        for(it=_tags.begin(); it!=_tags.end(); ++it)
203
 
        {
204
 
            QString s = *it;
205
 
            if( s.startsWith("&") ) s = "&"+s;
206
 
            if(!tList.contains(s))
207
 
            {
208
 
                _tagsMenu->insertItem(s,counter);
209
 
                tList.append(s);
210
 
            }
211
 
            counter++;
212
 
        }
213
 
    }
214
 
 
215
 
    bool haveTags = (_tags.count() > 0);
216
 
 
217
 
    if(isReadOnly())
218
 
        haveTags=false;
219
 
 
220
 
    if(haveTags != hadTags)
221
 
    {
222
 
        emit signalNextTagAvailable(haveTags);
223
 
        emit signalTagsAvailable(haveTags);
224
 
    }
225
 
 
226
 
    _currentTag = 0;
227
 
 
228
 
    if(haveTags)
229
 
    {
230
 
        _tagExtractor->setString(_catalog->msgid(_currentIndex).first());
231
 
    }
232
 
    // if there is no tag, it will set invalid tag
233
 
    selectTag();
234
 
}
235
 
 
236
 
void KBabelView::skipToNextTag()
237
 
{
238
 
    if( (uint)_currentTag >= _tags.count()-1 ) return;
239
 
    ++_currentTag;
240
 
    selectTag();
241
 
}
242
 
 
243
 
void KBabelView::skipToPreviousTag()
244
 
{
245
 
    if( _currentTag == 0 ) return;
246
 
    --_currentTag;
247
 
    selectTag();
248
 
}
249
 
 
250
 
void KBabelView::selectTag()
251
 
{
252
 
    if( _tagExtractor->countMatches() == 0 ) {
253
 
        // no tags, select none
254
 
        kdDebug() << "No tags" << endl;
255
 
        msgidLabel->selectTag(0,0);
256
 
        return;
257
 
    }
258
 
 
259
 
    // count number of eofs in tag
260
 
    uint diff=0;
261
 
    // FIXME: what about plural forms
262
 
    QString msgid = _catalog->msgid(_currentIndex).first();
263
 
 
264
 
    for( uint i = _tagExtractor->matchIndex(_currentTag); i < _tagExtractor->matchIndex(_currentTag)+_tags[_currentTag].length()+1; i++ )
265
 
    {
266
 
        if( msgid[i] == '\n' ) diff++;
267
 
    }
268
 
    msgidLabel->selectTag(_tagExtractor->matchIndex(_currentTag),_tags[_currentTag].length()+diff);
269
 
    emit signalNextTag (_currentTag);
270
 
}
271
 
 
272
 
void KBabelView::setTagsMenu(QPopupMenu *menu)
273
 
{
274
 
    _tagsMenu=menu;
275
 
 
276
 
    connect(_tagsMenu,SIGNAL(activated(int)),this,SLOT(insertTag(int)));
277
 
}
278
 
 
279
 
void KBabelView::modifyMsgstrText(const uint offset, const QString& text, bool clearFirst)
280
 
{
281
 
    _catalog->applyBeginCommand( _currentIndex, Msgstr ,this);
282
 
 
283
 
    if( clearFirst ) msgstrEdit->clear();
284
 
 
285
 
    InsTextCmd* insCmd = new InsTextCmd(offset,text,msgstrEdit->currentForm());
286
 
    insCmd->setPart(Msgstr);
287
 
    insCmd->setIndex(_currentIndex);
288
 
 
289
 
    msgstrEdit->processCommand(insCmd,false);
290
 
    forwardMsgstrEditCmd(insCmd);
291
 
 
292
 
    _catalog->applyEndCommand(_currentIndex, Msgstr,this);
293
 
 
294
 
    autoCheck( true ); // check it NOW - it should not be needed, but it is, I don't know why :-(
295
 
}
296
 
 
297
 
void KBabelView::insertTag(int n)
298
 
{
299
 
    QString tag = _tagsMenu->text(n);
300
 
    if( tag.startsWith( "&&" ) ) tag = tag.remove(0,1); // replace && -> &. && is used for correct menu display
301
 
 
302
 
    modifyMsgstrText( msgstrEdit->currentIndex(), tag );
303
 
}
304
 
 
305
 
void KBabelView::insertNextTag()
306
 
{
307
 
    if(_currentTag >= (int)_tags.count())
308
 
    {
309
 
        KNotifyClient::beep();
310
 
        return;
311
 
    }
312
 
 
313
 
    int offset = msgstrEdit->currentIndex();
314
 
 
315
 
    _currentTag++;
316
 
    selectTag();
317
 
 
318
 
    modifyMsgstrText( offset, _tags[_currentTag-1] );
319
 
}
320
 
 
321
 
void KBabelView::insertNextTagMsgid()
322
 
{
323
 
    TagExtractor extractor;
324
 
 
325
 
    int offset = msgstrEdit->beginOfLastMarkedText(); //msgstrEdit->currentIndex();
326
 
 
327
 
    QString s = (*_catalog->msgstr(_currentIndex).at(msgstrEdit->currentForm())).left(offset);
328
 
 
329
 
    QString t;
330
 
 
331
 
    if( _catalog->pluralForm( _currentIndex ) == KDESpecific )
332
 
    {
333
 
            int pos = msgstrEdit->currentIndex();
334
 
            int currentFormBegin=s.findRev("\\n",pos);
335
 
            if( currentFormBegin == -1 ) currentFormBegin=0;
336
 
            else currentFormBegin+=3; // skip the newline
337
 
            int currentFormEnd=s.find("\\n",pos);
338
 
            if( currentFormEnd == -1 ) currentFormEnd=s.length();
339
 
 
340
 
            s=s.mid(currentFormBegin,currentFormEnd-currentFormBegin);
341
 
    }
342
 
 
343
 
    extractor.setString(s);
344
 
    uint num= extractor.countMatches();
345
 
    if(num >= _tags.count())
346
 
    {
347
 
        KNotifyClient::beep();
348
 
        return;
349
 
    }
350
 
 
351
 
    t=_tags[num];
352
 
 
353
 
    modifyMsgstrText( offset, t );
354
 
}
355
 
 
356
 
void KBabelView::showTagsMenu()
357
 
{
358
 
    if(_tagsMenu && _tags.count() > 0)
359
 
    {
360
 
        int y=msgstrEdit->height()/2;
361
 
        int x=msgstrEdit->width()/2;
362
 
        _tagsMenu->exec(msgstrEdit->mapToGlobal( QPoint(x,y) ) );
363
 
 
364
 
        return;
365
 
    }
366
 
}
367
 
 
368
 
 
369
 
void KBabelView::updateArgs()
370
 
{
371
 
    bool hadArgs = _args.count() > 0;
372
 
 
373
 
    _args = _catalog->argList(_currentIndex);
374
 
 
375
 
    if(_argsMenu)
376
 
    {
377
 
        _argsMenu->clear();
378
 
 
379
 
        QStringList tList;
380
 
        QStringList::Iterator it;
381
 
        int counter=0;
382
 
        for(it=_args.begin(); it!=_args.end(); ++it)
383
 
        {
384
 
            QString s = *it;
385
 
            if(!tList.contains(s))
386
 
            {
387
 
                _argsMenu->insertItem(s,counter);
388
 
                tList.append(s);
389
 
            }
390
 
            counter++;
391
 
        }
392
 
    }
393
 
 
394
 
    bool haveArgs = (_args.count() > 0);
395
 
 
396
 
    if(isReadOnly())
397
 
        haveArgs=false;
398
 
 
399
 
    if(haveArgs != hadArgs)
400
 
    {
401
 
        emit signalNextArgAvailable(haveArgs);
402
 
        emit signalArgsAvailable(haveArgs);
403
 
    }
404
 
}
405
 
 
406
 
void KBabelView::setArgsMenu(QPopupMenu *menu)
407
 
{
408
 
    _argsMenu=menu;
409
 
 
410
 
    connect(_argsMenu,SIGNAL(activated(int)),this,SLOT(insertArg(int)));
411
 
}
412
 
 
413
 
 
414
 
void KBabelView::insertArg(int n)
415
 
{
416
 
    QString arg = _argsMenu->text(n);
417
 
 
418
 
    modifyMsgstrText( msgstrEdit->currentIndex(), arg );
419
 
}
420
 
 
421
 
void KBabelView::insertNextArg()
422
 
{
423
 
    int offset = msgstrEdit->currentIndex();
424
 
 
425
 
    QString s = (*_catalog->msgstr(_currentIndex).at(msgstrEdit->currentForm())).left(offset);
426
 
 
427
 
    if( _catalog->pluralForm( _currentIndex ) == KDESpecific )
428
 
    {
429
 
        int pos = msgstrEdit->currentIndex();
430
 
        int currentFormBegin=s.findRev("\\n",pos);
431
 
        if( currentFormBegin == -1 ) currentFormBegin=0;
432
 
        else currentFormBegin+=3; // skip the newline
433
 
        int currentFormEnd=s.find("\\n",pos);
434
 
        if( currentFormEnd == -1 ) currentFormEnd=s.length();
435
 
 
436
 
        s=s.mid(currentFormBegin,currentFormEnd-currentFormBegin);
437
 
    }
438
 
 
439
 
    _argExtractor->setString(s);
440
 
    uint num=_argExtractor->countMatches();
441
 
    if(num >= _args.count())
442
 
    {
443
 
        KNotifyClient::beep();
444
 
        return;
445
 
    }
446
 
 
447
 
    QString t=_args[num];
448
 
 
449
 
    modifyMsgstrText( offset,t );
450
 
}
451
 
 
452
 
void KBabelView::showArgsMenu()
453
 
{
454
 
    if(_argsMenu && _args.count() > 0)
455
 
    {
456
 
        int y=msgstrEdit->height()/2;
457
 
        int x=msgstrEdit->width()/2;
458
 
        _argsMenu->exec(msgstrEdit->mapToGlobal( QPoint(x,y) ) );
459
 
 
460
 
        return;
461
 
    }
462
 
}
463
 
 
464
 
 
465
 
void KBabelView::diff()
466
 
{
467
 
    diffInternal(false);
468
 
    msgstrEdit->setFocus();
469
 
}
470
 
 
471
 
void KBabelView::diffShowOrig()
472
 
{
473
 
    msgidLabel->setText(_catalog->msgid(_currentIndex));
474
 
    msgidLabel->forceUpdate();
475
 
    msgstrEdit->setFocus();
476
 
}
477
 
 
478
 
void KBabelView::toggleAutoDiff(bool on)
479
 
{
480
 
    if(on != _diffEnabled)
481
 
    {
482
 
        _diffEnabled = on;
483
 
 
484
 
        if(on)
485
 
        {
486
 
            diff();
487
 
        }
488
 
        else
489
 
        {
490
 
            diffShowOrig();
491
 
        }
492
 
    }
493
 
}
494
 
 
495
 
void KBabelView::autoDiff()
496
 
{
497
 
    diffInternal(true);
498
 
}
499
 
 
500
 
void KBabelView::diffInternal(bool autoDf)
501
 
{
502
 
    if(_diffing || _loadingDiffFile)
503
 
    {
504
 
        return;
505
 
    }
506
 
 
507
 
    _diffing = true;
508
 
    uint diffIndex = _currentIndex;
509
 
 
510
 
    QString diffString;
511
 
 
512
 
    Catalog::DiffResult r = _catalog->diff(_currentIndex, &diffString);
513
 
 
514
 
    if(r == Catalog::DiffNeedList)
515
 
    {
516
 
        switch( _project->settings()->useDBForDiff() )
517
 
        {
518
 
            case 1:
519
 
            {
520
 
                _loadingDiffFile=true;
521
 
                bool wasEnabled=_diffEnabled;
522
 
                _diffEnabled=false;
523
 
 
524
 
                QValueList<DiffEntry> diffList;
525
 
                QString error;
526
 
                QString package = _catalog->packageName()+".po";
527
 
                kdDebug(KBABEL) << "getting list for " << package << endl;
528
 
 
529
 
                if(dictBox->messagesForPackage(package,diffList,error))
530
 
                {
531
 
                    kdDebug(KBABEL) << "got " << diffList.count()
532
 
                        << " messages" << endl;
533
 
                    _catalog->setDiffList(diffList);
534
 
                }
535
 
                else
536
 
                {
537
 
                    KMessageBox::sorry(this
538
 
                        ,i18n("An error occurred while trying to get the list "
539
 
                        "of messages for this file from the database:\n"
540
 
                        "%1").arg(error));
541
 
 
542
 
                    _diffing=false;
543
 
                    _diffEnabled=false;
544
 
                    _loadingDiffFile=false;
545
 
                    emit signalDiffEnabled(false);
546
 
 
547
 
                    return;
548
 
                }
549
 
 
550
 
                _diffEnabled=wasEnabled;
551
 
                _loadingDiffFile=false;
552
 
                break;
553
 
            }
554
 
            case 0:
555
 
            {
556
 
                _diffing=false;
557
 
                if(!openDiffFile(true))
558
 
                {
559
 
                    _diffEnabled=false;
560
 
                     emit signalDiffEnabled(false);
561
 
 
562
 
                    _diffing=false;
563
 
                    return;
564
 
                }
565
 
 
566
 
                _diffing = true;
567
 
                break;
568
 
            }
569
 
            case 2:
570
 
            {
571
 
                // get the list of all entries
572
 
                QValueList<DiffEntry> diffList = _catalog->asDiffList();
573
 
 
574
 
                QValueList<DiffEntry> resultList;
575
 
 
576
 
                // swap msgstr and msgid
577
 
                QValueList<DiffEntry>::iterator it;
578
 
                DiffEntry entry;
579
 
 
580
 
                 for ( it = diffList.begin(); it != diffList.end(); ++it )
581
 
                 {
582
 
                    entry.msgstr = (*it).msgid;
583
 
                    // if there is no translation, do not show difference
584
 
                    if( !(*it).msgstr.isEmpty() )
585
 
                    {
586
 
                        entry.msgid = (*it).msgstr;
587
 
                    }
588
 
                    else
589
 
                    {
590
 
                        entry.msgid = (*it).msgid;
591
 
                    }
592
 
                    resultList.append(entry);
593
 
                 }
594
 
 
595
 
                // set as a source for diff
596
 
                _catalog->setDiffList(resultList);
597
 
 
598
 
                _diffing=false;
599
 
                _diffEnabled=true;
600
 
                _loadingDiffFile=false;
601
 
                emit signalDiffEnabled(true);
602
 
            }
603
 
        }
604
 
 
605
 
        diffIndex = _currentIndex;
606
 
        r = _catalog->diff(_currentIndex, &diffString);
607
 
    }
608
 
 
609
 
    // if the index changed in the meanwhile
610
 
    while(diffIndex != _currentIndex)
611
 
    {
612
 
        diffIndex=_currentIndex;
613
 
        r = _catalog->diff(_currentIndex,&diffString);
614
 
    }
615
 
 
616
 
    if(r == Catalog::DiffOk)
617
 
    {
618
 
        msgidLabel->setText(diffString);
619
 
        msgidLabel->forceUpdate();
620
 
 
621
 
        // FIXME: should care about plural forms
622
 
        if(diffString == _catalog->msgid(_currentIndex).first() )
623
 
        {
624
 
            emit signalChangeStatusbar(i18n("No difference found"));
625
 
        }
626
 
        else
627
 
        {
628
 
            emit signalChangeStatusbar(i18n("Difference found"));
629
 
        }
630
 
    }
631
 
    else
632
 
    {
633
 
        if(!autoDf)
634
 
        {
635
 
            KMessageBox::information(this
636
 
                ,i18n("No corresponding message found."));
637
 
        }
638
 
        else
639
 
        {
640
 
            emit signalChangeStatusbar(
641
 
                    i18n("No corresponding message found"));
642
 
        }
643
 
    }
644
 
 
645
 
    _diffing = false;
646
 
}
647
 
 
648
 
bool KBabelView::openDiffFile()
649
 
{
650
 
    return openDiffFile(false);
651
 
}
652
 
 
653
 
bool KBabelView::openDiffFile(bool autoDiff)
654
 
{
655
 
    if(_diffing || _loadingDiffFile)
656
 
        return false;
657
 
 
658
 
    KURL url;
659
 
 
660
 
    if( autoDiff && ! _project->settings()->diffBaseDir().isEmpty() )
661
 
    {
662
 
        KURL fileURL = _catalog->currentURL();
663
 
        
664
 
        KURL poBaseURL( _project->catManSettings().poBaseDir );
665
 
        
666
 
        QString poBase = poBaseURL.path();
667
 
        int len = poBase.length();
668
 
        if(fileURL.path().left(len) == poBase)
669
 
        {
670
 
           QString fileRelPath = fileURL.path().mid(len);
671
 
           if(fileRelPath[0] == '/')
672
 
               fileRelPath=fileRelPath.mid(1);
673
 
 
674
 
           if(_project->settings()->diffBaseDir().right(1) != "/")
675
 
               _project->settings()->diffBaseDir() += '/';
676
 
 
677
 
           QString diffFilePath = _project->settings()->diffBaseDir() + fileRelPath;
678
 
 
679
 
 
680
 
           KURL diffFileURL(diffFilePath);
681
 
 
682
 
           if(diffFileURL.isValid() && KIO::NetAccess::exists(diffFileURL,true,NULL))
683
 
           {
684
 
               url = diffFileURL;
685
 
 
686
 
               kdDebug(KBABEL) << "using file " << diffFileURL.prettyURL()
687
 
                         << " as diff file" << endl;
688
 
           }
689
 
        }
690
 
    }
691
 
 
692
 
 
693
 
    if(url.isEmpty())
694
 
    {
695
 
        url = KFileDialog::getOpenURL(_project->settings()->diffBaseDir(), 
696
 
"application/x-gettext", this, i18n("Select File to Diff With"));
697
 
    }
698
 
 
699
 
    if(url.isEmpty())
700
 
        return false;
701
 
 
702
 
    _loadingDiffFile=true;
703
 
    bool wasEnabled=_diffEnabled;
704
 
    _diffEnabled=false;
705
 
 
706
 
 
707
 
    Catalog cat;
708
 
 
709
 
    connect(&cat,SIGNAL(signalProgress(int)),this,SIGNAL(signalProgress(int)));
710
 
    emit signalResetProgressBar(i18n("loading file for diff"),100);
711
 
 
712
 
    ConversionStatus stat = cat.openURL(url);
713
 
 
714
 
    emit signalClearProgressBar();
715
 
 
716
 
 
717
 
    if(stat != OK && stat != RECOVERED_PARSE_ERROR)
718
 
    {
719
 
        switch(stat)
720
 
        {
721
 
            case PARSE_ERROR:
722
 
            {
723
 
                KMessageBox::sorry(this
724
 
                    ,i18n("Error while trying to read file:\n %1\n"
725
 
                   "Maybe it is not a valid PO file.").arg(url.prettyURL()));
726
 
                break;
727
 
            }
728
 
            case NO_PERMISSIONS:
729
 
            {
730
 
                KMessageBox::sorry(this,i18n(
731
 
                "You do not have permissions to read file:\n %1")
732
 
                        .arg(url.prettyURL()));
733
 
                break;
734
 
            }
735
 
            case NO_FILE:
736
 
            {
737
 
                 KMessageBox::sorry(this,i18n(
738
 
                 "You have not specified a valid file:\n %1")
739
 
                         .arg(url.prettyURL()));
740
 
                break;
741
 
            }
742
 
            case NO_PLUGIN:
743
 
            {
744
 
                 KMessageBox::error(this,i18n(
745
 
                      "KBabel cannot find a corresponding plugin for the MIME type of the file:\n %1").arg(url.prettyURL()));
746
 
                 break;
747
 
            }
748
 
            case UNSUPPORTED_TYPE:
749
 
            {
750
 
                  KMessageBox::error(this,i18n(
751
 
                       "The import plugin cannot handle this type of the file:\n %1").arg(url.prettyURL()));
752
 
                  break;
753
 
            }
754
 
            default:
755
 
            {
756
 
                 KMessageBox::sorry(this,i18n(
757
 
                  "Error while trying to open file:\n %1")
758
 
                         .arg(url.prettyURL()));
759
 
                 break;
760
 
            }
761
 
 
762
 
        }
763
 
 
764
 
        _diffEnabled=wasEnabled;
765
 
        _loadingDiffFile=false;
766
 
 
767
 
        return false;
768
 
    }
769
 
 
770
 
    _catalog->setDiffList( cat.asDiffList() );
771
 
 
772
 
    _diffEnabled=wasEnabled;
773
 
    _loadingDiffFile=false;
774
 
 
775
 
    return true;
776
 
}
777
 
 
778
 
void KBabelView::showTryLaterMessageBox()
779
 
{
780
 
    if( !_showTryLaterBox ) return;
781
 
 
782
 
    KDialogBase *dialog= new KDialogBase(
783
 
                i18n("Information"),
784
 
                KDialogBase::Yes,
785
 
                KDialogBase::Yes, KDialogBase::Yes,
786
 
                this, "information", true, true,
787
 
                KStdGuiItem::ok() );
788
 
 
789
 
    QVBox *topcontents = new QVBox (dialog);
790
 
    topcontents->setSpacing(KDialog::spacingHint()*2);
791
 
    topcontents->setMargin(KDialog::marginHint()*2);
792
 
 
793
 
    QWidget *contents = new QWidget(topcontents);
794
 
    QHBoxLayout * lay = new QHBoxLayout(contents);
795
 
    lay->setSpacing(KDialog::spacingHint()*2);
796
 
 
797
 
    lay->addStretch(1);
798
 
    QLabel *label1 = new QLabel( contents);
799
 
    label1->setPixmap(QMessageBox::standardIcon(QMessageBox::Information));
800
 
    lay->add( label1 );
801
 
    QLabel *label2 = new QLabel( i18n("The search string has not been found yet.\n"
802
 
                 "However, the string might be found "
803
 
                 "in the files being searched at the moment.\n"
804
 
                 "Please try later."), contents);
805
 
    label2->setAlignment( Qt::AlignAuto | Qt::AlignVCenter | Qt::ExpandTabs | Qt::WordBreak );
806
 
    label2->setMinimumSize(label2->sizeHint());
807
 
    lay->add( label2 );
808
 
    lay->addStretch(1);
809
 
 
810
 
    QCheckBox *checkbox = new QCheckBox(i18n("Do not show in this find/replace session again"), topcontents);
811
 
 
812
 
    dialog->setMainWidget(topcontents);
813
 
    dialog->enableButtonSeparator(false);
814
 
    dialog->incInitialSize( QSize(50,0) );
815
 
 
816
 
    dialog->exec();
817
 
 
818
 
    _showTryLaterBox = !checkbox->isChecked();
819
 
    delete dialog;
820
 
}
821
 
 
822
 
void KBabelView::setFilePackage()
823
 
{
824
 
    bool result=false;
825
 
    QString p = KInputDialog::getText(QString::null, i18n("Enter new package for the current file:"),_catalog->package(),&result,this);
826
 
    if( result )
827
 
    {
828
 
        _catalog->setPackage(p);
829
 
        emit signalChangeCaption(p);
830
 
    }
831
 
}
832
 
 
833
 
void KBabelView::insertTagFromTool( const QString& tag )
834
 
{
835
 
    modifyMsgstrText(msgstrEdit->currentIndex(),tag);
836
 
 
837
 
    msgstrEdit->setFocus();
838
 
}
839
 
 
840
 
void KBabelView::skipToTagFromTool( int index )
841
 
{
842
 
    _currentTag = index;
843
 
    selectTag();
844
 
}
845
 
 
846
 
void KBabelView::plural2msgstr()
847
 
{
848
 
    int currentFormBegin, currentFormEnd, pos;
849
 
    uint i;
850
 
 
851
 
    QStringList msgs = _catalog->msgstr(_currentIndex);
852
 
    QString text= *msgs.at(msgstrEdit->currentForm());
853
 
    uint numForms = _catalog->numberOfPluralForms(_currentIndex);
854
 
 
855
 
    if( text.isEmpty() || _catalog->pluralForm(_currentIndex) == NoPluralForm) return;
856
 
 
857
 
 
858
 
    QString result;
859
 
 
860
 
    switch( _catalog->pluralForm(_currentIndex) )
861
 
    {
862
 
        case Gettext:
863
 
 
864
 
            _catalog->applyBeginCommand( _currentIndex, Msgstr ,this);
865
 
 
866
 
            i=0;
867
 
            for( QStringList::Iterator it=msgs.begin() ; it!=msgs.end() ; ++it )
868
 
            {
869
 
                if( i!= msgstrEdit->currentForm() )
870
 
                {
871
 
                    // clear first
872
 
                    DelTextCmd* insCmd = new DelTextCmd(0,(*it),i);
873
 
                    insCmd->setPart(Msgstr);
874
 
                    insCmd->setIndex(_currentIndex);
875
 
                    msgstrEdit->processCommand(insCmd,false);
876
 
                    forwardMsgstrEditCmd(insCmd);
877
 
 
878
 
                    // insert text
879
 
                    insCmd = new InsTextCmd(0,text,i);
880
 
                    insCmd->setPart(Msgstr);
881
 
                    insCmd->setIndex(_currentIndex);
882
 
                    msgstrEdit->processCommand(insCmd,false);
883
 
                    forwardMsgstrEditCmd(insCmd);
884
 
                }
885
 
                i++;
886
 
            }
887
 
            
888
 
            // fill the non-initialized ones
889
 
            while (i != numForms)
890
 
            {
891
 
                // insert text
892
 
                DelTextCmd* insCmd = new InsTextCmd(0,text,i);
893
 
                insCmd->setPart(Msgstr);
894
 
                insCmd->setIndex(_currentIndex);
895
 
                msgstrEdit->processCommand(insCmd,false);
896
 
                forwardMsgstrEditCmd(insCmd);
897
 
                i++;
898
 
            }
899
 
 
900
 
            _catalog->applyEndCommand( _currentIndex, Msgstr ,this);
901
 
 
902
 
            break;
903
 
 
904
 
        case KDESpecific:
905
 
            {
906
 
                pos = msgstrEdit->currentIndex();
907
 
                currentFormBegin=text.findRev("\\n",pos);
908
 
                if( currentFormBegin == -1 ) currentFormBegin=0;
909
 
                else currentFormBegin+=3; // skip the newline
910
 
                currentFormEnd=text.find("\\n",pos);
911
 
                if( currentFormEnd == -1 ) currentFormEnd=text.length();
912
 
 
913
 
                text=text.mid(currentFormBegin,currentFormEnd-currentFormBegin);
914
 
 
915
 
                QString result=text;
916
 
                for( i=1; i<numForms ; i++ )
917
 
                    result+="\\n\n"+text;
918
 
 
919
 
                modifyMsgstrText( 0, result, true );
920
 
            }
921
 
            break;
922
 
 
923
 
        case NoPluralForm: break;
924
 
    }
925
 
 
926
 
}
927
 
 
928
 
bool KBabelView::validateUsingTool( const KDataToolInfo & info, const QString &command )
929
 
{
930
 
    if(currentURL().isEmpty())
931
 
        return false;
932
 
 
933
 
    KDataTool* tool = info.createTool();
934
 
    if( !tool )
935
 
    {
936
 
        kdWarning() << "Cannot create tool" << endl;
937
 
        return false;
938
 
    }
939
 
 
940
 
    bool result=_catalog->checkUsingTool(tool);
941
 
    emitEntryState();
942
 
 
943
 
    QString checkName = *(info.userCommands().at( info.commands().findIndex(command) ));
944
 
 
945
 
    if(result)
946
 
    {
947
 
        KMessageBox::information(this
948
 
            ,i18n("No mismatch has been found.")
949
 
            ,checkName);
950
 
    }
951
 
    else
952
 
    {
953
 
        int index=0;
954
 
        DocPosition pos;
955
 
 
956
 
        if(!_catalog->hasError(0,pos))
957
 
            index = _catalog->nextError(0,pos);
958
 
        if(index>=0)
959
 
        {
960
 
            kdDebug(KBABEL) << "Going to " << pos.item << ", " << pos.form << endl;
961
 
            gotoEntry(pos);
962
 
        }
963
 
 
964
 
        KMessageBox::error(this
965
 
            ,i18n("Some mismatches have been found.\n"
966
 
            "Please check the questionable entries by using "
967
 
            "Go->Next error")
968
 
            ,checkName);
969
 
    }
970
 
    delete tool;
971
 
 
972
 
    return result;
973
 
}
974
 
 
975
 
void KBabelView::modifyUsingTool( const KDataToolInfo & info, const QString &command )
976
 
{
977
 
    KDataTool* tool = info.createTool();
978
 
    if( !tool )
979
 
    {
980
 
        kdWarning() << "Cannot create tool" << endl;
981
 
        return;
982
 
    }
983
 
 
984
 
    // do some stuff on all entries
985
 
    _catalog->modifyUsingTool(tool, command);
986
 
 
987
 
    delete tool;
988
 
}
989
 
 
990
 
void KBabelView::modifyCatalogUsingTool( const KDataToolInfo & info, const QString &command )
991
 
{
992
 
    KDataTool* tool = info.createTool();
993
 
    if( !tool )
994
 
    {
995
 
        kdWarning() << "Cannot create tool" << endl;
996
 
        return;
997
 
    }
998
 
 
999
 
    // do some stuff on the catalog
1000
 
    tool->run(command, _catalog, "Catalog", "application/x-kbabel-catalog");
1001
 
 
1002
 
    delete tool;
1003
 
}
1004
 
 
1005
 
void KBabelView::insertChar( QChar ch )
1006
 
{
1007
 
    if( isReadOnly() || _catalog->package().isEmpty() )
1008
 
        return;
1009
 
 
1010
 
    modifyMsgstrText(msgstrEdit->currentIndex(),ch);
1011
 
    msgstrEdit->setFocus();
1012
 
}
1013
 
 
1014
 
void KBabelView::wordCount()
1015
 
{
1016
 
    uint total, untranslated, fuzzy;
1017
 
    
1018
 
    _catalog->wordCount( total, fuzzy, untranslated );
1019
 
    
1020
 
    KMessageBox::information( this
1021
 
        , i18n("Total words: %1\n\n"
1022
 
"Words in untranslated messages: %2\n\n"
1023
 
"Words in fuzzy messages: %3").arg(total).arg(untranslated).arg(fuzzy)
1024
 
        , i18n("Word Count") );
1025
 
}