OSDN Git Service

MacGui: Re-locate logic for encode done notifications as per user preferences.
[handbrake-jp/handbrake-jp-git.git] / macosx / Controller.m
index d9122b5..5bba38c 100644 (file)
@@ -85,11 +85,6 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
     NSString *versionStringFull = [[NSString stringWithFormat: @"Handbrake Version: %@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]] stringByAppendingString: [NSString stringWithFormat: @" (%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]]];
     [self writeToActivityLog: "%s", [versionStringFull UTF8String]];
     
-    /* Get the PID number for this hb instance, used in multi instance encoding */
-    //pidNum = [self getThisHBInstancePID];
-    /* Report this pid to the activity log */
-    //[self writeToActivityLog: "Pid for this instance:%d", pidNum];
-    
     return self;
 }
 
@@ -182,9 +177,9 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
          */
         if (hbInstanceNum > 1)
         {
-        alertTitle = [NSString stringWithFormat:
-                         NSLocalizedString(@"There is already an instance of HandBrake running.", @"")];
-        NSBeginCriticalAlertSheet(
+            alertTitle = [NSString stringWithFormat:
+                          NSLocalizedString(@"There is already an instance of HandBrake running.", @"")];
+            NSBeginCriticalAlertSheet(
                                       alertTitle,
                                       NSLocalizedString(@"Reload Queue", nil),
                                       nil,
@@ -195,29 +190,48 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
         }
         else
         {
-            if (fWorkingCount > 0)
+            if (fWorkingCount > 0 || fPendingCount > 0)
             {
-                alertTitle = [NSString stringWithFormat:
-                              NSLocalizedString(@"HandBrake Has Detected %d Previously Encoding Item and %d Pending Item(s) In Your Queue.", @""),
-                              fWorkingCount,fPendingCount];
+                if (fWorkingCount > 0)
+                {
+                    alertTitle = [NSString stringWithFormat:
+                                  NSLocalizedString(@"HandBrake Has Detected %d Previously Encoding Item(s) and %d Pending Item(s) In Your Queue.", @""),
+                                  fWorkingCount,fPendingCount];
+                }
+                else
+                {
+                    alertTitle = [NSString stringWithFormat:
+                                  NSLocalizedString(@"HandBrake Has Detected %d Pending Item(s) In Your Queue.", @""),
+                                  fPendingCount];
+                }
+                
+                NSBeginCriticalAlertSheet(
+                                          alertTitle,
+                                          NSLocalizedString(@"Reload Queue", nil),
+                                          nil,
+                                          NSLocalizedString(@"Empty Queue", nil),
+                                          fWindow, self,
+                                          nil, @selector(didDimissReloadQueue:returnCode:contextInfo:), nil,
+                                          NSLocalizedString(@" Do you want to reload them ?", nil));
             }
             else
             {
-                alertTitle = [NSString stringWithFormat:
-                              NSLocalizedString(@"HandBrake Has Detected %d Pending Item(s) In Your Queue.", @""),
-                              fPendingCount];
+                /* Since we addressed any pending or previously encoding items above, we go ahead and make sure the queue
+                 * is empty of any finished items or cancelled items */
+                [self clearQueueAllItems];
+                /* We show whichever open source window specified in LaunchSourceBehavior preference key */
+                if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"LaunchSourceBehavior"] isEqualToString: @"Open Source"])
+                {
+                    [self browseSources:nil];
+                }
+                
+                if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"LaunchSourceBehavior"] isEqualToString: @"Open Source (Title Specific)"])
+                {
+                    [self browseSources:(id)fOpenSourceTitleMMenu];
+                }
             }
             
-            NSBeginCriticalAlertSheet(
-                                      alertTitle,
-                                      NSLocalizedString(@"Reload Queue", nil),
-                                      nil,
-                                      NSLocalizedString(@"Empty Queue", nil),
-                                      fWindow, self,
-                                      nil, @selector(didDimissReloadQueue:returnCode:contextInfo:), nil,
-                                      NSLocalizedString(@" Do you want to reload them ?", nil));
         }
