workbenchclientuser.cpp 8.26 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6 7 8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
** Commercial Usage
10
**
11 12 13 14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25 26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
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
#include "workbenchclientuser.h"
#include "perforceoutputwindow.h"
#include "perforceplugin.h"

#include <coreplugin/filemanager.h>
#include <coreplugin/actionmanager/actionmanagerinterface.h>
#include <coreplugin/editormanager/editormanager.h>

#include <QtCore/QEventLoop>
#include <QtCore/QTemporaryFile>
#include <QtGui/QMessageBox>
#include <QtGui/QRadioButton>
#include <QtGui/QMessageBox>
#include <QtGui/QMainWindow>

using namespace Perforce::Internal;

PromptDialog::PromptDialog(const QString &choice, const QString &text,
                           QWidget *parent)
    : QDialog(parent)
{
    m_ui.setupUi(this);
    m_ui.msgLabel->setText(text);

    const QChar closingParenthesis = QLatin1Char(')');
    const QStringList opts = choice.split(QString(closingParenthesis));
    int row = 0;
    int column = 0;
    QString opt;
    QRadioButton *rb = 0;
    for (int i=0; i<opts.count(); ++i) {
        opt = opts.at(i).trimmed();
        if (opt.isEmpty() || opt.startsWith(QLatin1String("Help")))
            continue;
        if (i == opts.count()-1)
            opt = QLatin1String("Default(") + opt.left(opt.length()-1);
        opt.append(QLatin1String(")"));
        rb = new QRadioButton(opt, this);
        rb->setChecked(true);
        if (column>0 && column%3==0)
            ++row;
        m_ui.gridLayout->addWidget(rb, row, column%3, 1, 1);
        ++column;

        const int j = opt.lastIndexOf(QLatin1Char('('));
        opt = opt.mid(j+1, opt.lastIndexOf(closingParenthesis)-j-1);
        m_optionsMap.insert(rb, opt);
    }
}

QString PromptDialog::input() const
{
    QMapIterator<QRadioButton*, QString> it(m_optionsMap);
    while (it.hasNext()) {
        it.next();
        if (it.key()->isChecked())
            return it.value();
    }
    return QString();
}

WorkbenchClientUser::WorkbenchClientUser(PerforceOutputWindow *out, PerforcePlugin *plugin)  :
    QObject(out),
    m_plugin(plugin),
hjk's avatar
hjk committed
94
    m_core(Core::ICore::instance()),
con's avatar
con committed
95 96 97 98 99 100 101
    m_currentEditorIface(0),
    m_userCancelled(false),
    m_mode(Submit),
    m_perforceOutputWindow(out),
    m_skipNextMsg(false),
    m_eventLoop(new QEventLoop(this))
{
hjk's avatar
hjk committed
102
    connect(m_core, SIGNAL(coreAboutToClose()),
con's avatar
con committed
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
        this, SLOT(cancelP4Command()));
}

WorkbenchClientUser::~WorkbenchClientUser()
{
}

void WorkbenchClientUser::setMode(WorkbenchClientUser::Mode mode)
{
    m_mode = mode;
}

void WorkbenchClientUser::cancelP4Command()
{
    m_userCancelled = true;
    m_eventLoop->quit();
}

void WorkbenchClientUser::Message(Error* err)
{
    StrBuf buf;
    err->Fmt(&buf);
    QString s = buf.Text();
    m_perforceOutputWindow->append(s);
    if (!m_skipNextMsg) {
        if (err->GetSeverity() == E_FAILED || err->GetSeverity() == E_FATAL) {
            if (!s.startsWith("Client side operation(s) failed."))
                m_errMsg.append(s);
        } else {
            m_msg.append(s);
        }
    }
    m_skipNextMsg = false;
}

void WorkbenchClientUser::displayErrorMsg(const QString &msg)
{
    if (msg.isEmpty())
        return;

    const QString title = tr("Perforce Error");
    switch (m_mode) {
    case Submit: {
hjk's avatar
hjk committed
146
        QMessageBox msgBox(QMessageBox::Critical, title,  msg, QMessageBox::Ok, m_core->mainWindow());
con's avatar
con committed
147 148 149 150 151
        msgBox.setDetailedText(m_msg);
        msgBox.exec();
    }
        break;
    default:
hjk's avatar
hjk committed
152
        QMessageBox::critical(m_core->mainWindow(), title, msg);
con's avatar
con committed
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
        break;
    }
    m_errMsg.clear();
}

