Changeset 1218


Ignore:
Timestamp:
Dec 14, 2006, 11:44:54 PM (15 years ago)
Author:
titer
Message:

Prevent several instances of Transmission.app from running simultaneously

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/Controller.m

    r1118 r1218  
    7575+ (void) initialize
    7676{
     77    /* Make sure another Transmission.app isn't running already */
     78    NSString * myBundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
     79    int myProcessIdentifier = [[NSProcessInfo processInfo] processIdentifier];
     80
     81    NSDictionary * dico;
     82    NSEnumerator * enumerator = [[[NSWorkspace sharedWorkspace] launchedApplications] objectEnumerator];
     83    while( ( dico = [enumerator nextObject] ) )
     84    {
     85        if( ![[dico objectForKey: @"NSApplicationBundleIdentifier"] isEqualToString: myBundleIdentifier] )
     86        {
     87            /* This isn't Transmission */
     88            continue;
     89        }
     90        if( [[dico objectForKey: @"NSApplicationProcessIdentifier"] intValue] == myProcessIdentifier )
     91        {
     92            /* This is ourselves */
     93            continue;
     94        }
     95       
     96        /* Activate the already running instance, then kill ourselves right away */
     97        NSLog( @"Another instance of Transmission is already running, aborting" );
     98        [[NSWorkspace sharedWorkspace] launchApplication: [dico objectForKey: @"NSApplicationPath"]];
     99        exit( 0 );
     100    }
     101
     102
    77103    [[NSUserDefaults standardUserDefaults] registerDefaults: [NSDictionary dictionaryWithContentsOfFile:
    78104        [[NSBundle mainBundle] pathForResource: @"Defaults" ofType: @"plist"]]];
Note: See TracChangeset for help on using the changeset viewer.