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