Skip to content

Commit aa5547d

Browse files
committed
- OCDrive: add OCDriveResource enum and values
- OCCore: - add space resource folder path config - add new methods to retrieve and set items used as a drive resource
1 parent b81a291 commit aa5547d

File tree

7 files changed

+135
-5
lines changed

7 files changed

+135
-5
lines changed

ownCloudSDK/Connection/OCConnection+Spaces.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ - (nullable NSProgress *)updateDrive:(OCDrive *)drive properties:(NSDictionary<O
124124
}]);
125125
}
126126

127-
- (NSProgress *)updateDrive:(OCDrive *)drive resourceFor:(OCDataItemPresentableResource)resource withItem:(nullable OCItem *)item completionHandler:(void(^)(NSError * _Nullable error, OCDrive * _Nullable drive))completionHandler
127+
- (NSProgress *)updateDrive:(OCDrive *)drive resourceFor:(OCDriveResource)resource withItem:(nullable OCItem *)item completionHandler:(void(^)(NSError * _Nullable error, OCDrive * _Nullable drive))completionHandler
128128
{
129129
// Compose drive item
130130
GADriveItem *driveItem = [GADriveItem new];
131131

132132
// - set special folder name based on OCDataItemPresentableResource
133133
driveItem.specialFolder = [GASpecialFolder new];
134-
if ([resource isEqual:OCDataItemPresentableResourceCoverImage]) {
134+
if ([resource isEqual:OCDriveResourceCoverImage]) {
135135
driveItem.specialFolder.name = GASpecialFolderNameImage;
136-
} else if ([resource isEqual:OCDataItemPresentableResourceCoverDescription]) {
136+
} else if ([resource isEqual:OCDriveResourceCoverDescription]) {
137137
driveItem.specialFolder.name = GASpecialFolderNameReadme;
138138
} else {
139139
// Unknown/unsupported resource type -> return error

ownCloudSDK/Connection/OCConnection.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ typedef void(^OCConnectionDriveManagementCompletionHandler)(NSError * _Nullable
368368

369369
#pragma mark - Change attributes
370370
- (nullable NSProgress *)updateDrive:(OCDrive *)drive properties:(NSDictionary<OCDriveProperty, id> *)updateProperties completionHandler:(OCConnectionDriveCompletionHandler)completionHandler;
371-
- (nullable NSProgress *)updateDrive:(OCDrive *)drive resourceFor:(OCDataItemPresentableResource)resource withItem:(nullable OCItem *)item completionHandler:(void(^)(NSError * _Nullable error, OCDrive * _Nullable drive))completionHandler; //!< Updates special items of a drive with the provided item.
371+
- (nullable NSProgress *)updateDrive:(OCDrive *)drive resourceFor:(OCDriveResource)resource withItem:(nullable OCItem *)item completionHandler:(void(^)(NSError * _Nullable error, OCDrive * _Nullable drive))completionHandler; //!< Updates special items of a drive with the provided item.
372372

373373
@end
374374

ownCloudSDK/Core/DriveManagement/OCCore+DriveManagement.m

+114
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
#import "OCCore.h"
2020
#import "OCCore+ItemList.h"
21+
#import "OCDataRenderer.h"
22+
#import "NSProgress+OCExtensions.h"
23+
#import "GADrive.h"
24+
#import "GADriveItem.h"
25+
#import "NSError+OCError.h"
2126

2227
@implementation OCCore (DriveManagement)
2328

@@ -74,4 +79,113 @@ - (nullable NSProgress *)updateDrive:(OCDrive *)drive properties:(NSDictionary<O
7479
}]);
7580
}
7681

82+
- (void)retrieveDrive:(OCDrive *)drive itemForResource:(OCDriveResource)resource completionHandler:(void(^)(NSError * _Nullable error, OCItem * _Nullable item))completionHandler
83+
{
84+
NSError *error = nil;
85+
GADriveItem *driveItem = nil;
86+
87+
if ([resource isEqual:OCDriveResourceCoverImage]) {
88+
// Use special "image" drive item from GADrive
89+
driveItem = [drive.gaDrive specialDriveItemFor:GASpecialFolderNameImage];
90+
} else if ([resource isEqual:OCDriveResourceCoverDescription]) {
91+
// Use special "readme" drive item from GADrive
92+
driveItem = [drive.gaDrive specialDriveItemFor:GASpecialFolderNameReadme];
93+
} else if ([resource isEqual:OCDriveResourceSpaceFolder]) {
94+
// Use APIs to determine .space folder and find an item for it, creating it if needed
95+
OCPath spacesFolderPath;
96+
if ((spacesFolderPath = [self classSettingForOCClassSettingsKey:OCCoreSpaceResourceFolderPath]) != nil)
97+
{
98+
OCLocation *spacesFolderLocation = [[OCLocation alloc] initWithDriveID:drive.identifier path:[NSString stringWithFormat:@"/%@/", spacesFolderPath]]; // Add leading and trailing / to spaces folder path
99+
__block OCCoreItemTracking itemTracking;
100+
__weak OCCore *weakCore = self;
101+
dispatch_queue_t queue = _queue;
102+
itemTracking = [self trackItemAtLocation:spacesFolderLocation trackingHandler:^(NSError * _Nullable error, OCItem * _Nullable item, BOOL isInitial) {
103+
if (isInitial)
104+
{
105+
dispatch_async(queue, ^{
106+
// End tracking
107+
itemTracking = nil;
108+
109+
// Check for errors
110+
if (error != nil)
111+
{
112+
completionHandler(error, nil);
113+
return;
114+
}
115+
116+
// Folder found -> return
117+
if (item != nil)
118+
{
119+
completionHandler(nil, item);
120+
return;
121+
}
122+
123+
// Folder not found -> create it
124+
OCCore *strongCore;
125+
126+
if ((strongCore = weakCore) == nil)
127+
{
128+
completionHandler(OCError(OCErrorInternal), nil);
129+
return;
130+
}
131+
132+
[strongCore.vault.database retrieveCacheItemsAtLocation:drive.rootLocation itemOnly:YES completionHandler:^(OCDatabase *db, NSError *error, OCSyncAnchor syncAnchor, NSArray<OCItem *> *items) {
133+
// Determine root item
134+
if (error != nil)
135+
{
136+
completionHandler(error, nil);
137+
return;
138+
}
139+
140+
OCItem *rootItem = items.firstObject;
141+
if (rootItem != nil)
142+
{
143+
// Create folder
144+
OCCore *strongCore;
145+
if ((strongCore = weakCore) == nil)
146+
{
147+
completionHandler(OCError(OCErrorInternal), nil);
148+
return;
149+
}
150+
151+
[strongCore createFolder:spacesFolderPath inside:rootItem options:nil resultHandler:^(NSError * _Nullable error, OCCore * _Nonnull core, OCItem * _Nullable item, id _Nullable parameter) {
152+
// Return created folder result
153+
completionHandler(error, item);
154+
}];
155+
}
156+
}];
157+
});
158+
}
159+
}];
160+
}
161+
return;
162+
} else {
163+
// Unknown/unsupported resource type -> return error
164+
completionHandler(OCError(OCErrorInvalidParameter), nil);
165+
}
166+
167+
if ((driveItem != nil) && (driveItem.identifier != nil))
168+
{
169+
// GADriveItem.id is the FileID of the item, so fetch it from the database
170+
[self.vault.database retrieveCacheItemForFileID:driveItem.identifier completionHandler:^(OCDatabase *db, NSError *error, OCSyncAnchor syncAnchor, OCItem *item) {
171+
completionHandler(error, item);
172+
}];
173+
}
174+
else
175+
{
176+
// No drive item of type found for drive
177+
completionHandler(OCError(OCErrorResourceNotFound), nil);
178+
}
179+
}
180+
181+
- (nullable NSProgress *)updateDrive:(OCDrive *)drive resourceFor:(OCDriveResource)resource withItem:(nullable OCItem *)item completionHandler:(void(^)(NSError * _Nullable error, OCDrive * _Nullable drive))completionHandler
182+
{
183+
return ([self.connection updateDrive:drive resourceFor:resource withItem:item completionHandler:^(NSError * _Nullable error, OCDrive * _Nullable drive) {
184+
if (error == nil) {
185+
[self fetchUpdatesWithCompletionHandler:nil];
186+
}
187+
completionHandler(error, drive);
188+
}]);
189+
}
190+
77191
@end

ownCloudSDK/Core/OCCore.h

+4
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@ typedef void(^OCCoreShareRoleRetrievalHandler)(NSError * _Nullable error, NSArra
526526
// Change attributes
527527
- (nullable NSProgress *)updateDrive:(OCDrive *)drive properties:(NSDictionary<OCDriveProperty, id> *)updateProperties completionHandler:(OCCoreDriveCompletionHandler)completionHandler;
528528

529+
- (void)retrieveDrive:(OCDrive *)drive itemForResource:(OCDriveResource)resource completionHandler:(void(^)(NSError * _Nullable error, OCItem * _Nullable item))completionHandler;
530+
- (nullable NSProgress *)updateDrive:(OCDrive *)drive resourceFor:(OCDriveResource)resource withItem:(nullable OCItem *)item completionHandler:(void(^)(NSError * _Nullable error, OCDrive * _Nullable drive))completionHandler;
531+
529532
@end
530533

531534
extern OCClassSettingsKey OCCoreAddAcceptLanguageHeader;
@@ -535,6 +538,7 @@ extern OCClassSettingsKey OCCoreOverrideAvailabilitySignal;
535538
extern OCClassSettingsKey OCCoreActionConcurrencyBudgets;
536539
extern OCClassSettingsKey OCCoreCookieSupportEnabled;
537540
extern OCClassSettingsKey OCCoreScanForChangesInterval;
541+
extern OCClassSettingsKey OCCoreSpaceResourceFolderPath;
538542

539543
extern OCDatabaseCounterIdentifier OCCoreSyncAnchorCounter;
540544
extern OCDatabaseCounterIdentifier OCCoreSyncJournalCounter;

ownCloudSDK/Core/OCCore.m

+3-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ + (OCClassSettingsIdentifier)classSettingsIdentifier
166166
OCSyncActionCategoryDownloadWifiOnly : @(2), // Limit number of concurrent downloads by WiFi-only transfers to 2 (leaving at least one spot empty for cellular)
167167
OCSyncActionCategoryDownloadWifiAndCellular : @(3) // Limit number of concurrent downloads by WiFi and Cellular transfers to 3
168168
},
169-
OCCoreCookieSupportEnabled : @(YES)
169+
OCCoreCookieSupportEnabled : @(YES),
170+
OCCoreSpaceResourceFolderPath : @".space"
170171
});
171172
}
172173

