File indexing completed on 2024-02-25 05:07:05

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
0004    Copyright (C) 2009 Lukas Appelhans <l.appelhans@gmx.de>
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 */
0011 
0012 #ifndef JOB_H
0013 #define JOB_H
0014 
0015 /**
0016  *  @brief Job class
0017  *
0018  *  We want to abstract this common interface in order to simplify the
0019  *  Scheduler code. A Job can be either a Transfer or a search through the net.
0020  *  It is basically something you execute in background and that the scheduler
0021  *  can decide to start, stop or cancel. In this way we don't expose the complex
0022  *  API of a Transfer (or a Search), to the scheduler.
0023  *  By definition a job must always belong to a JobQueue (see jobqueue.h).
0024  **/
0025 
0026 #include "kget_export.h"
0027 #include <QObject>
0028 #include <QPixmap>
0029 
0030 class QDomNode;
0031 
0032 class Scheduler;
0033 class JobQueue;
0034 
0035 class KGET_EXPORT Job : public QObject
0036 {
0037     Q_OBJECT
0038 public:
0039     /**
0040      * The status property describes the current job status
0041      */
0042     enum Status {
0043         Running = 0, /// The job is being executed
0044         Stopped = 2, /// The job is stopped
0045         Aborted = 3, /// The job is stopped, but this also indicates that it
0046                      /// stopped because an error occurred
0047         Finished = 4, /// The job exited from its Running state successfully
0048         FinishedKeepAlive = 5, /// The job exited from its Running state successfully
0049                                /// but wants to be restarted by the scheduler
0050         Moving = 6 /// The associated files to that job (e.g. Download) are
0051                    /// moved to a different location
0052     };
0053 
0054     /**
0055      * The policy property describes how the scheduler should manage this job.
0056      */
0057     enum Policy {
0058         Start, /// The scheduler should start this job even if its queue
0059                /// isn't in a Running status
0060         Stop, /// The scheduler shouldn't never start this job, even if
0061               /// if its queue is in a Running status
0062         None /// The scheduler should start this job depending on its
0063              /// queue status
0064     };
0065     /**
0066      * Describes different types of errors and how the scheduler should manage them.
0067      */
0068     enum ErrorType { AutomaticRetry, ManualSolve, NotSolveable };
0069     struct Error {
0070         int id;
0071         QString text;
0072         QString iconName;
0073         ErrorType type;
0074     };
0075     Job(Scheduler *scheduler, JobQueue *parent);
0076     ~Job() override;
0077 
0078     // Job commands
0079     virtual void start() = 0;
0080     virtual void stop() = 0;
0081 
0082     JobQueue *jobQueue()
0083     {
0084         return m_jobQueue;
0085     }
0086 
0087     // Job properties
0088     void setStatus(Status jobStatus);
0089     void setPolicy(Policy jobPolicy);
0090     void setError(const QString &text, const QString &iconName, ErrorType type = AutomaticRetry, int errorId = -1);
0091 
0092     Status status() const
0093     {
0094         return m_status;
0095     }
0096     Status startStatus() const
0097     {
0098         return m_startStatus;
0099     }
0100     Policy policy() const
0101     {
0102         return m_policy;
0103     }
0104     Error error() const
0105     {
0106         return m_error;
0107     }
0108 
0109     virtual int elapsedTime() const = 0;
0110     virtual int remainingTime() const = 0;
0111     virtual bool isStalled() const = 0;
0112     virtual bool isWorking() const = 0;
0113 
0114     virtual void resolveError(int errorId);
0115 
0116 protected:
0117     Scheduler *scheduler() const
0118     {
0119         return m_scheduler;
0120     }
0121 
0122     void read(QDomNode *n);
0123     void write(QDomNode *n);
0124 
0125     void setStartStatus(Status jobStatus);
0126 
0127     /**
0128      * This one posts a job event to the scheduler
0129      */
0130     void postJobEvent(Status); // do we need a JobEvent instead of JobStatus?
0131 
0132     JobQueue *m_jobQueue;
0133     Scheduler *m_scheduler;
0134 
0135 private:
0136     Status m_status;
0137     // our status when KGet is started
0138     Status m_startStatus;
0139     Policy m_policy;
0140     Error m_error;
0141 };
0142 
0143 #endif