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