VLCAddMediaViewController.m 9.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
//
//  VLCAddMediaViewController.m
//  VLC for iOS
//
//  Created by Felix Paul Kühne on 19.05.13.
//  Copyright (c) 2013 VideoLAN. All rights reserved.
//

#import "VLCAddMediaViewController.h"
#import "VLCAppDelegate.h"
#import "VLCPlaylistViewController.h"
#import "VLCAboutViewController.h"
#import "VLCMovieViewController.h"
14
#import "VLCHTTPUploaderController.h"
15
#import "VLCSettingsController.h"
16
#import "HTTPServer.h"
17
#import "Reachability.h"
18
#import "VLCHTTPFileDownloader.h"
19
#import "IASKAppSettingsViewController.h"
20
21
22

#import <ifaddrs.h>
#import <arpa/inet.h>
23

24
25
@interface VLCAddMediaViewController () {
    VLCHTTPUploaderController *_uploadController;
26
    Reachability *_reachability;
27
    VLCHTTPFileDownloader *_httpDownloader;
28
}
29
30
31
32
33
34
35
36
37
38
39
40

@end

@implementation VLCAddMediaViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    return self;
}

41
42
43
44
45
46
- (void)dealloc
{
    [_reachability stopNotifier];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

47
48
49
50
51
- (void)viewDidLoad
{
    [super viewDidLoad];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
52
53
54
55
56
        [self.dismissButton setTitle:NSLocalizedString(@"BUTTON_DONE", @"") forState:UIControlStateNormal];
    [self.aboutButton setTitle:NSLocalizedString(@"ABOUT_APP", @"") forState:UIControlStateNormal];
    [self.openNetworkStreamButton setTitle:NSLocalizedString(@"OPEN_NETWORK", @"") forState:UIControlStateNormal];
    [self.downloadFromHTTPServerButton setTitle:NSLocalizedString(@"DOWNLOAD_FROM_HTTP", @"") forState:UIControlStateNormal];
    [self.openURLButton setTitle:NSLocalizedString(@"BUTTON_OPEN", @"") forState:UIControlStateNormal];
57
    self.httpUploadLabel.text = NSLocalizedString(@"HTTP_UPLOAD", @"");
Felix Paul Kühne's avatar
Felix Paul Kühne committed
58
    [self.settingsButton setTitle:NSLocalizedString(@"Settings", @"") forState:UIControlStateNormal]; // plain text key to keep compatibility with InAppSettingsKit's upstream
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    _reachability = [Reachability reachabilityForLocalWiFi];
    [_reachability startNotifier];

    [self netReachabilityChanged:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachabilityChanged:) name:kReachabilityChangedNotification object:nil];
}

- (void)netReachabilityChanged:(NSNotification *)notification
{
    if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
        self.httpUploadServerSwitch.enabled = YES;
        self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
    } else {
        self.httpUploadServerSwitch.enabled = NO;
        self.httpUploadServerSwitch.on = NO;
        self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", @"");
    }
77
78
79
80
}

- (void)viewWillAppear:(BOOL)animated
{
81
82
83
    [self.openURLButton sizeToFit];
    if (self.openURLView.superview)
        [self.openURLView removeFromSuperview];
84

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    [super viewWillAppear:animated];
}

- (void)_hideAnimated:(BOOL)animated
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
        [appDelegate.playlistViewController.addMediaPopoverController dismissPopoverAnimated:YES];
    } else
        [self dismissViewControllerAnimated:animated completion:NULL];
}

- (IBAction)dismiss:(id)sender
{
    [self _hideAnimated:YES];
}

102
- (IBAction)openAboutPanel:(id)sender
103
104
105
106
107
108
109
110
111
112
{
    VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;

    if (!appDelegate.playlistViewController.aboutViewController)
        appDelegate.playlistViewController.aboutViewController = [[VLCAboutViewController alloc] initWithNibName:@"VLCAboutViewController" bundle:nil];
    [appDelegate.playlistViewController.navigationController pushViewController:appDelegate.playlistViewController.aboutViewController animated:YES];

    [self _hideAnimated:NO];
}

113
- (IBAction)openNetworkStream:(id)sender
114
{
115
    if (sender == self.openNetworkStreamButton) {
116
        if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
117
118
119
120
121
122
            NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
            if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
                NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
                pasteURL = [NSURL URLWithString:pasteString];
            }

123
            if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
124
                self.openURLField.text = [pasteURL absoluteString];
125
        }
