File: kfilereplacepart.cpp

package info (click to toggle)
kdewebdev 1%3A3.3.2-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 33,200 kB
  • ctags: 13,047
  • sloc: cpp: 165,029; sh: 9,033; perl: 3,315; makefile: 1,618; xml: 497; ansic: 83
file content (1229 lines) | stat: -rw-r--r-- 43,414 bytes parent folder | download
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
//
//
// C++ Implementation: kfilereplacepart
//
// Description:
//
//
// Author: Andras Mantia <amantia@kde.org>, (C) 2003
// Maintainer: Emiliano Gulmini <emi_barbarossa@yahoo.it>, (C) 2004
//
// Copyright: GPL v2. See COPYING file that comes with this distribution
//
//

#undef APP

//qt includes
#include <qdir.h>
#include <qdatastream.h>
#include <qdatetime.h>
#include <qlistview.h>

//kde includes
#include <kaboutkfilereplace.h>
#include <kapplication.h>
#include <kaction.h>
#include <kbugreport.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kfiledialog.h>
#include <kinstance.h>
#include <kio/netaccess.h>
#include <kmessagebox.h>
#include <kparts/genericfactory.h>
#include <kstandarddirs.h>
#include <dcopclient.h>
#include <dcopref.h>

//own includes
#include "apistruct.h"
#include "kfilereplacelib.h"
#include "kernel.h"
#include "resource.h"
#include "kfilereplacepart.h"
#include "kfilereplacedoc.h"
#include "kfilereplaceview.h"
#include "koptionsdlg.h"

#include "whatthis.h"

using namespace whatthisNameSpace;

// Global Thread data
bool g_bThreadRunning = false;
bool g_bThreadMustStop = false;
int g_nFilesRep = 0;
int g_nStringsRep = 0;
int g_nOperation;
RepDirArg g_argu;
QString g_szErrMsg;

// Factory code for KDE 3
typedef KParts::GenericFactory<KFileReplacePart> KFileReplaceFactory;
K_EXPORT_COMPONENT_FACTORY( libkfilereplacepart, KFileReplaceFactory )

KFileReplacePart::KFileReplacePart(QWidget* parentWidget, const char* , QObject* parent, const char* name, const QStringList & )
  : KParts::ReadOnlyPart(parent,name)
{
  setInstance(KFileReplaceFactory::instance());

  // Initialize variables
  g_bThreadRunning = false;
  g_bThreadMustStop = false;
  g_nFilesRep = 0;
  g_szErrMsg = "";
  m_parentWidget = parentWidget;
  QString configName = locateLocal("config", "kfilereplacerc");
  //kdDebug(23000) << "config file: " << configName << endl;
  m_config = new KConfig(configName);
  m_dlgAbout = 0L;

  initView();
  initGUI();
  setWhatsThis();
  readOptions();
  updateCommands(); // Gray or ungray commands
  initDocument();
}

KFileReplacePart::~KFileReplacePart()
{
  saveOptions();
  slotFileStop();
  delete m_doc;
  if(m_dlgAbout != 0)
    delete m_dlgAbout;
  delete m_config;
}

KAboutData* KFileReplacePart::createAboutData()
{
  KAboutData* aboutData = new KAboutData( "kfilereplacepart", I18N_NOOP("KFileReplacePart"),
                                          "1.0", I18N_NOOP( "Batch search and replace tool" ),
                                          KAboutData::License_GPL_V2,
                                          "(C) 1999-2002 Francois Dupoux\n(C) 2003 Andras Mantia");
                                          aboutData->addAuthor("Francois Dupou",I18N_NOOP("Original author of the KFileReplace tool"), "dupoux@dupoux.com");
                                          aboutData->addAuthor("Andras Mantia",I18N_NOOP("Current maintainer, KPart creator"), "amantia@kde.org");

  return aboutData;
}

bool KFileReplacePart::openURL(const KURL &a_url)
{
  KURL url = a_url;
  if (url.protocol() != "file")
  {
    if (KMessageBox::warningContinueCancel(0, i18n("KFileReplace part currently works only for local files. Do you still want to continue?"), i18n("Non Local File"), KStdGuiItem::cont(), "Non Local File Warning") == KMessageBox::Cancel)
    {
      emit canceled("");
      return false;
    }
    url = KURL::fromPathOrURL(QDir::homeDirPath());
  }
  if (m_doc->newDocument(url.path(), "*", true))
  {
      m_settings.bRecursive = m_doc->includeSubfolders();
      m_settings.bCaseSensitive = m_doc->caseSensitive();
      m_settings.bWildcards = m_doc->enableWildcards();
      m_settings.bVariables = m_doc->enableVariables();
      m_view->addString(0L, m_doc->searchFor(), m_doc->replaceWith());
      if (!m_doc->searchLater())
      {
          if (m_doc->replaceWith().isEmpty())
            slotFileSearch();
          else
            slotFileReplace();
      }
      return true;
  } else return false;
}

