Skip to content
This repository
tree: eb6e9483d2
Fetching contributors…

Cannot retrieve contributors at this time

file 613 lines (460 sloc) 25.028 kb
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 121 122 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
//
// ZPAttachmentCarouselDelegate.m
// ZotPad
//
// This class assumes that there is only one carousel that it serves
//
//
// Created by Mikko Rönkkö on 25.6.2012.
// Copyright (c) 2012 Mikko Rönkkö. All rights reserved.
//

#import "ZPAttachmentCarouselDelegate.h"
#import "ZPPreviewController.h"
#import "ZPServerConnection.h"
#import "ZPDataLayer.h"
#import "ZPAttachmentIconImageFactory.h"
#import <QuartzCore/QuartzCore.h>
#import <zlib.h>




NSInteger const ZPATTACHMENTICONGVIEWCONTROLLER_MODE_STATIC = 0;
NSInteger const ZPATTACHMENTICONGVIEWCONTROLLER_MODE_UPLOAD = 1;
NSInteger const ZPATTACHMENTICONGVIEWCONTROLLER_MODE_DOWNLOAD = 2;
NSInteger const ZPATTACHMENTICONGVIEWCONTROLLER_MODE_FIRST_STATIC_SECOND_DOWNLOAD = 3;

NSInteger const ZPATTACHMENTICONGVIEWCONTROLLER_SHOW_ORIGINAL = 10;
NSInteger const ZPATTACHMENTICONGVIEWCONTROLLER_SHOW_MODIFIED = 11;
NSInteger const ZPATTACHMENTICONGVIEWCONTROLLER_SHOW_FIRST_MODIFIED_SECOND_ORIGINAL = 12;


//The tags are negative, because iCarousel adds positive tags to the root views

NSInteger const ZPATTACHMENTICONGVIEWCONTROLLER_TAG_FILEIMAGE = -1;
NSInteger const ZPATTACHMENTICONGVIEWCONTROLLER_TAG_ERRORLABEL = -2;
NSInteger const ZPATTACHMENTICONGVIEWCONTROLLER_TAG_STATUSLABEL = -3;
NSInteger const ZPATTACHMENTICONGVIEWCONTROLLER_TAG_PROGRESSVIEW = -4;
NSInteger const ZPATTACHMENTICONGVIEWCONTROLLER_TAG_TITLELABEL = -5;


@interface ZPAttachmentCarouselDelegate(){
    NSMutableSet* _progressViews;
}

-(void) _toggleActionButtonState;
-(BOOL) _fileExistsForAttachment:(ZPZoteroAttachment*) attachment;
-(void) _configureFileImageView:(UIImageView*) imageView withAttachment:(ZPZoteroAttachment*)attachment;
-(void) _configureProgressLabel:(UILabel*) label withAttachment:(ZPZoteroAttachment*)attachment;
-(NSInteger) _modeForAttachment:(ZPZoteroAttachment*)attachment;
-(NSInteger) _showForAttachment:(ZPZoteroAttachment*)attachment;
-(void) _setLabelsForAttachment:(ZPZoteroAttachment*)attachment progressText:(NSString*)progressText errorText:(NSString*)errorText mode:(NSInteger)mode reconfigureIcon:(BOOL)reconfigureIcon;

@end


@implementation ZPAttachmentCarouselDelegate

@synthesize actionButton, attachmentCarousel, mode, show;

-(id) init{
    _progressViews = [[NSMutableSet alloc] init];
    
    self = [super init];

    //Register self as observer for item downloads
    [[ZPDataLayer instance] registerAttachmentObserver:self];
    [[ZPDataLayer instance] registerItemObserver:self];

    return self;
}

-(void) dealloc{
    // Removes the progress views from FileChannels
    @synchronized(_progressViews){
        for(UIProgressView* progressView in _progressViews){
            
        }
    }
    [[ZPDataLayer instance] removeItemObserver:self];
    [[ZPDataLayer instance] removeAttachmentObserver:self];
}



-(void) configureWithAttachmentArray:(NSArray*) attachments{
    _item = NULL;
    _attachments = attachments;
}
-(void) configureWithZoteroItem:(ZPZoteroItem*) item{
    _item = item;
    _attachments = item.attachments;
}

/*
Checks if the currently selected attachment has a file and enables or disables the activity buttone
*/


