You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tdeutils/tdefilereplace/tdefilereplaceview.cpp

690 lines
18 KiB

/*****************************************************************************
tdefilereplaceview.cpp - description
-------------------
begin : sam oct 16 15:28:00 CEST 1999
copyright : (C) 1999 by Fran<61>is Dupoux <dupoux@dupoux.com>
(C) 2004 Emiliano Gulmini <emi_barbarossa@yahoo.it>
*****************************************************************************/
/*****************************************************************************
* *
* 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();
// result listview: connect events
connect(m_lvResults, TQT_SIGNAL(mouseButtonClicked(int, TQListViewItem *, const TQPoint &, int)),
this, TQT_SLOT(slotResultMouseButtonClicked(int, TQListViewItem *, const TQPoint &)));
connect(m_lvResults, TQT_SIGNAL(returnPressed(TQListViewItem *)),
this, TQT_SLOT(slotResultReturnPressed(TQListViewItem *)));
connect(m_lvResults, TQT_SIGNAL(executed(TQListViewItem *)),
this, TQT_SLOT(slotResultReturnPressed(TQListViewItem *)));
connect(m_lvResults_2, TQT_SIGNAL(mouseButtonClicked(int, TQListViewItem *, const TQPoint &, int)),
this, TQT_SLOT(slotResultMouseButtonClicked(int, TQListViewItem *, const TQPoint &)));
connect(m_lvResults_2, TQT_SIGNAL(returnPressed(TQListViewItem *)),
this, TQT_SLOT(slotResultReturnPressed(TQListViewItem *)));
connect(m_lvResults_2, TQT_SIGNAL(executed(TQListViewItem *)),
this, TQT_SLOT(slotResultReturnPressed(TQListViewItem *)));
// connect header events
connect(m_lvResults->header(), TQT_SIGNAL(clicked(int)), this,TQT_SLOT(slotResultHeaderClicked(int)));
connect(m_lvResults_2->header(), TQT_SIGNAL(clicked(int)), this,TQT_SLOT(slotResultHeaderClicked(int)));
// string listview: connect events
connect(m_lvStrings, TQT_SIGNAL(doubleClicked(TQListViewItem *)), this, TQT_SLOT(slotStringsAdd()));
connect(m_lvStrings_2, TQT_SIGNAL(doubleClicked(TQListViewItem *)), this, TQT_SLOT(slotStringsAdd()));
whatsThis();
}
TQString TDEFileReplaceView::getItemPath(const TQListViewItem *lvi)
{
if (!lvi)
{
return TQString::null;
}
return TQString(lvi->text(1)+"/"+lvi->text(0));
}
TQListViewItem* TDEFileReplaceView::getCurrItemTopLevelParent()
{
TQListViewItem *lvi = m_rv->currentItem();
while (lvi && lvi->parent())
{
lvi = lvi->parent();
}
return lvi;
}
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::displayScannedFiles(int foldersNumber, int filesNumber)
{
m_lcdFoldersNumber->display(TQString::number(foldersNumber,10));
m_lcdFilesNumber->display(TQString::number(filesNumber,10));
}
void TDEFileReplaceView::stringsInvert(bool invertAll)
{
if (m_option->m_searchingOnlyMode)
{
return;
}
TDEListView *sv = getStringsView();
TQListViewItem *lviCurItem;
if (invertAll)
{
lviCurItem = sv->firstChild();
}
else
{
lviCurItem = sv->currentItem();
}
if (!lviCurItem)
{
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));
}
else
{
lviCurItem->setText(0, replaceText);
lviCurItem->setText(1, searchText);
}
lviCurItem = lviCurItem->nextSibling();
} while (invertAll && lviCurItem);
updateStringMap();
emit updateGUI();
}
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::slotResultMouseButtonClicked(int button, TQListViewItem *lvi, const TQPoint &pos)
{
if (lvi == 0) // No item selected
return;
if (button == Qt::RightButton)
{
// Right mouse button: open popup menub
m_menuResult->popup(pos);
}
}
void TDEFileReplaceView::slotResultReturnPressed (TQListViewItem *lvi)
{
if (lvi == 0) // No item selected
return;
// Left mouse button: open item in default view
slotResultOpen();
}
void TDEFileReplaceView::slotResultProperties()
{
TQString currItemPath = getItemPath(getCurrItemTopLevelParent());
if (!currItemPath.isEmpty())
{
KURL url(currItemPath);
(void) new KPropertiesDialog(url);
}
}
void TDEFileReplaceView::slotResultOpen()
{
TQString currItemPath = getItemPath(getCurrItemTopLevelParent());
if (!currItemPath.isEmpty())
{
(void) new KRun(KURL(currItemPath), 0, true, true);
}
}
void TDEFileReplaceView::slotResultOpenWith()
{
TQString currItemPath = getItemPath(getCurrItemTopLevelParent());
if (!currItemPath.isEmpty())
{
KURL::List kurls;
kurls.append(KURL(currItemPath));
KRun::displayOpenWithDialog(kurls);
}
}
void TDEFileReplaceView::slotResultDirOpen()
{
TQString currItemPath = getItemPath(getCurrItemTopLevelParent());
if (!currItemPath.isEmpty())
{
TQFileInfo fi;
fi.setFile(currItemPath);
(void) new KRun (KURL::fromPathOrURL(fi.dirPath()), 0, true, true);
}
}
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();
}
}
void TDEFileReplaceView::slotResultRemoveEntry()
{
TQListViewItem *currItem = getCurrItemTopLevelParent();
if (currItem)
{
delete currItem;
}
}
void TDEFileReplaceView::slotResultDelete()
{
TQListViewItem *currItem = getCurrItemTopLevelParent();
TQString currItemPath = getItemPath(currItem);
if (currItem)
{
TQFile fi;
int answer = KMessageBox::warningContinueCancel(this, i18n("Do you really want to delete %1?").arg(currItemPath),
TQString(),KStdGuiItem::del());
if(answer == KMessageBox::Continue)
{
fi.setName(currItemPath);
fi.remove();
delete currItem;
}
}
}
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::slotResultHeaderClicked(int sortCol)
{
TDEListView *lv = getResultsView();
int oldSortCol = lv->columnSorted();
if (oldSortCol == -1 || sortCol != oldSortCol)
{
lv->setSorting(sortCol, true);
}
else
{
lv->setSorting(sortCol, lv->ascendingSort());
}
}
void TDEFileReplaceView::slotStringsAdd()
{
KAddStringDlg addStringDlg(m_option);
if(!addStringDlg.exec())
{
return;
}
raiseResultsView();
raiseStringsView();
loadMapIntoView(m_option->m_mapStringsView);
emit updateGUI();
}
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::slotStringsSave()
{
// Check there are strings in the list
TDEListView *sv = getStringsView();
if (!sv->firstChild())
{
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>%1</oldstring>"
"\n\t\t<newstring>%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::slotStringsInvertCur()
{
stringsInvert(false);
}
void TDEFileReplaceView::slotStringsInvertAll()
{
stringsInvert(true);
}
void TDEFileReplaceView::slotStringsDeleteItem()
{
TQListViewItem *item = m_sv->currentItem();
if (item)
{
KeyValueMap m = m_option->m_mapStringsView;
m.remove(item->text(0));
m_option->m_mapStringsView = m;
delete item;
emit updateGUI();
}
}
void TDEFileReplaceView::slotStringsEmpty()
{
m_sv->clear();
m_option->m_mapStringsView.clear();
emit updateGUI();
}
//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()));
m_menuResult->insertItem(SmallIconSet(TQString::fromLatin1("document-open")),
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-clear")),
i18n("Remove &Entry"),
this,
TQT_SLOT(slotResultRemoveEntry()));
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::updateStringMap()
{
m_option->m_mapStringsView.clear();
TQListViewItem *item = m_sv->firstChild();
while (item)
{
if (m_option->m_searchingOnlyMode)
{
m_option->m_mapStringsView[item->text(0)] = TQString();
}
else
{
m_option->m_mapStringsView[item->text(0)] = item->text(1);
}
item = item->nextSibling();
}
}
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"