Merge branch '1.0' of /home/carl/git/dvdomatic into 1.0
[dcpomatic.git] / src / lib / job_manager.cc
index a166b5924dd0e3c472c89bdcc24bdd378c07797a..a841fa60bd553698a873e7105d5f2086e8e4040b 100644 (file)
 #include "job_manager.h"
 #include "job.h"
 #include "cross.h"
+#include "ui_signaller.h"
 
-using namespace std;
-using namespace boost;
+using std::string;
+using std::list;
+using std::cout;
+using boost::shared_ptr;
+using boost::weak_ptr;
 
 JobManager* JobManager::_instance = 0;
 
 JobManager::JobManager ()
+       : _last_active_jobs (false)
 {
        boost::thread (boost::bind (&JobManager::scheduler, this));
 }
 
-void
+shared_ptr<Job>
 JobManager::add (shared_ptr<Job> j)
 {
-       boost::mutex::scoped_lock lm (_mutex);
-       _jobs.push_back (j);
-}
+       {
+               boost::mutex::scoped_lock lm (_mutex);
+               _jobs.push_back (j);
+       }
 
-void
-JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
-{
-       boost::mutex::scoped_lock lm (_mutex);
-       list<shared_ptr<Job> >::iterator i = find (_jobs.begin(), _jobs.end(), after);
-       assert (i != _jobs.end ());
-       ++i;
-       _jobs.insert (i, j);
+       if (ui_signaller) {
+               ui_signaller->emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
+       }
+       
+       return j;
 }
 
 list<shared_ptr<Job> >
@@ -73,29 +76,56 @@ JobManager::work_to_do () const
        return i != _jobs.end ();
 }
 
+bool
+JobManager::errors () const
+{
+       boost::mutex::scoped_lock lm (_mutex);
+       for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
+               if ((*i)->finished_in_error ()) {
+                       return true;
+               }
+       }
+
+       return false;
+}      
+
 void
 JobManager::scheduler ()
 {
        while (1) {
+
+               bool active_jobs = false;
+
                {
                        boost::mutex::scoped_lock lm (_mutex);
-                       int running = 0;
-                       shared_ptr<Job> first_new;
                        for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
+
+                               if (!(*i)->finished ()) {
+                                       active_jobs = true;
+                               }
+                               
                                if ((*i)->running ()) {
-                                       ++running;
-                               } else if (!(*i)->finished () && first_new == 0) {
-                                       first_new = *i;
+                                       /* Something is already happening */
+                                       break;
                                }
-
-                               if (running == 0 && first_new) {
-                                       first_new->start ();
+                               
+                               if ((*i)->is_new()) {
+                                       (*i)->start ();
+                                       
+                                       /* Only start one job at once */
                                        break;
                                }
                        }
                }
 
-               dvdomatic_sleep (1);
+               if (active_jobs != _last_active_jobs) {
+                       _last_active_jobs = active_jobs;
+                       if (ui_signaller) {
+                               ui_signaller->emit (boost::bind (boost::ref (ActiveJobsChanged), active_jobs));
+                       }
+               }
+
+               dcpomatic_sleep (1);
        }
 }