forked from qt-creator/qt-creator
don't create a temp file just for mime type detection
This commit is contained in:
@@ -274,8 +274,7 @@ static inline QString filePrefixFromTitle(const QString &title)
|
||||
}
|
||||
|
||||
// Return a temp file pattern with extension or not
|
||||
static inline QString tempFilePattern(const QString &prefix,
|
||||
const QString &extension = QString())
|
||||
static inline QString tempFilePattern(const QString &prefix, const QString &extension)
|
||||
{
|
||||
// Get directory
|
||||
QString pattern = QDir::tempPath();
|
||||
@@ -283,11 +282,8 @@ static inline QString tempFilePattern(const QString &prefix,
|
||||
pattern.append(QDir::separator());
|
||||
// Prefix, placeholder, extension
|
||||
pattern += prefix;
|
||||
pattern += QLatin1String("_XXXXXX");
|
||||
if (!extension.isEmpty()) {
|
||||
pattern += QLatin1Char('.');
|
||||
pattern += extension;
|
||||
}
|
||||
pattern += QLatin1String("_XXXXXX.");
|
||||
pattern += extension;
|
||||
return pattern;
|
||||
}
|
||||
|
||||
@@ -322,29 +318,19 @@ void CodepasterPlugin::finishFetch(const QString &titleDescription,
|
||||
messageManager->printToOutputPane(tr("Empty snippet received for \"%1\".").arg(titleDescription), true);
|
||||
return;
|
||||
}
|
||||
// Write the file out and do a mime type detection on the content. Note
|
||||
// that for the initial detection, there must not be a suffix
|
||||
// as we want mime type detection to trigger on the content and not on
|
||||
// higher-prioritized suffixes.
|
||||
const QString filePrefix = filePrefixFromTitle(titleDescription);
|
||||
QString errorMessage;
|
||||
TemporaryFilePtr tempFile = writeTemporaryFile(tempFilePattern(filePrefix), content, &errorMessage);
|
||||
if (tempFile.isNull()) {
|
||||
messageManager->printToOutputPane(errorMessage);
|
||||
return;
|
||||
}
|
||||
// If the mime type has a preferred suffix (cpp/h/patch...), use that for
|
||||
// the temporary file. This is to make it more convenient to "Save as"
|
||||
// for the user and also to be able to tell a patch or diff in the VCS plugins
|
||||
// by looking at the file name of FileManager::currentFile() without expensive checking.
|
||||
// Default to "txt".
|
||||
QString suffix;
|
||||
if (const Core::MimeType mimeType = Core::ICore::instance()->mimeDatabase()->findByFile(QFileInfo(tempFile->fileName())))
|
||||
if (const Core::MimeType mimeType = Core::ICore::instance()->mimeDatabase()->findByData(content.toUtf8()))
|
||||
suffix = mimeType.preferredSuffix();
|
||||
if (suffix.isEmpty())
|
||||
suffix = QLatin1String("txt");
|
||||
// Write out with extension.
|
||||
tempFile = writeTemporaryFile(tempFilePattern(filePrefix, suffix), content, &errorMessage);
|
||||
const QString filePrefix = filePrefixFromTitle(titleDescription);
|
||||
QString errorMessage;
|
||||
TemporaryFilePtr tempFile = writeTemporaryFile(tempFilePattern(filePrefix, suffix), content, &errorMessage);
|
||||
if (tempFile.isNull()) {
|
||||
messageManager->printToOutputPane(errorMessage);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user