Qt: Move the MIME init thread to WiresharkApplication.

The Qt 4.8 "Starting a Thread" document at
http://doc.qt.io/qt-4.8/threads-starting.html says,

"Note that you must create the QApplication (or QCoreApplication) object
 before you can create a QThread."

It looks like this changed some time around Qt 5.5 or 5.6, e.g.
https://codereview.qt-project.org/#/c/120793/, but just to be safe move
the MIME database initialization thread to WiresharkApplication. We
start reading the database later than I'd like, but it's still early
enough to make a difference here.

QMimeDatabase was added in Qt 5.0. Add version checks.

Change-Id: Ic80ebb8692e93b1e4aea7a8670f4fcf45e385b29
Reviewed-on: https://code.wireshark.org/review/24512
Petri-Dish: Gerald Combs <gerald@wireshark.org>
Tested-by: Petri Dish Buildbot
Reviewed-by: Gerald Combs <gerald@wireshark.org>
This commit is contained in:
Gerald Combs 2017-11-20 09:18:59 -08:00
parent 5968fc1cae
commit dd562b503f
2 changed files with 27 additions and 23 deletions

View File

@ -93,11 +93,15 @@
#include <QMainWindow>
#include <QMutableListIterator>
#include <QSocketNotifier>
#include <QThread>
#include <QUrl>
#include <QColorDialog>
#include <qmath.h>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QMimeDatabase>
#include <QThreadPool>
#endif
#ifdef _MSC_VER
#pragma warning(pop)
#endif
@ -115,6 +119,22 @@ static QHash<int, QList<QAction *> > removed_menu_groups_;
QString WiresharkApplication::window_title_separator_ = QString::fromUtf8(" " UTF8_MIDDLE_DOT " ");
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
// QMimeDatabase parses a large-ish XML file and can be slow to initialize.
// Do so in a worker thread as early as possible.
// https://github.com/lxde/pcmanfm-qt/issues/415
class MimeDatabaseInitThread : public QRunnable
{
private:
void run()
{
QMimeDatabase mime_db;
mime_db.mimeTypeForData(QByteArray());
}
};
#endif
void
topic_action(topic_action_e action)
{
@ -438,9 +458,6 @@ void WiresharkApplication::reloadLuaPluginsDelayed()
QTimer::singleShot(0, this, SIGNAL(reloadLuaPlugins()));
}
// This should be the first icon we fetch. We delay loading it as much as
// possible in order to allow time for MimeDatabaseInitThread in
// wireshark-qt.cpp to do its work.
const QIcon &WiresharkApplication::normalIcon()
{
if (normal_icon_.isNull()) {
@ -731,6 +748,11 @@ WiresharkApplication::WiresharkApplication(int &argc, char **argv) :
wsApp = this;
setApplicationName("Wireshark");
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
MimeDatabaseInitThread *mime_db_init_thread = new(MimeDatabaseInitThread);
QThreadPool::globalInstance()->start(mime_db_init_thread);
#endif
Q_INIT_RESOURCE(about);
Q_INIT_RESOURCE(i18n);
Q_INIT_RESOURCE(layout);
@ -992,7 +1014,7 @@ void WiresharkApplication::clearDynamicMenuGroupItems()
void WiresharkApplication::initializeIcons()
{
// Do this as late as possible in order to allow time for
// MimeDatabaseInitThread in wireshark-qt.cpp to do its work.
// MimeDatabaseInitThread to do its work.
QList<int> icon_sizes = QList<int>() << 16 << 24 << 32 << 48 << 64 << 128 << 256 << 512 << 1024;
foreach (int icon_size, icon_sizes) {
QString icon_path = QString(":/wsicon/wsicon%1.png").arg(icon_size);

View File

@ -96,8 +96,6 @@
#include "caputils/capture-pcap-util.h"
#include <QMessageBox>
#include <QMimeDatabase>
#include <QThreadPool>
#ifdef _WIN32
# include "caputils/capture-wpcap.h"
@ -140,19 +138,6 @@
*/
#define DEBUG_STARTUP_TIME_LOGLEVEL 252
// QMimeDatabase can be slow to initialize. Do so in a worker thread
// as early as possible.
// https://github.com/lxde/pcmanfm-qt/issues/415
class MimeDatabaseInitThread : public QRunnable
{
private:
void run()
{
QMimeDatabase mime_db;
mime_db.mimeTypeForData(QByteArray());
}
};
/* update the main window */
void main_window_update(void)
{
@ -395,9 +380,6 @@ int main(int argc, char *qt_argv[])
QTextCodec::setCodecForTr(utf8codec);
#endif
MimeDatabaseInitThread *mime_db_init_thread = new(MimeDatabaseInitThread);
QThreadPool::globalInstance()->start(mime_db_init_thread);
/* Set the C-language locale to the native environment. */
setlocale(LC_ALL, "");