File: AppController.m

package info (click to toggle)
mediainfo 25.04-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,124 kB
  • sloc: cpp: 18,542; objc: 3,089; sh: 1,417; xml: 1,268; python: 319; makefile: 214; perl: 207
file content (239 lines) | stat: -rw-r--r-- 7,171 bytes parent folder | download | duplicates (4)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*  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.
 */

//  Created by Max Pozdeev on 06.02.12.

#import "AppController.h"
#import "MyWindowController.h"
#import "AboutWindowController.h"
#import "PurchaseReportController.h"
#import "SubscribeWindowController.h"
#import "PreferencesWindowController.h"
#import "oMediaInfoList.h"

@implementation AppController

+(void) initialize
{
	
	NSDictionary *defaults = [NSDictionary dictionaryWithObjectsAndKeys:
							  [NSNumber numberWithBool:YES],	@"NSDisabledCharacterPaletteMenuItem", //Disable "Special Characters" in Edit menu
							  [NSNumber numberWithInt:12],		@"treeFontSize",
							  @"__default__",					@"prefLanguage",
							  nil];
	
	// Load default userDefaults
    [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}

- (void)awakeFromNib {
	filesToOpenAtStart = nil;
	wc = nil;

    if (@available(macOS 10.9, *)) {
        subscriptionManager = [SubscriptionManager shared];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeExternally) name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:[NSUbiquitousKeyValueStore defaultStore]];

        [[NSUbiquitousKeyValueStore defaultStore] synchronize];
        [subscriptionManager parseSubscriptions];

        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    }
    else {
        subscriptionManager=nil;
    }
}

- (void)dealloc {
    if (@available(macOS 10.9, *)) {
        [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
    }

	[filesToOpenAtStart release];
	[wc release];
	[super dealloc];
}

- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames {
	
	//open multiple files at once
	//this is called before applicationDidFinishLaunching: at startup

	//convert to array of NSURLs
	NSMutableArray *a = [NSMutableArray array];
	NSUInteger i, max = [filenames count];
	for(i=0; i<max; i++) {
		[a addObject:[NSURL fileURLWithPath:[filenames objectAtIndex:i]]];
	}
	
	[self doWorkWithFiles:a];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	// Insert code here to initialize your application 
	
	[NSApp setServicesProvider:self];
	
	//get and set MI language contents
	NSString *langFile = [[NSBundle mainBundle] pathForResource:@"lang" ofType:@"csv"];
	NSString *langContents = [NSString stringWithContentsOfFile:langFile encoding:NSUTF8StringEncoding error:nil];
	if(langContents) {
		[oMediaInfoList setLanguageWithContents:langContents];
	}

}

- (void)applicationWillBecomeActive:(NSNotification *)aNotification {
	//called every time the app will in focus
	
	if(!wc) {
		wc = [[MyWindowController alloc] initWithWindowNibName:@"MyWindow"];
		[[wc window] registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType,nil]];
		[[wc window] makeKeyAndOrderFront:self];

        if (@available(macOS 10.9, *)) {
            if(subscriptionManager.shouldNotifyUserForSubscriptionEnd) {
                NSAlert *alert = [
                    NSAlert alertWithMessageText:NSLocalizedString(@"Your subscription has just ended.", @"Subscription Ended") defaultButton:NSLocalizedString(@"Renew", @"Renew") alternateButton:NSLocalizedString(@"Close", @"Close") otherButton:nil informativeTextWithFormat:NSLocalizedString(@"Renew subscription?", @"Renew subscription?")];
                if([alert runModal] == NSModalResponseOK) {
                    [self openSubscribePanel:nil];
                }
                [subscriptionManager userNotifiedForSubscriptionEnd];
            }

            if(subscriptionManager.shouldNotifyUserForSubscription) {
                [self openSubscribePanel:nil];
            }
        }

		if(filesToOpenAtStart) {
			[wc processFiles:filesToOpenAtStart];
		}
	}
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
	return YES;
}

/*
- (IBAction)openFile:(id)sender {

	NSOpenPanel *openPanel	= [NSOpenPanel openPanel];
	[openPanel setCanChooseDirectories:YES];
	[openPanel setAllowsMultipleSelection:YES];

	NSInteger retVal = [openPanel runModal];

	if(retVal != NSFileHandlingPanelOKButton){
     	return;
	} 
	
	//create new window and do some processing...
	
}
 */


-(void)analyzeService:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error {
	
	if([[pboard types] containsObject:NSFilenamesPboardType]) {
		
		NSArray *a = [pboard propertyListForType:NSFilenamesPboardType];
		
		NSMutableArray *r = [NSMutableArray array];
		for(NSString *path in a) {
			[r addObject:[NSURL fileURLWithPath:path]];
		}
		
		[self doWorkWithFiles:r];
		
	}
	else if([[pboard types] containsObject:NSStringPboardType]) {
		
		NSArray *classes = [NSArray arrayWithObject:[NSString class]];
		NSDictionary *options = [NSDictionary dictionary];
		
		if([pboard canReadObjectForClasses:classes options:options]) {
			
			NSString *str =  [pboard stringForType:NSStringPboardType];
			
			[self doWorkWithFiles:[NSArray arrayWithObject:[NSURL fileURLWithPath:str]]];

		}
	}
}



-(void)doWorkWithFiles:(NSArray*)array {

	if(wc) {
		[wc processFiles:array];
	}
	else {
		filesToOpenAtStart = [[NSArray alloc] initWithArray:array];
	}

}

- (IBAction)openAboutPanel:(id)sender {
	[[AboutWindowController controller] show];
}

- (IBAction)openSubscribePanel:(id)sender {
    [[SubscribeWindowController controller] show];
}

- (IBAction)openSubscriptionReportPanel:(id)sender {
    [[PurchaseReportController controller] show];
}

- (IBAction)clickAuthorWebsite:(id)sender {
	[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://mediainfo.sourceforge.net/"]];
}

- (IBAction)openPreferences:(id)sender {
	[[PreferencesWindowController controller] show];	
}

-(void)didChangeExternally {
    if(!subscriptionManager)
        return;

    [subscriptionManager parseSubscriptions];
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
    if(!subscriptionManager)
        return;

    for(SKPaymentTransaction *transaction in transactions) {
        switch ([transaction transactionState]) {
            case SKPaymentTransactionStatePurchasing:
                break;
            case SKPaymentTransactionStatePurchased:
                [subscriptionManager purchaseSucceeded:transaction];
                [queue finishTransaction:transaction];
                break;
            case SKPaymentTransactionStateDeferred:
                [subscriptionManager purchaseDeferred:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [subscriptionManager purchaseRestored:transaction];
                [queue finishTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [subscriptionManager purchaseFailed:transaction];
                [queue finishTransaction:transaction];
                break;
            default:
                break;
        }
    }
}

@end