- (void) _toggleActionButtonState{
    if(actionButton != NULL){
        if([_attachments count]==0){
            self.actionButton.enabled = FALSE;
        }
        else{
            NSInteger currentIndex = attachmentCarousel.currentItemIndex;
            // Initially the iCarousel can return a negative index. This is probably a bug.
            ZPZoteroAttachment* attachment = [_attachments objectAtIndex:currentIndex];
            self.actionButton.enabled = [self _fileExistsForAttachment:attachment] &! [attachment.contentType isEqualToString:@"text/html"];
        }
    }
}

/*
This is needed because the different show-values that the carousel can have
*/
-(BOOL) _fileExistsForAttachment:(ZPZoteroAttachment*) attachment{
    if(show == ZPATTACHMENTICONGVIEWCONTROLLER_SHOW_ORIGINAL){
        return attachment.fileExists_original;
    }
    else return(attachment.fileExists);
}

#pragma mark - iCarousel delegate


- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel{
    NSAssert(carousel==self.attachmentCarousel,@"ZPAttachmentCarouselDelegate can only be used with the iCarousel set in the carousel property");
    return 0;
}

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    NSAssert(carousel==self.attachmentCarousel,@"ZPAttachmentCarouselDelegate can only be used with the iCarousel set in the carousel property");
    return [_attachments count];
}


- (NSUInteger) numberOfVisibleItemsInCarousel:(iCarousel*)carousel{
    NSAssert(carousel==self.attachmentCarousel,@"ZPAttachmentCarouselDelegate can only be used with the iCarousel set in the carousel property");
    NSInteger numItems = [self numberOfItemsInCarousel:carousel];
    NSInteger ret= MAX(numItems,5);
    return ret;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView*)view
{
    NSAssert(carousel==self.attachmentCarousel,@"ZPAttachmentCarouselDelegate can only be used with the iCarousel set in the carousel property");
  
    if(view != NULL && view.tag == index) return view;

    
    ZPZoteroAttachment* attachment = [_attachments objectAtIndex:index];
    

    UIImageView* fileImage;
    UIProgressView* progressView;
    UILabel* titleLabel;
    UILabel* progressLabel;
    UILabel* errorLabel;
    
    if(view==NULL){
        //Construct a blank view
        
        NSInteger height = carousel.frame.size.height*0.95;
        NSInteger width= height/1.4142;
        
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width,height)];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.borderWidth = 2.0f;
        view.tag = index;
        
        fileImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,(height-width)/2, width, width)];
        fileImage.tag = ZPATTACHMENTICONGVIEWCONTROLLER_TAG_FILEIMAGE;
        [view addSubview:fileImage];
        
        NSInteger labelHeight = height*2/5;
        NSInteger labelWidth = width*4/5;
        
        UIView* labelBackground = [[UIView alloc] initWithFrame:CGRectMake((width-labelWidth)/2, (height-labelHeight)/2, labelWidth, labelHeight)];
        labelBackground.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.5];
        labelBackground.layer.cornerRadius = 8;
        [view addSubview:labelBackground];
        
        NSInteger labelSubviewOffset = 10;
        NSInteger labelSubviewWidth = labelWidth-2*labelSubviewOffset;
        NSInteger labelSubviewHeight = labelHeight-2*labelSubviewOffset;

        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelSubviewOffset, labelSubviewOffset, labelSubviewWidth, labelSubviewHeight*.6)];
        titleLabel.numberOfLines = 4;
        titleLabel.tag = ZPATTACHMENTICONGVIEWCONTROLLER_TAG_TITLELABEL;
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor whiteColor];
        
        [labelBackground addSubview:titleLabel];
        
        progressLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelSubviewOffset, labelSubviewOffset + labelSubviewHeight*.6, labelSubviewWidth, labelSubviewHeight*.15)];
        progressLabel.tag = ZPATTACHMENTICONGVIEWCONTROLLER_TAG_STATUSLABEL;
        progressLabel.backgroundColor = [UIColor clearColor];
        progressLabel.textColor = [UIColor whiteColor];

        [labelBackground addSubview: progressLabel];
        
        progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(labelSubviewOffset, labelSubviewOffset + labelSubviewHeight*.75, labelSubviewWidth, labelSubviewHeight*.15)];
        progressView.tag = ZPATTACHMENTICONGVIEWCONTROLLER_TAG_PROGRESSVIEW;
        progressView.backgroundColor = [UIColor clearColor];
        
        [labelBackground addSubview: progressView];
        
        @synchronized(_progressViews){
            [_progressViews addObject:progressView];
        }
        
        errorLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelSubviewOffset, labelSubviewOffset + labelSubviewHeight*.75, labelSubviewWidth, labelSubviewHeight*.25)];
        errorLabel.tag = ZPATTACHMENTICONGVIEWCONTROLLER_TAG_ERRORLABEL;
        errorLabel.backgroundColor = [UIColor clearColor];
        errorLabel.textColor = [UIColor whiteColor];
        
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
            errorLabel.font = [UIFont systemFontOfSize:12];
        }
        else {
            errorLabel.font = [UIFont systemFontOfSize:10];
        }
        
        errorLabel.numberOfLines = 4;
        [labelBackground addSubview:errorLabel];
        
        
    }
    else{
        
        fileImage = (UIImageView*)[view viewWithTag:ZPATTACHMENTICONGVIEWCONTROLLER_TAG_FILEIMAGE];
        titleLabel = (UILabel*)[view viewWithTag:ZPATTACHMENTICONGVIEWCONTROLLER_TAG_TITLELABEL];
        progressLabel = (UILabel*)[view viewWithTag:ZPATTACHMENTICONGVIEWCONTROLLER_TAG_STATUSLABEL];
        progressView = (UIProgressView*)[view viewWithTag:ZPATTACHMENTICONGVIEWCONTROLLER_TAG_PROGRESSVIEW];
        errorLabel = (UILabel*)[view viewWithTag:ZPATTACHMENTICONGVIEWCONTROLLER_TAG_ERRORLABEL];
    }


    [self _configureFileImageView:fileImage withAttachment:attachment];
    [self _configureProgressLabel:progressLabel withAttachment:attachment];

    titleLabel.text = attachment.title;
    errorLabel.hidden = TRUE;
    
    //TODO: Recycle progressviews better using notifications
    progressView.hidden=TRUE;
    
    //Set the status
    //TODO: Refactor: This is not the right place for this call
    [self _toggleActionButtonState];
    
    return view;
}

