PageRenderTime 291ms CodeModel.GetById 100ms app.highlight 103ms RepoModel.GetById 79ms app.codeStats 1ms

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