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
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "UBDesktopServices.h"
#include <QtCore>
#import <Carbon/Carbon.h>
/*
Translates a QDesktopServices::StandardLocation into the mac equivalent.
*/
OSType translateLocation(QDesktopServices::StandardLocation type)
{
switch (type) {
case QDesktopServices::DesktopLocation:
return kDesktopFolderType; break;
case QDesktopServices::DocumentsLocation:
return kDocumentsFolderType; break;
case QDesktopServices::FontsLocation:
// There are at least two different font directories on the mac: /Library/Fonts and ~/Library/Fonts.
// To select a specific one we have to specify a different first parameter when calling FSFindFolder.
return kFontsFolderType; break;
case QDesktopServices::ApplicationsLocation:
return kApplicationsFolderType; break;
case QDesktopServices::MusicLocation:
return kMusicDocumentsFolderType; break;
case QDesktopServices::MoviesLocation:
return kMovieDocumentsFolderType; break;
case QDesktopServices::PicturesLocation:
return kPictureDocumentsFolderType; break;
case QDesktopServices::TempLocation:
return kTemporaryFolderType; break;
case QDesktopServices::DataLocation:
return kApplicationSupportFolderType; break;
case QDesktopServices::CacheLocation:
return kCachedDataFolderType; break;
default:
return kDesktopFolderType; break;
}
}
/*
Constructs a full unicode path from a FSRef.
*/
static QString getFullPath(const FSRef &ref)
{
QByteArray ba(2048, 0);
if (FSRefMakePath(&ref, reinterpret_cast<UInt8 *>(ba.data()), ba.size()) == noErr)
return QString::fromUtf8(ba).normalized(QString::NormalizationForm_C);
return QString();
}
// Mac OS X implementation of QDesktopServices is bugged in Qt 4.4,
// we use the implementation inspired from Qt 4.5 snapshot
QString UBDesktopServices::storageLocation(StandardLocation type)
{
if (QDesktopServices::HomeLocation == type)
return QDir::homePath();
else if (QDesktopServices::TempLocation == type)
return QDir::tempPath();
short domain = kOnAppropriateDisk;
if (QDesktopServices::DataLocation == type || QDesktopServices::CacheLocation == type)
domain = kUserDomain;
// http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/Reference/reference.html
FSRef ref;
OSErr err = FSFindFolder(domain, translateLocation(type), false, &ref);
if (err)
return QString();
QString path = getFullPath(ref);
if (QDesktopServices::DataLocation == type || QDesktopServices::CacheLocation == type)
path += "/Sankore/Sankore 3.1";
return path;
}