-(NSInteger) _modeForAttachment:(ZPZoteroAttachment*)attachment{
    if(self.mode == ZPATTACHMENTICONGVIEWCONTROLLER_MODE_FIRST_STATIC_SECOND_DOWNLOAD){
        if([_attachments objectAtIndex:0] == attachment) return ZPATTACHMENTICONGVIEWCONTROLLER_MODE_STATIC;
        else return ZPATTACHMENTICONGVIEWCONTROLLER_MODE_DOWNLOAD;
    }
    else return self.mode;
}

-(NSInteger) _showForAttachment:(ZPZoteroAttachment*)attachment{
    if(self.mode == ZPATTACHMENTICONGVIEWCONTROLLER_SHOW_FIRST_MODIFIED_SECOND_ORIGINAL){
        if([_attachments objectAtIndex:0] == attachment) return ZPATTACHMENTICONGVIEWCONTROLLER_SHOW_MODIFIED;
        else return ZPATTACHMENTICONGVIEWCONTROLLER_SHOW_ORIGINAL;
    }
    else return self.show;
}


-(void) _configureFileImageView:(UIImageView*) imageView withAttachment:(ZPZoteroAttachment*)attachment{
    
    //TODO: Cache rendered PDF images

    if([attachment.contentType isEqualToString:@"application/pdf"] &&
        ([attachment.linkMode intValue] == LINK_MODE_IMPORTED_FILE ||
         [attachment.linkMode intValue] == LINK_MODE_IMPORTED_URL)){
        
        NSString* path;
        if([self _showForAttachment:attachment] == ZPATTACHMENTICONGVIEWCONTROLLER_SHOW_ORIGINAL){
            path =attachment.fileSystemPath_original;
        }
        else{
            path =attachment.fileSystemPath;
        }
        
        if([[NSFileManager defaultManager] fileExistsAtPath:path]){
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^{
                [ZPAttachmentIconImageFactory renderPDFPreviewForFileAtPath:path intoImageView:imageView];
            });
        }
        
    }
    
    // Assing a place holder icon if the current icon is null while we wait for the previews to render
    if(imageView.image == nil){
        [ZPAttachmentIconImageFactory renderFileTypeIconForAttachment:attachment intoImageView:imageView];
    }
}


