From 6971b49f85d9ddeb2da968a594d65affec3d2b44 Mon Sep 17 00:00:00 2001 From: Benjamin Dobell Date: Tue, 10 Mar 2015 02:49:32 +1100 Subject: Lots of Frontend refactoring with a few bug fixes There is still far too much overly convoluted code. However, the introduction of some RAII when dealing with files has made the code-base slightly less error prone. --- heimdall-frontend/source/qml/FileUtils.js | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 heimdall-frontend/source/qml/FileUtils.js (limited to 'heimdall-frontend/source/qml/FileUtils.js') diff --git a/heimdall-frontend/source/qml/FileUtils.js b/heimdall-frontend/source/qml/FileUtils.js new file mode 100644 index 0000000..3735a37 --- /dev/null +++ b/heimdall-frontend/source/qml/FileUtils.js @@ -0,0 +1,44 @@ +function clipFileExtension(filename) { + var periodIndex = filename.lastIndexOf('.'); + + if (periodIndex > 0) { + return filename.slice(0, periodIndex - 1); + } else if (periodIndex === 0) { + return ""; + } + + return filename; +} + +function filenameFromUrl(url) { + var urlString = url.toString(); + return urlString.slice(urlString.lastIndexOf('/') + 1); +} + +function fileExtension(url) { + var filename = filenameFromUrl(url); + var periodIndex = filename.lastIndexOf('.'); + + if (periodIndex >= 0) { + return filename.slice(periodIndex + 1); + } + + return ""; +} + +// TODO: Real implemention - call out to C++ and validate with QFileInfo etc. +function isFile(url) { + var filename = filenameFromUrl(url); + return filename.length > 0; +} + +function isArchive(url) { + var filename = filenameFromUrl(url); + var extension = fileExtension(filename); + return (extension === 'tar' || extension === 'zip') + || (extension === 'gz' && fileExtension(clipFileExtension(filename)) === 'tar'); +} + +function extractArchive(url) { + +} -- cgit v1.2.3