Changeset View
Changeset View
Standalone View
Standalone View
src/document/katedocument.cpp
Show First 20 Lines • Show All 91 Lines • ▼ Show 20 Line(s) | |||||
193 | // KTextEditor::DocumentPrivate Constructor | 193 | // KTextEditor::DocumentPrivate Constructor | ||
194 | // | 194 | // | ||
195 | KTextEditor::DocumentPrivate::DocumentPrivate(bool bSingleViewMode, | 195 | KTextEditor::DocumentPrivate::DocumentPrivate(bool bSingleViewMode, | ||
196 | bool bReadOnly, QWidget *parentWidget, | 196 | bool bReadOnly, QWidget *parentWidget, | ||
197 | QObject *parent) | 197 | QObject *parent) | ||
198 | : KTextEditor::Document (this, parent), | 198 | : KTextEditor::Document (this, parent), | ||
199 | m_bSingleViewMode(bSingleViewMode), | 199 | m_bSingleViewMode(bSingleViewMode), | ||
200 | m_bReadOnly(bReadOnly), | 200 | m_bReadOnly(bReadOnly), | ||
201 | | ||||
202 | m_undoManager(new KateUndoManager(this)), | 201 | m_undoManager(new KateUndoManager(this)), | ||
203 | | ||||
204 | m_buffer(new KateBuffer(this)), | 202 | m_buffer(new KateBuffer(this)), | ||
205 | m_indenter(new KateAutoIndent(this)), | 203 | m_indenter(new KateAutoIndent(this)), | ||
206 | | ||||
207 | m_docName(QStringLiteral("need init")), | | |||
208 | | ||||
209 | m_fileType(QStringLiteral("Normal")), | 204 | m_fileType(QStringLiteral("Normal")), | ||
210 | | ||||
211 | m_config(new KateDocumentConfig(this)) | 205 | m_config(new KateDocumentConfig(this)) | ||
212 | | ||||
213 | { | 206 | { | ||
214 | /** | 207 | /** | ||
215 | * no plugins from kparts here | 208 | * no plugins from kparts here | ||
216 | */ | 209 | */ | ||
217 | setPluginLoadingMode (DoNotLoadPlugins); | 210 | setPluginLoadingMode (DoNotLoadPlugins); | ||
218 | 211 | | |||
219 | /** | 212 | /** | ||
220 | * pass on our component data, do this after plugin loading is off | 213 | * pass on our component data, do this after plugin loading is off | ||
▲ Show 20 Lines • Show All 182 Lines • ▼ Show 20 Line(s) | |||||
4226 | inline static QString removeNewLines(const QString &str) | 4219 | inline static QString removeNewLines(const QString &str) | ||
4227 | { | 4220 | { | ||
4228 | QString tmp(str); | 4221 | QString tmp(str); | ||
4229 | return tmp.replace(QLatin1String("\r\n"), QLatin1String(" ")) | 4222 | return tmp.replace(QLatin1String("\r\n"), QLatin1String(" ")) | ||
4230 | .replace(QLatin1Char('\r'), QLatin1Char(' ')) | 4223 | .replace(QLatin1Char('\r'), QLatin1Char(' ')) | ||
4231 | .replace(QLatin1Char('\n'), QLatin1Char(' ')); | 4224 | .replace(QLatin1Char('\n'), QLatin1Char(' ')); | ||
4232 | } | 4225 | } | ||
4233 | 4226 | | |||
4227 | static QStringList reverseSplittedUrl(const QUrl &url) { | ||||
4228 | | ||||
4229 | QString fullUrl = removeNewLines(url.url()); | ||||
dhaumann: missing const. | |||||
4230 | | ||||
4231 | if (fullUrl.isEmpty()) { | ||||
4232 | return QStringList(); | ||||
4233 | } | ||||
4234 | | ||||
4235 | QStringList splittedUrl = fullUrl.split(QLatin1Char('/'), QString::SkipEmptyParts); | ||||
Could you use QString::splitRef() to avoid not needed QString allocations? You will get a QVector<QStringRef> instead of a QStringList. dhaumann: Could you use QString::splitRef() to avoid not needed QString allocations? You will get a… | |||||
4236 | | ||||
4237 | // Remove not useful parts... | ||||
4238 | splittedUrl.removeLast(); // The file name | ||||
4239 | // ...to avoid odd naming in rare cases we force numbering fallback | ||||
4240 | if (splittedUrl.at(0) == QLatin1String("file:")) { | ||||
dhaumann: I wonder how this behaves on Windows :-) | |||||
4241 | splittedUrl.removeAt(0); | ||||
4242 | if (splittedUrl.at(0) == QLatin1String("home")) { | ||||
dhaumann: Same here: Windows? | |||||
4243 | splittedUrl.removeAt(0); | ||||
4244 | } | ||||
4245 | } | ||||
4246 | | ||||
4247 | // Reverse our url; Thanks to https://stackoverflow.com/a/20652805 | ||||
4248 | for(int k = 0, s = splittedUrl.size(), max = (s / 2); k < max; k++) { | ||||
4249 | splittedUrl.swap(k, s - (1 + k)); | ||||
4250 | } | ||||
4251 | | ||||
4252 | return splittedUrl; | ||||
4253 | } | ||||
4254 | | ||||
4234 | void KTextEditor::DocumentPrivate::updateDocName() | 4255 | void KTextEditor::DocumentPrivate::updateDocName() | ||
4235 | { | 4256 | { | ||
4257 | QString fileName = removeNewLines(url().fileName()); | ||||
4258 | | ||||
4236 | // if the name is set, and starts with FILENAME, it should not be changed! | 4259 | // if the name is set, and starts with FILENAME, it should not be changed! | ||
4237 | if (! url().isEmpty() | 4260 | if (! url().isEmpty() && m_docName.startsWith(fileName)) { | ||
4238 | && (m_docName == removeNewLines(url().fileName()) || | | |||
4239 | m_docName.startsWith(removeNewLines(url().fileName()) + QLatin1String(" (")))) { | | |||
4240 | return; | 4261 | return; | ||
4241 | } | 4262 | } | ||
4242 | 4263 | | |||
4243 | int count = -1; | 4264 | QString newName = m_docName; | ||
4265 | | ||||
4266 | if (m_docName.isEmpty() && url().isEmpty()) { | ||||
4267 | newName = i18n("Untitled"); | ||||
4268 | } else if (m_docName == i18n("Untitled") && !url().isEmpty()) { | ||||
4269 | newName = fileName; | ||||
4270 | } | ||||
4271 | | ||||
4272 | // Collect all files with same name... | ||||
4273 | typedef QPair<KTextEditor::DocumentPrivate*, QStringList> FooPair; | ||||
dhaumann: FooPair? ... | |||||
4274 | QList<FooPair> fooList; | ||||
4275 | // ...and save to each the url in a special rehashed form, starting with current document | ||||
4276 | fooList << FooPair(this, reverseSplittedUrl(url())); | ||||
4244 | 4277 | | |||
4278 | int count = -1; // To catch multiple "Untitled" case only | ||||
4245 | foreach (KTextEditor::DocumentPrivate *doc, KTextEditor::EditorPrivate::self()->kateDocuments()) { | 4279 | foreach (KTextEditor::DocumentPrivate *doc, KTextEditor::EditorPrivate::self()->kateDocuments()) { | ||
4246 | if ((doc != this) && (doc->url().fileName() == url().fileName())) | 4280 | if ((doc != this) && (doc->url().fileName() == url().fileName())) { | ||
4247 | if (doc->m_docNameNumber > count) { | 4281 | count = qMax(count, doc->m_docNameNumber); | ||
4248 | count = doc->m_docNameNumber; | 4282 | fooList << FooPair(doc, reverseSplittedUrl(doc->url())); | ||
4249 | } | 4283 | } | ||
4250 | } | 4284 | } | ||
4251 | 4285 | | |||
4252 | m_docNameNumber = count + 1; | 4286 | // Did we find at least two files with same name? | ||
4287 | if (fooList.size() > 1 && !url().isEmpty()) { | ||||
4288 | count = -1; // Count not unique names as fallback | ||||
4289 | // Sort our list with shortest url on top (position 0) | ||||
4290 | std::sort(fooList.begin(), fooList.end(), [](FooPair a, FooPair b) { return a.second.size() < b.second.size(); }); | ||||
4291 | QSet<QString> uniqueNames; // Remember given names | ||||
4292 | // To make an unique name, prefix some folder name to the document name | ||||
4293 | for (QList<FooPair>::iterator i = fooList.begin(); i != fooList.end(); ++i) { | ||||
4294 | if (!(*i).second.size()) { | ||||
4295 | // Can happen in rare cases due to quirks in reverseSplittedUrl(..); use numbering scheme fallback | ||||
4296 | (*i).first->setDocumentName(fileName, count++); | ||||
4297 | continue; | ||||
4298 | } | ||||
4299 | | ||||
4300 | // First Try: Is the folder name, where file is located, enough to be unique? | ||||
4301 | QString docName = (*i).second.at(0) + QLatin1String("/") + fileName; | ||||
4302 | if (!uniqueNames.contains(docName)) { | ||||
4303 | uniqueNames.insert(docName); | ||||
4304 | (*i).first->setDocumentName(docName); | ||||
4253 | 4305 | | |||
4254 | QString oldName = m_docName; | 4306 | } else { | ||
4255 | m_docName = removeNewLines(url().fileName()); | 4307 | // Second Try: Find a folder name somewhere above to become an unique name | ||
4308 | bool noSuccess = true; | ||||
4309 | for (int j = 1; j < (*i).second.size(); ++j) { | ||||
4310 | // docName = (*i).second.at(j) + QLatin1String("/…/") + fileName; // Bad shown, why? | ||||
4311 | // docName = (*i).second.at(j) + QLatin1String("/~/") + fileName; // How about that? | ||||
4312 | docName = (*i).second.at(j) + QLatin1String("/.../") + fileName; | ||||
4313 | if (!uniqueNames.contains(docName)) { | ||||
4314 | uniqueNames.insert(docName); | ||||
4315 | (*i).first->setDocumentName(docName); | ||||
4316 | noSuccess = false; | ||||
4317 | break; | ||||
4318 | } | ||||
4319 | } | ||||
4320 | // Third: We need to use the numbering scheme fallback | ||||
4321 | if (noSuccess) { | ||||
4322 | (*i).first->setDocumentName(fileName, count++); | ||||
4323 | } | ||||
4324 | } | ||||
4325 | } | ||||
4256 | 4326 | | |||
4257 | m_isUntitled = m_docName.isEmpty(); | 4327 | // Either no doubles or an "Untitled" document | ||
4258 | if (m_isUntitled) { | 4328 | } else { | ||
4259 | m_docName = i18n("Untitled"); | 4329 | setDocumentName(newName, count); | ||
4260 | } | 4330 | } | ||
4331 | } | ||||
4332 | | ||||
4333 | void KTextEditor::DocumentPrivate::setDocumentName(const QString &name, int number /*= -1*/) | ||||
4334 | { | ||||
4335 | m_docNameNumber = number + 1; | ||||
4336 | | ||||
4337 | QString newName = name; | ||||
4261 | 4338 | | |||
4262 | if (m_docNameNumber > 0) { | 4339 | if (m_docNameNumber > 0) { | ||
4263 | m_docName = QString(m_docName + QLatin1String(" (%1)")).arg(m_docNameNumber + 1); | 4340 | // Should rarely happens in normal cases, but always when we have more than one "Untitled" document | ||
4341 | newName = QString(name + QLatin1String(" (%1)")).arg(m_docNameNumber + 1); | ||||
4264 | } | 4342 | } | ||
4265 | 4343 | | |||
4266 | /** | 4344 | if (newName == m_docName) { | ||
4267 | * avoid to emit this, if name doesn't change! | 4345 | return; | ||
4268 | */ | | |||
4269 | if (oldName != m_docName) { | | |||
4270 | emit documentNameChanged(this); | | |||
4271 | } | 4346 | } | ||
4347 | | ||||
4348 | m_docName = newName; | ||||
4349 | | ||||
4350 | emit documentNameChanged(this); | ||||
4272 | } | 4351 | } | ||
4273 | 4352 | | |||
4274 | void KTextEditor::DocumentPrivate::slotModifiedOnDisk(KTextEditor::View * /*v*/) | 4353 | void KTextEditor::DocumentPrivate::slotModifiedOnDisk(KTextEditor::View * /*v*/) | ||
4275 | { | 4354 | { | ||
4276 | if (url().isEmpty() || !m_modOnHd) { | 4355 | if (url().isEmpty() || !m_modOnHd) { | ||
4277 | return; | 4356 | return; | ||
4278 | } | 4357 | } | ||
4279 | 4358 | | |||
▲ Show 20 Lines • Show All 91 Lines • Show Last 20 Lines |
missing const.