void KFileReplacePart::initGUI()
{
   setXMLFile("kfilereplacepartui.rc");

   // File
   (void)new KAction(i18n("New Search Project..."), "newproject", 0, this, SLOT(slotFileNew()), actionCollection(), "new_project");

   (void)new KAction(i18n("&Search"), "find", 0, this, SLOT(slotFileSearch()), actionCollection(), "search");

   (void)new KAction(i18n("&Simulate"), "filesimulate", 0, this, SLOT(slotFileSimulate()), actionCollection(), "file_simulate");

   (void)new KAction(i18n("&Replace"), "filereplace", 0, this, SLOT(slotFileReplace()), actionCollection(), "replace");

   (void)new KAction(i18n("Sto&p"), "filestop", 0, this, SLOT(slotFileStop()), actionCollection(), "stop");

   (void)new KAction(i18n("Save &Results As..."), "filesave", 0, this, SLOT(slotFileSave()), actionCollection(), "save_results");

   // Strings
   (void)new KAction(i18n("&Add String..."), "edit_add", 0, this, SLOT(slotStringsAdd()), actionCollection(), "strings_add");

   (void)new KAction(i18n("&Delete String"), "edit_remove", 0, this, SLOT(slotStringsDel()), actionCollection(), "strings_del");

   (void)new KAction(i18n("&Empty Strings List"), "strempty", 0, this, SLOT(slotStringsEmpty()), actionCollection(), "strings_empty");

   (void)new KAction(i18n("Edi&t Selected String..."), "lineedit", 0, this, SLOT(slotStringsEdit()), actionCollection(), "strings_edit");

   (void)new KAction(i18n("&Save Strings List to File..."), "filesave", 0, this, SLOT(slotStringsSave()), actionCollection(), "strings_save");

   (void)new KAction(i18n("&Load Strings List From File..."), "unsortedList", 0, this, SLOT(slotStringsLoad()), actionCollection(), "strings_load");

   (void)new KRecentFilesAction(i18n("&Load Recent Strings Files"), "fileopen", 0, this, SLOT(slotOpenRecentStringFile(const KURL&)), actionCollection(),"strings_load_recent");

   (void)new KAction(i18n("&Invert Current String (search <--> replace)"), "invert", 0, this, SLOT(slotStringsInvertCur()), actionCollection(), "strings_invert");

   (void)new KAction(i18n("&Invert All Strings (search <--> replace)"), "invert", 0, this, SLOT(slotStringsInvertAll()), actionCollection(), "strings_invert_all");

   // Options
   (void)new KToggleAction(i18n("&Include Sub-Folders"), "recursive", 0, this, SLOT(slotOptionsRecursive()), actionCollection(), "options_recursive");

   (void)new KToggleAction(i18n("Create &Backup"), "backup", 0, this, SLOT(slotOptionsBackup()), actionCollection(), "options_backup");

   (void)new KToggleAction(i18n("Case &Sensitive"), "casesensitive", 0, this, SLOT(slotOptionsCaseSensitive()), actionCollection(), "options_case");

   KToggleAction* action=new KToggleAction(i18n("Enable &Wildcards"), "optwildcards", 0, this, SLOT(slotOptionsWildcards()), actionCollection(), "options_wildcards");
#if KDE_IS_VERSION(3,2,90)
   action->setCheckedState(i18n("Disable &Wildcards"));
#endif
   action=new KToggleAction(i18n("Enable &Variables in Replace String: [$name:format$]"), "optvar", 0, this, SLOT(slotOptionsVariables()), actionCollection(), "options_var");
#if KDE_IS_VERSION(3,2,90)
   action->setCheckedState(i18n("Disable &Variables in Replace String: [$name:format$]"));
#endif
   (void) new KAction(i18n("Configure &KFileReplace..."), "configure", 0, this, SLOT(slotOptionsPreferences()), actionCollection(), "configure_kfilereplace");

   // Results
   (void)new KAction(i18n("&Properties"), "resfileinfo", 0, m_view, SLOT(slotResultProperties()), actionCollection(), "results_infos");

   (void)new KAction(i18n("&Open"), "resfileopen", 0, m_view, SLOT(slotResultOpen()), actionCollection(), "results_openfile");

   //if(QString(kapp->startupId()).contains("quanta")==0)
   (void)new KAction(i18n("&Open in Quanta"), "resfileedit", 0, m_view, SLOT(slotResultEdit()), actionCollection(), "results_editfile");


   (void)new KAction(i18n("Open Parent &Folder"), "resdiropen", 0, m_view, SLOT(slotResultDirOpen()), actionCollection(), "results_opendir");

   (void)new KAction(i18n("&Delete"), "resfiledel", 0, m_view, SLOT(slotResultDelete()), actionCollection(), "results_delete");

   (void)new KAction(i18n("E&xpand Tree"), 0, m_view, SLOT(slotResultTreeExpand()), actionCollection(), "results_treeexpand");

   (void)new KAction(i18n("&Reduce Tree"), 0, m_view, SLOT(slotResultTreeReduce()), actionCollection(), "results_treereduce");

   // Help menu
//   setHelpMenuEnabled(false);
   (void)new KAction(i18n("&About KFileReplace"), "kfilereplace", 0, this, SLOT(showAboutApplication()), actionCollection(), "about_kfilereplace");

   (void)new KAction(i18n("KFileReplace &Handbook"), "help", 0, this, SLOT(appHelpActivated()), actionCollection(), "help_kfilereplace");

   (void)new KAction(i18n("&Report Bug..."), 0, 0, this, SLOT(reportBug()), actionCollection(), "report_bug");
}

void KFileReplacePart::initDocument()
{
  bool nRes;

  m_doc = new KFileReplaceDoc(m_parentWidget, this);
  m_doc->addView(m_view);
  nRes = m_doc->newDocument(QDir::homeDirPath(), "*", false);
}

void KFileReplacePart::initView()
{
  ////////////////////////////////////////////////////////////////////
  // create the main widget here that is managed by KMainWindow's view-region and
  // connect the widget to your document to display document contents.

  m_view = new KFileReplaceView(m_parentWidget, "view");

  setWidget(m_view);

  m_view->setAcceptDrops(false);

}

KFileReplaceDoc* KFileReplacePart::document() const
{
  return m_doc;
}

KConfig* KFileReplacePart::config()
{
  return m_config;
}

