Qt 4.8
Classes | Enumerations | Functions | Variables
qsettings_mac.cpp File Reference
#include "qsettings.h"
#include "qsettings_p.h"
#include "qdatetime.h"
#include "qdir.h"
#include "qvarlengtharray.h"
#include "private/qcore_mac_p.h"
#include "qcoreapplication.h"

Go to the source code of this file.

Classes

class  QMacSettingsPrivate
 
struct  QMacSettingsPrivate::SearchDomain
 

Enumerations

enum  RotateShift { Macify = 1, Qtify = 2 }
 

Functions

static QString comify (const QString &organization)
 
static QCFType< CFStringRefmacKey (const QString &key)
 
static CFArrayRef macList (const QList< QVariant > &list)
 
static QCFType< CFPropertyListRef > macValue (const QVariant &value)
 
static QString qtKey (CFStringRef cfkey)
 
static QVariant qtValue (CFPropertyListRef cfvalue)
 
static QString rotateSlashesDotsAndMiddots (const QString &key, int shift)
 
static QCFType< CFURLRef > urlFromFileName (const QString &fileName)
 

Variables

static const CFStringRef hostNames [2] = { kCFPreferencesCurrentHost, kCFPreferencesAnyHost }
 
static const int numHostNames = 2
 

Enumeration Type Documentation

◆ RotateShift

Enumerator
Macify 
Qtify 

Definition at line 69 of file qsettings_mac.cpp.

69 { Macify = 1, Qtify = 2 };

Function Documentation

◆ comify()

static QString comify ( const QString organization)
static

Definition at line 304 of file qsettings_mac.cpp.

Referenced by QMacSettingsPrivate::QMacSettingsPrivate().

305 {
306  for (int i = organization.size() - 1; i >= 0; --i) {
307  QChar ch = organization.at(i);
308  if (ch == QLatin1Char('.') || ch == QChar(0x3002) || ch == QChar(0xff0e)
309  || ch == QChar(0xff61)) {
310  QString suffix = organization.mid(i + 1).toLower();
311  if (suffix.size() == 2 || suffix == QLatin1String("com")
312  || suffix == QLatin1String("org") || suffix == QLatin1String("net")
313  || suffix == QLatin1String("edu") || suffix == QLatin1String("gov")
314  || suffix == QLatin1String("mil") || suffix == QLatin1String("biz")
315  || suffix == QLatin1String("info") || suffix == QLatin1String("name")
316  || suffix == QLatin1String("pro") || suffix == QLatin1String("aero")
317  || suffix == QLatin1String("coop") || suffix == QLatin1String("museum")) {
318  QString result = organization;
319  result.replace(QLatin1Char('/'), QLatin1Char(' '));
320  return result;
321  }
322  break;
323  }
324  int uc = ch.unicode();
325  if ((uc < 'a' || uc > 'z') && (uc < 'A' || uc > 'Z'))
326  break;
327  }
328 
329  QString domain;
330  for (int i = 0; i < organization.size(); ++i) {
331  QChar ch = organization.at(i);
332  int uc = ch.unicode();
333  if ((uc >= 'a' && uc <= 'z') || (uc >= '0' && uc <= '9')) {
334  domain += ch;
335  } else if (uc >= 'A' && uc <= 'Z') {
336  domain += ch.toLower();
337  } else {
338  domain += QLatin1Char(' ');
339  }
340  }
341  domain = domain.simplified();
342  domain.replace(QLatin1Char(' '), QLatin1Char('-'));
343  if (!domain.isEmpty())
344  domain.append(QLatin1String(".com"));
345  return domain;
346 }
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
QString & append(QChar c)
Definition: qstring.cpp:1777
QString simplified() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end, and that has each sequence o...
Definition: qstring.cpp:4415
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
QByteArray suffix
QChar toLower() const
Returns the lowercase equivalent if the character is uppercase or titlecase; otherwise returns the ch...
Definition: qchar.cpp:1239
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ macKey()

static QCFType<CFStringRef> macKey ( const QString key)
static

Definition at line 88 of file qsettings_mac.cpp.

Referenced by QMacSettingsPrivate::get(), QMacSettingsPrivate::remove(), QMacSettingsPrivate::set(), and QConfFileSettingsPrivate::writePlistFile().

89 {
91 }
static CFStringRef toCFStringRef(const QString &str)
Definition: qcore_mac.cpp:69
static QString rotateSlashesDotsAndMiddots(const QString &key, int shift)

◆ macList()

static CFArrayRef macList ( const QList< QVariant > &  list)
static

Definition at line 100 of file qsettings_mac.cpp.

Referenced by macValue().

101 {
102  int n = list.size();
104  for (int i = 0; i < n; ++i)
105  cfvalues[i] = macValue(list.at(i));
106  return CFArrayCreate(kCFAllocatorDefault, reinterpret_cast<const void **>(cfvalues.data()),
107  CFIndex(n), &kCFTypeArrayCallBacks);
108 }
static QCFType< CFPropertyListRef > macValue(const QVariant &value)
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
int size() const
Returns the number of items in the list.
Definition: qlist.h:137

◆ macValue()

static QCFType< CFPropertyListRef > macValue ( const QVariant value)
static

Definition at line 110 of file qsettings_mac.cpp.

Referenced by macList(), qtKey(), QMacSettingsPrivate::set(), and QConfFileSettingsPrivate::writePlistFile().

111 {
112  CFPropertyListRef result = 0;
113 
114  switch (value.type()) {
115  case QVariant::ByteArray:
116  {
117  QByteArray ba = value.toByteArray();
118  result = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(ba.data()),
119  CFIndex(ba.size()));
120  }
121  break;
122  // should be same as below (look for LIST)
123  case QVariant::List:
125  case QVariant::Polygon:
126  result = macList(value.toList());
127  break;
128  case QVariant::Map:
129  {
130  /*
131  QMap<QString, QVariant> is potentially a multimap,
132  whereas CFDictionary is a single-valued map. To allow
133  for multiple values with the same key, we store
134  multiple values in a CFArray. To avoid ambiguities,
135  we also wrap lists in a CFArray singleton.
136  */
139 
140  int maxUniqueKeys = map.size();
141  int numUniqueKeys = 0;
142  QVarLengthArray<QCFType<CFPropertyListRef> > cfkeys(maxUniqueKeys);
143  QVarLengthArray<QCFType<CFPropertyListRef> > cfvalues(maxUniqueKeys);
144 
145  while (i != map.constEnd()) {
146  const QString &key = i.key();
148 
149  do {
150  values << i.value();
151  ++i;
152  } while (i != map.constEnd() && i.key() == key);
153 
154  bool singleton = (values.count() == 1);
155  if (singleton) {
156  switch (values.first().type()) {
157  // should be same as above (look for LIST)
158  case QVariant::List:
160  case QVariant::Polygon:
161  singleton = false;
162  default:
163  ;
164  }
165  }
166 
167  cfkeys[numUniqueKeys] = QCFString::toCFStringRef(key);
168  cfvalues[numUniqueKeys] = singleton ? macValue(values.first()) : macList(values);
169  ++numUniqueKeys;
170  }
171 
172  result = CFDictionaryCreate(kCFAllocatorDefault,
173  reinterpret_cast<const void **>(cfkeys.data()),
174  reinterpret_cast<const void **>(cfvalues.data()),
175  CFIndex(numUniqueKeys),
176  &kCFTypeDictionaryKeyCallBacks,
177  &kCFTypeDictionaryValueCallBacks);
178  }
179  break;
180  case QVariant::DateTime:
181  {
182  /*
183  CFDate, unlike QDateTime, doesn't store timezone information.
184  */
185  QDateTime dt = value.toDateTime();
186  if (dt.timeSpec() == Qt::LocalTime) {
187  QDateTime reference;
188  reference.setTime_t((uint)kCFAbsoluteTimeIntervalSince1970);
189  result = CFDateCreate(kCFAllocatorDefault, CFAbsoluteTime(reference.secsTo(dt)));
190  } else {
191  goto string_case;
192  }
193  }
194  break;
195  case QVariant::Bool:
196  result = value.toBool() ? kCFBooleanTrue : kCFBooleanFalse;
197  break;
198  case QVariant::Int:
199  case QVariant::UInt:
200  {
201  int n = value.toInt();
202  result = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &n);
203  }
204  break;
205  case QVariant::Double:
206  {
207  double n = value.toDouble();
208  result = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &n);
209  }
210  break;
211  case QVariant::LongLong:
212  case QVariant::ULongLong:
213  {
214  qint64 n = value.toLongLong();
215  result = CFNumberCreate(0, kCFNumberLongLongType, &n);
216  }
217  break;
218  case QVariant::String:
219  string_case:
220  default:
222  }
223  return result;
224 }
const Key key(const T &value) const
Returns the first key with value value.
Definition: qmap.h:844
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QMap< QString, QVariant > toMap() const
Returns the variant as a QMap<QString, QVariant> if the variant has type() Map ; otherwise returns an...
Definition: qvariant.cpp:2281
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static CFStringRef toCFStringRef(const QString &str)
Definition: qcore_mac.cpp:69
int size() const
Returns the number of (key, value) pairs in the map.
Definition: qmap.h:201
QDateTime toDateTime() const
Returns the variant as a QDateTime if the variant has type() DateTime , Date , or String ; otherwise ...
Definition: qvariant.cpp:2349
QList< QVariant > toList() const
Returns the variant as a QVariantList if the variant has type() List or StringList ; otherwise return...
Definition: qvariant.cpp:2751
bool toBool() const
Returns the variant as a bool if the variant has type() Bool.
Definition: qvariant.cpp:2691
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
QByteArray toByteArray() const
Returns the variant as a QByteArray if the variant has type() ByteArray or String (converted using QS...
Definition: qvariant.cpp:2383
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
qlonglong toLongLong(bool *ok=0) const
Returns the variant as a long long int if the variant has type() LongLong , Bool , ByteArray , Char , Double , Int , String , UInt , or ULongLong ; otherwise returns 0.
Definition: qvariant.cpp:2659
static QCFType< CFPropertyListRef > macValue(const QVariant &value)
void setTime_t(uint secsSince1Jan1970UTC)
Sets the date and time given the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).
Definition: qdatetime.cpp:2566
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
int secsTo(const QDateTime &) const
Returns the number of seconds from this datetime to the other datetime.
Definition: qdatetime.cpp:2914
unsigned int uint
Definition: qglobal.h:996
__int64 qint64
Definition: qglobal.h:942
static QString variantToString(const QVariant &v)
Definition: qsettings.cpp:464
quint16 values[128]
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
int key
Qt::TimeSpec timeSpec() const
Returns the time specification of the datetime.
Definition: qdatetime.cpp:2379
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
double toDouble(bool *ok=0) const
Returns the variant as a double if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2710
static CFArrayRef macList(const QList< QVariant > &list)

◆ qtKey()

static QString qtKey ( CFStringRef  cfkey)
static

◆ qtValue()

static QVariant qtValue ( CFPropertyListRef  cfvalue)
static

Definition at line 226 of file qsettings_mac.cpp.

Referenced by QMacSettingsPrivate::get(), and QConfFileSettingsPrivate::readPlistFile().

227 {
228  if (!cfvalue)
229  return QVariant();
230 
231  CFTypeID typeId = CFGetTypeID(cfvalue);
232 
233  /*
234  Sorted grossly from most to least frequent type.
235  */
236  if (typeId == CFStringGetTypeID()) {
237  return QSettingsPrivate::stringToVariant(QCFString::toQString(static_cast<CFStringRef>(cfvalue)));
238  } else if (typeId == CFNumberGetTypeID()) {
239  CFNumberRef cfnumber = static_cast<CFNumberRef>(cfvalue);
240  if (CFNumberIsFloatType(cfnumber)) {
241  double d;
242  CFNumberGetValue(cfnumber, kCFNumberDoubleType, &d);
243  return d;
244  } else {
245  int i;
246  qint64 ll;
247 
248  if (CFNumberGetValue(cfnumber, kCFNumberIntType, &i))
249  return i;
250  CFNumberGetValue(cfnumber, kCFNumberLongLongType, &ll);
251  return ll;
252  }
253  } else if (typeId == CFArrayGetTypeID()) {
254  CFArrayRef cfarray = static_cast<CFArrayRef>(cfvalue);
255  QList<QVariant> list;
256  CFIndex size = CFArrayGetCount(cfarray);
257  bool metNonString = false;
258  for (CFIndex i = 0; i < size; ++i) {
259  QVariant value = qtValue(CFArrayGetValueAtIndex(cfarray, i));
260  if (value.type() != QVariant::String)
261  metNonString = true;
262  list << value;
263  }
264  if (metNonString)
265  return list;
266  else
267  return QVariant(list).toStringList();
268  } else if (typeId == CFBooleanGetTypeID()) {
269  return (bool)CFBooleanGetValue(static_cast<CFBooleanRef>(cfvalue));
270  } else if (typeId == CFDataGetTypeID()) {
271  CFDataRef cfdata = static_cast<CFDataRef>(cfvalue);
272  return QByteArray(reinterpret_cast<const char *>(CFDataGetBytePtr(cfdata)),
273  CFDataGetLength(cfdata));
274  } else if (typeId == CFDictionaryGetTypeID()) {
275  CFDictionaryRef cfdict = static_cast<CFDictionaryRef>(cfvalue);
276  CFTypeID arrayTypeId = CFArrayGetTypeID();
277  int size = (int)CFDictionaryGetCount(cfdict);
280  CFDictionaryGetKeysAndValues(cfdict, keys.data(), values.data());
281 
283  for (int i = 0; i < size; ++i) {
284  QString key = QCFString::toQString(static_cast<CFStringRef>(keys[i]));
285 
286  if (CFGetTypeID(values[i]) == arrayTypeId) {
287  CFArrayRef cfarray = static_cast<CFArrayRef>(values[i]);
288  CFIndex arraySize = CFArrayGetCount(cfarray);
289  for (CFIndex j = arraySize - 1; j >= 0; --j)
290  map.insert(key, qtValue(CFArrayGetValueAtIndex(cfarray, j)));
291  } else {
292  map.insert(key, qtValue(values[i]));
293  }
294  }
295  return map;
296  } else if (typeId == CFDateGetTypeID()) {
297  QDateTime dt;
298  dt.setTime_t((uint)kCFAbsoluteTimeIntervalSince1970);
299  return dt.addSecs((int)CFDateGetAbsoluteTime(static_cast<CFDateRef>(cfvalue)));
300  }
301  return QVariant();
302 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QMultiMap class is a convenience QMap subclass that provides multi-valued maps.
Definition: qcontainerfwd.h:59
QDateTime addSecs(int secs) const
Returns a QDateTime object containing a datetime s seconds later than the datetime of this object (or...
Definition: qdatetime.cpp:2869
double d
Definition: qnumeric_p.h:62
static QVariant qtValue(CFPropertyListRef cfvalue)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
The QString class provides a Unicode character string.
Definition: qstring.h:83
QStringList keys
QStringList toStringList() const
Returns the variant as a QStringList if the variant has type() StringList, String ...
Definition: qvariant.cpp:2259
QFuture< void > map(Sequence &sequence, MapFunction function)
void setTime_t(uint secsSince1Jan1970UTC)
Sets the date and time given the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).
Definition: qdatetime.cpp:2566
unsigned int uint
Definition: qglobal.h:996
__int64 qint64
Definition: qglobal.h:942
quint16 values[128]
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
int key
static QVariant stringToVariant(const QString &s)
Definition: qsettings.cpp:551
const struct __CFArray * CFArrayRef

◆ rotateSlashesDotsAndMiddots()

static QString rotateSlashesDotsAndMiddots ( const QString key,
int  shift 
)
static

Definition at line 71 of file qsettings_mac.cpp.

Referenced by macKey(), and qtKey().

72 {
73  static const int NumKnights = 3;
74  static const char knightsOfTheRoundTable[NumKnights] = { '/', '.', '\xb7' };
75  QString result = key;
76 
77  for (int i = 0; i < result.size(); ++i) {
78  for (int j = 0; j < NumKnights; ++j) {
79  if (result.at(i) == QLatin1Char(knightsOfTheRoundTable[j])) {
80  result[i] = QLatin1Char(knightsOfTheRoundTable[(j + shift) % NumKnights]).unicode();
81  break;
82  }
83  }
84  }
85  return result;
86 }
static ShiftResult shift(const QBezier *orig, QBezier *shifted, qreal offset, qreal threshold)
Definition: qbezier.cpp:289
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
The QString class provides a Unicode character string.
Definition: qstring.h:83
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
ushort unicode() const
Converts a Latin-1 character to an 16-bit-encoded Unicode representation of the character.
Definition: qchar.h:64
int key
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ urlFromFileName()

static QCFType<CFURLRef> urlFromFileName ( const QString fileName)
static

Definition at line 671 of file qsettings_mac.cpp.

Referenced by QConfFileSettingsPrivate::readPlistFile(), and QConfFileSettingsPrivate::writePlistFile().

672 {
673  return CFURLCreateWithFileSystemPath(kCFAllocatorDefault, QCFString(fileName),
674  kCFURLPOSIXPathStyle, false);
675 }

Variable Documentation

◆ hostNames

const CFStringRef hostNames[2] = { kCFPreferencesCurrentHost, kCFPreferencesAnyHost }
static

◆ numHostNames

const int numHostNames = 2
static