]> git.sesse.net Git - kdenlive/blob - src/renderwidget.cpp
Krazy fixes: cleanup all headers
[kdenlive] / src / renderwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "renderwidget.h"
22 #include "kdenlivesettings.h"
23 #include "ui_saveprofile_ui.h"
24
25 #include <KStandardDirs>
26 #include <KDebug>
27 #include <KMessageBox>
28 #include <KComboBox>
29 #include <KRun>
30 #include <KIO/NetAccess>
31 // #include <knewstuff2/engine.h>
32
33 #include <QDomDocument>
34 #include <QItemDelegate>
35 #include <QTreeWidgetItem>
36 #include <QListWidgetItem>
37 #include <QHeaderView>
38 #include <QMenu>
39 #include <QProcess>
40 #include <QInputDialog>
41
42 const int GroupRole = Qt::UserRole;
43 const int ExtensionRole = GroupRole + 1;
44 const int StandardRole = GroupRole + 2;
45 const int RenderRole = GroupRole + 3;
46 const int ParamsRole = GroupRole + 4;
47 const int EditableRole = GroupRole + 5;
48 const int MetaGroupRole = GroupRole + 6;
49 const int ExtraRole = GroupRole + 7;
50
51 RenderWidget::RenderWidget(const QString &projectfolder, QWidget * parent): QDialog(parent), m_projectFolder(projectfolder) {
52     m_view.setupUi(this);
53     setWindowTitle(i18n("Rendering"));
54     m_view.buttonDelete->setIcon(KIcon("trash-empty"));
55     m_view.buttonDelete->setToolTip(i18n("Delete profile"));
56     m_view.buttonDelete->setEnabled(false);
57
58     m_view.buttonEdit->setIcon(KIcon("document-properties"));
59     m_view.buttonEdit->setToolTip(i18n("Edit profile"));
60     m_view.buttonEdit->setEnabled(false);
61
62     m_view.buttonSave->setIcon(KIcon("document-new"));
63     m_view.buttonSave->setToolTip(i18n("Create new profile"));
64
65     m_view.buttonInfo->setIcon(KIcon("help-about"));
66
67     if (KdenliveSettings::showrenderparams()) {
68         m_view.buttonInfo->setDown(true);
69     } else m_view.advanced_params->hide();
70
71     m_view.rescale_size->setInputMask("0099\\x0099");
72     m_view.rescale_size->setText("320x240");
73
74
75     QMenu *renderMenu = new QMenu(i18n("Start Rendering"), this);
76     QAction *renderAction = renderMenu->addAction(KIcon("video-x-generic"), i18n("Render to File"));
77     connect(renderAction, SIGNAL(triggered()), this, SLOT(slotExport()));
78     QAction *scriptAction = renderMenu->addAction(KIcon("application-x-shellscript"), i18n("Generate Script"));
79     connect(scriptAction, SIGNAL(triggered()), this, SLOT(slotGenerateScript()));
80
81     m_view.buttonStart->setMenu(renderMenu);
82     m_view.buttonStart->setPopupMode(QToolButton::MenuButtonPopup);
83     m_view.buttonStart->setDefaultAction(renderAction);
84     m_view.buttonStart->setToolButtonStyle(Qt::ToolButtonTextOnly);
85     m_view.abort_job->setEnabled(false);
86     m_view.start_script->setEnabled(false);
87     m_view.delete_script->setEnabled(false);
88
89     m_view.format_list->setAlternatingRowColors(true);
90     m_view.size_list->setAlternatingRowColors(true);
91
92     parseProfiles();
93     parseScriptFiles();
94
95     connect(m_view.start_script, SIGNAL(clicked()), this, SLOT(slotStartScript()));
96     connect(m_view.delete_script, SIGNAL(clicked()), this, SLOT(slotDeleteScript()));
97     connect(m_view.scripts_list, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(slotCheckScript()));
98     connect(m_view.running_jobs, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(slotCheckJob()));
99
100     connect(m_view.buttonInfo, SIGNAL(clicked()), this, SLOT(showInfoPanel()));
101
102     connect(m_view.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
103     connect(m_view.buttonEdit, SIGNAL(clicked()), this, SLOT(slotEditProfile()));
104     connect(m_view.buttonDelete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
105     connect(m_view.abort_job, SIGNAL(clicked()), this, SLOT(slotAbortCurrentJob()));
106     connect(m_view.buttonClose, SIGNAL(clicked()), this, SLOT(hide()));
107     connect(m_view.buttonClose2, SIGNAL(clicked()), this, SLOT(hide()));
108     connect(m_view.buttonClose3, SIGNAL(clicked()), this, SLOT(hide()));
109     connect(m_view.rescale, SIGNAL(toggled(bool)), m_view.rescale_size, SLOT(setEnabled(bool)));
110     connect(m_view.destination_list, SIGNAL(activated(int)), this, SLOT(refreshView()));
111     connect(m_view.out_file, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateButtons()));
112     connect(m_view.out_file, SIGNAL(urlSelected(const KUrl &)), this, SLOT(slotUpdateButtons(const KUrl &)));
113     connect(m_view.format_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshView()));
114     connect(m_view.size_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshParams()));
115
116     connect(m_view.size_list, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(slotEditItem(QListWidgetItem *)));
117
118     connect(m_view.render_guide, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
119     connect(m_view.render_zone, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
120     connect(m_view.render_full, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
121
122     connect(m_view.guide_end, SIGNAL(activated(int)), this, SLOT(slotCheckStartGuidePosition()));
123     connect(m_view.guide_start, SIGNAL(activated(int)), this, SLOT(slotCheckEndGuidePosition()));
124
125     connect(m_view.format_selection, SIGNAL(activated(int)), this, SLOT(refreshView()));
126
127     m_view.buttonStart->setEnabled(false);
128     m_view.rescale_size->setEnabled(false);
129     m_view.guides_box->setVisible(false);
130     m_view.open_dvd->setVisible(false);
131     m_view.open_browser->setVisible(false);
132     m_view.error_box->setVisible(false);
133
134     m_view.splitter->setStretchFactor(1, 5);
135     m_view.splitter->setStretchFactor(0, 2);
136
137     m_view.out_file->setMode(KFile::File);
138
139     m_view.running_jobs->setHeaderLabels(QStringList() << QString() << i18n("File") << i18n("Progress"));
140     m_view.running_jobs->setItemDelegate(new RenderViewDelegate(this));
141
142     QHeaderView *header = m_view.running_jobs->header();
143     QFontMetrics fm = fontMetrics();
144     //header->resizeSection(0, fm.width("typical-name-for-a-torrent.torrent"));
145     header->setResizeMode(0, QHeaderView::Fixed);
146     header->resizeSection(0, 30);
147     header->setResizeMode(1, QHeaderView::Interactive);
148     header->resizeSection(1, fm.width("typical-name-for-a-file.torrent"));
149     header->setResizeMode(2, QHeaderView::Fixed);
150     header->resizeSection(1, width() * 2 / 3);
151     header->setResizeMode(2, QHeaderView::Interactive);
152     //header->setResizeMode(1, QHeaderView::Fixed);
153
154     m_view.scripts_list->setHeaderLabels(QStringList() << i18n("Script Files"));
155     m_view.scripts_list->setItemDelegate(new RenderScriptDelegate(this));
156
157
158     focusFirstVisibleItem();
159 }
160
161 void RenderWidget::slotEditItem(QListWidgetItem *item) {
162     QString edit = item->data(EditableRole).toString();
163     if (edit.isEmpty() || !edit.endsWith("customprofiles.xml")) slotSaveProfile();
164     else slotEditProfile();
165 }
166
167 void RenderWidget::showInfoPanel() {
168     if (m_view.advanced_params->isVisible()) {
169         m_view.advanced_params->setVisible(false);
170         m_view.buttonInfo->setDown(false);
171         KdenliveSettings::setShowrenderparams(false);
172     } else {
173         m_view.advanced_params->setVisible(true);
174         m_view.buttonInfo->setDown(true);
175         KdenliveSettings::setShowrenderparams(true);
176     }
177 }
178
179 void RenderWidget::setDocumentPath(const QString path) {
180     m_projectFolder = path;
181     const QString fileName = m_view.out_file->url().fileName();
182     m_view.out_file->setUrl(KUrl(m_projectFolder + '/' + fileName));
183     parseScriptFiles();
184 }
185
186 void RenderWidget::slotUpdateGuideBox() {
187     m_view.guides_box->setVisible(m_view.render_guide->isChecked());
188 }
189
190 void RenderWidget::slotCheckStartGuidePosition() {
191     if (m_view.guide_start->currentIndex() > m_view.guide_end->currentIndex())
192         m_view.guide_start->setCurrentIndex(m_view.guide_end->currentIndex());
193 }
194
195 void RenderWidget::slotCheckEndGuidePosition() {
196     if (m_view.guide_end->currentIndex() < m_view.guide_start->currentIndex())
197         m_view.guide_end->setCurrentIndex(m_view.guide_start->currentIndex());
198 }
199
200 void RenderWidget::setGuides(QDomElement guidesxml, double duration) {
201     m_view.guide_start->clear();
202     m_view.guide_end->clear();
203     QDomNodeList nodes = guidesxml.elementsByTagName("guide");
204     if (nodes.count() > 0) {
205         m_view.guide_start->addItem(i18n("Render"), "0");
206         m_view.render_guide->setEnabled(true);
207     } else m_view.render_guide->setEnabled(false);
208     for (int i = 0; i < nodes.count(); i++) {
209         QDomElement e = nodes.item(i).toElement();
210         if (!e.isNull()) {
211             m_view.guide_start->addItem(e.attribute("comment"), e.attribute("time").toDouble());
212             m_view.guide_end->addItem(e.attribute("comment"), e.attribute("time").toDouble());
213         }
214     }
215     if (nodes.count() > 0)
216         m_view.guide_end->addItem(i18n("End"), QString::number(duration));
217 }
218
219 // Will be called when the user selects an output file via the file dialog.
220 // File extension will be added automatically.
221 void RenderWidget::slotUpdateButtons(KUrl url) {
222     if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
223     else m_view.buttonStart->setEnabled(true);
224     if (url != 0) {
225         QListWidgetItem *item = m_view.size_list->currentItem();
226         QString extension = item->data(ExtensionRole).toString();
227         url = filenameWithExtension(url, extension);
228         m_view.out_file->setUrl(url);
229     }
230 }
231
232 // Will be called when the user changes the output file path in the text line.
233 // File extension must NOT be added, would make editing impossible!
234 void RenderWidget::slotUpdateButtons() {
235     if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
236     else m_view.buttonStart->setEnabled(true);
237 }
238
239 void RenderWidget::slotSaveProfile() {
240     //TODO: update to correctly use metagroups
241     Ui::SaveProfile_UI ui;
242     QDialog *d = new QDialog(this);
243     ui.setupUi(d);
244
245     for (int i = 0; i < m_view.destination_list->count(); i++)
246         ui.destination_list->addItem(m_view.destination_list->itemIcon(i), m_view.destination_list->itemText(i), m_view.destination_list->itemData(i, Qt::UserRole));
247
248     ui.destination_list->setCurrentIndex(m_view.destination_list->currentIndex());
249     QString dest = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
250
251     QString customGroup = m_view.format_list->currentItem()->text();
252     if (customGroup.isEmpty()) customGroup = i18n("Custom");
253     ui.group_name->setText(customGroup);
254
255     ui.parameters->setText(m_view.advanced_params->toPlainText());
256     ui.extension->setText(m_view.size_list->currentItem()->data(ExtensionRole).toString());
257     ui.profile_name->setFocus();
258     if (d->exec() == QDialog::Accepted && !ui.profile_name->text().simplified().isEmpty()) {
259         QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
260         QDomDocument doc;
261         QFile file(exportFile);
262         doc.setContent(&file, false);
263         file.close();
264         QDomElement documentElement;
265         QDomElement profiles = doc.documentElement();
266         if (profiles.isNull() || profiles.tagName() != "profiles") {
267             doc.clear();
268             profiles = doc.createElement("profiles");
269             profiles.setAttribute("version", 1);
270             doc.appendChild(profiles);
271         }
272         int version = profiles.attribute("version", 0).toInt();
273         if (version < 1) {
274             kDebug() << "// OLD profile version";
275             doc.clear();
276             profiles = doc.createElement("profiles");
277             profiles.setAttribute("version", 1);
278             doc.appendChild(profiles);
279         }
280
281         QString newProfileName = ui.profile_name->text().simplified();
282         QString newGroupName = ui.group_name->text().simplified();
283         if (newGroupName.isEmpty()) newGroupName = i18n("Custom");
284         QString newMetaGroupId = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
285         QDomNodeList profilelist = doc.elementsByTagName("profile");
286         int i = 0;
287         while (!profilelist.item(i).isNull()) {
288             // make sure a profile with same name doesn't exist
289             documentElement = profilelist.item(i).toElement();
290             QString profileName = documentElement.attribute("name");
291             if (profileName == newProfileName) {
292                 // a profile with that same name already exists
293                 bool ok;
294                 newProfileName = QInputDialog::getText(this, i18n("Profile already exists"), i18n("This profile name already exists. Change the name if you don't want to overwrite it."), QLineEdit::Normal, newProfileName, &ok);
295                 if (!ok) return;
296                 if (profileName == newProfileName) {
297                     profiles.removeChild(profilelist.item(i));
298                     break;
299                 }
300             }
301             i++;
302         }
303
304         QDomElement profileElement = doc.createElement("profile");
305         profileElement.setAttribute("name", newProfileName);
306         profileElement.setAttribute("category", newGroupName);
307         profileElement.setAttribute("destinationid", newMetaGroupId);
308         profileElement.setAttribute("extension", ui.extension->text().simplified());
309         profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
310         profiles.appendChild(profileElement);
311
312         //QCString save = doc.toString().utf8();
313
314         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
315             KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
316             delete d;
317             return;
318         }
319         QTextStream out(&file);
320         out << doc.toString();
321         file.close();
322         parseProfiles(newMetaGroupId, newGroupName, newProfileName);
323     }
324     delete d;
325 }
326
327 void RenderWidget::slotEditProfile() {
328     QListWidgetItem *item = m_view.size_list->currentItem();
329     if (!item) return;
330     QString currentGroup = m_view.format_list->currentItem()->text();
331
332     QString params = item->data(ParamsRole).toString();
333     QString extension = item->data(ExtensionRole).toString();
334     QString currentProfile = item->text();
335
336     Ui::SaveProfile_UI ui;
337     QDialog *d = new QDialog(this);
338     ui.setupUi(d);
339
340     for (int i = 0; i < m_view.destination_list->count(); i++)
341         ui.destination_list->addItem(m_view.destination_list->itemIcon(i), m_view.destination_list->itemText(i), m_view.destination_list->itemData(i, Qt::UserRole));
342
343     ui.destination_list->setCurrentIndex(m_view.destination_list->currentIndex());
344     QString dest = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
345
346     QString customGroup = m_view.format_list->currentItem()->text();
347     if (customGroup.isEmpty()) customGroup = i18n("Custom");
348     ui.group_name->setText(customGroup);
349
350     ui.profile_name->setText(currentProfile);
351     ui.extension->setText(extension);
352     ui.parameters->setText(params);
353     ui.profile_name->setFocus();
354     d->setWindowTitle(i18n("Edit Profile"));
355     if (d->exec() == QDialog::Accepted) {
356         slotDeleteProfile(false);
357         QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
358         QDomDocument doc;
359         QFile file(exportFile);
360         doc.setContent(&file, false);
361         file.close();
362         QDomElement documentElement;
363         QDomElement profiles = doc.documentElement();
364
365         if (profiles.isNull() || profiles.tagName() != "profiles") {
366             doc.clear();
367             profiles = doc.createElement("profiles");
368             profiles.setAttribute("version", 1);
369             doc.appendChild(profiles);
370         }
371
372         int version = profiles.attribute("version", 0).toInt();
373         if (version < 1) {
374             kDebug() << "// OLD profile version";
375             doc.clear();
376             profiles = doc.createElement("profiles");
377             profiles.setAttribute("version", 1);
378             doc.appendChild(profiles);
379         }
380
381         QString newProfileName = ui.profile_name->text().simplified();
382         QString newGroupName = ui.group_name->text().simplified();
383         if (newGroupName.isEmpty()) newGroupName = i18n("Custom");
384         QString newMetaGroupId = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
385         QDomNodeList profilelist = doc.elementsByTagName("profile");
386         int i = 0;
387         while (!profilelist.item(i).isNull()) {
388             // make sure a profile with same name doesn't exist
389             documentElement = profilelist.item(i).toElement();
390             QString profileName = documentElement.attribute("name");
391             if (profileName == newProfileName) {
392                 // a profile with that same name already exists
393                 bool ok;
394                 newProfileName = QInputDialog::getText(this, i18n("Profile already exists"), i18n("This profile name already exists. Change the name if you don't want to overwrite it."), QLineEdit::Normal, newProfileName, &ok);
395                 if (!ok) return;
396                 if (profileName == newProfileName) {
397                     profiles.removeChild(profilelist.item(i));
398                     break;
399                 }
400             }
401             i++;
402         }
403
404         QDomElement profileElement = doc.createElement("profile");
405         profileElement.setAttribute("name", newProfileName);
406         profileElement.setAttribute("category", newGroupName);
407         profileElement.setAttribute("destinationid", newMetaGroupId);
408         profileElement.setAttribute("extension", ui.extension->text().simplified());
409         profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
410         profiles.appendChild(profileElement);
411
412         //QCString save = doc.toString().utf8();
413
414         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
415             KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
416             delete d;
417             return;
418         }
419         QTextStream out(&file);
420         out << doc.toString();
421         file.close();
422         parseProfiles(newMetaGroupId, newGroupName, newProfileName);
423     }
424     delete d;
425 }
426
427 void RenderWidget::slotDeleteProfile(bool refresh) {
428     //TODO: delete a profile installed by KNewStuff the easy way
429     /*
430     QString edit = m_view.size_list->currentItem()->data(EditableRole).toString();
431     if (!edit.endsWith("customprofiles.xml")) {
432         // This is a KNewStuff installed file, process through KNS
433         KNS::Engine engine(0);
434         if (engine.init("kdenlive_render.knsrc")) {
435             KNS::Entry::List entries;
436         }
437         return;
438     }*/
439     QString currentGroup = m_view.format_list->currentItem()->text();
440     QString currentProfile = m_view.size_list->currentItem()->text();
441     QString metaGroupId = m_view.destination_list->itemData(m_view.destination_list->currentIndex(), Qt::UserRole).toString();
442
443     QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
444     QDomDocument doc;
445     QFile file(exportFile);
446     doc.setContent(&file, false);
447     file.close();
448
449     QDomElement documentElement;
450     QDomNodeList profiles = doc.elementsByTagName("profile");
451     int i = 0;
452     QString groupName;
453     QString profileName;
454     QString destination;
455
456     while (!profiles.item(i).isNull()) {
457         documentElement = profiles.item(i).toElement();
458         profileName = documentElement.attribute("name");
459         groupName = documentElement.attribute("category");
460         destination = documentElement.attribute("destinationid");
461
462         if (profileName == currentProfile && groupName == currentGroup && destination == metaGroupId) {
463             kDebug() << "// GOT it: " << profileName;
464             doc.documentElement().removeChild(profiles.item(i));
465             break;
466         }
467         i++;
468     }
469
470     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
471         KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
472         return;
473     }
474     QTextStream out(&file);
475     out << doc.toString();
476     file.close();
477     if (refresh) {
478         parseProfiles(metaGroupId, currentGroup);
479         focusFirstVisibleItem();
480     }
481 }
482
483 void RenderWidget::updateButtons() {
484     if (!m_view.size_list->currentItem() || m_view.size_list->currentItem()->isHidden()) {
485         m_view.buttonSave->setEnabled(false);
486         m_view.buttonDelete->setEnabled(false);
487         m_view.buttonEdit->setEnabled(false);
488         m_view.buttonStart->setEnabled(false);
489     } else {
490         m_view.buttonSave->setEnabled(true);
491         m_view.buttonStart->setEnabled(m_view.size_list->currentItem()->flags() & Qt::ItemIsEnabled);
492         QString edit = m_view.size_list->currentItem()->data(EditableRole).toString();
493         if (edit.isEmpty() || !edit.endsWith("customprofiles.xml")) {
494             m_view.buttonDelete->setEnabled(false);
495             m_view.buttonEdit->setEnabled(false);
496         } else {
497             m_view.buttonDelete->setEnabled(true);
498             m_view.buttonEdit->setEnabled(true);
499         }
500     }
501 }
502
503
504 void RenderWidget::focusFirstVisibleItem() {
505     if (m_view.size_list->currentItem() && !m_view.size_list->currentItem()->isHidden()) {
506         updateButtons();
507         return;
508     }
509     for (uint ix = 0; ix < m_view.size_list->count(); ix++) {
510         QListWidgetItem *item = m_view.size_list->item(ix);
511         if (item && !item->isHidden()) {
512             m_view.size_list->setCurrentRow(ix);
513             break;
514         }
515     }
516     if (!m_view.size_list->currentItem()) m_view.size_list->setCurrentRow(0);
517     updateButtons();
518 }
519
520 void RenderWidget::slotExport(bool scriptExport) {
521     QListWidgetItem *item = m_view.size_list->currentItem();
522     if (!item) return;
523
524     const QString dest = m_view.out_file->url().path();
525     if (dest.isEmpty()) return;
526     QFile f(dest);
527     if (f.exists()) {
528         if (KMessageBox::warningYesNo(this, i18n("Output file already exists. Do you want to overwrite it ?")) != KMessageBox::Yes)
529             return;
530     }
531
532     QString scriptName;
533     if (scriptExport) {
534         bool ok;
535         int ix = 0;
536         QString scriptsFolder = m_projectFolder + "/scripts/";
537         KStandardDirs::makeDir(scriptsFolder);
538         QString path = scriptsFolder + i18n("script") + QString::number(ix).rightJustified(3, '0', false) + ".sh";
539         while (QFile::exists(path)) {
540             ix++;
541             path = scriptsFolder + i18n("script") + QString::number(ix).rightJustified(3, '0', false) + ".sh";
542         }
543         scriptName = QInputDialog::getText(this, i18n("Create Render Script"), i18n("Script name (will be saved in: %1)", scriptsFolder), QLineEdit::Normal, KUrl(path).fileName(), &ok);
544         if (!ok || scriptName.isEmpty()) return;
545         scriptName.prepend(scriptsFolder);
546         QFile f(scriptName);
547         if (f.exists()) {
548             if (KMessageBox::warningYesNo(this, i18n("Script file already exists. Do you want to overwrite it ?")) != KMessageBox::Yes)
549                 return;
550         }
551     }
552
553     QStringList overlayargs;
554     if (m_view.tc_overlay->isChecked()) {
555         QString filterFile = KStandardDirs::locate("appdata", "metadata.properties");
556         overlayargs << "meta.attr.timecode=1" << "meta.attr.timecode.markup=#timecode";
557         overlayargs << "-attach" << "data_feed:attr_check" << "-attach";
558         overlayargs << "data_show:" + filterFile << "_fezzik=1" << "dynamic=1";
559     }
560     double startPos = -1;
561     double endPos = -1;
562     if (m_view.render_guide->isChecked()) {
563         startPos = m_view.guide_start->itemData(m_view.guide_start->currentIndex()).toDouble();
564         endPos = m_view.guide_end->itemData(m_view.guide_end->currentIndex()).toDouble();
565     }
566     QString renderArgs = m_view.advanced_params->toPlainText();
567
568     // Adjust frame scale
569     int width;
570     int height;
571     if (m_view.rescale->isChecked() && m_view.rescale->isEnabled()) {
572         width = m_view.rescale_size->text().section('x', 0, 0).toInt();
573         height = m_view.rescale_size->text().section('x', 1, 1).toInt();
574     } else {
575         width = m_profile.width;
576         height = m_profile.height;
577     }
578     renderArgs.replace("%dar", '@' + QString::number(m_profile.display_aspect_num) + '/' + QString::number(m_profile.display_aspect_den));
579
580     // Adjust scanning
581     if (m_view.scanning_list->currentIndex() == 1) renderArgs.append(" progressive=1");
582     else if (m_view.scanning_list->currentIndex() == 2) renderArgs.append(" progressive=0");
583
584     // disable audio if requested
585     if (!m_view.export_audio->isChecked())
586         renderArgs.append(" an=1 ");
587
588     // Check if the rendering profile is different from project profile,
589     // in which case we need to use the producer_comsumer from MLT
590     bool resizeProfile = false;
591
592     QString std = renderArgs;
593     QString destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
594     if (std.contains(" s=")) {
595         QString subsize = std.section(" s=", 1, 1);
596         subsize = subsize.section(' ', 0, 0).toLower();
597         const QString currentSize = QString::number(width) + 'x' + QString::number(height);
598         if (subsize != currentSize) resizeProfile = true;
599     } else if (destination != "audioonly") {
600         // Add current size parametrer
601         renderArgs.append(QString(" s=%1x%2").arg(width).arg(height));
602     }
603
604     // insert item in running jobs list
605     QTreeWidgetItem *renderItem;
606     QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
607     if (!existing.isEmpty()) renderItem = existing.at(0);
608     else renderItem = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
609     renderItem->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
610     renderItem->setData(1, Qt::UserRole + 1, QTime::currentTime());
611
612     // Set rendering type
613     QString group = m_view.size_list->currentItem()->data(MetaGroupRole).toString();
614     if (group == "dvd" && m_view.open_dvd->isChecked()) {
615         renderItem->setData(0, Qt::UserRole, group);
616         if (renderArgs.contains("profile=")) {
617             // rendering profile contains an MLT profile, so pass it to the running jog item, useful for dvd
618             QString prof = renderArgs.section("profile=", 1, 1);
619             prof = prof.section(' ', 0, 0);
620             kDebug() << "// render profile: " << prof;
621             renderItem->setData(0, Qt::UserRole + 1, prof);
622         }
623     } else if (group == "websites" && m_view.open_browser->isChecked()) {
624         renderItem->setData(0, Qt::UserRole, group);
625         // pass the url
626         QString url = m_view.size_list->currentItem()->data(ExtraRole).toString();
627         renderItem->setData(0, Qt::UserRole + 1, url);
628     }
629
630     emit doRender(dest, item->data(RenderRole).toString(), overlayargs, renderArgs.simplified().split(' '), m_view.render_zone->isChecked(), m_view.play_after->isChecked(), startPos, endPos, resizeProfile, scriptName);
631     if (scriptName.isEmpty()) m_view.tabWidget->setCurrentIndex(1);
632     else {
633         QTimer::singleShot(400, this, SLOT(parseScriptFiles()));
634         m_view.tabWidget->setCurrentIndex(2);
635     }
636 }
637
638 void RenderWidget::setProfile(MltVideoProfile profile) {
639     m_profile = profile;
640     //WARNING: this way to tell the video standard is a bit hackish...
641     if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) m_view.format_selection->setCurrentIndex(0);
642     else m_view.format_selection->setCurrentIndex(1);
643     m_view.scanning_list->setCurrentIndex(0);
644     refreshView();
645 }
646
647 void RenderWidget::refreshView() {
648     m_view.size_list->blockSignals(true);
649     QListWidgetItem *sizeItem;
650
651     QString destination;
652     KIcon brokenIcon("dialog-close");
653     if (m_view.destination_list->currentIndex() > 0)
654         destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
655
656
657     if (destination == "dvd") m_view.open_dvd->setVisible(true);
658     else m_view.open_dvd->setVisible(false);
659     if (destination == "websites") m_view.open_browser->setVisible(true);
660     else m_view.open_browser->setVisible(false);
661     if (!destination.isEmpty() && QString("dvd websites audioonly").contains(destination))
662         m_view.rescale->setEnabled(false);
663     else m_view.rescale->setEnabled(true);
664     // hide groups that are not in the correct destination
665     for (int i = 0; i < m_view.format_list->count(); i++) {
666         sizeItem = m_view.format_list->item(i);
667         if (sizeItem->data(MetaGroupRole).toString() == destination) {
668             sizeItem->setHidden(false);
669             //kDebug() << "// SET GRP:: " << sizeItem->text() << ", METY:" << sizeItem->data(MetaGroupRole).toString();
670         } else sizeItem->setHidden(true);
671     }
672
673     // activate first visible item
674     QListWidgetItem * item = m_view.format_list->currentItem();
675     if (!item || item->isHidden()) {
676         for (int i = 0; i < m_view.format_list->count(); i++) {
677             if (!m_view.format_list->item(i)->isHidden()) {
678                 m_view.format_list->setCurrentRow(i);
679                 break;
680             }
681         }
682         item = m_view.format_list->currentItem();
683     }
684     if (!item || item->isHidden()) {
685         m_view.format_list->setEnabled(false);
686         m_view.size_list->setEnabled(false);
687         return;
688     } else {
689         m_view.format_list->setEnabled(true);
690         m_view.size_list->setEnabled(true);
691     }
692     int count = 0;
693     for (int i = 0; i < m_view.format_list->count() && count < 2; i++) {
694         if (!m_view.format_list->isRowHidden(i)) count++;
695     }
696     if (count > 1) m_view.format_list->setVisible(true);
697     else m_view.format_list->setVisible(false);
698     QString std;
699     QString group = item->text();
700     bool firstSelected = false;
701     const QStringList formatsList = KdenliveSettings::supportedformats();
702     const QStringList vcodecsList = KdenliveSettings::videocodecs();
703     const QStringList acodecsList = KdenliveSettings::audiocodecs();
704
705     for (int i = 0; i < m_view.size_list->count(); i++) {
706         sizeItem = m_view.size_list->item(i);
707         if ((sizeItem->data(GroupRole) == group || sizeItem->data(GroupRole).toString().isEmpty()) && sizeItem->data(MetaGroupRole) == destination) {
708             std = sizeItem->data(StandardRole).toString();
709             if (!std.isEmpty()) {
710                 if (std.contains("PAL", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 0);
711                 else if (std.contains("NTSC", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 1);
712             } else {
713                 sizeItem->setHidden(false);
714                 if (!firstSelected) m_view.size_list->setCurrentItem(sizeItem);
715                 firstSelected = true;
716             }
717
718             if (!sizeItem->isHidden()) {
719                 // Make sure the selected profile uses an installed avformat codec / format
720                 std = sizeItem->data(ParamsRole).toString();
721
722                 if (!formatsList.isEmpty()) {
723                     QString format;
724                     if (std.startsWith("f=")) format = std.section("f=", 1, 1);
725                     else if (std.contains(" f=")) format = std.section(" f=", 1, 1);
726                     if (!format.isEmpty()) {
727                         format = format.section(' ', 0, 0).toLower();
728                         if (!formatsList.contains(format)) {
729                             kDebug() << "***** UNSUPPORTED F: " << format;
730                             //sizeItem->setHidden(true);
731                             sizeItem->setFlags(Qt::ItemIsSelectable);
732                             sizeItem->setToolTip(i18n("Unsupported video format: %1", format));
733                             sizeItem->setIcon(brokenIcon);
734                         }
735                     }
736                 }
737                 if (!acodecsList.isEmpty() && !sizeItem->isHidden()) {
738                     QString format;
739                     if (std.startsWith("acodec=")) format = std.section("acodec=", 1, 1);
740                     else if (std.contains(" acodec=")) format = std.section(" acodec=", 1, 1);
741                     if (!format.isEmpty()) {
742                         format = format.section(' ', 0, 0).toLower();
743                         if (!acodecsList.contains(format)) {
744                             kDebug() << "*****  UNSUPPORTED ACODEC: " << format;
745                             //sizeItem->setHidden(true);
746                             sizeItem->setFlags(Qt::ItemIsSelectable);
747                             sizeItem->setToolTip(i18n("Unsupported audio codec: %1", format));
748                             sizeItem->setIcon(brokenIcon);
749                         }
750                     }
751                 }
752                 if (!vcodecsList.isEmpty() && !sizeItem->isHidden()) {
753                     QString format;
754                     if (std.startsWith("vcodec=")) format = std.section("vcodec=", 1, 1);
755                     else if (std.contains(" vcodec=")) format = std.section(" vcodec=", 1, 1);
756                     if (!format.isEmpty()) {
757                         format = format.section(' ', 0, 0).toLower();
758                         if (!vcodecsList.contains(format)) {
759                             kDebug() << "*****  UNSUPPORTED VCODEC: " << format;
760                             //sizeItem->setHidden(true);
761                             sizeItem->setFlags(Qt::ItemIsSelectable);
762                             sizeItem->setToolTip(i18n("Unsupported video codec: %1", format));
763                             sizeItem->setIcon(brokenIcon);
764                         }
765                     }
766                 }
767             }
768         } else sizeItem->setHidden(true);
769     }
770     focusFirstVisibleItem();
771     m_view.size_list->blockSignals(false);
772     refreshParams();
773 }
774
775 KUrl RenderWidget::filenameWithExtension(KUrl url, QString extension) {
776     QString path;
777     if (!url.isEmpty()) {
778         path = url.path();
779         int pos = path.lastIndexOf('.') + 1;
780         if (pos == 0) path.append('.' + extension);
781         else path = path.left(pos) + extension;
782
783     } else {
784         path = m_projectFolder + "/untitled." + extension;
785     }
786     return KUrl(path);
787 }
788
789
790 void RenderWidget::refreshParams() {
791     QListWidgetItem *item = m_view.size_list->currentItem();
792     if (!item || item->isHidden()) {
793         m_view.advanced_params->clear();
794         m_view.buttonStart->setEnabled(false);
795         return;
796     }
797     QString params = item->data(ParamsRole).toString();
798     QString extension = item->data(ExtensionRole).toString();
799     m_view.advanced_params->setPlainText(params);
800     QString destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
801     if (params.contains(" s=") || destination == "audioonly") {
802         // profile has a fixed size, do not allow resize
803         m_view.rescale->setEnabled(false);
804         m_view.rescale_size->setEnabled(false);
805     } else {
806         m_view.rescale->setEnabled(true);
807         m_view.rescale_size->setEnabled(true);
808     }
809     KUrl url = filenameWithExtension(m_view.out_file->url(), extension);
810     m_view.out_file->setUrl(url);
811 //     if (!url.isEmpty()) {
812 //         QString path = url.path();
813 //         int pos = path.lastIndexOf('.') + 1;
814 //  if (pos == 0) path.append('.' + extension);
815 //         else path = path.left(pos) + extension;
816 //         m_view.out_file->setUrl(KUrl(path));
817 //     } else {
818 //         m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
819 //     }
820     m_view.out_file->setFilter("*." + extension);
821     QString edit = item->data(EditableRole).toString();
822     if (edit.isEmpty() || !edit.endsWith("customprofiles.xml")) {
823         m_view.buttonDelete->setEnabled(false);
824         m_view.buttonEdit->setEnabled(false);
825     } else {
826         m_view.buttonDelete->setEnabled(true);
827         m_view.buttonEdit->setEnabled(true);
828     }
829
830     m_view.buttonStart->setEnabled(m_view.size_list->currentItem()->flags() & Qt::ItemIsEnabled);
831 }
832
833 void RenderWidget::reloadProfiles() {
834     parseProfiles();
835 }
836
837 void RenderWidget::parseProfiles(QString meta, QString group, QString profile) {
838     m_view.size_list->clear();
839     m_view.format_list->clear();
840     m_view.destination_list->clear();
841     m_view.destination_list->addItem(KIcon("video-x-generic"), i18n("File rendering"));
842     m_view.destination_list->addItem(KIcon("media-optical"), i18n("DVD"), "dvd");
843     m_view.destination_list->addItem(KIcon("audio-x-generic"), i18n("Audio only"), "audioonly");
844     m_view.destination_list->addItem(KIcon("applications-internet"), i18n("Web sites"), "websites");
845     m_view.destination_list->addItem(KIcon("applications-multimedia"), i18n("Media players"), "mediaplayers");
846     m_view.destination_list->addItem(KIcon("drive-harddisk"), i18n("Lossless / HQ"), "lossless");
847     m_view.destination_list->addItem(KIcon("pda"), i18n("Mobile devices"), "mobile");
848
849     QString exportFile = KStandardDirs::locate("appdata", "export/profiles.xml");
850     parseFile(exportFile, false);
851
852
853     QString exportFolder = KStandardDirs::locateLocal("appdata", "export/");
854     QDir directory = QDir(exportFolder);
855     QStringList filter;
856     filter << "*.xml";
857     QStringList fileList = directory.entryList(filter, QDir::Files);
858     // We should parse customprofiles.xml in last position, so that user profiles
859     // can also override profiles installed by KNewStuff
860     fileList.removeAll("customprofiles.xml");
861     foreach(const QString &filename, fileList)
862         parseFile(exportFolder + '/' + filename, true);
863     if (QFile::exists(exportFolder + "/customprofiles.xml")) parseFile(exportFolder + "/customprofiles.xml", true);
864
865     if (!meta.isEmpty()) {
866         m_view.destination_list->blockSignals(true);
867         m_view.destination_list->setCurrentIndex(m_view.destination_list->findData(meta));
868         m_view.destination_list->blockSignals(false);
869     }
870     refreshView();
871     QList<QListWidgetItem *> child;
872     if (!group.isEmpty()) child = m_view.format_list->findItems(group, Qt::MatchExactly);
873     if (!child.isEmpty()) {
874         for (int i = 0; i < child.count(); i++) {
875             if (child.at(i)->data(MetaGroupRole).toString() == meta) {
876                 m_view.format_list->setCurrentItem(child.at(i));
877                 break;
878             }
879         }
880     }
881     child.clear();
882     if (!profile.isEmpty()) child = m_view.size_list->findItems(profile, Qt::MatchExactly);
883     if (!child.isEmpty()) m_view.size_list->setCurrentItem(child.at(0));
884 }
885
886 void RenderWidget::parseFile(QString exportFile, bool editable) {
887     kDebug() << "// Parsing file: " << exportFile;
888     kDebug() << "------------------------------";
889     QDomDocument doc;
890     QFile file(exportFile);
891     doc.setContent(&file, false);
892     file.close();
893     QDomElement documentElement;
894     QDomElement profileElement;
895     QString extension;
896     QListWidgetItem *item;
897     QDomNodeList groups = doc.elementsByTagName("group");
898
899     if (editable || groups.count() == 0) {
900         QDomElement profiles = doc.documentElement();
901         if (editable && profiles.attribute("version", 0).toInt() < 1) {
902             kDebug() << "// OLD profile version";
903             // this is an old profile version, update it
904             QDomDocument newdoc;
905             QDomElement newprofiles = newdoc.createElement("profiles");
906             newprofiles.setAttribute("version", 1);
907             newdoc.appendChild(newprofiles);
908             QDomNodeList profilelist = doc.elementsByTagName("profile");
909             for (int i = 0; i < profilelist.count(); i++) {
910                 QString category = i18n("Custom");
911                 QString extension;
912                 QDomNode parent = profilelist.at(i).parentNode();
913                 if (!parent.isNull()) {
914                     QDomElement parentNode = parent.toElement();
915                     if (parentNode.hasAttribute("name")) category = parentNode.attribute("name");
916                     extension = parentNode.attribute("extension");
917                 }
918                 profilelist.at(i).toElement().setAttribute("category", category);
919                 if (!extension.isEmpty()) profilelist.at(i).toElement().setAttribute("extension", extension);
920                 QDomNode n = profilelist.at(i).cloneNode();
921                 newprofiles.appendChild(newdoc.importNode(n, true));
922             }
923             if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
924                 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
925                 return;
926             }
927             QTextStream out(&file);
928             out << newdoc.toString();
929             file.close();
930             parseFile(exportFile, editable);
931             return;
932         }
933
934         QDomNode node = doc.elementsByTagName("profile").at(0);
935         if (node.isNull()) {
936             kDebug() << "// Export file: " << exportFile << " IS BROKEN";
937             return;
938         }
939         int count = 1;
940         while (!node.isNull()) {
941             QDomElement profile = node.toElement();
942             QString profileName = profile.attribute("name");
943             QString standard = profile.attribute("standard");
944             QString params = profile.attribute("args");
945             QString category = profile.attribute("category", i18n("Custom"));
946             QString dest = profile.attribute("destinationid");
947             QString prof_extension = profile.attribute("extension");
948             if (!prof_extension.isEmpty()) extension = prof_extension;
949
950             QList <QListWidgetItem *> list = m_view.format_list->findItems(category, Qt::MatchExactly);
951             bool exists = false;
952             for (int j = 0; j < list.count(); j++) {
953                 if (list.at(j)->data(MetaGroupRole) == dest) {
954                     exists = true;
955                     break;
956                 }
957             }
958             if (!exists) {
959                 item = new QListWidgetItem(category, m_view.format_list);
960                 item->setData(MetaGroupRole, dest);
961             }
962
963             // Check if item with same name already exists and replace it,
964             // allowing to override default profiles
965
966             list = m_view.size_list->findItems(profileName, Qt::MatchExactly);
967
968             for (int j = 0; j < list.count(); j++) {
969                 if (list.at(j)->data(MetaGroupRole) == dest) {
970                     QListWidgetItem *duplicate = list.takeAt(j);
971                     delete duplicate;
972                     j--;
973                 }
974             }
975
976             item = new QListWidgetItem(profileName, m_view.size_list);
977             //kDebug() << "// ADDINg item with name: " << profileName << ", GRP" << category << ", DEST:" << dest ;
978             item->setData(GroupRole, category);
979             item->setData(MetaGroupRole, dest);
980             item->setData(ExtensionRole, extension);
981             item->setData(RenderRole, "avformat");
982             item->setData(StandardRole, standard);
983             item->setData(ParamsRole, params);
984             if (profile.hasAttribute("url")) item->setData(ExtraRole, profile.attribute("url"));
985             if (editable) {
986                 item->setData(EditableRole, exportFile);
987                 if (exportFile.endsWith("customprofiles.xml")) item->setIcon(KIcon("emblem-favorite"));
988                 else item->setIcon(KIcon("applications-internet"));
989             }
990             node = doc.elementsByTagName("profile").at(count);
991             count++;
992         }
993         return;
994     }
995
996     int i = 0;
997     QString groupName;
998     QString profileName;
999
1000     QString prof_extension;
1001     QString renderer;
1002     QString params;
1003     QString standard;
1004     KIcon icon;
1005
1006     while (!groups.item(i).isNull()) {
1007         documentElement = groups.item(i).toElement();
1008         QDomNode gname = documentElement.elementsByTagName("groupname").at(0);
1009         QString metagroupName;
1010         QString metagroupId;
1011         if (!gname.isNull()) {
1012             metagroupName = gname.firstChild().nodeValue();
1013             metagroupId = gname.toElement().attribute("id");
1014             if (!metagroupName.isEmpty() && !m_view.destination_list->contains(metagroupName)) {
1015                 if (metagroupId == "dvd") icon = KIcon("media-optical");
1016                 else if (metagroupId == "audioonly") icon = KIcon("audio-x-generic");
1017                 else if (metagroupId == "websites") icon = KIcon("applications-internet");
1018                 else if (metagroupId == "mediaplayers") icon = KIcon("applications-multimedia");
1019                 else if (metagroupId == "lossless") icon = KIcon("drive-harddisk");
1020                 else if (metagroupId == "mobile") icon = KIcon("pda");
1021                 m_view.destination_list->addItem(icon, i18n(metagroupName.toUtf8().data()), metagroupId);
1022             }
1023         }
1024         groupName = documentElement.attribute("name", i18n("Custom"));
1025         extension = documentElement.attribute("extension", QString());
1026         renderer = documentElement.attribute("renderer", QString());
1027         QList <QListWidgetItem *> list = m_view.format_list->findItems(groupName, Qt::MatchExactly);
1028         bool exists = false;
1029         for (int j = 0; j < list.count(); j++) {
1030             if (list.at(j)->data(MetaGroupRole) == metagroupId) {
1031                 exists = true;
1032                 break;
1033             }
1034         }
1035         if (!exists) {
1036             item = new QListWidgetItem(groupName, m_view.format_list);
1037             item->setData(MetaGroupRole, metagroupId);
1038         }
1039
1040         QDomNode n = groups.item(i).firstChild();
1041         while (!n.isNull()) {
1042             if (n.toElement().tagName() != "profile") {
1043                 n = n.nextSibling();
1044                 continue;
1045             }
1046             profileElement = n.toElement();
1047             profileName = profileElement.attribute("name");
1048             standard = profileElement.attribute("standard");
1049             params = profileElement.attribute("args");
1050             prof_extension = profileElement.attribute("extension");
1051             if (!prof_extension.isEmpty()) extension = prof_extension;
1052             item = new QListWidgetItem(profileName, m_view.size_list);
1053             item->setData(GroupRole, groupName);
1054             item->setData(MetaGroupRole, metagroupId);
1055             item->setData(ExtensionRole, extension);
1056             item->setData(RenderRole, renderer);
1057             item->setData(StandardRole, standard);
1058             item->setData(ParamsRole, params);
1059             if (profileElement.hasAttribute("url")) item->setData(ExtraRole, profileElement.attribute("url"));
1060             if (editable) item->setData(EditableRole, exportFile);
1061             n = n.nextSibling();
1062         }
1063
1064         i++;
1065     }
1066 }
1067
1068 void RenderWidget::setRenderJob(const QString &dest, int progress) {
1069     QTreeWidgetItem *item;
1070     QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
1071     if (!existing.isEmpty()) item = existing.at(0);
1072     else item = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
1073     item->setData(2, Qt::UserRole, progress);
1074     if (progress == 0) {
1075         item->setIcon(0, KIcon("system-run"));
1076         item->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
1077         item->setData(1, Qt::UserRole + 1, QTime::currentTime());
1078         slotCheckJob();
1079     } else {
1080         QTime startTime = item->data(1, Qt::UserRole + 1).toTime();
1081         int seconds = startTime.secsTo(QTime::currentTime());;
1082         const QString t = i18n("Estimated time %1", QTime().addSecs(seconds * (100 - progress) / progress).toString("hh:mm:ss"));
1083         item->setData(1, Qt::UserRole, t);
1084     }
1085 }
1086
1087 void RenderWidget::setRenderStatus(const QString &dest, int status, const QString &error) {
1088     QTreeWidgetItem *item;
1089     QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
1090     if (!existing.isEmpty()) item = existing.at(0);
1091     else item = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
1092     if (status == -1) {
1093         // Job finished successfully
1094         item->setIcon(0, KIcon("dialog-ok"));
1095         item->setData(2, Qt::UserRole, 100);
1096         QTime startTime = item->data(1, Qt::UserRole + 1).toTime();
1097         int seconds = startTime.secsTo(QTime::currentTime());
1098         const QTime tm = QTime().addSecs(seconds);
1099         const QString t = i18n("Rendering finished in %1", tm.toString("hh:mm:ss"));
1100         item->setData(1, Qt::UserRole, t);
1101         QString itemGroup = item->data(0, Qt::UserRole).toString();
1102         if (itemGroup == "dvd") {
1103             emit openDvdWizard(item->text(1), item->data(0, Qt::UserRole + 1).toString());
1104         } else if (itemGroup == "websites") {
1105             QString url = item->data(0, Qt::UserRole + 1).toString();
1106             if (!url.isEmpty()) KRun *openBrowser = new KRun(url, this);
1107         }
1108     } else if (status == -2) {
1109         // Rendering crashed
1110         item->setData(1, Qt::UserRole, i18n("Rendering crashed"));
1111         item->setIcon(0, KIcon("dialog-close"));
1112         item->setData(2, Qt::UserRole, 100);
1113         m_view.error_log->append(i18n("<strong>Rendering of %1 crashed</strong><br />", dest));
1114         m_view.error_log->append(error);
1115         m_view.error_log->append("<hr />");
1116         m_view.error_box->setVisible(true);
1117     } else if (status == -3) {
1118         // User aborted job
1119         item->setData(1, Qt::UserRole, i18n("Rendering aborted"));
1120         item->setIcon(0, KIcon("dialog-cancel"));
1121         item->setData(2, Qt::UserRole, 100);
1122     }
1123     slotCheckJob();
1124 }
1125
1126 void RenderWidget::slotAbortCurrentJob() {
1127     QTreeWidgetItem *current = m_view.running_jobs->currentItem();
1128     if (current) emit abortProcess(current->text(1));
1129 }
1130
1131 void RenderWidget::slotCheckJob() {
1132     bool activate = false;
1133     QTreeWidgetItem *current = m_view.running_jobs->currentItem();
1134     if (current) {
1135         int percent = current->data(2, Qt::UserRole).toInt();
1136         if (percent < 100) activate = true;
1137     }
1138     m_view.abort_job->setEnabled(activate);
1139 }
1140
1141 void RenderWidget::parseScriptFiles() {
1142     QStringList scriptsFilter;
1143     scriptsFilter << "*.sh";
1144     m_view.scripts_list->clear();
1145
1146     QTreeWidgetItem *item;
1147     // List the project scripts
1148     QStringList scriptFiles = QDir(m_projectFolder + "/scripts").entryList(scriptsFilter, QDir::Files);
1149     for (int i = 0; i < scriptFiles.size(); ++i) {
1150         KUrl scriptpath(m_projectFolder + "/scripts/" + scriptFiles.at(i));
1151         item = new QTreeWidgetItem(m_view.scripts_list, QStringList() << scriptpath.fileName());
1152         QString target;
1153         QFile file(scriptpath.path());
1154         if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
1155             while (!file.atEnd()) {
1156                 QByteArray line = file.readLine();
1157                 if (line.startsWith("TARGET=")) {
1158                     target = QString(line).section("TARGET=", 1);
1159                     target.remove(QChar('"'));
1160                     break;
1161                 }
1162             }
1163             file.close();
1164         }
1165         item->setSizeHint(0, QSize(m_view.scripts_list->columnWidth(0), fontMetrics().height() * 2));
1166         item->setData(0, Qt::UserRole, target);
1167         item->setData(0, Qt::UserRole + 1, scriptpath.path());
1168     }
1169     bool activate = false;
1170     QTreeWidgetItemIterator it(m_view.scripts_list);
1171     if (*it) {
1172         kDebug() << "// FOUND SCRIPT ITEM:" << (*it)->text(0);
1173         // Selecting item does not work, why ???
1174         m_view.scripts_list->setCurrentItem(*it);
1175         (*it)->setSelected(true);
1176         activate = true;
1177     }
1178     kDebug() << "SELECTED SCRIPTS: " << m_view.scripts_list->selectedItems().count();
1179     m_view.start_script->setEnabled(activate);
1180     m_view.delete_script->setEnabled(activate);
1181 }
1182
1183 void RenderWidget::slotCheckScript() {
1184     bool activate = false;
1185     QTreeWidgetItemIterator it(m_view.scripts_list);
1186     if (*it) activate = true;
1187     m_view.start_script->setEnabled(activate);
1188     m_view.delete_script->setEnabled(activate);
1189 }
1190
1191 void RenderWidget::slotStartScript() {
1192     QTreeWidgetItem *item = m_view.scripts_list->currentItem();
1193     if (item) {
1194         QString path = item->data(0, Qt::UserRole + 1).toString();
1195         QProcess::startDetached(path);
1196         m_view.tabWidget->setCurrentIndex(1);
1197     }
1198 }
1199
1200 void RenderWidget::slotDeleteScript() {
1201     QTreeWidgetItem *item = m_view.scripts_list->currentItem();
1202     if (item) {
1203         QString path = item->data(0, Qt::UserRole + 1).toString();
1204         KIO::NetAccess::del(path + ".westley", this);
1205         KIO::NetAccess::del(path, this);
1206         parseScriptFiles();
1207     }
1208 }
1209
1210 void RenderWidget::slotGenerateScript() {
1211     slotExport(true);
1212 }
1213