
#include <QUrl>

#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSURL.h>

QString fromNSString(const NSString *string)
{
    if (!string)
        return QString();
   QString qstring;
   qstring.resize([string length]);
   [string getCharacters: reinterpret_cast<unichar*>(qstring.data()) range: NSMakeRange(0, [string length])];
   return qstring;
}

NSString *toNSString(const QString &str)
{
    return [NSString stringWithCharacters: reinterpret_cast<const UniChar*>(str.unicode()) length: str.length()];
}

QUrl fromNSURL(const NSURL *url)
{
    return QUrl(fromNSString([url absoluteString]));
}

NSURL *toNSURL(const QUrl &url)
{
    return [NSURL URLWithString:toNSString(url.toEncoded())];
}

QUrl filePathUrl(const QUrl &url)
{
    QUrl ret = url;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSURL *nsurl = toNSURL(url);
    if ([nsurl isFileReferenceURL])
        ret = fromNSURL([nsurl filePathURL]);
    [pool release];
    return ret;
}
