~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/plugins/platforms/qnx/qqnxfiledialoghelper.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Research In Motion
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the plugins of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "qqnxfiledialoghelper.h"
 
43
 
 
44
#include "qqnxbpseventfilter.h"
 
45
#include "qqnxscreen.h"
 
46
#include "qqnxintegration.h"
 
47
 
 
48
#include <QDebug>
 
49
#include <QEventLoop>
 
50
#include <QScreen>
 
51
#include <QTimer>
 
52
#include <QWindow>
 
53
 
 
54
#ifdef QQNXFILEDIALOGHELPER_DEBUG
 
55
#define qFileDialogHelperDebug qDebug
 
56
#else
 
57
#define qFileDialogHelperDebug QT_NO_QDEBUG_MACRO
 
58
#endif
 
59
 
 
60
QT_BEGIN_NAMESPACE
 
61
 
 
62
QQnxFileDialogHelper::QQnxFileDialogHelper(const QQnxIntegration *integration)
 
63
    : QPlatformFileDialogHelper(),
 
64
      m_integration(integration),
 
65
      m_dialog(0),
 
66
      m_acceptMode(QFileDialogOptions::AcceptOpen),
 
67
      m_selectedFilter(),
 
68
      m_result(QPlatformDialogHelper::Rejected),
 
69
      m_paths()
 
70
{
 
71
}
 
72
 
 
73
QQnxFileDialogHelper::~QQnxFileDialogHelper()
 
74
{
 
75
    if (m_dialog)
 
76
        dialog_destroy(m_dialog);
 
77
}
 
78
 
 
79
bool QQnxFileDialogHelper::handleEvent(bps_event_t *event)
 
80
{
 
81
    qFileDialogHelperDebug() << Q_FUNC_INFO;
 
82
 
 
83
    // Check dialog event response type (OK vs CANCEL)
 
84
    // CANCEL => index = 0
 
85
    //     OK => index = 1
 
86
    int index = dialog_event_get_selected_index(event);
 
87
    qFileDialogHelperDebug() << "Index =" << index;
 
88
    if (index == 1) {
 
89
        m_result = QPlatformDialogHelper::Accepted;
 
90
 
 
91
        if (m_acceptMode == QFileDialogOptions::AcceptOpen) {
 
92
            // File open dialog
 
93
 
 
94
            // ###TODO Check that this actually gets multiple paths and cleans up properly
 
95
            char **filePaths = 0;
 
96
            int pathCount = 0;
 
97
            int result = dialog_event_get_filebrowse_filepaths(event, &filePaths, &pathCount);
 
98
            if (result != BPS_SUCCESS) {
 
99
                qWarning() << "Could not get paths from native file dialog";
 
100
                return false;
 
101
            }
 
102
 
 
103
            for (int i = 0; i < pathCount; ++i) {
 
104
                QString path = QFile::decodeName(filePaths[i]);
 
105
                m_paths.append(path);
 
106
                qFileDialogHelperDebug() << "path =" << path;
 
107
            }
 
108
 
 
109
            bps_free(filePaths);
 
110
        } else {
 
111
            // File save dialog
 
112
            const char *filePath = dialog_event_get_filesave_filepath(event);
 
113
            QString path = QFile::decodeName(filePath);
 
114
            qFileDialogHelperDebug() << "path =" << path;
 
115
            m_paths.append(path);
 
116
        }
 
117
    } else { // Cancel
 
118
        m_result = QPlatformDialogHelper::Rejected;
 
119
    }
 
120
 
 
121
    emit dialogClosed();
 
122
 
 
123
    return true;
 
124
}
 
125
 
 
126
void QQnxFileDialogHelper::exec()
 
127
{
 
128
    qFileDialogHelperDebug() << Q_FUNC_INFO;
 
129
 
 
130
    // Clear any previous results
 
131
    m_paths.clear();
 
132
 
 
133
    QEventLoop loop;
 
134
    connect(this, SIGNAL(dialogClosed()), &loop, SLOT(quit()));
 
135
    loop.exec();
 
136
 
 
137
    if (m_result == QPlatformDialogHelper::Accepted)
 
138
        emit accept();
 
139
    else
 
140
        emit reject();
 
141
}
 