-        
     }
     else
     {
@@ -255,7 +269,6 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
        {
         if ([[runningAppsDictionary valueForKey:@"NSApplicationName"] isEqualToString:@"HandBrake"])
                {
-            hbInstances++;
             /*Report the path to each active instances app path */
             runningInstancePidNum = [[runningAppsDictionary valueForKey:@"NSApplicationProcessIdentifier"] intValue];
             runningInstanceAppPath = [runningAppsDictionary valueForKey:@"NSApplicationPath"];
@@ -272,7 +285,8 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
                 /* Tell fQueueController what our pidNum is */
                 [fQueueController setPidNum:pidNum];
             }
-               }
+            hbInstances++;
+        }
        }
     return hbInstances;
 }
@@ -281,8 +295,11 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
 
 - (void) didDimissReloadQueue: (NSWindow *)sheet returnCode: (int)returnCode contextInfo: (void *)contextInfo
 {
+    
+    [self writeToActivityLog: "didDimissReloadQueue number of hb instances:%d", hbInstanceNum];
     if (returnCode == NSAlertOtherReturn)
     {
+        [self writeToActivityLog: "didDimissReloadQueue NSAlertOtherReturn Chosen"];
         [self clearQueueAllItems];
         
         /* We show whichever open source window specified in LaunchSourceBehavior preference key */
@@ -298,8 +315,10 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
     }
     else
     {
-        if ([self hbInstances] == 1)
+        [self writeToActivityLog: "didDimissReloadQueue First Button Chosen"];
+        if (hbInstanceNum == 1)
         {
+            
             [self setQueueEncodingItemsAsPending];
         }
         [self showQueueWindow:NULL];
@@ -984,53 +1003,23 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
                 pathOfFinishedEncode = [[QueueFileArray objectAtIndex:currentQueueEncodeIndex] objectForKey:@"DestinationPath"];
                 
                 /* Both the Growl Alert and Sending to MetaX can be done as encodes roll off the queue */
-                /* Growl alert */
-                [self showGrowlDoneNotification:pathOfFinishedEncode];
-                /* Send to MetaX */
-                [self sendToMetaX:pathOfFinishedEncode];
-                
-                /* since we have successfully completed an encode, we increment the queue counter */
-                [self incrementQueueItemDone:currentQueueEncodeIndex]; 
-                
-                /* all end of queue actions below need to be done after all queue encodes have finished 
-                 * and there are no pending jobs left to process
-                 */
-                if (fPendingCount == 0)
+                if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Growl Notification"] || 
+                    [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window And Growl"])
                 {
-                    /* If Alert Window or Window and Growl has been selected */
-                    if( [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window"] ||
-                       [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window And Growl"] )
+                    /* If Play System Alert has been selected in Preferences */
+                    if( [[NSUserDefaults standardUserDefaults] boolForKey:@"AlertWhenDoneSound"] == YES )
                     {
-                        /*On Screen Notification*/
-                        int status;
                         NSBeep();
-                        status = NSRunAlertPanel(@"Put down that cocktail...",@"Your HandBrake queue is done!", @"OK", nil, nil);
-                        [NSApp requestUserAttention:NSCriticalRequest];
-                    }
-                    
-                    /* If sleep has been selected */
-                    if( [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Put Computer To Sleep"] )
-                    {
-                        /* Sleep */
-                        NSDictionary* errorDict;
-                        NSAppleEventDescriptor* returnDescriptor = nil;
-                        NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource:
-                                                       @"tell application \"Finder\" to sleep"];
-                        returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
-                        [scriptObject release];
-                    }
-                    /* If Shutdown has been selected */
-                    if( [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Shut Down Computer"] )
-                    {
-                        /* Shut Down */
-                        NSDictionary* errorDict;
-                        NSAppleEventDescriptor* returnDescriptor = nil;
-                        NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource:
-                                                       @"tell application \"Finder\" to shut down"];
-                        returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
-                        [scriptObject release];
                     }
+                    [self showGrowlDoneNotification:pathOfFinishedEncode];
                 }
+                
+                /* Send to MetaX */
+                [self sendToMetaX:pathOfFinishedEncode];
+                
+                /* since we have successfully completed an encode, we increment the queue counter */
+                [self incrementQueueItemDone:currentQueueEncodeIndex]; 
+
             }
             
             break;
@@ -1402,23 +1391,19 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
 -(void)showGrowlDoneNotification:(NSString *) filePath
 {
     /* This end of encode action is called as each encode rolls off of the queue */
+    /* Setup the Growl stuff ... */
     NSString * finishedEncode = filePath;
     /* strip off the path to just show the file name */
     finishedEncode = [finishedEncode lastPathComponent];
-    if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Growl Notification"] || 
-        [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window And Growl"])
-    {
-        NSString * growlMssg = [NSString stringWithFormat: @"your HandBrake encode %@ is done!",finishedEncode];
-        [GrowlApplicationBridge 
-         notifyWithTitle:@"Put down that cocktail..." 
-         description:growlMssg 
-         notificationName:SERVICE_NAME
-         iconData:nil 
-         priority:0 
-         isSticky:1 
-         clickContext:nil];
-    }
-    
+    NSString * growlMssg = [NSString stringWithFormat: @"your HandBrake encode %@ is done!",finishedEncode];
+    [GrowlApplicationBridge 
+     notifyWithTitle:@"Put down that cocktail..." 
+     description:growlMssg 
+     notificationName:SERVICE_NAME
+     iconData:nil 
+     priority:0 
+     isSticky:1 
+     clickContext:nil];
 }
 -(void)sendToMetaX:(NSString *) filePath
 {
@@ -1436,6 +1421,49 @@ static NSString *        ChooseSourceIdentifier             = @"Choose Source It
         
     }
 }
+
+- (void) queueCompletedAlerts
+{
+    /* If Play System Alert has been selected in Preferences */
+    if( [[NSUserDefaults standardUserDefaults] boolForKey:@"AlertWhenDoneSound"] == YES )
+    {
+        NSBeep();
+    }
+    
+    /* If Alert Window or Window and Growl has been selected */
+    if( [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window"] ||
+       [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window And Growl"] )
+    {
+        /*On Screen Notification*/
+        int status;
+        status = NSRunAlertPanel(@"Put down that cocktail...",@"Your HandBrake queue is done!", @"OK", nil, nil);
+        [NSApp requestUserAttention:NSCriticalRequest];
+    }
+    
+    /* If sleep has been selected */
+    if( [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Put Computer To Sleep"] )
+    {
+        /* Sleep */
+        NSDictionary* errorDict;
+        NSAppleEventDescriptor* returnDescriptor = nil;
+        NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource:
+                                       @"tell application \"Finder\" to sleep"];
+        returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
+        [scriptObject release];
+    }
+    /* If Shutdown has been selected */
+    if( [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Shut Down Computer"] )
+    {
+        /* Shut Down */
+        NSDictionary* errorDict;
+        NSAppleEventDescriptor* returnDescriptor = nil;
+        NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource:
+                                       @"tell application \"Finder\" to shut down"];
+        returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
+        [scriptObject release];
+    }
+}
+
 #pragma mark -
 #pragma mark Get New Source
 
@@ -2081,15 +2109,19 @@ fWorkingCount = 0;
     /* we look here to see if the preset is we move on to the next one */
     while ( tempObject = [enumerator nextObject] )  
     {
-        /* If the queue item is marked as "encoding" (1)
-         * then change its status back to pending (2) which effectively
-         * puts it back into the queue to be encoded
-         */
-        if ([[tempObject objectForKey:@"Status"] intValue] == 1)
+        /* We want to keep any queue item that is pending or was previously being encoded */
+        if ([[tempObject objectForKey:@"Status"] intValue] == 1 || [[tempObject objectForKey:@"Status"] intValue] == 2)
         {
-            [tempObject setObject:[NSNumber numberWithInt: 2] forKey:@"Status"];
+            /* If the queue item is marked as "encoding" (1)
+             * then change its status back to pending (2) which effectively
+             * puts it back into the queue to be encoded
+             */
+            if ([[tempObject objectForKey:@"Status"] intValue] == 1)
+            {
+                [tempObject setObject:[NSNumber numberWithInt: 2] forKey:@"Status"];
+            }
+            [tempArray addObject:tempObject];
         }
-        [tempArray addObject:tempObject];
     }
     
     [QueueFileArray setArray:tempArray];
@@ -2496,6 +2528,8 @@ fWorkingCount = 0;
     else
     {
         [self writeToActivityLog: "incrementQueueItemDone there are no more pending encodes"];
+        /*Since there are no more items to encode, go to queueCompletedAlerts for user specified alerts after queue completed*/
+        [self queueCompletedAlerts];
     }
 }
 
@@ -2679,8 +2713,6 @@ fWorkingCount = 0;
     [self doRip];
 }
 
-
-
 #pragma mark -
 #pragma mark Queue Item Editing