/*
**  FolderSource.m
**
**  Copyright (c) 2004
**
**  Author: Yen-Ju Chen <yjchenx@hotmail.com>
**
**  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 2 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, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "FolderSource.h"
#include "LibrarySource.h"
#include "GNUstep.h"
#include <Foundation/Foundation.h>
#include "Constants.h"
#include <AppKit/NSPanel.h>

@implementation FolderSource

/* For newer format */
- (unsigned int) _reIndexUniqueNumber
{
  NSLog(@"_reindex Folder");
  unsigned int i, count = [self count];
  unsigned int index, uid;
  NSString *title;
  LibrarySource *library = [LibrarySource sharedSource];
  id object;
  for(i = 0; i < count; i++)
    {
      title = [self titleAtIndex: i];
      index = [library indexOfTitle: title];
      uid = [library uniqueNumberAtIndex: index];
      [self setUniqueNumber: uid atIndex: i];
    }
}


- (BOOL) loadSourceAtPath: (NSString *) path
{
  if ([super loadSourceAtPath: path] == NO) return NO;
  NSString *folderPath = [self path];
  BOOL loadResult;
  loadResult = [self readFromFile: folderPath];
  if (loadResult == YES) 
    {
      makeBackup(folderPath);
      /* Check whether it is a new version */
      if (([self count] > 0) && ([self uniqueNumberAtIndex: 0] == NSNotFound))
        {
          [self _reIndexUniqueNumber];
        }

      /* Remove old one in case the name changes */
      NSFileManager *fm = [NSFileManager defaultManager];
      [fm removeFileAtPath: folderPath handler: nil];
      return YES;
    }
  else
    {
      /* Check backup file */
      NSFileManager *fm = [NSFileManager defaultManager];
      BOOL isDir;
      NSString *backup = [folderPath stringByAppendingString: @"~"];
      loadResult = [self readFromFile: backup];
      if (loadResult == YES)
        {
          int result = NSRunAlertPanel(sFileError, [NSString stringWithFormat: sFileNotExistWantBackup____, sFolder, folderPath, sFolder, sFolder], sYES, sNO, nil, nil);
          if (result == NSAlertDefaultReturn)
            {
              /* Check whether it is a new version */
              if (([self count] > 0) && 
                  ([self uniqueNumberAtIndex: 0] == NSNotFound))
                {
                  [self _reIndexUniqueNumber];
                }
              return YES;
            }
	}
    }
  [container removeAllObjects];
  return YES;
}

- (BOOL) unloadSourceAtPath: (NSString *) path
{
  if ([[self path] isEqualToString: path] == NO)
    {
      /* Load and save file in different name.
       * Move the backup file to new name */
      NSString *extension = [[self path] pathExtension];
      NSString *oldPath = [[self path] stringByAppendingString: @"~"];
      NSString *newPath = [[self path] stringByDeletingLastPathComponent];
      newPath = [newPath stringByAppendingPathComponent: [[path lastPathComponent] stringByAppendingString: @"~"]];
      //NSLog(@"Move backup %@", newPath);

      NSFileManager *fm = [NSFileManager defaultManager];
      [fm movePath: oldPath toPath: newPath handler: nil];
    }
  return [super unloadSourceAtPath: path];
}

@end
