Codepaster: Compile with QT_NO_CAST_FROM_ASCII.

- Change the diff parsing to use QString reducing the
  conversions.

Change-Id: Id4d84947479a14d58b8a60157a98a56db5a89ddb
Reviewed-by: Tobias Hunger <tobias.hunger@nokia.com>
This commit is contained in:
Friedemann Kleint
2012-01-27 15:26:46 +01:00
parent d2f8baad67
commit 663bdbdd59
10 changed files with 48 additions and 48 deletions

View File

@@ -102,7 +102,7 @@ void CodePasterProtocol::fetch(const QString &id)
QTC_ASSERT(!m_fetchReply, return; ) QTC_ASSERT(!m_fetchReply, return; )
QString hostName = m_page->hostName(); QString hostName = m_page->hostName();
const QString httpPrefix = "http://"; const QString httpPrefix = QLatin1String("http://");
QString link; QString link;
// Did the user enter a complete URL instead of an id? // Did the user enter a complete URL instead of an id?
if (id.startsWith(httpPrefix)) { if (id.startsWith(httpPrefix)) {
@@ -114,7 +114,7 @@ void CodePasterProtocol::fetch(const QString &id)
} else { } else {
link = httpPrefix; link = httpPrefix;
link.append(hostName); link.append(hostName);
link.append("/?format=raw&id="); link.append(QLatin1String("/?format=raw&id="));
link.append(id); link.append(id);
m_fetchId = id; m_fetchId = id;
} }
@@ -193,10 +193,10 @@ void CodePasterProtocol::fetchFinished()
if (error) { if (error) {
content = m_fetchReply->errorString(); content = m_fetchReply->errorString();
} else { } else {
content = m_fetchReply->readAll(); content = QString::fromAscii(m_fetchReply->readAll()); // Codepaster does not support special characters.
if (debug) if (debug)
qDebug() << content; qDebug() << content;
if (content.contains("<B>No such paste!</B>")) { if (content.contains(QLatin1String("<B>No such paste!</B>"))) {
content = tr("No such paste"); content = tr("No such paste");
error = true; error = true;
} }

View File

@@ -49,7 +49,7 @@ ColumnIndicatorTextEdit::ColumnIndicatorTextEdit(QWidget *parent) :
setSizePolicy(sizePolicy); setSizePolicy(sizePolicy);
int cmx = 0, cmy = 0, cmw = 0, cmh = 0; int cmx = 0, cmy = 0, cmw = 0, cmh = 0;
getContentsMargins(&cmx, &cmy, &cmw, &cmh); getContentsMargins(&cmx, &cmy, &cmw, &cmh);
m_columnIndicator = QFontMetrics(font).width('W') * 100 + cmx + 1; m_columnIndicator = QFontMetrics(font).width(QLatin1Char('W')) * 100 + cmx + 1;
m_columnIndicatorFont.setFamily(QString::fromUtf8("Times")); m_columnIndicatorFont.setFamily(QString::fromUtf8("Times"));
m_columnIndicatorFont.setPointSizeF(7.0); m_columnIndicatorFont.setPointSizeF(7.0);
} }
@@ -63,7 +63,7 @@ void ColumnIndicatorTextEdit::paintEvent(QPaintEvent *event)
p.setPen(QPen(QColor(0xa0, 0xa0, 0xa0, 0xa0))); p.setPen(QPen(QColor(0xa0, 0xa0, 0xa0, 0xa0)));
p.drawLine(m_columnIndicator, 0, m_columnIndicator, viewport()->height()); p.drawLine(m_columnIndicator, 0, m_columnIndicator, viewport()->height());
int yOffset = verticalScrollBar()->value(); int yOffset = verticalScrollBar()->value();
p.drawText(m_columnIndicator + 1, m_columnIndicatorFont.pointSize() - yOffset, "100"); p.drawText(m_columnIndicator + 1, m_columnIndicatorFont.pointSize() - yOffset, QLatin1String("100"));
} }
} // namespace CodePaster } // namespace CodePaster

View File

