From ab3c63410fff287da0cc5118119fe24d09bb6a21 Mon Sep 17 00:00:00 2001 From: "craig.p.drummond" Date: Sat, 4 May 2013 10:35:06 +0000 Subject: [PATCH] - Fix deletion of threads. - Enable online services for windows builds. --- CMakeLists.txt | 9 +----- ChangeLog | 6 ++-- INSTALL | 2 +- devices/cddb.cpp | 7 ++-- devices/cddb.h | 4 +-- devices/filejob.cpp | 7 ++-- devices/filejob.h | 4 +-- devices/fsdevice.cpp | 6 ++-- devices/fsdevice.h | 4 +-- devices/mtpdevice.cpp | 6 ++-- devices/mtpdevice.h | 4 +-- devices/musicbrainz.cpp | 7 ++-- devices/musicbrainz.h | 4 +-- gui/cachesettings.cpp | 10 +++--- gui/cachesettings.h | 6 ++-- gui/covers.cpp | 12 +++---- gui/covers.h | 6 ++-- gui/mainwindow.cpp | 1 - http/httpserver.cpp | 10 +++--- http/httpserver.h | 4 +-- mpd/mpdconnection.cpp | 6 ++-- mpd/mpdconnection.h | 4 +-- online/onlineservice.cpp | 21 +++++++----- online/onlineservice.h | 9 ++++-- replaygain/jobcontroller.cpp | 6 ++-- replaygain/jobcontroller.h | 8 +++-- support/CMakeLists.txt | 2 ++ support/thread.cpp | 62 ++++++++++++++++++++++++++++++++++++ support/thread.h | 55 ++++++++++++++++++++++++++++++++ support/utils.cpp | 6 ---- support/utils.h | 2 -- 31 files changed, 204 insertions(+), 96 deletions(-) create mode 100644 support/thread.cpp create mode 100644 support/thread.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8feafc0a2..fb181ebda 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,14 +31,7 @@ OPTION(ENABLE_MPG123 "Enable mpg123 libraries (required for replaygain calculati OPTION(ENABLE_SPEEXDSP "Enable SpeexDSP libraries (used to speed-up replaygain calculation)" ON) OPTION(ENABLE_UDISKS2 "Build UDisks2 backend, and NOT UDisks, for Qt builds" OFF) OPTION(ENABLE_OVERLAYSCROLLBARS "Enable support for overlay style scrollbars when using QGtkStyle (Linux only)" OFF) - -if (WIN32) - OPTION(ENABLE_ONLINE_SERVICES "Enable support for online services (Jamendo and Magantune)" OFF) -else (WIN32) - OPTION(ENABLE_ONLINE_SERVICES "Enable support for online services (Jamendo and Magantune)" ON) -endif (WIN32) - -#ifdef ENABLE_ONLINE_SERVICES +OPTION(ENABLE_ONLINE_SERVICES "Enable support for online services (Jamendo and Magantune)" ON) if (ENABLE_QT5) set(ENABLE_PHONON FALSE) diff --git a/ChangeLog b/ChangeLog index 49b149f5f..74c3552cd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,8 +6,6 @@ 3. If cover name contains %artist%, then replace with the album artist of the current song. Likewise for %album% 4. Add option (to sidebar context menu) to toggle usage of monochrome icons. -5. Fix loading of SVGs in windows build. -6. Fix cover downloading. 1.0.2 ----- @@ -15,6 +13,10 @@ 2. Connect 'Set Cover' signal for windows builds. 3. Fix usage of timers in threads. 4. Fix non-taglib build. +5. Fix loading of SVGs in windows build. +6. Fix cover downloading. +7. Fix deletion of threads. +8. Enable online services for windows builds. 1.0.1 ----- diff --git a/INSTALL b/INSTALL index babcd4fdd..942919d4c 100644 --- a/INSTALL +++ b/INSTALL @@ -59,5 +59,5 @@ The following options may be passed to CMake: -DENABLE_ONLINE_SERVICES=ON Enable support for Jamendo and Magnatune online services. - Default: ON (Linux) OFF (Windows) + Default: ON diff --git a/devices/cddb.cpp b/devices/cddb.cpp index d8f1373b1..72979dd7a 100644 --- a/devices/cddb.cpp +++ b/devices/cddb.cpp @@ -29,10 +29,9 @@ #include "networkproxyfactory.h" #endif #include "localize.h" -#include "utils.h" +#include "thread.h" #include #include -#include #include #include #include @@ -57,14 +56,14 @@ Cddb::Cddb(const QString &device) : dev(device) , disc(0) { - thread=new QThread(); + thread=new Thread(metaObject()->className()); moveToThread(thread); thread->start(); } Cddb::~Cddb() { - Utils::stopThread(thread); + thread->stop(); if (disc) { cddb_disc_destroy(disc); } diff --git a/devices/cddb.h b/devices/cddb.h index a047195c3..d518f0257 100644 --- a/devices/cddb.h +++ b/devices/cddb.h @@ -42,7 +42,7 @@ struct CdAlbum { }; #ifdef CDDB_FOUND -class QThread; +class Thread; typedef struct cddb_disc_s cddb_disc_t; class Cddb : public QObject @@ -67,7 +67,7 @@ private: void readDisc(); private: - QThread *thread; + Thread *thread; QString dev; cddb_disc_t *disc; CdAlbum initial; diff --git a/devices/filejob.cpp b/devices/filejob.cpp index 672badd6f..61e6b0430 100644 --- a/devices/filejob.cpp +++ b/devices/filejob.cpp @@ -26,8 +26,8 @@ #include "device.h" #include "lyricspage.h" #include "covers.h" +#include "thread.h" #include -#include #include #include @@ -57,7 +57,7 @@ FileScheduler::FileScheduler() void FileScheduler::addJob(FileJob *job) { if (!thread) { - thread=new QThread(); + thread=new Thread(metaObject()->className()); moveToThread(thread); thread->start(); } @@ -67,8 +67,7 @@ void FileScheduler::addJob(FileJob *job) void FileScheduler::stop() { if (thread) { - Utils::stopThread(thread); - thread=0; + thread->stop(); } } diff --git a/devices/filejob.h b/devices/filejob.h index d76d91a59..7c2f02bd5 100644 --- a/devices/filejob.h +++ b/devices/filejob.h @@ -30,7 +30,7 @@ #include "deviceoptions.h" class QTemporaryFile; -class QThread; +class Thread; class FileJob; class FileScheduler : public QObject @@ -43,7 +43,7 @@ public: void addJob(FileJob *job); void stop(); private: - QThread *thread; + Thread *thread; }; class FileJob : public QObject diff --git a/devices/fsdevice.cpp b/devices/fsdevice.cpp index 80605e4b0..8dfe57b57 100644 --- a/devices/fsdevice.cpp +++ b/devices/fsdevice.cpp @@ -38,12 +38,12 @@ #include "actiondialog.h" #include "localize.h" #include "covers.h" +#include "thread.h" #include #include #include #include #include -#include #include const QLatin1String FsDevice::constCantataCacheFile("/.cache"); @@ -67,7 +67,7 @@ MusicScanner::MusicScanner() , count(0) , lastUpdate(0) { - thread=new QThread(); + thread=new Thread(metaObject()->className()); moveToThread(thread); thread->start(); } @@ -140,7 +140,7 @@ void MusicScanner::saveCache(const QString &cache, MusicLibraryItemRoot *lib) void MusicScanner::stop() { stopRequested=true; - Utils::stopThread(thread); + thread->stop(); thread=0; } diff --git a/devices/fsdevice.h b/devices/fsdevice.h index 3ac7ac1ef..574eaf492 100644 --- a/devices/fsdevice.h +++ b/devices/fsdevice.h @@ -31,7 +31,7 @@ #include "musiclibraryitemroot.h" #include -class QThread; +class Thread; struct FileOnlySong : public Song { @@ -74,7 +74,7 @@ private: void scanFolder(MusicLibraryItemRoot *library, const QString &topLevel, const QString &f, QSet &existing, int level); private: - QThread *thread; + Thread *thread; bool stopRequested; int count; int lastUpdate; diff --git a/devices/mtpdevice.cpp b/devices/mtpdevice.cpp index 134f9d8b5..a6370dfd3 100644 --- a/devices/mtpdevice.cpp +++ b/devices/mtpdevice.cpp @@ -39,7 +39,7 @@ #include "localize.h" #include "filejob.h" #include "settings.h" -#include +#include "thread.h" #include #include #include @@ -87,7 +87,7 @@ MtpConnection::MtpConnection(MtpDevice *p) size=0; used=0; LIBMTP_Init(); - thread=new QThread(); + thread=new Thread(metaObject()->className()); moveToThread(thread); thread->start(); } @@ -95,7 +95,7 @@ MtpConnection::MtpConnection(MtpDevice *p) MtpConnection::~MtpConnection() { disconnectFromDevice(false); - Utils::stopThread(thread); + thread->stop(); } MusicLibraryItemRoot * MtpConnection::takeLibrary() diff --git a/devices/mtpdevice.h b/devices/mtpdevice.h index f3771a07a..d0a216393 100644 --- a/devices/mtpdevice.h +++ b/devices/mtpdevice.h @@ -34,7 +34,7 @@ #include class MusicLibraryItemRoot; -class QThread; +class Thread; class MtpDevice; class QTemporaryFile; @@ -123,7 +123,7 @@ private: void destroyData(); private: - QThread *thread; + Thread *thread; LIBMTP_mtpdevice_t *device; QMap folderMap; MusicLibraryItemRoot *library; diff --git a/devices/musicbrainz.cpp b/devices/musicbrainz.cpp index 09af3d436..0fa9c552b 100644 --- a/devices/musicbrainz.cpp +++ b/devices/musicbrainz.cpp @@ -44,11 +44,10 @@ #include #include #include -#include #include #include #include "config.h" -#include "utils.h" +#include "thread.h" #include "localize.h" #include #include @@ -137,14 +136,14 @@ static QString artistFromCreditList(MusicBrainz5::CArtistCredit *artistCredit ) MusicBrainz::MusicBrainz(const QString &device) : dev(device) { - thread=new QThread(); + thread=new Thread(metaObject()->className()); moveToThread(thread); thread->start(); } MusicBrainz::~MusicBrainz() { - Utils::stopThread(thread); + thread->stop(); } void MusicBrainz::readDisc() diff --git a/devices/musicbrainz.h b/devices/musicbrainz.h index 379baca12..f04f6ca33 100644 --- a/devices/musicbrainz.h +++ b/devices/musicbrainz.h @@ -29,7 +29,7 @@ #include "cddb.h" #include -class QThread; +class Thread; class MusicBrainz : public QObject { @@ -50,7 +50,7 @@ private: void readDisc(); private: - QThread *thread; + Thread *thread; QString dev; QString discId; CdAlbum initial; diff --git a/gui/cachesettings.cpp b/gui/cachesettings.cpp index c587958c2..8e409c55d 100644 --- a/gui/cachesettings.cpp +++ b/gui/cachesettings.cpp @@ -30,11 +30,11 @@ #include "utils.h" #include "messagebox.h" #include "config.h" +#include "thread.h" #include #include #include #include -#include #include #include #include @@ -85,11 +85,11 @@ static void deleteAll(const QString &d, const QStringList &types, int level=0) } } -CacheItemCounter::CacheItemCounter(const QString &d, const QStringList &t) +CacheItemCounter::CacheItemCounter(const QString &name, const QString &d, const QStringList &t) : dir(d) , types(t) { - thread=new QThread(); + thread=new Thread(name+" CacheCleaner"); moveToThread(thread); thread->start(); } @@ -97,7 +97,7 @@ CacheItemCounter::CacheItemCounter(const QString &d, const QStringList &t) CacheItemCounter::~CacheItemCounter() { if (thread) { - Utils::stopThread(thread); + thread->stop(); } } @@ -117,7 +117,7 @@ void CacheItemCounter::deleteAll() CacheItem::CacheItem(const QString &title, const QString &d, const QStringList &t, QTreeWidget *p) : QTreeWidgetItem(p, QStringList() << title) - , counter(new CacheItemCounter(d, t)) + , counter(new CacheItemCounter(title, d, t)) , empty(true) { connect(this, SIGNAL(getCount()), counter, SLOT(getCount()), Qt::QueuedConnection); diff --git a/gui/cachesettings.h b/gui/cachesettings.h index 01ec69aff..abb7706dd 100644 --- a/gui/cachesettings.h +++ b/gui/cachesettings.h @@ -30,14 +30,14 @@ #include class QPushButton; -class QThread; +class Thread; class CacheItemCounter : public QObject { Q_OBJECT public: - CacheItemCounter(const QString &d, const QStringList &t); + CacheItemCounter(const QString &name, const QString &d, const QStringList &t); ~CacheItemCounter(); Q_SIGNALS: @@ -50,7 +50,7 @@ public Q_SLOTS: private: QString dir; QStringList types; - QThread *thread; + Thread *thread; }; class CacheItem : public QObject, public QTreeWidgetItem diff --git a/gui/covers.cpp b/gui/covers.cpp index 4e9da7bbe..3980fe8e7 100644 --- a/gui/covers.cpp +++ b/gui/covers.cpp @@ -29,6 +29,7 @@ #include "settings.h" #include "config.h" #include "deviceoptions.h" +#include "thread.h" #ifdef TAGLIB_FOUND #include "tags.h" #endif @@ -50,7 +51,6 @@ #include #include #include -#include #include #ifdef ENABLE_KDE_SUPPORT @@ -362,7 +362,7 @@ CoverDownloader::CoverDownloader() { manager=new NetworkAccessManager(this); #ifndef ENABLE_KDE_SUPPORT // KIO is not thread safe!!! - thread=new QThread(); + thread=new Thread(metaObject()->className()); moveToThread(thread); thread->start(); #endif @@ -371,8 +371,7 @@ CoverDownloader::CoverDownloader() void CoverDownloader::stop() { #ifndef ENABLE_KDE_SUPPORT - Utils::stopThread(thread); - thread=0; + thread->stop(); #endif } @@ -738,15 +737,14 @@ QHash::Iterator CoverDownloader::findJob( CoverLocator::CoverLocator() : timer(0) { - thread=new QThread(); + thread=new Thread(metaObject()->className()); moveToThread(thread); thread->start(); } void CoverLocator::stop() { - Utils::stopThread(thread); - thread=0; + thread->stop(); } void CoverLocator::startTimer(int interval) diff --git a/gui/covers.h b/gui/covers.h index 8564eb41b..a6563c75f 100644 --- a/gui/covers.h +++ b/gui/covers.h @@ -36,7 +36,7 @@ #include "config.h" class QString; -class QThread; +class Thread; class QNetworkReply; class QMutex; class QTimer; @@ -99,7 +99,7 @@ private: private: #ifndef ENABLE_KDE_SUPPORT - QThread *thread; + Thread *thread; #endif NetworkAccessManager *manager; }; @@ -133,7 +133,7 @@ private: void startTimer(int interval); private: - QThread *thread; + Thread *thread; QTimer *timer; QList queue; }; diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 3485d9925..46fd06b73 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #ifdef ENABLE_KDE_SUPPORT diff --git a/http/httpserver.cpp b/http/httpserver.cpp index ba58f214c..bef791eb7 100644 --- a/http/httpserver.cpp +++ b/http/httpserver.cpp @@ -23,14 +23,13 @@ #include "httpserver.h" #include "httpsocket.h" -#include "utils.h" #include "tags.h" #include "settings.h" +#include "thread.h" #include #if QT_VERSION >= 0x050000 #include #endif -#include #ifdef ENABLE_KDE_SUPPORT #include K_GLOBAL_STATIC(HttpServer, instance) @@ -57,7 +56,7 @@ void HttpServer::stop() } if (thread) { - Utils::stopThread(thread); + thread->stop(); thread=0; } } @@ -78,13 +77,12 @@ bool HttpServer::readConfig() } if (thread) { - thread->quit(); - thread->deleteLater(); + thread->stop(); thread=0; } if (Settings::self()->enableHttp()) { - thread=new QThread(0); + thread=new Thread("HttpServer"); socket=new HttpSocket(addr, port, prevPort); if (socket->serverPort()!=port) { Settings::self()->saveHttpAllocatedPort(socket->serverPort()); diff --git a/http/httpserver.h b/http/httpserver.h index eaf3639b0..9be378985 100644 --- a/http/httpserver.h +++ b/http/httpserver.h @@ -29,7 +29,7 @@ #include "song.h" class HttpSocket; -class QThread; +class Thread; class QUrl; class HttpServer @@ -51,7 +51,7 @@ public: Song decodeUrl(const QString &file) const; private: - QThread *thread; + Thread *thread; HttpSocket *socket; }; diff --git a/mpd/mpdconnection.cpp b/mpd/mpdconnection.cpp index 2997e09f9..0f3449d28 100644 --- a/mpd/mpdconnection.cpp +++ b/mpd/mpdconnection.cpp @@ -33,9 +33,9 @@ #endif #include #include -#include #include #include +#include "thread.h" #include "settings.h" // #define DBUG qWarning() << "MPDConnection" << QThread::currentThreadId() @@ -183,7 +183,7 @@ MPDConnection::~MPDConnection() void MPDConnection::start() { if (!thread) { - thread=new QThread(); + thread=new Thread(metaObject()->className()); moveToThread(thread); thread->start(); } @@ -192,7 +192,7 @@ void MPDConnection::start() void MPDConnection::stop() { if (thread) { - Utils::stopThread(thread); + thread->stop(); thread=0; } } diff --git a/mpd/mpdconnection.h b/mpd/mpdconnection.h index 4234b6834..be05056a7 100644 --- a/mpd/mpdconnection.h +++ b/mpd/mpdconnection.h @@ -42,7 +42,7 @@ class MusicLibraryItemArtist; class DirViewItemRoot; class MusicLibraryItemRoot; class QTimer; -class QThread; +class Thread; class MpdSocket : public QObject { @@ -303,7 +303,7 @@ private: void toggleStopAfterCurrent(bool afterCurrent); private: - QThread *thread; + Thread *thread; long ver; QSet handlers; MPDConnectionDetails details; diff --git a/online/onlineservice.cpp b/online/onlineservice.cpp index bdf9d3431..f773e16c6 100644 --- a/online/onlineservice.cpp +++ b/online/onlineservice.cpp @@ -29,6 +29,7 @@ #include "mpdparseutils.h" #include "covers.h" #include "qtiocompressor/qtiocompressor.h" +#include "thread.h" #include #include @@ -45,10 +46,18 @@ OnlineMusicLoader::OnlineMusicLoader(const QUrl &src) , stopRequested(false) , lastProg(-1) { - moveToThread(this); + connect(this, SIGNAL(load()), this, SLOT(doLoad())); + thread=new Thread(metaObject()->className()); + moveToThread(thread); + thread->start(); } -void OnlineMusicLoader::run() +void OnlineMusicLoader::start() +{ + emit load(); +} + +void OnlineMusicLoader::doLoad() { if (library) { delete library; @@ -62,17 +71,13 @@ void OnlineMusicLoader::run() downloadJob=network->get(source); connect(downloadJob, SIGNAL(finished()), SLOT(downloadFinished())); connect(downloadJob, SIGNAL(downloadProgress(qint64,qint64)), SLOT(downloadProgress(qint64,qint64))); - exec(); } } void OnlineMusicLoader::stop() { - if (downloadJob) { - quit(); - } stopRequested=true; - Utils::stopThread(this); + thread->stop(); } MusicLibraryItemRoot * OnlineMusicLoader::takeLibrary() @@ -143,7 +148,6 @@ void OnlineMusicLoader::downloadFinished() } else { emit error(i18n("Failed to download")); } - quit(); } void OnlineMusicLoader::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) @@ -287,6 +291,7 @@ void OnlineService::loaderError(const QString &msg) { lProgress=0; setStatusMessage(msg); + stopLoader(); } void OnlineService::loaderstatus(const QString &msg, int prog) diff --git a/online/onlineservice.h b/online/onlineservice.h index f31e876de..9c319440e 100644 --- a/online/onlineservice.h +++ b/online/onlineservice.h @@ -30,21 +30,21 @@ #include "localize.h" #include #include -#include +class Thread; class NetworkAccessManager; class OnlineServicesModel; class QNetworkReply; class QXmlStreamReader; -class OnlineMusicLoader : public QThread, public MusicLibraryProgressMonitor +class OnlineMusicLoader : public QObject, public MusicLibraryProgressMonitor { Q_OBJECT public: OnlineMusicLoader(const QUrl &src); - void run(); + void start(); void stop(); bool wasStopped() const { return stopRequested; } MusicLibraryItemRoot * takeLibrary(); @@ -52,11 +52,13 @@ public: void setCacheFileName(const QString &c) { cache=c; } Q_SIGNALS: + void load(); void status(const QString &msg, int prog); void error(const QString &msg); void loaded(); private Q_SLOTS: + void doLoad(); void downloadFinished(); void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); @@ -68,6 +70,7 @@ private: void progressReport(const QString &str, int pc); protected: + Thread *thread; QUrl source; QString cache; MusicLibraryItemRoot *library; diff --git a/replaygain/jobcontroller.cpp b/replaygain/jobcontroller.cpp index defc2d0c0..8226b6740 100644 --- a/replaygain/jobcontroller.cpp +++ b/replaygain/jobcontroller.cpp @@ -22,7 +22,7 @@ */ #include "jobcontroller.h" -#include "utils.h" +#include "thread.h" #ifdef ENABLE_KDE_SUPPORT #include K_GLOBAL_STATIC(JobController, instance) @@ -45,7 +45,7 @@ Job::~Job() void Job::start() { if (!thread) { - thread=new QThread(); + thread=new Thread(metaObject()->className()); moveToThread(thread); thread->start(); connect(this, SIGNAL(exec()), this, SLOT(run()), Qt::QueuedConnection); @@ -57,7 +57,7 @@ void Job::stop() { requestAbort(); if (thread) { - Utils::stopThread(thread); + thread->stop(); thread=0; } deleteLater(); diff --git a/replaygain/jobcontroller.h b/replaygain/jobcontroller.h index e0ecd5699..99a6422e2 100644 --- a/replaygain/jobcontroller.h +++ b/replaygain/jobcontroller.h @@ -24,9 +24,11 @@ #ifndef JOB_CONTROLLER #define JOB_CONTROLLER -#include +#include -class Job : public QThread +class Thread; + +class Job : public QObject { Q_OBJECT public: @@ -51,7 +53,7 @@ protected: bool abortRequested; bool finished; private: - QThread *thread; + Thread *thread; }; class JobController : public QObject diff --git a/support/CMakeLists.txt b/support/CMakeLists.txt index f47ac64e4..055a854e7 100644 --- a/support/CMakeLists.txt +++ b/support/CMakeLists.txt @@ -10,6 +10,7 @@ SET( SUPPORT_SRCS spinner.cpp messagebox.cpp inputdialog.cpp + thread.cpp ) SET( SUPPORT_MOC_HDRS @@ -17,6 +18,7 @@ SET( SUPPORT_MOC_HDRS actioncollection.h fancytabwidget.h messagewidget.h + thread.h ) if (NOT WIN32) diff --git a/support/thread.cpp b/support/thread.cpp new file mode 100644 index 000000000..ca6fbf22d --- /dev/null +++ b/support/thread.cpp @@ -0,0 +1,62 @@ +/* + * Cantata + * + * Copyright (c) 2011-2013 Craig Drummond + * + * ---- + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "thread.h" +//#include +#ifdef ENABLE_KDE_SUPPORT +#include +K_GLOBAL_STATIC(ThreadCleaner, instance) +#endif + +ThreadCleaner * ThreadCleaner::self() +{ + #ifdef ENABLE_KDE_SUPPORT + return instance; + #else + static ThreadCleaner *instance=0; + if(!instance) { + instance=new ThreadCleaner; + } + return instance; + #endif +} + +void ThreadCleaner::threadFinished() +{ + QThread *thread=qobject_cast(sender()); + if (thread) { + thread->deleteLater(); + } +} + +Thread::Thread(const QString &name, QObject *p) + : QThread(p) +{ + setObjectName(name); + connect(this, SIGNAL(finished()), ThreadCleaner::self(), SLOT(threadFinished())); +} + +Thread::~Thread() +{ +// qWarning() << objectName() << "destroyed"; +} diff --git a/support/thread.h b/support/thread.h new file mode 100644 index 000000000..4357837e7 --- /dev/null +++ b/support/thread.h @@ -0,0 +1,55 @@ +/* + * Cantata + * + * Copyright (c) 2011-2013 Craig Drummond + * + * ---- + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef THREAD_H +#define THREAD_H + +#include + +// ThreadCleaner *needs* to resde in the GUI thread. When a 'Thread' is created it will connect +// its finished signal to threadFinished(), this then calls deleteLater() to ensure that the +// thread is finished before it is deleted - and is deleted in the gui thread. +class ThreadCleaner : public QObject +{ + Q_OBJECT +public: + static ThreadCleaner * self(); + ThreadCleaner() { } + ~ThreadCleaner() { } +public Q_SLOTS: + void threadFinished(); +}; + +class Thread : public QThread +{ + Q_OBJECT +public: + Thread(const QString &name, QObject *p=0); + virtual ~Thread(); + +public Q_SLOTS: + void stop() { quit(); } +}; + +#endif + diff --git a/support/utils.cpp b/support/utils.cpp index b5a205fbe..ddece7559 100644 --- a/support/utils.cpp +++ b/support/utils.cpp @@ -238,12 +238,6 @@ void Utils::msleep(int msecs) Thread::msleep(msecs); } -void Utils::stopThread(QThread *thread) -{ - thread->quit(); - thread->connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); -} - #ifndef ENABLE_KDE_SUPPORT // Copied from KDE... START #include diff --git a/support/utils.h b/support/utils.h index 3044bdef9..4db980327 100644 --- a/support/utils.h +++ b/support/utils.h @@ -35,7 +35,6 @@ #endif class QString; -class QThread; namespace Utils { @@ -56,7 +55,6 @@ namespace Utils extern bool createDir(const QString &dir, const QString &base, const char *groupName="users"); extern void msleep(int msecs); inline void sleep() { msleep(100); } - extern void stopThread(QThread *thread); #ifdef ENABLE_KDE_SUPPORT inline QString findExe(const QString &appname, const QString &pathstr=QString()) { return KStandardDirs::findExe(appname, pathstr); }