@@ -2540,6 +2541,7 @@ + (void)initialize
25402541
OCClassSettingsKey OCCoreActionConcurrencyBudgets = @"action-concurrency-budgets";
25412542
OCClassSettingsKey OCCoreCookieSupportEnabled = @"cookie-support-enabled";
25422543
OCClassSettingsKey OCCoreScanForChangesInterval = @"scan-for-changes-interval";
2544+
OCClassSettingsKey OCCoreSpaceResourceFolderPath = @"space-resource-folder-path";
25432545

25442546
OCDatabaseCounterIdentifier OCCoreSyncAnchorCounter = @"syncAnchor";
25452547
OCDatabaseCounterIdentifier OCCoreSyncJournalCounter = @"syncJournal";

ownCloudSDK/Drive/OCDrive.h

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ typedef NSString* OCDriveSpecialType NS_TYPED_ENUM;
3535

3636
typedef NSString* OCDriveProperty NS_TYPED_ENUM;
3737

38+
typedef NSString* OCDriveResource NS_TYPED_ENUM;
39+
3840
typedef NS_ENUM(NSInteger, OCDriveDetachedState)
3941
{
4042
OCDriveDetachedStateNone,
@@ -100,6 +102,10 @@ extern OCDriveProperty OCDrivePropertyQuotaTotal; //!< The quota total of / spac
100102

101103
extern OCDriveID OCDriveIDSharesJail; //!< The static UUID of the Shares Jail
102104

105+
extern OCDriveResource OCDriveResourceCoverImage; //!< The cover image of a drive/space
106+
extern OCDriveResource OCDriveResourceCoverDescription; //!< The MD description of a drive/space
107+
extern OCDriveResource OCDriveResourceSpaceFolder; //!< The folder typically used to store a drive's/space's cover image and/or MD description.
108+
103109
#define OCDriveIDNil ((OCDriveID)NSNull.null)
104110
#define OCDriveIDWrap(driveID) ((OCDriveID)((driveID == nil) ? OCDriveIDNil : driveID))
105111
#define OCDriveIDUnwrap(driveID) ((OCDriveID)(((driveID!=nil) && [driveID isKindOfClass:NSNull.class]) ? nil : driveID))

ownCloudSDK/Drive/OCDrive.m

+4
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,8 @@ - (NSString *)description
309309
OCDriveProperty OCDrivePropertyDescription = @"desc"; // Must correspond to GADriveUpdate key path
310310
OCDriveProperty OCDrivePropertyQuotaTotal = @"quota.total"; // Must correspond to GADriveUpdate key path
311311

312+
OCDriveResource OCDriveResourceCoverImage = @"image";
313+
OCDriveResource OCDriveResourceCoverDescription = @"description";
314+
OCDriveResource OCDriveResourceSpaceFolder = @"space-folder";
315+
312316
OCDriveID OCDriveIDSharesJail = @"a0ca6a90-a365-4782-871e-d44447bbc668$a0ca6a90-a365-4782-871e-d44447bbc668";

0 commit comments

Comments
 (0)