+
+bool ArchiveWidget::processProjectFile()
+{
+ KUrl destUrl;
+ QTreeWidgetItem *item;
+ bool isArchive = compressed_archive->isChecked();
+
+ for (int i = 0; i < files_list->topLevelItemCount(); i++) {
+ QTreeWidgetItem *parentItem = files_list->topLevelItem(i);
+ if (parentItem->childCount() > 0) {
+ destUrl = KUrl(archive_url->url().path(KUrl::AddTrailingSlash) + parentItem->data(0, Qt::UserRole).toString());
+ bool isSlideshow = parentItem->data(0, Qt::UserRole).toString() == "slideshows";
+ for (int j = 0; j < parentItem->childCount(); j++) {
+ item = parentItem->child(j);
+ KUrl src(item->text(0));
+ KUrl dest = destUrl;
+ if (isSlideshow) {
+ dest = KUrl(destUrl.path(KUrl::AddTrailingSlash) + item->data(0, Qt::UserRole).toString() + "/" + src.fileName());
+ }
+ else if (item->data(0, Qt::UserRole).isNull()) {
+ dest.addPath(src.fileName());
+ }
+ else {
+ dest.addPath(item->data(0, Qt::UserRole).toString());
+ }
+ m_replacementList.insert(src, dest);
+ }
+ }
+ }
+
+ QDomElement mlt = m_doc.documentElement();
+ QString root = mlt.attribute("root") + "/";
+
+ // Adjust global settings
+ QString basePath;
+ if (isArchive) basePath = "$CURRENTPATH";
+ else basePath = archive_url->url().path(KUrl::RemoveTrailingSlash);
+ mlt.setAttribute("root", basePath);
+ QDomElement project = mlt.firstChildElement("kdenlivedoc");
+ project.setAttribute("projectfolder", basePath);
+
+ // process kdenlive producers
+ QDomNodeList prods = mlt.elementsByTagName("kdenlive_producer");
+ for (int i = 0; i < prods.count(); i++) {
+ QDomElement e = prods.item(i).toElement();
+ if (e.isNull()) continue;
+ if (e.hasAttribute("resource")) {
+ KUrl src(e.attribute("resource"));
+ KUrl dest = m_replacementList.value(src);
+ if (!dest.isEmpty()) e.setAttribute("resource", dest.path());
+ }
+ if (e.hasAttribute("proxy") && e.attribute("proxy") != "-") {
+ KUrl src(e.attribute("proxy"));
+ KUrl dest = m_replacementList.value(src);
+ if (!dest.isEmpty()) e.setAttribute("proxy", dest.path());
+ }
+ }
+
+ // process mlt producers
+ prods = mlt.elementsByTagName("producer");
+ for (int i = 0; i < prods.count(); i++) {
+ QDomElement e = prods.item(i).toElement();
+ if (e.isNull()) continue;
+ QString src = EffectsList::property(e, "resource");
+ if (!src.isEmpty()) {
+ if (!src.startsWith('/')) src.prepend(root);
+ KUrl srcUrl(src);
+ KUrl dest = m_replacementList.value(src);
+ if (!dest.isEmpty()) EffectsList::setProperty(e, "resource", dest.path());
+ }
+ }
+
+ // process mlt transitions (for luma files)
+ prods = mlt.elementsByTagName("transition");
+ QString attribute;
+ for (int i = 0; i < prods.count(); i++) {
+ QDomElement e = prods.item(i).toElement();
+ if (e.isNull()) continue;
+ attribute = "resource";
+ QString src = EffectsList::property(e, attribute);
+ if (src.isEmpty()) attribute = "luma";
+ src = EffectsList::property(e, attribute);
+ if (!src.isEmpty()) {
+ if (!src.startsWith('/')) src.prepend(root);
+ KUrl srcUrl(src);
+ KUrl dest = m_replacementList.value(src);
+ if (!dest.isEmpty()) EffectsList::setProperty(e, attribute, dest.path());
+ }
+ }
+
+ QString playList = m_doc.toString();
+ if (isArchive) {
+ QString startString("\"");
+ startString.append(archive_url->url().path(KUrl::RemoveTrailingSlash));
+ QString endString("\"");
+ endString.append(basePath);
+ playList.replace(startString, endString);
+ startString = ">" + archive_url->url().path(KUrl::RemoveTrailingSlash);
+ endString = ">" + basePath;
+ playList.replace(startString, endString);
+ }
+
+ if (isArchive) {
+ m_temp = new KTemporaryFile;
+ if (!m_temp->open()) KMessageBox::error(this, i18n("Cannot create temporary file"));
+ m_temp->write(playList.toUtf8());
+ m_temp->close();
+ m_archiveThread = QtConcurrent::run(this, &ArchiveWidget::createArchive);
+ return true;
+ }
+
+ QString path = archive_url->url().path(KUrl::AddTrailingSlash) + m_name + ".kdenlive";
+ QFile file(path);
+ if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+ kWarning() << "////// ERROR writing to file: " << path;
+ KMessageBox::error(this, i18n("Cannot write to file %1", path));
+ return false;
+ }
+
+ file.write(m_doc.toString().toUtf8());
+ if (file.error() != QFile::NoError) {
+ KMessageBox::error(this, i18n("Cannot write to file %1", path));
+ file.close();
+ return false;
+ }
+ file.close();
+ return true;
+}
+
+void ArchiveWidget::createArchive()
+{
+ QFileInfo dirInfo(archive_url->url().path());
+ QString user = dirInfo.owner();
+ QString group = dirInfo.group();
+ KTar archive(archive_url->url().path(KUrl::AddTrailingSlash) + m_name + ".tar.gz", "application/x-gzip");
+ archive.open( QIODevice::WriteOnly );
+
+ // Create folders
+ foreach(const QString &path, m_foldersList) {
+ archive.writeDir(path, user, group);
+ }
+
+ // Add files
+ int ix = 0;
+ QMapIterator<QString, QString> i(m_filesList);
+ while (i.hasNext()) {
+ i.next();
+ archive.addLocalFile(i.key(), i.value());
+ emit archiveProgress((int) 100 * ix / m_filesList.count());
+ ix++;
+ }
+
+ // Add project file
+ archive.addLocalFile(m_temp->fileName(), m_name + ".kdenlive");
+ bool result = archive.close();
+ delete m_temp;
+ emit archivingFinished(result);
+}
+
+void ArchiveWidget::slotArchivingFinished(bool result)
+{
+ if (result) {
+ slotDisplayMessage("dialog-ok", i18n("Project was successfully archived."));
+ }
+ else {
+ slotDisplayMessage("dialog-close", i18n("There was an error processing project file"));
+ }
+ progressBar->setValue(100);
+ buttonBox->button(QDialogButtonBox::Apply)->setText(i18n("Archive"));
+ archive_url->setEnabled(true);
+ compressed_archive->setEnabled(true);
+ for (int i = 0; i < files_list->topLevelItemCount(); i++) {
+ files_list->topLevelItem(i)->setDisabled(false);
+ for (int j = 0; j < files_list->topLevelItem(i)->childCount(); j++)
+ files_list->topLevelItem(i)->child(j)->setDisabled(false);
+ }
+}
+
+void ArchiveWidget::slotArchivingProgress(int p)
+{
+ progressBar->setValue(p);
+}
+
+void ArchiveWidget::slotStartExtracting()
+{
+ if (m_archiveThread.isRunning()) {
+ //TODO: abort extracting
+ return;
+ }
+ QFileInfo f(m_extractUrl.path());
+ m_requestedSize = f.size();
+ KIO::NetAccess::mkdir(archive_url->url().path(KUrl::RemoveTrailingSlash), this);
+ slotDisplayMessage("system-run", i18n("Extracting..."));
+ buttonBox->button(QDialogButtonBox::Apply)->setText(i18n("Abort"));
+ m_progressTimer = new QTimer;
+ m_progressTimer->setInterval(800);
+ m_progressTimer->setSingleShot(false);
+ connect(m_progressTimer, SIGNAL(timeout()), this, SLOT(slotExtractProgress()));
+ m_archiveThread = QtConcurrent::run(this, &ArchiveWidget::doExtracting);
+ m_progressTimer->start();
+}
+
+void ArchiveWidget::slotExtractProgress()
+{
+ KIO::DirectorySizeJob *job = KIO::directorySize(archive_url->url());
+ connect(job, SIGNAL(result(KJob*)), this, SLOT(slotGotProgress(KJob*)));
+}
+
+void ArchiveWidget::slotGotProgress(KJob* job)
+{
+ if (!job->error()) {
+ KIO::DirectorySizeJob *j = static_cast <KIO::DirectorySizeJob *>(job);
+ progressBar->setValue((int) 100 * j->totalSize() / m_requestedSize);
+ }
+ job->deleteLater();
+}
+
+void ArchiveWidget::doExtracting()
+{
+ m_extractArchive->directory()->copyTo(archive_url->url().path(KUrl::AddTrailingSlash));
+ m_extractArchive->close();
+ emit extractingFinished();
+}
+
+QString ArchiveWidget::extractedProjectFile()
+{
+ return archive_url->url().path(KUrl::AddTrailingSlash) + m_projectName;
+}
+
+void ArchiveWidget::slotExtractingFinished()
+{
+ m_progressTimer->stop();
+ delete m_progressTimer;
+ // Process project file
+ QFile file(extractedProjectFile());
+ bool error = false;
+ if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ error = true;
+ }
+ else {
+ QString playList = file.readAll();
+ file.close();
+ if (playList.isEmpty()) {
+ error = true;
+ }
+ else {
+ playList.replace("$CURRENTPATH", archive_url->url().path(KUrl::RemoveTrailingSlash));
+ if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+ kWarning() << "////// ERROR writing to file: ";
+ error = true;
+ }
+ else {
+ file.write(playList.toUtf8());
+ if (file.error() != QFile::NoError) {
+ error = true;
+ }
+ file.close();
+ }
+ }
+ }
+ if (error) {
+ KMessageBox::sorry(kapp->activeWindow(), i18n("Cannot open project file %1", extractedProjectFile()), i18n("Cannot open file"));
+ reject();
+ }
+ else accept();
+}