VLCOneDriveObject.m 5.09 KB
Newer Older
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
/*****************************************************************************
 * VLCOneDriveObject.h
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCOneDriveObject.h"

@implementation VLCOneDriveObject

#pragma mark properties

- (BOOL)isFolder
{
    return [self.type isEqual:@"folder"] || [self.type isEqual:@"album"];
}

- (NSString *)filesPath
{
    return [self.objectId stringByAppendingString:@"/files"];
}

- (BOOL)hasFullFolderTree
{
    BOOL hasFullTree = YES;

    if (self.folders != nil) {
        NSUInteger count = self.folders.count;

        for (NSUInteger x = 0; x < count; x++) {
            VLCOneDriveObject *folder = self.folders[x];
            if (!folder.hasFullFolderTree) {
                hasFullTree = NO;
                break;
            }
        }
    } else
        hasFullTree = NO;

    return hasFullTree;
}

#pragma mark - actions

- (void)loadFolderContent
{
    NSLog(@"loadFolderContent");
    if (!self.isFolder) {
        APLog(@"%@ is no folder, can't load content", self.objectId);
        return;
    }

    if (self.folders == nil) {
        [self.liveClient getWithPath:self.filesPath
                            delegate:self
                           userState:@"load-folder-content"];
    } else {
        NSUInteger count = self.folders.count;

        for (NSUInteger x = 0; x < count; x++) {
            VLCOneDriveObject *folder = self.folders[x];
            if (!folder.hasFullFolderTree) {
                [folder loadFolderContent];
                return;
            }
        }

        [self.delegate fullFolderTreeLoaded:self];
    }
}

- (void)loadFileContent
{
}

#pragma mark - live operations

- (void)liveOperationSucceeded:(LiveDownloadOperation *)operation
{
    NSString *userState = operation.userState;

    NSLog(@"liveOperationSucceeded: %@", userState);

    if ([userState isEqualToString:@"load-file-content"]) {
//        LiveDownloadOperation *downloadOperation = (LiveDownloadOperation *)operation;

        //FIXME: handle the incoming data!

        [self.delegate fileContentLoaded:self];
    } else if ([userState isEqualToString:@"load-folder-content"]) {
        NSMutableArray *subFolders = [[NSMutableArray alloc] init];
        NSMutableArray *folderFiles = [[NSMutableArray alloc] init];
        NSMutableArray *items = [[NSMutableArray alloc] init];
        NSArray *rawFolderObjects = operation.result[@"data"];
        BOOL hasSubFolders = NO;
        NSUInteger count = rawFolderObjects.count;

        for (NSUInteger x = 0; x < count; x++) {
            NSDictionary *rawObject = rawFolderObjects[x];
            VLCOneDriveObject *oneDriveObject = [[VLCOneDriveObject alloc] init];

            oneDriveObject.parent = self;
            oneDriveObject.objectId = rawObject[@"id"];
            oneDriveObject.name = rawObject[@"name"];
            oneDriveObject.type = rawObject[@"type"];

            oneDriveObject.liveClient = self.liveClient;

            if (oneDriveObject.isFolder) {
                hasSubFolders = YES;
                [subFolders addObject:oneDriveObject];
            } else {
                oneDriveObject.size = rawObject[@"size"];
                oneDriveObject.thumbnailURL = rawObject[@"picture"];
121
122
                oneDriveObject.downloadPath = rawObject[@"source"];
                oneDriveObject.duration = rawObject[@"duration"];
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
                [folderFiles addObject:oneDriveObject];
            }
            [items addObject:oneDriveObject];
        }

        NSLog(@"we found %i items", items.count);
        for (NSUInteger x = 0; x < items.count; x++)
            NSLog(@"%@", [items[x] name]);

        self.folders = subFolders;
        self.files = folderFiles;
        self.items = items;
        [self.delegate folderContentLoaded:self];
    }
}

- (void)liveOperationFailed:(NSError *)error operation:(LiveDownloadOperation *)operation
{
    NSString *userState = operation.userState;

    NSLog(@"liveOperationFailed %@ (%@)", userState, error);

    if ([userState isEqualToString:@"load-folder-content"])
        [self.delegate folderContentLoadingFailed:error sender:self];
    else if ([userState isEqualToString:@"load-file-content"])
        [self.delegate fileContentLoadingFailed:error sender:self];
    else
        APLog(@"failing live operation with state %@ failed with error %@", userState, error);
}

#pragma mark - delegation

- (void)folderContentLoaded:(VLCOneDriveObject *)sender
{
}

- (void)folderContentLoadingFailed:(NSError *)error sender:(VLCOneDriveObject *)sender
{
    if (self.delegate)
        [self.delegate folderContentLoadingFailed:error sender:self];
}

- (void)fullFolderTreeLoaded:(VLCOneDriveObject *)sender
{
    [self loadFolderContent];
}

- (void)fileContentLoaded:(VLCOneDriveObject *)sender
{
}

- (void)fileContentLoadingFailed:(NSError *)error sender:(VLCOneDriveObject *)sender
{
}

@end