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