void KFileReplacePart::updateCommands() // Gray or ungray commands
{
  // File
  actionCollection()->action("new_project")->setEnabled(!g_bThreadRunning);
  actionCollection()->action("search")->setEnabled(!g_bThreadRunning);
  actionCollection()->action("file_simulate")->setEnabled(!g_bThreadRunning);
  actionCollection()->action("replace")->setEnabled(!g_bThreadRunning && m_view->stringView()->childCount() > 0);
  actionCollection()->action("save_results")->setEnabled(!g_bThreadRunning && m_view->resultView()->childCount() > 0);
  actionCollection()->action("stop")->setEnabled(g_bThreadRunning);

  // Strings
  actionCollection()->action("strings_add")->setEnabled(!g_bThreadRunning);
  actionCollection()->action("strings_del")->setEnabled(!g_bThreadRunning && m_view->stringView()->childCount() > 0);
  actionCollection()->action("strings_empty")->setEnabled(!g_bThreadRunning && m_view->stringView()->childCount() > 0);
  actionCollection()->action("strings_edit")->setEnabled(!g_bThreadRunning && m_view->stringView()->childCount() > 0);
  actionCollection()->action("strings_save")->setEnabled(!g_bThreadRunning && m_view->stringView()->childCount() > 0);
  actionCollection()->action("strings_load")->setEnabled(!g_bThreadRunning);
  actionCollection()->action("strings_invert")->setEnabled(!g_bThreadRunning && m_view->stringView()->childCount() > 0);
  actionCollection()->action("strings_invert_all")->setEnabled(!g_bThreadRunning && m_view->stringView()->childCount() > 0);

  // Options
  actionCollection()->action("options_recursive")->setEnabled(!g_bThreadRunning);
  actionCollection()->action("options_backup")->setEnabled(!g_bThreadRunning);
  actionCollection()->action("options_case")->setEnabled(!g_bThreadRunning);
  actionCollection()->action("options_wildcards")->setEnabled(!g_bThreadRunning);
  actionCollection()->action("options_var")->setEnabled(!g_bThreadRunning);
  actionCollection()->action("configure_kfilereplace")->setEnabled(!g_bThreadRunning);

  // Results
  actionCollection()->action("results_infos")->setEnabled(!g_bThreadRunning && m_view->resultView()->childCount() > 0);
  actionCollection()->action("results_openfile")->setEnabled(!g_bThreadRunning && m_view->resultView()->childCount() > 0);
  actionCollection()->action("results_editfile")->setEnabled(!g_bThreadRunning && m_view->resultView()->childCount() > 0);
  actionCollection()->action("results_opendir")->setEnabled(!g_bThreadRunning && m_view->resultView()->childCount() > 0);
  actionCollection()->action("results_delete")->setEnabled(!g_bThreadRunning && m_view->resultView()->childCount() > 0);
  actionCollection()->action("results_treeexpand")->setEnabled(!g_bThreadRunning && m_view->resultView()->childCount() > 0);
  actionCollection()->action("results_treereduce")->setEnabled(!g_bThreadRunning && m_view->resultView()->childCount() > 0);

  // Update menus and toolbar
  ((KToggleAction* ) actionCollection()->action("options_recursive"))->setChecked(m_settings.bRecursive);
  ((KToggleAction* ) actionCollection()->action("options_backup"))->setChecked(m_settings.bBackup);
  ((KToggleAction* ) actionCollection()->action("options_case"))->setChecked(m_settings.bCaseSensitive);
  ((KToggleAction* ) actionCollection()->action("options_wildcards"))->setChecked(m_settings.bWildcards);
  ((KToggleAction* ) actionCollection()->action("options_var"))->setChecked(m_settings.bVariables);
}

