+void RenderWidget::setRenderJob(const QString &dest, int progress)
+{
+ RenderJobItem *item;
+ QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
+ if (!existing.isEmpty()) {
+ item = static_cast<RenderJobItem*> (existing.at(0));
+ } else {
+ item = new RenderJobItem(m_view.running_jobs, QStringList() << QString() << dest);
+ if (progress == 0) {
+ item->setStatus(WAITINGJOB);
+ }
+ }
+ item->setData(1, ProgressRole, progress);
+ item->setStatus(RUNNINGJOB);
+ if (progress == 0) {
+ item->setIcon(0, KIcon("system-run"));
+ item->setData(1, TimeRole, QTime::currentTime());
+ slotCheckJob();
+ } else {
+ QTime startTime = item->data(1, TimeRole).toTime();
+ int seconds = startTime.secsTo(QTime::currentTime());;
+ const QString t = i18n("Estimated time %1", QTime().addSecs(seconds * (100 - progress) / progress).toString("hh:mm:ss"));
+ item->setData(1, Qt::UserRole, t);
+ }
+}
+
+void RenderWidget::setRenderStatus(const QString &dest, int status, const QString &error)
+{
+ RenderJobItem *item;
+ QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
+ if (!existing.isEmpty()) item = static_cast<RenderJobItem*> (existing.at(0));
+ else {
+ item = new RenderJobItem(m_view.running_jobs, QStringList() << QString() << dest);
+ }
+ if (status == -1) {
+ // Job finished successfully
+ item->setStatus(FINISHEDJOB);
+ QTime startTime = item->data(1, TimeRole).toTime();
+ int seconds = startTime.secsTo(QTime::currentTime());
+ const QTime tm = QTime().addSecs(seconds);
+ const QString t = i18n("Rendering finished in %1", tm.toString("hh:mm:ss"));
+ item->setData(1, Qt::UserRole, t);
+ QString itemGroup = item->data(0, Qt::UserRole).toString();
+ if (itemGroup == "dvd") {
+ emit openDvdWizard(item->text(1));
+ } else if (itemGroup == "websites") {
+ QString url = item->metadata();
+ if (!url.isEmpty()) new KRun(url, this);
+ }
+ } else if (status == -2) {
+ // Rendering crashed
+ item->setStatus(FAILEDJOB);
+ m_view.error_log->append(i18n("<strong>Rendering of %1 crashed</strong><br />", dest));
+ m_view.error_log->append(error);
+ m_view.error_log->append("<hr />");
+ m_view.error_box->setVisible(true);
+ } else if (status == -3) {
+ // User aborted job
+ item->setStatus(ABORTEDJOB);
+ }
+ slotCheckJob();
+ checkRenderStatus();
+}
+
+void RenderWidget::slotAbortCurrentJob()
+{
+ RenderJobItem *current = static_cast<RenderJobItem*> (m_view.running_jobs->currentItem());
+ if (current) {
+ if (current->status() == RUNNINGJOB)
+ emit abortProcess(current->text(1));
+ else {
+ delete current;
+ slotCheckJob();
+ checkRenderStatus();
+ }
+ }
+}
+
+void RenderWidget::slotStartCurrentJob()
+{
+ RenderJobItem *current = static_cast<RenderJobItem*> (m_view.running_jobs->currentItem());
+ if (current && current->status() == WAITINGJOB)
+ startRendering(current);
+ m_view.start_job->setEnabled(false);
+}
+
+void RenderWidget::slotCheckJob()
+{
+ bool activate = false;
+ RenderJobItem *current = static_cast<RenderJobItem*> (m_view.running_jobs->currentItem());
+ if (current) {
+ if (current->status() == RUNNINGJOB) {
+ m_view.abort_job->setText(i18n("Abort Job"));
+ m_view.start_job->setEnabled(false);
+ } else {
+ m_view.abort_job->setText(i18n("Remove Job"));
+ m_view.start_job->setEnabled(current->status() == WAITINGJOB);
+ }
+ activate = true;
+ }
+ m_view.abort_job->setEnabled(activate);
+ /*
+ for (int i = 0; i < m_view.running_jobs->topLevelItemCount(); ++i) {
+ current = static_cast<RenderJobItem*>(m_view.running_jobs->topLevelItem(i));
+ if (current == static_cast<RenderJobItem*> (m_view.running_jobs->currentItem())) {
+ current->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 3));
+ } else current->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
+ }*/
+}
+
+void RenderWidget::slotCLeanUpJobs()
+{
+ int ix = 0;
+ RenderJobItem *current = static_cast<RenderJobItem*> (m_view.running_jobs->topLevelItem(ix));
+ while (current) {
+ if (current->status() == FINISHEDJOB)
+ delete current;
+ else ix++;
+ current = static_cast<RenderJobItem*>(m_view.running_jobs->topLevelItem(ix));
+ }
+ slotCheckJob();
+}
+
+void RenderWidget::parseScriptFiles()
+{
+ QStringList scriptsFilter;
+ scriptsFilter << "*.sh";
+ m_view.scripts_list->clear();
+
+ QTreeWidgetItem *item;
+ // List the project scripts
+ QStringList scriptFiles = QDir(m_projectFolder + "scripts").entryList(scriptsFilter, QDir::Files);
+ for (int i = 0; i < scriptFiles.size(); ++i) {
+ KUrl scriptpath(m_projectFolder + "scripts/" + scriptFiles.at(i));
+ QString target;
+ QString renderer;
+ QString melt;
+ QFile file(scriptpath.path());
+ kDebug()<<"------------------\n"<<scriptpath.path();
+ if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ QTextStream stream(&file);
+ while (!stream.atEnd()) {
+ QString line = stream.readLine();
+ //kDebug()<<"# :"<<line;
+ if (line.startsWith("TARGET=")) {
+ target = line.section("TARGET=\"", 1);
+ target = target.section('"', 0, 0);
+ } else if (line.startsWith("RENDERER=")) {
+ renderer = line.section("RENDERER=\"", 1);
+ renderer = renderer.section('"', 0, 0);
+ } else if (line.startsWith("MELT=")) {
+ melt = line.section("MELT=\"", 1);
+ melt = melt.section('"', 0, 0);
+ }
+ }
+ file.close();
+ }
+ if (target.isEmpty()) continue;
+ //kDebug()<<"ScRIPT RENDERER: "<<renderer<<"\n++++++++++++++++++++++++++";
+ item = new QTreeWidgetItem(m_view.scripts_list, QStringList() << QString() << scriptpath.fileName());
+ if (!renderer.isEmpty() && renderer.contains('/') && !QFile::exists(renderer)) {
+ item->setIcon(0, KIcon("dialog-cancel"));
+ item->setToolTip(1, i18n("Script contains wrong command: %1", renderer));
+ item->setData(0, Qt::UserRole, '1');
+ } else if (!melt.isEmpty() && melt.contains('/') && !QFile::exists(melt)) {
+ item->setIcon(0, KIcon("dialog-cancel"));
+ item->setToolTip(1, i18n("Script contains wrong command: %1", melt));
+ item->setData(0, Qt::UserRole, '1');
+ } else item->setIcon(0, KIcon("application-x-executable-script"));
+ item->setSizeHint(0, QSize(m_view.scripts_list->columnWidth(0), fontMetrics().height() * 2));
+ item->setData(1, Qt::UserRole, KUrl(QUrl::fromEncoded(target.toUtf8())).pathOrUrl());
+ item->setData(1, Qt::UserRole + 1, scriptpath.path());
+ }
+ QTreeWidgetItem *script = m_view.scripts_list->topLevelItem(0);
+ if (script) {
+ m_view.scripts_list->setCurrentItem(script);
+ script->setSelected(true);
+ }
+}
+
+void RenderWidget::slotCheckScript()
+{
+ QTreeWidgetItem *current = m_view.scripts_list->currentItem();
+ if (current == NULL)
+ return;
+ m_view.start_script->setEnabled(current->data(0, Qt::UserRole).toString().isEmpty());
+ m_view.delete_script->setEnabled(true);
+ for (int i = 0; i < m_view.scripts_list->topLevelItemCount(); ++i) {
+ current = m_view.scripts_list->topLevelItem(i);
+ if (current == m_view.scripts_list->currentItem()) {
+ current->setSizeHint(1, QSize(m_view.scripts_list->columnWidth(1), fontMetrics().height() * 3));
+ } else current->setSizeHint(1, QSize(m_view.scripts_list->columnWidth(1), fontMetrics().height() * 2));
+ }
+}
+
+void RenderWidget::slotStartScript()
+{
+ RenderJobItem* item = static_cast<RenderJobItem*> (m_view.scripts_list->currentItem());
+ if (item) {
+ kDebug() << "// STARTING SCRIPT: "<<item->text(1);
+ QString destination = item->data(1, Qt::UserRole).toString();
+ QString path = item->data(1, Qt::UserRole + 1).toString();
+ // Insert new job in queue
+ RenderJobItem *renderItem = NULL;
+ QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(destination, Qt::MatchExactly, 1);
+ kDebug() << "------ START SCRIPT";
+ if (!existing.isEmpty()) {
+ renderItem = static_cast<RenderJobItem*> (existing.at(0));
+ if (renderItem->status() == RUNNINGJOB || renderItem->status() == WAITINGJOB) {
+ KMessageBox::information(this, i18n("There is already a job writing file:<br /><b>%1</b><br />Abort the job if you want to overwrite it...", destination), i18n("Already running"));
+ return;
+ }
+ else if (renderItem->type() != ScriptRenderType) {
+ delete renderItem;
+ renderItem = NULL;
+ }
+ }
+ if (!renderItem) renderItem = new RenderJobItem(m_view.running_jobs, QStringList() << QString() << destination, ScriptRenderType);
+ renderItem->setData(1, ProgressRole, 0);
+ renderItem->setStatus(WAITINGJOB);
+ renderItem->setIcon(0, KIcon("media-playback-pause"));
+ renderItem->setData(1, Qt::UserRole, i18n("Waiting..."));
+ renderItem->setData(1, TimeRole, QTime::currentTime());
+ renderItem->setData(1, ParametersRole, path);
+ checkRenderStatus();
+ m_view.tabWidget->setCurrentIndex(1);
+ }
+}