Main Page | Alphabetical List | Class List | Directories | File List | Class Members | Related Pages

DAApplication.m

00001 //
00002 //  DAApplication.m
00003 //  DownAndOutª
00004 //
00005 //  Created by Fritz Anderson on Wed Aug 20 2003.
00006 //  Copyright (c) 2003 Trustees of the University of Chicago. All rights reserved.
00007 //  This program is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU General Public License as published by
00009 //  the Free Software Foundation; either version 2 of the License, or
00010 //  (at your option) any later version.
00011 //  
00012 //  This program is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU General Public License for more details.
00016 //  
00017 //  You should have received a copy of the GNU General Public License
00018 //  along with this program; if not, write to the Free Software
00019 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020 //
00021 
00022 #import "DAApplication.h"
00023 #import <signal.h>
00024 
00025 @implementation DAApplication
00026 
00027 + (int) countApplications
00028 {
00029     NSArray *       appList = [[NSWorkspace sharedWorkspace] launchedApplications];
00030     return [appList count];
00031 }
00032 
00033 + (NSMutableArray *) launchedApplications
00034 {
00035     NSArray *           appList = [[NSWorkspace sharedWorkspace] launchedApplications];
00036     NSMutableArray *    retval = [NSMutableArray arrayWithCapacity: [appList count]];
00037     NSEnumerator *      iter = [appList objectEnumerator];
00038     NSDictionary *      curr;
00039     
00040     while (curr = [iter nextObject]) {
00041         NSString *      appName = [curr objectForKey: @"NSApplicationName"];
00042         DAApplication * item = [[DAApplication alloc] initWithName: appName];
00043         [retval addObject: item];
00044         [item release];
00045     }
00046     
00047     return retval;
00048 }
00049 
00050 static NSMutableArray *     sQuitApplications = nil;
00051 + (NSMutableArray *) quitApplicationNames
00052 {
00053     if (!sQuitApplications)
00054         sQuitApplications = [[NSMutableArray alloc] init];
00055     
00056     return sQuitApplications;
00057 }
00058 
00059 static NSMutableArray *     sKilledApplications = nil;
00060 + (NSMutableArray *) killedApplicationNames
00061 {
00062     if (!sKilledApplications)
00063         sKilledApplications = [[NSMutableArray alloc] init];
00064     
00065     return sKilledApplications;
00066 }
00067 
00068 - (id) initWithName: (NSString *) inName
00069 {
00070     name = [inName retain];
00071     return self;
00072 }
00073 
00074 - (id) initWithIndex: (int) index
00075 {
00076     if (index < 0) {
00077         [self release];
00078         return nil;
00079     }
00080     
00081     NSArray *       appList = [[NSWorkspace sharedWorkspace] launchedApplications];
00082     
00083     if (index >= [appList count]) {
00084         [self release];
00085         return nil;
00086     }
00087     
00088     return [self initWithName: [[appList objectAtIndex: index] objectForKey: @"NSApplicationName"]];
00089 }
00090 
00092 - (void) dealloc
00093 {
00094     [name release];
00095     [super dealloc];
00096 }
00097 
00101 - (NSString *) description
00102 {
00103     return [NSString stringWithFormat: @"<DAApplication @%p, name=%@>", self, name];
00104 }
00105 
00110 - (BOOL) isEqualToString: (NSString *) aString
00111 {
00112     return [name isEqualToString: aString];
00113 }
00114 
00115 - (NSString *) name { return name; }
00116 
00117 - (void) quitWithoutSaving
00118 {
00119     NSString *      fileName = [self fileName];
00120     if (! fileName)
00121         return;
00122     
00123     NSAppleScript * script = [[NSAppleScript alloc]
00124         initWithSource: [NSString stringWithFormat:
00125             @"ignoring application responses\r"
00126             @"quit application \"%@\" saving no\r"
00127             @"end ignoring", fileName]
00128         ];
00129     NSDictionary *              error;
00130     NSAppleEventDescriptor *    result;
00131     
00132     result = [script executeAndReturnError: &error];
00133     if (result)
00134         [[DAApplication quitApplicationNames] addObject: [self name]];
00135     else
00136         NSLog(@"%@ error in quit: %@", name, error);
00137 }
00138 
00143 - (NSDictionary *) recordInApplicationList
00144 {
00145     NSArray *       appList = [[NSWorkspace sharedWorkspace] launchedApplications];
00146     NSEnumerator *  iter = [appList objectEnumerator];
00147     NSDictionary *  curr;
00148     
00149     while (curr = [iter nextObject]) {
00150         if ([[curr objectForKey: @"NSApplicationName"] isEqualToString: name])
00151             return curr;
00152     }
00153     
00154     return nil;
00155 }
00156 
00157 - (BOOL) isRunning
00158 {
00159     return [self recordInApplicationList] != nil;
00160 }
00161 
00162 - (int) processID
00163 {
00164     NSDictionary *  record = [self recordInApplicationList];
00165     if (record)
00166         return [[record objectForKey: @"NSApplicationProcessIdentifier"] intValue];
00167     else
00168         return DAAPPLICATION_INVALID_PID;
00169 }
00170 
00171 - (NSString *) bundleID
00172 {
00173     NSDictionary *  record = [self recordInApplicationList];
00174     if (record)
00175         return [record objectForKey: @"NSApplicationBundleIdentifier"];
00176     else
00177         return nil;
00178 }
00179 
00180 - (NSString *) fileName
00181 {
00182     NSDictionary *  record = [self recordInApplicationList];
00183     if (record) {
00184         NSString *  retval = [[record objectForKey: @"NSApplicationPath"] lastPathComponent];
00185         
00186         if ([retval hasSuffix: @".app"]) {
00187             retval = [retval substringToIndex: [retval length] - 4];
00188         }
00189         return retval;
00190     }
00191     else
00192         return nil;
00193 }
00194 
00195 - (BOOL) kill: (int) signal
00196 {
00197     int     pid = [self processID];
00198     
00199     if (pid == DAAPPLICATION_INVALID_PID)
00200         return NO;
00201     
00202     int     result = kill(pid, signal);
00203     
00204     [[DAApplication killedApplicationNames] addObject: [self name]];
00205     
00206     return result == 0;
00207 }
00208 
00209 - (BOOL) killObj: (id) hasIntValue
00210 {
00211     if ([hasIntValue respondsToSelector: @selector(intValue)])
00212         return [self kill: [hasIntValue intValue]];
00213     else
00214         return NO;
00215 }
00216 
00217 @end

Generated on Wed Jan 25 12:04:27 2006 for DownAndOut by  doxygen 1.4.4