int KFileReplacePart::checkBeforeOperation(int nTypeOfOperation)
{
  QString strMess;
  QListViewItem* lviCurItem;
  QListViewItem* lviFirst;
  QString strString;
  /*char cFirst, cLast;
  char szString[256];*/
  bool bWildcardsArePresent=false;
  QWidget* w = widget();

  // ================= Check Thread is not running ========================
  if (g_bThreadRunning)
    {
      KMessageBox::error(w, i18n("The replacing operation is already running. You must finish it before."));
      return -1;
    }

  // Check there are strings to replace (not need in search operation)
  if (nTypeOfOperation == OPERATION_REPLACE && m_view->stringView()->childCount() == 0)
    {
      KMessageBox::error(w, i18n("There are no strings to search and replace."));
      return -1;
    }

  // Check the main directory can be accessed
  QDir dir(m_doc->m_strProjectDirectory);

  if (!dir.exists())
    {
      strMess = i18n("<qt>The main folder of the project <b>%1</b> does not exist.</qt>").arg(m_doc->m_strProjectDirectory);
      KMessageBox::error(w, strMess);
      return -1;
    }

  if (::access(QFile::encodeName( m_doc->m_strProjectDirectory ), R_OK | X_OK) == -1)
    {
      strMess = i18n("<qt>Access denied in the main folder of the project:<br><b>%1</b></qt>").arg(m_doc->m_strProjectDirectory);
      KMessageBox::error(w, strMess);
      return -1;
    }

  // ================= Check wildcards properties ========================
  if (m_settings.bWildcards)
    {
      // -- Check first and last char of Searched string is not a wildcard

      //lviCurItem = lviFirst = m_view->stringView()->firstChild();
    /*  if (lviCurItem)
        {
          do
            {
              // Get first char of the string
              strString = lviCurItem->text(0).left(1);
              strncpy(szString, strString.ascii(), 255);
              cFirst = *szString;

              // Get last char of the string
              strString = lviCurItem->text(0).right(1);
              strncpy(szString, strString.ascii(), 255);
              cLast = *szString;*/

              /*if ((cFirst == m_settings.cWildcardsWords) || (cLast == m_settings.cWildcardsWords) || (cFirst == m_settings.cWildcardsLetters) || (cLast == m_settings.cWildcardsLetters))
                {        KMessageBox::error(this, i18n("First and last characters of the searched string cannot be a wildcard."));
                return -1;
                }*/

          /*    lviCurItem = lviCurItem->nextSibling();
            } while(lviCurItem && lviCurItem != lviFirst);
        }*/

      // -- Check Maximum expression length is valid
      if ((m_settings.nMaxExpressionLength < 2) || (m_settings.nMaxExpressionLength > 10000))
        {
           KMessageBox::error(w, i18n("The maximum expression wildcard length is not valid (should be between 2 and 10000)"));
           return -1;
         }

      // check expression wildcard and character wildcards are not the same
      if (m_settings.cWildcardsWords == m_settings.cWildcardsLetters)
        {
           KMessageBox::error(w, i18n("<qt>You cannot use the same character for <b>expression wildcard</b> and for <b>character wildcard</b>.</qt>"));
           return -1;
        }

      // -- If wildcards are enabled, check there are wildcards in the strings, else, desactivate them to optimize
      bWildcardsArePresent = false;

      lviCurItem = lviFirst = m_view->stringView()->firstChild();
      if (lviCurItem)
        {
         do
           {
              QString szString = lviCurItem->text(0);

              if ( szString.contains(m_settings.cWildcardsLetters) || szString.contains(m_settings.cWildcardsWords))
                {
                  // Wildcards are present in at less a string
                  bWildcardsArePresent = true;
                }

              lviCurItem = lviCurItem->nextSibling();
            } while(lviCurItem && lviCurItem != lviFirst);
        }
    }

  /** Prepare argument structure to pass to the replaceDirectory function */

  g_argu.szDir = m_doc->m_strProjectDirectory;
  g_argu.szFilter = m_doc->m_strProjectFilter;
  g_argu.qlvResult = m_view->resultView();
  g_argu.qlvStrings = m_view->stringView();

  g_argu.bMinSize = m_doc->m_bMinSize;
  g_argu.bMaxSize = m_doc->m_bMaxSize;
  g_argu.nMinSize = m_doc->m_nMinSize;
  g_argu.nMaxSize = m_doc->m_nMaxSize;
  g_argu.nTypeOfAccess = m_doc->m_nTypeOfAccess;
  g_argu.bMinDate = m_doc->m_bMinDate;
  g_argu.bMaxDate = m_doc->m_bMaxDate;
  g_argu.qdMinDate = m_doc->m_qdMinDate;
  g_argu.qdMaxDate = m_doc->m_qdMaxDate;

  g_argu.bOwnerUserBool = m_doc->m_bOwnerUserBool;
  g_argu.bOwnerGroupBool = m_doc->m_bOwnerGroupBool;
  g_argu.bOwnerUserMustBe = m_doc->m_bOwnerUserMustBe;
  g_argu.bOwnerGroupMustBe = m_doc->m_bOwnerGroupMustBe;
  g_argu.strOwnerUserType = m_doc->m_strOwnerUserType;
  g_argu.strOwnerGroupType = m_doc->m_strOwnerGroupType;
  g_argu.strOwnerUserValue = m_doc->m_strOwnerUserValue;
  g_argu.strOwnerGroupValue = m_doc->m_strOwnerGroupValue;

  g_argu.bConfirmFiles = m_settings.bConfirmFiles;
  g_argu.bConfirmStrings = m_settings.bConfirmStrings;
  g_argu.bConfirmDirs = m_settings.bConfirmDirs;
  g_argu.bCaseSensitive = m_settings.bCaseSensitive;
  g_argu.bRecursive = m_settings.bRecursive;
  g_argu.bBackup = m_settings.bBackup;
  g_argu.bVariables = m_settings.bVariables;
  g_argu.bFollowSymLinks = m_settings.bFollowSymLinks;
  g_argu.bAllStringsMustBeFound = m_settings.bAllStringsMustBeFound;
  g_argu.bWildcards = m_settings.bWildcards && bWildcardsArePresent;
  g_argu.cWildcardsLetters = m_settings.cWildcardsLetters;
  g_argu.cWildcardsWords = m_settings.cWildcardsWords;
  g_argu.bWildcardsInReplaceStrings = m_settings.bWildcardsInReplaceStrings;
  g_argu.nMaxExpressionLength = m_settings.nMaxExpressionLength;
  g_argu.bHaltOnFirstOccur = m_settings.bHaltOnFirstOccur;
  g_argu.bIgnoreWhitespaces = m_settings.bIgnoreWhitespaces;
  g_argu.bIgnoreHidden = m_settings.bIgnoreHidden;

  g_argu.mainwnd = (QWidget* ) this;
  g_argu.view = m_view;

  // Clear the list view
  m_view->resultView()->clear();

  return 0; // Operation successfully prepared
}

void KFileReplacePart::readOptions()
{
  m_config->setGroup("General Options");

  // Recent files
  m_recentStringFileList = m_config->readListEntry("Recent files");
  ((KRecentFilesAction* ) actionCollection()->action("strings_load_recent"))->setItems(m_recentStringFileList);

  // options seetings (recursivity, backup, case sensitive)
  m_config->setGroup("Options");

  m_settings.bRecursive = m_config->readBoolEntry("Recursivity", RecursiveOption);
  m_settings.bBackup = m_config->readBoolEntry("Backup", BackupOption);
  m_settings.bCaseSensitive = m_config->readBoolEntry("Case sensitive", CaseSensitiveOption);
  m_settings.bWildcards = m_config->readBoolEntry("Wildcards", WildcardsOption);
  m_settings.bVariables = m_config->readBoolEntry("Variables", VariablesOption);
  m_settings.bFollowSymLinks = m_config->readBoolEntry("FollowSymLinks", FollowSymbolicLinksOption);
  m_settings.bAllStringsMustBeFound = m_config->readBoolEntry("AllStringsMustBeFound", AllStringsMustBeFoundOption);
  m_settings.bConfirmFiles = m_config->readBoolEntry("ConfirmFiles", ConfirmFilesOption);
  m_settings.bConfirmStrings = m_config->readBoolEntry("ConfirmStrings", ConfirmStringsOption);
  m_settings.bConfirmDirs = m_config->readBoolEntry("ConfirmDirs", ConfirmDirectoriesOption);
  m_settings.bHaltOnFirstOccur = m_config->readBoolEntry("HaltOnFirstOccur", StopWhenFirstOccurenceOption);
  m_settings.bIgnoreWhitespaces = m_config->readBoolEntry("IgnoreWhitespaces", IgnoreWhiteSpacesOption);
  m_settings.bIgnoreHidden = m_config->readBoolEntry("IgnoreHidden", IgnoreHiddenOption);

  // Wildcards properties
  m_config->setGroup("Wildcards");
  m_settings.bWildcardsInReplaceStrings = m_config->readBoolEntry("WildcardsInReplaceStrings", WildcardsInReplaceStringOption);
  m_settings.cWildcardsLetters = m_config->readNumEntry("WildcardSymbolForLetters", (int) WildcardsLetterOption); // '?'
  m_settings.cWildcardsWords = m_config->readNumEntry("WildcardSymbolForWords", (int) WildcardsWordOption); // '*'
  m_settings.nMaxExpressionLength = m_config->readNumEntry("MaximumExpressionLength", WildcardsExpressionLengthOption);
}

