File: ProgressDialog.m

package info (click to toggle)
mediainfo 20.09-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 14,732 kB
  • sloc: cpp: 15,682; objc: 2,760; sh: 1,343; xml: 926; python: 259; perl: 207; makefile: 173
file content (120 lines) | stat: -rw-r--r-- 4,540 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license that can
 *  be found in the License.html file in the root of the source tree.
 */

#import "ProgressDialog.h"

@interface ProgressDialog ()
@end

@implementation ProgressDialog

-(void)dealloc {
    for(id observer in observers) {
        [[NSNotificationCenter defaultCenter] removeObserver:observer];
    }
    [observers release];
    [coordinator release];

    [super dealloc];
}

-(void)windowDidLoad {
    [super windowDidLoad];

    coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
    canceled = NO;

    _progressBar.indeterminate=YES;
    [_progressBar startAnimation:self];

    if(_mediaList && _items && [_items count]) {
        [self queryItem:0];
    }
    else {
        [self cancel];
    }
}

-(void)cancel {
    canceled = YES;
    [coordinator cancel];
    [_progressBar stopAnimation:self];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [NSApp abortModal];
    });
}

-(void)next {
    if(!canceled && currentItemIndex < [_items count] - 1) {
        [self queryItem:currentItemIndex + 1];
    }
    else {
        [_progressBar stopAnimation:self];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [NSApp stopModal];
        });
    }
}

-(IBAction)cancelAction:(id)sender {
    [self cancel];
}

-(void) queryItem:(size_t)index {
    if(index >= [_items count]) {
        [self cancelAction:self];
        return;
    }

    currentItemIndex = index;

    NSURL *url = _items[index];
    if([[url absoluteString] hasSuffix:@".icloud"])
        url = [NSURL URLWithString:[[url absoluteString] stringByDeletingPathExtension]];

    if([[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
        [_progressMessage setStringValue:[NSString stringWithFormat:NSLocalizedString(@"Opening file(s) %lu of %lu ...", @"Opening File"), index + 1,  [_items count]]];
        if(![_mediaList openURL:url]) {
            [[NSAlert alertWithMessageText:NSLocalizedString(@"Error", @"Error header")
                defaultButton:nil alternateButton:nil otherButton:nil
                informativeTextWithFormat:NSLocalizedString(@"Can not open %@", @"Open error with filename"), url.absoluteString] runModal];
        }

        [self next];
    }
    else if(@available(macOS 10.7, *) && [[NSFileManager defaultManager] isUbiquitousItemAtURL:url]) {
        [_progressMessage setStringValue:[NSString stringWithFormat:NSLocalizedString(@"Downloading file(s) %lu of %lu ...", @"Downloading File"), index + 1,  [_items count]]];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSError *error = nil;
            [coordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [_progressMessage setStringValue:[NSString stringWithFormat:NSLocalizedString(@"Opening file(s) %lu of %lu ...", @"Opening File"), index + 1,  [_items count]]];
                    if(!canceled && ![_mediaList openURL:newURL]) {
                        [[NSAlert alertWithMessageText:NSLocalizedString(@"Error", @"Error header")
                            defaultButton:nil alternateButton:nil otherButton:nil
                            informativeTextWithFormat:NSLocalizedString(@"Can not open %@", @"Open error with filename"), url.absoluteString] runModal];
                    }
                });

                dispatch_async(dispatch_get_main_queue(), ^{
                    if(!canceled && error) {
                        [[NSAlert alertWithMessageText:NSLocalizedString(@"Error", @"Error header")
                            defaultButton:nil alternateButton:nil otherButton:nil
                            informativeTextWithFormat:NSLocalizedString(@"Can not download %@", @"Download error with filename"), url.absoluteString] runModal];
                    }
                    [self next];
                });
            }];
        });
    }
    else {
        [[NSAlert alertWithMessageText:NSLocalizedString(@"Error", @"Error header")
            defaultButton:nil alternateButton:nil otherButton:nil
            informativeTextWithFormat:NSLocalizedString(@"Can not open %@", @"Open error with filename"), url.absoluteString] runModal];
    }
}
@end