summaryrefslogtreecommitdiffstats
path: root/heimdall-frontend/source/qml/DropFiles.qml
blob: 9187b4f12a80e6e267681f5c2851604558b40a12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
import QtQuick.Dialogs 1.2
import "FileUtils.js" as FileUtils

DropFilesForm {
	id: background
	property var fileUrls: []
	signal nextPressed(var files)

	ListModel {
		id: fileModel
	}

	function setFileGridVisible(visible) {
		if (fileGridContainer.visible !== visible) {
			dropFilesContainer.visible = !visible;
			bottomButtonsContainer.visible = visible;
			fileGridContainer.visible = visible;

			if (visible) {
				fileGridView.forceActiveFocus();
			}
		}
	}

	function addFiles(urls) {
		var count = urls.length;
		if (count > 0) {
			for (var i = 0; i < count; i++) {
				if (FileUtils.isFile(urls[i])) {
					var filename = FileUtils.filenameFromUrl(urls[i]);

					fileModel.append({ icon: "drop_zone.svg", text: filename });

					if (FileUtils.isArchive(filename)) {
						fileUrls.push(FileUtils.extractArchive(urls[i]));
					} else {
						fileUrls.push(urls[i]);
					}
				}
			}

			setFileGridVisible(true);
		}
	}

	function removeFile(index) {
		if (index >= 0) {
			fileModel.remove(index);
			fileUrls.splice(index, 1);

			if (fileUrls.length == 0) {
				setFileGridVisible(false);
			}
		}
	}

	onKeyPressed: {
		if (event.key === Qt.Key_Delete || event.key === Qt.Key_Backspace) {
			event.accepted = true;
			removeFile(fileGridView.currentIndex);
		}
	}

	dropFilesArea {
		onDropped: {
			if ((drop.action == Qt.CopyAction || drop.action == Qt.MoveAction) && drop.urls.length > 0) {
				addFiles(drop.urls);
				drop.acceptProposedAction();
				drop.accept(Qt.CopyAction);
			}
		}
	}

	browseButton.onClicked: {
		browseDialog.open()
	}

	bottomBrowseButton.onClicked: {
		browseDialog.open()
	}

	nextButton {
		onClicked: nextPressed(fileUrls)
	}

	fileGridView {
		model: fileModel
	}

	FileDialog {
		id: browseDialog
		title: "Select firmware file(s)"
		selectMultiple: true
		selectFolder: false
		onAccepted: {
			addFiles(browseDialog.fileUrls);
		}
	}
}