void KFileReplacePart::saveOptions()
{
  m_config->setGroup("General Options");
  // Recent file list
  m_config->writeEntry("Recent files", m_recentStringFileList);

  // options seetings (recursivity, backup, case sensitive)
  m_config->setGroup("Options");

  m_config->writeEntry("Recursivity", m_settings.bRecursive);
  m_config->writeEntry("Backup", m_settings.bBackup);
  m_config->writeEntry("Case sensitive", m_settings.bCaseSensitive);
  m_config->writeEntry("Wildcards", m_settings.bWildcards);
  m_config->writeEntry("Variables", m_settings.bVariables);
  m_config->writeEntry("FollowSymLinks", m_settings.bFollowSymLinks);
  m_config->writeEntry("AllStringsMustBeFound", m_settings.bAllStringsMustBeFound);
  m_config->writeEntry("ConfirmFiles", m_settings.bConfirmFiles);
  m_config->writeEntry("ConfirmStrings", m_settings.bConfirmStrings);
  m_config->writeEntry("ConfirmDirs", m_settings.bConfirmDirs);
  m_config->writeEntry("HaltOnFirstOccur", m_settings.bHaltOnFirstOccur);
  m_config->writeEntry("IgnoreWhitespaces", m_settings.bIgnoreWhitespaces);
  m_config->writeEntry("IgnoreHidden", m_settings.bIgnoreHidden);

  // Wildcards properties
  m_config->setGroup("Wildcards");
  m_config->writeEntry("WildcardsInReplaceStrings", m_settings.bWildcardsInReplaceStrings);
  m_config->writeEntry("WildcardSymbolForLetters", (int) m_settings.cWildcardsLetters);
  m_config->writeEntry("WildcardSymbolForWords", (int) m_settings.cWildcardsWords);
  m_config->writeEntry("MaximumExpressionLength", m_settings.nMaxExpressionLength);

  m_config->sync();
}

void KFileReplacePart::slotFileNew()
{
  emit setStatusBarText(i18n("Creating new document..."));

  if (m_doc->newDocument())
  {
      // Empty lists views
    // m_view->stringView()->clear();
      m_view->resultView()->clear();

      emit setStatusBarText(i18n("Ready."));
      updateCommands();
      m_settings.bRecursive = m_doc->includeSubfolders();
      m_settings.bCaseSensitive = m_doc->caseSensitive();
      m_settings.bWildcards = m_doc->enableWildcards();
      m_settings.bVariables = m_doc->enableVariables();
      m_view->addString(0L, m_doc->searchFor(), m_doc->replaceWith());
      if (!m_doc->searchLater())
      {
          if (m_doc->replaceWith().isEmpty())
            slotFileSearch();
          else
            slotFileReplace();
      }
  }

}

void KFileReplacePart::slotFileSearch()
{
   QString strMess;

   if (checkBeforeOperation(OPERATION_SEARCH) == -1)
      return;

   emit setStatusBarText(i18n("Searching files..."));

   // Run the Replacing operation Thread
   g_bThreadRunning = true;
   g_bThreadMustStop = false;
   g_nFilesRep = 0;
   g_nStringsRep = 0;
   g_nOperation = OPERATION_SEARCH;
   g_argu.view->setSearchMode(true);

   updateCommands();

   // Default error message
   g_szErrMsg = "";

   // show wait cursor
   QApplication::setOverrideCursor( Qt::waitCursor );

   //nRes = pthread_create(&g_threadReplace, NULL, searchThread, (void *) &g_argu);
   //startTimer(100);
   Kernel::instance()->searchThread(&g_argu);

   // restore cursor
   QApplication::restoreOverrideCursor();

   // Show results after operation
   if (g_nFilesRep == -1) // Error
      strMess = i18n("Error while searching/replacing");
   else // Success
   {
      if (!g_argu.bHaltOnFirstOccur) {
         strMess = i18n("%n string found", "%n strings found", g_nStringsRep);
         strMess += i18n(" (in %n file)", " (in %n files)", g_nFilesRep);
      } else
         strMess = i18n("%n file found", "%n files found", g_nFilesRep);
   }

   emit setStatusBarText(strMess); // Show message in status bar
   updateCommands(); // Gray or ungray commands

}

void KFileReplacePart::slotFileReplace()
{
   QString strMess;

   if (checkBeforeOperation(OPERATION_REPLACE) == -1)
      return;

   emit setStatusBarText(i18n("Replacing files..."));

   // Run the Replacing operation Thread
   g_bThreadRunning = true;
   g_bThreadMustStop = false;
   g_nFilesRep = 0;
   g_nStringsRep = 0;
   g_nOperation = OPERATION_REPLACE;
   g_argu.view->setSearchMode(false);
   updateCommands();

   // Default error message
   g_szErrMsg = "";

   // cancel impossible options
   g_argu.bHaltOnFirstOccur = false;

   g_argu.bSimulation = false;

   // show wait cursor
   QApplication::setOverrideCursor( Qt::waitCursor );

   Kernel::instance()->replaceThread(&g_argu);

   // restore cursor
   QApplication::restoreOverrideCursor();

   // Show results after operation
   if (g_nFilesRep == -1) // Error
     strMess = i18n("Error while searching/replacing");
   else // Success
    {
      if (!g_argu.bHaltOnFirstOccur)
        {
          strMess = i18n("%n string successfully replaced", "%n strings successfully replaced", g_nStringsRep);
          strMess += i18n(" (in %n file)", " (in %n files)", g_nFilesRep);
        }
      else
        strMess = i18n("%n file successfully replaced", "%n files successfully replaced", g_nFilesRep);
    }

   emit setStatusBarText(strMess); // Show message in status bar
   updateCommands(); // Gray or ungray commands

}

