don't create a temp file just for mime type detection

This commit is contained in:
Oswald Buddenhagen
2011-03-09 12:17:56 +01:00
parent 93195356a9
commit 53298420ed

View File

@@ -274,8 +274,7 @@ static inline QString filePrefixFromTitle(const QString &title)
} }
// Return a temp file pattern with extension or not // Return a temp file pattern with extension or not
static inline QString tempFilePattern(const QString &prefix, static inline QString tempFilePattern(const QString &prefix, const QString &extension)
const QString &extension = QString())
{ {
// Get directory // Get directory
QString pattern = QDir::tempPath(); QString pattern = QDir::tempPath();
@@ -283,11 +282,8 @@ static inline QString tempFilePattern(const QString &prefix,
pattern.append(QDir::separator()); pattern.append(QDir::separator());
// Prefix, placeholder, extension // Prefix, placeholder, extension
pattern += prefix; pattern += prefix;
pattern += QLatin1String("_XXXXXX"); pattern += QLatin1String("_XXXXXX.");
if (!extension.isEmpty()) { pattern += extension;
pattern += QLatin1Char('.');
pattern += extension;
}
return pattern; return pattern;
} }
@@ -322,29 +318,19 @@ void CodepasterPlugin::finishFetch(const QString &titleDescription,
messageManager->printToOutputPane(tr("Empty snippet received for \"%1\".").arg(titleDescription), true); messageManager->printToOutputPane(tr("Empty snippet received for \"%1\".").arg(titleDescription), true);
return; 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 // 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" // 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 // 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. // by looking at the file name of FileManager::currentFile() without expensive checking.
// Default to "txt". // Default to "txt".
QString suffix; 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(); suffix = mimeType.preferredSuffix();
if (suffix.isEmpty()) if (suffix.isEmpty())
suffix = QLatin1String("txt"); suffix = QLatin1String("txt");
// Write out with extension. const QString filePrefix = filePrefixFromTitle(titleDescription);
tempFile = writeTemporaryFile(tempFilePattern(filePrefix, suffix), content, &errorMessage); QString errorMessage;
TemporaryFilePtr tempFile = writeTemporaryFile(tempFilePattern(filePrefix, suffix), content, &errorMessage);
if (tempFile.isNull()) { if (tempFile.isNull()) {
messageManager->printToOutputPane(errorMessage); messageManager->printToOutputPane(errorMessage);
return; return;