| 208 | } |
| 209 | |
| 210 | void Scratchpad::renameScratch(const QModelIndex& index, const QString& previousName) |
| 211 | { |
| 212 | const QString newName = index.data().toString(); |
| 213 | if (newName.contains(QDir::separator())) { |
| 214 | m_model->setData(index, previousName); // undo |
| 215 | emit actionFailed(i18n("Failed to rename scratch: Names must not include path separator")); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | const QString previousPath = m_dataDirectory + previousName; |
| 220 | const QString newPath = m_dataDirectory + index.data().toString(); |
| 221 | if (previousPath == newPath) { |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | // close the document before renaming its file |
| 226 | // FIXME is there a better way ? this feels hacky |
| 227 | auto* document = core()->documentController()->documentForUrl(QUrl::fromLocalFile(previousPath)); |
| 228 | if (document) { |
| 229 | const QPointer thisGuard(this); |
| 230 | if (!document->close()) { |
| 231 | // canceled by the user => rollback |
| 232 | if (thisGuard) { |
| 233 | m_model->setData(index, previousName); |
| 234 | } // else: already destroyed (KDevelop is probably exiting now) |
| 235 | return; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if (QFile::rename(previousPath, newPath)) { |
| 240 | qCDebug(PLUGIN_SCRATCHPAD) << "renamed" << previousPath << "to" << newPath; |
| 241 | |
| 242 | m_model->setData(index, newPath, Scratchpad::FullPathRole); |
| 243 | m_model->itemFromIndex(index)->setIcon(m_iconProvider.icon(QFileInfo(newPath))); |
| 244 | auto config = scratchCommands(); |
| 245 | config.deleteEntry(previousName); |
| 246 | config.writeEntry(newName, index.data(Scratchpad::RunCommandRole)); |
| 247 | |
| 248 | if (document) { |
| 249 | // re-open the closed document |
| 250 | openScratchDocument(newPath, newName); |
| 251 | } |
| 252 | } else { |
| 253 | qCWarning(PLUGIN_SCRATCHPAD) << "failed renaming" << previousPath << "to" << newPath; |
| 254 | // rollback |
| 255 | m_model->setData(index, previousName); |
| 256 | emit actionFailed(i18n("Failed renaming scratch.")); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void Scratchpad::addFileToModel(const QFileInfo& fileInfo) |
| 261 | { |
no test coverage detected