• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

digikam

actionthread.cpp

Go to the documentation of this file.
00001 /* ============================================================
00002  *
00003  * This file is a part of digiKam project
00004  * http://www.digikam.org
00005  *
00006  * Date        : 2009-02-06
00007  * Description : Thread actions manager.
00008  *
00009  * Copyright (C) 2009 by Gilles Caulier <caulier dot gilles at gmail dot com>
00010  *
00011  * This program is free software; you can redistribute it
00012  * and/or modify it under the terms of the GNU General
00013  * Public License as published by the Free Software Foundation;
00014  * either version 2, or (at your option)
00015  * any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * ============================================================ */
00023 
00024 #include "actionthread.moc"
00025 
00026 // C ANSI includes
00027 
00028 extern "C"
00029 {
00030 #include <unistd.h>
00031 }
00032 
00033 // Qt includes
00034 
00035 #include <QFileInfo>
00036 #include <QMutex>
00037 #include <QMutexLocker>
00038 #include <QWaitCondition>
00039 
00040 // KDE includes
00041 
00042 #include <klocale.h>
00043 #include <kstandarddirs.h>
00044 #include <kdebug.h>
00045 
00046 // Local includes
00047 
00048 #include "dimg.h"
00049 
00050 namespace Digikam
00051 {
00052 
00053 class ActionThreadPriv
00054 {
00055 public:
00056 
00057     ActionThreadPriv()
00058     {
00059         tool               = 0;
00060         running            = false;
00061         cancel             = false;
00062         exifSetOrientation = true;
00063     }
00064 
00065     class Task
00066     {
00067         public:
00068 
00069             Task(){};
00070 
00071             AssignedBatchTools item;
00072     };
00073 
00074     bool            running;
00075     bool            cancel;
00076     bool            exifSetOrientation;
00077 
00078     QMutex          mutex;
00079 
00080     QWaitCondition  condVar;
00081 
00082     QList<Task*>    todo;
00083 
00084     KUrl            workingUrl;
00085 
00086     BatchTool*      tool;
00087 };
00088 
00089 ActionThread::ActionThread(QObject *parent)
00090             : QThread(parent), d(new ActionThreadPriv)
00091 {
00092     qRegisterMetaType<ActionData>();
00093 }
00094 
00095 ActionThread::~ActionThread()
00096 {
00097     // cancel the thread
00098     cancel();
00099     // wait for the thread to finish
00100     wait();
00101 
00102     delete d;
00103 }
00104 
00105 void ActionThread::setWorkingUrl(const KUrl& workingUrl)
00106 {
00107     d->workingUrl = workingUrl;
00108 }
00109 
00110 void ActionThread::setExifSetOrientation(bool set)
00111 {
00112     d->exifSetOrientation = set;
00113 }
00114 
00115 void ActionThread::processFile(const AssignedBatchTools& item)
00116 {
00117     ActionThreadPriv::Task *t = new ActionThreadPriv::Task;
00118     t->item                   = item;
00119 
00120     QMutexLocker lock(&d->mutex);
00121     d->todo << t;
00122     d->condVar.wakeAll();
00123 }
00124 
00125 void ActionThread::cancel()
00126 {
00127     d->cancel  = true;
00128     if (d->tool) d->tool->cancel();
00129 
00130     QMutexLocker lock(&d->mutex);
00131     d->todo.clear();
00132     d->running = false;
00133     d->condVar.wakeAll();
00134 }
00135 
00136 void ActionThread::run()
00137 {
00138     d->running = true;
00139 
00140     while (d->running)
00141     {
00142         ActionThreadPriv::Task *t = 0;
00143         {
00144             QMutexLocker lock(&d->mutex);
00145             if (!d->todo.isEmpty())
00146                 t = d->todo.takeFirst();
00147             else
00148                 d->condVar.wait(&d->mutex);
00149         }
00150 
00151         if (t)
00152         {
00153             ActionData ad1;
00154             ad1.fileUrl = t->item.itemUrl;
00155             ad1.status  = ActionData::BatchStarted;
00156             emit starting(ad1);
00157 
00158             // Loop with all batch tools operations to apply on item.
00159 
00160             d->cancel    = false;
00161             int index    = 0;
00162             bool success = false;
00163             KUrl outUrl  = t->item.itemUrl;
00164             KUrl inUrl;
00165             KUrl::List tmp2del;
00166             DImg tmpImage;
00167 
00168             for (BatchToolMap::const_iterator it = t->item.toolsMap.constBegin();
00169                  !d->cancel && (it != t->item.toolsMap.constEnd()) ; ++it)
00170             {
00171                 index                      = it.key();
00172                 BatchToolSet set           = it.value();
00173                 d->tool                    = set.tool;
00174                 BatchToolSettings settings = set.settings;
00175                 inUrl                      = outUrl;
00176 
00177                 kDebug() << "Tool Index: " << index;
00178 
00179                 ActionData ad2;
00180                 ad2.fileUrl = t->item.itemUrl;
00181                 ad2.status  = ActionData::TaskStarted;
00182                 ad2.index   = index;
00183                 emit finished(ad2);
00184 
00185                 d->tool->setImageData(tmpImage);
00186                 d->tool->setWorkingUrl(d->workingUrl);
00187                 d->tool->setExifSetOrientation(d->exifSetOrientation);
00188                 d->tool->setLastChainedTool(index == t->item.toolsMap.count());
00189                 d->tool->setInputUrl(inUrl);
00190                 d->tool->setSettings(settings);
00191                 d->tool->setInputUrl(inUrl);
00192                 d->tool->setOutputUrlFromInputUrl();
00193 
00194                 outUrl   = d->tool->outputUrl();
00195                 success  = d->tool->apply();
00196                 tmpImage = d->tool->imageData();
00197                 tmp2del.append(outUrl);
00198                 d->tool  = 0;
00199 
00200                 if (success && !d->cancel)
00201                 {
00202                     ActionData ad3;
00203                     ad3.fileUrl = t->item.itemUrl;
00204                     ad3.status  = ActionData::TaskDone;
00205                     ad3.index   = index;
00206                     emit finished(ad3);
00207                 }
00208                 else
00209                 {
00210                     ActionData ad4;
00211                     ad4.fileUrl = t->item.itemUrl;
00212                     ad4.status  = ActionData::TaskFailed;
00213                     ad4.index   = index;
00214                     emit finished(ad4);
00215 
00216                     ActionData ad5;
00217                     ad5.fileUrl = t->item.itemUrl;
00218                     ad5.status  = ActionData::BatchFailed;
00219                     emit finished(ad5);
00220 
00221                     break;
00222                 }
00223             }
00224 
00225             if (success && !d->cancel)
00226             {
00227                 // if success, we don't remove last output tmp url.
00228                 tmp2del.removeAll(outUrl);
00229 
00230                 ActionData ad6;
00231                 ad6.fileUrl = t->item.itemUrl;
00232                 ad6.destUrl = outUrl;
00233                 ad6.status  = ActionData::BatchDone;
00234                 emit finished(ad6);
00235             }
00236 
00237             // Clean up all tmp url.
00238 
00239             for (KUrl::List::const_iterator it = tmp2del.constBegin(); it != tmp2del.constEnd() ; ++it)
00240             {
00241                 unlink(QFile::encodeName((*it).toLocalFile()));
00242             }
00243         }
00244 
00245         delete t;
00246     }
00247 }
00248 
00249 }  // namespace Digikam

digikam

Skip menu "digikam"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • digikam
Generated for API Reference by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal