-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathArticleListView.m
More file actions
1583 lines (1385 loc) · 54.2 KB
/
ArticleListView.m
File metadata and controls
1583 lines (1385 loc) · 54.2 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
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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// ArticleListView.m
// Vienna
//
// Created by Steve on 8/27/05.
// Copyright (c) 2004-2017 Steve Palmer and Vienna contributors. 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.
//
// Handle the Horizontal (also known as Report) and Vertical (also known as Condensed) layouts
#import "ArticleListView.h"
#import "Preferences.h"
#import "Constants.h"
#import "DateFormatterExtension.h"
#import "ArticleController.h"
#import "StringExtensions.h"
#import "HelperFunctions.h"
#import "Field.h"
#import "ProgressTextCell.h"
#import "Article.h"
#import "Folder.h"
#import "EnclosureView.h"
#import "Database.h"
#import "Vienna-Swift.h"
#import "GeneratedAssetSymbols.h"
#import "AppController.h"
#import "FilterBarViewController.h"
#import "VNAVerticallyCenteredTextFieldCell.h"
// Shared defaults key
NSString * const MAPref_ShowEnclosureBar = @"ShowEnclosureBar";
static CGFloat const VNAMinimumArticleListViewHeight = 160.0;
static CGFloat const VNAMinimumArticleListViewWidth = 200.0;
static CGFloat const VNAMinimumArticleTextViewWidth = 360.0;
static void *VNAArticleListViewObserverContext = &VNAArticleListViewObserverContext;
@interface ArticleListView ()
@property (weak, nonatomic) IBOutlet NSStackView *contentStackView;
@property (weak, nonatomic) IBOutlet EnclosureView *enclosureView;
-(void)initTableView;
-(BOOL)copyTableSelection:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard;
-(void)setTableViewFont;
-(void)showSortDirection;
-(void)handleReadingPaneChange:(NSNotification *)notification;
-(BOOL)viewNextUnreadInCurrentFolder:(NSInteger)currentRow;
-(void)markCurrentRead:(NSTimer *)aTimer;
-(void)refreshImmediatelyArticleAtCurrentRow;
-(void)refreshArticleAtCurrentRow;
-(void)makeRowSelectedAndVisible:(NSInteger)rowIndex;
-(void)updateArticleListRowHeight;
-(void)setOrientation:(NSInteger)newLayout;
@property NSView *articleTextView;
@property (nonatomic) NSLayoutManager *layoutManager;
// MARK: ArticleView delegate
@property (readwrite, getter=isCurrentPageFullHTML, nonatomic) BOOL currentPageFullHTML;
@end
@implementation ArticleListView {
IBOutlet MessageListView *articleList;
NSObject<ArticleContentView, Tab> *articleText;
IBOutlet NSSplitView *splitView2;
NSInteger tableLayout;
BOOL isAppInitialising;
BOOL isChangingOrientation;
BOOL isInTableInit;
BOOL blockSelectionHandler;
NSTimer *markReadTimer;
NSFont *articleListFont;
NSFont *articleListUnreadFont;
NSMutableDictionary *reportCellDict;
NSMutableDictionary *unreadReportCellDict;
NSMutableDictionary *topLineDict;
NSMutableDictionary *linkLineDict;
NSMutableDictionary *middleLineDict;
NSMutableDictionary *bottomLineDict;
NSMutableDictionary *unreadTopLineDict;
BOOL isLoadingHTMLArticle;
}
@synthesize filterBarViewController = _filterBarViewController;
/* initWithFrame
* Initialise our view.
*/
-(instancetype)initWithFrame:(NSRect)frame
{
self= [super initWithFrame:frame];
if (self) {
isChangingOrientation = NO;
isInTableInit = NO;
blockSelectionHandler = NO;
markReadTimer = nil;
_currentPageFullHTML = NO;
isLoadingHTMLArticle = NO;
_layoutManager = [NSLayoutManager new];
_filterBarViewController = [VNAFilterBarViewController instantiateFromNib];
}
return self;
}
/* awakeFromNib
* Do things that only make sense once the NIB is loaded.
*/
-(void)awakeFromNib
{
// Register for notification
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(handleArticleListFontChange:) name:MA_Notify_ArticleListFontChange object:nil];
[nc addObserver:self selector:@selector(handleReadingPaneChange:) name:MA_Notify_ReadingPaneChange object:nil];
[nc addObserver:self selector:@selector(handleLoadFullHTMLChange:) name:MA_Notify_LoadFullHTMLChange object:nil];
[nc addObserver:self selector:@selector(handleStyleChange:) name:MA_Notify_StyleChange object:nil];
[nc addObserver:self selector:@selector(handleRefreshArticle:) name:MA_Notify_ArticleViewChange object:nil];
[nc addObserver:self selector:@selector(handleArticleViewEnded:) name:MA_Notify_ArticleViewEnded object:nil];
[self initialiseArticleView];
}
/* initialiseArticleView
* Do the things to initialise the article view from the database. This is the
* only point during initialisation where the database is guaranteed to be
* ready for use.
*/
-(void)initialiseArticleView
{
WebKitArticleTab *articleTextController = [[WebKitArticleTab alloc] init];
articleText = articleTextController;
self.articleTextView = articleTextController.view;
[self.contentStackView addView:self.articleTextView inGravity:NSStackViewGravityTop];
Preferences * prefs = [Preferences standardPreferences];
// Mark the start of the init phase
isAppInitialising = YES;
articleText.listView = self;
// Create report and condensed view attribute dictionaries
NSMutableParagraphStyle * style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.lineBreakMode = NSLineBreakByTruncatingTail;
style.tighteningFactorForTruncation = 0.0;
reportCellDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:style, NSParagraphStyleAttributeName, [NSColor textColor], NSForegroundColorAttributeName, nil];
unreadReportCellDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:style, NSParagraphStyleAttributeName, [NSColor textColor], NSForegroundColorAttributeName, nil];
unreadTopLineDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:style, NSParagraphStyleAttributeName, [NSColor textColor], NSForegroundColorAttributeName, nil];
topLineDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:style, NSParagraphStyleAttributeName, [NSColor textColor], NSForegroundColorAttributeName, nil];
middleLineDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:style, NSParagraphStyleAttributeName, [NSColor systemBlueColor], NSForegroundColorAttributeName, nil];
linkLineDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:style, NSParagraphStyleAttributeName, [NSColor systemBlueColor], NSForegroundColorAttributeName, nil];
bottomLineDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:style, NSParagraphStyleAttributeName, NSColor.secondaryLabelColor, NSForegroundColorAttributeName, nil];
NSScrollView *articleListScrollView = articleList.enclosingScrollView;
self.filterBarViewController.filterBarContainer = articleListScrollView;
// Set the reading pane orientation
[self setOrientation:prefs.layout];
// Initialise the article list view
[self initTableView];
// Make sure we skip the column filter button in the Tab order
articleList.nextKeyView = self.articleTextView;
// Done initialising
isAppInitialising = NO;
NSUserDefaults *userDefaults = NSUserDefaults.standardUserDefaults;
[userDefaults addObserver:self
forKeyPath:MAPref_ShowEnclosureBar
options:NSKeyValueObservingOptionNew
context:VNAArticleListViewObserverContext];
[userDefaults addObserver:self
forKeyPath:MAPref_ShowUnreadArticlesInBold
options:0
context:VNAArticleListViewObserverContext];
}
/* initTableView
* Do all the initialization for the article list table view control
*/
-(void)initTableView
{
Preferences * prefs = [Preferences standardPreferences];
// Variable initialization here
articleListFont = nil;
articleListUnreadFont = nil;
// Initialize the article columns from saved data
NSArray * dataArray = [prefs arrayForKey:MAPref_ArticleListColumns];
Database * db = [Database sharedManager];
Field * field;
NSUInteger index;
for (index = 0; index < dataArray.count;) {
NSString * name;
NSInteger width = 100;
BOOL visible = NO;
name = dataArray[index++];
if (index < dataArray.count) {
visible = [dataArray[index++] integerValue] == YES;
}
if (index < dataArray.count) {
width = [dataArray[index++] integerValue];
}
field = [db fieldByName:name];
field.visible = visible;
field.width = width;
}
// Set the default fonts
[self setTableViewFont];
// Get the default list of visible columns
[self updateVisibleColumns];
// In condensed mode, the summary field takes up the whole space.
articleList.columnAutoresizingStyle = NSTableViewUniformColumnAutoresizingStyle;
NSMenu *articleListMenu = [[NSMenu alloc] init];
[articleListMenu addItemWithTitle:NSLocalizedStringWithDefaultValue(@"markRead.menuItem",
nil,
NSBundle.mainBundle,
@"Mark Read",
@"Title of a menu item")
action:@selector(markAsRead:)
keyEquivalent:@""];
[articleListMenu addItemWithTitle:NSLocalizedString(@"Mark Unread", @"Title of a menu item")
action:@selector(markAsUnread:)
keyEquivalent:@""];
[articleListMenu addItemWithTitle:NSLocalizedString(@"Mark Flagged", @"Title of a menu item")
action:@selector(toggleFlag:)
keyEquivalent:@""];
[articleListMenu addItemWithTitle:NSLocalizedString(@"Delete Article", @"Title of a menu item")
action:@selector(delete:)
keyEquivalent:@""];
[articleListMenu addItemWithTitle:NSLocalizedString(@"Restore Article", @"Title of a menu item")
action:@selector(restore:)
keyEquivalent:@""];
[articleListMenu addItemWithTitle:NSLocalizedString(@"Download Enclosure", @"Title of a menu item")
action:@selector(downloadEnclosure:)
keyEquivalent:@""];
[articleListMenu addItem:[NSMenuItem separatorItem]];
[articleListMenu addItemWithTitle:NSLocalizedString(@"Open Subscription Home Page", @"Title of a menu item")
action:@selector(viewSourceHomePage:)
keyEquivalent:@""];
NSMenuItem *openFeedInBrowser = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Open Subscription Home Page in External Browser", @"Title of a menu item")
action:@selector(viewSourceHomePageInAlternateBrowser:)
keyEquivalent:@""];
openFeedInBrowser.keyEquivalentModifierMask = NSEventModifierFlagOption;
openFeedInBrowser.alternate = YES;
[articleListMenu addItem:openFeedInBrowser];
[articleListMenu addItemWithTitle:NSLocalizedString(@"Open Article Page", @"Title of a menu item")
action:@selector(viewArticlePages:)
keyEquivalent:@""];
NSMenuItem *openItemInBrowser = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Open Article Page in External Browser", @"Title of a menu item")
action:@selector(viewArticlePagesInAlternateBrowser:)
keyEquivalent:@""];
openItemInBrowser.keyEquivalentModifierMask = NSEventModifierFlagOption;
openItemInBrowser.alternate = YES;
[articleListMenu addItem:openItemInBrowser];
articleListMenu.delegate = self;
articleList.menu = articleListMenu;
// Set the target for double-click actions
articleList.doubleAction = @selector(doubleClickRow:);
articleList.action = @selector(singleClickRow:);
articleList.delegate = self;
articleList.dataSource = self;
articleList.target = self;
articleList.accessibilityValueDescription = NSLocalizedString(@"Articles", nil);
}
/* singleClickRow
* Handle a single click action. If the click was in the read or flagged column then
* treat it as an action to mark the article read/unread or flagged/unflagged. If
* the click lands on the enclosure colum, download the associated enclosure.
*/
-(IBAction)singleClickRow:(id)sender
{
NSInteger row = articleList.clickedRow;
NSInteger column = articleList.clickedColumn;
NSArray * allArticles = self.articleController.allArticles;
if (row >= 0 && row < (NSInteger)allArticles.count) {
NSArray * columns = articleList.tableColumns;
if (column >= 0 && column < (NSInteger)columns.count) {
Article * theArticle = allArticles[row];
NSString * columnName = ((NSTableColumn *)columns[column]).identifier;
if ([columnName isEqualToString:MA_Field_Read]) {
[self.articleController markReadByArray:@[theArticle] readFlag:!theArticle.isRead];
return;
}
if ([columnName isEqualToString:MA_Field_Flagged]) {
[self.articleController markFlaggedByArray:@[theArticle] flagged:!theArticle.isFlagged];
return;
}
if ([columnName isEqualToString:MA_Field_HasEnclosure]) {
// TODO: Do interesting stuff with the enclosure here.
}
}
}
}
/* doubleClickRow
* Handle double-click on the selected article. Open the original feed item in
* the default browser.
*/
-(IBAction)doubleClickRow:(id)sender
{
NSInteger clickedRow = articleList.clickedRow;
if (clickedRow != -1) {
Article * theArticle = self.articleController.allArticles[clickedRow];
[self.appController openURLFromString:theArticle.link inPreferredBrowser:YES];
}
}
/* ensureSelectedArticle
* Ensure that there is a selected article and that it is visible.
*/
-(void)ensureSelectedArticle
{
if (articleList.selectedRow == -1) {
[self makeRowSelectedAndVisible:0];
} else {
[articleList scrollRowToVisible:articleList.selectedRow];
}
}
/* updateVisibleColumns
* Iterates through the array of visible columns and makes them
* visible or invisible as needed.
*/
-(void)updateVisibleColumns
{
NSArray *fields = Database.sharedManager.fields;
NSInteger count = fields.count;
NSInteger index;
// Save current selection
NSIndexSet * selArray = articleList.selectedRowIndexes;
// Mark we're doing an update of the tableview
isInTableInit = YES;
[articleList setAutosaveName:nil];
[self updateArticleListRowHeight];
// Create the new columns
for (index = 0; index < count; ++index) {
Field * field = fields[index];
NSString * identifier = field.name;
BOOL showField;
// Handle which fields can be visible in the condensed (vertical) layout
// versus the report (horizontal) layout
if (tableLayout == VNALayoutReport) {
showField = field.isVisible && ![identifier isEqualToString:MA_Field_Headlines];
} else {
showField = NO;
if ([identifier isEqualToString:MA_Field_Read] || [identifier isEqualToString:MA_Field_Flagged] || [identifier isEqualToString:MA_Field_HasEnclosure]) {
showField = field.isVisible;
}
if ([identifier isEqualToString:MA_Field_Headlines]) {
showField = YES;
}
}
// Set column hidden or shown
NSTableColumn *col = [articleList tableColumnWithIdentifier:identifier];
col.hidden = !showField;
// Add to the end only those columns which should be visible
// and aren't created yet
if (showField && [articleList columnWithIdentifier:identifier]==-1) {
NSTableColumn * column = [[NSTableColumn alloc] initWithIdentifier:identifier];
// Replace the normal text field cell with a progress text cell so we can
// display a progress indicator when loading HTML pages. NOTE: This is handled
// in willDisplayCell:forTableColumn:row: where it sets the inProgress flag.
// We need to use a different column for condensed layout vs. table layout.
BOOL isProgressColumn = NO;
if (tableLayout == VNALayoutReport && [column.identifier isEqualToString:MA_Field_Subject]) {
isProgressColumn = YES;
}
if (tableLayout == VNALayoutCondensed && [column.identifier isEqualToString:MA_Field_Headlines]) {
isProgressColumn = YES;
}
if (isProgressColumn) {
ProgressTextCell * progressCell;
progressCell = [[ProgressTextCell alloc] init];
column.dataCell = progressCell;
} else {
VNAVerticallyCenteredTextFieldCell * cell;
cell = [[VNAVerticallyCenteredTextFieldCell alloc] init];
column.dataCell = cell;
}
BOOL isResizable = field.customizationOptions & VNAFieldCustomizationResizing;
column.resizingMask = (isResizable ? NSTableColumnUserResizingMask : NSTableColumnNoResizing);
// the headline column is auto-resizable
column.resizingMask = column.resizingMask | ([column.identifier isEqualToString:MA_Field_Headlines] ? NSTableColumnAutoresizingMask : 0);
// Set the header attributes.
NSTableHeaderCell * headerCell = column.headerCell;
headerCell.title = field.displayName;
// Set the other column atributes.
[column setEditable:NO];
column.minWidth = 10;
[articleList addTableColumn:column];
}
// Set column size for visible columns
if (showField) {
NSTableColumn *column = [articleList tableColumnWithIdentifier:identifier];
column.width = field.width;
}
}
// Set the images for specific header columns
if (@available(macOS 11, *)) {
NSImageSymbolScale scale = NSImageSymbolScaleSmall;
NSImageSymbolConfiguration *config = nil;
config = [NSImageSymbolConfiguration configurationWithScale:scale];
NSImage *readImage = [NSImage imageWithSystemSymbolName:@"circlebadge"
accessibilityDescription:nil];
readImage = [readImage imageWithSymbolConfiguration:config];
NSImage *flagImage = [NSImage imageWithSystemSymbolName:@"flag"
accessibilityDescription:nil];
flagImage = [flagImage imageWithSymbolConfiguration:config];
NSImage *enclImage = [NSImage imageWithSystemSymbolName:@"paperclip"
accessibilityDescription:nil];
enclImage = [enclImage imageWithSymbolConfiguration:config];
[articleList setTableColumnHeaderImage:readImage
forColumnWithIdentifier:MA_Field_Read];
[articleList setTableColumnHeaderImage:flagImage
forColumnWithIdentifier:MA_Field_Flagged];
[articleList setTableColumnHeaderImage:enclImage
forColumnWithIdentifier:MA_Field_HasEnclosure];
} else {
[articleList setTableColumnHeaderImage:[NSImage imageNamed:ACImageNameUnreadHeader]
forColumnWithIdentifier:MA_Field_Read];
[articleList setTableColumnHeaderImage:[NSImage imageNamed:ACImageNameFlaggedHeader]
forColumnWithIdentifier:MA_Field_Flagged];
[articleList setTableColumnHeaderImage:[NSImage imageNamed:ACImageNameEnclosureHeader]
forColumnWithIdentifier:MA_Field_HasEnclosure];
}
// Initialise the sort direction
[self showSortDirection];
// Put the selection back
[articleList selectRowIndexes:selArray byExtendingSelection:NO];
if (tableLayout == VNALayoutReport) {
articleList.autosaveName = @"Vienna3ReportLayoutColumns";
} else {
articleList.autosaveName = @"Vienna3CondensedLayoutColumns";
}
[articleList setAutosaveTableColumns:YES];
// Done
isInTableInit = NO;
}
/* saveTableSettings
* Save the table column settings, specifically the visibility and width.
*/
-(void)saveTableSettings
{
Preferences * prefs = [Preferences standardPreferences];
// Remember the current folder and article
NSString * guid = self.selectedArticle.guid;
[prefs setInteger:self.articleController.currentFolderId forKey:MAPref_CachedFolderID];
[prefs setString:(guid != nil ? guid : @"") forKey:MAPref_CachedArticleGUID];
// An array we need for the settings
NSMutableArray * dataArray = [[NSMutableArray alloc] init];
// Create the new columns
for (Field *field in Database.sharedManager.fields) {
[dataArray addObject:field.name];
[dataArray addObject:@(field.isVisible)];
[dataArray addObject:@(field.width)];
}
// Save these to the preferences
[prefs setObject:dataArray forKey:MAPref_ArticleListColumns];
// We're done
}
/* setTableViewFont
* Gets the font for the article list and adjusts the table view
* row height to properly display that font.
*/
-(void)setTableViewFont
{
Preferences * prefs = [Preferences standardPreferences];
articleListFont = prefs.articleListFont;
articleListUnreadFont = [prefs boolForKey:MAPref_ShowUnreadArticlesInBold] ? [[NSFontManager sharedFontManager] convertWeight:YES ofFont:articleListFont] : articleListFont;
reportCellDict[NSFontAttributeName] = articleListFont;
unreadReportCellDict[NSFontAttributeName] = articleListUnreadFont;
topLineDict[NSFontAttributeName] = articleListFont;
unreadTopLineDict[NSFontAttributeName] = articleListUnreadFont;
middleLineDict[NSFontAttributeName] = articleListFont;
linkLineDict[NSFontAttributeName] = articleListFont;
bottomLineDict[NSFontAttributeName] = articleListFont;
[self updateArticleListRowHeight];
}
/* updateArticleListRowHeight
* Compute the number of rows that the current view requires. For table layout, there's just
* one line. For condensed layout, the number of lines depends on which fields are visible but
* there's always a minimum of one line anyway.
*/
-(void)updateArticleListRowHeight
{
Database * db = [Database sharedManager];
CGFloat height = [self.layoutManager defaultLineHeightForFont:articleListFont];
NSInteger numberOfRowsInCell;
if (tableLayout == VNALayoutReport) {
numberOfRowsInCell = 1;
} else {
numberOfRowsInCell = 0;
if ([db fieldByName:MA_Field_Subject].isVisible) {
++numberOfRowsInCell;
}
if ([db fieldByName:MA_Field_Folder].isVisible || [db fieldByName:MA_Field_LastUpdate].isVisible || [db fieldByName:MA_Field_Author].isVisible) {
++numberOfRowsInCell;
}
if ([db fieldByName:MA_Field_Link].isVisible) {
++numberOfRowsInCell;
}
if ([db fieldByName:MA_Field_Summary].isVisible) {
++numberOfRowsInCell;
}
if (numberOfRowsInCell == 0) {
++numberOfRowsInCell;
}
}
articleList.rowHeight = (height + 2.0f) * (CGFloat)numberOfRowsInCell;
}
/* showSortDirection
* Shows the current sort column and direction in the table.
*/
-(void)showSortDirection
{
NSString * sortColumnIdentifier = self.articleController.sortColumnIdentifier;
if (!sortColumnIdentifier) {
sortColumnIdentifier = [Preferences.standardPreferences stringForKey:MAPref_SortColumn];
}
for (NSTableColumn * column in articleList.tableColumns) {
if ([column.identifier isEqualToString:sortColumnIdentifier]) {
// These NSImage names are available in AppKit, but not as constants.
// https://developer.apple.com/library/archive/releasenotes/AppKit/RN-AppKitOlderNotes/
NSImageName imageName = ([Preferences.standardPreferences.articleSortDescriptors[0] ascending]) ? @"NSAscendingSortIndicator" : @"NSDescendingSortIndicator";
articleList.highlightedTableColumn = column;
[articleList setIndicatorImage:[NSImage imageNamed:imageName] inTableColumn:column];
} else {
// Remove any existing image in the column header.
[articleList setIndicatorImage:nil inTableColumn:column];
}
}
}
/* scrollToArticle
* Moves the selection to the specified article.
*/
-(void)scrollToArticle:(NSString *)guid
{
if (guid != nil) {
NSInteger rowIndex = 0;
for (Article * thisArticle in self.articleController.allArticles) {
if ([thisArticle.guid isEqualToString:guid]) {
[self makeRowSelectedAndVisible:rowIndex];
return;
}
++rowIndex;
}
} else {
[articleList scrollRowToVisible:0];
}
[articleList deselectAll:self];
[self refreshArticleAtCurrentRow];
}
/* mainView
* Return the primary view of this view.
*/
-(NSView *)mainView
{
return articleList;
}
/* makeTextStandardSize
* Reset webview text size to default
*/
-(IBAction)makeTextStandardSize:(id)sender
{
[articleText resetTextSize];
}
/* makeTextSmaller
* Make webview text size smaller
*/
-(IBAction)makeTextSmaller:(id)sender
{
[articleText decreaseTextSize];
}
/* makeTextLarger
* Make webview text size larger
*/
-(IBAction)makeTextLarger:(id)sender
{
[articleText increaseTextSize];
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
/* selectedArticle
* Returns the selected article, or nil if no article is selected.
*/
-(Article *)selectedArticle
{
NSInteger currentSelectedRow = articleList.selectedRow;
return (currentSelectedRow >= 0 && currentSelectedRow < self.articleController.allArticles.count) ? self.articleController.allArticles[currentSelectedRow] : nil;
}
/* printDocument
* Print the active article.
*/
-(void)printDocument:(id)sender
{
[articleText printDocument:sender];
}
/* handleArticleListFontChange
* Called when the user changes the article list font and/or size in the Preferences
*/
-(void)handleArticleListFontChange:(NSNotification *)note
{
[self setTableViewFont];
if (self == self.articleController.mainArticleView) {
[articleList reloadData];
}
}
/* handleLoadFullHTMLChange
* Called when the user changes the folder setting to load the article in full HTML.
*/
-(void)handleLoadFullHTMLChange:(NSNotification *)note
{
if (self == self.articleController.mainArticleView) {
[self refreshArticlePane];
}
}
/* handleReadingPaneChange
* Respond to the change to the reading pane orientation.
*/
-(void)handleReadingPaneChange:(NSNotification *)notification
{
if (self == self.articleController.mainArticleView) {
[self setOrientation:[Preferences standardPreferences].layout];
[self updateVisibleColumns];
[articleList reloadData];
}
}
/* handleStyleChange
* Respond to an article style change
*/
-(void)handleStyleChange:(NSNotification *)notification
{
if (self == self.articleController.mainArticleView) {
[self performSelector:@selector(refreshArticleAtCurrentRow) withObject:nil afterDelay:0.0];
}
}
/* setOrientation
* Adjusts the article view orientation and updates the article list row
* height to accommodate the summary view
*/
-(void)setOrientation:(NSInteger)newLayout
{
isChangingOrientation = YES;
tableLayout = newLayout;
splitView2.autosaveName = nil;
splitView2.vertical = (newLayout == VNALayoutCondensed);
if (splitView2.vertical) {
splitView2.dividerStyle = NSSplitViewDividerStyleThin;
splitView2.autosaveName = @"Vienna3SplitView2CondensedLayout";
} else {
splitView2.dividerStyle = NSSplitViewDividerStylePaneSplitter;
splitView2.autosaveName = @"Vienna3SplitView2ReportLayout";
}
[splitView2 display];
isChangingOrientation = NO;
}
/* makeRowSelectedAndVisible
* Selects the specified row in the table and makes it visible by
* scrolling it to the center of the table.
*/
-(void)makeRowSelectedAndVisible:(NSInteger)rowIndex
{
if (self.articleController.allArticles.count == 0u) {
[articleList deselectAll:self];
} else if (rowIndex != articleList.selectedRow) {
[articleList selectRowIndexes:[NSIndexSet indexSetWithIndex:rowIndex] byExtendingSelection:NO];
// make sure our current selection is visible
[articleList scrollRowToVisible:rowIndex];
// then try to center it in the list
NSInteger pageSize = [articleList rowsInRect:articleList.visibleRect].length;
NSInteger lastRow = articleList.numberOfRows - 1;
NSInteger visibleRow = rowIndex + (pageSize / 2);
if (visibleRow > lastRow) {
visibleRow = lastRow;
}
[articleList scrollRowToVisible:visibleRow];
}
}
/*
* viewNextUnreadInFolder
* Search the following unread article in the current folder
* and select it if found
*/
-(BOOL)viewNextUnreadInFolder
{
return [self viewNextUnreadInCurrentFolder:(articleList.selectedRow + 1)];
}
/* viewNextUnreadInCurrentFolder
* Select the next unread article in the current folder after currentRow.
*/
-(BOOL)viewNextUnreadInCurrentFolder:(NSInteger)currentRow
{
if (currentRow < 0) {
currentRow = 0;
}
NSArray * allArticles = self.articleController.allArticles;
NSInteger totalRows = allArticles.count;
Article * theArticle;
while (currentRow < totalRows) {
theArticle = allArticles[currentRow];
if (!theArticle.isRead) {
[self makeRowSelectedAndVisible:currentRow];
return YES;
}
++currentRow;
}
return NO;
}
// Display the enclosure view below the article list view.
- (void)showEnclosureView {
NSUserDefaults *userDefaults = NSUserDefaults.standardUserDefaults;
if (![userDefaults boolForKey:MAPref_ShowEnclosureBar]) {
return;
}
if (![self.contentStackView.views containsObject:self.enclosureView]) {
self.articleTextView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentStackView addView:self.enclosureView
inGravity:NSStackViewGravityTop];
}
}
// Hide the enclosure view if it is present.
- (void)hideEnclosureView {
if ([self.contentStackView.views containsObject:self.enclosureView]) {
self.articleTextView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentStackView removeView:self.enclosureView];
}
}
/* selectFirstUnreadInFolder
* Moves the selection to the first unread article in the current article list or the
* first article if the folder has no unread articles.
*/
-(BOOL)selectFirstUnreadInFolder
{
BOOL result = [self viewNextUnreadInCurrentFolder:-1];
if (!result) {
NSInteger count = self.articleController.allArticles.count;
if (count > 0) {
[self makeRowSelectedAndVisible:0];
}
}
return result;
}
-(void)scrollDownDetailsOrNextUnread
{
if (articleText.canScrollDown) {
[(NSView *)articleText scrollPageDown:nil];
} else {
ArticleController * articleController = self.articleController;
[articleController markReadByArray:self.markedArticleRange readFlag:YES];
[articleController displayNextUnread];
}
}
-(void)scrollLineDownDetails
{
[(NSView *)articleText scrollLineDown:nil];
}
-(void)scrollLineUpDetails
{
[(NSView *)articleText scrollLineUp:nil];
}
-(void)scrollUpDetailsOrGoBack
{
if (articleText.canScrollUp) {
[(NSView *)articleText scrollPageUp:nil];
} else {
[self.articleController goBack:nil];
}
}
/* performFindPanelAction
* Implement the search action.
*/
-(void)performFindPanelAction:(NSInteger)actionTag
{
[self.articleController reloadArrayOfArticles];
// This action is send continuously by the filter field, so make sure not the mark read while searching
if (articleList.selectedRow < 0 && self.articleController.allArticles.count > 0 ) {
BOOL shouldSelectArticle = YES;
if ([Preferences standardPreferences].markReadInterval > 0.0f) {
Article * article = self.articleController.allArticles[0u];
if (!article.isRead) {
shouldSelectArticle = NO;
}
}
if (shouldSelectArticle) {
[self makeRowSelectedAndVisible:0];
}
}
}
/* refreshFolder
* Refreshes the current folder by applying the current sort or thread
* logic and redrawing the article list. The selected article is preserved
* and restored on completion of the refresh.
*/
-(void)refreshFolder:(NSInteger)refreshFlag
{
blockSelectionHandler = YES;
Article * currentSelectedArticle = self.selectedArticle;
switch (refreshFlag) {
case VNARefreshRedrawList:
break;
case VNARefreshReapplyFilter:
[self.articleController refilterArrayOfArticles];
[self.articleController sortArticles];
break;
case VNARefreshSortAndRedraw:
[self.articleController sortArticles];
[self showSortDirection];
break;
}
[articleList reloadData];
[self scrollToArticle:currentSelectedArticle.guid];
blockSelectionHandler = NO;
}
/* refreshImmediatelyArticleAtCurrentRow
* Refreshes the article at the current selected row.
*/
-(void)refreshImmediatelyArticleAtCurrentRow
{
[self refreshArticlePane];
Article * theArticle = self.selectedArticle;
if (theArticle != nil && !theArticle.isRead) {
CGFloat interval = [Preferences standardPreferences].markReadInterval;
if (interval > 0 && !isAppInitialising) {
markReadTimer = [NSTimer scheduledTimerWithTimeInterval:(double)interval
target:self
selector:@selector(markCurrentRead:)
userInfo:nil
repeats:NO];
}
}
}
/* refreshArticleAtCurrentRow
* Refreshes the article at the current selected row.
*/
-(void)refreshArticleAtCurrentRow
{
Article * article = self.selectedArticle;
if (article == nil) {
[articleText setArticles:@[]];
[self hideEnclosureView];
} else {
[self refreshImmediatelyArticleAtCurrentRow];
// Add this to the backtrack list
NSString * guid = article.guid;
[self.articleController addBacktrack:guid];
}
}
/* handleRefreshArticle
* Respond to the notification to refresh the current article pane.
*/
-(void)handleRefreshArticle:(NSNotification *)nc
{
if (self == self.articleController.mainArticleView && !isAppInitialising) {
[self refreshArticlePane];
}
}
/* handleArticleViewEnded
* Handle the end of a load whether or not it completed and whether or not an
* error occurred.
*/
- (void)handleArticleViewEnded:(NSNotification *)nc
{
if (nc.object == articleText) {
[self endMainFrameLoad];
}
}
/* loadArticleLink
* Loads the specified link into the article text view. NOTE: This is done
* via this selector method so that this is called via the event queue in
* order to give the WebView drawing a chance to clear out the WebView
* before this link is loaded.
*/
-(void)loadArticleLink:(NSString *) articleLink
{
// Remember we're loading from HTML so the status message is set
// appropriately.
[self startMainFrameLoad];
// Load the actual link.
articleText.tabUrl = cleanedUpUrlFromString(articleLink);
[articleText loadTab];