1
/****************************************************************************
2
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the QtCore module of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#include <private/qcore_mac_p.h>
43
#include <new>
44
45
QT_BEGIN_NAMESPACE
46
47
QString QCFString::toQString(CFStringRef str)
48
{
49
    if (!str)
50
        return QString();
51
52
    CFIndex length = CFStringGetLength(str);
53
    if (length == 0)
54
        return QString();
55
56
    QString string(length, Qt::Uninitialized);
57
    CFStringGetCharacters(str, CFRangeMake(0, length), reinterpret_cast<UniChar *>(const_cast<QChar *>(string.unicode())));
58
59
    return string;
60
}
61
62
QCFString::operator QString() const
63
{
64
    if (string.isEmpty() && type)
65
        const_cast<QCFString*>(this)->string = toQString(type);
66
    return string;
67
}
68
69
CFStringRef QCFString::toCFStringRef(const QString &string)
70
{
71
    return CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar *>(string.unicode()),
72
                                        string.length());
73
}
74
75
QCFString::operator CFStringRef() const
76
{
77
    if (!type) {
78
        if (string.d->data != string.d->array)
79
            const_cast<QCFString*>(this)->string.realloc(); // ### Qt5: do we really need this stupid user protection?
80
        const_cast<QCFString*>(this)->type =
81
            CFStringCreateWithCharactersNoCopy(0,
82
                                               reinterpret_cast<const UniChar *>(string.unicode()),
83
                                               string.length(),
84
                                               kCFAllocatorNull);
85
    }
86
    return type;
87
}
88
89
90
#ifndef QT_NO_CORESERVICES
91
void qt_mac_to_pascal_string(const QString &s, Str255 str, TextEncoding encoding, int len)
92
{
93
    if(len == -1)
94
        len = s.length();
95
#if 0
96
    UnicodeMapping mapping;
97
    mapping.unicodeEncoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
98
                                                 kTextEncodingDefaultVariant,
99
                                                 kUnicode16BitFormat);
100
    mapping.otherEncoding = (encoding ? encoding : );
101
    mapping.mappingVersion = kUnicodeUseLatestMapping;
102
103
    UnicodeToTextInfo info;
104
    OSStatus err = CreateUnicodeToTextInfo(&mapping, &info);
105
    if(err != noErr) {
106
        qDebug("Qt: internal: Unable to create pascal string '%s'::%d [%ld]",
107
               s.left(len).latin1(), (int)encoding, err);
108
        return;
109
    }
110
    const int unilen = len * 2;
111
    const UniChar *unibuf = (UniChar *)s.unicode();
112
    ConvertFromUnicodeToPString(info, unilen, unibuf, str);
113
    DisposeUnicodeToTextInfo(&info);
114
#else
115
    Q_UNUSED(encoding);
116
    CFStringGetPascalString(QCFString(s), str, 256, CFStringGetSystemEncoding());
117
#endif
118
}
119
120
QString qt_mac_from_pascal_string(const Str255 pstr)
121
{
122
    return QCFString(CFStringCreateWithPascalString(0, pstr, CFStringGetSystemEncoding()));
123
}
124
125
OSErr qt_mac_create_fsref(const QString &file, FSRef *fsref)
126
{
127
    return FSPathMakeRef(reinterpret_cast<const UInt8 *>(file.toUtf8().constData()), fsref, 0);
128
}
129
130
// Don't use this function, it won't work in 10.5 (Leopard) and up
131
OSErr qt_mac_create_fsspec(const QString &file, FSSpec *spec)
132
{
133
    FSRef fsref;
134
    OSErr ret = qt_mac_create_fsref(file, &fsref);
135
    if (ret == noErr)
136
        ret = FSGetCatalogInfo(&fsref, kFSCatInfoNone, 0, 0, spec, 0);
137
    return ret;
138
}
139
#endif // QT_NO_CORESERVICES
140
141
QT_END_NAMESPACE