126
127
        if (self.openURLView.superview)
            [self.openURLView removeFromSuperview];
128
129
        [self.openURLButton removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
        [self.openURLButton addTarget:self action:@selector(openNetworkStream:) forControlEvents:UIControlEventTouchUpInside];
130
        [self.openNetworkStreamButton addSubview:self.openURLView];
131
    } else {
132
        VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
133
134
        [appDelegate.playlistViewController openMovieFromURL:[NSURL URLWithString:self.openURLField.text]];
        [self _hideAnimated:YES];
135
136
137
    }
}

138
- (IBAction)downloadFromHTTPServer:(id)sender
139
{
140
141
142
143
144
    if (_httpDownloader) {
        if (_httpDownloader.downloadInProgress)
            return;
    }

145
146
147
148
149
150
151
152
153
154
155
156
157
    if (sender == self.downloadFromHTTPServerButton) {
        if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
            NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
            if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
                NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
                pasteURL = [NSURL URLWithString:pasteString];
            }

            if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
                self.openURLField.text = [pasteURL absoluteString];
        }
        if (self.openURLView.superview)
            [self.openURLView removeFromSuperview];
158
159
        [self.openURLButton removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
        [self.openURLButton addTarget:self action:@selector(downloadFromHTTPServer:) forControlEvents:UIControlEventTouchUpInside];
160
161
162
        [self.downloadFromHTTPServerButton addSubview:self.openURLView];
    } else {
        NSURL *URLtoSave = [NSURL URLWithString:self.openURLField.text];
163
164
165
166
167
168
169
170
171
172
173
        if (([URLtoSave.scheme isEqualToString:@"http"] || [URLtoSave.scheme isEqualToString:@"https"]) && ![URLtoSave.lastPathComponent.pathExtension isEqualToString:@""]) {
            if (!_httpDownloader) {
                _httpDownloader = [[VLCHTTPFileDownloader alloc] init];
                _httpDownloader.mediaViewController = self;
            }
            [_httpDownloader downloadFileFromURL:URLtoSave];
            [self.openURLView removeFromSuperview];
        } else {
            APLog(@"URL is not a file download");
            [self _hideAnimated:YES];
        }
174
    }
175
176
}

177
178
- (IBAction)showSettings:(id)sender
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
179
    if (!self.settingsViewController)
180
        self.settingsViewController = [[IASKAppSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
181

182
183
    if (!self.settingsController)
        self.settingsController = [[VLCSettingsController alloc] init];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
184
185

    self.settingsViewController.modalPresentationStyle = UIModalPresentationFormSheet;
186
187
188
189
190
191
192
193
194
195
    self.settingsViewController.delegate = self.settingsController;
    self.settingsViewController.showDoneButton = YES;
    self.settingsViewController.showCreditsFooter = NO;

    self.settingsController.viewController = self.settingsViewController;

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.settingsViewController];
    navController.navigationBarHidden = NO;
    navController.navigationBar.barStyle = UIBarStyleBlack;
    [self presentModalViewController:navController animated:YES];
196
197
}

198
- (NSString *)_currentIPAddress
199
{
200
201
202
203
204
205
206
207
    NSString *address = @"";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = getifaddrs(&interfaces);
    if (success == 0) {
        temp_addr = interfaces;
        while(temp_addr != NULL) {
            if(temp_addr->ifa_addr->sa_family == AF_INET) {
208
209
                if([@(temp_addr->ifa_name) isEqualToString:@"en0"])
                    address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
210
211
212
213
214
215
216
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    // Free memory
    freeifaddrs(interfaces);
    return address;
217
218
}

219
- (IBAction)toggleHTTPServer:(UISwitch *)sender
220
{
221
222
    _uploadController = [[VLCHTTPUploaderController alloc] init];
    [_uploadController changeHTTPServerState: sender.on];
223
224
225
226
227
228

    HTTPServer *server = _uploadController.httpServer;
    if (server.isRunning)
        self.httpUploadServerLocationLabel.text = [NSString stringWithFormat:@"http://%@:%i", [self _currentIPAddress], server.listeningPort];
    else
        self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
229
230
}

231
@end