Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PathListWidget.cpp « mumble « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6aabe69fa293ec34079832e3b1abe8d0b798644b (plain)
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
// Copyright 2017-2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "PathListWidget.h"

#include "Overlay.h"

#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QMimeData>
#include <QtGui/QDragEnterEvent>
#include <QtGui/QDragMoveEvent>
#include <QtGui/QDropEvent>

PathListWidget::PathListWidget(QWidget *parent) : QListWidget(parent), pathType(FILE_EXE) {
	setAcceptDrops(true);
}

void PathListWidget::setPathType(PathType type) {
	pathType = type;
}

void PathListWidget::addFilePath(const QString &path) {
	QString qsAppIdentifier = OverlayAppInfo::applicationIdentifierForPath(path);
	QStringList qslIdentifiers;
	for (int i = 0; i < count(); i++) {
		qslIdentifiers << item(i)->data(Qt::UserRole).toString();
	}
	if (!qslIdentifiers.contains(qsAppIdentifier)) {
		OverlayAppInfo oai               = OverlayAppInfo::applicationInfoForId(qsAppIdentifier);
		QListWidgetItem *qlwiApplication = new QListWidgetItem(oai.qiIcon, oai.qsDisplayName, this);
		qlwiApplication->setData(Qt::UserRole, QVariant(qsAppIdentifier));
		setCurrentItem(qlwiApplication);
	}
}

void PathListWidget::addFolderPath(const QString &path) {
	QString dir = QDir::toNativeSeparators(path);
	QStringList qslIdentifiers;
	for (int i = 0; i < count(); i++) {
		qslIdentifiers << item(i)->data(Qt::UserRole).toString();
	}
	if (!dir.isEmpty() && !qslIdentifiers.contains(dir)) {
		QListWidgetItem *qlwiPath = new QListWidgetItem(QIcon(), QDir(dir).path(), this);
		qlwiPath->setData(Qt::UserRole, QVariant(dir));
		setCurrentItem(qlwiPath);
	}
}

void PathListWidget::checkAcceptDragEvent(QDropEvent *event, bool store) {
	if (event->mimeData()->hasUrls()) {
		foreach (QUrl url, event->mimeData()->urls()) {
			if (url.isLocalFile()) {
				QFileInfo info(url.toLocalFile());
				switch (pathType) {
					case FILE_EXE:
						if (info.isFile() && info.isExecutable()) {
							if (store) {
								addFilePath(info.filePath());
							}
							event->setDropAction(Qt::LinkAction);
							event->accept();
						}
						break;
					case FOLDER:
						if (info.isDir()) {
							if (store) {
								addFolderPath(url.toLocalFile());
							}
							event->setDropAction(Qt::LinkAction);
							event->accept();
						}
						break;
				}
			}
		}
	}
}

void PathListWidget::dragEnterEvent(QDragEnterEvent *event) {
	checkAcceptDragEvent(event, false);
}

void PathListWidget::dragMoveEvent(QDragMoveEvent *event) {
	checkAcceptDragEvent(event, false);
}

void PathListWidget::dropEvent(QDropEvent *event) {
	checkAcceptDragEvent(event, true);
}