]> git.sesse.net Git - kdenlive/blobdiff - src/renderwidget.cpp
Get ready for 0.7.3 release
[kdenlive] / src / renderwidget.cpp
index c585b0f584ad9785069fa8afd62ba66d210a5cf6..355c632064fbf8edb7749ac48e4c87ea21f073ed 100644 (file)
  ***************************************************************************/
 
 
-#include <QDomDocument>
-#include <QItemDelegate>
-#include <QTreeWidgetItem>
-#include <QHeaderView>
-#include <QMenu>
-#include <QProcess>
-#include <QInputDialog>
+#include "renderwidget.h"
+#include "kdenlivesettings.h"
+#include "ui_saveprofile_ui.h"
 
 #include <KStandardDirs>
 #include <KDebug>
 #include <KComboBox>
 #include <KRun>
 #include <KIO/NetAccess>
+// #include <knewstuff2/engine.h>
 
-#include "kdenlivesettings.h"
-#include "renderwidget.h"
-#include "ui_saveprofile_ui.h"
+#include <QDomDocument>
+#include <QItemDelegate>
+#include <QTreeWidgetItem>
+#include <QListWidgetItem>
+#include <QHeaderView>
+#include <QMenu>
+#include <QProcess>
+#include <QInputDialog>
 
 const int GroupRole = Qt::UserRole;
 const int ExtensionRole = GroupRole + 1;
@@ -46,7 +48,10 @@ const int EditableRole = GroupRole + 5;
 const int MetaGroupRole = GroupRole + 6;
 const int ExtraRole = GroupRole + 7;
 
-RenderWidget::RenderWidget(const QString &projectfolder, QWidget * parent): QDialog(parent), m_projectFolder(projectfolder) {
+RenderWidget::RenderWidget(const QString &projectfolder, QWidget * parent) :
+        QDialog(parent),
+        m_projectFolder(projectfolder)
+{
     m_view.setupUi(this);
     setWindowTitle(i18n("Rendering"));
     m_view.buttonDelete->setIcon(KIcon("trash-empty"));
@@ -71,9 +76,9 @@ RenderWidget::RenderWidget(const QString &projectfolder, QWidget * parent): QDia
 
 
     QMenu *renderMenu = new QMenu(i18n("Start Rendering"), this);
-    QAction *renderAction = renderMenu->addAction(KIcon("file-new"), i18n("Render to File"));
+    QAction *renderAction = renderMenu->addAction(KIcon("video-x-generic"), i18n("Render to File"));
     connect(renderAction, SIGNAL(triggered()), this, SLOT(slotExport()));
-    QAction *scriptAction = renderMenu->addAction(KIcon("file-new"), i18n("Generate Script"));
+    QAction *scriptAction = renderMenu->addAction(KIcon("application-x-shellscript"), i18n("Generate Script"));
     connect(scriptAction, SIGNAL(triggered()), this, SLOT(slotGenerateScript()));
 
     m_view.buttonStart->setMenu(renderMenu);
@@ -84,6 +89,8 @@ RenderWidget::RenderWidget(const QString &projectfolder, QWidget * parent): QDia
     m_view.start_script->setEnabled(false);
     m_view.delete_script->setEnabled(false);
 
+    m_view.format_list->setAlternatingRowColors(true);
+    m_view.size_list->setAlternatingRowColors(true);
 
     parseProfiles();
     parseScriptFiles();
@@ -109,6 +116,8 @@ RenderWidget::RenderWidget(const QString &projectfolder, QWidget * parent): QDia
     connect(m_view.format_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshView()));
     connect(m_view.size_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshParams()));
 
