1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2015 - 2017 Michal Dutkiewicz aka Emdek <michal@emdek.pl>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 **************************************************************************/
19
20 #include "ApplicationComboBoxWidget.h"
21 #include "../core/Utils.h"
22
23 #include <QtCore/QEvent>
24 #include <QtCore/QStandardPaths>
25 #include <QtWidgets/QFileDialog>
26
27 namespace Otter
28 {
29
ApplicationComboBoxWidget(QWidget * parent)30 ApplicationComboBoxWidget::ApplicationComboBoxWidget(QWidget *parent) : QComboBox(parent),
31 m_previousIndex(0),
32 m_alwaysShowDefaultApplication(false)
33 {
34 addItem(tr("Default Application"));
35 insertSeparator(1);
36 addItem(tr("Other…"));
37
38 connect(this, static_cast<void(ApplicationComboBoxWidget::*)(int)>(&ApplicationComboBoxWidget::currentIndexChanged), this, &ApplicationComboBoxWidget::handleIndexChanged);
39 }
40
changeEvent(QEvent * event)41 void ApplicationComboBoxWidget::changeEvent(QEvent *event)
42 {
43 QComboBox::changeEvent(event);
44
45 if (event->type() == QEvent::LanguageChange)
46 {
47 setItemData(0, tr("Default Application"), Qt::DisplayRole);
48 setItemData((count() - 1), tr("Other…"), Qt::DisplayRole);
49 }
50 }
51
handleIndexChanged(int index)52 void ApplicationComboBoxWidget::handleIndexChanged(int index)
53 {
54 if (index == (count() - 1))
55 {
56 disconnect(this, static_cast<void(ApplicationComboBoxWidget::*)(int)>(&ApplicationComboBoxWidget::currentIndexChanged), this, &ApplicationComboBoxWidget::handleIndexChanged);
57
58 setCurrentIndex(m_previousIndex);
59
60 const QString path(QFileDialog::getOpenFileName(this, tr("Select Application"), QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation).first()));
61
62 if (!path.isEmpty())
63 {
64 if (count() == 3)
65 {
66 insertSeparator(2);
67 }
68
69 m_previousIndex = (count() - 2);
70
71 insertItem(m_previousIndex, QFileInfo(path).baseName(), path);
72 setCurrentIndex(m_previousIndex);
73 }
74
75 connect(this, static_cast<void(ApplicationComboBoxWidget::*)(int)>(&ApplicationComboBoxWidget::currentIndexChanged), this, &ApplicationComboBoxWidget::handleIndexChanged);
76 }
77 else
78 {
79 m_previousIndex = index;
80 }
81 }
82
setCurrentCommand(const QString & command)83 void ApplicationComboBoxWidget::setCurrentCommand(const QString &command)
84 {
85 if (command.isEmpty())
86 {
87 if (itemData(0, Qt::UserRole).toString().isEmpty())
88 {
89 setCurrentIndex(0);
90 }
91
92 return;
93 }
94
95 int index(findData(command));
96
97 if (index < 0)
98 {
99 if (count() == 3)
100 {
101 insertSeparator(1);
102 }
103
104 index = (count() - 2);
105
106 insertItem(index, command, command);
107 }
108
109 setCurrentIndex(index);
110 }
111
setMimeType(const QMimeType & mimeType)112 void ApplicationComboBoxWidget::setMimeType(const QMimeType &mimeType)
113 {
114 disconnect(this, static_cast<void(ApplicationComboBoxWidget::*)(int)>(&ApplicationComboBoxWidget::currentIndexChanged), this, &ApplicationComboBoxWidget::handleIndexChanged);
115
116 clear();
117
118 const QVector<ApplicationInformation> applications(Utils::getApplicationsForMimeType(mimeType));
119
120 if (applications.isEmpty() || m_alwaysShowDefaultApplication)
121 {
122 addItem(tr("Default Application"));
123 insertSeparator(1);
124 }
125
126 if (!applications.isEmpty())
127 {
128 for (int i = 0; i < applications.count(); ++i)
129 {
130 addItem(applications.at(i).icon, ((applications.at(i).name.isEmpty()) ? tr("Unknown") : applications.at(i).name), applications.at(i).command);
131
132 if (applications.at(i).icon.isNull())
133 {
134 model()->setData(model()->index((count() - 1), 0), QColor(Qt::transparent), Qt::DecorationRole);
135 }
136 }
137 }
138
139 if (count() > 2)
140 {
141 insertSeparator(count() + 1);
142 }
143
144 addItem(tr("Other…"));
145
146 connect(this, static_cast<void(ApplicationComboBoxWidget::*)(int)>(&ApplicationComboBoxWidget::currentIndexChanged), this, &ApplicationComboBoxWidget::handleIndexChanged);
147 }
148
setAlwaysShowDefaultApplication(bool show)149 void ApplicationComboBoxWidget::setAlwaysShowDefaultApplication(bool show)
150 {
151 m_alwaysShowDefaultApplication = show;
152 }
153
getCommand() const154 QString ApplicationComboBoxWidget::getCommand() const
155 {
156 return currentData().toString();
157 }
158
159 }
160