-(void) _configureProgressLabel:(UILabel*) label withAttachment:(ZPZoteroAttachment*)attachment{

    NSInteger thisMode = [self _modeForAttachment:attachment];
    NSInteger thisShow = [self _showForAttachment:attachment];
                          
    if(thisMode == ZPATTACHMENTICONGVIEWCONTROLLER_MODE_DOWNLOAD){

        if([[ZPServerConnection instance] isAttachmentDownloading:attachment]){
            [self notifyAttachmentDownloadStarted:attachment];
        }
        else{
            
            //Imported files and URLs have files that can be downloaded
            
            NSInteger linkMode = [attachment.linkMode intValue ];
            BOOL exists;
            if(thisShow == ZPATTACHMENTICONGVIEWCONTROLLER_SHOW_ORIGINAL){
                exists = [attachment fileExists_original];
            }
            else{
                exists = [attachment fileExists];
            }
            if((linkMode == LINK_MODE_IMPORTED_FILE || linkMode == LINK_MODE_IMPORTED_URL )
               && ! exists){
                                
                if([[ZPPreferences instance] online]){
                    
                    //TODO: Check if already downloading.
                    
                    if ([[ZPPreferences instance] useDropbox]) label.text = @"Download from Dropbox";
                    else if([[ZPPreferences instance] useWebDAV] && [attachment.libraryID intValue] == 1) label.text = @"Download from WebDAV";
                    else if ([attachment.existsOnZoteroServer intValue]==1){
                        if(attachment.attachmentSize!= NULL && attachment.attachmentSize != [NSNull null]){
                            NSInteger size = [attachment.attachmentSize intValue];
                            label.text = [NSString stringWithFormat:@"Download from Zotero (%i KB)",size/1024];
                        }
                        else{
                            label.text = @"Download from Zotero";
                        }
                    }
                    else label.text = @"File cannot be found for download";
                }
                else label.text = @"File cannot be downloaded when offline";
            }
            
            // Linked URL will be shown directly from web
            
            else if ([attachment.linkMode intValue] == LINK_MODE_LINKED_URL &&
                     ! [[ZPPreferences instance] online]){
                label.text = @"Linked URL cannot be viewed in offline mode";
                
            }
            
            //Linked files are available only on the computer where they were created
            
            else if ([attachment.linkMode intValue] == LINK_MODE_LINKED_FILE) {
                label.text = @"Linked files cannot be viewed in ZotPad";
            }
            
            else{
                label.hidden = TRUE;
            }
        }
    }
    
    
    
    
    
    else if(self.mode == ZPATTACHMENTICONGVIEWCONTROLLER_MODE_UPLOAD){
        label.hidden = FALSE;
        
        if([[ZPServerConnection instance] isAttachmentDownloading:attachment]){
            [self notifyAttachmentDownloadStarted:attachment];
        }
        else{
            label.text = @"Waiting for upload";
        }
    }
    if(self.mode == ZPATTACHMENTICONGVIEWCONTROLLER_MODE_STATIC){
        if(attachment.fileExists)
            label.hidden =TRUE;
        else
            label.text = @"File not found";
    }
    
}


//This is implemented because it is a mandatory protocol method
- (UIView *)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)index reusingView:(UIView *)view{
    NSAssert(carousel==self.attachmentCarousel,@"ZPAttachmentCarouselDelegate can only be used with the iCarousel set in the carousel property");
    return view;
}

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index{
    NSAssert(carousel==self.attachmentCarousel,@"ZPAttachmentCarouselDelegate can only be used with the iCarousel set in the carousel property");
    if([carousel currentItemIndex] == index){
        
        ZPZoteroAttachment* attachment = [_attachments objectAtIndex:index];
        
        if([self _fileExistsForAttachment:attachment]){
            UIView* sourceView;
            for(sourceView in carousel.visibleItemViews){
                if([carousel indexOfItemView:sourceView] == index){
                    break;
                }
            }
            
            [ZPPreviewController displayQuicklookWithAttachment:attachment sourceView:sourceView];
        }
        else if([attachment.linkMode intValue] == LINK_MODE_LINKED_URL && [ZPServerConnection instance]){
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:attachment.url]];
        }
        else if(self.mode == ZPATTACHMENTICONGVIEWCONTROLLER_MODE_DOWNLOAD && ( [attachment.linkMode intValue] == LINK_MODE_IMPORTED_FILE ||
                [attachment.linkMode intValue] == LINK_MODE_IMPORTED_URL)){
            
            ZPServerConnection* connection = [ZPServerConnection instance];
            
            
            if(connection!=NULL && ! [connection isAttachmentDownloading:attachment]){
                [connection checkIfCanBeDownloadedAndStartDownloadingAttachment:attachment];
            }
            
        }
        
    }
}

