2
* Copyright (C) 2014 - 2015 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
#include "appchoosercombobox.h"
21
#include "icontheme.h"
22
#include "appchooserdialog.h"
23
#include "utilities.h"
27
AppChooserComboBox::AppChooserComboBox(QWidget* parent):
34
blockOnCurrentIndexChanged_(false) {
36
// the new Qt5 signal/slot syntax cannot handle overloaded methods by default
37
// hence a type-casting is needed here. really ugly!
38
// reference: http://qt-project.org/forums/viewthread/21513
39
connect((QComboBox*)this, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &AppChooserComboBox::onCurrentIndexChanged);
42
AppChooserComboBox::~AppChooserComboBox() {
44
fm_mime_type_unref(mimeType_);
46
g_object_unref(defaultApp_);
47
// delete GAppInfo objects stored for Combobox
49
g_list_foreach(appInfos_, (GFunc)g_object_unref, NULL);
50
g_list_free(appInfos_);
54
void AppChooserComboBox::setMimeType(FmMimeType* mimeType) {
57
fm_mime_type_unref(mimeType_);
59
mimeType_ = fm_mime_type_ref(mimeType);
61
const char* typeName = fm_mime_type_get_type(mimeType_);
62
defaultApp_ = g_app_info_get_default_for_type(typeName, FALSE);
63
appInfos_ = g_app_info_get_all_for_type(typeName);
65
for(GList* l = appInfos_; l; l = l->next, ++i) {
66
GAppInfo* app = G_APP_INFO(l->data);
67
GIcon* gicon = g_app_info_get_icon(app);
68
QString name = QString::fromUtf8(g_app_info_get_name(app));
69
// QVariant data = qVariantFromValue<void*>(app);
70
// addItem(IconTheme::icon(gicon), name, data);
71
addItem(IconTheme::icon(gicon), name);
72
if(g_app_info_equal(app, defaultApp_))
76
// add "Other applications" item
77
insertSeparator(count());
78
addItem(tr("Customize"));
79
if(defaultAppIndex_ != -1)
80
setCurrentIndex(defaultAppIndex_);
83
// returns the currently selected app.
84
GAppInfo* AppChooserComboBox::selectedApp() {
85
return G_APP_INFO(g_list_nth_data(appInfos_, currentIndex()));
88
bool AppChooserComboBox::isChanged() {
89
return (defaultAppIndex_ != currentIndex());
92
void AppChooserComboBox::onCurrentIndexChanged(int index) {
93
if(index == -1 || index == prevIndex_ || blockOnCurrentIndexChanged_)
96
// the last item is "Customize"
97
if(index == (count() - 1)) {
98
/* TODO: let the user choose an app or add custom actions here. */
99
QWidget* toplevel = topLevelWidget();
100
AppChooserDialog dlg(mimeType_, toplevel);
101
dlg.setWindowModality(Qt::WindowModal);
102
dlg.setCanSetDefault(false);
103
if(dlg.exec() == QDialog::Accepted) {
104
GAppInfo* app = dlg.selectedApp();
106
/* see if it's already in the list to prevent duplication */
108
for(found = appInfos_; found; found = found->next) {
109
if(g_app_info_equal(app, G_APP_INFO(found->data)))
113
// inserting new items or change current index will recursively trigger onCurrentIndexChanged.
114
// we need to block our handler to prevent recursive calls.
115
blockOnCurrentIndexChanged_ = true;
116
/* if it's already in the list, select it */
118
setCurrentIndex(g_list_position(appInfos_, found));
121
else { /* if it's not found, add it to the list */
122
appInfos_ = g_list_prepend(appInfos_, app);
123
GIcon* gicon = g_app_info_get_icon(app);
124
QString name = QString::fromUtf8(g_app_info_get_name(app));
125
insertItem(0, IconTheme::icon(gicon), name);
128
blockOnCurrentIndexChanged_ = false;
133
// block our handler to prevent recursive calls.
134
blockOnCurrentIndexChanged_ = true;
135
// restore to previously selected item
136
setCurrentIndex(prevIndex_);
137
blockOnCurrentIndexChanged_ = false;
146
/* get a list of custom apps added with app-chooser.
147
* the returned GList is owned by the combo box and shouldn't be freed. */
148
const GList* AppChooserComboBox::customApps() {