-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathArxivVersionCheckingOperation.m
90 lines (79 loc) · 2.71 KB
/
ArxivVersionCheckingOperation.m
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
//
// ArxivVersionCheckingOperation.m
// spires
//
// Created by Yuji on 09/02/07.
// Copyright 2009 Y. Tachikawa. All rights reserved.
//
#import "ArxivVersionCheckingOperation.h"
#import "Article.h"
#import "ArxivPDFDownloadOperation.h"
#import "DeferredPDFOpenOperation.h"
#import "NSString+magic.h"
#import "AppDelegate.h"
#import "PDFHelper.h"
// #import <Quartz/Quartz.h>
#if TARGET_OS_IPHONE
#define NSAlert NSString
#define NSAlertFirstButtonReturn 0
typedef NSInteger NSModalResponse;
#endif
@implementation ArxivVersionCheckingOperation
-(ArxivVersionCheckingOperation*)initWithArticle:(Article*)a usingViewer:(PDFViewerType)t;
{
self=[super init];
article=a;
type=t;
return self;
}
-(NSString*)description
{
return [NSString stringWithFormat:@"version checking: %@", article.eprint];
}
-(void)run
{
self.isExecuting=YES;
if(article.version==nil || [article.version intValue]==0 || !article.hasPDFLocally){
[self finish];
return;
}
int v=[[PDFHelper sharedHelper] tryToDetermineVersionFromPDF:article.pdfPath];
if(v==[article.version intValue] || v==0){
[self finish];
return;
}
#if TARGET_OS_IPHONE
[self downloadAlertDidEnd:nil code:NSAlertFirstButtonReturn];
#else
NSString*commentsLine=@"";
if(article.comments && ![article.comments isEqualToString:@""]){
commentsLine=[NSString stringWithFormat:@"Comments: %@",article.comments];
}
NSAlert *alert = [[NSAlert alloc] init];
alert.messageText=[NSString stringWithFormat:@"A new version of %@ has been found.",article.eprint];
[alert addButtonWithTitle:@"Download"];
[alert addButtonWithTitle:@"Cancel"];
alert.informativeText=[NSString stringWithFormat:@"Your PDF is version %d, which is older than the latest version %d on the web.\n%@",
v,[article.version intValue],commentsLine];
[alert setAlertStyle:NSAlertStyleWarning];
// [NSApp unhide:self];
[alert beginSheetModalForWindow:[[NSApp appDelegate] mainWindow]
completionHandler:^(NSModalResponse choice) {
[self downloadAlertDidEnd:alert code:choice];
}
];
#endif
}
-(void)downloadAlertDidEnd:(NSAlert*)alert code:(NSModalResponse)choice
{
if(choice==NSAlertFirstButtonReturn){
NSOperation*downloadOp=[[ArxivPDFDownloadOperation alloc] initWithArticle:article shouldAsk:NO];
NSOperation*openOp=[[DeferredPDFOpenOperation alloc] initWithArticle:article
usingViewer:type];
[openOp addDependency:downloadOp];
[[OperationQueues arxivQueue] addOperation:downloadOp];
[[OperationQueues sharedQueue] addOperation:openOp];
}
[self finish];
}
@end