Changeset View
Changeset View
Standalone View
Standalone View
kerfuffle/addtoarchive.cpp
Show First 20 Lines • Show All 153 Lines • ▼ Show 20 Line(s) | 149 | } else { | |||
---|---|---|---|---|---|
154 | } | 154 | } | ||
155 | 155 | | |||
156 | if (m_firstPath.isEmpty()) { | 156 | if (m_firstPath.isEmpty()) { | ||
157 | qCWarning(ARK) << "Weird, this should not happen. no firstpath defined. aborting"; | 157 | qCWarning(ARK) << "Weird, this should not happen. no firstpath defined. aborting"; | ||
158 | emitResult(); | 158 | emitResult(); | ||
159 | return; | 159 | return; | ||
160 | } | 160 | } | ||
161 | 161 | | |||
162 | QString base = QFileInfo(m_inputs.first()).absoluteFilePath(); | 162 | const QString base = detectBaseName(m_inputs); | ||
elvisangelaccio: How about introducing a new (public) function?
You would then do something like `const QString… | |||||
163 | if (base.endsWith(QLatin1Char('/'))) { | | |||
164 | base.chop(1); | | |||
165 | } | | |||
166 | 163 | | |||
167 | QString finalName = base + QLatin1Char( '.' ) + m_autoFilenameSuffix; | 164 | QString finalName = base + QLatin1Char( '.' ) + m_autoFilenameSuffix; | ||
168 | 165 | | |||
169 | //if file already exists, append a number to the base until it doesn't | 166 | //if file already exists, append a number to the base until it doesn't | ||
rthomsen: Coding style: Please remove the spaces within the QLatin1Char(). | |||||
170 | //exist | 167 | //exist | ||
171 | int appendNumber = 0; | 168 | int appendNumber = 0; | ||
172 | while (QFileInfo(finalName).exists()) { | 169 | while (QFileInfo(finalName).exists()) { | ||
173 | ++appendNumber; | 170 | ++appendNumber; | ||
174 | finalName = base + QLatin1Char( '_' ) + QString::number(appendNumber) + QLatin1Char( '.' ) + m_autoFilenameSuffix; | 171 | finalName = base + QLatin1Char( '_' ) + QString::number(appendNumber) + QLatin1Char( '.' ) + m_autoFilenameSuffix; | ||
175 | } | 172 | } | ||
176 | 173 | | |||
177 | qCDebug(ARK) << "Autoset filename to "<< finalName; | 174 | qCDebug(ARK) << "Autoset filename to "<< finalName; | ||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Line(s) | 224 | { | |||
228 | qCDebug(ARK) << "AddToArchive job finished"; | 225 | qCDebug(ARK) << "AddToArchive job finished"; | ||
229 | 226 | | |||
230 | if (job->error() && !job->errorText().isEmpty()) { | 227 | if (job->error() && !job->errorText().isEmpty()) { | ||
231 | KMessageBox::error(Q_NULLPTR, job->errorText()); | 228 | KMessageBox::error(Q_NULLPTR, job->errorText()); | ||
232 | } | 229 | } | ||
233 | 230 | | |||
234 | emitResult(); | 231 | emitResult(); | ||
235 | } | 232 | } | ||
233 | | ||||
234 | QString AddToArchive::detectBaseName(const QStringList &paths) const | ||||
235 | { | ||||
236 | QString base = QFileInfo(paths.first()).absoluteFilePath(); | ||||
237 | if (paths.size() > 1) { | ||||
238 | QDir dir = QFileInfo(paths.first()).dir(); | ||||
239 | if (!dir.isRoot()) { | ||||
240 | base = dir.absolutePath() + QLatin1Char('/') + dir.dirName(); | ||||
241 | } | ||||
242 | } | ||||
243 | | ||||
244 | if (base.endsWith(QLatin1Char('/'))) { | ||||
245 | base.chop(1); | ||||
246 | } | ||||
247 | | ||||
248 | return base; | ||||
249 | } | ||||
250 | | ||||
236 | } | 251 | } |
How about introducing a new (public) function?
You would then do something like const QString base = detectBaseName(m_input);
This would allow us to easily unit test it, in the future.