00001 /* 00002 * Copyright (C) 2006 Thomas Zander <zander@kde.org> 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Library General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this library; see the file COPYING.LIB. If not, write to 00016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 */ 00019 #include "ActionJob_p.h" 00020 #include "KoAction.h" 00021 00022 #include <QCoreApplication> 00023 #include <QEvent> 00024 #include <QThread> 00025 00026 class ActionJobEvent : public QEvent { 00027 public: 00028 ActionJobEvent() : QEvent(QEvent::User) {} 00029 }; 00030 00031 ActionJob::ActionJob(KoAction *parent, Enable enable, QVariant *params) 00032 : Job(parent), 00033 m_action(parent), 00034 m_enable(enable), 00035 m_started(false), 00036 m_params(params) 00037 { 00038 } 00039 00040 void ActionJob::run() { 00041 m_started = true; 00042 m_action->doAction(m_params); 00043 switch(m_enable) { 00044 case EnableOn: 00045 m_action->setEnabled(true); 00046 break; 00047 case EnableOff: 00048 m_action->setEnabled(false); 00049 break; 00050 case EnableNoChange: 00051 break; 00052 } 00053 if(QThread::currentThread() == QCoreApplication::instance()->thread()) 00054 m_action->doActionUi(m_params); 00055 else { 00056 // do it in the main thread. 00057 QCoreApplication::postEvent(this, new ActionJobEvent()); 00058 m_semaphore.acquire(); 00059 } 00060 } 00061 00062 bool ActionJob::event(QEvent *e) { 00063 ActionJobEvent *event = dynamic_cast<ActionJobEvent*> (e); 00064 if(event) { 00065 m_action->doActionUi(m_params); 00066 m_semaphore.release(); 00067 return true; 00068 } 00069 return QObject::event(e); 00070 } 00071 00072 #include "ActionJob_p.moc"