aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/clearcase/versionselector.cpp
blob: 2c0d46a6a30dfc80b8408ed757f342262cece389 (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
// Copyright (C) 2016 AudioCodes Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "versionselector.h"

#include "clearcasetr.h"

#include <utils/layoutbuilder.h>

#include <QDialogButtonBox>
#include <QLabel>
#include <QPlainTextEdit>
#include <QRadioButton>
#include <QRegularExpression>
#include <QTextEdit>
#include <QTextStream>

namespace ClearCase::Internal {

VersionSelector::VersionSelector(const QString &fileName, const QString &message, QWidget *parent) :
    QDialog(parent)
{
    resize(413, 435);
    setWindowTitle(Tr::tr("Confirm Version to Check Out"));

    auto headerLabel = new QLabel(Tr::tr("Multiple versions of \"%1\" can be checked out. "
                                     "Select the version to check out:").arg(fileName));
    headerLabel->setWordWrap(true);
    headerLabel->setTextInteractionFlags(Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse);

    auto loadedRadioButton = new QRadioButton(Tr::tr("&Loaded version"));
    loadedRadioButton->setChecked(true);

    auto loadedLabel = new QLabel;
    auto loadedCreatedByLabel = new QLabel;
    auto loadedCreatedOnLabel = new QLabel;
    auto updatedLabel = new QLabel;
    auto updatedCreatedByLabel = new QLabel;
    auto updatedCreatedOnLabel = new QLabel;

    auto updatedText = new QPlainTextEdit;
    updatedText->setReadOnly(true);

    m_updatedRadioButton = new QRadioButton(Tr::tr("Version after &update"));

    auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);

    auto loadedText = new QTextEdit;
    loadedText->setHtml("<html><head/><body><p><b>"
                            + Tr::tr("Note: You will not be able to check in this file without merging "
                                 "the changes (not supported by the plugin)")
                            + "</b></p></body></html>");

    using namespace Utils::Layouting;

    Column {
        headerLabel,
        Form {
            loadedRadioButton, loadedLabel, br,
            Tr::tr("Created by:"), loadedCreatedByLabel, br,
            Tr::tr("Created on:"),  loadedCreatedOnLabel, br,
            Span(2, loadedText),
        },
        Form {
            m_updatedRadioButton, updatedLabel, br,
            Tr::tr("Created by:"), updatedCreatedByLabel, br,
            Tr::tr("Created on:"),  updatedCreatedOnLabel, br,
            Span(2, updatedText)
        },
        buttonBox,
    }.attachTo(this);

    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

    m_stream = new QTextStream(message.toLocal8Bit(), QIODevice::ReadOnly | QIODevice::Text);
    QString line;
    while (!m_stream->atEnd() && !line.contains(QLatin1String("1) Loaded version")))
        line = m_stream->readLine();
    if (!readValues())
        return;
    loadedLabel->setText(m_versionID);
    loadedCreatedByLabel->setText(m_createdBy);
    loadedCreatedOnLabel->setText(m_createdOn);
    loadedText->insertPlainText(m_message + QLatin1Char(' '));

    line = m_stream->readLine(); // 2) Version after update
    if (!readValues())
        return;
    updatedLabel->setText(m_versionID);
    updatedCreatedByLabel->setText(m_createdBy);
    updatedCreatedOnLabel->setText(m_createdOn);
    updatedText->setPlainText(m_message);
}

VersionSelector::~VersionSelector()
{
    delete m_stream;
}

bool VersionSelector::readValues()
{
    QString line;
    line = m_stream->readLine();
    const QRegularExpression id("Version ID: (.*)");
    const QRegularExpressionMatch idMatch = id.match(line);
    if (!idMatch.hasMatch())
        return false;
    m_versionID = idMatch.captured(1);

    line = m_stream->readLine();
    const QRegularExpression owner("Created by: (.*)");
    const QRegularExpressionMatch ownerMatch = owner.match(line);
    if (!ownerMatch.hasMatch())
        return false;
    m_createdBy = ownerMatch.captured(1);

    line = m_stream->readLine();
    const QRegularExpression dateTimeRE("Created on: (.*)");
    const QRegularExpressionMatch dateTimeMatch = dateTimeRE.match(line);
    if (!dateTimeMatch.hasMatch())
        return false;
    m_createdOn = dateTimeMatch.captured(1);

    QStringList messageLines;
    do
    {
        line = m_stream->readLine().trimmed();
        if (line.isEmpty())
            break;
        messageLines << line;
    } while (!m_stream->atEnd());
    m_message = messageLines.join(QLatin1Char(' '));
    return true;
}

bool VersionSelector::isUpdate() const
{
    return m_updatedRadioButton->isChecked();
}

} // ClearCase::Internal