示例#1
0
        public virtual bool WriteToFile(NSString path)
        {
            NSFileManager fm = NSFileManager.DefaultManager;
            NSString tmpPath = @"";
            bool isDir = false;
            bool success = false;
            bool path_is_standard = true;

            /*
             * We need to initialize before saving, to avoid the new file being
             * counted as a different list thus making us appear twice
             */
            NSColorList._LoadAvailableColorLists(null);

            if (path == null)
            {
                NSArray paths;

                // FIXME the standard path for saving color lists?
                paths = NSSearchPathForDirectoriesInDomains(
                    NSSearchPathDirectory.NSLibraryDirectory,
                    NSSearchPathDomainMask.NSUserDomainMask, true);

                if (paths.Count == 0)
                {
                    //NSLog (@"Failed to find Library directory for user");
                    return false;	// No directory to save to.
                }
                path = ((NSString)paths.ObjectAtIndex(0)).StringByAppendingPathComponent(@"Colors");
                isDir = true;
            }
            else
            {
                fm.FileExistsAtPath(path, ref isDir);
            }

            if (isDir)
            {
                _fullFileName = path.StringByAppendingPathComponent(_name).StringByAppendingPathExtension(@"clr");
            }
            else // it is a file
            {
                if (path.PathExtension().IsEqualToString(@"clr") == true)
                {
                    _fullFileName = path;
                }
                else
                {
                    _fullFileName = path.StringByDeletingPathExtension().StringByAppendingPathExtension(@"clr");
                }
                path = path.StringByDeletingLastPathComponent();
            }

            // Check if the path is a standard path
            if (path.LastPathComponent().IsEqualToString(@"Colors") == false)
            {
                path_is_standard = false;
            }
            else
            {
                tmpPath = path.StringByDeletingLastPathComponent();
                if (!NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.NSLibraryDirectory, NSSearchPathDomainMask.NSAllDomainsMask, true).ContainsObject(tmpPath))
                {
                    path_is_standard = false;
                }
            }

            /*
             * If path is standard and it does not exist, try to create it.
             * System standard paths should always be assumed to exist;
             * this will normally then only try to create user paths.
             */
            if (path_is_standard && (fm.FileExistsAtPath(path) == false))
            {
                NSError err = null;
                if (fm.CreateDirectoryAtPath(path, true, null, ref err))
                {
                    //NSLog (@"Created standard directory %@", path);
                }
                else
                {
                    //NSLog (@"Failed attempt to create directory %@", path);
                }
            }

            //success = [NSArchiver archiveRootObject: self  toFile: _fullFileName];

            if (success && path_is_standard)
            {
                _colorListLock.Lock();
                if (_availableColorLists.ContainsObject(this) == false)
                    _availableColorLists.AddObject(this);

                _colorListLock.Unlock();
                return true;
            }

            return success;
        }