Changeset View
Changeset View
Standalone View
Standalone View
git/fileviewgitplugin.cpp
Show All 33 Lines | |||||
34 | #include <KPluginFactory> | 34 | #include <KPluginFactory> | ||
35 | 35 | | |||
36 | #include <QTemporaryFile> | 36 | #include <QTemporaryFile> | ||
37 | #include <QProcess> | 37 | #include <QProcess> | ||
38 | #include <QString> | 38 | #include <QString> | ||
39 | #include <QStringList> | 39 | #include <QStringList> | ||
40 | #include <QTextCodec> | 40 | #include <QTextCodec> | ||
41 | #include <QDir> | 41 | #include <QDir> | ||
42 | #include <QTextBrowser> | ||||
42 | 43 | | |||
43 | K_PLUGIN_FACTORY(FileViewGitPluginFactory, registerPlugin<FileViewGitPlugin>();) | 44 | K_PLUGIN_FACTORY(FileViewGitPluginFactory, registerPlugin<FileViewGitPlugin>();) | ||
44 | 45 | | |||
45 | FileViewGitPlugin::FileViewGitPlugin(QObject* parent, const QList<QVariant>& args) : | 46 | FileViewGitPlugin::FileViewGitPlugin(QObject* parent, const QList<QVariant>& args) : | ||
46 | KVersionControlPlugin(parent), | 47 | KVersionControlPlugin(parent), | ||
47 | m_pendingOperation(false), | 48 | m_pendingOperation(false), | ||
48 | m_addAction(0), | 49 | m_addAction(0), | ||
49 | m_removeAction(0), | 50 | m_removeAction(0), | ||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Line(s) | 101 | // m_pushAction->setIcon(QIcon::fromTheme("svn-commit")); | |||
101 | m_pushAction->setText(xi18nd("@action:inmenu", "<application>Git</application> Push...")); | 102 | m_pushAction->setText(xi18nd("@action:inmenu", "<application>Git</application> Push...")); | ||
102 | connect(m_pushAction, SIGNAL(triggered()), | 103 | connect(m_pushAction, SIGNAL(triggered()), | ||
103 | this, SLOT(push())); | 104 | this, SLOT(push())); | ||
104 | m_pullAction = new QAction(this); | 105 | m_pullAction = new QAction(this); | ||
105 | m_pullAction->setText(xi18nd("@action:inmenu", "<application>Git</application> Pull...")); | 106 | m_pullAction->setText(xi18nd("@action:inmenu", "<application>Git</application> Pull...")); | ||
106 | connect(m_pullAction, SIGNAL(triggered()), | 107 | connect(m_pullAction, SIGNAL(triggered()), | ||
107 | this, SLOT(pull())); | 108 | this, SLOT(pull())); | ||
108 | 109 | | |||
110 | m_logAction = new QAction(this); | ||||
111 | m_logAction->setText(xi18nd("@action:inmenu", "<application>Git</application> Log...")); | ||||
112 | connect(m_logAction, &QAction::triggered, this, &FileViewGitPlugin::log); | ||||
113 | | ||||
109 | connect(&m_process, SIGNAL(finished(int, QProcess::ExitStatus)), | 114 | connect(&m_process, SIGNAL(finished(int, QProcess::ExitStatus)), | ||
110 | this, SLOT(slotOperationCompleted(int, QProcess::ExitStatus))); | 115 | this, SLOT(slotOperationCompleted(int, QProcess::ExitStatus))); | ||
111 | connect(&m_process, SIGNAL(error(QProcess::ProcessError)), | 116 | connect(&m_process, SIGNAL(error(QProcess::ProcessError)), | ||
112 | this, SLOT(slotOperationError())); | 117 | this, SLOT(slotOperationError())); | ||
113 | } | 118 | } | ||
114 | 119 | | |||
115 | FileViewGitPlugin::~FileViewGitPlugin() | 120 | FileViewGitPlugin::~FileViewGitPlugin() | ||
116 | { | 121 | { | ||
▲ Show 20 Lines • Show All 235 Lines • ▼ Show 20 Line(s) | 349 | while (it != m_versionInfoHash.constEnd()) { | |||
352 | if (state == ConflictingVersion) { | 357 | if (state == ConflictingVersion) { | ||
353 | canCommit = false; | 358 | canCommit = false; | ||
354 | showChanges = true; | 359 | showChanges = true; | ||
355 | break; | 360 | break; | ||
356 | } | 361 | } | ||
357 | ++it; | 362 | ++it; | ||
358 | } | 363 | } | ||
359 | 364 | | |||
365 | m_logAction->setEnabled(!m_pendingOperation); | ||||
366 | actions.append(m_logAction); | ||||
367 | | ||||
360 | m_showLocalChangesAction->setEnabled(!m_pendingOperation && showChanges); | 368 | m_showLocalChangesAction->setEnabled(!m_pendingOperation && showChanges); | ||
361 | actions.append(m_showLocalChangesAction); | 369 | actions.append(m_showLocalChangesAction); | ||
362 | 370 | | |||
363 | m_commitAction->setEnabled(!m_pendingOperation && canCommit); | 371 | m_commitAction->setEnabled(!m_pendingOperation && canCommit); | ||
364 | actions.append(m_commitAction); | 372 | actions.append(m_commitAction); | ||
365 | 373 | | |||
366 | m_tagAction->setEnabled(!m_pendingOperation); | 374 | m_tagAction->setEnabled(!m_pendingOperation); | ||
367 | actions.append(m_tagAction); | 375 | actions.append(m_tagAction); | ||
Show All 34 Lines | |||||
402 | 410 | | |||
403 | void FileViewGitPlugin::showLocalChanges() | 411 | void FileViewGitPlugin::showLocalChanges() | ||
404 | { | 412 | { | ||
405 | Q_ASSERT(!m_contextDir.isEmpty()); | 413 | Q_ASSERT(!m_contextDir.isEmpty()); | ||
406 | 414 | | |||
407 | KRun::runCommand(QLatin1String("git difftool --dir-diff ."), nullptr, m_contextDir); | 415 | KRun::runCommand(QLatin1String("git difftool --dir-diff ."), nullptr, m_contextDir); | ||
408 | } | 416 | } | ||
409 | 417 | | |||
418 | void FileViewGitPlugin::showDiff(const QUrl &link) | ||||
419 | { | ||||
420 | if (link.scheme() != QLatin1String("rev")) { | ||||
elvisangelaccio: `QLatin1String` here (faster with != operator) | |||||
421 | return; | ||||
elvisangelaccio: `QLatin1Starting` here (because `startsWith()` has an overload for it). | |||||
Actually, maybe we can just check if the scheme() of the url is "rev". elvisangelaccio: Actually, maybe we can just check if the `scheme()` of the url is "rev". | |||||
422 | } | ||||
423 | KRun::runCommand(QStringLiteral("git difftool --dir-diff %1^ %1").arg(link.path()), nullptr, m_contextDir); | ||||
424 | } | ||||
425 | | ||||
426 | void FileViewGitPlugin::log() | ||||
427 | { | ||||
428 | QProcess process; | ||||
429 | process.setWorkingDirectory(m_contextDir); | ||||
430 | process.start( | ||||
431 | QLatin1String("git"), | ||||
432 | QStringList { | ||||
433 | QStringLiteral("log"), | ||||
434 | QStringLiteral("--relative=."), | ||||
435 | QStringLiteral("--date=format:%d-%m-%Y"), | ||||
Why not using the new C++11 syntax: QStringList {QStringLiteral("foo"), QStringLiteral("bar"), ...} elvisangelaccio: Why not using the new C++11 syntax:
```
QStringList {QStringLiteral("foo"), QStringLiteral… | |||||
anthonyfieroni: It does not matter in that case, about me. | |||||
436 | QStringLiteral("-n 100"), | ||||
437 | QStringLiteral("--pretty=format:<tr> <td><a href=\"rev:%h\">%h</a></td> <td>%ad</td> <td>%s</td> <td>%an</td> </tr>") | ||||
438 | } | ||||
439 | ); | ||||
440 | | ||||
441 | if (!process.waitForFinished() || process.exitCode() != 0) { | ||||
442 | emit errorMessage(xi18nd("@info:status", "<application>Git</application> Log failed.")); | ||||
443 | return; | ||||
444 | } | ||||
445 | | ||||
446 | const QString gitOutput = process.readAllStandardOutput(); | ||||
447 | | ||||
448 | QPalette palette; | ||||
449 | const QString styleSheet = QStringLiteral( | ||||
450 | "body { background: %1; color: %2; }" \ | ||||
451 | "table.logtable td { padding: 9px 8px 9px; }" \ | ||||
452 | "a { color: %3; }" \ | ||||
453 | "a:visited { color: %4; } " | ||||
454 | ).arg(palette.background().color().name(), | ||||
455 | palette.text().color().name(), | ||||
456 | palette.link().color().name(), | ||||
457 | palette.linkVisited().color().name()); | ||||
458 | | ||||
Hmm this should probably not be translated. If you are using git, you know what "git log" means ;) elvisangelaccio: Hmm this should probably not be translated. If you are using git, you know what "git log" means… | |||||
I'll update to xi18nd("@action:intitle", "<application>Git</application> Log") but should check if intitle presents :) anthonyfieroni: I'll update to
```
xi18nd("@action:intitle", "<application>Git</application> Log")
```
but… | |||||
459 | auto view = new QTextBrowser(); | ||||
460 | view->setAttribute(Qt::WA_DeleteOnClose); | ||||
461 | view->setWindowTitle(xi18nd("@title:window", "<application>Git</application> Log")); | ||||
462 | view->setOpenLinks(false); | ||||
463 | view->setOpenExternalLinks(false); | ||||
464 | connect(view, &QTextBrowser::anchorClicked, this, &FileViewGitPlugin::showDiff); | ||||
465 | view->setHtml(QStringLiteral( | ||||
466 | "<html>" \ | ||||
467 | "<style> %1 </style>" \ | ||||
468 | "<table class=\"logtable\">" \ | ||||
469 | "<tr bgcolor=\"%2\">" \ | ||||
470 | "<td> %3 </td> <td> %4 </td> <td> %5 </p> </td> <td> %6 </td>" \ | ||||
471 | "</tr>" \ | ||||
472 | "%7" \ | ||||
473 | "</table>" \ | ||||
474 | "</html>" | ||||
475 | ).arg(styleSheet, | ||||
476 | palette.highlight().color().name(), | ||||
477 | i18nc("Git commit hash", "Commit"), | ||||
478 | i18nc("Git commit date", "Date"), | ||||
479 | i18nc("Git commit message", "Message"), | ||||
480 | i18nc("Git commit author", "Author"), | ||||
481 | gitOutput)); | ||||
482 | | ||||
483 | view->resize(QSize(720, 560)); | ||||
484 | view->show(); | ||||
485 | } | ||||
486 | | ||||
410 | void FileViewGitPlugin::checkout() | 487 | void FileViewGitPlugin::checkout() | ||
411 | { | 488 | { | ||
412 | CheckoutDialog dialog; | 489 | CheckoutDialog dialog; | ||
413 | if (dialog.exec() == QDialog::Accepted){ | 490 | if (dialog.exec() == QDialog::Accepted){ | ||
414 | QProcess process; | 491 | QProcess process; | ||
415 | process.setWorkingDirectory(m_contextDir); | 492 | process.setWorkingDirectory(m_contextDir); | ||
416 | QStringList arguments; | 493 | QStringList arguments; | ||
417 | arguments << "checkout"; | 494 | arguments << "checkout"; | ||
▲ Show 20 Lines • Show All 260 Lines • Show Last 20 Lines |
QLatin1String here (faster with != operator)