Warning, file /sdk/cervisia/protocolview.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  Copyright (C) 1999-2002 Bernd Gehrmann
0003  *                          bernd@physik.hu-berlin.de
0004  *  Copyright (c) 2003-2007 Christian Loose <christian.loose@kdemail.net>
0005  *
0006  * This program is free software; you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License as published by
0008  * the Free Software Foundation; either version 2 of the License, or
0009  * (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program; if not, write to the Free Software
0018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0019  */
0020 
0021 #include "protocolview.h"
0022 #include "protocolviewadaptor.h"
0023 
0024 #include <QAction>
0025 #include <QContextMenuEvent>
0026 #include <QMenu>
0027 
0028 #include <KLocalizedString>
0029 #include <kmessagebox.h>
0030 
0031 #include "cervisiapart.h"
0032 #include "cervisiasettings.h"
0033 #include "cvsjobinterface.h"
0034 #include "debug.h"
0035 
0036 ProtocolView::ProtocolView(const QString &appId, QWidget *parent)
0037     : QTextEdit(parent)
0038     , job(0)
0039     , m_isUpdateJob(false)
0040 {
0041     new ProtocolviewAdaptor(this);
0042     QDBusConnection::sessionBus().registerObject("/ProtocolView", this);
0043 
0044     setReadOnly(true);
0045     setUndoRedoEnabled(false);
0046     setTabChangesFocus(true);
0047 
0048     // qCDebug(log_cervisia) << "protocol view appId :" << appId;
0049 
0050     job = new OrgKdeCervisia5CvsserviceCvsjobInterface(appId, "/NonConcurrentJob", QDBusConnection::sessionBus(), this);
0051 
0052     QDBusConnection::sessionBus()
0053         .connect(QString(), "/NonConcurrentJob", "org.kde.cervisia5.cvsservice.cvsjob", "jobExited", this, SLOT(slotJobExited(bool, int)));
0054     QDBusConnection::sessionBus()
0055         .connect(QString(), "/NonConcurrentJob", "org.kde.cervisia5.cvsservice.cvsjob", "receivedStdout", this, SLOT(slotReceivedOutput(QString)));
0056     QDBusConnection::sessionBus()
0057         .connect(QString(), "/NonConcurrentJob", "org.kde.cervisia5.cvsservice.cvsjob", "receivedStderr", this, SLOT(slotReceivedOutput(QString)));
0058 
0059     configChanged();
0060 
0061     connect(CervisiaSettings::self(), SIGNAL(configChanged()), this, SLOT(configChanged()));
0062 }
0063 
0064 ProtocolView::~ProtocolView()
0065 {
0066     delete job;
0067 }
0068 
0069 bool ProtocolView::startJob(bool isUpdateJob)
0070 {
0071     m_isUpdateJob = isUpdateJob;
0072 
0073     // get command line and add it to output buffer
0074     QString cmdLine = job->cvsCommand();
0075     buf += cmdLine;
0076     buf += '\n';
0077     processOutput();
0078 
0079     // disconnect 3rd party slots from our signals
0080     disconnect(SIGNAL(receivedLine(QString)));
0081     disconnect(SIGNAL(jobFinished(bool, int)));
0082 
0083     return job->execute();
0084 }
0085 
0086 void ProtocolView::contextMenuEvent(QContextMenuEvent *event)
0087 {
0088     QMenu *menu = QTextEdit::createStandardContextMenu();
0089 
0090     QAction *clearAction = menu->addAction(i18n("Clear"), this, SLOT(clear()));
0091 
0092     if (document()->isEmpty())
0093         clearAction->setEnabled(false);
0094 
0095     menu->exec(event->globalPos());
0096     delete menu;
0097 }
0098 
0099 void ProtocolView::cancelJob()
0100 {
0101     qCDebug(log_cervisia);
0102     job->cancel();
0103 }
0104 
0105 void ProtocolView::configChanged()
0106 {
0107     conflictColor = CervisiaSettings::conflictColor();
0108     localChangeColor = CervisiaSettings::localChangeColor();
0109     remoteChangeColor = CervisiaSettings::remoteChangeColor();
0110 
0111     setFont(CervisiaSettings::protocolFont());
0112 }
0113 
0114 void ProtocolView::slotReceivedOutput(QString buffer)
0115 {
0116     buf += buffer;
0117     processOutput();
0118 }
0119 
0120 void ProtocolView::slotJobExited(bool normalExit, int exitStatus)
0121 {
0122     qCDebug(log_cervisia);
0123     QString msg;
0124 
0125     if (normalExit) {
0126         if (exitStatus)
0127             msg = i18n("[Exited with status %1]\n", exitStatus);
0128         else
0129             msg = i18n("[Finished]\n");
0130     } else
0131         msg = i18n("[Aborted]\n");
0132 
0133     buf += '\n';
0134     buf += msg;
0135     processOutput();
0136 
0137     emit jobFinished(normalExit, exitStatus);
0138 }
0139 
0140 void ProtocolView::processOutput()
0141 {
0142     int pos;
0143     while ((pos = buf.indexOf('\n')) != -1) {
0144         QString line = buf.left(pos);
0145         if (!line.isEmpty()) {
0146             appendLine(line);
0147             emit receivedLine(line);
0148         }
0149         buf = buf.right(buf.length() - pos - 1);
0150     }
0151 }
0152 
0153 void ProtocolView::appendLine(const QString &line)
0154 {
0155     // Escape output line, so that html tags in commit
0156     // messages aren't interpreted
0157     const QString escapedLine = line.toHtmlEscaped();
0158 
0159     // When we don't get the output from an update job then
0160     // just add it to the text edit.
0161     if (!m_isUpdateJob) {
0162         appendHtml(escapedLine);
0163         return;
0164     }
0165 
0166     QColor color;
0167     // Colors are the same as in UpdateViewItem::paintCell()
0168     if (line.startsWith(QLatin1String("C ")))
0169         color = conflictColor;
0170     else if (line.startsWith(QLatin1String("M ")) || line.startsWith(QLatin1String("A ")) || line.startsWith(QLatin1String("R ")))
0171         color = localChangeColor;
0172     else if (line.startsWith(QLatin1String("P ")) || line.startsWith(QLatin1String("U ")))
0173         color = remoteChangeColor;
0174 
0175     appendHtml(color.isValid() ? QString("<font color=\"%1\"><b>%2</b></font>").arg(color.name()).arg(escapedLine) : escapedLine);
0176 }
0177 
0178 void ProtocolView::appendHtml(const QString &html)
0179 {
0180     QTextCursor cursor(textCursor());
0181     cursor.insertHtml(html);
0182     cursor.insertBlock();
0183     ensureCursorVisible();
0184 }
0185 
0186 // Local Variables:
0187 // c-basic-offset: 4
0188 // End: