MCPcopy Create free account
hub / github.com/KDE/kdevelop / renameScratch

Method renameScratch

plugins/scratchpad/scratchpad.cpp:210–258  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

208}
209
210void 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
260void Scratchpad::addFileToModel(const QFileInfo& fileInfo)
261{

Callers 1

setModelDataMethod · 0.80

Calls 14

renameFunction · 0.85
scratchCommandsFunction · 0.85
documentForUrlMethod · 0.80
documentControllerMethod · 0.80
setIconMethod · 0.80
writeEntryMethod · 0.80
QFileInfoClass · 0.70
toStringMethod · 0.45
dataMethod · 0.45
containsMethod · 0.45
setDataMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected