/thirdparty/liblastfm2/src/core/misc.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 175 lines · 124 code · 26 blank · 25 comment · 11 complexity · 7b5fae719ef1e50eb6dd5d4d520f6297 MD5 · raw file

  1. /*
  2. Copyright 2009 Last.fm Ltd.
  3. - Primarily authored by Max Howell, Jono Cole and Doug Mansell
  4. This file is part of liblastfm.
  5. liblastfm is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. liblastfm is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with liblastfm. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "misc.h"
  17. #include <QDir>
  18. #ifdef WIN32
  19. #include <shlobj.h>
  20. #endif
  21. #ifdef Q_WS_MAC
  22. #include <Carbon/Carbon.h>
  23. #endif
  24. #ifdef Q_WS_MAC
  25. QDir
  26. lastfm::dir::bundle()
  27. {
  28. // Trolltech provided example
  29. CFURLRef appUrlRef = CFBundleCopyBundleURL( CFBundleGetMainBundle() );
  30. CFStringRef macPath = CFURLCopyFileSystemPath( appUrlRef, kCFURLPOSIXPathStyle );
  31. QString path = CFStringToQString( macPath );
  32. CFRelease(appUrlRef);
  33. CFRelease(macPath);
  34. return QDir( path );
  35. }
  36. #endif
  37. static QDir dataDotDot()
  38. {
  39. #ifdef WIN32
  40. if ((QSysInfo::WindowsVersion & QSysInfo::WV_DOS_based) == 0)
  41. {
  42. // Use this for non-DOS-based Windowses
  43. char path[MAX_PATH];
  44. HRESULT h = SHGetFolderPathA( NULL,
  45. CSIDL_LOCAL_APPDATA | CSIDL_FLAG_CREATE,
  46. NULL,
  47. 0,
  48. path );
  49. if (h == S_OK)
  50. return QString::fromLocal8Bit( path );
  51. }
  52. return QDir::home();
  53. #elif defined(Q_WS_MAC)
  54. return QDir::home().filePath( "Library/Application Support" );
  55. #elif defined(Q_WS_X11)
  56. return QDir::home().filePath( ".local/share" );
  57. #else
  58. return QDir::home();
  59. #endif
  60. }
  61. QDir
  62. lastfm::dir::runtimeData()
  63. {
  64. return dataDotDot().filePath( "Last.fm" );
  65. }
  66. QDir
  67. lastfm::dir::logs()
  68. {
  69. #ifdef Q_WS_MAC
  70. return QDir::home().filePath( "Library/Logs/Last.fm" );
  71. #else
  72. return runtimeData();
  73. #endif
  74. }
  75. QDir
  76. lastfm::dir::cache()
  77. {
  78. #ifdef Q_WS_MAC
  79. return QDir::home().filePath( "Library/Caches/Last.fm" );
  80. #else
  81. return runtimeData().filePath( "cache" );
  82. #endif
  83. }
  84. #ifdef WIN32
  85. QDir
  86. lastfm::dir::programFiles()
  87. {
  88. char path[MAX_PATH];
  89. // TODO: this call is dependant on a specific version of shell32.dll.
  90. // Need to degrade gracefully. Need to bundle SHFolder.exe with installer
  91. // and execute it on install for this to work on Win98.
  92. HRESULT h = SHGetFolderPathA( NULL,
  93. CSIDL_PROGRAM_FILES,
  94. NULL,
  95. 0, // current path
  96. path );
  97. if (h != S_OK)
  98. {
  99. qCritical() << "Couldn't get Program Files dir. Possibly Win9x?";
  100. return QDir();
  101. }
  102. return QString::fromLocal8Bit( path );
  103. }
  104. #endif
  105. #ifdef Q_WS_MAC
  106. CFStringRef
  107. lastfm::QStringToCFString( const QString &s )
  108. {
  109. return CFStringCreateWithCharacters( 0, (UniChar*)s.unicode(), s.length() );
  110. }
  111. QByteArray
  112. lastfm::CFStringToUtf8( CFStringRef s )
  113. {
  114. QByteArray result;
  115. if (s != NULL)
  116. {
  117. CFIndex length;
  118. length = CFStringGetLength( s );
  119. length = CFStringGetMaximumSizeForEncoding( length, kCFStringEncodingUTF8 ) + 1;
  120. char* buffer = new char[length];
  121. if (CFStringGetCString( s, buffer, length, kCFStringEncodingUTF8 ))
  122. result = QByteArray( buffer );
  123. else
  124. qWarning() << "CFString conversion failed.";
  125. delete[] buffer;
  126. }
  127. return result;
  128. }
  129. #endif
  130. #if 0
  131. // this is a Qt implementation I found
  132. QString cfstring2qstring(CFStringRef str)
  133. {
  134. if(!str)
  135. return QString();
  136. CFIndex length = CFStringGetLength(str);
  137. if(const UniChar *chars = CFStringGetCharactersPtr(str))
  138. return QString((QChar *)chars, length);
  139. UniChar *buffer = (UniChar*)malloc(length * sizeof(UniChar));
  140. CFStringGetCharacters(str, CFRangeMake(0, length), buffer);
  141. QString ret((QChar *)buffer, length);
  142. free(buffer);
  143. return ret;
  144. }
  145. #endif