Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
375 lines (248 sloc) 9.87 KB
/*
* Librairie - Comic library for Mac OS
* Copyright (C) 2019 Old-Geek
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#import "SidebarView.h"
#import "Item/SidebarItem.h"
#import "DBCommon.h"
#import "ImageFactory.h"
#import "FileUtil.h"
#import "LibrairieFolder.h"
#import "RenameDialog.h"
#import "MainCache.h"
@implementation SidebarView
RenameDialog *renameDialog;
NSViewController *currentContentViewController;
NSMutableDictionary *sideBarItem;
NSArray *topLevelItem;
NSArray *folderList;
NSArray *librairiesList;
static NSString *pathSelected;
int librarySelected = 0;
int clickedRow;
BOOL folderNIBLoaded = NO;
+ (NSString *)getPath {
return pathSelected;
}
- (id)init {
if (self = [super init]) {
[MainViewController sMainViewController].delegateSidebar = self;
[self listener];
[WriteZip sWriteZip].delegateSidebar = self;
}
return self;
}
- (void)viewLoaded {
[self initLists];
progressindicator.appearance = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
}
- (void)initLists {
librairiesList = nil;
sideBarItem = nil;
LibrairieDB *libdb = [[LibrairieDB alloc] init];
librairiesList = [libdb getAllLibrairies];
topLevelItem = @[@"Librairies", @"Dossiers"];
sideBarItem = [NSMutableDictionary new];
NSMutableArray *libraryName = [NSMutableArray array];
for (LibrairieFolder *robert in librairiesList)
[libraryName addObject:robert.name];
if ([librairiesList count] > 0) {
sideBarItem[@"Librairies"] = libraryName;
[libSelectedTitle setStringValue:libraryName[0]];
} else {
sideBarItem[@"Librairies"] = @[@"Vide"];
}
BOOL folder = NO;
if ( [librairiesList count] > 0) {
FileUtil *fileUtil = [FileUtil alloc];
LibrairieFolder *comic = librairiesList[librarySelected];
folderList = [fileUtil subfolderList:comic.path];
sideBarItem[@"Dossiers"] = folderList;
[self addSubFolder];
if ([folderList count] > 0)
folder = YES;
}
[_sidebarView reloadData];
for (NSString *item in topLevelItem)
[_sidebarView expandItem:item];
if (folder)
[_sidebarView selectRowIndexes:[NSIndexSet indexSetWithIndex:[librairiesList count] + 2] byExtendingSelection:NO];
}
- (void)setLibrairiesView:(NSString *)name selectedRow:(int)row {
librarySelected = row -1;
if ([librairiesList count] > 0) {
sideBarItem = nil;
folderList = nil;
sideBarItem = [NSMutableDictionary new];
NSMutableArray *libraryName = [NSMutableArray array];
for (LibrairieFolder *robert in librairiesList)
[libraryName addObject:robert.name];
sideBarItem[@"Librairies"] = libraryName;
FileUtil *fileUtil = [FileUtil alloc];
LibrairieFolder *comic = librairiesList[(row -1)];
folderList = [fileUtil subfolderList:comic.path];
sideBarItem[@"Dossiers"] = folderList;
[self addSubFolder];
[_sidebarView reloadData];
}
}
- (void)addSubFolder {
FileUtil *fileUtil = [FileUtil alloc];
LibrairieFolder *comic = librairiesList[librarySelected];
for (NSString *string in folderList) {
NSString *path = [NSString stringWithFormat:@"%@/%@", comic.path, string];
if ([fileUtil hasSubfolder:path]) {
NSArray *array = [fileUtil subfolderList:path];
sideBarItem[string] = array;
}
}
}
- (NSString *)getPath:(NSString *)item {
LibrairieFolder *comic = librairiesList[librarySelected];
NSMutableString *string = [NSMutableString stringWithString:comic.path];
NSString *itemName = item;
NSMutableArray *array = [NSMutableArray array];
while (![itemName isEqualToString:@"Dossiers"]) {
[array addObject:itemName];
itemName = [_sidebarView parentForItem:itemName];
}
NSArray* reversedArray = [[array reverseObjectEnumerator] allObjects];
for (NSString *str in reversedArray)
[string appendString:[NSString stringWithFormat:@"/%@", str]];
return string;
}
- (void)setContentView:(NSString *)name selectedRow:(int)row {
if ([[_sidebarView parentForItem:name] isEqualToString:@"Librairies"]) {
[self setLibrairiesView:name selectedRow:row];
[libSelectedTitle setStringValue:name];
[_sidebarView selectRowIndexes:[NSIndexSet indexSetWithIndex:[librairiesList count] + 2] byExtendingSelection:NO];
} else {
[self setFolderView:name selectedRow:row];
}
}
- (void)setFolderView:(NSString *)name selectedRow:(int)row {
// annule la mise en cache des covers actuel
[[MainCache sMainCache] purgeCoverLoader];
[mainWindow setTitle:name];
int count = row - [librairiesList count] -2;
if (count > -1)
pathSelected = [self getPath:name];
if (!folderNIBLoaded) {
[self setView];
folderNIBLoaded = YES;
} else {
[[MainViewController sMainViewController] reloadFolder:YES];
}
}
- (void)setView {
if (currentContentViewController)
[[currentContentViewController view] removeFromSuperview];
currentContentViewController = [[NSViewController alloc] initWithNibName:@"Folder" bundle:nil];
NSView *view = [currentContentViewController view];
view.frame = masterView.bounds;
[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[masterView addSubview:view];
}
- (void)outlineViewSelectionDidChange:(NSNotification *)notification {
if ([_sidebarView selectedRow] != -1) {
NSString *item = [_sidebarView itemAtRow:[_sidebarView selectedRow]];
[self setContentView:item selectedRow:[_sidebarView selectedRow]];
}
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
if (item == nil)
return [topLevelItem count];
return [sideBarItem[item] count];
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
if (item == nil)
return topLevelItem[index];
return sideBarItem[item][index];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
if ([sideBarItem[item] count] >0 )
return YES;
return [outlineView parentForItem:item] == nil;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item {
return [outlineView parentForItem:item] != nil;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
return [topLevelItem containsObject:item];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldShowOutlineCellForItem:(id)item {
return ![item isEqualToString:@"Librairies"];
}
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
if ([topLevelItem containsObject:item]) {
NSTextField *result = [outlineView makeViewWithIdentifier:@"HeaderTextField" owner:self];
[result setStringValue:item];
return result;
}
SidebarItem *result = [outlineView makeViewWithIdentifier:@"MainCell" owner:self];
result.textField.stringValue = item;
if ([[outlineView parentForItem:item] isEqualToString:@"Librairies"])
result.imageView.image = [NSImage imageNamed:@"sidebar_library"];
else {
NSImage *img = [[NSImage imageNamed:@"NSFolder"] resizeWidth:sidebariconsize];
result.imageView.image = img;
}
return result;
}
- (void)onProgressUpdate:(double)progress count:(int)count finish:(BOOL)end {
if (!end) {
progressCount.stringValue = [NSString stringWithFormat:@"%d", count];
progressindicator.doubleValue = progress*100;
[progressindicator setHidden:NO];
[progressCount setHidden:NO];
} else {
[progressindicator setHidden:YES];
[progressCount setHidden:YES];
}
}
- (void)listener {
[[NSNotificationCenter defaultCenter] addObserverForName:@"sidebarListener" object:nil queue:nil usingBlock:^(NSNotification *note) {
[self initLists];
}];
}
- (void)menuNeedsUpdate:(NSMenu *)menu {
// retour via délégate dans MainViewController
clickedRow = [_sidebarView clickedRow];
[menu removeAllItems];
if ([librairiesList count] > 0 && clickedRow > 0 && clickedRow <= [librairiesList count]){
[menu addItemWithTitle:@"Renommer" action:@selector(rename) keyEquivalent:@""];
[menu addItemWithTitle:@"Supprimer" action:@selector(remove) keyEquivalent:@""];
}
}
- (void)rename {
LibrairieFolder *lib = librairiesList[clickedRow - 1];
renameDialog = [[RenameDialog alloc] initWithString:lib.name path:lib.path];
[renameDialog showWindow:self];
}
- (void)remove {
LibrairieFolder *lib = librairiesList[clickedRow - 1];
LibrairieDB *libdb = [[LibrairieDB alloc] init];
[libdb remove:lib.name];
librarySelected = 0;
// TODO
// Why ? O_ô
// not work
//[self initLists];
//work...
[[NSNotificationCenter defaultCenter] postNotificationName:@"sidebarListener" object:nil userInfo:nil];
}
@end
You can’t perform that action at this time.