- (void)carouselCurrentItemIndexUpdated:(iCarousel *)carousel{
    NSAssert(carousel==self.attachmentCarousel,@"ZPAttachmentCarouselDelegate can only be used with the iCarousel set in the carousel property");
    
    if(actionButton != NULL){
        if([_attachments count]==0){
            actionButton.enabled = FALSE;
        }
        else{
            NSInteger currentIndex = carousel.currentItemIndex;
            // Initially the iCarousel can return a negative index. This is probably a bug in iCarousel.
            if(currentIndex <0) currentIndex = 0;
            ZPZoteroAttachment* attachment = [_attachments objectAtIndex:currentIndex];
            actionButton.enabled = [self _fileExistsForAttachment:attachment] &! [attachment.contentType isEqualToString:@"text/html"];
        }
    }
}


#pragma mark - Item observer methods

/*
These are called by data layer to notify that more information about an item has become available from the server
*/

-(void) notifyItemAvailable:(ZPZoteroItem*) item{
    
    if([item.key isEqualToString:_item.key]){
        _item = item;
        _attachments = item.attachments;
        
        if([self.attachmentCarousel numberOfItems]!= _attachments.count){
            if([_attachments count]==0){
                [self.attachmentCarousel setHidden:TRUE];
            }
            else{
                [self.attachmentCarousel setHidden:FALSE];
                [self.attachmentCarousel setScrollEnabled:[_attachments count]>1];
                [self.attachmentCarousel performSelectorOnMainThread:@selector(reloadData) withObject:NULL waitUntilDone:NO];
            }
        }
    }
}


-(void) _reloadAttachmentInCarousel:(ZPZoteroItem*)attachment {
    NSInteger index = [_attachments indexOfObject:attachment];
    if(index !=NSNotFound){
        [self performSelectorOnMainThread:@selector(_reloadCarouselItemAtIndex:) withObject:[NSNumber numberWithInt:index] waitUntilDone:YES];
        [self _toggleActionButtonState];
    }
}

-(void) _reloadCarouselItemAtIndex:(NSInteger) index{
    [attachmentCarousel reloadItemAtIndex:index animated:YES];
}

#pragma mark - Attachment observer methods


-(void) notifyAttachmentDownloadCompleted:(ZPZoteroAttachment*) attachment{

    if(mode ==ZPATTACHMENTICONGVIEWCONTROLLER_MODE_DOWNLOAD ||
       (ZPATTACHMENTICONGVIEWCONTROLLER_MODE_FIRST_STATIC_SECOND_DOWNLOAD && [_attachments indexOfObject:attachment]==2)){
        
        [self _toggleActionButtonState];
        [self _setLabelsForAttachment:attachment progressText:NULL errorText:NULL mode:ZPATTACHMENTICONGVIEWCONTROLLER_MODE_STATIC reconfigureIcon:TRUE];
    }
}
-(void) notifyAttachmentDownloadFailed:(ZPZoteroAttachment *)attachment withError:(NSError *)error{
    
    if(mode ==ZPATTACHMENTICONGVIEWCONTROLLER_MODE_DOWNLOAD ||
       (ZPATTACHMENTICONGVIEWCONTROLLER_MODE_FIRST_STATIC_SECOND_DOWNLOAD && [_attachments indexOfObject:attachment]==2)){
        [self _setLabelsForAttachment:attachment progressText:@"Download failed" errorText:[error localizedDescription] mode:ZPATTACHMENTICONGVIEWCONTROLLER_MODE_STATIC reconfigureIcon:FALSE];
    }
}

-(void) notifyAttachmentDownloadStarted:(ZPZoteroAttachment*) attachment{
    if(mode ==ZPATTACHMENTICONGVIEWCONTROLLER_MODE_DOWNLOAD ||
       (ZPATTACHMENTICONGVIEWCONTROLLER_MODE_FIRST_STATIC_SECOND_DOWNLOAD && [_attachments indexOfObject:attachment]==2)){
        [self _setLabelsForAttachment:attachment progressText:@"Downloading" errorText:NULL mode:ZPATTACHMENTICONGVIEWCONTROLLER_MODE_DOWNLOAD reconfigureIcon:FALSE];
    }
}

-(void) notifyAttachmentDeleted:(ZPZoteroAttachment*) attachment fileAttributes:(NSDictionary*) fileAttributes{
    
    [self _toggleActionButtonState];
    [self _setLabelsForAttachment:attachment progressText:@"File deleted" errorText:NULL mode:ZPATTACHMENTICONGVIEWCONTROLLER_MODE_STATIC reconfigureIcon:FALSE];

}

