Skip to content

Commit e5ac36d

Browse files
committed
refactor: simplify archive path parsing
Eliminate needing to call parseArchivePath directly: - make dirPath() return the archive path - add containerPath() - optimize and unit test Media path helpers Replace remaining occurances of archivePaths()
1 parent a95129d commit e5ac36d

File tree

10 files changed

+193
-162
lines changed

10 files changed

+193
-162
lines changed

src/database.cpp

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,28 +1230,12 @@ bool Database::filterMatch(const SearchParams& params, MediaGroup& match) {
12301230

12311231
// remove match if in the same directory/zip as needle
12321232
if (params.filterParent && match.count() > 1) {
1233-
if (match[0].isArchived()) {
1234-
QString parent;
1235-
match[0].archivePaths(&parent);
1236-
for (int i = 1; i < match.count(); ++i)
1237-
if (match[i].isArchived()) {
1238-
QString p;
1239-
match[i].archivePaths(&p);
1240-
if (p == parent) {
1241-
match.remove(i);
1242-
--i;
1243-
}
1244-
}
1245-
} else {
1246-
auto parent = match[0].path().split("/");
1247-
parent.pop_back();
1248-
for (int i = 1; i < match.count(); ++i) {
1249-
auto tmp = match[i].path().split("/");
1250-
tmp.pop_back();
1251-
if (tmp == parent) {
1252-
match.remove(i);
1253-
--i;
1254-
}
1233+
const QString parent = match[0].dirPath();
1234+
for (int i = 1; i < match.count(); ++i) {
1235+
const QString tmp = match[i].dirPath();
1236+
if (tmp == parent) {
1237+
match.remove(i);
1238+
--i;
12551239
}
12561240
}
12571241
}

src/engine.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,18 @@ void Engine::update(bool wait, const QString& dirPath) {
147147
// updating correctly
148148
// FIXME: this can take a long time, figure out where
149149
// the invalid paths actually come from
150-
QStringList paths = indexedFiles.values(); // sort to reduce random access
151-
paths.sort();
150+
const QStringList paths = ([&indexedFiles] {
151+
auto list = indexedFiles.values(); // sort to reduce random access
152+
list.sort();
153+
return list;
154+
})();
152155
QSet<QString> checked;
153156
int progress = 0;
154-
for (int i = 0; i < paths.count(); ++i) {
155-
auto& path = paths[i];
156-
if (Media::isArchived(path)) Media::archivePaths(path, &path);
157+
int i = 0;
158+
for (auto path : paths) {
159+
if (auto archive = Media::parseArchivePath(path)) {
160+
path = QString(archive->parentPath);
161+
}
157162

158163
if (checked.contains(path)) continue; // skip zip members
159164
checked.insert(path);
@@ -171,6 +176,7 @@ void Engine::update(bool wait, const QString& dirPath) {
171176
relPath.contains("//"))
172177
qCritical("invalid path in database:\n\tcanonical=%s\n\tdatabase =%s",
173178
qUtf8Printable(canRelPath), qUtf8Printable(relPath));
179+
i++;
174180
if (progress++ == 10) {
175181
progress = 0;
176182
qInfo("<NC>%s: validating index <PL> %d/%lld", qUtf8Printable(db->path()), i,

src/gui/mediabrowser.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ int MediaBrowser::showFolders(const MediaGroupList& list, const MediaWidgetOptio
136136
if (!key.isEmpty()) key = key.split(qq("==")).back(); // don't display -group-by expression
137137

138138
if (key.isEmpty()) {
139-
if (first.isArchived())
140-
first.archivePaths(&key);
141-
else if (first.type() == Media::TypeVideo) // don't group videos TODO: option
139+
if (first.type() == Media::TypeVideo) // don't group videos TODO: option
142140
key = first.path();
143141
else
144142
key = first.dirPath();
@@ -209,11 +207,7 @@ int MediaBrowser::showSets(const MediaGroupList& list, const MediaWidgetOptions&
209207
for (const MediaGroup& g : list) {
210208
QStringList dirPaths;
211209
for (const Media& m : g) {
212-
QString path;
213-
if (m.isArchived())
214-
m.archivePaths(&path);
215-
else
216-
path = m.dirPath();
210+
QString path = m.dirPath();
217211
if (!dirPaths.contains(path)) dirPaths.append(path);
218212
}
219213

@@ -315,8 +309,7 @@ void MediaBrowser::showIndex(const MediaGroup& index,
315309
// if we opened the folder and modified the path
316310
// we will not see it here, check if it still exists
317311
const Media& m = group.at(_groupIndex);
318-
QString filePath = m.path();
319-
if (m.isArchived()) m.archivePaths(&filePath);
312+
const QString filePath = m.containerPath();
320313
if (!QFile::exists(filePath)) return;
321314

322315
QImage img = loadThumb(group.at(_groupIndex), _options);

src/gui/mediagrouplistwidget.cpp

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -759,20 +759,20 @@ QMenu* MediaGroupListWidget::dirMenu(const char* slot) {
759759
const QModelIndex index = currentIndex();
760760
if (index.isValid()) selectedIndex = index.row();
761761

762-
for (int i = 0; i < group.count(); ++i)
763-
if (i != selectedIndex) {
764-
const auto& m = group.at(i);
765-
if (!MediaPage::isAnalysis(m)) {
766-
QString path = m.dirPath();
767-
if (m.isArchived()) {
768-
m.archivePaths(&path);
769-
auto list = path.split(lc('/'));
770-
list.removeLast();
771-
path = list.join(lc('/'));
772-
}
773-
groupDirs.insert(path);
774-
}
762+
for (int i = 0; i < group.count(); ++i) {
763+
const Media& m = group.at(i);
764+
if (i == selectedIndex) continue;
765+
if (MediaPage::isAnalysis(m)) continue;
766+
767+
QString path = m.dirPath();
768+
if (m.isArchived()) {
769+
int index = path.lastIndexOf(u'/'); // parent dir of zipfile
770+
if (index >= 0) path = path.mid(0, index);
771+
}
772+
if (!path.isEmpty()) {
773+
groupDirs.insert(path);
775774
}
775+
}
776776

777777
const auto& keys = groupDirs.values();
778778
QList<QAction*> actions;
@@ -964,9 +964,6 @@ void MediaGroupListWidget::removeSelection(bool deleteFiles, bool replace) {
964964
Q_ASSERT(index >=0 && index < groupCount);
965965
const Media& m = page->group[index];
966966

967-
QString path = m.path();
968-
if (m.isArchived()) m.archivePaths(&path);
969-
970967
if (!deleteFiles) {
971968
removedIndices.insert(index);
972969
if (m.isValid())
@@ -987,6 +984,7 @@ void MediaGroupListWidget::removeSelection(bool deleteFiles, bool replace) {
987984
continue;
988985
}
989986

987+
QString path = m.containerPath();
990988
{
991989
static bool skipDeleteConfirmation = false;
992990
int button = 0;
@@ -1206,12 +1204,7 @@ void MediaGroupListWidget::renameFileAction() {
12061204

12071205
// names of matches
12081206
for (auto& m2 : group) {
1209-
if (m2.isArchived()) {
1210-
QString fileName;
1211-
m2.archivePaths(nullptr, &fileName);
1212-
maybeAppend(completions, fileName);
1213-
} else
1214-
maybeAppend(completions, m2.name());
1207+
maybeAppend(completions, m2.name());
12151208
}
12161209

12171210
// also files in same directory
@@ -1265,13 +1258,7 @@ void MediaGroupListWidget::copyNameAction() {
12651258
}
12661259

12671260
const QFileInfo info(selected->path());
1268-
QString otherName;
1269-
if (other->isArchived())
1270-
other->archivePaths(nullptr, &otherName);
1271-
else
1272-
otherName = other->name(); // TODO: should name() work with archives?
1273-
1274-
QString newName = QFileInfo(otherName).completeBaseName() + "." + info.suffix();
1261+
const QString newName = other->completeBaseName() + u'.' + info.suffix();
12751262
const QString oldPath = selected->path();
12761263
if (_options.db) {
12771264
if (_options.db->rename(*selected, newName))
@@ -1351,16 +1338,11 @@ void MediaGroupListWidget::moveFolderAction() {
13511338
QSet<QString> moved;
13521339

13531340
for (Media& m : selectedMedia()) {
1354-
QString srcPath;
1355-
if (m.isArchived())
1356-
m.archivePaths(&srcPath);
1357-
else
1358-
srcPath = m.dirPath();
1359-
1341+
const QString srcPath = m.dirPath();
13601342
if (moved.contains(srcPath)) // already moved
13611343
continue;
13621344

1363-
const QString dstPath = dirPath + "/" + QFileInfo(srcPath).fileName();
1345+
const QString dstPath = dirPath + u'/' + QFileInfo(srcPath).fileName();
13641346
moveDatabaseDir(m, dstPath);
13651347
moved += srcPath;
13661348
}
@@ -1881,11 +1863,7 @@ void MediaGroupListWidget::browseParentAction() {
18811863
}
18821864

18831865
const Media& m = g.first();
1884-
QString path;
1885-
if (m.isArchived())
1886-
m.archivePaths(&path);
1887-
else
1888-
path = m.dirPath();
1866+
const QString path = m.dirPath();
18891867

18901868
MediaGroup siblings = _options.db->mediaWithPathLike(path + lc('%'));
18911869
Media::sortGroup(siblings, {qq("path")});
@@ -2002,17 +1980,15 @@ void MediaGroupListWidget::updateItems() {
20021980
}
20031981

20041982
int fileCount = 0;
2005-
if (m.isArchived()) {
1983+
if (auto archive = m.parseArchivePath()) {
20061984
// can be slow for large archives, we can cache since
20071985
// archives are immutable here
2008-
QString archivePath;
2009-
m.archivePaths(&archivePath);
2010-
auto it = _archiveFileCount.find(archivePath);
1986+
auto it = _archiveFileCount.find(archive->parentPath);
20111987
if (it != _archiveFileCount.end())
20121988
fileCount = *it;
20131989
else {
20141990
fileCount = m.archiveCount();
2015-
_archiveFileCount.insert(archivePath, fileCount);
1991+
_archiveFileCount.insert(archive->parentPath.toString(), fileCount);
20161992
}
20171993
} else if (fileInfo.isFile()) {
20181994
const auto key = fileInfo.absolutePath();

src/main.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,9 +564,8 @@ static void nuke(const MediaGroup& group) {
564564
bool yesAll = false; // don't ask again
565565
for (auto& m : std::as_const(group)) {
566566
QString path = m.path();
567-
if (m.isArchived()) {
568-
m.archivePaths(&path);
569-
567+
if (auto archive = m.parseArchivePath()) {
568+
path = archive->parentPath.toString();
570569
if (zips.contains(path)) continue;
571570
zips.insert(path);
572571

@@ -1347,13 +1346,13 @@ int main(int argc, char** argv) {
13471346
MediaGroup moveable;
13481347
QStringList movedZips;
13491348
for (auto& m : selection) {
1350-
if (!m.isArchived()) {
1349+
auto archive = m.parseArchivePath();
1350+
if (!archive) {
13511351
moveable.append(m);
13521352
continue;
13531353
}
13541354

1355-
QString zipPath;
1356-
m.archivePaths(&zipPath);
1355+
QString zipPath(archive->parentPath);
13571356
if (movedZips.contains(zipPath)) continue;
13581357

13591358
const QFileInfo info(zipPath);
@@ -1679,7 +1678,7 @@ int main(int argc, char** argv) {
16791678
if (selection.count() && !selection.first().attributes().contains("sort"))
16801679
Media::sortGroup(selection, {"path"});
16811680

1682-
auto getProp = Media::propertyFunc("parentPath");
1681+
auto getProp = Media::propertyFunc("dirPath");
16831682

16841683
qDebug("make groups of %d", widgetOptions.maxPerPage);
16851684
MediaGroupList list;

0 commit comments

Comments
 (0)