void KFileReplacePart::slotFileSimulate()
{
   QString strMess;

   if (checkBeforeOperation(OPERATION_REPLACE) == -1)
      return;

   emit setStatusBarText(i18n("Replacing files... (simulation)"));

   // Run the Replacing operation Thread
   g_bThreadRunning = true;
   g_bThreadMustStop = false;
   g_nFilesRep = 0;
   g_nStringsRep = 0;
   g_nOperation = OPERATION_REPLACE;
   g_argu.view->setSearchMode(false);
   updateCommands();

   // Default error message
   g_szErrMsg = "";

   // cancel impossible options
   g_argu.bHaltOnFirstOccur = false;

   g_argu.bSimulation = true;

   // show wait cursor
   QApplication::setOverrideCursor( Qt::waitCursor );

   Kernel::instance()->replaceThread(&g_argu);

   // restore cursor
   QApplication::restoreOverrideCursor();

   // Show results after operation
   if (g_nFilesRep == -1) // Error
     strMess = i18n("Error while searching/replacing");
   else // Success
     {
       if (!g_argu.bHaltOnFirstOccur)
         {
           strMess = i18n("%n string successfully replaced", "%n strings successfully replaced", g_nStringsRep);
           strMess += i18n(" (in %n file)", " (in %n files)", g_nFilesRep);
         }
       else
         strMess = i18n("%n file successfully replaced", "%n files successfully replaced", g_nFilesRep);
     }

   emit setStatusBarText(strMess); // Show message in status bar
   updateCommands(); // Gray or ungray commands
}

void KFileReplacePart::slotFileStop()
{
   if (!g_bThreadRunning) // If no thread running
      return ;

   // Stop the current thread
   g_bThreadMustStop = true;
   updateCommands();

   // restore cursor
   QApplication::restoreOverrideCursor();
}

void KFileReplacePart::slotFileSave()
{
  QString fileName;

  QWidget* w = widget();

  QListView* lvResult = m_view->resultView();

  // Check there are results
  if (lvResult->childCount() == 0)
    {
      KMessageBox::error(w, i18n("There are no results to save: the result list is empty."));
      return ;
    }

  // Select the file where results will be saved
  fileName = KFileDialog::getSaveFileName(QString::null, i18n("*.xhtml|XHTML Files (*.xhtml)\n*|All Files (*)"), w, i18n("Save Results"));
  if (fileName.isEmpty())
    return ;

  // Force the extension to be "html"
  fileName = KFileReplaceLib::instance()->addFilenameExtension(fileName, "xhtml");

  // Save results to file
 // a) Open the file
  QFile fResults(fileName);
  if ( !fResults.open( IO_WriteOnly ) )
    {
      KMessageBox::error(w, i18n("<qt>Cannot open the file <b>%1</b> for writing the save results.</qt>").arg(fileName));
      return ;
    }

    // b) Write header of the XHTML file
   QDateTime datetime = QDateTime::currentDateTime(Qt::LocalTime);
   QString dateString = datetime.toString(Qt::LocalDate);
   QString XHTML = "<?xml version=\"1.0\" ?>\n"
                   "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
                   "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
                   "\t<head>\n"
                   "\t\t<title>";
           XHTML += i18n("KFileReplace Results File")+ "</title>\n"
                    "\t\t\t<style type=\"text/css\">\n"
                    "\t\t\t\t.a {background-color : lightblue;}\n"
                    "\t\t\t\t.b {background-color : paleturquoise;}\n"
                    "\t\t\t\tbody { border: solid teal;}\n"
                    "\t\t\t\t.date { text-align:right; color:darkcyan;}\n"
                    "\t\t\t</style>\n"
                    "\t</head>\n"
                    "\t<body>\n"
                    "\t\t<div style=\"background-color:ivory;padding :10px;\">\n"
                    "\t\t\t<table width=\"100%\">\n"
                    "\t\t\t\t<tr>\n"
                    "\t\t\t\t\t<td><h1>";
           XHTML += i18n("KFileReplace Results File")+"</h1></td>\n"
                    "\t\t\t\t\t<td><div class=\"date\">";
           XHTML += i18n("Creation date: %1").arg( dateString ) + "</div></td>\n"
                    "\t\t\t\t</tr>\n"
                    "\t\t\t</table>\n"
                    "\t\t<div>\n"
                    "\t\t\t<dl>\n";
   QTextStream oTStream( &fResults );
   oTStream << XHTML;

  // c) Write the file list
  QListViewItem* lviCurItem;
  QListViewItem* lviFirst;
  QString strPath;

  lviCurItem = lviFirst = lvResult->firstChild();

  if (lviCurItem == 0)
    return ;

  unsigned int replacedFileNumber = 0;

  QString classValue="a";
  do
    {
      strPath = KFileReplaceLib::instance()->formatFullPath(lviCurItem->text(1), lviCurItem->text(0));
      QString divclassString ="\t\t\t\t<div class=\""+classValue+"\">\n"
                              "\t\t\t\t\t<dt><a href=\"file:"+strPath+"\">file:"+strPath+"</a>\n"
                              "\t\t\t\t\t</dt>\n";
              divclassString += "\t\t\t\t\t<dd>"
                             + i18n("Old size:")
                             +" "
                             + lviCurItem->text(2)
                             +" "
                             +i18n("--> New size:")
                             + " "
                             + lviCurItem->text(3)
                             +" "
                             +i18n("--> Replaced strings:")
                             +" "
                             + lviCurItem->text(4)
                             +"</dd>\n"
                             "\t\t\t\t</div>\n";
      oTStream << divclassString;

      if(classValue == "a")
        classValue = "b";
      else
        classValue = "a";

      replacedFileNumber += lviCurItem->text(4).toInt();

      lviCurItem = lviCurItem->nextSibling();
    } while(lviCurItem && lviCurItem != lviFirst);


  // d) Write the end of the file

   oTStream<<"\t\t\t\t</dl>\n"
             "\t\t\t</div>\n"
             "\t\t\t<div style=\"text-align:right;color:darkcyan\">"
           <<i18n("Number of replaced strings: %1").arg( replacedFileNumber )
           <<"</div>\n"
             "\t\t</div>\n"
             "\t</body>\n"
             "</html>\n";
   fResults.close();
   updateCommands();
}

