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
|
/*****************************************************************************
tdefilereplaceview.cpp - description
-------------------
begin : sam oct 16 15:28:00 CEST 1999
copyright : (C) 1999 by François Dupoux <[email protected]>
(C) 2004 Emiliano Gulmini <[email protected]>
*****************************************************************************/
/*****************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
*****************************************************************************/
// TQt
#include <tqwhatsthis.h>
#include <tqmap.h>
#include <tqfileinfo.h>
// KDE
#include <tdelistview.h>
#include <tdemessagebox.h>
#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <krun.h>
#include <kpropertiesdialog.h>
#include <tdeapplication.h>
#include <dcopclient.h>
#include <dcopref.h>
//#include <kdebug.h>
#include <kiconloader.h>
#include <kled.h>
#include <tdefiledialog.h>
// local
#include "tdefilereplaceview.h"
#include "tdefilereplacelib.h"
#include "kaddstringdlg.h"
#include "whatthis.h"
using namespace whatthisNameSpace;
TDEFileReplaceView::TDEFileReplaceView(RCOptions* info, TQWidget *parent,const char *name):TDEFileReplaceViewWdg(parent,name)
{
m_option = info;
initGUI();
// connect events
connect(m_lvResults, TQT_SIGNAL(mouseButtonClicked(int, TQListViewItem *, const TQPoint &, int)), this, TQT_SLOT(slotMouseButtonClicked(int, TQListViewItem *, const TQPoint &)));
connect(m_lvResults_2, TQT_SIGNAL(mouseButtonClicked(int, TQListViewItem *, const TQPoint &, int)), this, TQT_SLOT(slotMouseButtonClicked(int, TQListViewItem *, const TQPoint &)));
connect(m_lvStrings, TQT_SIGNAL(doubleClicked(TQListViewItem *)), this, TQT_SLOT(slotStringsEdit()));
connect(m_lvStrings_2, TQT_SIGNAL(doubleClicked(TQListViewItem *)), this, TQT_SLOT(slotStringsEdit()));
whatsThis();
}
TQString TDEFileReplaceView::currentPath()
{
TQListViewItem *lvi;
if(! m_lviCurrent) lvi = m_rv->currentItem();
else lvi = (TQListViewItem*) m_lviCurrent;
while (lvi->parent())
lvi = lvi->parent();
return TQString(lvi->text(1)+"/"+lvi->text(0));
}
void TDEFileReplaceView::showSemaphore(TQString s)
{
if(s == "green")
{
m_ledGo->setState(KLed::On);
m_ledWait->setState(KLed::Off);
m_ledStop->setState(KLed::Off);
}
else
if(s == "yellow")
{
m_ledGo->setState(KLed::Off);
m_ledWait->setState(KLed::On);
m_ledStop->setState(KLed::Off);
}
else
if(s == "red")
{
m_ledGo->setState(KLed::Off);
m_ledWait->setState(KLed::Off);
m_ledStop->setState(KLed::On);
}
}
void TDEFileReplaceView::stringsInvert(bool invertAll)
{
TQListViewItem* lviCurItem,
* lviFirst;
TDEListView* sv = getStringsView();
if(invertAll)
lviCurItem = lviFirst = sv->firstChild();
else
lviCurItem = lviFirst = sv->currentItem();
if(lviCurItem == 0)
return ;
do
{
TQString searchText = lviCurItem->text(0),
replaceText = lviCurItem->text(1);
// Cannot invert the string when search string is empty
if (replaceText.isEmpty())
{
KMessageBox::error(0, 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();
if(!invertAll)
break;
} while(lviCurItem && lviCurItem != lviFirst);
setCurrentStringsViewMap();
}
void TDEFileReplaceView::changeView(bool searchingOnlyMode)
{
if(searchingOnlyMode)
{
m_stackResults->raiseWidget(m_lvResults_2);
m_stackStrings->raiseWidget(m_lvStrings_2);
m_rv = m_lvResults_2;
m_sv = m_lvStrings_2;
}
else
{
m_stackResults->raiseWidget(m_lvResults);
m_stackStrings->raiseWidget(m_lvStrings);
m_rv = m_lvResults;
m_sv = m_lvStrings;
}
}
TDEListView* TDEFileReplaceView::getResultsView()
{
if(m_option->m_searchingOnlyMode)
m_rv = m_lvResults_2;
else
m_rv = m_lvResults;
return m_rv;
}
TDEListView* TDEFileReplaceView::getStringsView()
{
if(m_option->m_searchingOnlyMode)
m_sv = m_lvStrings_2;
else
m_sv = m_lvStrings;
return m_sv;
}
//PUBLIC SLOTS
void TDEFileReplaceView::slotMouseButtonClicked (int button, TQListViewItem *lvi, const TQPoint &pos)
{
if (lvi == 0) // No item selected
return;
// RIGHT BUTTON
if (button == Qt::RightButton)
{
m_lviCurrent = static_cast<TDEListViewItem*>(lvi);
m_menuResult->popup(pos);
}
}
void TDEFileReplaceView::slotResultProperties()
{
TQString currItem = currentPath();
if(! currItem.isEmpty())
{
KURL url(currItem);
(void) new KPropertiesDialog(url);
m_lviCurrent = 0;
}
}
void TDEFileReplaceView::slotResultOpen()
{
TQString currItem = currentPath();
if(!currItem.isEmpty())
{
(void) new KRun(KURL(currItem), 0, true, true);
m_lviCurrent = 0;
}
}
void TDEFileReplaceView::slotResultOpenWith()
{
TQString currItem = currentPath();
if(!currItem.isEmpty())
{
KURL::List kurls;
kurls.append(KURL(currItem));
KRun::displayOpenWithDialog(kurls);
m_lviCurrent = 0;
}
}
void TDEFileReplaceView::slotResultDirOpen()
{
TQString currItem = currentPath();
if(!currItem.isEmpty())
{
TQFileInfo fi;
fi.setFile(currItem);
(void) new KRun (KURL::fromPathOrURL(fi.dirPath()), 0, true, true);
m_lviCurrent = 0;
}
}
void TDEFileReplaceView::slotResultEdit()
{
TQListViewItem *lvi = m_rv->firstChild();
while (lvi)
{
DCOPClient *client = kapp->dcopClient();
DCOPRef quanta(client->appId(),"WindowManagerIf");
TQString path = TQString(lvi->text(1)+"/"+lvi->text(0));
TQListViewItem *lviChild = lvi;
while(lviChild)
{
if(lviChild->isSelected())
{
coord c;
if(lviChild == lvi)
{
c.line = 0;
c.column = 0;
}
else
{
c= extractWordCoordinates(lviChild);
}
bool success = quanta.send("openFile", path, c.line, c.column);
if(!success)
{
TQString message = i18n("File %1 cannot be opened. Might be a DCOP problem.").arg(path);
KMessageBox::error(parentWidget(), message);
}
}
if (lviChild == lvi)
lviChild = lviChild->firstChild();
else
lviChild = lviChild->nextSibling();
}
lvi = lvi->nextSibling();
}
m_lviCurrent = 0;
}
void TDEFileReplaceView::slotResultDelete()
{
TQString currItem = currentPath();
if (!currItem.isEmpty())
{
TQFile fi;
int answer = KMessageBox::warningContinueCancel(this, i18n("Do you really want to delete %1?").arg(currItem),
TQString(),KStdGuiItem::del());
if(answer == KMessageBox::Continue)
{
fi.setName(currItem);
fi.remove();
delete m_lviCurrent;
m_lviCurrent = 0;
}
}
}
void TDEFileReplaceView::slotResultTreeExpand()
{
TQListViewItem *lviRoot = getResultsView()->firstChild();
if(lviRoot)
expand(lviRoot, true);
}
void TDEFileReplaceView::slotResultTreeReduce()
{
TQListViewItem *lviRoot = getResultsView()->firstChild();
if(lviRoot)
expand(lviRoot, false);
}
void TDEFileReplaceView::slotStringsAdd()
{
KeyValueMap oldMap(m_option->m_mapStringsView);
KAddStringDlg addStringDlg(m_option, false);
if(!addStringDlg.exec())
return;
KeyValueMap addedStringsMap(m_option->m_mapStringsView);
KeyValueMap::Iterator itMap;
for(itMap = oldMap.begin(); itMap != oldMap.end(); ++itMap)
addedStringsMap.insert(itMap.key(),itMap.data());
m_option->m_mapStringsView = addedStringsMap;
raiseResultsView();
raiseStringsView();
loadMapIntoView(addedStringsMap);
}
void TDEFileReplaceView::slotQuickStringsAdd(const TQString& quickSearch, const TQString& quickReplace)
{
if(!quickSearch.isEmpty())
{
KeyValueMap map;
if(quickReplace.isEmpty())
{
map[quickSearch] = TQString();
m_option->m_searchingOnlyMode = true;
}
else
{
map[quickSearch] = quickReplace;
m_option->m_searchingOnlyMode = false;
}
m_option->m_mapStringsView = map;
raiseResultsView();
raiseStringsView();
loadMapIntoView(map);
}
}
void TDEFileReplaceView::slotStringsEdit()
{
KeyValueMap oldMap(m_option->m_mapStringsView);
if(oldMap.isEmpty()) return;
bool oldSearchFlagValue = m_option->m_searchingOnlyMode;
oldMap.remove(m_sv->currentItem()->text(0));
m_option->m_mapStringsView.clear();
m_option->m_mapStringsView.insert(m_sv->currentItem()->text(0), m_sv->currentItem()->text(1));
KAddStringDlg addStringDlg(m_option, true);
if(!addStringDlg.exec())
return;
KeyValueMap newMap(m_option->m_mapStringsView);
if(oldSearchFlagValue == m_option->m_searchingOnlyMode)
{
KeyValueMap::Iterator itMap;
//merges the two maps
for(itMap = oldMap.begin(); itMap != oldMap.end(); ++itMap)
newMap.insert(itMap.key(),itMap.data());
}
m_option->m_mapStringsView = newMap;
raiseResultsView();
raiseStringsView();
loadMapIntoView(newMap);
}
void TDEFileReplaceView::slotStringsSave()
{
// Check there are strings in the list
TDEListView* sv = getStringsView();
if (sv->firstChild() == 0)
{
KMessageBox::error(0, i18n("No strings to save as the list is empty."));
return ;
}
TQString header("<?xml version=\"1.0\" ?>\n<kfr>"),
footer("\n</kfr>"),
body;
if(m_option->m_searchingOnlyMode)
header += "\n\t<mode search=\"true\"/>";
else
header += "\n\t<mode search=\"false\"/>";
TQListViewItem* lvi = sv->firstChild();
while( lvi )
{
body += TQString("\n\t<replacement>"
"\n\t\t<oldstring><![CDATA[%1]]></oldstring>"
"\n\t\t<newstring><![CDATA[%2]]></newstring>"
"\n\t</replacement>").arg(lvi->text(0)).arg(lvi->text(1));
lvi = lvi->nextSibling();
}
// Selects the file where strings will be saved
TQString menu = "*.kfr|" + i18n("TDEFileReplace Strings") + " (*.kfr)\n*|" + i18n("All Files") + " (*)";
TQString fileName = KFileDialog::getSaveFileName(TQString(), menu, 0, i18n("Save Strings to File"));
if (fileName.isEmpty())
return;
// Forces the extension to be "kfr" == TDEFileReplace extension
fileName = TDEFileReplaceLib::addExtension(fileName, "kfr");
TQFile file( fileName );
if(!file.open( IO_WriteOnly ))
{
KMessageBox::error(0, i18n("File %1 cannot be saved.").arg(fileName));
return ;
}
TQTextStream oTStream( &file );
oTStream.setEncoding(TQTextStream::UnicodeUTF8);
oTStream << header
<< body
<< footer;
file.close();
}
void TDEFileReplaceView::slotStringsDeleteItem()
{
TQListViewItem* item = m_sv->currentItem();
if(item != 0)
{
KeyValueMap m = m_option->m_mapStringsView;
m.remove(item->text(0));
m_option->m_mapStringsView = m;
delete item;
}
}
void TDEFileReplaceView::slotStringsEmpty()
{
TQListViewItem * myChild = m_sv->firstChild();
while( myChild )
{
TQListViewItem* item = myChild;
myChild = myChild->nextSibling();
delete item;
}
KeyValueMap m;
m_option->m_mapStringsView = m;
}
//PRIVATE
void TDEFileReplaceView::initGUI()
{
m_option->m_searchingOnlyMode = true;
m_stackResults->addWidget(m_lvResults);
m_stackResults->addWidget(m_lvResults_2);
m_stackStrings->addWidget(m_lvStrings);
m_stackStrings->addWidget(m_lvStrings_2);
DCOPClient *client = kapp->dcopClient();
QCStringList appList = client->registeredApplications();
bool quantaFound = false;
for(QCStringList::Iterator it = appList.begin(); it != appList.end(); ++it)
{
if((*it).left(6) == "quanta")
{
quantaFound = true;
break;
}
}
m_menuResult = new TDEPopupMenu(this, "ResultPopup");
m_menuResult->insertItem(SmallIconSet(TQString::fromLatin1("document-open")),
i18n("&Open"),
this,
TQT_SLOT(slotResultOpen()));
if(!quantaFound)
{
m_menuResult->insertItem(i18n("Open &With..."),
this,
TQT_SLOT(slotResultOpenWith()));
}
if(quantaFound)
{
m_menuResult->insertItem(SmallIconSet("quanta"),
i18n("&Edit in Quanta"),
this,
TQT_SLOT(slotResultEdit()));
}
m_menuResult->insertItem(SmallIconSet(TQString::fromLatin1("go-up")),
i18n("Open Parent &Folder"),
this,
TQT_SLOT(slotResultDirOpen()));
m_menuResult->insertItem(SmallIconSet(TQString::fromLatin1("edit-delete")),
i18n("&Delete"),
this,
TQT_SLOT(slotResultDelete()));
m_menuResult->insertSeparator();
m_menuResult->insertItem(SmallIconSet(TQString::fromLatin1("application-vnd.tde.info")),
i18n("&Properties"),
this,
TQT_SLOT(slotResultProperties()));
raiseResultsView();
raiseStringsView();
}
void TDEFileReplaceView::raiseStringsView()
{
if(m_option->m_searchingOnlyMode)
m_sv = m_lvStrings_2;
else
m_sv = m_lvStrings;
m_stackStrings->raiseWidget(m_sv);
}
void TDEFileReplaceView::raiseResultsView()
{
if(m_option->m_searchingOnlyMode)
m_rv = m_lvResults_2;
else
m_rv = m_lvResults;
m_stackResults->raiseWidget(m_rv);
}
coord TDEFileReplaceView::extractWordCoordinates(TQListViewItem* lvi)
{
//get coordinates of the first string of the current selected file
coord c;
c.line = 0;
c.column = 0;
TQString s = lvi->text(0);
//tqWarning("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW=%s",s.latin1());
/* if(lvi->parent()) s = lvi->text(0);
else return c;*/
TQString temp;
int i = 0;
//extracts line and column from lvi->text(0)
//FIXME: Don't get the line and column number from the text as it's translated and it will
//fail for non-English languages!
//EMILIANO: This is not a good fixing but for now it should reduce the problems
while(true)
{
if(s[i] < '0' || s[i] > '9')
i++;
else
break;
}
while(true)
{
if(s[i] >= '0' && s[i] <= '9')
{
temp += s[i];
i++;
}
else
break;
}
c.line = temp.toInt();
temp = TQString();
while(true)
{
if(s[i] < '0' || s[i] > '9')
i++;
else
break;
}
while(true)
{
if(s[i] >= '0' && s[i] <= '9')
{
temp += s[i];
i++;
}
else
break;
}
c.column = temp.toInt();
if(c.line > 0) c.line--;
if(c.column > 0) c.column--;
return c;
}
void TDEFileReplaceView::expand(TQListViewItem *lviCurrent, bool b)
{
// current item
lviCurrent->setOpen(b);
// recursivity
while((lviCurrent = lviCurrent->nextSibling()) != 0)
{
lviCurrent->setOpen(b);
if (lviCurrent->firstChild())
expand(lviCurrent->firstChild(), b);
}
}
void TDEFileReplaceView::setMap()
{
KeyValueMap map;
TQListViewItem* i = m_sv->firstChild();
while(i != 0)
{
if(m_option->m_searchingOnlyMode)
map[i->text(0)] = TQString();
else
map[i->text(0)] = i->text(1);
i = i->nextSibling();
}
m_option->m_mapStringsView = map;
}
void TDEFileReplaceView::loadMapIntoView(KeyValueMap map)
{
m_sv->clear();
KeyValueMap::Iterator itMap;
for(itMap = map.begin(); itMap != map.end(); ++itMap)
{
TQListViewItem* lvi = new TQListViewItem(m_sv);
lvi->setMultiLinesEnabled(true);
lvi->setText(0,itMap.key());
if(!m_option->m_searchingOnlyMode)
lvi->setText(1,itMap.data());
}
}
void TDEFileReplaceView::whatsThis()
{
TQWhatsThis::add(getResultsView(), lvResultWhatthis);
TQWhatsThis::add(getStringsView(), lvStringsWhatthis);
}
#include "tdefilereplaceview.moc"
|