forked from ViennaRSS/vienna-rss
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArticleController.m
938 lines (839 loc) · 29.1 KB
/
ArticleController.m
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
//
// ArticleController.m
// Vienna
//
// Created by Steve on 5/6/06.
// Copyright (c) 2004-2005 Steve Palmer. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "ArticleController.h"
#import "AppController.h"
#import "Preferences.h"
#import "Constants.h"
#import "Database.h"
#import "ArticleFilter.h"
#import "ArticleRef.h"
#import "StringExtensions.h"
#import "GoogleReader.h"
#import "RefreshManager.h"
// Private functions
@interface ArticleController (Private)
-(NSArray *)applyFilter:(NSArray *)unfilteredArray;
-(void)setSortColumnIdentifier:(NSString *)str;
-(NSArray *)wrappedMarkAllReadInArray:(NSArray *)folderArray withUndo:(BOOL)undoFlag;
-(void)innerMarkReadByArray:(NSArray *)articleArray readFlag:(BOOL)readFlag;
@end
@implementation ArticleController
/* init
* Initialise.
*/
-(id)init
{
if ((self = [super init]) != nil)
{
isBacktracking = NO;
mainArticleView = nil;
currentFolderId = -1;
currentArrayOfArticles = nil;
folderArrayOfArticles = nil;
articleToPreserve = nil;
// Set default values to generate article sort descriptors
articleSortSpecifiers = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSDictionary dictionaryWithObjectsAndKeys:
@"containingFolder.name", @"key",
@"compare:", @"selector",
nil], MA_Field_Folder,
[NSDictionary dictionaryWithObjectsAndKeys:
@"isRead", @"key",
@"compare:", @"selector",
nil], MA_Field_Read,
[NSDictionary dictionaryWithObjectsAndKeys:
@"isFlagged", @"key",
@"compare:", @"selector",
nil], MA_Field_Flagged,
[NSDictionary dictionaryWithObjectsAndKeys:
@"hasComments", @"key",
@"compare:", @"selector",
nil], MA_Field_Comments,
[NSDictionary dictionaryWithObjectsAndKeys:
[@"articleData." stringByAppendingString:MA_Field_Date], @"key",
@"compare:", @"selector",
nil], MA_Field_Date,
[NSDictionary dictionaryWithObjectsAndKeys:
[@"articleData." stringByAppendingString:MA_Field_Author], @"key",
@"caseInsensitiveCompare:", @"selector",
nil], MA_Field_Author,
[NSDictionary dictionaryWithObjectsAndKeys:
[@"articleData." stringByAppendingString:MA_Field_Subject], @"key",
@"numericCompare:", @"selector",
nil], MA_Field_Headlines,
[NSDictionary dictionaryWithObjectsAndKeys:
[@"articleData." stringByAppendingString:MA_Field_Subject], @"key",
@"numericCompare:", @"selector",
nil], MA_Field_Subject,
[NSDictionary dictionaryWithObjectsAndKeys:
[@"articleData." stringByAppendingString:MA_Field_Link], @"key",
@"caseInsensitiveCompare:", @"selector",
nil], MA_Field_Link,
[NSDictionary dictionaryWithObjectsAndKeys:
[@"articleData." stringByAppendingString:MA_Field_Summary], @"key",
@"caseInsensitiveCompare:", @"selector",
nil], MA_Field_Summary,
[NSDictionary dictionaryWithObjectsAndKeys:
@"hasEnclosure", @"key",
@"compare:", @"selector",
nil], MA_Field_HasEnclosure,
[NSDictionary dictionaryWithObjectsAndKeys:
@"enclosure", @"key",
@"caseInsensitiveCompare:", @"selector",
nil], MA_Field_Enclosure,
nil];
// Pre-set sort to what was saved in the preferences
Preferences * prefs = [Preferences standardPreferences];
NSArray * sortDescriptors = [prefs articleSortDescriptors];
if ([sortDescriptors count] == 0)
{
NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey:[@"articleData." stringByAppendingString:MA_Field_Date] ascending:YES] autorelease];
[prefs setArticleSortDescriptors:[NSArray arrayWithObject:descriptor]];
[prefs setObject:MA_Field_Date forKey:MAPref_SortColumn];
}
[self setSortColumnIdentifier:[prefs stringForKey:MAPref_SortColumn]];
// Create a backtrack array
backtrackArray = [[BackTrackArray alloc] initWithMaximum:[prefs backTrackQueueSize]];
// Register for notifications
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(handleFilterChange:) name:@"MA_Notify_FilteringChange" object:nil];
[nc addObserver:self selector:@selector(handleFolderNameChange:) name:@"MA_Notify_FolderNameChanged" object:nil];
[nc addObserver:self selector:@selector(handleFolderUpdate:) name:@"MA_Notify_FoldersUpdated" object:nil];
[nc addObserver:self selector:@selector(handleRefreshArticle:) name:@"MA_Notify_ArticleViewChange" object:nil];
[nc addObserver:self selector:@selector(handleArticleListStateChange:) name:@"MA_Notify_ArticleListStateChange" object:nil];
operationQueue = [[NSOperationQueue alloc] init];
}
return self;
}
/* mainArticleView
* Returns the current view being used to display the articles.
*/
-(NSView<ArticleBaseView, BaseView> *)mainArticleView
{
return mainArticleView;
}
/* setMainArticleView
* Sets the view to use for displaying the articles.
*/
-(void)setMainArticleView:(NSView<ArticleBaseView, BaseView> *)newView
{
[newView retain];
[mainArticleView release];
mainArticleView = newView;
}
/* refreshCurrentFolder
*/
-(void)refreshCurrentFolder
{
[mainArticleView refreshCurrentFolder];
}
/* currentFolderId
* Returns the ID of the current folder being displayed by the view.
*/
-(int)currentFolderId
{
return currentFolderId;
}
/* selectedArticle
* Returns the currently selected article from the article list.
*/
-(Article *)selectedArticle
{
return [mainArticleView selectedArticle];
}
/* allArticles
* Returns the current filtered and sorted article array.
*/
-(NSArray *)allArticles
{
return currentArrayOfArticles;
}
/* ensureSelectedArticle
* Ensures that an article is selected in the list and that any selected
* article is scrolled into view.
*/
-(void)ensureSelectedArticle:(BOOL)singleSelection
{
[mainArticleView ensureSelectedArticle:singleSelection];
}
/* searchPlaceholderString
* Return the search field placeholder.
*/
-(NSString *)searchPlaceholderString
{
if (currentFolderId == -1)
return @"";
Folder * folder = [[Database sharedDatabase] folderFromID:currentFolderId];
return [NSString stringWithFormat:NSLocalizedString(@"Search in %@", nil), [folder name]];
}
/* sortColumnIdentifier
* Returns the name of the column on which we're currently sorting.
*/
-(NSString *)sortColumnIdentifier
{
return sortColumnIdentifier;
}
/* setSortColumnIdentifier
* Sets the name of the column on which we're currently sorting.
*/
-(void)setSortColumnIdentifier:(NSString *)str
{
[str retain];
[sortColumnIdentifier release];
sortColumnIdentifier = str;
}
/* sortByIdentifier
* Sort by the column indicated by the specified column name.
*/
-(void)sortByIdentifier:(NSString *)columnName
{
Preferences * prefs = [Preferences standardPreferences];
NSMutableArray * descriptors = [NSMutableArray arrayWithArray:[prefs articleSortDescriptors]];
if ([sortColumnIdentifier isEqualToString:columnName])
[descriptors replaceObjectAtIndex:0 withObject:[[descriptors objectAtIndex:0] reversedSortDescriptor]];
else
{
[self setSortColumnIdentifier:columnName];
[prefs setObject:sortColumnIdentifier forKey:MAPref_SortColumn];
NSSortDescriptor * sortDescriptor;
NSDictionary * specifier = [articleSortSpecifiers valueForKey:sortColumnIdentifier];
NSUInteger index = [[descriptors valueForKey:@"key"] indexOfObject:[specifier valueForKey:@"key"]];
if (index == NSNotFound)
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:[specifier valueForKey:@"key"] ascending:YES selector:NSSelectorFromString([specifier valueForKey:@"selector"])];
else
{
sortDescriptor = [[descriptors objectAtIndex:index] retain];
[descriptors removeObjectAtIndex:index];
}
[descriptors insertObject:sortDescriptor atIndex:0];
[sortDescriptor release];
}
[prefs setArticleSortDescriptors:descriptors];
[mainArticleView refreshFolder:MA_Refresh_SortAndRedraw];
}
/* sortIsAscending
* Returns YES if the sort direction is currently set to ascending.
*/
-(BOOL)sortIsAscending
{
Preferences * prefs = [Preferences standardPreferences];
NSMutableArray * descriptors = [NSMutableArray arrayWithArray:[prefs articleSortDescriptors]];
NSSortDescriptor * sortDescriptor = [descriptors objectAtIndex:0];
BOOL ascending = [sortDescriptor ascending];
return ascending;
}
/* sortAscending
* Sort by the direction indicated.
*/
-(void)sortAscending:(BOOL)newAscending
{
Preferences * prefs = [Preferences standardPreferences];
NSMutableArray * descriptors = [NSMutableArray arrayWithArray:[prefs articleSortDescriptors]];
NSSortDescriptor * sortDescriptor = [descriptors objectAtIndex:0];
BOOL existingAscending = [sortDescriptor ascending];
if ( newAscending != existingAscending )
{
[descriptors replaceObjectAtIndex:0 withObject:[sortDescriptor reversedSortDescriptor]];
[prefs setArticleSortDescriptors:descriptors];
[mainArticleView refreshFolder:MA_Refresh_SortAndRedraw];
}
}
/* sortArticles
* Re-orders the articles in currentArrayOfArticles by the current sort order
*/
-(void)sortArticles
{
NSArray * sortedArrayOfArticles;
sortedArrayOfArticles = [currentArrayOfArticles sortedArrayUsingDescriptors:[[Preferences standardPreferences] articleSortDescriptors]];
NSAssert([sortedArrayOfArticles count] == [currentArrayOfArticles count], @"Lost articles from currentArrayOfArticles during sort");
[currentArrayOfArticles autorelease];
currentArrayOfArticles = [sortedArrayOfArticles retain];
}
/* displayFirstUnread
* Instructs the current article view to display the first unread article
* in the database.
*/
-(void)displayFirstUnread
{
[mainArticleView displayFirstUnread];
}
/* displayNextUnread
* Instructs the current article view to display the next unread article
* in the database.
*/
-(void)displayNextUnread
{
[mainArticleView displayNextUnread];
}
/* displayFolder
* Call the current article view to display the specified folder if it
* is different from the current one.
*/
-(void)displayFolder:(int)newFolderId
{
if (currentFolderId != newFolderId && newFolderId != 0)
{
[[[Database sharedDatabase] folderFromID:currentFolderId] clearCache];
currentFolderId = newFolderId;
[mainArticleView selectFolderWithFilter:newFolderId];
}
}
/* reloadArrayOfArticles
* Reload the folderArrayOfArticles from the current folder and applies the
* current filter.
*/
-(void)reloadArrayOfArticles
{
[folderArrayOfArticles autorelease];
[[RefreshManager articlesUpdateSemaphore] lock];
Folder * folder = [[Database sharedDatabase] folderFromID:currentFolderId];
folderArrayOfArticles = [[folder articlesWithFilter:[[NSApp delegate] filterString]] retain];
[[RefreshManager articlesUpdateSemaphore] unlock];
[self refilterArrayOfArticles];
}
/* refilterArrayOfArticles
* Reapply the current filter to the article array.
*/
-(void)refilterArrayOfArticles
{
[currentArrayOfArticles autorelease];
currentArrayOfArticles = [[self applyFilter:folderArrayOfArticles] retain];
}
/* applyFilter
* Apply the active filter to unfilteredArray and return the filtered array.
* This is done here rather than in the folder management code for simplicity.
*/
-(NSArray *)applyFilter:(NSArray *)unfilteredArray
{
NSMutableArray * filteredArray = [NSMutableArray arrayWithArray:unfilteredArray];
NSString * guidOfArticleToPreserve = (articleToPreserve != nil) ? [articleToPreserve guid] : @"";
int folderIdOfArticleToPreserve = [articleToPreserve folderId];
ArticleFilter * filter = [ArticleFilter filterByTag:[[Preferences standardPreferences] filterMode]];
SEL comparator = [filter comparator];
int count = [filteredArray count];
int index;
for (index = count - 1; index >= 0; --index)
{
Article * article = [filteredArray objectAtIndex:index];
if (([article folderId] == folderIdOfArticleToPreserve) && [[article guid] isEqualToString:guidOfArticleToPreserve])
guidOfArticleToPreserve = @"";
else if ((comparator != nil) && !((BOOL)(NSInteger)[ArticleFilter performSelector:comparator withObject:article]))
[filteredArray removeObjectAtIndex:index];
}
if (![guidOfArticleToPreserve isEqualToString:@""])
{
Article * articleToAdd = nil;
Folder * folder = [[Database sharedDatabase] folderFromID:folderIdOfArticleToPreserve];
if (folder != nil)
{
[folder clearCache];
[folder articles];
articleToAdd = [folder articleFromGuid:guidOfArticleToPreserve];
}
if (articleToAdd == nil)
articleToAdd = articleToPreserve;
[filteredArray addObject:articleToAdd];
}
[self setArticleToPreserve:nil];
return filteredArray;
}
/* markDeletedUndo
* Undo handler to restore a series of deleted articles.
*/
-(void)markDeletedUndo:(id)anObject
{
[self markDeletedByArray:(NSArray *)anObject deleteFlag:NO];
}
/* markUndeletedUndo
* Undo handler to delete a series of articles.
*/
-(void)markUndeletedUndo:(id)anObject
{
[self markDeletedByArray:(NSArray *)anObject deleteFlag:YES];
}
/* markDeletedByArray
* Helper function. Takes as an input an array of articles and deletes or restores
* the articles.
*/
-(void)markDeletedByArray:(NSArray *)articleArray deleteFlag:(BOOL)deleteFlag
{
// Set up to undo this action
NSUndoManager * undoManager = [[NSApp mainWindow] undoManager];
SEL markDeletedUndoAction = deleteFlag ? @selector(markDeletedUndo:) : @selector(markUndeletedUndo:);
[undoManager registerUndoWithTarget:self selector:markDeletedUndoAction object:articleArray];
[undoManager setActionName:NSLocalizedString(@"Delete", nil)];
// We will make a new copy of currentArrayOfArticles and folderArrayOfArticles with the selected articles removed.
NSMutableArray * currentArrayCopy = [[NSMutableArray alloc] initWithArray:currentArrayOfArticles];
NSMutableArray * folderArrayCopy = [[NSMutableArray alloc] initWithArray:folderArrayOfArticles];
BOOL needFolderRedraw = NO;
BOOL needReload = NO;
// Iterate over every selected article in the table and set the deleted
// flag on the article while simultaneously removing it from our copies
Database * db = [Database sharedDatabase];
[db beginTransaction];
for (Article * theArticle in articleArray)
{
if (![theArticle isRead])
needFolderRedraw = YES;
[db markArticleDeleted:[theArticle folderId] guid:[theArticle guid] isDeleted:deleteFlag];
if (![currentArrayOfArticles containsObject:theArticle])
needReload = YES;
else if (deleteFlag && (currentFolderId != [db trashFolderId]))
{
[currentArrayCopy removeObject:theArticle];
[folderArrayCopy removeObject:theArticle];
}
else if (!deleteFlag && (currentFolderId == [db trashFolderId]))
{
[currentArrayCopy removeObject:theArticle];
[folderArrayCopy removeObject:theArticle];
}
else
needReload = YES;
}
[db commitTransaction];
[currentArrayOfArticles autorelease];
currentArrayOfArticles = currentArrayCopy;
[folderArrayOfArticles autorelease];
folderArrayOfArticles = folderArrayCopy;
if (needReload)
[mainArticleView refreshFolder:MA_Refresh_ReloadFromDatabase];
else
{
[mainArticleView refreshFolder:MA_Refresh_RedrawList];
if ([currentArrayOfArticles count] > 0u)
[mainArticleView ensureSelectedArticle:YES];
else
[[NSApp mainWindow] makeFirstResponder:[foldersTree mainView]];
}
// If any of the articles we deleted were unread then the
// folder's unread count just changed.
if (needFolderRedraw)
{
[foldersTree updateFolder:currentFolderId recurseToParents:YES];
[[NSApp delegate] showUnreadCountOnApplicationIconAndWindowTitle];
}
}
/* deleteArticlesByArray
* Physically delete all selected articles in the article list.
*/
-(void)deleteArticlesByArray:(NSArray *)articleArray
{
// Make a new copy of currentArrayOfArticles and folderArrayOfArticles with the selected article removed.
NSMutableArray * currentArrayCopy = [[NSMutableArray alloc] initWithArray:currentArrayOfArticles];
NSMutableArray * folderArrayCopy = [[NSMutableArray alloc] initWithArray:folderArrayOfArticles];
BOOL needFolderRedraw = NO;
// Iterate over every selected article in the table and remove it from
// the database.
Database * db = [Database sharedDatabase];
[db beginTransaction];
for (Article * theArticle in articleArray)
{
if (![theArticle isRead])
needFolderRedraw = YES;
if ([db deleteArticle:[theArticle folderId] guid:[theArticle guid]])
{
[currentArrayCopy removeObject:theArticle];
[folderArrayCopy removeObject:theArticle];
}
}
[db commitTransaction];
[currentArrayOfArticles autorelease];
currentArrayOfArticles = currentArrayCopy;
[folderArrayOfArticles autorelease];
folderArrayOfArticles = folderArrayCopy;
[mainArticleView refreshFolder:MA_Refresh_RedrawList];
// If any of the articles we deleted were unread then the
// folder's unread count just changed.
if (needFolderRedraw)
[foldersTree updateFolder:currentFolderId recurseToParents:YES];
// Ensure there's a valid selection
if ([currentArrayOfArticles count] > 0u)
[mainArticleView ensureSelectedArticle:YES];
else
[[NSApp mainWindow] makeFirstResponder:[foldersTree mainView]];
// Read and/or unread count may have changed
if (needFolderRedraw)
[[NSApp delegate] showUnreadCountOnApplicationIconAndWindowTitle];
}
/* markUnflagUndo
* Undo handler to un-flag an array of articles.
*/
-(void)markUnflagUndo:(id)anObject
{
[self markFlaggedByArray:(NSArray *)anObject flagged:NO];
}
/* markFlagUndo
* Undo handler to flag an array of articles.
*/
-(void)markFlagUndo:(id)anObject
{
[self markFlaggedByArray:(NSArray *)anObject flagged:YES];
}
/* markFlaggedByArray
* Mark the specified articles in articleArray as flagged.
*/
-(void)markFlaggedByArray:(NSArray *)articleArray flagged:(BOOL)flagged
{
Database * db = [Database sharedDatabase];
// Set up to undo this action
NSUndoManager * undoManager = [[NSApp mainWindow] undoManager];
SEL markFlagUndoAction = flagged ? @selector(markUnflagUndo:) : @selector(markFlagUndo:);
[undoManager registerUndoWithTarget:self selector:markFlagUndoAction object:articleArray];
[undoManager setActionName:NSLocalizedString(@"Flag", nil)];
[db beginTransaction];
for (Article * theArticle in articleArray)
{
Folder *myFolder = [db folderFromID:[theArticle folderId]];
if (IsGoogleReaderFolder(myFolder)) {
[[GoogleReader sharedManager] markStarred:[theArticle guid] starredFlag:flagged];
}
[theArticle markFlagged:flagged];
[db markArticleFlagged:[theArticle folderId] guid:[theArticle guid] isFlagged:flagged];
}
[db commitTransaction];
[mainArticleView refreshFolder:MA_Refresh_RedrawList];
}
/* markUnreadUndo
* Undo handler to mark an array of articles unread.
*/
-(void)markUnreadUndo:(id)anObject
{
[self markAllReadByReferencesArray:(NSArray *)anObject readFlag:NO];
}
/* markReadUndo
* Undo handler to mark an array of articles read.
*/
-(void)markReadUndo:(id)anObject
{
[self markAllReadByReferencesArray:(NSArray *)anObject readFlag:YES];
}
/* markReadByArray
* Helper function. Takes as an input an array of articles and marks those articles read or unread.
*/
-(void)markReadByArray:(NSArray *)articleArray readFlag:(BOOL)readFlag
{
// Set up to undo this action
NSUndoManager * undoManager = [[NSApp mainWindow] undoManager];
SEL markReadUndoAction = readFlag ? @selector(markUnreadUndo:) : @selector(markReadUndo:);
[undoManager registerUndoWithTarget:self selector:markReadUndoAction object:articleArray];
[undoManager setActionName:NSLocalizedString(@"Mark Read", nil)];
Database * db = [Database sharedDatabase];
BOOL singleArticle = [articleArray count] < 2;
if (!singleArticle)
[db beginTransaction];
[self innerMarkReadByArray:articleArray readFlag:readFlag];
if (!singleArticle)
[db commitTransaction];
[mainArticleView refreshFolder:MA_Refresh_RedrawList];
// The info bar has a count of unread articles so we need to
// update that.
[[NSApp delegate] showUnreadCountOnApplicationIconAndWindowTitle];
}
/* innerMarkReadByArray
* Marks all articles in the specified array read or unread.
*/
-(void)innerMarkReadByArray:(NSArray *)articleArray readFlag:(BOOL)readFlag
{
Database * db = [Database sharedDatabase];
NSInteger lastFolderId = -1;
for (Article * theArticle in articleArray)
{
NSInteger folderId = [theArticle folderId];
if (IsGoogleReaderFolder([db folderFromID:folderId]) && ([theArticle isRead] != readFlag)) {
[[GoogleReader sharedManager] markRead:[theArticle guid] readFlag:readFlag];
}
//FIX: article status should be "settato" from httprequest success block
[db markArticleRead:folderId guid:[theArticle guid] isRead:readFlag];
[theArticle markRead:readFlag];
if (folderId != lastFolderId && lastFolderId != -1)
[foldersTree updateFolder:lastFolderId recurseToParents:YES];
lastFolderId = folderId;
}
if (lastFolderId != -1)
[foldersTree updateFolder:lastFolderId recurseToParents:YES];
}
/* markAllReadUndo
* Undo the most recent Mark All Read.
*/
-(void)markAllReadUndo:(id)anObject
{
[self markAllReadByReferencesArray:(NSArray *)anObject readFlag:NO];
}
/* markAllReadRedo
* Redo the most recent Mark All Read.
*/
-(void)markAllReadRedo:(id)anObject
{
[self markAllReadByReferencesArray:(NSArray *)anObject readFlag:YES];
}
/* markAllReadByArray
* Given an array of folders, mark all the articles in those folders as read and
* return a reference array listing all the articles that were actually marked.
*/
-(void)markAllReadByArray:(NSArray *)folderArray withUndo:(BOOL)undoFlag withRefresh:(BOOL)refreshFlag
{
Database * db = [Database sharedDatabase];
NSArray * refArray = nil;
[db beginTransaction];
refArray = [self wrappedMarkAllReadInArray:folderArray withUndo:undoFlag];
[db commitTransaction];
if (refArray != nil && [refArray count] > 0)
{
NSUndoManager * undoManager = [[NSApp mainWindow] undoManager];
[undoManager registerUndoWithTarget:self selector:@selector(markAllReadUndo:) object:refArray];
[undoManager setActionName:NSLocalizedString(@"Mark All Read", nil)];
}
for (Article * theArticle in folderArrayOfArticles)
{
if (IsGoogleReaderFolder([db folderFromID:[theArticle folderId]]) && ![theArticle isRead]) {
[[GoogleReader sharedManager] markRead:[theArticle guid] readFlag:YES];
}
[theArticle markRead:YES];
}
if (refreshFlag)
[mainArticleView refreshFolder:MA_Refresh_RedrawList];
[[NSApp delegate] showUnreadCountOnApplicationIconAndWindowTitle];
}
/* wrappedMarkAllReadInArray
* Given an array of folders, mark all the articles in those folders as read and
* return a reference array listing all the articles that were actually marked.
*/
-(NSArray *)wrappedMarkAllReadInArray:(NSArray *)folderArray withUndo:(BOOL)undoFlag
{
NSMutableArray * refArray = [NSMutableArray array];
Database * db = [Database sharedDatabase];
for (Folder * folder in folderArray)
{
int folderId = [folder itemId];
if (IsGroupFolder(folder) && undoFlag)
{
[refArray addObjectsFromArray:[self wrappedMarkAllReadInArray:[db arrayOfFolders:folderId] withUndo:undoFlag]];
}
else if (IsRSSFolder(folder))
{
if (undoFlag)
[refArray addObjectsFromArray:[db arrayOfUnreadArticles:folderId]];
if ([db markFolderRead:folderId])
{
[foldersTree updateFolder:folderId recurseToParents:YES];
//TOFIX
/*
GRSMarkReadOperation * op = [[GRSMarkReadOperation alloc] init];
[op setArticles:[folder articles]];
[op setReadFlag:YES];
[operationQueue addOperation:op];
[op release];
*/
}
}
else
{
// Google Reader feeds and smart folders
// For smart folders, we only mark all read the current folder to
// simplify things.
if (undoFlag && (IsGoogleReaderFolder(folder) || folderId == currentFolderId))
{
[refArray addObjectsFromArray:currentArrayOfArticles];
[self innerMarkReadByArray:currentArrayOfArticles readFlag:YES];
}
}
}
return refArray;
}
/* currentCacheContainsFolder
* Scans the current article cache to determine if any article is a member of the specified
* folder and returns YES if so.
*/
-(BOOL)currentCacheContainsFolder:(int)folderId
{
int count = [currentArrayOfArticles count];
int index = 0;
while (index < count)
{
Article * anArticle = [currentArrayOfArticles objectAtIndex:index];
if ([anArticle folderId] == folderId)
return YES;
++index;
}
return NO;
}
/* markAllReadByReferencesArray
* Given an array of references, mark all those articles read or unread.
*/
-(void)markAllReadByReferencesArray:(NSArray *)refArray readFlag:(BOOL)readFlag
{
Database * db = [Database sharedDatabase];
int lastFolderId = -1;
BOOL needRefilter = NO;
// Set up to undo or redo this action
NSUndoManager * undoManager = [[NSApp mainWindow] undoManager];
SEL markAllReadUndoAction = readFlag ? @selector(markAllReadUndo:) : @selector(markAllReadRedo:);
[undoManager registerUndoWithTarget:self selector:markAllReadUndoAction object:refArray];
[undoManager setActionName:NSLocalizedString(@"Mark All Read", nil)];
[db beginTransaction];
for (ArticleReference *ref in refArray)
{
int folderId = [ref folderId];
[db markArticleRead:folderId guid:[ref guid] isRead:readFlag];
if (folderId != lastFolderId && lastFolderId != -1)
{
[foldersTree updateFolder:lastFolderId recurseToParents:YES];
if (lastFolderId == currentFolderId)
needRefilter = YES;
}
lastFolderId = folderId;
}
[db commitTransaction];
if (lastFolderId != -1)
{
[foldersTree updateFolder:lastFolderId recurseToParents:YES];
if (lastFolderId == currentFolderId)
needRefilter = YES;
if (!IsRSSFolder([db folderFromID:currentFolderId]))
[mainArticleView refreshFolder:MA_Refresh_ReloadFromDatabase];
else if (needRefilter)
[mainArticleView refreshFolder:MA_Refresh_ReapplyFilter];
}
// The info bar has a count of unread articles so we need to
// update that.
[[NSApp delegate] showUnreadCountOnApplicationIconAndWindowTitle];
}
/* addBacktrack
* Add the specified article to the backtrack queue. The folder is taken from
* the controller's current folder index.
*/
-(void)addBacktrack:(NSString *)guid
{
if (!isBacktracking)
[backtrackArray addToQueue:currentFolderId guid:guid];
}
/* goForward
* Move forward through the backtrack queue.
*/
-(void)goForward
{
int folderId;
NSString * guid;
if ([backtrackArray nextItemAtQueue:&folderId guidPointer:&guid])
{
isBacktracking = YES;
[mainArticleView selectFolderAndArticle:folderId guid:guid];
isBacktracking = NO;
}
}
/* goBack
* Move backward through the backtrack queue.
*/
-(void)goBack
{
int folderId;
NSString * guid;
if ([backtrackArray previousItemAtQueue:&folderId guidPointer:&guid])
{
isBacktracking = YES;
[mainArticleView selectFolderAndArticle:folderId guid:guid];
isBacktracking = NO;
}
}
/* canGoForward
* Return TRUE if we can go forward in the backtrack queue.
*/
-(BOOL)canGoForward
{
return ![backtrackArray isAtEndOfQueue];
}
/* canGoBack
* Return TRUE if we can go backward in the backtrack queue.
*/
-(BOOL)canGoBack
{
return ![backtrackArray isAtStartOfQueue];
}
/* handleFilterChange
* Update the list of articles when the user changes the filter.
*/
-(void)handleFilterChange:(NSNotification *)nc
{
[mainArticleView refreshFolder:MA_Refresh_ReapplyFilter];
}
/* handleFolderNameChange
* Some folder metadata changed. Update the article list header and the
* current article with a possible name change.
*/
-(void)handleFolderNameChange:(NSNotification *)nc
{
int folderId = [(NSNumber *)[nc object] intValue];
if (folderId == currentFolderId)
[mainArticleView refreshArticlePane];
}
/* handleRefreshArticle
* Respond to the notification to refresh the current article pane.
*/
-(void)handleRefreshArticle:(NSNotification *)nc
{
[mainArticleView handleRefreshArticle:nc];
}
// TODO this is being called too often by sync operations. We should try to avoid
// querying the database so often. Need to look into having the sync operations know
// when everything has been completed and only query the database once.
-(void)handleArticleListStateChange:(NSNotification *)nc
{
[mainArticleView refreshFolder:MA_Refresh_ReloadFromDatabase];
//[mainArticleView refreshFolder:MA_Refresh_RedrawList];
}
/* handleFolderUpdate
* Called if a folder content has changed.
*/
-(void)handleFolderUpdate:(NSNotification *)nc
{
int folderId = [(NSNumber *)[nc object] intValue];
if (folderId != currentFolderId)
return;
Folder * folder = [[Database sharedDatabase] folderFromID:folderId];
if (IsSmartFolder(folder) || IsTrashFolder(folder))
[mainArticleView refreshFolder:MA_Refresh_ReloadFromDatabase];
}
/* setArticleToPreserve
* Sets the article to preserve when reloading the array of articles.
*/
-(void)setArticleToPreserve:(Article *)article
{
[article retain];
[articleToPreserve release];
articleToPreserve = article;
}
/* dealloc
* Clean up behind us.
*/
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[mainArticleView release];
[backtrackArray release];
[sortColumnIdentifier release];
[folderArrayOfArticles release];
[currentArrayOfArticles release];
[articleSortSpecifiers release];
[articleToPreserve release];
[operationQueue release];
[super dealloc];
}
@end