void KFileReplacePart::slotStringsAdd()
{
  m_view->slotStringsAdd();
  updateCommands();
  return;
}

void KFileReplacePart::slotStringsDel()
{
  delete m_view->stringView()->currentItem(); // Remove item from ListView
  updateCommands();
}

void KFileReplacePart::slotStringsEmpty()
{
  m_view->stringView()->clear();
  updateCommands();
}

void KFileReplacePart::slotStringsEdit()
{
  m_view->slotStringsEdit(0L);
  updateCommands();
}

void KFileReplacePart::slotStringsSave()
{
  QWidget* w = widget();

  // Check there are strings in the list
  if (m_view->stringView()->childCount() == 0)
    {
      KMessageBox::error(w, i18n("There are no strings to save in the list."));
      return ;
    }

   QString header("<?xml version=\"1.0\" ?>\n<kfr>"),
           footer("\n</kfr>"),
           body;
   QListViewItem*  lvi = m_view->stringView()->firstChild();

   while( lvi )
     {
       body += "\n\t<replacement>\n\t\t<oldstring><![CDATA[";
       body += lvi->text(0);
       body += "]]></oldstring>\n\t\t";
       body += "<newstring><![CDATA[";
       body += lvi->text(1);
       body += "]]></newstring>\n\t</replacement>";
       lvi = lvi->nextSibling();
     }

   // Select the file where strings will be saved
   QString fileName = KFileDialog::getSaveFileName(QString::null, i18n("*.kfr|KFileReplace Strings (*.kfr)\n*|All Files (*)"), w, i18n("Save Strings to File"));
   if (fileName.isEmpty())
     return;

   // Force the extension to be "kfr" == KFileReplace extension
   fileName = KFileReplaceLib::instance()->addFilenameExtension(fileName, "kfr");

   QFile file( fileName );
   if(!file.open( IO_WriteOnly ))
     {
       KMessageBox::error(w, i18n("File %1 cannot be saved.").arg(fileName));
       return ;
     }
   QTextStream oTStream( &file );
   oTStream << header
            << body
            << footer;
   file.close();
}

void KFileReplacePart::loadStringFile(const QString& fileName)
{
  //load kfr file
  QDomDocument doc( "mydocument" );
  QFile file( fileName );
  if ( !file.open( IO_ReadOnly ) )
    {
      KMessageBox::error(widget(), i18n("<qt>Cannot open the file <b>%1</b> and load the string list.</qt>").arg(fileName));
      return ;
    }
  if ( !doc.setContent( &file ) )
    {
      file.close();
      KMessageBox::information(widget(), i18n("<qt>File <b>%1</b> seems not to be written in new kfr format. Remember that old kfr format will be soon abandoned! You can convert your old rules files by simply saving them with kfilereplace.</qt>").arg(fileName),i18n("Warning"));

      convertOldToNewKFRFormat(fileName,m_view);
      return;

    }
  file.close();

  //clear view
  m_view->stringView()->clear();

  QDomElement docElem = doc.documentElement();

  QDomNode n = docElem.firstChild();
  while( !n.isNull() )
    {
      QDomElement e = n.toElement(); // try to convert the node to an element.
      if( !e.isNull() )
        {
          QListViewItem* lvi = new QListViewItem(m_view->stringView());
          QString oldString = e.firstChild().toElement().text();
          lvi->setText(0,oldString);
          QString newString = e.lastChild().toElement().text();
          lvi->setText(1,newString);
          lvi->setPixmap(0, m_view->iconString());
        }
      n = n.nextSibling();
    }

     // Add file to "load strings form file" menu
  if (!m_recentStringFileList.contains(fileName))
    {
      m_recentStringFileList.append(fileName);
      ((KRecentFilesAction* ) actionCollection()->action("strings_load_recent"))->setItems(m_recentStringFileList);
    }
  updateCommands();
}

void KFileReplacePart::slotStringsLoad()
{
  QString fileName;

  // Select the file to load from
  fileName = KFileDialog::getOpenFileName(QString::null, i18n("*.kfr|KFileReplace Strings (*.kfr)\n*|All Files (*)"), widget(), i18n("Load Strings From File"));

  if(!fileName.isEmpty())
    loadStringFile(fileName);
  updateCommands();
}

void KFileReplacePart::slotStringsInvertCur()
{
  QListViewItem* lviCurItem;
  lviCurItem = m_view->stringView()->currentItem();
  if (lviCurItem == 0)
    return;

  QString searchText, replaceText;
  searchText = lviCurItem->text(0);
  replaceText = lviCurItem->text(1);

  // Cannot invert the string if search string will be empty
  if (replaceText.isEmpty())
    {
      KMessageBox::error(widget(), i18n("<qt>Cannot invert string <b>%1</b>, because the search string would be empty.</qt>").arg(searchText));
      return;
    }

  lviCurItem->setText(0, replaceText);
  lviCurItem->setText(1, searchText);
}

void KFileReplacePart::slotStringsInvertAll()
{
  QListViewItem* lviCurItem,
               * lviFirst;
  int nItemPos;
  QString searchText,
          replaceText,
          strMess;

  nItemPos = 0;
  lviCurItem = lviFirst = m_view->stringView()->firstChild();
  if (lviCurItem == 0)
    return ;

  do
    {
      searchText = lviCurItem->text(0);
      replaceText = lviCurItem->text(1);

      // Cannot invert the string if search string will be empty
      if (replaceText.isEmpty())
        {
          KMessageBox::error(widget(), i18n("<qt>Cannot invert string <b>%1</b>, because the search string would be empty.</qt>").arg(searchText));
          return;
        }

      lviCurItem->setText(0, replaceText);
      lviCurItem->setText(1, searchText);

      lviCurItem = lviCurItem->nextSibling();
    } while(lviCurItem && lviCurItem != lviFirst);
}

