int Folder::renameElement(int elementPosition, string newName){ if(findFolderPosition(newName) != -1 || findFilePosition(newName) != -1){ return -1; } int newPosition = 0, renamingResult; if((size_t)elementPosition < folders.size()){ string oldPath = folders[elementPosition].getPath(); string newPath = this->getPath() + "/" + newName; renamingResult = rename(oldPath.c_str(), newPath.c_str()); if(renamingResult == 0){ folders[elementPosition].setName(newName); folders[elementPosition].setQItemName(newName); this->sort(); newPosition = findFolderPosition(newName); } } else{ string oldPath = files[elementPosition - folders.size()].getPath(); string newPath = this->getPath() + "/" + newName; renamingResult = rename(oldPath.c_str(), newPath.c_str()); if(renamingResult == 0){ files[elementPosition - folders.size()].setName(newName); files[elementPosition - folders.size()].setQItemName(newName); this->sort(); newPosition = findFilePosition(newName); } } qItem->insertRow(newPosition, qItem->takeRow(elementPosition)); return renamingResult; }
int Folder::newFile(string fileName,string content){ if(findFilePosition(fileName) != -1){ return -1; } ofstream file; string filePath = this->getPath() + '/' + fileName; file.open(filePath.c_str(), ios::out); if(file.bad()){ cerr << "Erreur d'ouverture du fichier" << endl; return -1; } else{ File *logicalFile = new File(fileName, this->getPath()); if(!content.empty()){ logicalFile->setContent(content); } this->addFile(logicalFile); this->sort(); int position = findFilePosition(fileName); qItem->insertRow(position + folders.size(), logicalFile->getQItem()); } file.close(); return 0; }
static void listDirectory(const char * directory) { WIN32_FIND_DATA ffd; HANDLE fHandle; watchDirectory(directory); const char * directoryQueryString = createDirectoryQueryString(directory); fHandle = FindFirstFile(directoryQueryString, &ffd); if (INVALID_HANDLE_VALUE == fHandle) { printf("Failed to list directory!\n"); return; } do { const char * fullPath; if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(ffd.cFileName,".") && strcmp(ffd.cFileName,"..")) { fullPath = concatDirAndFileName(directory,ffd.cFileName,true); listDirectory(fullPath); free((void*)fullPath); } else if (isLuaFile(ffd.cFileName)) { int pos; fullPath = concatDirAndFileName(directory,ffd.cFileName,false); bool isNew = findFilePosition(fullPath,&pos); if (isNew) { newFileInfoOnPos(fullPath,&ffd,pos); } else if (CompareFileTime(&ffd.ftLastWriteTime,&filesInfo[pos]->lastwrite) == 1) { reloadLuaFile(filesInfo[pos]->fileName); filesInfo[pos]->lastwrite = ffd.ftLastWriteTime; } filesInfo[pos]->shouldDelete = false; free((void*)fullPath); } } while (FindNextFile(fHandle, &ffd) != 0); free((void*)directoryQueryString); }