]> git.sesse.net Git - kdenlive/blob - src/renderwidget.cpp
Replace acodec=vorbis with acodec=libvorbis if required for rendering:
[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 #include "timecode.h"
25
26 #include <KStandardDirs>
27 #include <KDebug>
28 #include <KMessageBox>
29 #include <KComboBox>
30 #include <KRun>
31 #include <KIO/NetAccess>
32 #include <KColorScheme>
33 #include <KNotification>
34 // #include <knewstuff2/engine.h>
35
36 #include <QDomDocument>
37 #include <QItemDelegate>
38 #include <QTreeWidgetItem>
39 #include <QListWidgetItem>
40 #include <QHeaderView>
41 #include <QMenu>
42 #include <QProcess>
43 #include <QInputDialog>
44
45 const int GroupRole = Qt::UserRole;
46 const int ExtensionRole = GroupRole + 1;
47 const int StandardRole = GroupRole + 2;
48 const int RenderRole = GroupRole + 3;
49 const int ParamsRole = GroupRole + 4;
50 const int EditableRole = GroupRole + 5;
51 const int MetaGroupRole = GroupRole + 6;
52 const int ExtraRole = GroupRole + 7;
53
54 // Running job status
55 const int WAITINGJOB = 0;
56 const int RUNNINGJOB = 1;
57 const int FINISHEDJOB = 2;
58
59
60 RenderWidget::RenderWidget(const QString &projectfolder, QWidget * parent) :
61         QDialog(parent),
62         m_projectFolder(projectfolder),
63         m_blockProcessing(false)
64 {
65     m_view.setupUi(this);
66     setWindowTitle(i18n("Rendering"));
67     m_view.buttonDelete->setIcon(KIcon("trash-empty"));
68     m_view.buttonDelete->setToolTip(i18n("Delete profile"));
69     m_view.buttonDelete->setEnabled(false);
70
71     m_view.buttonEdit->setIcon(KIcon("document-properties"));
72     m_view.buttonEdit->setToolTip(i18n("Edit profile"));
73     m_view.buttonEdit->setEnabled(false);
74
75     m_view.buttonSave->setIcon(KIcon("document-new"));
76     m_view.buttonSave->setToolTip(i18n("Create new profile"));
77
78     m_view.buttonInfo->setIcon(KIcon("help-about"));
79     m_view.hide_log->setIcon(KIcon("go-down"));
80
81     if (KdenliveSettings::showrenderparams()) {
82         m_view.buttonInfo->setDown(true);
83     } else m_view.advanced_params->hide();
84
85     m_view.rescale_size->setInputMask("0099\\x0099");
86     m_view.rescale_size->setText("320x240");
87
88
89     QMenu *renderMenu = new QMenu(i18n("Start Rendering"), this);
90     QAction *renderAction = renderMenu->addAction(KIcon("video-x-generic"), i18n("Render to File"));
91     connect(renderAction, SIGNAL(triggered()), this, SLOT(slotPrepareExport()));
92     QAction *scriptAction = renderMenu->addAction(KIcon("application-x-shellscript"), i18n("Generate Script"));
93     connect(scriptAction, SIGNAL(triggered()), this, SLOT(slotGenerateScript()));
94
95     m_view.buttonStart->setMenu(renderMenu);
96     m_view.buttonStart->setPopupMode(QToolButton::MenuButtonPopup);
97     m_view.buttonStart->setDefaultAction(renderAction);
98     m_view.buttonStart->setToolButtonStyle(Qt::ToolButtonTextOnly);
99     m_view.abort_job->setEnabled(false);
100     m_view.start_script->setEnabled(false);
101     m_view.delete_script->setEnabled(false);
102
103     m_view.format_list->setAlternatingRowColors(true);
104     m_view.size_list->setAlternatingRowColors(true);
105
106     parseProfiles();
107     parseScriptFiles();
108
109     connect(m_view.start_script, SIGNAL(clicked()), this, SLOT(slotStartScript()));
110     connect(m_view.delete_script, SIGNAL(clicked()), this, SLOT(slotDeleteScript()));
111     connect(m_view.scripts_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckScript()));
112     connect(m_view.running_jobs, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckJob()));
113     connect(m_view.running_jobs, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotPlayRendering(QTreeWidgetItem *, int)));
114
115     connect(m_view.buttonInfo, SIGNAL(clicked()), this, SLOT(showInfoPanel()));
116
117     connect(m_view.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
118     connect(m_view.buttonEdit, SIGNAL(clicked()), this, SLOT(slotEditProfile()));
119     connect(m_view.buttonDelete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
120     connect(m_view.abort_job, SIGNAL(clicked()), this, SLOT(slotAbortCurrentJob()));
121     connect(m_view.clean_up, SIGNAL(clicked()), this, SLOT(slotCLeanUpJobs()));
122     connect(m_view.hide_log, SIGNAL(clicked()), this, SLOT(slotHideLog()));
123
124     connect(m_view.buttonClose, SIGNAL(clicked()), this, SLOT(hide()));
125     connect(m_view.buttonClose2, SIGNAL(clicked()), this, SLOT(hide()));
126     connect(m_view.buttonClose3, SIGNAL(clicked()), this, SLOT(hide()));
127     connect(m_view.rescale, SIGNAL(toggled(bool)), m_view.rescale_size, SLOT(setEnabled(bool)));
128     connect(m_view.destination_list, SIGNAL(activated(int)), this, SLOT(refreshView()));
129     connect(m_view.out_file, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateButtons()));
130     connect(m_view.out_file, SIGNAL(urlSelected(const KUrl &)), this, SLOT(slotUpdateButtons(const KUrl &)));
131     connect(m_view.format_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshView()));
132     connect(m_view.size_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshParams()));
133
134     connect(m_view.size_list, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(slotEditItem(QListWidgetItem *)));
135
136     connect(m_view.render_guide, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
137     connect(m_view.render_zone, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
138     connect(m_view.render_full, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
139
140     connect(m_view.guide_end, SIGNAL(activated(int)), this, SLOT(slotCheckStartGuidePosition()));
141     connect(m_view.guide_start, SIGNAL(activated(int)), this, SLOT(slotCheckEndGuidePosition()));
142
143     connect(m_view.format_selection, SIGNAL(activated(int)), this, SLOT(refreshView()));
144
145     m_view.buttonStart->setEnabled(false);
146     m_view.rescale_size->setEnabled(false);
147     m_view.guides_box->setVisible(false);
148     m_view.open_dvd->setVisible(false);
149     m_view.create_chapter->setVisible(false);
150     m_view.open_browser->setVisible(false);
151     m_view.error_box->setVisible(false);
152
153     m_view.splitter->setStretchFactor(1, 5);
154     m_view.splitter->setStretchFactor(0, 2);
155
156     m_view.out_file->setMode(KFile::File);
157
158     m_view.running_jobs->setHeaderLabels(QStringList() << QString() << i18n("File") << i18n("Progress"));
159     m_view.running_jobs->setItemDelegate(new RenderViewDelegate(this));
160
161     QHeaderView *header = m_view.running_jobs->header();
162     QFontMetrics fm = fontMetrics();
163     header->setResizeMode(0, QHeaderView::Fixed);
164     header->resizeSection(0, 30);
165     header->setResizeMode(1, QHeaderView::Interactive);
166     header->setResizeMode(2, QHeaderView::Fixed);
167     header->resizeSection(1, width() * 2 / 3 - 15);
168     header->setResizeMode(2, QHeaderView::Interactive);
169     //header->setResizeMode(1, QHeaderView::Fixed);
170
171
172     m_view.scripts_list->setHeaderLabels(QStringList() << QString() << i18n("Script Files"));
173     m_view.scripts_list->setItemDelegate(new RenderViewDelegate(this));
174     header = m_view.scripts_list->header();
175     header->setResizeMode(0, QHeaderView::Fixed);
176     header->resizeSection(0, 30);
177
178     focusFirstVisibleItem();
179 }
180
181 void RenderWidget::slotEditItem(QListWidgetItem *item)
182 {
183     QString edit = item->data(EditableRole).toString();
184     if (edit.isEmpty() || !edit.endsWith("customprofiles.xml")) slotSaveProfile();
185     else slotEditProfile();
186 }
187
188 void RenderWidget::showInfoPanel()
189 {
190     if (m_view.advanced_params->isVisible()) {
191         m_view.advanced_params->setVisible(false);
192         m_view.buttonInfo->setDown(false);
193         KdenliveSettings::setShowrenderparams(false);
194     } else {
195         m_view.advanced_params->setVisible(true);
196         m_view.buttonInfo->setDown(true);
197         KdenliveSettings::setShowrenderparams(true);
198     }
199 }
200
201 void RenderWidget::setDocumentPath(const QString path)
202 {
203     if (m_view.out_file->url().directory() == KUrl(m_projectFolder).directory()) {
204         const QString fileName = m_view.out_file->url().fileName();
205         m_view.out_file->setUrl(KUrl(path + fileName));
206     }
207     m_projectFolder = path;
208     parseScriptFiles();
209
210 }
211
212 void RenderWidget::slotUpdateGuideBox()
213 {
214     m_view.guides_box->setVisible(m_view.render_guide->isChecked());
215 }
216
217 void RenderWidget::slotCheckStartGuidePosition()
218 {
219     if (m_view.guide_start->currentIndex() > m_view.guide_end->currentIndex())
220         m_view.guide_start->setCurrentIndex(m_view.guide_end->currentIndex());
221 }
222
223 void RenderWidget::slotCheckEndGuidePosition()
224 {
225     if (m_view.guide_end->currentIndex() < m_view.guide_start->currentIndex())
226         m_view.guide_end->setCurrentIndex(m_view.guide_start->currentIndex());
227 }
228
229 void RenderWidget::setGuides(QDomElement guidesxml, double duration)
230 {
231     m_view.guide_start->clear();
232     m_view.guide_end->clear();
233     QDomNodeList nodes = guidesxml.elementsByTagName("guide");
234     if (nodes.count() > 0) {
235         m_view.guide_start->addItem(i18n("Beginning"), "0");
236         m_view.render_guide->setEnabled(true);
237         m_view.create_chapter->setEnabled(true);
238     } else {
239         m_view.render_guide->setEnabled(false);
240         m_view.create_chapter->setEnabled(false);
241     }
242     double fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
243     for (int i = 0; i < nodes.count(); i++) {
244         QDomElement e = nodes.item(i).toElement();
245         if (!e.isNull()) {
246             GenTime pos = GenTime(e.attribute("time").toDouble());
247             const QString guidePos = Timecode::getStringTimecode(pos.frames(fps), fps);
248             m_view.guide_start->addItem(e.attribute("comment") + '/' + guidePos, e.attribute("time").toDouble());
249             m_view.guide_end->addItem(e.attribute("comment") + '/' + guidePos, e.attribute("time").toDouble());
250         }
251     }
252     if (nodes.count() > 0)
253         m_view.guide_end->addItem(i18n("End"), QString::number(duration));
254 }
255
256 /**
257  * Will be called when the user selects an output file via the file dialog.
258  * File extension will be added automatically.
259  */
260 void RenderWidget::slotUpdateButtons(KUrl url)
261 {
262     if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
263     else {
264         updateButtons(); // This also checks whether the selected format is available
265         //m_view.buttonStart->setEnabled(true);
266     }
267     if (url != 0) {
268         QListWidgetItem *item = m_view.size_list->currentItem();
269         QString extension = item->data(ExtensionRole).toString();
270         url = filenameWithExtension(url, extension);
271         m_view.out_file->setUrl(url);
272     }
273 }
274
275 /**
276  * Will be called when the user changes the output file path in the text line.
277  * File extension must NOT be added, would make editing impossible!
278  */
279 void RenderWidget::slotUpdateButtons()
280 {
281     if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
282     else updateButtons(); // This also checks whether the selected format is available
283     //else m_view.buttonStart->setEnabled(true);
284 }
285
286 void RenderWidget::slotSaveProfile()
287 {
288     //TODO: update to correctly use metagroups
289     Ui::SaveProfile_UI ui;
290     QDialog *d = new QDialog(this);
291     ui.setupUi(d);
292
293     for (int i = 0; i < m_view.destination_list->count(); i++)
294         ui.destination_list->addItem(m_view.destination_list->itemIcon(i), m_view.destination_list->itemText(i), m_view.destination_list->itemData(i, Qt::UserRole));
295
296     ui.destination_list->setCurrentIndex(m_view.destination_list->currentIndex());
297     QString dest = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
298
299     QString customGroup = m_view.format_list->currentItem()->text();
300     if (customGroup.isEmpty()) customGroup = i18n("Custom");
301     ui.group_name->setText(customGroup);
302
303     ui.parameters->setText(m_view.advanced_params->toPlainText());
304     ui.extension->setText(m_view.size_list->currentItem()->data(ExtensionRole).toString());
305     ui.profile_name->setFocus();
306
307     if (d->exec() == QDialog::Accepted && !ui.profile_name->text().simplified().isEmpty()) {
308         QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
309         QDomDocument doc;
310         QFile file(exportFile);
311         doc.setContent(&file, false);
312         file.close();
313         QDomElement documentElement;
314         QDomElement profiles = doc.documentElement();
315         if (profiles.isNull() || profiles.tagName() != "profiles") {
316             doc.clear();
317             profiles = doc.createElement("profiles");
318             profiles.setAttribute("version", 1);
319             doc.appendChild(profiles);
320         }
321         int version = profiles.attribute("version", 0).toInt();
322         if (version < 1) {
323             kDebug() << "// OLD profile version";
324             doc.clear();
325             profiles = doc.createElement("profiles");
326             profiles.setAttribute("version", 1);
327             doc.appendChild(profiles);
328         }
329
330         QString newProfileName = ui.profile_name->text().simplified();
331         QString newGroupName = ui.group_name->text().simplified();
332         if (newGroupName.isEmpty()) newGroupName = i18n("Custom");
333         QString newMetaGroupId = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
334         QDomNodeList profilelist = doc.elementsByTagName("profile");
335         int i = 0;
336         while (!profilelist.item(i).isNull()) {
337             // make sure a profile with same name doesn't exist
338             documentElement = profilelist.item(i).toElement();
339             QString profileName = documentElement.attribute("name");
340             if (profileName == newProfileName) {
341                 // a profile with that same name already exists
342                 bool ok;
343                 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);
344                 if (!ok) return;
345                 if (profileName == newProfileName) {
346                     profiles.removeChild(profilelist.item(i));
347                     break;
348                 }
349             }
350             i++;
351         }
352
353         QDomElement profileElement = doc.createElement("profile");
354         profileElement.setAttribute("name", newProfileName);
355         profileElement.setAttribute("category", newGroupName);
356         profileElement.setAttribute("destinationid", newMetaGroupId);
357         profileElement.setAttribute("extension", ui.extension->text().simplified());
358         profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
359         profiles.appendChild(profileElement);
360
361         //QCString save = doc.toString().utf8();
362
363         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
364             KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
365             delete d;
366             return;
367         }
368         QTextStream out(&file);
369         out << doc.toString();
370         if (file.error() != QFile::NoError) {
371             KMessageBox::error(this, i18n("Cannot write to file %1", exportFile));
372             file.close();
373             delete d;
374             return;
375         }
376         file.close();
377         parseProfiles(newMetaGroupId, newGroupName, newProfileName);
378     }
379     delete d;
380 }
381
382 void RenderWidget::slotEditProfile()
383 {
384     QListWidgetItem *item = m_view.size_list->currentItem();
385     if (!item) return;
386     QString currentGroup = m_view.format_list->currentItem()->text();
387
388     QString params = item->data(ParamsRole).toString();
389     QString extension = item->data(ExtensionRole).toString();
390     QString currentProfile = item->text();
391
392     Ui::SaveProfile_UI ui;
393     QDialog *d = new QDialog(this);
394     ui.setupUi(d);
395
396     for (int i = 0; i < m_view.destination_list->count(); i++)
397         ui.destination_list->addItem(m_view.destination_list->itemIcon(i), m_view.destination_list->itemText(i), m_view.destination_list->itemData(i, Qt::UserRole));
398
399     ui.destination_list->setCurrentIndex(m_view.destination_list->currentIndex());
400     QString dest = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
401
402     QString customGroup = m_view.format_list->currentItem()->text();
403     if (customGroup.isEmpty()) customGroup = i18n("Custom");
404     ui.group_name->setText(customGroup);
405
406     ui.profile_name->setText(currentProfile);
407     ui.extension->setText(extension);
408     ui.parameters->setText(params);
409     ui.profile_name->setFocus();
410     d->setWindowTitle(i18n("Edit Profile"));
411     if (d->exec() == QDialog::Accepted) {
412         slotDeleteProfile(false);
413         QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
414         QDomDocument doc;
415         QFile file(exportFile);
416         doc.setContent(&file, false);
417         file.close();
418         QDomElement documentElement;
419         QDomElement profiles = doc.documentElement();
420
421         if (profiles.isNull() || profiles.tagName() != "profiles") {
422             doc.clear();
423             profiles = doc.createElement("profiles");
424             profiles.setAttribute("version", 1);
425             doc.appendChild(profiles);
426         }
427
428         int version = profiles.attribute("version", 0).toInt();
429         if (version < 1) {
430             kDebug() << "// OLD profile version";
431             doc.clear();
432             profiles = doc.createElement("profiles");
433             profiles.setAttribute("version", 1);
434             doc.appendChild(profiles);
435         }
436
437         QString newProfileName = ui.profile_name->text().simplified();
438         QString newGroupName = ui.group_name->text().simplified();
439         if (newGroupName.isEmpty()) newGroupName = i18n("Custom");
440         QString newMetaGroupId = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
441         QDomNodeList profilelist = doc.elementsByTagName("profile");
442         int i = 0;
443         while (!profilelist.item(i).isNull()) {
444             // make sure a profile with same name doesn't exist
445             documentElement = profilelist.item(i).toElement();
446             QString profileName = documentElement.attribute("name");
447             if (profileName == newProfileName) {
448                 // a profile with that same name already exists
449                 bool ok;
450                 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);
451                 if (!ok) return;
452                 if (profileName == newProfileName) {
453                     profiles.removeChild(profilelist.item(i));
454                     break;
455                 }
456             }
457             i++;
458         }
459
460         QDomElement profileElement = doc.createElement("profile");
461         profileElement.setAttribute("name", newProfileName);
462         profileElement.setAttribute("category", newGroupName);
463         profileElement.setAttribute("destinationid", newMetaGroupId);
464         profileElement.setAttribute("extension", ui.extension->text().simplified());
465         profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
466         profiles.appendChild(profileElement);
467
468         //QCString save = doc.toString().utf8();
469         delete d;
470         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
471             KMessageBox::error(this, i18n("Cannot write to file %1", exportFile));
472             return;
473         }
474         QTextStream out(&file);
475         out << doc.toString();
476         if (file.error() != QFile::NoError) {
477             KMessageBox::error(this, i18n("Cannot write to file %1", exportFile));
478             file.close();
479             return;
480         }
481         file.close();
482         parseProfiles(newMetaGroupId, newGroupName, newProfileName);
483     } else delete d;
484 }
485
486 void RenderWidget::slotDeleteProfile(bool refresh)
487 {
488     //TODO: delete a profile installed by KNewStuff the easy way
489     /*
490     QString edit = m_view.size_list->currentItem()->data(EditableRole).toString();
491     if (!edit.endsWith("customprofiles.xml")) {
492         // This is a KNewStuff installed file, process through KNS
493         KNS::Engine engine(0);
494         if (engine.init("kdenlive_render.knsrc")) {
495             KNS::Entry::List entries;
496         }
497         return;
498     }*/
499     QString currentGroup = m_view.format_list->currentItem()->text();
500     QString currentProfile = m_view.size_list->currentItem()->text();
501     QString metaGroupId = m_view.destination_list->itemData(m_view.destination_list->currentIndex(), Qt::UserRole).toString();
502
503     QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
504     QDomDocument doc;
505     QFile file(exportFile);
506     doc.setContent(&file, false);
507     file.close();
508
509     QDomElement documentElement;
510     QDomNodeList profiles = doc.elementsByTagName("profile");
511     int i = 0;
512     QString groupName;
513     QString profileName;
514     QString destination;
515
516     while (!profiles.item(i).isNull()) {
517         documentElement = profiles.item(i).toElement();
518         profileName = documentElement.attribute("name");
519         groupName = documentElement.attribute("category");
520         destination = documentElement.attribute("destinationid");
521
522         if (profileName == currentProfile && groupName == currentGroup && destination == metaGroupId) {
523             kDebug() << "// GOT it: " << profileName;
524             doc.documentElement().removeChild(profiles.item(i));
525             break;
526         }
527         i++;
528     }
529
530     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
531         KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
532         return;
533     }
534     QTextStream out(&file);
535     out << doc.toString();
536     if (file.error() != QFile::NoError) {
537         KMessageBox::error(this, i18n("Cannot write to file %1", exportFile));
538         file.close();
539         return;
540     }
541     file.close();
542     if (refresh) {
543         parseProfiles(metaGroupId, currentGroup);
544         focusFirstVisibleItem();
545     }
546 }
547
548 void RenderWidget::updateButtons()
549 {
550     if (!m_view.size_list->currentItem() || m_view.size_list->currentItem()->isHidden()) {
551         m_view.buttonSave->setEnabled(false);
552         m_view.buttonDelete->setEnabled(false);
553         m_view.buttonEdit->setEnabled(false);
554         m_view.buttonStart->setEnabled(false);
555     } else {
556         m_view.buttonSave->setEnabled(true);
557         m_view.buttonStart->setEnabled(m_view.size_list->currentItem()->toolTip().isEmpty());
558         QString edit = m_view.size_list->currentItem()->data(EditableRole).toString();
559         if (edit.isEmpty() || !edit.endsWith("customprofiles.xml")) {
560             m_view.buttonDelete->setEnabled(false);
561             m_view.buttonEdit->setEnabled(false);
562         } else {
563             m_view.buttonDelete->setEnabled(true);
564             m_view.buttonEdit->setEnabled(true);
565         }
566     }
567 }
568
569
570 void RenderWidget::focusFirstVisibleItem()
571 {
572     if (m_view.size_list->currentItem() && !m_view.size_list->currentItem()->isHidden()) {
573         updateButtons();
574         return;
575     }
576     for (int ix = 0; ix < m_view.size_list->count(); ix++) {
577         QListWidgetItem *item = m_view.size_list->item(ix);
578         if (item && !item->isHidden()) {
579             m_view.size_list->setCurrentRow(ix);
580             break;
581         }
582     }
583     if (!m_view.size_list->currentItem()) m_view.size_list->setCurrentRow(0);
584     updateButtons();
585 }
586
587 void RenderWidget::slotPrepareExport(bool scriptExport)
588 {
589     if (!QFile::exists(KdenliveSettings::rendererpath())) {
590         KMessageBox::sorry(this, i18n("Cannot find the melt program required for rendering (part of Mlt)"));
591         return;
592     }
593     if (m_view.play_after->isChecked() && KdenliveSettings::defaultplayerapp().isEmpty())
594         KMessageBox::sorry(this, i18n("Cannot play video after rendering because the default video player application is not set.\nPlease define it in Kdenlive settings dialog."));
595     QString chapterFile;
596     if (m_view.create_chapter->isChecked()) chapterFile = m_view.out_file->url().path() + ".dvdchapter";
597     emit prepareRenderingData(scriptExport, m_view.render_zone->isChecked(), chapterFile);
598 }
599
600
601 void RenderWidget::slotExport(bool scriptExport, int zoneIn, int zoneOut, const QString &playlistPath, const QString &scriptPath)
602 {
603     QListWidgetItem *item = m_view.size_list->currentItem();
604     if (!item) return;
605
606     QString dest = m_view.out_file->url().path();
607     if (dest.isEmpty()) return;
608
609     // Check whether target file has an extension.
610     // If not, ask whether extension should be added or not.
611     QString extension = item->data(ExtensionRole).toString();
612     if (!dest.endsWith(extension)) {
613         if (KMessageBox::questionYesNo(this, i18n("File has no extension. Add extension (%1)?", extension)) == KMessageBox::Yes) {
614             dest.append("." + extension);
615         }
616     }
617
618     QFile f(dest);
619     if (f.exists()) {
620         if (KMessageBox::warningYesNo(this, i18n("Output file already exists. Do you want to overwrite it?")) != KMessageBox::Yes)
621             return;
622     }
623
624     QStringList overlayargs;
625     if (m_view.tc_overlay->isChecked()) {
626         QString filterFile = KStandardDirs::locate("appdata", "metadata.properties");
627         overlayargs << "meta.attr.timecode=1" << "meta.attr.timecode.markup=#timecode";
628         overlayargs << "-attach" << "data_feed:attr_check" << "-attach";
629         overlayargs << "data_show:" + filterFile << "_loader=1" << "dynamic=1";
630     }
631
632     QStringList render_process_args;
633
634     if (!scriptExport) render_process_args << "-erase";
635     if (KdenliveSettings::usekuiserver()) render_process_args << "-kuiserver";
636
637     double guideStart = 0;
638     double guideEnd = 0;
639
640     if (m_view.render_zone->isChecked()) render_process_args << "in=" + QString::number(zoneIn) << "out=" + QString::number(zoneOut);
641     else if (m_view.render_guide->isChecked()) {
642         double fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
643         guideStart = m_view.guide_start->itemData(m_view.guide_start->currentIndex()).toDouble();
644         guideEnd = m_view.guide_end->itemData(m_view.guide_end->currentIndex()).toDouble();
645         render_process_args << "in=" + QString::number(GenTime(guideStart).frames(fps)) << "out=" + QString::number(GenTime(guideEnd).frames(fps));
646     }
647
648     render_process_args << overlayargs;
649     render_process_args << KdenliveSettings::rendererpath() << m_profile.path << item->data(RenderRole).toString();
650     if (m_view.play_after->isChecked()) render_process_args << KdenliveSettings::KdenliveSettings::defaultplayerapp();
651     else render_process_args << "-";
652
653     QString renderArgs = m_view.advanced_params->toPlainText().simplified();
654
655     // Adjust frame scale
656     int width;
657     int height;
658     if (m_view.rescale->isChecked() && m_view.rescale->isEnabled()) {
659         width = m_view.rescale_size->text().section('x', 0, 0).toInt();
660         height = m_view.rescale_size->text().section('x', 1, 1).toInt();
661     } else {
662         width = m_profile.width;
663         height = m_profile.height;
664     }
665     renderArgs.replace("%dar", '@' + QString::number(m_profile.display_aspect_num) + '/' + QString::number(m_profile.display_aspect_den));
666
667     // Adjust scanning
668     if (m_view.scanning_list->currentIndex() == 1) renderArgs.append(" progressive=1");
669     else if (m_view.scanning_list->currentIndex() == 2) renderArgs.append(" progressive=0");
670
671     // disable audio if requested
672     if (!m_view.export_audio->isChecked())
673         renderArgs.append(" an=1 ");
674
675     // Check if the rendering profile is different from project profile,
676     // in which case we need to use the producer_comsumer from MLT
677     QString std = renderArgs;
678     QString destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
679     const QString currentSize = QString::number(width) + 'x' + QString::number(height);
680     QString subsize = currentSize;
681     if (std.startsWith("s=")) {
682         subsize = std.section(' ', 0, 0).toLower();
683         subsize = subsize.section("=", 1, 1);
684     } else if (std.contains(" s=")) {
685         subsize = std.section(" s=", 1, 1);
686         subsize = subsize.section(' ', 0, 0).toLower();
687     } else if (destination != "audioonly" && m_view.rescale->isChecked() && m_view.rescale->isEnabled()) {
688         subsize = QString(" s=%1x%2").arg(width).arg(height);
689         // Add current size parameter
690         renderArgs.append(subsize);
691     }
692     bool resizeProfile = (subsize != currentSize);
693     QStringList paramsList = renderArgs.split(" ", QString::SkipEmptyParts);
694     for (int i = 0; i < paramsList.count(); i++) {
695         if (paramsList.at(i).startsWith("profile=")) {
696             if (paramsList.at(i).section('=', 1) != m_profile.path) resizeProfile = true;
697             break;
698         }
699     }
700
701     if (resizeProfile) render_process_args << "consumer:" + playlistPath;
702     else render_process_args << playlistPath;
703     render_process_args << dest;
704     render_process_args << paramsList;
705
706     QString group = m_view.size_list->currentItem()->data(MetaGroupRole).toString();
707
708     QStringList renderParameters;
709     renderParameters << dest << item->data(RenderRole).toString() << renderArgs.simplified();
710     renderParameters << QString::number(zoneIn) << QString::number(zoneOut) << QString::number(m_view.play_after->isChecked());
711     renderParameters << QString::number(guideStart) << QString::number(guideEnd) << QString::number(resizeProfile);
712
713     QString scriptName;
714     if (scriptExport) {
715         // Generate script file
716         QFile file(scriptPath);
717         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
718             KMessageBox::error(this, i18n("Cannot write to file %1", scriptPath));
719             return;
720         }
721         QString renderer = QCoreApplication::applicationDirPath() + QString("/kdenlive_render");
722         if (!QFile::exists(renderer)) renderer = "kdenlive_render";
723         QTextStream outStream(&file);
724         outStream << "#! /bin/sh" << "\n" << "\n";
725         outStream << "SOURCE=" << "\"" + playlistPath + "\"" << "\n";
726         outStream << "TARGET=" << "\"" + dest + "\"" << "\n";
727         outStream << "RENDERER=" << "\"" + renderer + "\"" << "\n";
728         outStream << "MELT=" << "\"" + render_process_args.takeFirst() + "\"" << "\n";
729         outStream << "PARAMETERS=" << "\"" + render_process_args.join(" ") + "\"" << "\n";
730         outStream << "$RENDERER $MELT $PARAMETERS" << "\n" << "\n";
731         if (file.error() != QFile::NoError) {
732             KMessageBox::error(this, i18n("Cannot write to file %1", scriptPath));
733             file.close();
734             return;
735         }
736         file.close();
737         QFile::setPermissions(scriptPath, file.permissions() | QFile::ExeUser);
738
739         QTimer::singleShot(400, this, SLOT(parseScriptFiles()));
740         m_view.tabWidget->setCurrentIndex(2);
741         return;
742     }
743     renderParameters << scriptName;
744     m_view.tabWidget->setCurrentIndex(1);
745
746     // Save rendering profile to document
747     emit selectedRenderProfile(m_view.size_list->currentItem()->data(MetaGroupRole).toString(), m_view.size_list->currentItem()->text(), dest);
748
749     // insert item in running jobs list
750     QTreeWidgetItem *renderItem;
751     QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
752     if (!existing.isEmpty()) {
753         renderItem = existing.at(0);
754         if (renderItem->data(1, Qt::UserRole + 2).toInt() == RUNNINGJOB) {
755             KMessageBox::information(this, i18n("There is already a job writing file:<br><b>%1</b><br>Abort the job if you want to overwrite it...", dest), i18n("Already running"));
756             return;
757         }
758         renderItem->setData(1, Qt::UserRole + 4, QString());
759     } else {
760         renderItem = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
761     }
762     renderItem->setData(1, Qt::UserRole + 2, WAITINGJOB);
763     renderItem->setIcon(0, KIcon("media-playback-pause"));
764     renderItem->setData(1, Qt::UserRole, i18n("Waiting..."));
765     renderItem->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
766     renderItem->setData(1, Qt::UserRole + 1, QTime::currentTime());
767
768     // Set rendering type
769     if (group == "dvd") {
770         if (m_view.open_dvd->isChecked()) {
771             renderItem->setData(0, Qt::UserRole, group);
772             if (renderArgs.contains("profile=")) {
773                 // rendering profile contains an MLT profile, so pass it to the running jog item, useful for dvd
774                 QString prof = renderArgs.section("profile=", 1, 1);
775                 prof = prof.section(' ', 0, 0);
776                 kDebug() << "// render profile: " << prof;
777                 renderItem->setData(0, Qt::UserRole + 1, prof);
778             }
779         }
780     } else {
781         if (group == "websites" && m_view.open_browser->isChecked()) {
782             renderItem->setData(0, Qt::UserRole, group);
783             // pass the url
784             QString url = m_view.size_list->currentItem()->data(ExtraRole).toString();
785             renderItem->setData(0, Qt::UserRole + 1, url);
786         }
787     }
788     renderItem->setData(1, Qt::UserRole + 3, render_process_args);
789     checkRenderStatus();
790 }
791
792 void RenderWidget::checkRenderStatus()
793 {
794     if (m_blockProcessing) return;
795     QTreeWidgetItem *item = m_view.running_jobs->topLevelItem(0);
796     while (item) {
797         if (item->data(1, Qt::UserRole + 2).toInt() == RUNNINGJOB) return;
798         item = m_view.running_jobs->itemBelow(item);
799     }
800     item = m_view.running_jobs->topLevelItem(0);
801     while (item) {
802         if (item->data(1, Qt::UserRole + 2).toInt() == WAITINGJOB) {
803             item->setData(1, Qt::UserRole + 1, QTime::currentTime());
804             if (item->data(1, Qt::UserRole + 4).isNull()) {
805                 // Normal render process
806                 QString renderer = QCoreApplication::applicationDirPath() + QString("/kdenlive_render");
807                 if (!QFile::exists(renderer)) renderer = "kdenlive_render";
808                 if (QProcess::startDetached(renderer, item->data(1, Qt::UserRole + 3).toStringList()) == false) {
809                     item->setData(1, Qt::UserRole + 2, FINISHEDJOB);
810                     item->setData(1, Qt::UserRole, i18n("Rendering crashed"));
811                     item->setIcon(0, KIcon("dialog-close"));
812                     item->setData(2, Qt::UserRole, 100);
813                 } else KNotification::event("RenderStarted", i18n("Rendering <i>%1</i> started", item->text(1)), QPixmap(), this);
814             } else {
815                 // Script item
816                 if (QProcess::startDetached(item->data(1, Qt::UserRole + 3).toString()) == false) {
817                     item->setData(1, Qt::UserRole + 2, FINISHEDJOB);
818                     item->setData(1, Qt::UserRole, i18n("Rendering crashed"));
819                     item->setIcon(0, KIcon("dialog-close"));
820                     item->setData(2, Qt::UserRole, 100);
821                 }
822             }
823             break;
824         }
825         item = m_view.running_jobs->itemBelow(item);
826     }
827 }
828
829 int RenderWidget::waitingJobsCount() const
830 {
831     int count = 0;
832     QTreeWidgetItem *item = m_view.running_jobs->topLevelItem(0);
833     while (item) {
834         if (item->data(1, Qt::UserRole + 2).toInt() == WAITINGJOB) count++;
835         item = m_view.running_jobs->itemBelow(item);
836     }
837     return count;
838 }
839
840 void RenderWidget::setProfile(MltVideoProfile profile)
841 {
842     m_profile = profile;
843     //WARNING: this way to tell the video standard is a bit hackish...
844     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);
845     else m_view.format_selection->setCurrentIndex(1);
846     m_view.scanning_list->setCurrentIndex(0);
847     refreshView();
848 }
849
850 void RenderWidget::refreshView()
851 {
852     m_view.size_list->blockSignals(true);
853     QListWidgetItem *sizeItem;
854
855     QString destination;
856     KIcon brokenIcon("dialog-close");
857     if (m_view.destination_list->currentIndex() > 0)
858         destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
859
860
861     if (destination == "dvd") {
862         m_view.open_dvd->setVisible(true);
863         m_view.create_chapter->setVisible(true);
864     } else {
865         m_view.open_dvd->setVisible(false);
866         m_view.create_chapter->setVisible(false);
867     }
868     if (destination == "websites") m_view.open_browser->setVisible(true);
869     else m_view.open_browser->setVisible(false);
870     if (!destination.isEmpty() && QString("dvd websites audioonly").contains(destination))
871         m_view.rescale->setEnabled(false);
872     else m_view.rescale->setEnabled(true);
873     // hide groups that are not in the correct destination
874     for (int i = 0; i < m_view.format_list->count(); i++) {
875         sizeItem = m_view.format_list->item(i);
876         if (sizeItem->data(MetaGroupRole).toString() == destination) {
877             sizeItem->setHidden(false);
878             //kDebug() << "// SET GRP:: " << sizeItem->text() << ", METY:" << sizeItem->data(MetaGroupRole).toString();
879         } else sizeItem->setHidden(true);
880     }
881
882     // activate first visible item
883     QListWidgetItem * item = m_view.format_list->currentItem();
884     if (!item || item->isHidden()) {
885         for (int i = 0; i < m_view.format_list->count(); i++) {
886             if (!m_view.format_list->item(i)->isHidden()) {
887                 m_view.format_list->setCurrentRow(i);
888                 break;
889             }
890         }
891         item = m_view.format_list->currentItem();
892     }
893     if (!item || item->isHidden()) {
894         m_view.format_list->setEnabled(false);
895         m_view.size_list->setEnabled(false);
896         return;
897     } else {
898         m_view.format_list->setEnabled(true);
899         m_view.size_list->setEnabled(true);
900     }
901     int count = 0;
902     for (int i = 0; i < m_view.format_list->count() && count < 2; i++) {
903         if (!m_view.format_list->isRowHidden(i)) count++;
904     }
905     if (count > 1) m_view.format_list->setVisible(true);
906     else m_view.format_list->setVisible(false);
907     QString std;
908     QString group = item->text();
909     bool firstSelected = false;
910     const QStringList formatsList = KdenliveSettings::supportedformats();
911     const QStringList vcodecsList = KdenliveSettings::videocodecs();
912     const QStringList acodecsList = KdenliveSettings::audiocodecs();
913
914     KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
915     const QColor disabled = scheme.foreground(KColorScheme::InactiveText).color();
916     const QColor disabledbg = scheme.background(KColorScheme::NegativeBackground).color();
917
918     for (int i = 0; i < m_view.size_list->count(); i++) {
919         sizeItem = m_view.size_list->item(i);
920         if ((sizeItem->data(GroupRole).toString() == group || sizeItem->data(GroupRole).toString().isEmpty()) && sizeItem->data(MetaGroupRole).toString() == destination) {
921             std = sizeItem->data(StandardRole).toString();
922             if (!std.isEmpty()) {
923                 if (std.contains("PAL", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 0);
924                 else if (std.contains("NTSC", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 1);
925             } else {
926                 sizeItem->setHidden(false);
927                 if (!firstSelected) m_view.size_list->setCurrentItem(sizeItem);
928                 firstSelected = true;
929             }
930
931             if (!sizeItem->isHidden()) {
932                 // Make sure the selected profile uses an installed avformat codec / format
933                 std = sizeItem->data(ParamsRole).toString();
934                 if (!formatsList.isEmpty()) {
935                     QString format;
936                     if (std.startsWith("f=")) format = std.section("f=", 1, 1);
937                     else if (std.contains(" f=")) format = std.section(" f=", 1, 1);
938                     if (!format.isEmpty()) {
939                         format = format.section(' ', 0, 0).toLower();
940                         if (!formatsList.contains(format)) {
941                             kDebug() << "***** UNSUPPORTED F: " << format;
942                             //sizeItem->setHidden(true);
943                             //sizeItem->setFlags(Qt::ItemIsSelectable);
944                             sizeItem->setToolTip(i18n("Unsupported video format: %1", format));
945                             sizeItem->setIcon(brokenIcon);
946                             sizeItem->setForeground(disabled);
947                         }
948                     }
949                 }
950                 if (!acodecsList.isEmpty() && !sizeItem->isHidden()) {
951                     QString format;
952                     if (std.startsWith("acodec=")) format = std.section("acodec=", 1, 1);
953                     else if (std.contains(" acodec=")) format = std.section(" acodec=", 1, 1);
954                     if (!format.isEmpty()) {
955                         format = format.section(' ', 0, 0).toLower();
956                         if (!acodecsList.contains(format)) {
957                             kDebug() << "*****  UNSUPPORTED ACODEC: " << format;
958                             //sizeItem->setHidden(true);
959                             //sizeItem->setFlags(Qt::ItemIsSelectable);
960                             sizeItem->setToolTip(i18n("Unsupported audio codec: %1", format));
961                             sizeItem->setIcon(brokenIcon);
962                             sizeItem->setForeground(disabled);
963                             sizeItem->setBackground(disabledbg);
964                         }
965                     }
966                 }
967                 if (!vcodecsList.isEmpty() && !sizeItem->isHidden()) {
968                     QString format;
969                     if (std.startsWith("vcodec=")) format = std.section("vcodec=", 1, 1);
970                     else if (std.contains(" vcodec=")) format = std.section(" vcodec=", 1, 1);
971                     if (!format.isEmpty()) {
972                         format = format.section(' ', 0, 0).toLower();
973                         if (!vcodecsList.contains(format)) {
974                             kDebug() << "*****  UNSUPPORTED VCODEC: " << format;
975                             //sizeItem->setHidden(true);
976                             //sizeItem->setFlags(Qt::ItemIsSelectable);
977                             sizeItem->setToolTip(i18n("Unsupported video codec: %1", format));
978                             sizeItem->setIcon(brokenIcon);
979                             sizeItem->setForeground(disabled);
980                         }
981                     }
982                 }
983             }
984         } else sizeItem->setHidden(true);
985     }
986     focusFirstVisibleItem();
987     m_view.size_list->blockSignals(false);
988     refreshParams();
989 }
990
991 KUrl RenderWidget::filenameWithExtension(KUrl url, QString extension)
992 {
993     QString path;
994     if (!url.isEmpty()) {
995         path = url.path();
996         int pos = path.lastIndexOf('.') + 1;
997         if (pos == 0) path.append('.' + extension);
998         else path = path.left(pos) + extension;
999
1000     } else {
1001         path = m_projectFolder + "untitled." + extension;
1002     }
1003     return KUrl(path);
1004 }
1005
1006
1007 /**
1008  * Called when a new format or size has been selected.
1009  */
1010 void RenderWidget::refreshParams()
1011 {
1012     // Format not available (e.g. codec not installed); Disable start button
1013     QListWidgetItem *item = m_view.size_list->currentItem();
1014     if (!item || item->isHidden()) {
1015         m_view.advanced_params->clear();
1016         m_view.buttonStart->setEnabled(false);
1017         return;
1018     }
1019     QString params = item->data(ParamsRole).toString();
1020     QString extension = item->data(ExtensionRole).toString();
1021     m_view.advanced_params->setPlainText(params);
1022     QString destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
1023     if (params.contains(" s=") || destination == "audioonly") {
1024         // profile has a fixed size, do not allow resize
1025         m_view.rescale->setEnabled(false);
1026         m_view.rescale_size->setEnabled(false);
1027     } else {
1028         m_view.rescale->setEnabled(true);
1029         m_view.rescale_size->setEnabled(true);
1030     }
1031     KUrl url = filenameWithExtension(m_view.out_file->url(), extension);
1032     m_view.out_file->setUrl(url);
1033 //     if (!url.isEmpty()) {
1034 //         QString path = url.path();
1035 //         int pos = path.lastIndexOf('.') + 1;
1036 //  if (pos == 0) path.append('.' + extension);
1037 //         else path = path.left(pos) + extension;
1038 //         m_view.out_file->setUrl(KUrl(path));
1039 //     } else {
1040 //         m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
1041 //     }
1042     m_view.out_file->setFilter("*." + extension);
1043     QString edit = item->data(EditableRole).toString();
1044     if (edit.isEmpty() || !edit.endsWith("customprofiles.xml")) {
1045         m_view.buttonDelete->setEnabled(false);
1046         m_view.buttonEdit->setEnabled(false);
1047     } else {
1048         m_view.buttonDelete->setEnabled(true);
1049         m_view.buttonEdit->setEnabled(true);
1050     }
1051
1052     m_view.buttonStart->setEnabled(m_view.size_list->currentItem()->toolTip().isEmpty());
1053 }
1054
1055 void RenderWidget::reloadProfiles()
1056 {
1057     parseProfiles();
1058 }
1059
1060 void RenderWidget::parseProfiles(QString meta, QString group, QString profile)
1061 {
1062     m_view.size_list->clear();
1063     m_view.format_list->clear();
1064     m_view.destination_list->clear();
1065     m_view.destination_list->addItem(KIcon("video-x-generic"), i18n("File rendering"));
1066     m_view.destination_list->addItem(KIcon("media-optical"), i18n("DVD"), "dvd");
1067     m_view.destination_list->addItem(KIcon("audio-x-generic"), i18n("Audio only"), "audioonly");
1068     m_view.destination_list->addItem(KIcon("applications-internet"), i18n("Web sites"), "websites");
1069     m_view.destination_list->addItem(KIcon("applications-multimedia"), i18n("Media players"), "mediaplayers");
1070     m_view.destination_list->addItem(KIcon("drive-harddisk"), i18n("Lossless / HQ"), "lossless");
1071     m_view.destination_list->addItem(KIcon("pda"), i18n("Mobile devices"), "mobile");
1072
1073     QString exportFile = KStandardDirs::locate("appdata", "export/profiles.xml");
1074     parseFile(exportFile, false);
1075
1076
1077     QString exportFolder = KStandardDirs::locateLocal("appdata", "export/");
1078     QDir directory = QDir(exportFolder);
1079     QStringList filter;
1080     filter << "*.xml";
1081     QStringList fileList = directory.entryList(filter, QDir::Files);
1082     // We should parse customprofiles.xml in last position, so that user profiles
1083     // can also override profiles installed by KNewStuff
1084     fileList.removeAll("customprofiles.xml");
1085     foreach(const QString &filename, fileList)
1086     parseFile(exportFolder + filename, true);
1087     if (QFile::exists(exportFolder + "customprofiles.xml")) parseFile(exportFolder + "customprofiles.xml", true);
1088
1089     if (!meta.isEmpty()) {
1090         m_view.destination_list->blockSignals(true);
1091         m_view.destination_list->setCurrentIndex(m_view.destination_list->findData(meta));
1092         m_view.destination_list->blockSignals(false);
1093     }
1094     refreshView();
1095     QList<QListWidgetItem *> child;
1096     if (!group.isEmpty()) child = m_view.format_list->findItems(group, Qt::MatchExactly);
1097     if (!child.isEmpty()) {
1098         for (int i = 0; i < child.count(); i++) {
1099             if (child.at(i)->data(MetaGroupRole).toString() == meta) {
1100                 m_view.format_list->setCurrentItem(child.at(i));
1101                 break;
1102             }
1103         }
1104     }
1105     child.clear();
1106     if (!profile.isEmpty()) child = m_view.size_list->findItems(profile, Qt::MatchExactly);
1107     if (!child.isEmpty()) m_view.size_list->setCurrentItem(child.at(0));
1108 }
1109
1110 void RenderWidget::parseFile(QString exportFile, bool editable)
1111 {
1112     kDebug() << "// Parsing file: " << exportFile;
1113     kDebug() << "------------------------------";
1114     QDomDocument doc;
1115     QFile file(exportFile);
1116     doc.setContent(&file, false);
1117     file.close();
1118     QDomElement documentElement;
1119     QDomElement profileElement;
1120     QString extension;
1121     QListWidgetItem *item;
1122     QDomNodeList groups = doc.elementsByTagName("group");
1123
1124     const QStringList acodecsList = KdenliveSettings::audiocodecs();
1125     bool replaceVorbisCodec = false;
1126     if (!acodecsList.contains("vorbis") && acodecsList.contains("libvorbis")) replaceVorbisCodec = true;
1127
1128
1129     if (editable || groups.count() == 0) {
1130         QDomElement profiles = doc.documentElement();
1131         if (editable && profiles.attribute("version", 0).toInt() < 1) {
1132             kDebug() << "// OLD profile version";
1133             // this is an old profile version, update it
1134             QDomDocument newdoc;
1135             QDomElement newprofiles = newdoc.createElement("profiles");
1136             newprofiles.setAttribute("version", 1);
1137             newdoc.appendChild(newprofiles);
1138             QDomNodeList profilelist = doc.elementsByTagName("profile");
1139             for (int i = 0; i < profilelist.count(); i++) {
1140                 QString category = i18n("Custom");
1141                 QString extension;
1142                 QDomNode parent = profilelist.at(i).parentNode();
1143                 if (!parent.isNull()) {
1144                     QDomElement parentNode = parent.toElement();
1145                     if (parentNode.hasAttribute("name")) category = parentNode.attribute("name");
1146                     extension = parentNode.attribute("extension");
1147                 }
1148                 profilelist.at(i).toElement().setAttribute("category", category);
1149                 if (!extension.isEmpty()) profilelist.at(i).toElement().setAttribute("extension", extension);
1150                 QDomNode n = profilelist.at(i).cloneNode();
1151                 newprofiles.appendChild(newdoc.importNode(n, true));
1152             }
1153             if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
1154                 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
1155                 return;
1156             }
1157             QTextStream out(&file);
1158             out << newdoc.toString();
1159             file.close();
1160             parseFile(exportFile, editable);
1161             return;
1162         }
1163
1164         QDomNode node = doc.elementsByTagName("profile").at(0);
1165         if (node.isNull()) {
1166             kDebug() << "// Export file: " << exportFile << " IS BROKEN";
1167             return;
1168         }
1169         int count = 1;
1170         while (!node.isNull()) {
1171             QDomElement profile = node.toElement();
1172             QString profileName = profile.attribute("name");
1173             QString standard = profile.attribute("standard");
1174             QString params = profile.attribute("args");
1175
1176             if (replaceVorbisCodec && params.contains("acodec=vorbis")) {
1177                 // replace vorbis with libvorbis
1178                 params = params.replace("vorbis", "libvorbis");
1179             }
1180
1181
1182             QString category = profile.attribute("category", i18n("Custom"));
1183             QString dest = profile.attribute("destinationid");
1184             QString prof_extension = profile.attribute("extension");
1185             if (!prof_extension.isEmpty()) extension = prof_extension;
1186
1187             QList <QListWidgetItem *> list = m_view.format_list->findItems(category, Qt::MatchExactly);
1188             bool exists = false;
1189             for (int j = 0; j < list.count(); j++) {
1190                 if (list.at(j)->data(MetaGroupRole) == dest) {
1191                     exists = true;
1192                     break;
1193                 }
1194             }
1195             if (!exists) {
1196                 item = new QListWidgetItem(category, m_view.format_list);
1197                 item->setData(MetaGroupRole, dest);
1198             }
1199
1200             // Check if item with same name already exists and replace it,
1201             // allowing to override default profiles
1202
1203             list = m_view.size_list->findItems(profileName, Qt::MatchExactly);
1204
1205             for (int j = 0; j < list.count(); j++) {
1206                 if (list.at(j)->data(MetaGroupRole) == dest) {
1207                     QListWidgetItem *duplicate = list.takeAt(j);
1208                     delete duplicate;
1209                     j--;
1210                 }
1211             }
1212
1213             item = new QListWidgetItem(profileName, m_view.size_list);
1214             //kDebug() << "// ADDINg item with name: " << profileName << ", GRP" << category << ", DEST:" << dest ;
1215             item->setData(GroupRole, category);
1216             item->setData(MetaGroupRole, dest);
1217             item->setData(ExtensionRole, extension);
1218             item->setData(RenderRole, "avformat");
1219             item->setData(StandardRole, standard);
1220             item->setData(ParamsRole, params);
1221             if (profile.hasAttribute("url")) item->setData(ExtraRole, profile.attribute("url"));
1222             if (editable) {
1223                 item->setData(EditableRole, exportFile);
1224                 if (exportFile.endsWith("customprofiles.xml")) item->setIcon(KIcon("emblem-favorite"));
1225                 else item->setIcon(KIcon("applications-internet"));
1226             }
1227             node = doc.elementsByTagName("profile").at(count);
1228             count++;
1229         }
1230         return;
1231     }
1232
1233     int i = 0;
1234     QString groupName;
1235     QString profileName;
1236
1237     QString prof_extension;
1238     QString renderer;
1239     QString params;
1240     QString standard;
1241     KIcon icon;
1242
1243     while (!groups.item(i).isNull()) {
1244         documentElement = groups.item(i).toElement();
1245         QDomNode gname = documentElement.elementsByTagName("groupname").at(0);
1246         QString metagroupName;
1247         QString metagroupId;
1248         if (!gname.isNull()) {
1249             metagroupName = gname.firstChild().nodeValue();
1250             metagroupId = gname.toElement().attribute("id");
1251
1252             if (!metagroupName.isEmpty() && m_view.destination_list->findData(metagroupId) == -1) {
1253                 if (metagroupId == "dvd") icon = KIcon("media-optical");
1254                 else if (metagroupId == "audioonly") icon = KIcon("audio-x-generic");
1255                 else if (metagroupId == "websites") icon = KIcon("applications-internet");
1256                 else if (metagroupId == "mediaplayers") icon = KIcon("applications-multimedia");
1257                 else if (metagroupId == "lossless") icon = KIcon("drive-harddisk");
1258                 else if (metagroupId == "mobile") icon = KIcon("pda");
1259                 m_view.destination_list->addItem(icon, i18n(metagroupName.toUtf8().data()), metagroupId);
1260             }
1261         }
1262         groupName = documentElement.attribute("name", i18n("Custom"));
1263         extension = documentElement.attribute("extension", QString());
1264         renderer = documentElement.attribute("renderer", QString());
1265         QList <QListWidgetItem *> list = m_view.format_list->findItems(groupName, Qt::MatchExactly);
1266         bool exists = false;
1267         for (int j = 0; j < list.count(); j++) {
1268             if (list.at(j)->data(MetaGroupRole) == metagroupId) {
1269                 exists = true;
1270                 break;
1271             }
1272         }
1273         if (!exists) {
1274             item = new QListWidgetItem(groupName, m_view.format_list);
1275             item->setData(MetaGroupRole, metagroupId);
1276         }
1277
1278         QDomNode n = groups.item(i).firstChild();
1279         while (!n.isNull()) {
1280             if (n.toElement().tagName() != "profile") {
1281                 n = n.nextSibling();
1282                 continue;
1283             }
1284             profileElement = n.toElement();
1285             profileName = profileElement.attribute("name");
1286             standard = profileElement.attribute("standard");
1287             params = profileElement.attribute("args");
1288
1289             if (replaceVorbisCodec && params.contains("acodec=vorbis")) {
1290                 // replace vorbis with libvorbis
1291                 params = params.replace("vorbis", "libvorbis");
1292             }
1293             prof_extension = profileElement.attribute("extension");
1294             if (!prof_extension.isEmpty()) extension = prof_extension;
1295             item = new QListWidgetItem(profileName, m_view.size_list);
1296             item->setData(GroupRole, groupName);
1297             item->setData(MetaGroupRole, metagroupId);
1298             item->setData(ExtensionRole, extension);
1299             item->setData(RenderRole, renderer);
1300             item->setData(StandardRole, standard);
1301             item->setData(ParamsRole, params);
1302             if (profileElement.hasAttribute("url")) item->setData(ExtraRole, profileElement.attribute("url"));
1303             if (editable) item->setData(EditableRole, exportFile);
1304             n = n.nextSibling();
1305         }
1306
1307         i++;
1308     }
1309 }
1310
1311 void RenderWidget::setRenderJob(const QString &dest, int progress)
1312 {
1313     QTreeWidgetItem *item;
1314     QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
1315     if (!existing.isEmpty()) item = existing.at(0);
1316     else {
1317         item = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
1318         item->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
1319         if (progress == 0) {
1320             item->setData(1, Qt::UserRole + 2, WAITINGJOB);
1321             item->setIcon(0, KIcon("media-playback-pause"));
1322             item->setData(1, Qt::UserRole, i18n("Waiting..."));
1323         }
1324     }
1325     item->setData(2, Qt::UserRole, progress);
1326     item->setData(1, Qt::UserRole + 2, RUNNINGJOB);
1327     if (progress == 0) {
1328         item->setIcon(0, KIcon("system-run"));
1329         item->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
1330         item->setData(1, Qt::UserRole + 1, QTime::currentTime());
1331         slotCheckJob();
1332     } else {
1333         QTime startTime = item->data(1, Qt::UserRole + 1).toTime();
1334         int seconds = startTime.secsTo(QTime::currentTime());;
1335         const QString t = i18n("Estimated time %1", QTime().addSecs(seconds * (100 - progress) / progress).toString("hh:mm:ss"));
1336         item->setData(1, Qt::UserRole, t);
1337     }
1338 }
1339
1340 void RenderWidget::setRenderStatus(const QString &dest, int status, const QString &error)
1341 {
1342     QTreeWidgetItem *item;
1343     QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
1344     if (!existing.isEmpty()) item = existing.at(0);
1345     else {
1346         item = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
1347         item->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
1348     }
1349     item->setData(1, Qt::UserRole + 2, FINISHEDJOB);
1350     if (status == -1) {
1351         // Job finished successfully
1352         item->setIcon(0, KIcon("dialog-ok"));
1353         item->setData(2, Qt::UserRole, 100);
1354         QTime startTime = item->data(1, Qt::UserRole + 1).toTime();
1355         int seconds = startTime.secsTo(QTime::currentTime());
1356         const QTime tm = QTime().addSecs(seconds);
1357         const QString t = i18n("Rendering finished in %1", tm.toString("hh:mm:ss"));
1358         item->setData(1, Qt::UserRole, t);
1359         QString itemGroup = item->data(0, Qt::UserRole).toString();
1360         if (itemGroup == "dvd") {
1361             emit openDvdWizard(item->text(1), item->data(0, Qt::UserRole + 1).toString());
1362         } else if (itemGroup == "websites") {
1363             QString url = item->data(0, Qt::UserRole + 1).toString();
1364             if (!url.isEmpty()) new KRun(url, this);
1365         }
1366     } else if (status == -2) {
1367         // Rendering crashed
1368         item->setData(1, Qt::UserRole, i18n("Rendering crashed"));
1369         item->setIcon(0, KIcon("dialog-close"));
1370         item->setData(2, Qt::UserRole, 100);
1371         m_view.error_log->append(i18n("<strong>Rendering of %1 crashed</strong><br />", dest));
1372         m_view.error_log->append(error);
1373         m_view.error_log->append("<hr />");
1374         m_view.error_box->setVisible(true);
1375     } else if (status == -3) {
1376         // User aborted job
1377         item->setData(1, Qt::UserRole, i18n("Rendering aborted"));
1378         item->setIcon(0, KIcon("dialog-cancel"));
1379         item->setData(2, Qt::UserRole, 100);
1380     }
1381     slotCheckJob();
1382     checkRenderStatus();
1383 }
1384
1385 void RenderWidget::slotAbortCurrentJob()
1386 {
1387     QTreeWidgetItem *current = m_view.running_jobs->currentItem();
1388     if (current) {
1389         if (current->data(1, Qt::UserRole + 2).toInt() == RUNNINGJOB)
1390             emit abortProcess(current->text(1));
1391         else {
1392             delete current;
1393             slotCheckJob();
1394             checkRenderStatus();
1395         }
1396     }
1397 }
1398
1399 void RenderWidget::slotCheckJob()
1400 {
1401     bool activate = false;
1402     QTreeWidgetItem *current = m_view.running_jobs->currentItem();
1403     if (current) {
1404         if (current->data(1, Qt::UserRole + 2).toInt() == RUNNINGJOB)
1405             m_view.abort_job->setText(i18n("Abort Job"));
1406         else m_view.abort_job->setText(i18n("Remove Job"));
1407         activate = true;
1408     }
1409     m_view.abort_job->setEnabled(activate);
1410 }
1411
1412 void RenderWidget::slotCLeanUpJobs()
1413 {
1414     int ix = 0;
1415     QTreeWidgetItem *current = m_view.running_jobs->topLevelItem(ix);
1416     while (current) {
1417         if (current->data(1, Qt::UserRole + 2).toInt() == FINISHEDJOB)
1418             delete current;
1419         else ix++;
1420         current = m_view.running_jobs->topLevelItem(ix);
1421     }
1422     slotCheckJob();
1423 }
1424
1425 void RenderWidget::parseScriptFiles()
1426 {
1427     QStringList scriptsFilter;
1428     scriptsFilter << "*.sh";
1429     m_view.scripts_list->clear();
1430
1431     QTreeWidgetItem *item;
1432     // List the project scripts
1433     QStringList scriptFiles = QDir(m_projectFolder + "scripts").entryList(scriptsFilter, QDir::Files);
1434     for (int i = 0; i < scriptFiles.size(); ++i) {
1435         KUrl scriptpath(m_projectFolder + "scripts/" + scriptFiles.at(i));
1436         QString target;
1437         QString renderer;
1438         QString melt;
1439         QFile file(scriptpath.path());
1440         if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
1441             while (!file.atEnd()) {
1442                 QByteArray line = file.readLine();
1443                 if (line.startsWith("TARGET=")) {
1444                     target = QString(line).section("TARGET=", 1).simplified();
1445                     target.remove(QChar('"'));
1446                 } else if (line.startsWith("RENDERER=")) {
1447                     renderer = QString(line).section("RENDERER=", 1).simplified();
1448                     renderer.remove(QChar('"'));
1449                 } else if (line.startsWith("MELT=")) {
1450                     melt = QString(line).section("MELT=", 1).simplified();
1451                     melt.remove(QChar('"'));
1452                 }
1453             }
1454             file.close();
1455         }
1456         if (target.isEmpty()) continue;
1457         item = new QTreeWidgetItem(m_view.scripts_list, QStringList() << QString() << scriptpath.fileName());
1458         if (!renderer.isEmpty() && renderer.contains('/') && !QFile::exists(renderer)) {
1459             item->setIcon(0, KIcon("dialog-cancel"));
1460             item->setToolTip(1, i18n("Script contains wrong command: %1", renderer));
1461             item->setData(0, Qt::UserRole, '1');
1462         } else if (!melt.isEmpty() && melt.contains('/') && !QFile::exists(melt)) {
1463             item->setIcon(0, KIcon("dialog-cancel"));
1464             item->setToolTip(1, i18n("Script contains wrong command: %1", melt));
1465             item->setData(0, Qt::UserRole, '1');
1466         } else item->setIcon(0, KIcon("application-x-executable-script"));
1467         item->setSizeHint(0, QSize(m_view.scripts_list->columnWidth(0), fontMetrics().height() * 2));
1468         item->setData(1, Qt::UserRole, target.simplified());
1469         item->setData(1, Qt::UserRole + 1, scriptpath.path());
1470     }
1471     bool activate = false;
1472     QTreeWidgetItem *script = m_view.scripts_list->topLevelItem(0);
1473     if (script) {
1474         script->setSelected(true);
1475         m_view.scripts_list->setCurrentItem(script);
1476         activate = true;
1477     }
1478 //    m_view.start_script->setEnabled(activate);
1479 //    m_view.delete_script->setEnabled(activate);
1480 }
1481
1482 void RenderWidget::slotCheckScript()
1483 {
1484     QTreeWidgetItem *item = m_view.scripts_list->currentItem();
1485     if (item == NULL) return;
1486     m_view.start_script->setEnabled(item->data(0, Qt::UserRole).toString().isEmpty());
1487     m_view.delete_script->setEnabled(true);
1488 }
1489
1490 void RenderWidget::slotStartScript()
1491 {
1492     QTreeWidgetItem *item = m_view.scripts_list->currentItem();
1493     if (item) {
1494         QString destination = item->data(1, Qt::UserRole).toString();
1495         QString path = item->data(1, Qt::UserRole + 1).toString();
1496         // Insert new job in queue
1497         QTreeWidgetItem *renderItem;
1498         QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(destination, Qt::MatchExactly, 1);
1499         kDebug() << "------  START SCRIPT";
1500         if (!existing.isEmpty()) {
1501             renderItem = existing.at(0);
1502             if (renderItem->data(1, Qt::UserRole + 2).toInt() == RUNNINGJOB) {
1503                 KMessageBox::information(this, i18n("There is already a job writing file:<br><b>%1</b><br>Abort the job if you want to overwrite it...", destination), i18n("Already running"));
1504                 return;
1505             }
1506         } else renderItem = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << destination << QString());
1507         kDebug() << "------  START SCRIPT 2";
1508         renderItem->setData(2, Qt::UserRole, 0);
1509         renderItem->setData(1, Qt::UserRole + 2, WAITINGJOB);
1510         renderItem->setIcon(0, KIcon("media-playback-pause"));
1511         renderItem->setData(1, Qt::UserRole, i18n("Waiting..."));
1512         renderItem->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
1513         renderItem->setData(1, Qt::UserRole + 1, QTime::currentTime());
1514         renderItem->setData(1, Qt::UserRole + 3, path);
1515         renderItem->setData(1, Qt::UserRole + 4, '1');
1516         checkRenderStatus();
1517         m_view.tabWidget->setCurrentIndex(1);
1518     }
1519 }
1520
1521 void RenderWidget::slotDeleteScript()
1522 {
1523     QTreeWidgetItem *item = m_view.scripts_list->currentItem();
1524     if (item) {
1525         QString path = item->data(1, Qt::UserRole + 1).toString();
1526         KIO::NetAccess::del(path + ".mlt", this);
1527         KIO::NetAccess::del(path, this);
1528         parseScriptFiles();
1529     }
1530 }
1531
1532 void RenderWidget::slotGenerateScript()
1533 {
1534     slotPrepareExport(true);
1535 }
1536
1537 void RenderWidget::slotHideLog()
1538 {
1539     m_view.error_box->setVisible(false);
1540 }
1541
1542 void RenderWidget::setRenderProfile(const QString &dest, const QString &name, const QString &url)
1543 {
1544     m_view.destination_list->blockSignals(true);
1545     m_view.format_list->blockSignals(true);
1546     m_view.size_list->blockSignals(true);
1547
1548     if (!url.isEmpty()) m_view.out_file->setUrl(KUrl(url));
1549
1550     for (int i = 0; i < m_view.destination_list->count(); i++) {
1551         if (m_view.destination_list->itemData(i, Qt::UserRole) == dest) {
1552             m_view.destination_list->setCurrentIndex(i);
1553             break;
1554         }
1555     }
1556     QList<QListWidgetItem *> childs = m_view.size_list->findItems(name, Qt::MatchExactly);
1557     if (!childs.isEmpty()) {
1558         QListWidgetItem *profile = childs.at(0);
1559         if (profile->isHidden()) {
1560             QString group = profile->data(GroupRole).toString();
1561             childs = m_view.format_list->findItems(group, Qt::MatchExactly);
1562             if (!childs.isEmpty()) {
1563                 m_view.format_list->setCurrentItem(childs.at(0));
1564             }
1565         }
1566         refreshView();
1567         m_view.size_list->blockSignals(false);
1568         m_view.size_list->setCurrentItem(profile);
1569     } else m_view.size_list->blockSignals(false);
1570     m_view.destination_list->blockSignals(false);
1571     m_view.format_list->blockSignals(false);
1572
1573 }
1574
1575 bool RenderWidget::startWaitingRenderJobs()
1576 {
1577     m_blockProcessing = true;
1578     QString autoscriptFile = getFreeScriptName("auto");
1579     QFile file(autoscriptFile);
1580     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
1581         kWarning() << "//////  ERROR writing to file: " << autoscriptFile;
1582         KMessageBox::error(0, i18n("Cannot write to file %1", autoscriptFile));
1583         return false;
1584     }
1585
1586     QString renderer = QCoreApplication::applicationDirPath() + QString("/kdenlive_render");
1587     if (!QFile::exists(renderer)) renderer = "kdenlive_render";
1588     QTextStream outStream(&file);
1589     outStream << "#! /bin/sh" << "\n" << "\n";
1590     QTreeWidgetItem *item = m_view.running_jobs->topLevelItem(0);
1591     while (item) {
1592         if (item->data(1, Qt::UserRole + 2).toInt() == WAITINGJOB) {
1593             if (item->data(1, Qt::UserRole + 4).isNull()) {
1594                 // Add render process for item
1595                 const QString params = item->data(1, Qt::UserRole + 3).toStringList().join(" ");
1596                 outStream << renderer << " " << params << "\n";
1597             } else {
1598                 // Script item
1599                 outStream << item->data(1, Qt::UserRole + 3).toString() << "\n";
1600             }
1601         }
1602         item = m_view.running_jobs->itemBelow(item);
1603     }
1604     // erase itself when rendering is finished
1605     outStream << "rm " << autoscriptFile << "\n" << "\n";
1606     if (file.error() != QFile::NoError) {
1607         KMessageBox::error(0, i18n("Cannot write to file %1", autoscriptFile));
1608         file.close();
1609         m_blockProcessing = false;
1610         return false;
1611     }
1612     file.close();
1613     QFile::setPermissions(autoscriptFile, file.permissions() | QFile::ExeUser);
1614     QProcess::startDetached(autoscriptFile, QStringList());
1615     return true;
1616 }
1617
1618 QString RenderWidget::getFreeScriptName(const QString &prefix)
1619 {
1620     int ix = 0;
1621     QString scriptsFolder = m_projectFolder + "scripts/";
1622     KStandardDirs::makeDir(scriptsFolder);
1623     QString path;
1624     while (path.isEmpty() || QFile::exists(path)) {
1625         ix++;
1626         path = scriptsFolder + prefix + i18n("script") + QString::number(ix).rightJustified(3, '0', false) + ".sh";
1627     }
1628     return path;
1629 }
1630
1631 void RenderWidget::slotPlayRendering(QTreeWidgetItem *item, int)
1632 {
1633     if (KdenliveSettings::defaultplayerapp().isEmpty() || item->data(1, Qt::UserRole + 2).toInt() != FINISHEDJOB) return;
1634     QProcess::startDetached(KdenliveSettings::defaultplayerapp(), QStringList() << item->text(1));
1635 }
1636
1637
1638