void KFileReplacePart::slotOpenRecentStringFile(const KURL& urlFile)
{
  QString fileName;
  QFileInfo fi;

  if (g_bThreadRunning) // Thread running: it has not finished since the last call of this function
    return ;

  // Download file if need (if url is "http://...")
  if (!(KIO::NetAccess::download(urlFile, fileName, 0L))) // Open the Archive
    return;

  // Check it's not a directory
  fi.setFile(fileName);
  if (fi.isDir())
    {
      KMessageBox::error(widget(), i18n("Cannot open folders."));
      return;
    }

  loadStringFile(fileName);
  updateCommands();
}

void KFileReplacePart::slotOptionsRecursive()
{
  m_settings.bRecursive = !(m_settings.bRecursive);
  updateCommands();
}

void KFileReplacePart::slotOptionsBackup()
{
  m_settings.bBackup = !(m_settings.bBackup);
  updateCommands();
}

void KFileReplacePart::slotOptionsCaseSensitive()
{
  m_settings.bCaseSensitive = !(m_settings.bCaseSensitive);
  updateCommands();
}

void KFileReplacePart::slotOptionsVariables()
{
  m_settings.bVariables = !(m_settings.bVariables);
  updateCommands();
}

void KFileReplacePart::slotOptionsWildcards()
{
  m_settings.bWildcards = !(m_settings.bWildcards);
  updateCommands();
}

void KFileReplacePart::slotOptionsPreferences()
{
  KOptionsDlg dlg(widget(), 0, m_settings);

  if (dlg.exec() != QDialog::Rejected) // If Cancel
  {
    m_settings = dlg.settings();
    saveOptions();
    updateCommands();
  }
}

void KFileReplacePart::setWhatsThis()
{
  actionCollection()->action("file_simulate")->setWhatsThis(i18n(fileSimulateWhatthis));
  actionCollection()->action("options_wildcards")->setWhatsThis(i18n(optionsWildcardsWhatthis));
  actionCollection()->action("options_backup")->setWhatsThis(i18n(optionsBackupWhatthis));
  actionCollection()->action("options_case")->setWhatsThis(i18n(optionsCaseWhatthis));
  actionCollection()->action("options_var")->setWhatsThis(i18n(optionsVarWhatthis));
  actionCollection()->action("options_recursive")->setWhatsThis(i18n(optionsRecursiveWhatthis));
}

void KFileReplacePart::reportBug()
{
  KAboutData aboutData("kfilereplace", I18N_NOOP("KFileReplace"), "1.0");
  KBugReport dlg(widget(), true, &aboutData);
  dlg.exec();
}

void KFileReplacePart::appHelpActivated()
{
  kapp->invokeHelp(QString::null, "kfilereplace");
}

void KFileReplacePart::showAboutApplication()
{
  m_dlgAbout = new KAboutKFileReplace(widget(), 0, false);
  if(m_dlgAbout == 0)
    return;

  if(!m_dlgAbout->isVisible())
    m_dlgAbout->show();
  else
    m_dlgAbout->raise();

  // Update menu & toolbar commands
  updateCommands();
}

void KFileReplacePart::convertOldToNewKFRFormat(const QString& fileName, KFileReplaceView* view)
{
 //this method convert old format in new XML-based format
 //if is an old format file try to open it
 typedef struct
 {
   char szPgm[13]; // Must be "KFileReplace" : like MZ for EXE files
   int nStringsNb; // Number of strings in file
   char cReserved[64]; // Reserved for future use
 } KFRHeader;

 KFRHeader head;

 FILE* f = fopen(fileName.ascii(),"rb");
 if(!f)
 {
  KMessageBox::error(widget(), i18n("<qt>Cannot open the file <b>%1</b> and load the string list.</qt>").arg(fileName));
  return ;
 }
 int err = fread(&head, sizeof(KFRHeader), 1, f);
 if(err != 1)
 {
  KMessageBox::error(widget(), i18n("<qt>Cannot open the file <b>%1</b> and load the string list.</qt>").arg(fileName));
  return ;
 }
 QString szPgm(head.szPgm);
 if (szPgm != "KFileReplace")
 {
  KMessageBox::error(widget(), i18n("<qt>Cannot open the file <b>%1</b> and load the string list. This file seems not to be a valid old kfr file.</qt>").arg(fileName));
  return ;
 }

  view->stringView()->clear();

  int nOldTextSize,
      nNewTextSize;
  int nErrors = 0;
  int nStrSize;
  QStringList l;

  int i ;
  for (i=0; i < head.nStringsNb; i++)
    {
      nErrors += (fread(&nOldTextSize, sizeof(int), 1, f)) != 1;
      nErrors += (fread(&nNewTextSize, sizeof(int), 1, f)) != 1;
      if (nErrors > 0)
        {
          KMessageBox::error(widget(), i18n("<qt>Cannot read data.</qt>"));
        }
      else
        {
          nStrSize = ((nOldTextSize > nNewTextSize) ? nOldTextSize : nNewTextSize) + 2;
          char* oldString = (char*) malloc(nStrSize+10),
              * newString = (char*) malloc(nStrSize+10);
          memset(oldString, 0, nStrSize);
          memset(newString,0, nStrSize);
          if (oldString == 0 || newString == 0)
            KMessageBox::error(widget(), i18n("<qt>Out of memory.</qt>"));

          else
            {
              if (fread(oldString, nOldTextSize, 1, f) != 1)
                KMessageBox::error(widget(), i18n("<qt>Cannot read data.</qt>"));

              else
                {
                  if (nNewTextSize > 0) // If there is a Replace text
                    {
                      if (fread(newString, nNewTextSize, 1, f) != 1)
                        KMessageBox::error(widget(), i18n("<qt>Cannot read data.</qt>"));

                      else
                        {
                          //
                          QListViewItem* lvi = new QListViewItem(view->stringView());
                          lvi->setText(0,oldString);
                          lvi->setText(1,newString);
                          lvi->setPixmap(0, view->iconString());

                          if(newString)
                            free(newString);
                          if(oldString)
                            free(oldString);
                        }
                    }
                }
            }
        }
    }
    fclose(f);
    return ;
 }

#include "kfilereplacepart.moc"