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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
|
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt3Support module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "q3filedialog.h"
#ifndef QT_NO_FILEDIALOG
/*****************************************************************************
Q3FileDialog debug facilities
*****************************************************************************/
//#define DEBUG_FILEDIALOG_FILTERS
#include "qapplication.h"
#include <private/qapplication_p.h>
#include <private/qt_mac_p.h>
#include "qregexp.h"
#include "qbuffer.h"
#include "qstringlist.h"
#include "qtextcodec.h"
#include "qdesktopwidget.h"
#include "qfiledialog.h"
#include <stdlib.h>
QT_BEGIN_NAMESPACE
#ifdef QT_MAC_USE_COCOA
QStringList Q3FileDialog::macGetOpenFileNames(const QString &filter, QString *pwd,
QWidget *parent, const char* /*name*/,
const QString& caption, QString *selectedFilter,
bool /*multi*/, bool /*directory*/)
{
return QFileDialog::getOpenFileNames(filter, *pwd, parent, 0,
caption);
}
QString Q3FileDialog::macGetSaveFileName(const QString &start, const QString &filter,
QString *, QWidget *parent, const char* /*name*/,
const QString& caption, QString *selectedFilter)
{
return QFileDialog::getSaveFileName(start, filter, parent, 0,
caption, selectedFilter);
}
#else
/*****************************************************************************
Externals
*****************************************************************************/
extern WindowPtr qt_mac_window_for(const QWidget*); //qwidget_mac.cpp
extern const char qt3_file_dialog_filter_reg_exp[]; //qfiledialog.cpp
/*****************************************************************************
Q3FileDialog utility functions
*****************************************************************************/
static UInt8 *str_buffer = NULL;
static void cleanup_str_buffer()
{
if(str_buffer) {
free(str_buffer);
str_buffer = NULL;
}
}
// Returns the wildcard part of a filter.
struct qt_mac_filter_name {
QString description, regxp, filter;
};
static qt_mac_filter_name *extractFilter(const QString& rawFilter)
{
qt_mac_filter_name *ret = new qt_mac_filter_name;
ret->filter = rawFilter;
QString result = rawFilter;
QRegExp r(QString::fromLatin1(qt3_file_dialog_filter_reg_exp));
int index = r.indexIn(result);
if(index >= 0) {
ret->description = r.cap(1).trimmed();
result = r.cap(2);
}
if(ret->description.isEmpty())
ret->description = result;
ret->regxp = result.replace(QLatin1Char(' '), QLatin1Char(';'));
return ret;
}
// Makes a list of filters from ;;-separated text.
static QList<qt_mac_filter_name*> makeFiltersList(const QString &filter)
{
#ifdef DEBUG_FILEDIALOG_FILTERS
qDebug("Q3FileDialog:%d - Got filter (%s)", __LINE__, filter.latin1());
#endif
QString f(filter);
if(f.isEmpty())
f = Q3FileDialog::tr("All Files (*)");
if(f.isEmpty())
return QList<qt_mac_filter_name*>();
QString sep(QLatin1String(";;"));
int i = f.indexOf(sep, 0);
if(i == -1) {
sep = QLatin1String("\n");
if(f.indexOf(sep, 0) != -1)
i = f.indexOf(sep, 0);
}
QList<qt_mac_filter_name*> ret;
QStringList filts = f.split(sep);
for (QStringList::Iterator it = filts.begin(); it != filts.end(); ++it) {
qt_mac_filter_name *filter = extractFilter((*it));
#ifdef DEBUG_FILEDIALOG_FILTERS
qDebug("Q3FileDialog:%d Split out filter (%d) '%s' '%s'", __LINE__, ret.count(),
filter->regxp.latin1(), filter->description.latin1());
#endif
ret.append(filter);
}
return ret;
}
struct qt_mac_nav_filter_type {
int index;
QList<qt_mac_filter_name*> *filts;
};
static Boolean qt_mac_nav_filter(AEDesc *theItem, void *info,
void *myd, NavFilterModes)
{
qt_mac_nav_filter_type *t = (qt_mac_nav_filter_type *)myd;
if(!t || !t->filts || t->index >= t->filts->count())
return true;
NavFileOrFolderInfo *theInfo = (NavFileOrFolderInfo *)info;
QString file;
qt_mac_filter_name *fn = t->filts->at(t->index);
if(!fn)
return true;
if(theItem->descriptorType == typeFSRef) {
FSRef ref;
AEGetDescData(theItem, &ref, sizeof(ref));
if(!str_buffer) {
qAddPostRoutine(cleanup_str_buffer);
str_buffer = (UInt8 *)malloc(1024);
}
FSRefMakePath(&ref, str_buffer, 1024);
file = QString::fromUtf8((const char *)str_buffer);
int slsh = file.lastIndexOf(QLatin1Char('/'));
if(slsh != -1)
file = file.right(file.length() - slsh - 1);
}
QStringList reg = fn->regxp.split(QLatin1String(";"));
for(QStringList::Iterator it = reg.begin(); it != reg.end(); ++it) {
QRegExp rg((*it), false, true);
#ifdef DEBUG_FILEDIALOG_FILTERS
qDebug("Q3FileDialog:%d, asked to filter.. %s (%s)", __LINE__,
file.latin1(), (*it).latin1());
#endif
if(rg.exactMatch(file))
return true;
}
return (theInfo->isFolder && !file.endsWith(QLatin1String(".app")));
}
//filter UPP stuff
static NavObjectFilterUPP mac_navFilterUPP = NULL;
static void cleanup_navFilterUPP()
{
DisposeNavObjectFilterUPP(mac_navFilterUPP);
mac_navFilterUPP = NULL;
}
static const NavObjectFilterUPP make_navFilterUPP()
{
if(mac_navFilterUPP)
return mac_navFilterUPP;
qAddPostRoutine(cleanup_navFilterUPP);
return mac_navFilterUPP = NewNavObjectFilterUPP(qt_mac_nav_filter);
}
//event UPP stuff
static NavEventUPP mac_navProcUPP = NULL;
static void cleanup_navProcUPP()
{
DisposeNavEventUPP(mac_navProcUPP);
mac_navProcUPP = NULL;
}
static bool g_nav_blocking=true;
static void qt_mac_filedialog_event_proc(const NavEventCallbackMessage msg,
NavCBRecPtr p, NavCallBackUserData myd)
{
switch(msg) {
case kNavCBPopupMenuSelect: {
qt_mac_nav_filter_type *t = (qt_mac_nav_filter_type *)myd;
NavMenuItemSpec *s = (NavMenuItemSpec*)p->eventData.eventDataParms.param;
t->index = s->menuType;
#ifdef DEBUG_FILEDIALOG_FILTERS
qDebug("Q3FileDialog:%d - Selected a filter: %ld", __LINE__, s->menuType);
#endif
break; }
case kNavCBStart:
g_nav_blocking=true;
break;
case kNavCBUserAction:
g_nav_blocking=false;
break;
}
}
static const NavEventUPP make_navProcUPP()
{
if(mac_navProcUPP)
return mac_navProcUPP;
qAddPostRoutine(cleanup_navProcUPP);
return mac_navProcUPP = NewNavEventUPP(qt_mac_filedialog_event_proc);
}
extern OSErr qt_mac_create_fsref(const QString &, FSRef *); //qglobal.cpp
QStringList Q3FileDialog::macGetOpenFileNames(const QString &filter, QString *pwd,
QWidget *parent, const char* /*name*/,
const QString& caption, QString *selectedFilter,
bool multi, bool directory)
{
OSErr err;
QStringList retstrl;
NavDialogCreationOptions options;
NavGetDefaultDialogCreationOptions(&options);
options.modality = kWindowModalityAppModal;
options.optionFlags |= kNavDontConfirmReplacement | kNavSupportPackages;
if (!multi)
options.optionFlags &= ~kNavAllowMultipleFiles;
if(!caption.isEmpty())
options.windowTitle = CFStringCreateWithCharacters(NULL, (UniChar *)caption.unicode(),
caption.length());
static const int w = 450, h = 350;
options.location.h = options.location.v = -1;
if(parent && parent->isVisible()) {
Qt::WindowType wt = parent->window()->windowType();
if (wt != Qt::Desktop && wt != Qt::Sheet && wt != Qt::Drawer) {
options.modality = kWindowModalityWindowModal;
options.parentWindow = qt_mac_window_for(parent);
} else {
parent = parent->window();
QString s = parent->windowTitle();
options.clientName = CFStringCreateWithCharacters(NULL, (UniChar *)s.unicode(), s.length());
options.location.h = (parent->x() + (parent->width() / 2)) - (w / 2);
options.location.v = (parent->y() + (parent->height() / 2)) - (h / 2);
QRect r = QApplication::desktop()->screenGeometry(
QApplication::desktop()->screenNumber(parent));
if(options.location.h + w > r.right())
options.location.h -= (options.location.h + w) - r.right() + 10;
if(options.location.v + h > r.bottom())
options.location.v -= (options.location.v + h) - r.bottom() + 10;
}
} else if(QWidget *p = qApp->mainWidget()) {
static int last_screen = -1;
int scr = QApplication::desktop()->screenNumber(p);
if(last_screen != scr) {
QRect r = QApplication::desktop()->screenGeometry(scr);
options.location.h = (r.x() + (r.width() / 2)) - (w / 2);
options.location.v = (r.y() + (r.height() / 2)) - (h / 2);
}
}
QList<qt_mac_filter_name*> filts = makeFiltersList(filter);
qt_mac_nav_filter_type t;
t.index = 0;
t.filts = &filts;
if(filts.count() > 1) {
int i = 0;
CFStringRef *arr = (CFStringRef *)malloc(sizeof(CFStringRef) * filts.count());
for (QList<qt_mac_filter_name*>::Iterator it = filts.begin(); it != filts.end(); ++it) {
QString rg = (*it)->description;
arr[i++] = CFStringCreateWithCharacters(NULL, (UniChar *)rg.unicode(), rg.length());
}
options.popupExtension = CFArrayCreate(NULL, (const void **)arr, filts.count(), NULL);
}
NavDialogRef dlg;
if(directory) {
if(NavCreateChooseFolderDialog(&options, make_navProcUPP(), NULL, NULL, &dlg)) {
qDebug("Shouldn't happen %s:%d", __FILE__, __LINE__);
return retstrl;
}
} else {
if(NavCreateGetFileDialog(&options, NULL, make_navProcUPP(), NULL,
make_navFilterUPP(), (void *) (filts.isEmpty() ? NULL : &t),
&dlg)) {
qDebug("Shouldn't happen %s:%d", __FILE__, __LINE__);
return retstrl;
}
}
if(pwd && !pwd->isEmpty()) {
FSRef fsref;
if(qt_mac_create_fsref(*pwd, &fsref) == noErr) {
AEDesc desc;
if(AECreateDesc(typeFSRef, &fsref, sizeof(FSRef), &desc) == noErr)
NavCustomControl(dlg, kNavCtlSetLocation, (void*)&desc);
}
}
NavDialogRun(dlg);
if (selectedFilter) {
NavMenuItemSpec navSpec;
bzero(&navSpec, sizeof(NavMenuItemSpec));
qt_mac_filter_name *sel_filt_name = makeFiltersList(*selectedFilter).at(0);
for (int i = 0; i < filts.count(); ++i) {
const qt_mac_filter_name *filter = filts.at(i);
if (sel_filt_name->description == filter->description
&& sel_filt_name->regxp == filter->regxp
&& sel_filt_name->filter == filter->filter) {
navSpec.menuType = i;
break;
}
}
NavCustomControl(dlg, kNavCtlSelectCustomType, &navSpec);
}
if(options.modality == kWindowModalityWindowModal) { //simulate modality
QWidget modal_widg(parent, __FILE__ "__modal_dlg",
Qt::WType_TopLevel | Qt::WStyle_Customize | Qt::WStyle_DialogBorder);
modal_widg.createWinId();
QApplicationPrivate::enterModal(&modal_widg);
while(g_nav_blocking)
qApp->processEvents(QEventLoop::WaitForMoreEvents);
QApplicationPrivate::leaveModal(&modal_widg);
}
if(!(NavDialogGetUserAction(dlg) &
(kNavUserActionOpen | kNavUserActionChoose | kNavUserActionNewFolder))) {
NavDialogDispose(dlg);
return retstrl;
}
NavReplyRecord ret;
NavDialogGetReply(dlg, &ret);
NavDialogDispose(dlg);
long count;
err = AECountItems(&(ret.selection), &count);
if(!ret.validRecord || err != noErr || !count) {
NavDisposeReply(&ret);
return retstrl;
}
for(long index = 1; index <= count; index++) {
FSRef ref;
err = AEGetNthPtr(&(ret.selection), index, typeFSRef, 0, 0, &ref, sizeof(ref), 0);
if(err != noErr)
break;
if(!str_buffer) {
qAddPostRoutine(cleanup_str_buffer);
str_buffer = (UInt8 *)malloc(1024);
}
FSRefMakePath(&ref, str_buffer, 1024);
retstrl.append(QString::fromUtf8((const char *)str_buffer));
}
NavDisposeReply(&ret);
if(selectedFilter)
*selectedFilter = filts.at(t.index)->filter;
while (!filts.isEmpty())
delete filts.takeFirst();
return retstrl;
}
// Copious copy and paste from qfiledialog.cpp. Fix in 4.0.
static QString encodeFileName(const QString &fName)
{
QString newStr;
QByteArray cName = fName.utf8();
const QByteArray sChars("<>#@\"&%$:,;?={}|^~[]\'`\\*");
int len = cName.length();
if (!len)
return QString();
for (int i = 0; i < len ;++i) {
uchar inCh = (uchar)cName[i];
if (inCh >= 128 || sChars.contains(inCh))
{
newStr += QLatin1Char('%');
ushort c = inCh / 16;
c += c > 9 ? 'A' - 10 : '0';
newStr += QLatin1Char((char)c);
c = inCh % 16;
c += c > 9 ? 'A' - 10 : '0';
newStr += QLatin1Char((char)c);
} else {
newStr += QLatin1Char((char)inCh);
}
}
return newStr;
}
QString Q3FileDialog::macGetSaveFileName(const QString &start, const QString &filter,
QString *, QWidget *parent, const char* /*name*/,
const QString& caption, QString *selectedFilter)
{
OSErr err;
QString retstr;
NavDialogCreationOptions options;
NavGetDefaultDialogCreationOptions(&options);
static const int w = 450, h = 350;
options.optionFlags |= kNavDontConfirmReplacement;
options.modality = kWindowModalityAppModal;
options.location.h = options.location.v = -1;
QString workingDir;
QString initialSelection;
if (!start.isEmpty()) {
Q3UrlOperator u(encodeFileName(start));
if (u.isLocalFile() && QFileInfo(u.path()).isDir()) {
workingDir = start;
} else {
if (u.isLocalFile()) {
QFileInfo fi(u.dirPath());
if (fi.exists()) {
workingDir = u.dirPath();
initialSelection = u.fileName();
}
} else {
workingDir = u.toString();
}
}
if (!initialSelection.isEmpty())
options.saveFileName = CFStringCreateWithCharacters(0,
(UniChar *)initialSelection.unicode(),
initialSelection.length());
}
if(!caption.isEmpty())
options.windowTitle = CFStringCreateWithCharacters(NULL, (UniChar *)caption.unicode(),
caption.length());
if(parent && parent->isVisible()) {
Qt::WindowType wt = parent->window()->windowType();
if (wt != Qt::Desktop && wt != Qt::Sheet && wt != Qt::Drawer) {
options.modality = kWindowModalityWindowModal;
options.parentWindow = qt_mac_window_for(parent);
} else {
parent = parent->window();
QString s = parent->windowTitle();
options.clientName = CFStringCreateWithCharacters(NULL, (UniChar *)s.unicode(), s.length());
options.location.h = (parent->x() + (parent->width() / 2)) - (w / 2);
options.location.v = (parent->y() + (parent->height() / 2)) - (h / 2);
QRect r = QApplication::desktop()->screenGeometry(
QApplication::desktop()->screenNumber(parent));
if(options.location.h + w > r.right())
options.location.h -= (options.location.h + w) - r.right() + 10;
if(options.location.v + h > r.bottom())
options.location.v -= (options.location.v + h) - r.bottom() + 10;
}
} else if(QWidget *p = qApp->mainWidget()) {
static int last_screen = -1;
int scr = QApplication::desktop()->screenNumber(p);
if(last_screen != scr) {
QRect r = QApplication::desktop()->screenGeometry(scr);
options.location.h = (r.x() + (r.width() / 2)) - (w / 2);
options.location.v = (r.y() + (r.height() / 2)) - (h / 2);
}
}
QList<qt_mac_filter_name*> filts = makeFiltersList(filter);
qt_mac_nav_filter_type t;
t.index = 0;
t.filts = &filts;
if(filts.count() > 1) {
int i = 0;
CFStringRef *arr = (CFStringRef *)malloc(sizeof(CFStringRef) * filts.count());
for (QList<qt_mac_filter_name*>::Iterator it = filts.begin(); it != filts.end(); ++it) {
QString rg = (*it)->description;
arr[i++] = CFStringCreateWithCharacters(NULL, (UniChar *)rg.unicode(), rg.length());
}
options.popupExtension = CFArrayCreate(NULL, (const void **)arr, filts.count(), NULL);
}
NavDialogRef dlg;
if(NavCreatePutFileDialog(&options, 'cute', kNavGenericSignature, make_navProcUPP(),
(void *) (filts.isEmpty() ? NULL : &t), &dlg)) {
qDebug("Shouldn't happen %s:%d", __FILE__, __LINE__);
return retstr;
}
if (!workingDir.isEmpty()) {
FSRef fsref;
if (qt_mac_create_fsref(workingDir, &fsref) == noErr) {
AEDesc desc;
if (AECreateDesc(typeFSRef, &fsref, sizeof(FSRef), &desc) == noErr)
NavCustomControl(dlg, kNavCtlSetLocation, (void*)&desc);
}
}
NavDialogRun(dlg);
if (selectedFilter) {
NavMenuItemSpec navSpec;
bzero(&navSpec, sizeof(NavMenuItemSpec));
qt_mac_filter_name *sel_filt_name = makeFiltersList(*selectedFilter).at(0);
for (int i = 0; i < filts.count(); ++i) {
const qt_mac_filter_name *filter = filts.at(i);
if (sel_filt_name->description == filter->description
&& sel_filt_name->regxp == filter->regxp
&& sel_filt_name->filter == filter->filter) {
navSpec.menuType = i;
break;
}
}
NavCustomControl(dlg, kNavCtlSelectCustomType, &navSpec);
}
if(options.modality == kWindowModalityWindowModal) { //simulate modality
QWidget modal_widg(parent, __FILE__ "__modal_dlg",
Qt::WType_TopLevel | Qt::WStyle_Customize | Qt::WStyle_DialogBorder);
modal_widg.createWinId();
QApplicationPrivate::enterModal(&modal_widg);
while(g_nav_blocking)
qApp->processEvents(QEventLoop::WaitForMoreEvents);
QApplicationPrivate::leaveModal(&modal_widg);
}
if(NavDialogGetUserAction(dlg) != kNavUserActionSaveAs) {
NavDialogDispose(dlg);
return retstr;
}
NavReplyRecord ret;
NavDialogGetReply(dlg, &ret);
NavDialogDispose(dlg);
long count;
err = AECountItems(&(ret.selection), &count);
if(!ret.validRecord || err != noErr || !count) {
NavDisposeReply(&ret);
return retstr;
}
AEKeyword keyword;
DescType type;
Size size;
FSRef ref;
err = AEGetNthPtr(&(ret.selection), 1, typeFSRef, &keyword,
&type, &ref, sizeof(ref), &size);
if(err == noErr) {
if(!str_buffer) {
qAddPostRoutine(cleanup_str_buffer);
str_buffer = (UInt8 *)malloc(1024);
}
FSRefMakePath(&ref, str_buffer, 1024);
retstr = QString::fromUtf8((const char *)str_buffer);
//now filename
CFStringGetCString(ret.saveFileName, (char *)str_buffer, 1024, kCFStringEncodingUTF8);
retstr += QLatin1Char('/') + QString::fromUtf8((const char *)str_buffer);
}
NavDisposeReply(&ret);
if(selectedFilter)
*selectedFilter = filts.at(t.index)->filter;
while (!filts.isEmpty())
delete filts.takeFirst();
return retstr;
}
#endif // QT_MAC_USE_COCOA
QT_END_NAMESPACE
#endif
|