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
|
/*
* Copyright (C) 1999-2002 Bernd Gehrmann
* bernd@mail.berlios.de
* Copyright (c) 2003-2007 Christian Loose <christian.loose@kdemail.net>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "diffdialog.h"
#include <KComboBox>
#include <qcheckbox.h>
#include <qfileinfo.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qnamespace.h>
#include <qpushbutton.h>
#include <qregexp.h>
#include <QBoxLayout>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QKeyEvent>
#include <QTextStream>
#include <QVBoxLayout>
#include <KLocalizedString>
#include <kconfig.h>
#include <kmessagebox.h>
#include "cvsserviceinterface.h"
#include "diffview.h"
#include "misc.h"
#include "progressdialog.h"
#include <KConfigGroup>
#include <KGuiItem>
#include <KHelpClient>
#include <QDialogButtonBox>
#include <QFileDialog>
#include <QPushButton>
#include <kconfiggroup.h>
#include <kprocess.h>
#include <kshell.h>
DiffDialog::DiffDialog(KConfig &cfg, QWidget *parent, bool modal)
: QDialog(parent)
, partConfig(cfg)
{
markeditem = -1;
setModal(modal);
auto mainLayout = new QVBoxLayout;
setLayout(mainLayout);
auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Help | QDialogButtonBox::Close);
connect(buttonBox, &QDialogButtonBox::helpRequested, this, &DiffDialog::slotHelp);
auto user1Button = new QPushButton;
buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
KGuiItem::assign(user1Button, KStandardGuiItem::saveAs());
auto pairlayout = new QGridLayout();
mainLayout->addLayout(pairlayout);
pairlayout->setRowStretch(0, 0);
pairlayout->setRowStretch(1, 1);
pairlayout->setColumnStretch(1, 0);
pairlayout->addItem(new QSpacerItem(16, 0), 0, 1);
pairlayout->setColumnStretch(0, 10);
pairlayout->setColumnStretch(2, 10);
revlabel1 = new QLabel;
pairlayout->addWidget(revlabel1, 0, 0);
revlabel2 = new QLabel;
pairlayout->addWidget(revlabel2, 0, 2);
diff1 = new DiffView(cfg, true, false, this);
diff2 = new DiffView(cfg, true, true, this);
auto zoom = new DiffZoomWidget(this);
zoom->setDiffView(diff2);
pairlayout->addWidget(diff1, 1, 0);
pairlayout->addWidget(zoom, 1, 1);
pairlayout->addWidget(diff2, 1, 2);
diff1->setPartner(diff2);
diff2->setPartner(diff1);
syncbox = new QCheckBox(i18n("Synchronize scroll bars"));
syncbox->setChecked(true);
connect(syncbox, SIGNAL(toggled(bool)), this, SLOT(toggleSynchronize(bool)));
itemscombo = new KComboBox;
itemscombo->addItem(QString());
connect(itemscombo, SIGNAL(activated(int)), this, SLOT(comboActivated(int)));
nofnlabel = new QLabel;
// avoids auto resize when the text is changed
nofnlabel->setMinimumWidth(fontMetrics().width(i18np("%1 difference", "%1 differences", 10000)));
backbutton = new QPushButton(QLatin1String("&<<"));
connect(backbutton, SIGNAL(clicked()), SLOT(backClicked()));
forwbutton = new QPushButton(QLatin1String("&>>"));
connect(forwbutton, SIGNAL(clicked()), SLOT(forwClicked()));
connect(user1Button, SIGNAL(clicked()), SLOT(saveAsClicked()));
QBoxLayout *buttonlayout = new QHBoxLayout();
mainLayout->addLayout(buttonlayout);
buttonlayout->addWidget(syncbox, 0);
buttonlayout->addStretch(4);
buttonlayout->addWidget(itemscombo);
buttonlayout->addStretch(1);
buttonlayout->addWidget(nofnlabel);
buttonlayout->addStretch(1);
buttonlayout->addWidget(backbutton);
buttonlayout->addWidget(forwbutton);
mainLayout->addWidget(buttonBox);
buttonBox->button(QDialogButtonBox::Close)->setDefault(true);
setAttribute(Qt::WA_DeleteOnClose, true);
KConfigGroup cg(&partConfig, "DiffDialog");
syncbox->setChecked(cg.readEntry("Sync", false));
restoreGeometry(cg.readEntry<QByteArray>("geometry", QByteArray()));
}
DiffDialog::~DiffDialog()
{
KConfigGroup cg(&partConfig, "DiffDialog");
cg.writeEntry("Sync", syncbox->isChecked());
cg.writeEntry("geometry", saveGeometry());
qDeleteAll(items);
}
void DiffDialog::slotHelp()
{
KHelpClient::invokeHelp(QLatin1String("diff"));
}
void DiffDialog::keyPressEvent(QKeyEvent *e)
{
switch (e->key()) {
case Qt::Key_Up:
diff1->up();
diff2->up();
break;
case Qt::Key_Down:
diff1->down();
diff2->down();
break;
case Qt::Key_PageDown:
diff1->next();
diff2->next();
break;
case Qt::Key_PageUp:
diff1->prior();
diff2->prior();
break;
default:
QDialog::keyPressEvent(e);
}
}
void DiffDialog::toggleSynchronize(bool b)
{
diff1->setPartner(b ? diff2 : 0);
diff2->setPartner(b ? diff1 : 0);
}
void DiffDialog::comboActivated(int index)
{
updateHighlight(index - 1);
}
static void interpretRegion(QString line, int *linenoA, int *linenoB)
{
QRegExp region("^@@ -([0-9]+),([0-9]+) \\+([0-9]+),([0-9]+) @@.*$");
if (!region.exactMatch(line))
return;
*linenoA = region.cap(1).toInt() - 1;
*linenoB = region.cap(3).toInt() - 1;
}
static QString regionAsString(int linenoA, int linecountA, int linenoB, int linecountB)
{
int lineendA = linenoA + linecountA - 1;
int lineendB = linenoB + linecountB - 1;
QString res;
if (linecountB == 0)
res = QString("%1,%2d%3").arg(linenoA).arg(lineendA).arg(linenoB - 1);
else if (linecountA == 0)
res = QString("%1a%2,%3").arg(linenoA - 1).arg(linenoB).arg(lineendB);
else if (linenoA == lineendA)
if (linenoB == lineendB)
res = QString("%1c%2").arg(linenoA).arg(linenoB);
else
res = QString("%1c%2,%3").arg(linenoA).arg(linenoB).arg(lineendB);
else if (linenoB == lineendB)
res = QString("%1,%2c%3").arg(linenoA).arg(lineendA).arg(linenoB);
else
res = QString("%1,%2c%3,%4").arg(linenoA).arg(lineendA).arg(linenoB).arg(lineendB);
return res;
}
class DiffItem
{
public:
DiffView::DiffType type;
int linenoA, linecountA;
int linenoB, linecountB;
};
bool DiffDialog::parseCvsDiff(OrgKdeCervisia5CvsserviceCvsserviceInterface *service, const QString &fileName, const QString &revA, const QString &revB)
{
QStringList linesA, linesB;
int linenoA, linenoB;
setWindowTitle(i18n("CVS Diff: %1", fileName));
revlabel1->setText(revA.isEmpty() ? i18n("Repository:") : i18n("Revision ") + revA + ':');
revlabel2->setText(revB.isEmpty() ? i18n("Working dir:") : i18n("Revision ") + revB + ':');
KConfigGroup cs(&partConfig, "General");
// Ok, this is a hack: When the user wants an external diff
// front end, it is executed from here. Of course, in that
// case this dialog wouldn't have to be created in the first
// place, but this design at least makes the handling trans-
// parent for the calling routines
QString extdiff = cs.readPathEntry("ExternalDiff", QString());
if (!extdiff.isEmpty()) {
callExternalDiff(extdiff, service, fileName, revA, revB);
return false;
}
const QString diffOptions = cs.readEntry("DiffOptions");
const unsigned contextLines = cs.readEntry("ContextLines", 65535);
QDBusReply<QDBusObjectPath> job = service->diff(fileName, revA, revB, diffOptions, contextLines);
if (!job.isValid())
return false;
ProgressDialog dlg(this, "Diff", service->service(), job, "diff", i18n("CVS Diff"));
if (!dlg.execute())
return false;
// remember diff output for "save as" action
m_diffOutput = dlg.getOutput();
QString line;
while (dlg.getLine(line) && !line.startsWith("+++"))
;
linenoA = linenoB = 0;
while (dlg.getLine(line)) {
// line contains diff region?
if (line.startsWith(QLatin1String("@@"))) {
interpretRegion(line, &linenoA, &linenoB);
diff1->addLine(line, DiffView::Separator);
diff2->addLine(line, DiffView::Separator);
continue;
}
if (line.length() < 1)
continue;
QChar marker = line[0];
line.remove(0, 1);
if (marker == '-')
linesA.append(line);
else if (marker == '+')
linesB.append(line);
else {
if (!linesA.isEmpty() || !linesB.isEmpty()) {
newDiffHunk(linenoA, linenoB, linesA, linesB);
linesA.clear();
linesB.clear();
}
diff1->addLine(line, DiffView::Unchanged, ++linenoA);
diff2->addLine(line, DiffView::Unchanged, ++linenoB);
}
}
if (!linesA.isEmpty() || !linesB.isEmpty())
newDiffHunk(linenoA, linenoB, linesA, linesB);
// sets the right size as there is no more auto resize in QComboBox
itemscombo->adjustSize();
updateNofN();
return true;
}
void DiffDialog::newDiffHunk(int &linenoA, int &linenoB, const QStringList &linesA, const QStringList &linesB)
{
auto item = new DiffItem;
item->linenoA = linenoA + 1;
item->linenoB = linenoB + 1;
item->linecountA = linesA.count();
item->linecountB = linesB.count();
items.append(item);
const QString region = regionAsString(linenoA + 1, linesA.count(), linenoB + 1, linesB.count());
itemscombo->addItem(region);
QStringList::ConstIterator itA = linesA.begin();
QStringList::ConstIterator itB = linesB.begin();
while (itA != linesA.end() || itB != linesB.end()) {
if (itA != linesA.end()) {
diff1->addLine(*itA, DiffView::Neutral, ++linenoA);
if (itB != linesB.end())
diff2->addLine(*itB, DiffView::Change, ++linenoB);
else
diff2->addLine("", DiffView::Delete);
} else {
diff1->addLine("", DiffView::Neutral);
diff2->addLine(*itB, DiffView::Insert, ++linenoB);
}
if (itA != linesA.end())
++itA;
if (itB != linesB.end())
++itB;
}
}
void DiffDialog::callExternalDiff(const QString &extdiff,
OrgKdeCervisia5CvsserviceCvsserviceInterface *service,
const QString &fileName,
const QString &revA,
const QString &revB)
{
QString extcmdline = extdiff;
extcmdline += ' ';
// create suffix for temporary file (used QFileInfo to remove path from file name)
const QString suffix = '-' + QFileInfo(fileName).fileName();
QDBusReply<QDBusObjectPath> job;
if (!revA.isEmpty() && !revB.isEmpty()) {
// We're comparing two revisions
QString revAFilename = tempFileName(suffix + QString("-") + revA);
QString revBFilename = tempFileName(suffix + QString("-") + revB);
// download the files for revision A and B
job = service->downloadRevision(fileName, revA, revAFilename, revB, revBFilename);
if (!job.isValid())
return;
extcmdline += KShell::quoteArg(revAFilename);
extcmdline += ' ';
extcmdline += KShell::quoteArg(revBFilename);
} else {
// We're comparing to a file, and perhaps one revision
QString revAFilename = tempFileName(suffix + QString("-") + revA);
job = service->downloadRevision(fileName, revA, revAFilename);
if (!job.isValid())
return;
extcmdline += KShell::quoteArg(revAFilename);
extcmdline += ' ';
extcmdline += KShell::quoteArg(QFileInfo(fileName).absoluteFilePath());
}
ProgressDialog dlg(this, "Diff", service->service(), job, "diff");
if (dlg.execute()) {
// call external diff application
KProcess proc;
proc.setShellCommand(extcmdline);
proc.startDetached();
}
}
void DiffDialog::updateNofN()
{
QString str;
if (markeditem >= 0)
str = i18n("%1 of %2", markeditem + 1, items.count());
else
str = i18np("%1 difference", "%1 differences", items.count());
nofnlabel->setText(str);
itemscombo->setCurrentIndex(markeditem == -2 ? 0 : markeditem + 1);
backbutton->setEnabled(markeditem != -1);
forwbutton->setEnabled(markeditem != -2 && items.count());
}
void DiffDialog::updateHighlight(int newitem)
{
if (markeditem >= 0) {
DiffItem *item = items.at(markeditem);
for (int i = item->linenoA; i < item->linenoA + item->linecountA; ++i)
diff1->setInverted(i, false);
for (int i = item->linenoB; i < item->linenoB + item->linecountB; ++i)
diff2->setInverted(i, false);
}
markeditem = newitem;
if (markeditem >= 0) {
DiffItem *item = items.at(markeditem);
for (int i = item->linenoA; i < item->linenoA + item->linecountA; ++i)
diff1->setInverted(i, true);
for (int i = item->linenoB; i < item->linenoB + item->linecountB; ++i)
diff2->setInverted(i, true);
diff1->setCenterLine(item->linenoA);
diff2->setCenterLine(item->linenoB);
}
diff1->repaint();
diff2->repaint();
updateNofN();
}
void DiffDialog::backClicked()
{
int newitem;
if (markeditem == -1)
return; // internal error (button not disabled)
else if (markeditem == -2) // past end
newitem = items.count() - 1;
else
newitem = markeditem - 1;
updateHighlight(newitem);
}
void DiffDialog::forwClicked()
{
int newitem;
if (markeditem == -2 || (markeditem == -1 && !items.count()))
return; // internal error (button not disabled)
else if (markeditem + 1 == items.count()) // past end
newitem = -2;
else
newitem = markeditem + 1;
updateHighlight(newitem);
}
void DiffDialog::saveAsClicked()
{
QString fileName = QFileDialog::getSaveFileName(this, QString(), QString(), QString());
if (fileName.isEmpty())
return;
if (!Cervisia::CheckOverwrite(fileName, this))
return;
QFile f(fileName);
if (!f.open(QIODevice::WriteOnly)) {
KMessageBox::error(this, i18n("Could not open file for writing."), "Cervisia");
return;
}
QTextStream ts(&f);
QStringList::const_iterator it = m_diffOutput.constBegin();
for (; it != m_diffOutput.constEnd(); ++it)
ts << *it << "\n";
f.close();
}
// Local Variables:
// c-basic-offset: 4
// End:
|