bool GalleryUtil::MoveDirectory(const QFileInfo src, QFileInfo &dst) { QDir srcDir(src.absoluteFilePath()); dst = MakeUniqueDirectory(dst); if (!dst.exists()) { srcDir.mkdir(dst.absoluteFilePath()); dst.refresh(); } if (!dst.exists() || !dst.isDir()) return false; bool ok = true; QDir dstDir(dst.absoluteFilePath()); srcDir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot); QFileInfoList list = srcDir.entryInfoList(); QFileInfoList::const_iterator it = list.begin(); for (; it != list.end(); ++it) { const QString fn = it->fileName(); QFileInfo dfi(dstDir, fn); ok &= Move(*it, dfi); } return ok && FileDelete(src); }
static status_t CopyDirectoryRecursive(const char * oldDirPath, const char * newDirPath) { if (strcmp(oldDirPath, newDirPath) == 0) return B_NO_ERROR; // paranoia: Copying a directory onto itself is a no-op Directory srcDir(oldDirPath); if (srcDir.IsValid() == false) return B_ERROR; if (Directory::MakeDirectory(newDirPath, true, true) != B_NO_ERROR) return B_ERROR; const String srcDirPath = srcDir.GetPath(); String dstDirPath; { // In an inner scope simply to keep (dstDir) off the stack during any recursion Directory dstDir(newDirPath); if (dstDir.IsValid() == false) return B_ERROR; dstDirPath = dstDir.GetPath(); } const char * curSourceName; while((curSourceName = srcDir.GetCurrentFileName()) != NULL) { if ((strcmp(curSourceName, ".") != 0)&&(strcmp(curSourceName, "..") != 0)) { if (CopyFile((srcDirPath+curSourceName)(), (dstDirPath+curSourceName)(), true) != B_NO_ERROR) return B_ERROR; } srcDir++; } return B_NO_ERROR; }
bool GalleryUtil::CopyDirectory(const QFileInfo src, QFileInfo &dst) { QDir srcDir(src.absoluteFilePath()); dst = MakeUniqueDirectory(dst); if (!dst.exists()) { srcDir.mkdir(dst.absoluteFilePath()); dst.refresh(); } if (!dst.exists() || !dst.isDir()) return false; bool ok = true; QDir dstDir(dst.absoluteFilePath()); QFileInfoList list = srcDir.entryInfoList(); QFileInfoList::const_iterator it = list.begin(); for (; it != list.end(); ++it) { const QString fn = it->fileName(); if (fn != "." && fn != "..") { QFileInfo dfi(dstDir, fn); ok &= Copy(*it, dfi); } } return ok; }
bool copyDirFiles(const QString &SourceDir,const QString &DestDir,bool coverFileIfExist) { qDebug() << Q_FUNC_INFO; QDir srcDir(SourceDir); QDir dstDir(DestDir); if(!dstDir.exists()) if(dstDir.mkdir(dstDir.absolutePath())) return false; QFileInfoList fileInfoList = srcDir.entryInfoList(); foreach(QFileInfo fileInfo,fileInfoList) { if(fileInfo.fileName() == "." || fileInfo.fileName() == "..") continue; if(fileInfo.isDir()) { if(!copyDirFiles(fileInfo.filePath(),dstDir.filePath(fileInfo.fileName()),coverFileIfExist)) return false; } else { if(coverFileIfExist && dstDir.exists(fileInfo.fileName())) dstDir.remove(fileInfo.fileName()); if(!QFile::copy(fileInfo.filePath(),dstDir.filePath(fileInfo.fileName()))) return false; } } return true; }
bool ValidatorGenerator::generate(const QString &dst) const { // Writes each files QDir dstDir(dst); QString output = QString(VALIDATOR_HEADER_TEMPLATE).arg(name.toUpper()).arg(name); FileWriter(dstDir.filePath(name.toLower() + "validator.h")).write(output, false); output = QString(VALIDATOR_IMPL_TEMPLATE).arg(name.toLower()).arg(name); FileWriter(dstDir.filePath(name.toLower() + "validator.cpp")).write(output, false); // Updates the project file ProjectFileGenerator progen(dstDir.filePath("helpers.pro")); QStringList files; files << name.toLower() + "validator.h" << name.toLower() + "validator.cpp"; return progen.add(files); }
void CBAttachmentsProvider::mergeAttachments(const QString &srcId, const QString &dstID,const QString &diff){ QString srcPath = pathForId(srcId); QDir srcDir(srcPath); if (!srcDir.exists() ){ // there is no attachments in source return; } QString dstPath = pathForId(dstID); QDir dstDir(dstPath); if (!dstDir.exists() ){ // just rename srcDir srcDir.rename(srcId,dstID); return; } QVariantList dstAttributes = attributesForId(dstID); QStringList dstFiles; for (int i = 0; i<dstAttributes.size(); ++i){ QVariantMap map = dstAttributes.at(i).toMap(); dstFiles.append(map["file"].toString()); } QVariantList srcAttributes = attributesForId(srcId); foreach (QVariant v, srcAttributes) { QVariantMap map = v.toMap(); QString fileName = map["file"].toString(); QString filePath = srcPath+fileName; if (dstFiles.contains(fileName)){ QFileInfo fileInfo(filePath); QUuid u=QUuid::createUuid(); fileName =u.toString() +"."+fileInfo.completeSuffix(); map["file"] = QVariant(fileName); } map["description"] = diff; QString copyTo = dstPath+fileName; bool flag = CBUtils::copyRecursively(filePath,copyTo); if (!flag){ return; } dstAttributes.append(map); }
int main(int argc, char *argv[]) { qInstallMsgHandler(myMessageHandler); QCoreApplication app(argc, argv); if (argc != 3) { qCritical("Usage: autotranslate <destination-dir> <source-dir>."); return -1; } QDir dstDir(app.arguments().value(1)); if (!dstDir.exists()) { qCritical("Destination directory '%s' not found.",dstDir.dirName().toLocal8Bit().constData()); return -1; } QDir srcDir(app.arguments().value(2),"*.ts",QDir::Name,QDir::Files); if (!srcDir.exists()) { qCritical("Source directory '%s' not found.",srcDir.dirName().toLocal8Bit().constData()); return -1; } QDirIterator srcIt(srcDir); while (srcIt.hasNext()) { QFile srcFile(srcIt.next()); if (srcFile.open(QFile::ReadOnly)) { QDomDocument doc; if (doc.setContent(&srcFile,true)) { qDebug("Generation auto translation from '%s'.",srcFile.fileName().toLocal8Bit().constData()); QDomElement rootElem = doc.firstChildElement("TS"); rootElem.setAttribute("language",rootElem.attribute("sourcelanguage","en")); QDomElement contextElem = rootElem.firstChildElement("context"); while(!contextElem.isNull()) { QDomElement messageElem = contextElem.firstChildElement("message"); while(!messageElem.isNull()) { QDomElement sourceElem = messageElem.firstChildElement("source"); QDomElement translationElem = messageElem.firstChildElement("translation"); if (!sourceElem.isNull() && !translationElem.isNull()) { QString sourceText = sourceElem.text(); if (messageElem.attribute("numerus") == "yes") { QDomElement numerusEelem = translationElem.firstChildElement("numerusform"); while(!numerusEelem.isNull()) { numerusEelem.removeChild(numerusEelem.firstChild()); numerusEelem.appendChild(doc.createTextNode(sourceText)); numerusEelem = numerusEelem.nextSiblingElement("numerusform"); } } else { translationElem.removeChild(translationElem.firstChild()); translationElem.appendChild(doc.createTextNode(sourceText)); } translationElem.removeAttribute("type"); } messageElem = messageElem.nextSiblingElement("message"); } contextElem = contextElem.nextSiblingElement("context"); } srcFile.close(); QFileInfo srcFileInfo(srcDir.absoluteFilePath(srcFile.fileName())); QFile dstFile(dstDir.absoluteFilePath(srcFileInfo.fileName())); if (dstFile.open(QFile::WriteOnly|QFile::Truncate)) { dstFile.write(doc.toByteArray()); dstFile.close(); } else { qWarning("Failed to open destination file '%s' for write.",dstFile.fileName().toLocal8Bit().constData()); } } else { qWarning("Invalid translation source file '%s'.",srcFile.fileName().toLocal8Bit().constData()); } srcFile.close(); } else { qWarning("Could not open translation source file '%s'.",srcFile.fileName().toLocal8Bit().constData()); } } return 0; }
bool synchDirStructures(const QString &srcPath, const QString &dstPath) { bool result = true; bool test = true; QDir srcDir(srcPath); QDir dstDir(dstPath); if (srcDir.canonicalPath() == dstDir.canonicalPath()){ LOG_FREE(Warn, "synchDirStructures", "Cannot synch destination '" << toString(dstPath) << "' with source '" << toString(srcPath) << "' because they resolve to the same location"); return true; // already synched } LOG_FREE(Info, "synchDirStructures", "Synching destination '" << toString(dstPath) << "' with source '" << toString(srcPath) << "'"); // remove all files in dst as well as any directories in dst that are not in src for (const QFileInfo &dstItemInfo : dstDir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)) { QString srcItemPath = srcPath + "/" + dstItemInfo.fileName(); QString dstItemPath = dstPath + "/" + dstItemInfo.fileName(); QFileInfo srcItemInfo = QFileInfo(srcItemPath); if (dstItemInfo.isDir()){ if (srcItemInfo.exists() && srcItemInfo.isDir()){ // directory also exists in source test = synchDirStructures(srcItemPath, dstItemPath); if (!test){ LOG_FREE(Error, "synchDirStructures", "Failed to synch destination '" << toString(dstItemPath) << "' with source '" << toString(srcItemPath) << "'"); result = false; // DLM: do we really want to give up here? //return result; } }else{ // directory does not exist in source test = removeDir(dstItemPath); if (!test){ LOG_FREE(Error, "synchDirStructures", "Failed to remove directory '" << toString(dstItemPath) << "'"); result = false; // DLM: do we really want to give up here? //return result; } } }else if (dstItemInfo.isFile()){ test = QFile::remove(dstItemInfo.absoluteFilePath()); if (!test){ LOG_FREE(Error, "synchDirStructures", "Failed to remove file '" << toString(dstItemInfo.absoluteFilePath()) << "'"); result = false; // DLM: do we really want to give up here? //return result; } } } return result; }