+    connect(m_view.size_list, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(slotEditItem(QListWidgetItem *)));
+
     connect(m_view.render_guide, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
     connect(m_view.render_zone, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
     connect(m_view.render_full, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
@@ -152,7 +161,15 @@ RenderWidget::RenderWidget(const QString &projectfolder, QWidget * parent): QDia
     focusFirstVisibleItem();
 }
 
-void RenderWidget::showInfoPanel() {
+void RenderWidget::slotEditItem(QListWidgetItem *item)
+{
+    QString edit = item->data(EditableRole).toString();
+    if (edit.isEmpty() || !edit.endsWith("customprofiles.xml")) slotSaveProfile();
+    else slotEditProfile();
+}
+
+void RenderWidget::showInfoPanel()
+{
     if (m_view.advanced_params->isVisible()) {
         m_view.advanced_params->setVisible(false);
         m_view.buttonInfo->setDown(false);
@@ -164,28 +181,33 @@ void RenderWidget::showInfoPanel() {
     }
 }
 
-void RenderWidget::setDocumentPath(const QString path) {
+void RenderWidget::setDocumentPath(const QString path)
+{
     m_projectFolder = path;
     const QString fileName = m_view.out_file->url().fileName();
     m_view.out_file->setUrl(KUrl(m_projectFolder + '/' + fileName));
     parseScriptFiles();
 }
 
-void RenderWidget::slotUpdateGuideBox() {
+void RenderWidget::slotUpdateGuideBox()
+{
     m_view.guides_box->setVisible(m_view.render_guide->isChecked());
 }
 
-void RenderWidget::slotCheckStartGuidePosition() {
+void RenderWidget::slotCheckStartGuidePosition()
+{
     if (m_view.guide_start->currentIndex() > m_view.guide_end->currentIndex())
         m_view.guide_start->setCurrentIndex(m_view.guide_end->currentIndex());
 }
 
-void RenderWidget::slotCheckEndGuidePosition() {
+void RenderWidget::slotCheckEndGuidePosition()
+{
     if (m_view.guide_end->currentIndex() < m_view.guide_start->currentIndex())
         m_view.guide_end->setCurrentIndex(m_view.guide_start->currentIndex());
 }
 
-void RenderWidget::setGuides(QDomElement guidesxml, double duration) {
+void RenderWidget::setGuides(QDomElement guidesxml, double duration)
+{
     m_view.guide_start->clear();
     m_view.guide_end->clear();
     QDomNodeList nodes = guidesxml.elementsByTagName("guide");
@@ -206,7 +228,8 @@ void RenderWidget::setGuides(QDomElement guidesxml, double duration) {
 
 // Will be called when the user selects an output file via the file dialog.
 // File extension will be added automatically.
-void RenderWidget::slotUpdateButtons(KUrl url) {
+void RenderWidget::slotUpdateButtons(KUrl url)
+{
     if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
     else m_view.buttonStart->setEnabled(true);
     if (url != 0) {
@@ -219,23 +242,28 @@ void RenderWidget::slotUpdateButtons(KUrl url) {
 
 // Will be called when the user changes the output file path in the text line.
 // File extension must NOT be added, would make editing impossible!
-void RenderWidget::slotUpdateButtons() {
+void RenderWidget::slotUpdateButtons()
+{
     if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
     else m_view.buttonStart->setEnabled(true);
 }
 
-void RenderWidget::slotSaveProfile() {
+void RenderWidget::slotSaveProfile()
+{
+    //TODO: update to correctly use metagroups
     Ui::SaveProfile_UI ui;
     QDialog *d = new QDialog(this);
     ui.setupUi(d);
-    QString customGroup = i18n("Custom");
-    QStringList groupNames;
-    for (int i = 0; i < m_view.format_list->count(); i++)
-        groupNames.append(m_view.format_list->item(i)->text());
-    if (!groupNames.contains(customGroup)) groupNames.prepend(customGroup);
-    ui.group_name->addItems(groupNames);
-    int pos = ui.group_name->findText(customGroup);
-    ui.group_name->setCurrentIndex(pos);
+
+    for (int i = 0; i < m_view.destination_list->count(); i++)
+        ui.destination_list->addItem(m_view.destination_list->itemIcon(i), m_view.destination_list->itemText(i), m_view.destination_list->itemData(i, Qt::UserRole));
+
+    ui.destination_list->setCurrentIndex(m_view.destination_list->currentIndex());
+    QString dest = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
+
+    QString customGroup = m_view.format_list->currentItem()->text();
+    if (customGroup.isEmpty()) customGroup = i18n("Custom");
+    ui.group_name->setText(customGroup);
 
     ui.parameters->setText(m_view.advanced_params->toPlainText());
     ui.extension->setText(m_view.size_list->currentItem()->data(ExtensionRole).toString());
@@ -246,38 +274,53 @@ void RenderWidget::slotSaveProfile() {
         QFile file(exportFile);
         doc.setContent(&file, false);
         file.close();
-
         QDomElement documentElement;
-        bool groupExists = false;
-        QString groupName;
+        QDomElement profiles = doc.documentElement();
+        if (profiles.isNull() || profiles.tagName() != "profiles") {
+            doc.clear();
+            profiles = doc.createElement("profiles");
+            profiles.setAttribute("version", 1);
+            doc.appendChild(profiles);
+        }
+        int version = profiles.attribute("version", 0).toInt();
+        if (version < 1) {
+            kDebug() << "// OLD profile version";
+            doc.clear();
+            profiles = doc.createElement("profiles");
+            profiles.setAttribute("version", 1);
+            doc.appendChild(profiles);
+        }
+
         QString newProfileName = ui.profile_name->text().simplified();
-        QString newGroupName = ui.group_name->currentText();
-        QDomNodeList groups = doc.elementsByTagName("group");
+        QString newGroupName = ui.group_name->text().simplified();
+        if (newGroupName.isEmpty()) newGroupName = i18n("Custom");
+        QString newMetaGroupId = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
+        QDomNodeList profilelist = doc.elementsByTagName("profile");
         int i = 0;
-        if (groups.count() == 0) {
-            QDomElement profiles = doc.createElement("profiles");
-            doc.appendChild(profiles);
-        } else while (!groups.item(i).isNull()) {
-                documentElement = groups.item(i).toElement();
-                groupName = documentElement.attribute("name");
-                kDebug() << "// SAVE, PARSING FROUP: " << i << ", name: " << groupName << ", LOOK FR: " << newGroupName;
-                if (groupName == newGroupName) {
-                    groupExists = true;
+        while (!profilelist.item(i).isNull()) {
+            // make sure a profile with same name doesn't exist
+            documentElement = profilelist.item(i).toElement();
+            QString profileName = documentElement.attribute("name");
+            if (profileName == newProfileName) {
+                // a profile with that same name already exists
+                bool ok;
+                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);
+                if (!ok) return;
+                if (profileName == newProfileName) {
+                    profiles.removeChild(profilelist.item(i));
                     break;
                 }
-                i++;
             }
-        if (!groupExists) {
-            documentElement = doc.createElement("group");
-            documentElement.setAttribute("name", ui.group_name->currentText());
-            documentElement.setAttribute("renderer", "avformat");
-            doc.documentElement().appendChild(documentElement);
+            i++;
         }
+
         QDomElement profileElement = doc.createElement("profile");
         profileElement.setAttribute("name", newProfileName);
+        profileElement.setAttribute("category", newGroupName);
+        profileElement.setAttribute("destinationid", newMetaGroupId);
         profileElement.setAttribute("extension", ui.extension->text().simplified());
         profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
-        documentElement.appendChild(profileElement);
+        profiles.appendChild(profileElement);
 
         //QCString save = doc.toString().utf8();
 
@@ -288,13 +331,20 @@ void RenderWidget::slotSaveProfile() {
         }
         QTextStream out(&file);
         out << doc.toString();
+        if (file.error() != QFile::NoError) {
+            KMessageBox::error(this, i18n("Cannot write to file %1", exportFile));
+            file.close();
+            delete d;
+            return;
+        }
         file.close();
-        parseProfiles(newGroupName, newProfileName);
+        parseProfiles(newMetaGroupId, newGroupName, newProfileName);
     }
     delete d;
 }
 
-void RenderWidget::slotEditProfile() {
+void RenderWidget::slotEditProfile()
+{
     QListWidgetItem *item = m_view.size_list->currentItem();
     if (!item) return;
     QString currentGroup = m_view.format_list->currentItem()->text();
@@ -306,76 +356,113 @@ void RenderWidget::slotEditProfile() {
     Ui::SaveProfile_UI ui;
     QDialog *d = new QDialog(this);
     ui.setupUi(d);
-    QStringList groupNames;
-    for (int i = 0; i < m_view.format_list->count(); i++)
-        groupNames.append(m_view.format_list->item(i)->text());
-    if (!groupNames.contains(currentGroup)) groupNames.prepend(currentGroup);
-    ui.group_name->addItems(groupNames);
-    int pos = ui.group_name->findText(currentGroup);
-    ui.group_name->setCurrentIndex(pos);
+
+    for (int i = 0; i < m_view.destination_list->count(); i++)
+        ui.destination_list->addItem(m_view.destination_list->itemIcon(i), m_view.destination_list->itemText(i), m_view.destination_list->itemData(i, Qt::UserRole));
+
+    ui.destination_list->setCurrentIndex(m_view.destination_list->currentIndex());
+    QString dest = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
+
+    QString customGroup = m_view.format_list->currentItem()->text();
+    if (customGroup.isEmpty()) customGroup = i18n("Custom");
+    ui.group_name->setText(customGroup);
+
     ui.profile_name->setText(currentProfile);
     ui.extension->setText(extension);
     ui.parameters->setText(params);
     ui.profile_name->setFocus();
-
+    d->setWindowTitle(i18n("Edit Profile"));
     if (d->exec() == QDialog::Accepted) {
-        slotDeleteProfile();
+        slotDeleteProfile(false);
         QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
         QDomDocument doc;
         QFile file(exportFile);
         doc.setContent(&file, false);
         file.close();
-
         QDomElement documentElement;
-        bool groupExists = false;
-        QString groupName;
-        QString newProfileName = ui.profile_name->text();
-        QString newGroupName = ui.group_name->currentText();
-        QDomNodeList groups = doc.elementsByTagName("group");
-        int i = 0;
-        if (groups.count() == 0) {
-            QDomElement profiles = doc.createElement("profiles");
+        QDomElement profiles = doc.documentElement();
+
+        if (profiles.isNull() || profiles.tagName() != "profiles") {
+            doc.clear();
+            profiles = doc.createElement("profiles");
+            profiles.setAttribute("version", 1);
+            doc.appendChild(profiles);
+        }
+
+        int version = profiles.attribute("version", 0).toInt();
+        if (version < 1) {
+            kDebug() << "// OLD profile version";
+            doc.clear();
+            profiles = doc.createElement("profiles");
+            profiles.setAttribute("version", 1);
             doc.appendChild(profiles);
-        } else while (!groups.item(i).isNull()) {
-                documentElement = groups.item(i).toElement();
-                groupName = documentElement.attribute("name");
-                kDebug() << "// SAVE, PARSING FROUP: " << i << ", name: " << groupName << ", LOOK FR: " << newGroupName;
-                if (groupName == newGroupName) {
-                    groupExists = true;
+        }
+
+        QString newProfileName = ui.profile_name->text().simplified();
+        QString newGroupName = ui.group_name->text().simplified();
+        if (newGroupName.isEmpty()) newGroupName = i18n("Custom");
+        QString newMetaGroupId = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
+        QDomNodeList profilelist = doc.elementsByTagName("profile");
+        int i = 0;
+        while (!profilelist.item(i).isNull()) {
+            // make sure a profile with same name doesn't exist
+            documentElement = profilelist.item(i).toElement();
+            QString profileName = documentElement.attribute("name");
+            if (profileName == newProfileName) {
+                // a profile with that same name already exists
+                bool ok;
+                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);
+                if (!ok) return;
+                if (profileName == newProfileName) {
+                    profiles.removeChild(profilelist.item(i));
                     break;
                 }
-                i++;
             }
-        if (!groupExists) {
-            documentElement = doc.createElement("group");
-            documentElement.setAttribute("name", ui.group_name->currentText());
-            documentElement.setAttribute("renderer", "avformat");
-            doc.documentElement().appendChild(documentElement);
+            i++;
         }
+
         QDomElement profileElement = doc.createElement("profile");
         profileElement.setAttribute("name", newProfileName);
+        profileElement.setAttribute("category", newGroupName);
+        profileElement.setAttribute("destinationid", newMetaGroupId);
         profileElement.setAttribute("extension", ui.extension->text().simplified());
         profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
-        documentElement.appendChild(profileElement);
+        profiles.appendChild(profileElement);
 
         //QCString save = doc.toString().utf8();
-
+        delete d;
         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
-            KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
-            delete d;
+            KMessageBox::error(this, i18n("Cannot write to file %1", exportFile));
             return;
         }
         QTextStream out(&file);
         out << doc.toString();
+        if (file.error() != QFile::NoError) {
+            KMessageBox::error(this, i18n("Cannot write to file %1", exportFile));
+            file.close();
+            return;
+        }
         file.close();
-        parseProfiles(newGroupName, newProfileName);
-    }
-    delete d;
+        parseProfiles(newMetaGroupId, newGroupName, newProfileName);
+    } else delete d;
 }
 
-void RenderWidget::slotDeleteProfile() {
+void RenderWidget::slotDeleteProfile(bool refresh)
+{
+    //TODO: delete a profile installed by KNewStuff the easy way
+    /*
+    QString edit = m_view.size_list->currentItem()->data(EditableRole).toString();
+    if (!edit.endsWith("customprofiles.xml")) {
+        // This is a KNewStuff installed file, process through KNS
+        KNS::Engine engine(0);
+        if (engine.init("kdenlive_render.knsrc")) {
+            KNS::Entry::List entries;
+        }
+        return;
+    }*/
     QString currentGroup = m_view.format_list->currentItem()->text();
     QString currentProfile = m_view.size_list->currentItem()->text();
+    QString metaGroupId = m_view.destination_list->itemData(m_view.destination_list->currentIndex(), Qt::UserRole).toString();
 
     QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
     QDomDocument doc;
@@ -384,25 +471,21 @@ void RenderWidget::slotDeleteProfile() {
     file.close();
 
     QDomElement documentElement;
-    bool groupExists = false;
-    QString groupName;
-    QDomNodeList groups = doc.elementsByTagName("group");
+    QDomNodeList profiles = doc.elementsByTagName("profile");
     int i = 0;
+    QString groupName;
+    QString profileName;
+    QString destination;
 
-    while (!groups.item(i).isNull()) {
-        documentElement = groups.item(i).toElement();
-        groupName = documentElement.attribute("name");
-        if (groupName == currentGroup) {
-            QDomNodeList children = documentElement.childNodes();
-            for (int j = 0; j < children.count(); j++) {
-                QDomElement pro = children.at(j).toElement();
-                if (pro.attribute("name") == currentProfile) {
-                    groups.item(i).removeChild(children.at(j));
-                    if (groups.item(i).childNodes().isEmpty())
-                        doc.documentElement().removeChild(groups.item(i));
-                    break;
-                }
-            }
+    while (!profiles.item(i).isNull()) {
+        documentElement = profiles.item(i).toElement();
+        profileName = documentElement.attribute("name");
+        groupName = documentElement.attribute("category");
+        destination = documentElement.attribute("destinationid");
+
+        if (profileName == currentProfile && groupName == currentGroup && destination == metaGroupId) {
+            kDebug() << "// GOT it: " << profileName;
+            doc.documentElement().removeChild(profiles.item(i));
             break;
         }
         i++;
@@ -414,19 +497,30 @@ void RenderWidget::slotDeleteProfile() {
     }
     QTextStream out(&file);
     out << doc.toString();
+    if (file.error() != QFile::NoError) {
+        KMessageBox::error(this, i18n("Cannot write to file %1", exportFile));
+        file.close();
+        return;
+    }
     file.close();
-    parseProfiles(currentGroup);
-    focusFirstVisibleItem();
+    if (refresh) {
+        parseProfiles(metaGroupId, currentGroup);
+        focusFirstVisibleItem();
+    }
 }
 
-void RenderWidget::updateButtons() {
+void RenderWidget::updateButtons()
+{
     if (!m_view.size_list->currentItem() || m_view.size_list->currentItem()->isHidden()) {
         m_view.buttonSave->setEnabled(false);
         m_view.buttonDelete->setEnabled(false);
         m_view.buttonEdit->setEnabled(false);
+        m_view.buttonStart->setEnabled(false);
     } else {
         m_view.buttonSave->setEnabled(true);
-        if (m_view.size_list->currentItem()->data(EditableRole).toString().isEmpty()) {
+        m_view.buttonStart->setEnabled(m_view.size_list->currentItem()->flags() & Qt::ItemIsEnabled);
+        QString edit = m_view.size_list->currentItem()->data(EditableRole).toString();
+        if (edit.isEmpty() || !edit.endsWith("customprofiles.xml")) {
             m_view.buttonDelete->setEnabled(false);
             m_view.buttonEdit->setEnabled(false);
         } else {
@@ -437,12 +531,13 @@ void RenderWidget::updateButtons() {
 }
 
 
-void RenderWidget::focusFirstVisibleItem() {
+void RenderWidget::focusFirstVisibleItem()
+{
     if (m_view.size_list->currentItem() && !m_view.size_list->currentItem()->isHidden()) {
         updateButtons();
         return;
     }
-    for (uint ix = 0; ix < m_view.size_list->count(); ix++) {
+    for (int ix = 0; ix < m_view.size_list->count(); ix++) {
         QListWidgetItem *item = m_view.size_list->item(ix);
         if (item && !item->isHidden()) {
             m_view.size_list->setCurrentRow(ix);
@@ -453,7 +548,8 @@ void RenderWidget::focusFirstVisibleItem() {
     updateButtons();
 }
 
-void RenderWidget::slotExport(bool scriptExport) {
+void RenderWidget::slotExport(bool scriptExport)
+{
     QListWidgetItem *item = m_view.size_list->currentItem();
     if (!item) return;
 
@@ -499,7 +595,7 @@ void RenderWidget::slotExport(bool scriptExport) {
         startPos = m_view.guide_start->itemData(m_view.guide_start->currentIndex()).toDouble();
         endPos = m_view.guide_end->itemData(m_view.guide_end->currentIndex()).toDouble();
     }
-    QString renderArgs = m_view.advanced_params->toPlainText();
+    QString renderArgs = m_view.advanced_params->toPlainText().simplified();
 
     // Adjust frame scale
     int width;
@@ -511,7 +607,7 @@ void RenderWidget::slotExport(bool scriptExport) {
         width = m_profile.width;
         height = m_profile.height;
     }
-    renderArgs.replace("%dar", "@" + QString::number(m_profile.display_aspect_num) + "/" + QString::number(m_profile.display_aspect_den));
+    renderArgs.replace("%dar", '@' + QString::number(m_profile.display_aspect_num) + '/' + QString::number(m_profile.display_aspect_den));
 
     // Adjust scanning
     if (m_view.scanning_list->currentIndex() == 1) renderArgs.append(" progressive=1");
@@ -527,7 +623,12 @@ void RenderWidget::slotExport(bool scriptExport) {
 
     QString std = renderArgs;
     QString destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
-    if (std.contains(" s=")) {
+    if (std.startsWith("s=")) {
+        QString subsize = std.section(' ', 0, 0).toLower();
+        subsize = subsize.section("=", 1, 1);
+        const QString currentSize = QString::number(width) + 'x' + QString::number(height);
+        if (subsize != currentSize) resizeProfile = true;
+    } else if (std.contains(" s=")) {
         QString subsize = std.section(" s=", 1, 1);
         subsize = subsize.section(' ', 0, 0).toLower();
         const QString currentSize = QString::number(width) + 'x' + QString::number(height);
@@ -550,7 +651,7 @@ void RenderWidget::slotExport(bool scriptExport) {
     if (group == "dvd" && m_view.open_dvd->isChecked()) {
         renderItem->setData(0, Qt::UserRole, group);
         if (renderArgs.contains("profile=")) {
-            // rendering profile contains an MLT profile, so pass it to the running jog item, usefull for dvd
+            // rendering profile contains an MLT profile, so pass it to the running jog item, useful for dvd
             QString prof = renderArgs.section("profile=", 1, 1);
             prof = prof.section(' ', 0, 0);
             kDebug() << "// render profile: " << prof;
@@ -571,7 +672,8 @@ void RenderWidget::slotExport(bool scriptExport) {
     }
 }
 
-void RenderWidget::setProfile(MltVideoProfile profile) {
+void RenderWidget::setProfile(MltVideoProfile profile)
+{
     m_profile = profile;
     //WARNING: this way to tell the video standard is a bit hackish...
     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);
@@ -580,14 +682,17 @@ void RenderWidget::setProfile(MltVideoProfile profile) {
     refreshView();
 }
 
-void RenderWidget::refreshView() {
+void RenderWidget::refreshView()
+{
     m_view.size_list->blockSignals(true);
     QListWidgetItem *sizeItem;
 
     QString destination;
+    KIcon brokenIcon("dialog-close");
     if (m_view.destination_list->currentIndex() > 0)
         destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
 
+
     if (destination == "dvd") m_view.open_dvd->setVisible(true);
     else m_view.open_dvd->setVisible(false);
     if (destination == "websites") m_view.open_browser->setVisible(true);
@@ -598,9 +703,10 @@ void RenderWidget::refreshView() {
     // hide groups that are not in the correct destination
     for (int i = 0; i < m_view.format_list->count(); i++) {
         sizeItem = m_view.format_list->item(i);
-        if (sizeItem->data(MetaGroupRole).toString() == destination)
+        if (sizeItem->data(MetaGroupRole).toString() == destination) {
             sizeItem->setHidden(false);
-        else sizeItem->setHidden(true);
+            //kDebug() << "// SET GRP:: " << sizeItem->text() << ", METY:" << sizeItem->data(MetaGroupRole).toString();
+        } else sizeItem->setHidden(true);
     }
 
     // activate first visible item
@@ -614,7 +720,14 @@ void RenderWidget::refreshView() {
         }
         item = m_view.format_list->currentItem();
     }
-    if (!item) return;
+    if (!item || item->isHidden()) {
+        m_view.format_list->setEnabled(false);
+        m_view.size_list->setEnabled(false);
+        return;
+    } else {
+        m_view.format_list->setEnabled(true);
+        m_view.size_list->setEnabled(true);
+    }
     int count = 0;
     for (int i = 0; i < m_view.format_list->count() && count < 2; i++) {
         if (!m_view.format_list->isRowHidden(i)) count++;
@@ -630,7 +743,7 @@ void RenderWidget::refreshView() {
 
     for (int i = 0; i < m_view.size_list->count(); i++) {
         sizeItem = m_view.size_list->item(i);
-        if (sizeItem->data(GroupRole) == group) {
+        if ((sizeItem->data(GroupRole) == group || sizeItem->data(GroupRole).toString().isEmpty()) && sizeItem->data(MetaGroupRole) == destination) {
             std = sizeItem->data(StandardRole).toString();
             if (!std.isEmpty()) {
                 if (std.contains("PAL", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 0);
@@ -652,8 +765,11 @@ void RenderWidget::refreshView() {
                     if (!format.isEmpty()) {
                         format = format.section(' ', 0, 0).toLower();
                         if (!formatsList.contains(format)) {
-                            kDebug() << "*****  UNSUPPORTED F: " << format;
-                            sizeItem->setHidden(true);
+                            kDebug() << "***** UNSUPPORTED F: " << format;
+                            //sizeItem->setHidden(true);
+                            sizeItem->setFlags(Qt::ItemIsSelectable);
+                            sizeItem->setToolTip(i18n("Unsupported video format: %1", format));
+                            sizeItem->setIcon(brokenIcon);
                         }
                     }
                 }
@@ -665,7 +781,10 @@ void RenderWidget::refreshView() {
                         format = format.section(' ', 0, 0).toLower();
                         if (!acodecsList.contains(format)) {
                             kDebug() << "*****  UNSUPPORTED ACODEC: " << format;
-                            sizeItem->setHidden(true);
+                            //sizeItem->setHidden(true);
+                            sizeItem->setFlags(Qt::ItemIsSelectable);
+                            sizeItem->setToolTip(i18n("Unsupported audio codec: %1", format));
+                            sizeItem->setIcon(brokenIcon);
                         }
                     }
                 }
@@ -677,7 +796,10 @@ void RenderWidget::refreshView() {
                         format = format.section(' ', 0, 0).toLower();
                         if (!vcodecsList.contains(format)) {
                             kDebug() << "*****  UNSUPPORTED VCODEC: " << format;
-                            sizeItem->setHidden(true);
+                            //sizeItem->setHidden(true);
+                            sizeItem->setFlags(Qt::ItemIsSelectable);
+                            sizeItem->setToolTip(i18n("Unsupported video codec: %1", format));
+                            sizeItem->setIcon(brokenIcon);
                         }
                     }
                 }
@@ -689,7 +811,8 @@ void RenderWidget::refreshView() {
     refreshParams();
 }
 
-KUrl RenderWidget::filenameWithExtension(KUrl url, QString extension) {
+KUrl RenderWidget::filenameWithExtension(KUrl url, QString extension)
+{
     QString path;
     if (!url.isEmpty()) {
         path = url.path();
@@ -704,7 +827,8 @@ KUrl RenderWidget::filenameWithExtension(KUrl url, QString extension) {
 }
 
 
-void RenderWidget::refreshParams() {
+void RenderWidget::refreshParams()
+{
     QListWidgetItem *item = m_view.size_list->currentItem();
     if (!item || item->isHidden()) {
         m_view.advanced_params->clear();
@@ -735,26 +859,36 @@ void RenderWidget::refreshParams() {
 //         m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
 //     }
     m_view.out_file->setFilter("*." + extension);
-
-    if (item->data(EditableRole).toString().isEmpty()) {
+    QString edit = item->data(EditableRole).toString();
+    if (edit.isEmpty() || !edit.endsWith("customprofiles.xml")) {
         m_view.buttonDelete->setEnabled(false);
         m_view.buttonEdit->setEnabled(false);
     } else {
         m_view.buttonDelete->setEnabled(true);
         m_view.buttonEdit->setEnabled(true);
     }
-    m_view.buttonStart->setEnabled(true);
+
+    m_view.buttonStart->setEnabled(m_view.size_list->currentItem()->flags() & Qt::ItemIsEnabled);
 }
 
-void RenderWidget::reloadProfiles() {
+void RenderWidget::reloadProfiles()
+{
     parseProfiles();
 }
 
-void RenderWidget::parseProfiles(QString group, QString profile) {
+void RenderWidget::parseProfiles(QString meta, QString group, QString profile)
+{
     m_view.size_list->clear();
     m_view.format_list->clear();
     m_view.destination_list->clear();
     m_view.destination_list->addItem(KIcon("video-x-generic"), i18n("File rendering"));
+    m_view.destination_list->addItem(KIcon("media-optical"), i18n("DVD"), "dvd");
+    m_view.destination_list->addItem(KIcon("audio-x-generic"), i18n("Audio only"), "audioonly");
+    m_view.destination_list->addItem(KIcon("applications-internet"), i18n("Web sites"), "websites");
+    m_view.destination_list->addItem(KIcon("applications-multimedia"), i18n("Media players"), "mediaplayers");
+    m_view.destination_list->addItem(KIcon("drive-harddisk"), i18n("Lossless / HQ"), "lossless");
+    m_view.destination_list->addItem(KIcon("pda"), i18n("Mobile devices"), "mobile");
+
     QString exportFile = KStandardDirs::locate("appdata", "export/profiles.xml");
     parseFile(exportFile, false);
 
@@ -763,43 +897,156 @@ void RenderWidget::parseProfiles(QString group, QString profile) {
     QDir directory = QDir(exportFolder);
     QStringList filter;
     filter << "*.xml";
-    const QStringList fileList = directory.entryList(filter, QDir::Files);
-    foreach(const QString filename, fileList)
-    parseFile(exportFolder + '/' + filename, filename == "customprofiles.xml");
-
+    QStringList fileList = directory.entryList(filter, QDir::Files);
+    // We should parse customprofiles.xml in last position, so that user profiles
+    // can also override profiles installed by KNewStuff
+    fileList.removeAll("customprofiles.xml");
+    foreach(const QString &filename, fileList)
+    parseFile(exportFolder + '/' + filename, true);
+    if (QFile::exists(exportFolder + "/customprofiles.xml")) parseFile(exportFolder + "/customprofiles.xml", true);
+
+    if (!meta.isEmpty()) {
+        m_view.destination_list->blockSignals(true);
+        m_view.destination_list->setCurrentIndex(m_view.destination_list->findData(meta));
+        m_view.destination_list->blockSignals(false);
+    }
     refreshView();
     QList<QListWidgetItem *> child;
     if (!group.isEmpty()) child = m_view.format_list->findItems(group, Qt::MatchExactly);
-    if (!child.isEmpty()) m_view.format_list->setCurrentItem(child.at(0));
+    if (!child.isEmpty()) {
+        for (int i = 0; i < child.count(); i++) {
+            if (child.at(i)->data(MetaGroupRole).toString() == meta) {
+                m_view.format_list->setCurrentItem(child.at(i));
+                break;
+            }
+        }
+    }
     child.clear();
     if (!profile.isEmpty()) child = m_view.size_list->findItems(profile, Qt::MatchExactly);
     if (!child.isEmpty()) m_view.size_list->setCurrentItem(child.at(0));
 }
 
-void RenderWidget::parseFile(QString exportFile, bool editable) {
+void RenderWidget::parseFile(QString exportFile, bool editable)
+{
+    kDebug() << "// Parsing file: " << exportFile;
+    kDebug() << "------------------------------";
     QDomDocument doc;
     QFile file(exportFile);
     doc.setContent(&file, false);
     file.close();
     QDomElement documentElement;
     QDomElement profileElement;
+    QString extension;
+    QListWidgetItem *item;
     QDomNodeList groups = doc.elementsByTagName("group");
 
-    if (groups.count() == 0) {
-        kDebug() << "// Export file: " << exportFile << " IS BROKEN";
+    if (editable || groups.count() == 0) {
+        QDomElement profiles = doc.documentElement();
+        if (editable && profiles.attribute("version", 0).toInt() < 1) {
+            kDebug() << "// OLD profile version";
+            // this is an old profile version, update it
+            QDomDocument newdoc;
+            QDomElement newprofiles = newdoc.createElement("profiles");
+            newprofiles.setAttribute("version", 1);
+            newdoc.appendChild(newprofiles);
+            QDomNodeList profilelist = doc.elementsByTagName("profile");
+            for (int i = 0; i < profilelist.count(); i++) {
+                QString category = i18n("Custom");
+                QString extension;
+                QDomNode parent = profilelist.at(i).parentNode();
+                if (!parent.isNull()) {
+                    QDomElement parentNode = parent.toElement();
+                    if (parentNode.hasAttribute("name")) category = parentNode.attribute("name");
+                    extension = parentNode.attribute("extension");
+                }
+                profilelist.at(i).toElement().setAttribute("category", category);
+                if (!extension.isEmpty()) profilelist.at(i).toElement().setAttribute("extension", extension);
+                QDomNode n = profilelist.at(i).cloneNode();
+                newprofiles.appendChild(newdoc.importNode(n, true));
+            }
+            if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+                KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
+                return;
+            }
+            QTextStream out(&file);
+            out << newdoc.toString();
+            file.close();
+            parseFile(exportFile, editable);
+            return;
+        }
+
+        QDomNode node = doc.elementsByTagName("profile").at(0);
+        if (node.isNull()) {
+            kDebug() << "// Export file: " << exportFile << " IS BROKEN";
+            return;
+        }
+        int count = 1;
+        while (!node.isNull()) {
+            QDomElement profile = node.toElement();
+            QString profileName = profile.attribute("name");
+            QString standard = profile.attribute("standard");
+            QString params = profile.attribute("args");
+            QString category = profile.attribute("category", i18n("Custom"));
+            QString dest = profile.attribute("destinationid");
+            QString prof_extension = profile.attribute("extension");
+            if (!prof_extension.isEmpty()) extension = prof_extension;
+
+            QList <QListWidgetItem *> list = m_view.format_list->findItems(category, Qt::MatchExactly);
+            bool exists = false;
+            for (int j = 0; j < list.count(); j++) {
+                if (list.at(j)->data(MetaGroupRole) == dest) {
+                    exists = true;
+                    break;
+                }
+            }
+            if (!exists) {
+                item = new QListWidgetItem(category, m_view.format_list);
+                item->setData(MetaGroupRole, dest);
+            }
+
+            // Check if item with same name already exists and replace it,
+            // allowing to override default profiles
+
+            list = m_view.size_list->findItems(profileName, Qt::MatchExactly);
+
+            for (int j = 0; j < list.count(); j++) {
+                if (list.at(j)->data(MetaGroupRole) == dest) {
+                    QListWidgetItem *duplicate = list.takeAt(j);
+                    delete duplicate;
+                    j--;
+                }
+            }
+
+            item = new QListWidgetItem(profileName, m_view.size_list);
+            //kDebug() << "// ADDINg item with name: " << profileName << ", GRP" << category << ", DEST:" << dest ;
+            item->setData(GroupRole, category);
+            item->setData(MetaGroupRole, dest);
+            item->setData(ExtensionRole, extension);
+            item->setData(RenderRole, "avformat");
+            item->setData(StandardRole, standard);
+            item->setData(ParamsRole, params);
+            if (profile.hasAttribute("url")) item->setData(ExtraRole, profile.attribute("url"));
+            if (editable) {
+                item->setData(EditableRole, exportFile);
+                if (exportFile.endsWith("customprofiles.xml")) item->setIcon(KIcon("emblem-favorite"));
+                else item->setIcon(KIcon("applications-internet"));
+            }
+            node = doc.elementsByTagName("profile").at(count);
+            count++;
+        }
         return;
     }
 
     int i = 0;
     QString groupName;
     QString profileName;
-    QString extension;
+
     QString prof_extension;
     QString renderer;
     QString params;
     QString standard;
     KIcon icon;
-    QListWidgetItem *item;
+
     while (!groups.item(i).isNull()) {
         documentElement = groups.item(i).toElement();
         QDomNode gname = documentElement.elementsByTagName("groupname").at(0);
@@ -818,10 +1065,18 @@ void RenderWidget::parseFile(QString exportFile, bool editable) {
                 m_view.destination_list->addItem(icon, i18n(metagroupName.toUtf8().data()), metagroupId);
             }
         }
-        groupName = documentElement.attribute("name", QString::null);
-        extension = documentElement.attribute("extension", QString::null);
-        renderer = documentElement.attribute("renderer", QString::null);
-        if (m_view.format_list->findItems(groupName, Qt::MatchExactly).isEmpty()) {
+        groupName = documentElement.attribute("name", i18n("Custom"));
+        extension = documentElement.attribute("extension", QString());
+        renderer = documentElement.attribute("renderer", QString());
+        QList <QListWidgetItem *> list = m_view.format_list->findItems(groupName, Qt::MatchExactly);
+        bool exists = false;
+        for (int j = 0; j < list.count(); j++) {
+            if (list.at(j)->data(MetaGroupRole) == metagroupId) {
+                exists = true;
+                break;
+            }
+        }
+        if (!exists) {
             item = new QListWidgetItem(groupName, m_view.format_list);
             item->setData(MetaGroupRole, metagroupId);
         }
@@ -846,7 +1101,7 @@ void RenderWidget::parseFile(QString exportFile, bool editable) {
             item->setData(StandardRole, standard);
             item->setData(ParamsRole, params);
             if (profileElement.hasAttribute("url")) item->setData(ExtraRole, profileElement.attribute("url"));
-            if (editable) item->setData(EditableRole, "true");
+            if (editable) item->setData(EditableRole, exportFile);
             n = n.nextSibling();
         }
 
@@ -854,7 +1109,8 @@ void RenderWidget::parseFile(QString exportFile, bool editable) {
     }
 }
 
-void RenderWidget::setRenderJob(const QString &dest, int progress) {
+void RenderWidget::setRenderJob(const QString &dest, int progress)
+{
     QTreeWidgetItem *item;
     QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
     if (!existing.isEmpty()) item = existing.at(0);
@@ -873,7 +1129,8 @@ void RenderWidget::setRenderJob(const QString &dest, int progress) {
     }
 }
 
-void RenderWidget::setRenderStatus(const QString &dest, int status, const QString &error) {
+void RenderWidget::setRenderStatus(const QString &dest, int status, const QString &error)
+{
     QTreeWidgetItem *item;
     QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
     if (!existing.isEmpty()) item = existing.at(0);
@@ -892,7 +1149,7 @@ void RenderWidget::setRenderStatus(const QString &dest, int status, const QStrin
             emit openDvdWizard(item->text(1), item->data(0, Qt::UserRole + 1).toString());
         } else if (itemGroup == "websites") {
             QString url = item->data(0, Qt::UserRole + 1).toString();
-            if (!url.isEmpty()) KRun *openBrowser = new KRun(url, this);
+            if (!url.isEmpty()) new KRun(url, this);
         }
     } else if (status == -2) {
         // Rendering crashed
@@ -912,12 +1169,14 @@ void RenderWidget::setRenderStatus(const QString &dest, int status, const QStrin
     slotCheckJob();
 }
 
-void RenderWidget::slotAbortCurrentJob() {
+void RenderWidget::slotAbortCurrentJob()
+{
     QTreeWidgetItem *current = m_view.running_jobs->currentItem();
     if (current) emit abortProcess(current->text(1));
 }
 
-void RenderWidget::slotCheckJob() {
+void RenderWidget::slotCheckJob()
+{
     bool activate = false;
     QTreeWidgetItem *current = m_view.running_jobs->currentItem();
     if (current) {
@@ -927,7 +1186,8 @@ void RenderWidget::slotCheckJob() {
     m_view.abort_job->setEnabled(activate);
 }
 
-void RenderWidget::parseScriptFiles() {
+void RenderWidget::parseScriptFiles()
+{
     QStringList scriptsFilter;
     scriptsFilter << "*.sh";
     m_view.scripts_list->clear();
@@ -964,12 +1224,13 @@ void RenderWidget::parseScriptFiles() {
         (*it)->setSelected(true);
         activate = true;
     }
-    kDebug() << "SELECTED SCRIPTS: " << m_view.scripts_list->selectedItems().count();
+    kDebug() << "SELECTED SCRIPTS: " << m_view.scripts_list->selectedItems().count();
     m_view.start_script->setEnabled(activate);
     m_view.delete_script->setEnabled(activate);
 }
 
-void RenderWidget::slotCheckScript() {
+void RenderWidget::slotCheckScript()
+{
     bool activate = false;
     QTreeWidgetItemIterator it(m_view.scripts_list);
     if (*it) activate = true;
@@ -977,7 +1238,8 @@ void RenderWidget::slotCheckScript() {
     m_view.delete_script->setEnabled(activate);
 }
 
-void RenderWidget::slotStartScript() {
+void RenderWidget::slotStartScript()
+{
     QTreeWidgetItem *item = m_view.scripts_list->currentItem();
     if (item) {
         QString path = item->data(0, Qt::UserRole + 1).toString();
@@ -986,7 +1248,8 @@ void RenderWidget::slotStartScript() {
     }
 }
 
-void RenderWidget::slotDeleteScript() {
+void RenderWidget::slotDeleteScript()
+{
     QTreeWidgetItem *item = m_view.scripts_list->currentItem();
     if (item) {
         QString path = item->data(0, Qt::UserRole + 1).toString();
@@ -996,6 +1259,8 @@ void RenderWidget::slotDeleteScript() {
     }
 }
 
-void RenderWidget::slotGenerateScript() {
+void RenderWidget::slotGenerateScript()
+{
     slotExport(true);
-}
\ No newline at end of file
+}
+