@@ -3,6 +3,7 @@ TEMPLATE = lib
TARGET = CodePaster TARGET = CodePaster
include(../../qtcreatorplugin.pri) include(../../qtcreatorplugin.pri)
include(cpaster_dependencies.pri) include(cpaster_dependencies.pri)
DEFINES += QT_NO_CAST_FROM_ASCII
HEADERS += cpasterplugin.h \ HEADERS += cpasterplugin.h \
settingspage.h \ settingspage.h \
protocol.h \ protocol.h \

View File

@@ -256,7 +256,7 @@ void CodepasterPlugin::post(QString data, const QString &mimeType)
PasteView view(m_protocols, mimeType, 0); PasteView view(m_protocols, mimeType, 0);
view.setProtocol(m_settings->protocol); view.setProtocol(m_settings->protocol);
const FileDataList diffChunks = splitDiffToFiles(data.toLatin1()); const FileDataList diffChunks = splitDiffToFiles(data);
const int dialogResult = diffChunks.isEmpty() ? const int dialogResult = diffChunks.isEmpty() ?
view.show(username, QString(), QString(), data) : view.show(username, QString(), QString(), data) :
view.show(username, QString(), QString(), diffChunks); view.show(username, QString(), QString(), diffChunks);

View File

@@ -93,12 +93,12 @@ QString PasteView::comment() const
return comment; return comment;
} }
QByteArray PasteView::content() const QString PasteView::content() const
{ {
if (m_mode == PlainTextMode) if (m_mode == PlainTextMode)
return m_ui.plainTextEdit->toPlainText().toUtf8(); return m_ui.plainTextEdit->toPlainText();
QByteArray newContent; QString newContent;
for (int i = 0; i < m_ui.uiPatchList->count(); ++i) { for (int i = 0; i < m_ui.uiPatchList->count(); ++i) {
QListWidgetItem *item = m_ui.uiPatchList->item(i); QListWidgetItem *item = m_ui.uiPatchList->item(i);
if (item->checkState() != Qt::Unchecked) if (item->checkState() != Qt::Unchecked)
@@ -157,7 +157,7 @@ int PasteView::show(const QString &user, const QString &description,
m_ui.uiPatchList->clear(); m_ui.uiPatchList->clear();
m_parts = parts; m_parts = parts;
m_mode = DiffChunkMode; m_mode = DiffChunkMode;
QByteArray content; QString content;
foreach (const FileData &part, parts) { foreach (const FileData &part, parts) {
QListWidgetItem *itm = new QListWidgetItem(part.filename, m_ui.uiPatchList); QListWidgetItem *itm = new QListWidgetItem(part.filename, m_ui.uiPatchList);
itm->setCheckState(Qt::Checked); itm->setCheckState(Qt::Checked);
@@ -191,7 +191,7 @@ void PasteView::accept()
if (!Protocol::ensureConfiguration(protocol, this)) if (!Protocol::ensureConfiguration(protocol, this))
return; return;
const QByteArray data = content(); const QString data = content();
if (data.isEmpty()) if (data.isEmpty())
return; return;

View File

@@ -69,7 +69,7 @@ public:
QString user() const; QString user() const;
QString description() const; QString description() const;
QString comment() const; QString comment() const;
QByteArray content() const; QString content() const;
QString protocol() const; QString protocol() const;
virtual void accept(); virtual void accept();

View File

@@ -68,9 +68,9 @@ void Settings::fromSettings(const QSettings *settings)
{ {
const QString rootKey = QLatin1String(groupC) + QLatin1Char('/'); const QString rootKey = QLatin1String(groupC) + QLatin1Char('/');
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
const QString defaultUser = qgetenv("USERNAME"); const QString defaultUser = QString::fromLocal8Bit(qgetenv("USERNAME"));
#else #else
const QString defaultUser = qgetenv("USER"); const QString defaultUser = QString::fromLocal8Bit(qgetenv("USER"));
#endif #endif
username = settings->value(rootKey + QLatin1String(userNameKeyC), defaultUser).toString(); username = settings->value(rootKey + QLatin1String(userNameKeyC), defaultUser).toString();
protocol = settings->value(rootKey + QLatin1String(defaultProtocolKeyC), PasteBinDotComProtocol::protocolName()).toString(); protocol = settings->value(rootKey + QLatin1String(defaultProtocolKeyC), PasteBinDotComProtocol::protocolName()).toString();

View File

@@ -44,13 +44,13 @@ QString CGI::encodeURL(const QString &rawText)
QByteArray::const_iterator it = utf.constBegin(); QByteArray::const_iterator it = utf.constBegin();
while (it != utf.constEnd()) { while (it != utf.constEnd()) {
char ch = *it; const char ch = *it;
if (('A' <= ch && ch <= 'Z') if (('A' <= ch && ch <= 'Z')
|| ('a' <= ch && ch <= 'z') || ('a' <= ch && ch <= 'z')
|| ('0' <= ch && ch <= '9')) || ('0' <= ch && ch <= '9'))
enc.append(*it); enc.append(QLatin1Char(ch));
else if (ch == ' ') else if (ch == ' ')
enc.append('+'); enc.append(QLatin1Char('+'));
else { else {
switch (ch) { switch (ch) {
case '-': case '_': case '-': case '_':
@@ -58,14 +58,14 @@ QString CGI::encodeURL(const QString &rawText)
case '.': case '!': case '.': case '!':
case '~': case '*': case '~': case '*':
case '\'': case '\'':
enc.append(ch); enc.append(QLatin1Char(ch));
break; break;
default: default:
ushort c1 = (*it & 0xF0) >> 4; ushort c1 = (*it & 0xF0) >> 4;
ushort c2 = (*it & 0x0F); ushort c2 = (*it & 0x0F);
enc.append('%'); enc.append(QLatin1Char('%'));
enc.append(QChar(*(cgi_chars + c1))); enc.append(QLatin1Char(*(cgi_chars + c1)));
enc.append(QChar(*(cgi_chars + c2))); enc.append(QLatin1Char(*(cgi_chars + c2)));
break; break;
} }
} }
@@ -403,22 +403,22 @@ QString CGI::encodeHTML(const QString &rawText, int conversionFlags)
while (it != rawText.constEnd()) { while (it != rawText.constEnd()) {
const char *html = unicodeToHTML((*it).unicode()); const char *html = unicodeToHTML((*it).unicode());
if (html) { if (html) {
enc.append('&'); enc.append(QLatin1Char('&'));
enc.append(html); enc.append(QLatin1String(html));
enc.append(';'); enc.append(QLatin1Char(';'));
} else if ((conversionFlags & CGI::LineBreaks) } else if ((conversionFlags & CGI::LineBreaks)
&& ((*it).toLatin1() == '\n')) { && ((*it).toLatin1() == '\n')) {
enc.append("<BR>\n"); enc.append(QLatin1String("<BR>\n"));
} else if ((conversionFlags & CGI::Spaces) } else if ((conversionFlags & CGI::Spaces)
&& ((*it).toLatin1() == ' ')) { && ((*it).toLatin1() == ' ')) {
enc.append("&nbsp;"); enc.append(QLatin1String("&nbsp;"));
} else if ((conversionFlags & CGI::Tabs) } else if ((conversionFlags & CGI::Tabs)
&& ((*it).toLatin1() == '\t')) { && ((*it).toLatin1() == '\t')) {
enc.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"); enc.append(QLatin1String("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
} else if ((*it).unicode() > 0x00FF) { } else if ((*it).unicode() > 0x00FF) {
enc.append("&#"); enc.append(QLatin1String("&#"));
enc.append(QString::number((*it).unicode())); enc.append(QString::number((*it).unicode()));
enc.append(';'); enc.append(QLatin1Char(';'));
} else { } else {
enc.append(*it); enc.append(*it);
} }

View File

@@ -34,25 +34,24 @@
#include <QtCore/QRegExp> #include <QtCore/QRegExp>
FileDataList splitDiffToFiles(const QByteArray &data) FileDataList splitDiffToFiles(const QString &strData)
{ {
FileDataList ret; FileDataList ret;
QString strData = data;
QString splitExpression; QString splitExpression;
if (data.contains("==== ") && data.contains(" ====\n")) { if (strData.contains(QLatin1String("==== ")) && strData.contains(QLatin1String(" ====\n"))) {
// Perforce diff // Perforce diff
splitExpression = "==== ([^\\n\\r]+) - ([^\\n\\r]+) ===="; splitExpression = QLatin1String("==== ([^\\n\\r]+) - ([^\\n\\r]+) ====");
} else if (data.contains("--- ") && data.contains("\n+++ ")) { } else if (strData.contains(QLatin1String("--- ")) && strData.contains(QLatin1String("\n+++ "))) {
// Unified contextual diff // Unified contextual diff
splitExpression = "\\-\\-\\- ([^\\n\\r]*)" splitExpression = QLatin1String("\\-\\-\\- ([^\\n\\r]*)"
"\\n\\+\\+\\+ ([^\\n\\r]*)"; "\\n\\+\\+\\+ ([^\\n\\r]*)");
} else if (data.contains("*** ") && data.contains("\n--- ")) { } else if (strData.contains(QLatin1String("*** ")) && strData.contains(QLatin1String("\n--- "))) {
// Copied contextual diff // Copied contextual diff
splitExpression = "\\*\\*\\* ([^\\n\\r]*) [0-9\\-]* [0-9:\\.]*[^\\n\\r]*" splitExpression = QLatin1String("\\*\\*\\* ([^\\n\\r]*) [0-9\\-]* [0-9:\\.]*[^\\n\\r]*"
"\\n\\-\\-\\- ([^\\n\\r]*) [0-9\\-]* [0-9:\\.]*[^\\n\\r]*"; "\\n\\-\\-\\- ([^\\n\\r]*) [0-9\\-]* [0-9:\\.]*[^\\n\\r]*");
} else { } else {
return FileDataList(); return FileDataList();
@@ -60,7 +59,7 @@ FileDataList splitDiffToFiles(const QByteArray &data)
int splitIndex = 0, previousSplit = -1; int splitIndex = 0, previousSplit = -1;
QRegExp splitExpr(splitExpression); QRegExp splitExpr(splitExpression);
QString filename, content; QString filename;
// The algorithm works like this: // The algorithm works like this:
// On the first match we only get the filename of the first patch part // On the first match we only get the filename of the first patch part
// On the second match (if any) we get the diff content, and the name of the next file patch // On the second match (if any) we get the diff content, and the name of the next file patch
@@ -68,7 +67,7 @@ FileDataList splitDiffToFiles(const QByteArray &data)
while (-1 != (splitIndex = splitExpr.indexIn(strData,splitIndex))) { while (-1 != (splitIndex = splitExpr.indexIn(strData,splitIndex))) {
if (!filename.isEmpty()) { if (!filename.isEmpty()) {
QString content = strData.mid(previousSplit, splitIndex - previousSplit); QString content = strData.mid(previousSplit, splitIndex - previousSplit);
ret.append(FileData(filename, content.toLatin1())); ret.append(FileData(filename, content));
} }
// If the first index in not at the beginning of the file, then we know there's content // If the first index in not at the beginning of the file, then we know there's content
@@ -76,7 +75,7 @@ FileDataList splitDiffToFiles(const QByteArray &data)
// a 'fake' filename. // a 'fake' filename.
if (previousSplit == -1 && splitIndex > 0 && filename.isEmpty()) { if (previousSplit == -1 && splitIndex > 0 && filename.isEmpty()) {
QString content = strData.left(splitIndex); QString content = strData.left(splitIndex);
ret.append(FileData("<Header information>", content.toLatin1())); ret.append(FileData(QLatin1String("<Header information>"), content));
} }
filename = splitExpr.cap(1); filename = splitExpr.cap(1);
@@ -85,8 +84,8 @@ FileDataList splitDiffToFiles(const QByteArray &data)
} }
// Append the last patch content // Append the last patch content
if (!filename.isEmpty()) { if (!filename.isEmpty()) {
QString content = strData.mid(previousSplit); const QString content = strData.mid(previousSplit);
ret.append(FileData(filename, content.toLatin1())); ret.append(FileData(filename, content));
} }
return ret; return ret;

View File

@@ -39,15 +39,15 @@
struct FileData struct FileData
{ {
FileData(const QString &f, const QByteArray &c) FileData(const QString &f, const QString &c)
{ filename = f; content = c; } { filename = f; content = c; }
QString filename; QString filename;
QByteArray content; QString content;
}; };
typedef QList<FileData> FileDataList; typedef QList<FileData> FileDataList;
FileDataList splitDiffToFiles(const QByteArray &data); FileDataList splitDiffToFiles(const QString &data);
#endif // SPLITTER_H #endif // SPLITTER_H