Mercurial > projects > dwt-mac
diff dwt/program/Program.d @ 121:e1c48e37e0f5
Ported dwt.program.Program
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Wed, 31 Dec 2008 16:50:37 +0100 |
parents | cfa563df4fdd |
children | 2e671fa40eec |
line wrap: on
line diff
--- a/dwt/program/Program.d Wed Dec 31 16:16:40 2008 +0100 +++ b/dwt/program/Program.d Wed Dec 31 16:50:37 2008 +0100 @@ -7,14 +7,15 @@ * * Contributors: * IBM Corporation - initial API and implementation + * + * Port to the D programming language: + * Jacob Carlborg <doob@me.com> *******************************************************************************/ module dwt.program.Program; import dwt.dwthelper.utils; -import java.util.Vector; - import dwt.DWT; import dwt.graphics.ImageData; import dwt.graphics.PaletteData; @@ -35,7 +36,12 @@ import dwt.internal.cocoa.NSURL; import dwt.internal.cocoa.NSWorkspace; import dwt.internal.cocoa.OS; -import dwt.internal.cocoa.id; +import cocoa = dwt.internal.cocoa.id; + +import dwt.dwthelper.array; +import dwt.internal.c.Carbon; +import dwt.internal.objc.cocoa.Cocoa; +import objc = dwt.internal.objc.runtime; /** * Instances of this class represent programs and @@ -48,14 +54,14 @@ public final class Program { String name, fullPath, identifier; - static final String PREFIX_FILE = "file://"; //$NON-NLS-1$ - static final String PREFIX_HTTP = "http://"; //$NON-NLS-1$ - static final String PREFIX_HTTPS = "https://"; //$NON-NLS-1$ + static const String PREFIX_FILE = "file://"; //$NON-NLS-1$ + static const String PREFIX_HTTP = "http://"; //$NON-NLS-1$ + static const String PREFIX_HTTPS = "https://"; //$NON-NLS-1$ /** * Prevents uninitialized instances from being created outside the package. */ -Program () { +this () { } /** @@ -74,25 +80,25 @@ public static Program findProgram (String extension) { if (extension is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); if (extension.length () is 0) return null; - if (extension.charAt(0) !is '.') extension = "." + extension; - NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); + if (extension.charAt(0) !is '.') extension = "." ~ extension; + NSAutoreleasePool pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init(); try { NSWorkspace workspace = NSWorkspace.sharedWorkspace(); - int /*long*/ appName = OS.malloc(C.PTR_SIZEOF); - int /*long*/ type = OS.malloc(C.PTR_SIZEOF); + objc.id appName = cast(objc.id) OS.malloc(C.PTR_SIZEOF); + objc.id type = cast(objc.id) OS.malloc(C.PTR_SIZEOF); NSString temp = new NSString(OS.NSTemporaryDirectory()); - NSString fileName = NSString.stringWith("swt" + System.currentTimeMillis() + extension); + NSString fileName = NSString.stringWith("swt" ~ Format("{}", System.currentTimeMillis()) ~ extension); NSString fullPath = temp.stringByAppendingPathComponent(fileName); NSFileManager fileManager = NSFileManager.defaultManager(); fileManager.createFileAtPath(fullPath, null, null); workspace.getInfoForFile(fullPath, appName, type); - fileManager.removeItemAtPath(fullPath, 0); - int /*long*/ [] buffer = new int /*long*/[1]; + fileManager.removeItemAtPath(fullPath, null); + objc.id buffer; OS.memmove(buffer, appName, C.PTR_SIZEOF); OS.free(appName); OS.free(type); - if (buffer [0] !is 0) { - NSString appPath = new NSString(buffer[0]); + if (buffer !is null) { + NSString appPath = new NSString(buffer); NSBundle bundle = NSBundle.bundleWithPath(appPath); if (bundle !is null) return getProgram(bundle); } @@ -110,20 +116,20 @@ * @return an array of extensions */ public static String [] getExtensions () { - NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); + NSAutoreleasePool pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init(); try { - NSMutableSet supportedDocumentTypes = (NSMutableSet)NSMutableSet.set(); + NSMutableSet supportedDocumentTypes = cast(NSMutableSet)NSMutableSet.set(); NSWorkspace workspace = NSWorkspace.sharedWorkspace(); NSString CFBundleDocumentTypes = NSString.stringWith("CFBundleDocumentTypes"); NSString CFBundleTypeExtensions = NSString.stringWith("CFBundleTypeExtensions"); NSArray array = new NSArray(OS.NSSearchPathForDirectoriesInDomains(OS.NSAllApplicationsDirectory, OS.NSAllDomainsMask, true)); - int count = (int)/*64*/array.count(); - for (int i = 0; i < count; i++) { + NSUInteger count = array.count(); + for (NSUInteger i = 0; i < count; i++) { NSString path = new NSString(array.objectAtIndex(i)); NSFileManager fileManager = NSFileManager.defaultManager(); NSDirectoryEnumerator enumerator = fileManager.enumeratorAtPath(path); if (enumerator !is null) { - id id; + cocoa.id id; while ((id = enumerator.nextObject()) !is null) { enumerator.skipDescendents(); NSString filePath = new NSString(id.id); @@ -147,12 +153,12 @@ } } int i = 0; - String[] exts = new String[(int)/*64*/supportedDocumentTypes.count()]; + String[] exts = new String[supportedDocumentTypes.count()]; NSEnumerator enumerator = supportedDocumentTypes.objectEnumerator(); - id id; + cocoa.id id; while ((id = enumerator.nextObject()) !is null) { - String ext = new NSString(id.id).getString(); - if (!ext.equals("*")) exts[i++] = "." + ext; + String ext = (new NSString(id.id)).getString(); + if (ext != "*") exts[i++] = "." ~ ext; } if (i !is exts.length) { String[] temp = new String[i]; @@ -170,7 +176,7 @@ NSString CFBundleDisplayName = NSString.stringWith("CFBundleDisplayName"); NSString fullPath = bundle.bundlePath(); NSString identifier = bundle.bundleIdentifier(); - id bundleName = bundle.objectForInfoDictionaryKey(CFBundleDisplayName); + cocoa.id bundleName = bundle.objectForInfoDictionaryKey(CFBundleDisplayName); if (bundleName is null) { bundleName = bundle.objectForInfoDictionaryKey(CFBundleName); } @@ -193,18 +199,18 @@ * @return an array of programs */ public static Program [] getPrograms () { - NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); + NSAutoreleasePool pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init(); try { - Vector vector = new Vector(); + Program[] vector; NSWorkspace workspace = NSWorkspace.sharedWorkspace(); NSArray array = new NSArray(OS.NSSearchPathForDirectoriesInDomains(OS.NSAllApplicationsDirectory, OS.NSAllDomainsMask, true)); - int count = (int)/*64*/array.count(); - for (int i = 0; i < count; i++) { + NSUInteger count = array.count(); + for (NSUInteger i = 0; i < count; i++) { NSString path = new NSString(array.objectAtIndex(i)); NSFileManager fileManager = NSFileManager.defaultManager(); NSDirectoryEnumerator enumerator = fileManager.enumeratorAtPath(path); if (enumerator !is null) { - id id; + cocoa.id id; while ((id = enumerator.nextObject()) !is null) { enumerator.skipDescendents(); NSString fullPath = path.stringByAppendingPathComponent(new NSString(id.id)); @@ -215,9 +221,7 @@ } } } - Program[] programs = new Program[vector.size()]; - vector.copyInto(programs); - return programs; + vector.dup; } finally { pool.release(); } @@ -238,7 +242,7 @@ */ public static bool launch (String fileName) { if (fileName is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); - NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); + NSAutoreleasePool pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init(); try { NSString unescapedStr = NSString.stringWith("%"); //$NON-NLS-1$ if (fileName.indexOf(':') is -1) { @@ -250,7 +254,7 @@ } } NSString fullPath = NSString.stringWith(fileName); - int /*long*/ ptr = OS.CFURLCreateStringByAddingPercentEscapes(0, fullPath.id, unescapedStr.id, 0, OS.kCFStringEncodingUTF8); + CFStringRef ptr = OS.CFURLCreateStringByAddingPercentEscapes(null, fullPath.id, unescapedStr.id, null, OS.kCFStringEncodingUTF8); NSString escapedString = new NSString(ptr); NSWorkspace workspace = NSWorkspace.sharedWorkspace(); bool result = workspace.openURL(NSURL.URLWithString(escapedString)); @@ -276,7 +280,7 @@ */ public bool execute (String fileName) { if (fileName is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); - NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); + NSAutoreleasePool pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init(); try { NSWorkspace workspace = NSWorkspace.sharedWorkspace(); NSString fullPath = NSString.stringWith(fileName); @@ -288,7 +292,7 @@ if (lowercaseName.startsWith (PREFIX_HTTP) || lowercaseName.startsWith (PREFIX_HTTPS)) { unescapedStr = NSString.stringWith("%#"); //$NON-NLS-1$ } - int /*long*/ ptr = OS.CFURLCreateStringByAddingPercentEscapes(0, fullPath.id, unescapedStr.id, 0, OS.kCFStringEncodingUTF8); + CFStringRef ptr = OS.CFURLCreateStringByAddingPercentEscapes(0, fullPath.id, unescapedStr.id, 0, OS.kCFStringEncodingUTF8); NSString escapedString = new NSString(ptr); NSArray urls = NSArray.arrayWithObject(NSURL.URLWithString(escapedString)); OS.CFRelease(ptr); @@ -306,7 +310,7 @@ * @return the image data for the program, may be null */ public ImageData getImageData () { - NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); + NSAutoreleasePool pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init(); try { NSWorkspace workspace = NSWorkspace.sharedWorkspace(); NSString fullPath; @@ -327,10 +331,10 @@ imageRep = new NSBitmapImageRep(rep.id); } if (imageRep !is null) { - int width = (int)/*64*/imageRep.pixelsWide(); - int height = (int)/*64*/imageRep.pixelsHigh(); - int bpr = (int)/*64*/imageRep.bytesPerRow(); - int bpp = (int)/*64*/imageRep.bitsPerPixel(); + int width = cast(int)/*64*/imageRep.pixelsWide(); + int height = cast(int)/*64*/imageRep.pixelsHigh(); + int bpr = cast(int)/*64*/imageRep.bytesPerRow(); + int bpp = cast(int)/*64*/imageRep.bitsPerPixel(); int dataSize = height * bpr; byte[] srcData = new byte[dataSize]; OS.memmove(srcData, imageRep.bitmapData(), dataSize); @@ -374,15 +378,17 @@ * * @see #hashCode() */ -public bool equals(Object other) { +public int opEquals(Object other) { if (this is other) return true; - if (other instanceof Program) { - final Program program = (Program) other; + if (cast(Program) other) { + final Program program = cast(Program) other; return name.equals(program.name); } return false; } +alias opEquals equals; + /** * Returns an integer hash code for the receiver. Any two * objects that return <code>true</code> when passed to @@ -393,10 +399,12 @@ * * @see #equals(Object) */ -public int hashCode() { - return name.hashCode(); +public hash_t toHash() { + return .toHash(name); } +alias toHash hashCode; + /** * Returns a string containing a concise, human-readable * description of the receiver. @@ -404,7 +412,7 @@ * @return a string representation of the program */ public String toString () { - return "Program {" + name + "}"; + return "Program {" ~ name ~ "}"; } }