2009-03-19 17:40:01 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2009-03-19 17:40:01 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2009-03-19 17:40:01 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** No Commercial Usage
|
2009-03-19 17:40:01 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** This file contains pre-release code and may not be distributed.
|
|
|
|
|
** You may use this file in accordance with the terms and conditions
|
|
|
|
|
** contained in the Technology Preview License Agreement accompanying
|
|
|
|
|
** this package.
|
2009-03-19 17:40:01 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
** If you have questions regarding the use of this file, please contact
|
|
|
|
|
** Nokia at qt-info@nokia.com.
|
2009-03-19 17:40:01 +01:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "nicknamedialog.h"
|
|
|
|
|
#include "ui_nicknamedialog.h"
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
|
#include <QtCore/QFile>
|
2010-07-05 09:52:32 +02:00
|
|
|
#include <QtCore/QDir>
|
2009-03-19 17:40:01 +01:00
|
|
|
#include <QtGui/QPushButton>
|
|
|
|
|
#include <QtGui/QStandardItemModel>
|
|
|
|
|
#include <QtGui/QSortFilterProxyModel>
|
|
|
|
|
|
2009-03-20 10:16:18 +01:00
|
|
|
enum { NickNameRole = Qt::UserRole + 1 };
|
|
|
|
|
|
2009-03-19 17:40:01 +01:00
|
|
|
namespace VCSBase {
|
|
|
|
|
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.
|
|
|
|
|
struct NickNameEntry {
|
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),
|
|
|
|
|
m_ui(new 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);
|
|
|
|
|
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);
|
|
|
|
|
connect(m_ui->filterTreeView, SIGNAL(doubleClicked(QModelIndex)), this,
|
|
|
|
|
SLOT(slotDoubleClicked(QModelIndex)));
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NickNameDialog::slotDoubleClicked(const QModelIndex &)
|
|
|
|
|
{
|
|
|
|
|
if (okButton()->isEnabled())
|
|
|
|
|
okButton()->animateClick();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
headers << tr("Name") << tr("E-mail")
|
|
|
|
|
<< tr("Alias") << tr("Alias e-mail");
|
|
|
|
|
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;
|
2009-03-19 17:40:01 +01:00
|
|
|
QFile file(fileName);
|
|
|
|
|
if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) {
|
2010-07-05 09:52:32 +02:00
|
|
|
*errorMessage = tr("Cannot open '%1': %2").
|
|
|
|
|
arg(QDir::toNativeSeparators(fileName), file.errorString());
|
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;
|
2009-03-19 17:40:01 +01:00
|
|
|
const QStringList lines = QString::fromUtf8(file.readAll()).trimmed().split(QLatin1Char('\n'));
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|