2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2009-03-19 17:40:01 +01:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2009-03-19 17:40:01 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2009-03-19 17:40:01 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2009-03-19 17:40:01 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2009-03-19 17:40:01 +01:00
|
|
|
|
|
|
|
|
#include "nicknamedialog.h"
|
|
|
|
|
#include "ui_nicknamedialog.h"
|
|
|
|
|
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QPushButton>
|
|
|
|
|
#include <QStandardItemModel>
|
|
|
|
|
#include <QSortFilterProxyModel>
|
2009-03-19 17:40:01 +01:00
|
|
|
|
2009-03-20 10:16:18 +01:00
|
|
|
enum { NickNameRole = Qt::UserRole + 1 };
|
|
|
|
|
|
2011-03-28 14:19:17 +02:00
|
|
|
/*!
|
2012-01-07 12:31:48 +01:00
|
|
|
\class VcsBase::Internal::NickNameDialog
|
2011-03-28 14:19:17 +02:00
|
|
|
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The NickNameDialog class shows users from a mail cap file.
|
2011-03-28 14:19:17 +02:00
|
|
|
|
|
|
|
|
Manages a list of users read from an extended
|
|
|
|
|
mail cap file, consisting of 4 columns: "Name Mail [AliasName [AliasMail]]".
|
|
|
|
|
|
|
|
|
|
The names can be used for insertion into "RevBy:" fields; aliases will
|
|
|
|
|
be preferred.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-01-07 12:31:48 +01:00
|
|
|
namespace VcsBase {
|
2009-03-19 17:40:01 +01:00
|
|
|
namespace Internal {
|
|
|
|
|
|
2009-03-20 10:16:18 +01:00
|
|
|
// For code clarity, a struct representing the entries of a mail map file
|
|
|
|
|
// with parse and model functions.
|
2012-01-07 03:35:54 +01:00
|
|
|
class NickNameEntry
|
|
|
|
|
{
|
2011-12-08 13:07:00 +01:00
|
|
|
public:
|
2009-03-19 17:40:01 +01:00
|
|
|
void clear();
|
|
|
|
|
bool parse(const QString &);
|
|
|
|
|
QString nickName() const;
|
2009-03-20 10:16:18 +01:00
|
|
|
QList<QStandardItem *> toModelRow() const;
|
|
|
|
|
static QString nickNameOf(const QStandardItem *item);
|
2009-03-19 17:40:01 +01:00
|
|
|
|
|
|
|
|
QString name;
|
|
|
|
|
QString email;
|
|
|
|
|
QString aliasName;
|
|
|
|
|
QString aliasEmail;
|
|
|
|
|
};
|
|
|
|
|
|
2009-03-20 10:16:18 +01:00
|
|
|
void NickNameEntry::clear()
|
2009-03-19 17:40:01 +01:00
|
|
|
{
|
|
|
|
|
name.clear();
|
|
|
|
|
email.clear();
|
|
|
|
|
aliasName.clear();
|
|
|
|
|
aliasEmail.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse "Hans Mustermann <HM@acme.de> [Alias [<alias@acme.de>]]"
|
|
|
|
|
|
2009-03-20 10:16:18 +01:00
|
|
|
bool NickNameEntry::parse(const QString &l)
|
2009-03-19 17:40:01 +01:00
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
const QChar lessThan = QLatin1Char('<');
|
|
|
|
|
const QChar greaterThan = QLatin1Char('>');
|
|
|
|
|
// Get first name/mail pair
|
|
|
|
|
int mailPos = l.indexOf(lessThan);
|
|
|
|
|
if (mailPos == -1)
|
|
|
|
|
return false;
|
|
|
|
|
name = l.mid(0, mailPos).trimmed();
|
|
|
|
|
mailPos++;
|
|
|
|
|
const int mailEndPos = l.indexOf(greaterThan, mailPos);
|
|
|
|
|
if (mailEndPos == -1)
|
|
|
|
|
return false;
|
|
|
|
|
email = l.mid(mailPos, mailEndPos - mailPos);
|
|
|
|
|
// get optional 2nd name/mail pair
|
|
|
|
|
const int aliasNameStart = mailEndPos + 1;
|
|
|
|
|
if (aliasNameStart >= l.size())
|
|
|
|
|
return true;
|
|
|
|
|
int aliasMailPos = l.indexOf(lessThan, aliasNameStart);
|
|
|
|
|
if (aliasMailPos == -1) {
|
|
|
|
|
aliasName =l.mid(aliasNameStart, l.size() - aliasNameStart).trimmed();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
aliasName = l.mid(aliasNameStart, aliasMailPos - aliasNameStart).trimmed();
|
|
|
|
|
aliasMailPos++;
|
|
|
|
|
const int aliasMailEndPos = l.indexOf(greaterThan, aliasMailPos);
|
|
|
|
|
if (aliasMailEndPos == -1)
|
|
|
|
|
return true;
|
|
|
|
|
aliasEmail = l.mid(aliasMailPos, aliasMailEndPos - aliasMailPos);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Format "Hans Mustermann <HM@acme.de>"
|
|
|
|
|
static inline QString formatNick(const QString &name, const QString &email)
|
|
|
|
|
{
|
|
|
|
|
QString rc = name;
|
|
|
|
|
if (!email.isEmpty()) {
|
|
|
|
|
rc += QLatin1String(" <");
|
|
|
|
|
rc += email;
|
|
|
|
|
rc += QLatin1Char('>');
|
|
|
|
|
}
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-20 10:16:18 +01:00
|
|
|
QString NickNameEntry::nickName() const
|
2009-03-19 17:40:01 +01:00
|
|
|
{
|
|
|
|
|
return aliasName.isEmpty() ? formatNick(name, email) : formatNick(aliasName, aliasEmail);
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-20 10:16:18 +01:00
|
|
|
QList<QStandardItem *> NickNameEntry::toModelRow() const
|
2009-03-19 17:40:01 +01:00
|
|
|
{
|
2010-01-29 21:33:57 +01:00
|
|
|
const QVariant nickNameData = nickName();
|
2009-03-20 14:20:28 +01:00
|
|
|
const Qt::ItemFlags flags = Qt::ItemIsSelectable|Qt::ItemIsEnabled;
|
2009-03-20 10:16:18 +01:00
|
|
|
QStandardItem *i1 = new QStandardItem(name);
|
2009-03-20 14:20:28 +01:00
|
|
|
i1->setFlags(flags);
|
2009-03-20 10:16:18 +01:00
|
|
|
i1->setData(nickNameData, NickNameRole);
|
|
|
|
|
QStandardItem *i2 = new QStandardItem(email);
|
2009-03-20 14:20:28 +01:00
|
|
|
i1->setFlags(flags);
|
2009-03-20 10:16:18 +01:00
|
|
|
i2->setData(nickNameData, NickNameRole);
|
|
|
|
|
QStandardItem *i3 = new QStandardItem(aliasName);
|
2009-03-20 14:20:28 +01:00
|
|
|
i3->setFlags(flags);
|
2009-03-20 10:16:18 +01:00
|
|
|
i3->setData(nickNameData, NickNameRole);
|
|
|
|
|
QStandardItem *i4 = new QStandardItem(aliasEmail);
|
2009-03-20 14:20:28 +01:00
|
|
|
i4->setFlags(flags);
|
2009-03-20 10:16:18 +01:00
|
|
|
i4->setData(nickNameData, NickNameRole);
|
|
|
|
|
QList<QStandardItem *> row;
|
|
|
|
|
row << i1 << i2 << i3 << i4;
|
|
|
|
|
return row;
|
2009-03-19 17:40:01 +01:00
|
|
|
}
|
|
|
|
|
|
2009-03-20 10:16:18 +01:00
|
|
|
QString NickNameEntry::nickNameOf(const QStandardItem *item)
|
|
|
|
|
{
|
|
|
|
|
return item->data(NickNameRole).toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QDebug operator<<(QDebug d, const NickNameEntry &e)
|
2009-03-19 17:40:01 +01:00
|
|
|
{
|
|
|
|
|
d.nospace() << "Name='" << e.name << "' Mail='" << e.email
|
|
|
|
|
<< " Alias='" << e.aliasName << " AliasEmail='" << e.aliasEmail << "'\n";
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-20 14:20:28 +01:00
|
|
|
NickNameDialog::NickNameDialog(QStandardItemModel *model, QWidget *parent) :
|
2009-03-19 17:40:01 +01:00
|
|
|
QDialog(parent),
|
2011-11-23 15:15:01 +00:00
|
|
|
m_ui(new Internal::Ui::NickNameDialog),
|
2009-03-20 14:20:28 +01:00
|
|
|
m_model(model),
|
2009-03-19 17:40:01 +01:00
|
|
|
m_filterModel(new QSortFilterProxyModel(this))
|
|
|
|
|
{
|
|
|
|
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
|
|
|
|
m_ui->setupUi(this);
|
|
|
|
|
okButton()->setEnabled(false);
|
|
|
|
|
|
|
|
|
|
// Populate model and grow tree to accommodate it
|
2009-03-20 14:20:28 +01:00
|
|
|
m_filterModel->setSourceModel(model);
|
2009-03-19 17:40:01 +01:00
|
|
|
m_filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
|
m_ui->filterTreeView->setModel(m_filterModel);
|
2014-04-14 15:43:55 +02:00
|
|
|
m_ui->filterTreeView->setActivationMode(Utils::DoubleClickActivation);
|
2009-03-19 17:40:01 +01:00
|
|
|
const int columnCount = m_filterModel->columnCount();
|
|
|
|
|
int treeWidth = 0;
|
|
|
|
|
for (int c = 0; c < columnCount; c++) {
|
|
|
|
|
m_ui->filterTreeView->resizeColumnToContents(c);
|
|
|
|
|
treeWidth += m_ui->filterTreeView->columnWidth(c);
|
|
|
|
|
}
|
|
|
|
|
m_ui->filterTreeView->setMinimumWidth(treeWidth + 20);
|
2014-04-14 15:43:55 +02:00
|
|
|
connect(m_ui->filterTreeView, SIGNAL(activated(QModelIndex)), this,
|
|
|
|
|
SLOT(slotActivated(QModelIndex)));
|
2009-03-19 17:40:01 +01:00
|
|
|
connect(m_ui->filterTreeView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
|
|
|
|
|
this, SLOT(slotCurrentItemChanged(QModelIndex)));
|
2009-12-16 15:58:10 +01:00
|
|
|
connect(m_ui->filterLineEdit, SIGNAL(filterChanged(QString)),
|
2009-03-19 17:40:01 +01:00
|
|
|
m_filterModel, SLOT(setFilterFixedString(QString)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NickNameDialog::~NickNameDialog()
|
|
|
|
|
{
|
|
|
|
|
delete m_ui;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QPushButton *NickNameDialog::okButton() const
|
|
|
|
|
{
|
|
|
|
|
return m_ui->buttonBox->button(QDialogButtonBox::Ok);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NickNameDialog::slotCurrentItemChanged(const QModelIndex &index)
|
|
|
|
|
{
|
|
|
|
|
okButton()->setEnabled(index.isValid());
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-14 15:43:55 +02:00
|
|
|
void NickNameDialog::slotActivated(const QModelIndex &)
|
2009-03-19 17:40:01 +01:00
|
|
|
{
|
|
|
|
|
if (okButton()->isEnabled())
|
2014-04-09 16:05:51 +02:00
|
|
|
okButton()->click();
|
2009-03-19 17:40:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString NickNameDialog::nickName() const
|
|
|
|
|
{
|
|
|
|
|
const QModelIndex index = m_ui->filterTreeView->selectionModel()->currentIndex();
|
|
|
|
|
if (index.isValid()) {
|
|
|
|
|
const QModelIndex sourceIndex = m_filterModel->mapToSource(index);
|
2009-03-20 14:20:28 +01:00
|
|
|
if (const QStandardItem *item = m_model->itemFromIndex(sourceIndex))
|
2009-03-20 10:16:18 +01:00
|
|
|
return NickNameEntry::nickNameOf(item);
|
2009-03-19 17:40:01 +01:00
|
|
|
}
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-20 14:20:28 +01:00
|
|
|
QStandardItemModel *NickNameDialog::createModel(QObject *parent)
|
2009-03-19 17:40:01 +01:00
|
|
|
{
|
2009-03-20 14:20:28 +01:00
|
|
|
QStandardItemModel *model = new QStandardItemModel(parent);
|
|
|
|
|
QStringList headers;
|
2014-07-16 10:51:42 +02:00
|
|
|
headers << tr("Name") << tr("Email")
|
|
|
|
|
<< tr("Alias") << tr("Alias email");
|
2009-03-20 14:20:28 +01:00
|
|
|
model->setHorizontalHeaderLabels(headers);
|
|
|
|
|
return model;
|
2009-03-19 17:40:01 +01:00
|
|
|
}
|
|
|
|
|
|
2009-03-20 14:20:28 +01:00
|
|
|
bool NickNameDialog::populateModelFromMailCapFile(const QString &fileName,
|
|
|
|
|
QStandardItemModel *model,
|
|
|
|
|
QString *errorMessage)
|
2009-03-19 17:40:01 +01:00
|
|
|
{
|
2009-03-20 14:20:28 +01:00
|
|
|
if (const int rowCount = model->rowCount())
|
|
|
|
|
model->removeRows(0, rowCount);
|
|
|
|
|
if (fileName.isEmpty())
|
|
|
|
|
return true;
|
2011-03-30 15:15:15 +02:00
|
|
|
Utils::FileReader reader;
|
|
|
|
|
if (!reader.fetch(fileName, QIODevice::Text, errorMessage))
|
2009-03-19 17:40:01 +01:00
|
|
|
return false;
|
|
|
|
|
// Split into lines and read
|
2009-03-20 10:16:18 +01:00
|
|
|
NickNameEntry entry;
|
2011-03-30 15:15:15 +02:00
|
|
|
const QStringList lines = QString::fromUtf8(reader.data()).trimmed().split(QLatin1Char('\n'));
|
2009-03-19 17:40:01 +01:00
|
|
|
const int count = lines.size();
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
if (entry.parse(lines.at(i))) {
|
2009-03-20 10:16:18 +01:00
|
|
|
model->appendRow(entry.toModelRow());
|
2009-03-19 17:40:01 +01:00
|
|
|
} else {
|
2010-07-05 09:52:32 +02:00
|
|
|
qWarning("%s: Invalid mail cap entry at line %d: '%s'\n",
|
|
|
|
|
qPrintable(QDir::toNativeSeparators(fileName)),
|
|
|
|
|
i + 1, qPrintable(lines.at(i)));
|
2009-03-19 17:40:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
2009-03-20 10:16:18 +01:00
|
|
|
model->sort(0);
|
2009-03-19 17:40:01 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-20 14:20:28 +01:00
|
|
|
QStringList NickNameDialog::nickNameList(const QStandardItemModel *model)
|
2009-03-19 17:40:01 +01:00
|
|
|
{
|
|
|
|
|
QStringList rc;
|
2009-03-20 10:16:18 +01:00
|
|
|
const int rowCount = model->rowCount();
|
|
|
|
|
for (int r = 0; r < rowCount; r++)
|
|
|
|
|
rc.push_back(NickNameEntry::nickNameOf(model->item(r, 0)));
|
2009-03-19 17:40:01 +01:00
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-07 03:35:54 +01:00
|
|
|
} // namespace Internal
|
2012-01-07 12:31:48 +01:00
|
|
|
} // namespace VcsBase
|