142
 
 
143
bool QQnxFileDialogHelper::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent)
 
144
{
 
145
    Q_UNUSED(flags);
 
146
    qFileDialogHelperDebug() << Q_FUNC_INFO;
 
147
 
 
148
    QQnxBpsEventFilter *eventFilter = m_integration->bpsEventFilter();
 
149
    // We *really* need the bps event filter ;)
 
150
    if (!eventFilter)
 
151
        return false;
 
152
 
 
153
    // Native dialogs can only handle application modal use cases so far
 
154
    if (modality != Qt::ApplicationModal)
 
155
        return false;
 
156
 
 
157
    // Tear down any existing dialog and start again as dialog mode may have changed
 
158
    if (m_dialog) {
 
159
        dialog_destroy(m_dialog);
 
160
        m_dialog = 0;
 
161
    }
 
162
 
 
163
    // Create dialog
 
164
    const QSharedPointer<QFileDialogOptions> &opts = options();
 
165
    if (opts->acceptMode() == QFileDialogOptions::AcceptOpen) {
 
166
        if (dialog_create_filebrowse(&m_dialog) != BPS_SUCCESS) {
 
167
            qWarning("dialog_create_filebrowse failed");
 
168
            return false;
 
169
        }
 
170
 
 
171
        // Select one or many files?
 
172
        bool multiSelect = (opts->fileMode() == QFileDialogOptions::ExistingFiles);
 
173
        dialog_set_filebrowse_multiselect(m_dialog, multiSelect);
 
174
 
 
175
        // Set the actual list of extensions
 
176
        if (!opts->nameFilters().isEmpty()) {
 
177
            qFileDialogHelperDebug() << "nameFilters =" << opts->nameFilters();
 
178
            setNameFilter(opts->nameFilters().first());
 
179
        } else {
 
180
            QString defaultNameFilter = QStringLiteral("*.*");
 
181
            setNameFilter(defaultNameFilter);
 
182
        }
 
183
    } else {
 
184
        if (dialog_create_filesave(&m_dialog) != BPS_SUCCESS) {
 
185
            qWarning("dialog_create_filesave failed");
 
186
            return false;
 
187
        }
 
188
 
 
189
        // Maybe pre-select a filename
 
190
        if (!opts->initiallySelectedFiles().isEmpty()) {
 
191
            QString fileName = opts->initiallySelectedFiles().first();
 
192
            dialog_set_filesave_filename(m_dialog, QFile::encodeName(fileName).constData());
 
193
        }
 
194
 
 
195
        // Add OK and Cancel buttons. We add the buttons in the order "CANCEL" followed by "OK
 
196
        // such that they have indices matching the buttons on the file open dialog which
 
197
        // is automatically populated with buttons.
 
198
        if (dialog_add_button(m_dialog, tr("CANCEL").toLocal8Bit().constData(), true, 0, true) != BPS_SUCCESS) {
 
199
            qWarning("dialog_add_button failed");
 
200
            return false;
 
201
        }
 
202
 
 
203
        if (dialog_add_button(m_dialog, tr("OK").toLocal8Bit().constData(), true, 0, true) != BPS_SUCCESS) {
 
204
            qWarning("dialog_add_button failed");
 
205
            return false;
 
206
        }
 
207
    }
 
208
 
 
209
    // Cache the accept mode so we know which functions to use to get the results back
 
210
    m_acceptMode = opts->acceptMode();
 
211
 
 
212
    // Set the libscreen window group and common properties
 
213
 
 
214
    QQnxScreen *nativeScreen = parent ? static_cast<QQnxScreen *>(parent->screen()->handle()) :
 
215
                                        m_integration->primaryDisplay();
 
216
    Q_ASSERT(nativeScreen);
 
217
    dialog_set_group_id(m_dialog, nativeScreen->windowGroupName());
 
218
    dialog_set_title_text(m_dialog, opts->windowTitle().toLocal8Bit().constData());
 
219
 
 
220
    // Register ourselves for dialog domain events from bps
 
221
    eventFilter->registerForDialogEvents(this);
 
222
 
 
223
    // Show the dialog
 
224
    dialog_show(m_dialog);
 
225
 
 
226
    return true;
 
227
}
 
