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