Cleanup some mess introduced by last commit, looking for missing slideshow clips now works

svn path=/branches/KDE4/; revision=2724
parent 228b8f42
......@@ -521,6 +521,7 @@ void DocClipBase::clearProperty(const QString &key) {
}
void DocClipBase::getFileHash(const QString &url) {
if (m_clipType == SLIDESHOW) return;
QFile file(url);
if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
QByteArray fileData;
......
......@@ -17,6 +17,9 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
#include <QCryptographicHash>
#include <QFile>
#include <KDebug>
#include <KStandardDirs>
#include <KMessageBox>
......@@ -25,7 +28,6 @@
#include <KIO/NetAccess>
#include <KApplication>
#include <QFile>
#include <mlt++/Mlt.h>
......@@ -1004,25 +1006,34 @@ void KdenliveDoc::addClip(QDomElement elem, QString clipId, bool createClipItem)
if (clip == NULL) {
elem.setAttribute("id", producerId);
QString path = elem.attribute("resource");
QString extension;
if (elem.attribute("type").toInt() == SLIDESHOW) {
extension = KUrl(path).fileName();
path = KUrl(path).directory();
}
if (!path.isEmpty() && !QFile::exists(path)) {
const QString size = elem.attribute("file_size");
const QString hash = elem.attribute("file_hash");
QString newpath;
KMessageBox::ButtonCode action = KMessageBox::No;
if (!size.isEmpty() && !hash.isEmpty()) {
if (!m_searchFolder.isEmpty()) newpath = Render::searchFileRecursively(m_searchFolder, size, hash);
else action = (KMessageBox::ButtonCode)KMessageBox::messageBox(kapp->activeWindow(), KMessageBox::WarningYesNoCancel, i18n("<qt>Clip <b>%1</b><br>is invalid, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search automatically")), KGuiItem(i18n("Remove from project")), KGuiItem(i18n("Keep as placeholder")));
if (!m_searchFolder.isEmpty()) newpath = searchFileRecursively(m_searchFolder, size, hash);
else action = (KMessageBox::ButtonCode)KMessageBox::messageBox(kapp->activeWindow(), KMessageBox::WarningYesNo, i18n("<qt>Clip <b>%1</b><br>is invalid, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search automatically")), /*KGuiItem(i18n("Remove from project")), */KGuiItem(i18n("Keep as placeholder")));
} else {
newpath = KFileDialog::getOpenFileName(KUrl("kfiledialog:///clipfolder"), QString(), kapp->activeWindow(), i18n("Looking for %1", path));
if (elem.attribute("type").toInt() == SLIDESHOW) {
if (KMessageBox::messageBox(kapp->activeWindow(), KMessageBox::WarningYesNo, i18n("<qt>Clip <b>%1</b><br>is invalid, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search automatically")), /*KGuiItem(i18n("Remove from project")),*/ KGuiItem(i18n("Keep as placeholder"))) == KMessageBox::Yes)
newpath = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow(), i18n("Looking for %1", path));
} else newpath = KFileDialog::getOpenFileName(KUrl("kfiledialog:///clipfolder"), QString(), kapp->activeWindow(), i18n("Looking for %1", path));
}
if (action == KMessageBox::Yes) {
kDebug() << "// ASKED FOR SRCH CLIP: " << clipId;
m_searchFolder = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), kapp->activeWindow());
if (!m_searchFolder.isEmpty()) {
newpath = Render::searchFileRecursively(QDir(m_searchFolder), size, hash);
newpath = searchFileRecursively(QDir(m_searchFolder), size, hash);
}
}
if (!newpath.isEmpty()) {
if (elem.attribute("type").toInt() == SLIDESHOW) newpath.append('/' + extension);
elem.setAttribute("resource", newpath);
setNewClipResource(clipId, newpath);
}
......@@ -1052,6 +1063,42 @@ void KdenliveDoc::setNewClipResource(const QString &id, const QString &path) {
}
}
QString KdenliveDoc::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const {
QString foundFileName;
QByteArray fileData;
QByteArray fileHash;
QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
if (file.open(QIODevice::ReadOnly)) {
if (QString::number(file.size()) == matchSize) {
/*
* 1 MB = 1 second per 450 files (or faster)
* 10 MB = 9 seconds per 450 files (or faster)
*/
if (file.size() > 1000000*2) {
fileData = file.read(1000000);
if (file.seek(file.size() - 1000000))
fileData.append(file.readAll());
} else
fileData = file.readAll();
file.close();
fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
if (QString(fileHash.toHex()) == matchHash)
return file.fileName();
}
}
kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
}
filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
if (!foundFileName.isEmpty())
break;
}
return foundFileName;
}
void KdenliveDoc::addClipInfo(QDomElement elem, QString clipId) {
DocClipBase *clip = m_clipManager->getClipById(clipId);
if (clip == NULL) {
......
......@@ -25,6 +25,7 @@
#include <QString>
#include <QMap>
#include <QList>
#include <QDir>
#include <QObject>
#include <QUndoGroup>
#include <QUndoStack>
......@@ -135,6 +136,7 @@ private:
QString colorToString(const QColor& c);
void checkProjectClips();
void setNewClipResource(const QString &id, const QString &path);
QString searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) const;
public slots:
void slotCreateTextClip(QString group, const QString &groupId);
......
......@@ -443,33 +443,10 @@ void ProjectList::slotRemoveInvalidClip(const QString &id) {
ProjectItem *item = getItemById(id);
if (item) {
const QString path = item->referencedClip()->fileURL().path();
//if (!path.isEmpty()) KMessageBox::sorry(this, i18n("<qt>Clip <b>%1</b><br>is invalid, will be removed from project.", path));
KMessageBox::ButtonCode action;
if (!path.isEmpty()) {
action = (KMessageBox::ButtonCode)KMessageBox::messageBox(this, KMessageBox::WarningYesNoCancel, i18n("<qt>Clip <b>%1</b><br>is invalid, what do you want to do?", path), i18n("File not found"), KGuiItem(i18n("Search automatically")), KGuiItem(i18n("Remove from project")), KGuiItem(i18n("Keep as placeholder")));
} else
action = KMessageBox::No; // then remove
if (action == KMessageBox::Yes) { // search
QString foundFileName;
if (!item->referencedClip()->getProperty("file_size").isEmpty() && !item->referencedClip()->getProperty("file_hash").isEmpty()) { // both hash and file size were registered
QString rootDir = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///clipfolder"), this);
if (!rootDir.isEmpty()) {
foundFileName = Render::searchFileRecursively(QDir(rootDir), item->referencedClip()->getProperty("file_size"), item->referencedClip()->getProperty("file_hash"));
}
}
if (foundFileName.isEmpty())
KMessageBox::sorry(this, i18n("<qt>Cannot find a match for clip<br><b>%1</b>,<br>leaving in project as a placeholder.", path));
else {
QMap <QString, QString> properties;
properties["resource"] = foundFileName;
kDebug() << "CLIP ID:" << item->referencedClip()->getId() << "--- setting 'resource' to" << foundFileName;
slotUpdateClipProperties(item->referencedClip()->getId(), properties);
}
} else if (action == KMessageBox::No) { // remove
QList <QString> ids;
ids << id;
m_doc->deleteProjectClip(ids);
} // else keep it (last choice to be automatically bound to ESC)
if (!path.isEmpty()) KMessageBox::sorry(this, i18n("<qt>Clip <b>%1</b><br>is invalid, will be removed from project.", path));
QList <QString> ids;
ids << id;
m_doc->deleteProjectClip(ids);
}
if (!m_infoQueue.isEmpty()) QTimer::singleShot(300, this, SLOT(slotProcessNextClipInQueue()));
}
......
......@@ -27,7 +27,7 @@
#include <QPainter>
#include <QItemDelegate>
#include <QUndoStack>
#include <QDir>
#include <KTreeWidgetSearchLine>
#include <KUrl>
......
......@@ -33,8 +33,6 @@ extern "C" {
#include <QTimer>
#include <QDir>
#include <QApplication>
//#include <QPainter>
#include <QCryptographicHash>
#include <KDebug>
#include <KStandardDirs>
......@@ -692,44 +690,6 @@ void Render::getFileProperties(const QDomElement &xml, const QString &clipId) {
}
//static
QString Render::searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash) {
QString foundFileName;
QByteArray fileData;
QByteArray fileHash;
QStringList filesAndDirs = dir.entryList(QDir::Files | QDir::Readable);
for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
QFile file(dir.absoluteFilePath(filesAndDirs.at(i)));
if (file.open(QIODevice::ReadOnly)) {
if (QString::number(file.size()) == matchSize) {
/*
* 1 MB = 1 second per 450 files (or faster)
* 10 MB = 9 seconds per 450 files (or faster)
*/
if (file.size() > 1000000*2) {
fileData = file.read(1000000);
if (file.seek(file.size() - 1000000))
fileData.append(file.readAll());
} else
fileData = file.readAll();
file.close();
fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
if (QString(fileHash.toHex()) == matchHash)
return file.fileName();
}
}
kDebug() << filesAndDirs.at(i) << file.size() << fileHash.toHex();
}
filesAndDirs = dir.entryList(QDir::Dirs | QDir::Readable | QDir::Executable | QDir::NoDotAndDotDot);
for (int i = 0; i < filesAndDirs.size() && foundFileName.isEmpty(); i++) {
foundFileName = searchFileRecursively(dir.absoluteFilePath(filesAndDirs.at(i)), matchSize, matchHash);
if (!foundFileName.isEmpty())
break;
}
return foundFileName;
}
/** Create the producer from the Westley QDomDocument */
#if 0
void Render::initSceneList() {
......
......@@ -22,8 +22,6 @@
#include <qstring.h>
#include <qmap.h>
#include <QList>
#include <QDir>
//#include <QWidget>
#include <kurl.h>
......@@ -180,7 +178,6 @@ Q_OBJECT public:
int mltChangeClipSpeed(ItemInfo info, double speed, double oldspeed, Mlt::Producer *prod);
QList <Mlt::Producer *> producersList();
static QString searchFileRecursively(const QDir &dir, const QString &matchSize, const QString &matchHash);
private: // Private attributes & methods
/** The name of this renderer - useful to identify the renderes by what they do - e.g. background rendering, workspace monitor, etc... */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment