+ QtConcurrent::run(this, &ProjectList::slotGenerateProxy, id);
+}
+
+void ProjectList::slotAbortProxy(const QString id)
+{
+ if (m_proxyList.contains(id)) m_proxyList.removeAll(id);
+ ProjectItem *item = getItemById(id);
+ if (item) {
+ emit projectModified();
+ if (item->isProxyReady()) slotGotProxy(id);
+ else if (item->isProxyRunning()) m_abortProxyId << id;
+ setProxyStatus(id, NOPROXY);
+ }
+}
+
+void ProjectList::slotGenerateProxy(const QString id)
+{
+ setProxyStatus(id, CREATINGPROXY);
+ ProjectItem *item = getItemById(id);
+ if (item == NULL) return;
+ QString path = item->referencedClip()->getProperty("proxy");
+ if (path.isEmpty()) {
+ setProxyStatus(id, PROXYCRASHED);
+ return;
+ }
+
+ QString url = item->clipUrl().path();
+
+ if (QFile::exists(path)) {
+ setProxyStatus(id, PROXYDONE);
+ slotGotProxy(id);
+ return;
+ }
+ else {
+ // Make sure proxy path is writable
+ QFile file(path);
+ if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+ setProxyStatus(id, PROXYCRASHED);
+ return;
+ }
+ file.close();
+ QFile::remove(path);
+ }
+
+ QStringList parameters;
+ parameters << "-i" << url;
+ QString params = proxyParams();
+ foreach(QString s, params.split(' '))
+ parameters << s;
+
+ // Make sure we don't block when proxy file already exists
+ parameters << "-y";
+ parameters << path;
+ QProcess myProcess;
+ myProcess.start("ffmpeg", parameters);
+ myProcess.waitForStarted();
+ int result = -1;
+ while (myProcess.state() != QProcess::NotRunning) {
+ // building proxy file
+ if (m_abortProxyId.contains(id)) {
+ myProcess.close();
+ myProcess.waitForFinished();
+ m_abortProxyId.removeAll(id);
+ QFile::remove(path);
+ setProxyStatus(id, NOPROXY);
+ result = -2;
+
+ }
+ myProcess.waitForFinished(500);
+ }
+ myProcess.waitForFinished();
+ if (result == -1) result = myProcess.exitStatus();
+ if (result == 0) {
+ // proxy successfully created
+ setProxyStatus(id, PROXYDONE);
+ slotGotProxy(id);
+ }
+ else if (result == 1) {
+ // Proxy process crashed
+ QFile::remove(path);
+ setProxyStatus(id, PROXYCRASHED);
+ }
+}
+
+void ProjectList::updateProxyConfig()
+{
+ ProjectItem *item;
+ QTreeWidgetItemIterator it(m_listView);
+ QUndoCommand *command = new QUndoCommand();
+ command->setText(i18n("Update proxy settings"));
+ QString proxydir = m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/";
+ while (*it) {
+ if ((*it)->type() != PROJECTCLIPTYPE) {
+ ++it;
+ continue;
+ }
+ item = static_cast<ProjectItem *>(*it);
+ if (item == NULL) {
+ ++it;
+ continue;
+ }
+ CLIPTYPE t = item->clipType();
+ if ((t == VIDEO || t == AV || t == UNKNOWN) && item->referencedClip() != NULL) {
+ if (generateProxy() && useProxy() && !item->isProxyRunning()) {
+ DocClipBase *clip = item->referencedClip();
+ if (clip->getProperty("frame_size").section('x', 0, 0).toInt() > proxyMinSize()) {
+ if (clip->getProperty("proxy").isEmpty()) {
+ // We need to insert empty proxy in old properties so that undo will work
+ QMap <QString, QString> oldProps = clip->properties();
+ oldProps.insert("proxy", QString());
+ QMap <QString, QString> newProps;
+ newProps.insert("proxy", proxydir + item->referencedClip()->getClipHash() + ".avi");
+ new EditClipCommand(this, clip->getId(), oldProps, newProps, true, command);
+ }
+ }
+ }
+ else if (item->hasProxy()) {
+ // remove proxy
+ QMap <QString, QString> newProps;
+ newProps.insert("proxy", QString());
+ newProps.insert("replace", "1");
+ new EditClipCommand(this, item->clipId(), item->referencedClip()->properties(), newProps, true, command);
+ /*item->referencedClip()->clearProperty("proxy");
+ QDomElement e = item->toXml().cloneNode().toElement();
+ e.removeAttribute("file_hash");
+ e.setAttribute("replace", 1);
+ m_infoQueue.insert(item->clipId(), e);*/
+ }
+ }
+ ++it;
+ }
+ if (command->childCount() > 0) m_doc->commandStack()->push(command);
+ else delete command;
+ if (!m_infoQueue.isEmpty() && !m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
+}
+
+void ProjectList::slotProxyCurrentItem(bool doProxy)
+{
+ QList<QTreeWidgetItem *> list = m_listView->selectedItems();
+ QTreeWidgetItem *listItem;
+ QUndoCommand *command = new QUndoCommand();
+ if (doProxy) command->setText(i18np("Add proxy clip", "Add proxy clips", list.count()));
+ else command->setText(i18np("Remove proxy clip", "Remove proxy clips", list.count()));
+
+ // Make sure the proxy folder exists
+ QString proxydir = m_doc->projectFolder().path( KUrl::AddTrailingSlash) + "proxy/";
+ KStandardDirs::makeDir(proxydir);
+
+ QMap <QString, QString> newProps;
+ QMap <QString, QString> oldProps;
+ if (!doProxy) newProps.insert("proxy", "-");
+ for (int i = 0; i < list.count(); i++) {
+ listItem = list.at(i);
+ if (listItem->type() == PROJECTFOLDERTYPE) {
+ for (int j = 0; j < listItem->childCount(); j++) {
+ QTreeWidgetItem *sub = listItem->child(j);
+ if (!list.contains(sub)) list.append(sub);
+ }
+ }
+ if (listItem->type() == PROJECTCLIPTYPE) {
+ ProjectItem *item = static_cast <ProjectItem*>(listItem);
+ CLIPTYPE t = item->clipType();
+ if ((t == VIDEO || t == AV || t == UNKNOWN) && item->referencedClip()) {
+ oldProps = item->referencedClip()->properties();
+ if (doProxy) {
+ newProps.clear();
+ QString path = proxydir + item->referencedClip()->getClipHash() + ".avi";
+ newProps.insert("proxy", path);
+ // We need to insert empty proxy so that undo will work
+ oldProps.insert("proxy", QString());
+ }
+ new EditClipCommand(this, item->clipId(), oldProps, newProps, true, command);
+ }
+ }
+ }
+ if (command->childCount() > 0) {
+ m_doc->commandStack()->push(command);
+ }
+ else delete command;
+ //if (!m_infoQueue.isEmpty() && !m_queueRunner.isRunning() && m_processingClips.isEmpty()) m_queueRunner = QtConcurrent::run(this, &ProjectList::slotProcessNextClipInQueue);
+}
+
+void ProjectList::setProxyStatus(const QString id, PROXYSTATUS status)
+{
+ ProjectItem *item = getItemById(id);
+ setProxyStatus(item, status);
+}
+
+void ProjectList::setProxyStatus(ProjectItem *item, PROXYSTATUS status)
+{
+ if (item == NULL) return;
+ monitorItemEditing(false);
+ item->setProxyStatus(status);
+ monitorItemEditing(true);
+}
+
+void ProjectList::monitorItemEditing(bool enable)
+{
+ if (enable) connect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
+ else disconnect(m_listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));