-(void) notifyAttachmentUploadCompleted:(ZPZoteroAttachment*) attachment{
    if(mode ==ZPATTACHMENTICONGVIEWCONTROLLER_MODE_UPLOAD){
        [self _setLabelsForAttachment:attachment progressText:@"Upload completed" errorText:NULL mode:ZPATTACHMENTICONGVIEWCONTROLLER_MODE_STATIC reconfigureIcon:FALSE];
    }
}

-(void) notifyAttachmentUploadFailed:(ZPZoteroAttachment*) attachment withError:(NSError*) error{
    if(mode ==ZPATTACHMENTICONGVIEWCONTROLLER_MODE_UPLOAD){
        [self _setLabelsForAttachment:attachment progressText:@"Upload failed" errorText:[error localizedDescription] mode:ZPATTACHMENTICONGVIEWCONTROLLER_MODE_STATIC reconfigureIcon:FALSE];
    }
}

-(void) notifyAttachmentUploadStarted:(ZPZoteroAttachment*) attachment{
    if(mode ==ZPATTACHMENTICONGVIEWCONTROLLER_MODE_UPLOAD){
        [self _setLabelsForAttachment:attachment progressText:@"Uploading" errorText:NULL mode:ZPATTACHMENTICONGVIEWCONTROLLER_MODE_UPLOAD reconfigureIcon:FALSE];
    }
}

-(void) notifyAttachmentUploadCanceled:(ZPZoteroAttachment*) attachment{
    if(mode ==ZPATTACHMENTICONGVIEWCONTROLLER_MODE_UPLOAD){
        [self _setLabelsForAttachment:attachment progressText:@"Upload canceled" errorText:NULL mode:ZPATTACHMENTICONGVIEWCONTROLLER_MODE_STATIC reconfigureIcon:FALSE];
    }
}

-(void) _setLabelsForAttachment:(ZPZoteroAttachment*)attachment progressText:(NSString*)progressText errorText:(NSString*)errorText mode:(NSInteger)mode reconfigureIcon:(BOOL)reconfigureIcon{
    NSInteger index = [_attachments indexOfObject:attachment];
    
    if(index!=NSNotFound){

        UIView* view = [attachmentCarousel itemViewAtIndex:index];
        
        if(view != NULL){
            
            if ([NSThread isMainThread]){

                UILabel* progressLabel = (UILabel*)[view viewWithTag:ZPATTACHMENTICONGVIEWCONTROLLER_TAG_STATUSLABEL];
                if(progressText == NULL){
                    progressLabel.hidden = TRUE;
                }
                else{
                    progressLabel.text = progressText;
                    progressLabel.hidden = FALSE;
                }
                
                UILabel* errorLabel = (UILabel*)[view viewWithTag:ZPATTACHMENTICONGVIEWCONTROLLER_TAG_ERRORLABEL];
                if(errorText == NULL){
                    errorLabel.hidden = TRUE;
                }
                else{
                    errorLabel.text = errorText;
                    errorLabel.hidden = FALSE;
                }
                
                UIProgressView* progressView = (UIProgressView*)[view viewWithTag:ZPATTACHMENTICONGVIEWCONTROLLER_TAG_PROGRESSVIEW];
                if(mode == ZPATTACHMENTICONGVIEWCONTROLLER_MODE_STATIC){
                    progressView.hidden = TRUE;
                    view.userInteractionEnabled = FALSE;
                }
                else if (mode==ZPATTACHMENTICONGVIEWCONTROLLER_MODE_UPLOAD){
                    progressView.hidden = FALSE;
                    progressView.progress = 0.0f;
                    [[ZPServerConnection instance] useProgressView:progressView forUploadingAttachment:attachment];
                    view.userInteractionEnabled = TRUE;
                }
                else if (mode==ZPATTACHMENTICONGVIEWCONTROLLER_MODE_DOWNLOAD){
                    progressView.hidden = FALSE;
                    progressView.progress = 0.0f;
                    [[ZPServerConnection instance] useProgressView:progressView forDownloadingAttachment:attachment];
                    view.userInteractionEnabled = FALSE;
                }
                
                if(reconfigureIcon) [self _configureFileImageView:(UIImageView*)[view viewWithTag:ZPATTACHMENTICONGVIEWCONTROLLER_TAG_FILEIMAGE] withAttachment:attachment];


            }
            else{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self _setLabelsForAttachment:attachment progressText:progressText errorText:errorText mode:mode reconfigureIcon:reconfigureIcon];
                });
            }
        }
    }
}


@end
Something went wrong with that request. Please try again.