File indexing completed on 2024-04-21 15:42:50

0001 /***************************************************************************
0002     This is the new synchronizer of Smb4K.
0003                              -------------------
0004     begin                : Fr Feb 04 2011
0005     copyright            : (C) 2011-2019 by Alexander Reinholdt
0006     email                : alexander.reinholdt@kdemail.net
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *   This program is free software; you can redistribute it and/or modify  *
0011  *   it under the terms of the GNU General Public License as published by  *
0012  *   the Free Software Foundation; either version 2 of the License, or     *
0013  *   (at your option) any later version.                                   *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful, but   *
0016  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
0018  *   General Public License for more details.                              *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program; if not, write to the                         *
0022  *   Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,*
0023  *   MA 02110-1335, USA                                                    *
0024  ***************************************************************************/
0025 
0026 #ifdef HAVE_CONFIG_H
0027 #include <config.h>
0028 #endif
0029 
0030 // application specific includes
0031 #include "smb4ksynchronizer.h"
0032 #include "smb4ksynchronizer_p.h"
0033 #include "smb4knotification.h"
0034 #include "smb4kglobal.h"
0035 #include "smb4kshare.h"
0036 
0037 // Qt includes
0038 #include <QTimer>
0039 #include <QDebug>
0040 #include <QFile>
0041 #include <QDir>
0042 #include <QApplication>
0043 
0044 // KDE includes
0045 #include <KCoreAddons/KShell>
0046 
0047 using namespace Smb4KGlobal;
0048 
0049 Q_GLOBAL_STATIC(Smb4KSynchronizerStatic, p);
0050 
0051 
0052 Smb4KSynchronizer::Smb4KSynchronizer(QObject *parent)
0053 : KCompositeJob(parent), d(new Smb4KSynchronizerPrivate)
0054 {
0055   setAutoDelete(false);
0056   connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()), SLOT(slotAboutToQuit()));
0057 }
0058 
0059 
0060 Smb4KSynchronizer::~Smb4KSynchronizer()
0061 {
0062 }
0063 
0064 
0065 Smb4KSynchronizer *Smb4KSynchronizer::self()
0066 {
0067   return &p->instance;
0068 }
0069 
0070 
0071 void Smb4KSynchronizer::synchronize(const SharePtr &share)
0072 {
0073   if (!isRunning(share))
0074   {
0075     // Create a new job, add it to the subjobs and register it
0076     // with the job tracker.
0077     Smb4KSyncJob *job = new Smb4KSyncJob(this);
0078     job->setObjectName(QString("SyncJob_%1").arg(share->canonicalPath()));
0079     job->setupSynchronization(share);
0080     
0081     connect(job, SIGNAL(result(KJob*)), SLOT(slotJobFinished(KJob*)));
0082     connect(job, SIGNAL(aboutToStart(QString)), SIGNAL(aboutToStart(QString)));
0083     connect(job, SIGNAL(finished(QString)), SIGNAL(finished(QString)));
0084 
0085     addSubjob(job);
0086     
0087     job->start();
0088   }
0089 }
0090 
0091 
0092 bool Smb4KSynchronizer::isRunning()
0093 {
0094   return hasSubjobs();
0095 }
0096 
0097 
0098 bool Smb4KSynchronizer::isRunning(const SharePtr &share)
0099 {
0100   bool running = false;
0101 
0102   for (int i = 0; i < subjobs().size(); ++i)
0103   {
0104     if (QString::compare(QString("SyncJob_%1").arg(share->canonicalPath()), subjobs().at(i)->objectName()) == 0)
0105     {
0106       running = true;
0107       break;
0108     }
0109     else
0110     {
0111       continue;
0112     }
0113   }
0114   
0115   return running;
0116 }
0117 
0118 
0119 void Smb4KSynchronizer::abort(const SharePtr &share)
0120 {
0121   if (share && !share.isNull())
0122   {
0123     for (KJob *job : subjobs())
0124     {
0125       if (QString("SyncJob_%1").arg(share->canonicalPath()) == job->objectName())
0126       {
0127         job->kill(KJob::EmitResult);
0128         break;
0129       }
0130     }
0131   }
0132   else
0133   {
0134     QListIterator<KJob *> it(subjobs());
0135       
0136     while (it.hasNext())
0137     {
0138       it.next()->kill(KJob::EmitResult);
0139     }
0140   }
0141 }
0142 
0143 
0144 void Smb4KSynchronizer::start()
0145 {
0146   QTimer::singleShot(0, this, SLOT(slotStartJobs()));
0147 }
0148 
0149 /////////////////////////////////////////////////////////////////////////////
0150 //   SLOT IMPLEMENTATIONS
0151 /////////////////////////////////////////////////////////////////////////////
0152 
0153 void Smb4KSynchronizer::slotStartJobs()
0154 {
0155   // FIXME: Not implemented yet. I do not see a use case at the moment.
0156 }
0157 
0158 
0159 void Smb4KSynchronizer::slotJobFinished(KJob *job)
0160 {
0161   // Remove the job.
0162   removeSubjob(job);
0163 }
0164 
0165 
0166 void Smb4KSynchronizer::slotAboutToQuit()
0167 {
0168   abort();
0169 }
0170