228
 
 
229
void QQnxFileDialogHelper::hide()
 
230
{
 
231
    qFileDialogHelperDebug() << Q_FUNC_INFO;
 
232
    if (!m_dialog)
 
233
        return;
 
234
    dialog_cancel(m_dialog);
 
235
}
 
236
 
 
237
bool QQnxFileDialogHelper::defaultNameFilterDisables() const
 
238
{
 
239
    qFileDialogHelperDebug() << Q_FUNC_INFO;
 
240
    return false;
 
241
}
 
242
 
 
243
void QQnxFileDialogHelper::setDirectory(const QString &directory)
 
244
{
 
245
    qFileDialogHelperDebug() << Q_FUNC_INFO << "directory =" << directory;
 
246
    // No native API for setting the directory(!). The best we can do is to
 
247
    // set it as the file name but even then only with a file save dialog.
 
248
    if (m_dialog && m_acceptMode == QFileDialogOptions::AcceptSave)
 
249
        dialog_set_filesave_filename(m_dialog, QFile::encodeName(directory).constData());
 
250
}
 
251
 
 
252
QString QQnxFileDialogHelper::directory() const
 
253
{
 
254
    qFileDialogHelperDebug() << Q_FUNC_INFO;
 
255
    return m_paths.first();
 
256
}
 
257
 
 
258
void QQnxFileDialogHelper::selectFile(const QString &fileName)
 
259
{
 
260
    qFileDialogHelperDebug() << Q_FUNC_INFO << "filename =" << fileName;
 
261
    if (m_dialog && m_acceptMode == QFileDialogOptions::AcceptSave)
 
262
        dialog_set_filesave_filename(m_dialog, QFile::encodeName(fileName).constData());
 
263
}
 
264
 
 
265
QStringList QQnxFileDialogHelper::selectedFiles() const
 
266
{
 
267
    qFileDialogHelperDebug() << Q_FUNC_INFO;
 
268
    return m_paths;
 
269
}
 
270
 
 
271
void QQnxFileDialogHelper::setFilter()
 
272
{
 
273
    // No native api to support setting a filter from QDir::Filters
 
274
    qFileDialogHelperDebug() << Q_FUNC_INFO;
 
275
}
 
276
 
 
277
void QQnxFileDialogHelper::selectNameFilter(const QString &filter)
 
278
{
 
279
    qFileDialogHelperDebug() << Q_FUNC_INFO << "filter =" << filter;
 
280
    setNameFilter(filter);
 
281
}
 
282
 
 
283
QString QQnxFileDialogHelper::selectedNameFilter() const
 
284
{
 
285
    // For now there is no way for the user to change the selected filter
 
286
    // so this just reflects what the developer has set programmatically.
 
287
    qFileDialogHelperDebug() << Q_FUNC_INFO;
 
288
    return m_selectedFilter;
 
289
}
 
290
 
 
291
void QQnxFileDialogHelper::setNameFilter(const QString &filter)
 
292
{
 
293
    qFileDialogHelperDebug() << Q_FUNC_INFO << "filter =" << filter;
 
294
 
 
295
    // Extract the globbing expressions
 
296
    QStringList filters = QPlatformFileDialogHelper::cleanFilterList(filter);
 
297
    char **globs = new char*[filters.size()];
 
298
    for (int i = 0; i < filters.size(); ++i) {
 
299
        QByteArray glob = filters.at(i).toLocal8Bit();
 
300
        globs[i] = new char[glob.length()];
 
301
        strcpy(globs[i], glob.constData());
 
302
    }
 
303
 
 
304
    // Set the filters
 
305
    dialog_set_filebrowse_filter(m_dialog, const_cast<const char**>(globs), filters.size());
 
306
    m_selectedFilter = filter;
 
307
 
 
308
    // Cleanup
 
309
    for (int i = 0; i < filters.size(); ++i)
 
310
        delete[] globs[i];
 
311
    delete[] globs;
 
312
}
 
313
 
 
314
QT_END_NAMESPACE