aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/perforce/workbenchclientuser.cpp
blob: 64dd3085eb5fa8efb6a0830e52a976a2cf81b5aa (plain)
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
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/

#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),
    m_core(Core::ICore::instance()),
    m_currentEditorIface(0),
    m_userCancelled(false),
    m_mode(Submit),
    m_perforceOutputWindow(out),
    m_skipNextMsg(false),
    m_eventLoop(new QEventLoop(this))
{
    connect(m_core, SIGNAL(coreAboutToClose()),
        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: {
        QMessageBox msgBox(QMessageBox::Critical, title,  msg, QMessageBox::Ok, m_core->mainWindow());
        msgBox.setDetailedText(m_msg);
        msgBox.exec();
    }
        break;
    default:
        QMessageBox::critical(m_core->mainWindow(), title, msg);
        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 =
                QMessageBox::question(m_core->mainWindow(),
                                      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;
            m_core->fileManager()->blockFileChange(m_currentEditorIface->file());
            m_currentEditorIface->file()->save();
            m_core->fileManager()->unblockFileChange(m_currentEditorIface->file());
        }
        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());
    m_currentEditorIface = m_core->editorManager()->newFile("Perforce Editor", &title, tmp.readAll());
    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 {
        m_currentEditorIface = m_core->editorManager()->openEditor(fileName);
        m_core->editorManager()->ensureEditorManagerVisible();
    }
    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;
    }
    PromptDialog dia(msg.Text(), m_msg, qobject_cast<QWidget*>(m_core));
    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 *)
{
    QMessageBox::warning(m_core->mainWindow(), tr("Perforce Error"), QString::fromUtf8(msg));
}