kitchensync

clientmanager.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2003 Mathias Froehlich <Mathias.Froehlich@web.de>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include <qobject.h>
00023 #include <qapplication.h>
00024 #include <qthread.h>
00025 
00026 #include <error.h>
00027 #include <progress.h>
00028 
00029 #include "clientthread.h"
00030 #include "clientmanager.h"
00031 
00032 using namespace Threaded;
00033 
00037 ClientManager::ClientManager( QObject* parent, const char* name )
00038   : QObject( parent, name ), mWorker( this )
00039 {
00040   mWorker.mLock.lock();
00041   start();
00042 }
00043 
00047 ClientManager::~ClientManager()
00048 {
00049   // Terminate the worker thread if required.
00050   if ( !finished() ) {
00051     // Tell the thread to terminate ...
00052     terminateThread();
00053     // ... and give it 5 seconds to terminate.
00054     if ( !wait( 5000 ) ) {
00055       // If it has not worked, try the hard way ...
00056       terminate();
00057       wait();
00058     }
00059   }
00060 }
00061 
00066 void ClientManager::run()
00067 {
00068   // Call the real working routine.
00069   mWorker.run();
00070 }
00071 
00077 bool ClientManager::isIdle()
00078 {
00079   return !mWorker.mLock.locked();
00080 }
00081 
00088 bool ClientManager::readSyncees()
00089 {
00090   if ( mWorker.mLock.tryLock() ) {
00091     mWorker.mFilename = QString::null;
00092     mWorker.mCommand = ClientThread::ReadSyncees;
00093     mWorker.mLock.unlock();
00094     mWorker.mWait.wakeOne();
00095     return true;
00096   } else
00097     return false;
00098 }
00099 
00106 bool ClientManager::writeSyncees()
00107 {
00108   if ( mWorker.mLock.tryLock() ) {
00109     mWorker.mFilename = QString::null;
00110     mWorker.mCommand = ClientThread::WriteSyncees;
00111     mWorker.mLock.unlock();
00112     mWorker.mWait.wakeOne();
00113     return true;
00114   } else
00115     return false;
00116 }
00117 
00125 bool ClientManager::connectDevice()
00126 {
00127   if ( mWorker.mLock.tryLock() ) {
00128     mWorker.mFilename = QString::null;
00129     mWorker.mCommand = ClientThread::Connect;
00130     mWorker.mLock.unlock();
00131     mWorker.mWait.wakeOne();
00132     return true;
00133   } else
00134     return false;
00135 }
00136 
00144 bool ClientManager::disconnectDevice()
00145 {
00146   if ( mWorker.mLock.tryLock() ) {
00147     mWorker.mFilename = QString::null;
00148     mWorker.mCommand = ClientThread::Disconnect;
00149     mWorker.mLock.unlock();
00150     mWorker.mWait.wakeOne();
00151     return true;
00152   } else
00153     return false;
00154 }
00155 
00159 bool ClientManager::terminateThread()
00160 {
00161   mWorker.mCancel = true;
00162   if ( mWorker.mLock.tryLock() ) {
00163     mWorker.mFilename = QString::null;
00164     mWorker.mCommand = ClientThread::TerminateThread;
00165     mWorker.mLock.unlock();
00166     mWorker.mWait.wakeOne();
00167     return true;
00168   } else
00169     return false;
00170 }
00171 
00176 void ClientManager::cancelJob()
00177 {
00178   mWorker.mCancel = true;
00179 }
00180 
00184 void ClientManager::customEvent( QCustomEvent* ce )
00185 {
00186   if ( ce->type() == QEvent::Type( ClientThread::ProgressEvent ) ) {
00187     // This should occur most often therefore it is the first ...
00188     // FIXME
00189 //     KSync::Progress* p = dynamic_cast<KSync::Progress *>( ce->data() );
00190     KSync::Progress* p = static_cast<KSync::Progress *>( ce->data() );
00191     if ( p )
00192       emit signalProgress( *p );
00193     else
00194       emit signalError( KSync::Error( "Internal error" )  );
00195 
00196   } else if ( ce->type() == QEvent::Type( ClientThread::ErrorEvent ) ) {
00197     // FIXME
00198 //     KSync::Error* e = dynamic_cast<KSync::Error *>( ce->data() );
00199     KSync::Error* e = static_cast<KSync::Error *>( ce->data() );
00200     if ( e )
00201       emit signalError( *e );
00202     else
00203       emit signalError( KSync::Error( "Internal error" )  );
00204 
00205   } else if ( ce->type() == QEvent::Type( ClientThread::FinishedEvent ) ) {
00206     emit signalFinished();
00207 
00208   } else if ( ce->type() == QEvent::Type( ClientThread::TerminatedEvent ) ) {
00209     // Make sure that we are really done ...
00210     wait();
00211     // ...
00212     emit signalTerminated();
00213     mWorker.mLock.unlock();
00214   }
00215 }
00216 #include "clientmanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys