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