void WorkbenchClientUser::OutputError(const char *errBuf)
{
    QString s(errBuf);
    s = s.trimmed();
    m_perforceOutputWindow->append(s);
    displayErrorMsg(s);
}

void WorkbenchClientUser::Finished()
{
    m_errMsg = m_errMsg.trimmed();
    displayErrorMsg(m_errMsg);
    m_msg.clear();
    m_currentEditorIface = 0;
    m_userCancelled = false;
    m_skipNextMsg = false;
}

bool WorkbenchClientUser::editorAboutToClose(Core::IEditor *editor)
{
    if (editor && editor == m_currentEditorIface) {
        if (m_mode == WorkbenchClientUser::Submit) {
            const QMessageBox::StandardButton answer =
hjk's avatar
hjk committed
181
                QMessageBox::question(m_core->mainWindow(),
con's avatar
con committed
182 183 184 185 186 187 188
                                      tr("Closing p4 Editor"),
                                      tr("Do you want to submit this change list?"),
                                      QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel, QMessageBox::Yes);
            if (answer == QMessageBox::Cancel)
                return false;
            if (answer == QMessageBox::No)
                m_userCancelled = true;
hjk's avatar
hjk committed
189
            m_core->fileManager()->blockFileChange(m_currentEditorIface->file());
con's avatar
con committed
190
            m_currentEditorIface->file()->save();
hjk's avatar
hjk committed
191
            m_core->fileManager()->unblockFileChange(m_currentEditorIface->file());
con's avatar
con committed
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
        }
        m_eventLoop->quit();
        m_currentEditorIface = 0;
    }
    return true;
}

void WorkbenchClientUser::Diff(FileSys *f1, FileSys *f2, int, char *, Error *err)
{
    if (!f1->IsTextual() || !f2->IsTextual())
        return;

    FileSys *file1 = File(FST_BINARY);
    file1->Set(f1->Name());

    FileSys *file2 = File(FST_BINARY);
    file2->Set(f2->Name());

    QTemporaryFile tmp;
    tmp.open();
    QString fileName = tmp.fileName();

    {
        ::Diff d;
        d.SetInput(file1, file2, DiffFlags(), err);
        if (!err->Test())
            d.SetOutput(fileName.toLatin1().constData(), err);
        if (!err->Test())
            d.DiffUnified();
        d.CloseOutput(err);
    }
    delete file1;
    delete file2;

    QString title = QString("diff %1").arg(f1->Name());
hjk's avatar
hjk committed
227
    m_currentEditorIface = m_core->editorManager()->newFile("Perforce Editor", &title, tmp.readAll());
con's avatar
con committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    if (!m_currentEditorIface) {
        err->Set(E_FAILED, "p4 data could not be opened!");
        return;
    }
    m_userCancelled = false;
    m_eventLoop->exec();
    if (m_userCancelled)
        err->Set(E_FAILED, "");
}

void WorkbenchClientUser::Edit(FileSys *f, Error *err)
{
    QString fileName(f->Name());
    if (m_mode == Submit) {
        m_currentEditorIface = m_plugin->openPerforceSubmitEditor(fileName, QStringList());
    }
    else {
hjk's avatar
hjk committed
245 246
        m_currentEditorIface = m_core->editorManager()->openEditor(fileName);
        m_core->editorManager()->ensureEditorManagerVisible();
con's avatar
con committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    }
    if (!m_currentEditorIface) {
        err->Set(E_FAILED, "p4 data could not be opened!");
        return;
    }
    m_userCancelled = false;
    m_eventLoop->exec();
    if (m_userCancelled)
        err->Set(E_FAILED, "");
}

void WorkbenchClientUser::Prompt(const StrPtr &msg, StrBuf &answer, int , Error *err)
{
    if (m_userCancelled) {
        err->Set(E_FATAL, "");
        return;
    }
hjk's avatar
hjk committed
264
    PromptDialog dia(msg.Text(), m_msg, qobject_cast<QWidget*>(m_core));
con's avatar
con committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    dia.exec();
    answer = qstrdup(dia.input().toLatin1().constData());
    if (m_mode == WorkbenchClientUser::Resolve) {
        if (strcmp(answer.Text(), "e") == 0) {
            ;
        } else if (strcmp(answer.Text(), "d") == 0) {
            ;
        } else {
            m_msg.clear();
            m_skipNextMsg = true;
        }
    }
}

void WorkbenchClientUser::ErrorPause(char *msg, Error *)
{
hjk's avatar
hjk committed
281
    QMessageBox::warning(m_core->mainWindow(), tr("Perforce Error"), QString::fromUtf8(msg));
con's avatar
con committed
282
}