91
92
return QLocale( langCode ).language();
94
} // namespace UnicornUtils
97
UnicornUtils::CFStringToUtf8( CFStringRef s )
104
length = CFStringGetLength( s );
105
length = CFStringGetMaximumSizeForEncoding( length, kCFStringEncodingUTF8 ) + 1;
106
char* buffer = new char[length];
108
if (CFStringGetCString( s, buffer, length, kCFStringEncodingUTF8 ))
109
result = QByteArray( buffer );
111
qWarning() << "CFString conversion failed.";
120
/** Returns a list of all BSD processes on the system. This routine
121
* allocates the list and puts it in *procList and a count of the
122
* number of entries in *procCount. You are responsible for freeing
123
* this list (use "free" from System framework).
124
* On success, the function returns 0.
125
* On error, the function returns a BSD errno value.
128
getBsdProcessList( kinfo_proc **procList, size_t *procCount )
133
static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
134
// Declaring name as const requires us to cast it when passing it to
135
// sysctl because the prototype doesn't include the const modifier.
138
// We start by calling sysctl with result == NULL and length == 0.
139
// That will succeed, and set length to the appropriate length.
140
// We then allocate a buffer of that size and call sysctl again
141
// with that buffer. If that succeeds, we're done. If that fails
142
// with ENOMEM, we have to throw away our buffer and loop. Note
143
// that the loop causes use to call sysctl with NULL again; this
144
// is necessary because the ENOMEM failure case sets length to
145
// the amount of data returned, not the amount of data that
146
// could have been returned.
153
// Call sysctl with a NULL buffer.
155
err = sysctl( (int *) name, ( sizeof( name ) / sizeof( *name ) ) - 1, NULL, &length, NULL, 0 );
161
// Allocate an appropriately sized buffer based on the results
162
// from the previous call.
165
result = (kinfo_proc*)malloc( length );
166
if ( result == NULL )
172
// Call sysctl again with the new buffer. If we get an ENOMEM
173
// error, toss away our buffer and start again.
176
err = sysctl( (int *) name, ( sizeof( name ) / sizeof( *name ) ) - 1, result, &length, NULL, 0 );
184
} else if ( err == ENOMEM )
191
} while ( err == 0 && !done );
193
// Clean up and establish post conditions.
194
if ( err != 0 && result != NULL )
203
*procCount = length / sizeof( kinfo_proc );
211
isProcessRunning( const QString& processName )
215
kinfo_proc* processList = NULL;
216
size_t processCount = 0;
218
if ( getBsdProcessList( &processList, &processCount ) )
223
uint const uid = ::getuid();
224
for ( size_t processIndex = 0; processIndex < processCount; processIndex++ )
226
if ( processList[processIndex].kp_eproc.e_pcred.p_ruid == uid )
228
if ( strcmp( processList[processIndex].kp_proc.p_comm,
229
processName.toLocal8Bit() ) == 0 )
243
UnicornUtils::isGrowlInstalled()
245
return isProcessRunning( "GrowlHelperApp" );
250
UnicornUtils::iTunesIsOpen()
252
return isProcessRunning( "iTunes" );
257
UnicornUtils::setPreferredAppForUrlScheme( const QUrl& url, const QString& app )
259
QString scheme = url.scheme();
260
CFStringRef urlStr = CFStringCreateWithCharacters( NULL, reinterpret_cast<const UniChar *>( scheme.unicode() ), scheme.length() );
261
CFStringRef bundleStr = CFStringCreateWithCharacters( NULL, reinterpret_cast<const UniChar *>( app.unicode() ), app.length() );
264
CFBundleRef bundleRef;
265
bundleURL = CFURLCreateWithFileSystemPath( kCFAllocatorDefault, bundleStr, kCFURLPOSIXPathStyle, true );
266
bundleRef = CFBundleCreate( kCFAllocatorDefault, bundleURL );
268
CFStringRef appIdentifier = CFBundleGetIdentifier( bundleRef );
269
OSStatus err = LSSetDefaultHandlerForURLScheme( urlStr, appIdentifier );
272
CFRelease( bundleStr );
273
CFRelease( bundleURL );
274
CFRelease( bundleRef );
276
return ( err == noErr );
281
UnicornUtils::preferredAppForUrlScheme( const QUrl& url )
284
QString scheme = url.scheme() + "://";
286
CFStringRef urlStr = CFStringCreateWithCharacters( NULL, reinterpret_cast<const UniChar *>( scheme.unicode() ), scheme.length() );
287
CFURLRef urlRef = CFURLCreateWithString( NULL, urlStr, NULL );
290
OSStatus err = LSGetApplicationForURL( urlRef, kLSRolesAll, &appRef, NULL );
294
unsigned char path[512];
295
err = ::FSRefMakePath( &appRef, path, 512 );
298
app = (const char*)path;
304
return QDir::cleanPath( app );