aboutsummaryrefslogtreecommitdiff
path: root/VKPC/HostsHackWindowController.m
blob: 17da2487da29fada2c5fecaaa94ba4d4513570f8 (plain)
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
//
//  HostsHackWindowController.m
//  VKPC
//
//  Created by Eugene on 10/30/14.
//  Copyright (c) 2014 Eugene Z. All rights reserved.
//

#import "HostsHackWindowController.h"
#import "HostsHack.h"

// TODO rewrite using normal coords

@implementation HostsHackWindowController {
    NSMutableAttributedString *configurationRequired;
    NSView *contentView;
}

- (BOOL)allowsClosingWithShortcut {
    return YES;
}

- (void)windowDidLoad {
    [super windowDidLoad];
    
    contentView = self.window.contentView;
    
    NSTextFieldCell *cell = (NSTextFieldCell *)_configurationRequiredTextField.cell;
    NSString *configurationTextHTML = [NSString stringWithFormat:
                                       @"<html><span style=\"font-family: %@;\">"
                                            "<span style=\"line-height: 10px; font-size: 14px\"><b>Welcome to VK Player Controller!</b></span>"
                                            "<span style=\"font-size: 6px\"><br/><br/></span>"
                                            "<span style=\"font-size: 13px\">"
                                                "Let's make one magic trick with system DNS settings, it's necessary for a proper work of VK Player Controller. Don't worry, <b>it's absolutely safe</b>. Please press <b>Continue</b> button below."
//                                                "For VK Player Controller to work it is necessary to do some hacking with DNS resolution configuration. Press <b>Continue</b> to continue."
                                            "</span>"
                                            "<span style=\"font-size: 7px\"><br/><br/></span>"
                                            "<span style=\"font-size: 12px; color: #707070;\">"
                                                "The app modifies the file <b>%@</b>. If you don't trust us, open that file manually with admin privileges, add this line: <b>127.0.0.1\t%@</b>, save it and relaunch the app."
                                            "</span>"
                                        "</span></html>",
                                        //GetSystemFontName(),
                                       @"Helvetica Neue",
                                       [NSString stringWithUTF8String:VKPCHostsFile],
                                       [NSString stringWithUTF8String:VKPCWSClientHost]];
    
    configurationRequired = [[NSMutableAttributedString alloc] initWithHTML:[configurationTextHTML dataUsingEncoding:NSUTF8StringEncoding]
                                                                                               options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                                                         NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                                                                    documentAttributes:nil];
    
    [_configurationRequiredTextField setAttributedStringValue:configurationRequired];
    [cell setWraps:YES];
    
    // Position text
    NSRect textFrame = _configurationRequiredTextField.frame;
    float textHeight = [configurationRequired boundingRectWithSize:CGSizeMake(textFrame.size.width, FLT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading].size.height;
    textFrame.origin.y += textFrame.size.height - textHeight;
    textFrame.size.height = textHeight;
    [_configurationRequiredTextField setFrame:textFrame];

    // Position button
    NSRect buttonFrame = _button.frame;
    float padding = contentView.frame.size.height - (textFrame.origin.y + textFrame.size.height);
    float buttonPadding = 20;
    float buttonY = [self.window.contentView frame].size.height - textHeight - padding * 2 - buttonFrame.size.height;
    buttonFrame.origin.y = buttonY;
    [_button setFrame:buttonFrame];
    
    float windowHeight = contentView.frame.size.height - buttonFrame.origin.y + padding + buttonPadding;
    NSRect windowFrame = self.window.frame;
    windowFrame.origin.y += windowFrame.size.height;
    windowFrame.origin.y -= windowHeight;
    windowFrame.size.height = windowHeight;
    [self.window setFrame:windowFrame display:YES];
}

- (void)setButtonContinue {
    [_button setTitle:@"Continue"];
    [_button setEnabled:YES];
}

- (void)setButtonRetry {
    [_button setTitle:@"Retry"];
    [_button setEnabled:YES];
}

- (void)setButtonWait {
    [_button setTitle:@"Please wait.."];
    [_button setEnabled:NO];
}

- (IBAction)buttonPressed:(id)sender {
    [HostsHack hack];
}

- (void)showWindow:(id)sender {
    [self setButtonContinue];
    
    [super showWindow:sender];
    
    [self.window setDefaultButtonCell:_button.cell];
}

@end