~lubuntu-dev/lxde/libfm-qt-debian-git

« back to all changes in this revision

Viewing changes to src/appchoosercombobox.cpp

  • Committer: Alf Gaida
  • Date: 2015-12-17 15:45:00 UTC
  • Revision ID: git-v1:99d4cf5e0b3761023e2285ffb96a79d050f0bdf4
Tags: upstream/0.10.0+20151214
Adding upstream version 0.10.0+20151214.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014 - 2015  Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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
 
17
 *
 
18
 */
 
19
 
 
20
#include "appchoosercombobox.h"
 
21
#include "icontheme.h"
 
22
#include "appchooserdialog.h"
 
23
#include "utilities.h"
 
24
 
 
25
namespace Fm {
 
26
 
 
27
AppChooserComboBox::AppChooserComboBox(QWidget* parent):
 
28
  QComboBox(parent),
 
29
  defaultApp_(NULL),
 
30
  appInfos_(NULL),
 
31
  defaultAppIndex_(-1),
 
32
  prevIndex_(0),
 
33
  mimeType_(NULL),
 
34
  blockOnCurrentIndexChanged_(false) {
 
35
 
 
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);
 
40
}
 
41
 
 
42
AppChooserComboBox::~AppChooserComboBox() {
 
43
  if(mimeType_)
 
44
    fm_mime_type_unref(mimeType_);
 
45
  if(defaultApp_)
 
46
    g_object_unref(defaultApp_);
 
47
  // delete GAppInfo objects stored for Combobox
 
48
  if(appInfos_) {
 
49
    g_list_foreach(appInfos_, (GFunc)g_object_unref, NULL);
 
50
    g_list_free(appInfos_);
 
51
  }
 
52
}
 
53
 
 
54
void AppChooserComboBox::setMimeType(FmMimeType* mimeType) {
 
55
  clear();
 
56
  if(mimeType_)
 
57
    fm_mime_type_unref(mimeType_);
 
58
 
 
59
  mimeType_ = fm_mime_type_ref(mimeType);
 
60
  if(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);
 
64
    int i = 0;
 
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_))
 
73
        defaultAppIndex_ = i;
 
74
    }
 
75
  }
 
76
  // add "Other applications" item
 
77
  insertSeparator(count());
 
78
  addItem(tr("Customize"));
 
79
  if(defaultAppIndex_ != -1)
 
80
    setCurrentIndex(defaultAppIndex_);
 
81
}
 
82
 
 
83
// returns the currently selected app.
 
84
GAppInfo* AppChooserComboBox::selectedApp() {
 
85
  return G_APP_INFO(g_list_nth_data(appInfos_, currentIndex()));
 
86
}
 
87
 
 
88
bool AppChooserComboBox::isChanged() {
 
89
  return (defaultAppIndex_ != currentIndex());
 
90
}
 
91
 
 
92
void AppChooserComboBox::onCurrentIndexChanged(int index) {
 
93
  if(index == -1 || index == prevIndex_ || blockOnCurrentIndexChanged_)
 
94
    return;
 
95
 
 
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();
 
105
      if(app) {
 
106
        /* see if it's already in the list to prevent duplication */
 
107
        GList* found = NULL;
 
108
        for(found = appInfos_; found; found = found->next) {
 
109
          if(g_app_info_equal(app, G_APP_INFO(found->data)))
 
110
            break;
 
111
        }
 
112
 
 
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 */
 
117
        if(found) {
 
118
          setCurrentIndex(g_list_position(appInfos_, found));
 
119
          g_object_unref(app);
 
120
        }
 
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);
 
126
          setCurrentIndex(0);
 
127
        }
 
128
        blockOnCurrentIndexChanged_ = false;
 
129
        return;
 
130
      }
 
131
    }
 
132
 
 
133
    // block our handler to prevent recursive calls.
 
134
    blockOnCurrentIndexChanged_ = true;
 
135
    // restore to previously selected item
 
136
    setCurrentIndex(prevIndex_);
 
137
    blockOnCurrentIndexChanged_ = false;
 
138
  }
 
139
  else {
 
140
    prevIndex_ = index;
 
141
  }
 
142
}
 
143
 
 
144
 
 
145
#if 0
 
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() {
 
149
 
 
150
}
 
151
#endif
 
152
 
 
153
} // namespace Fm