Example #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;
        }
        //
        // General methods
        //
        // write instance to disk at path; if directory type, this
        // method is recursive; if updateFilenamesFlag is YES, the wrapper
        // will be updated with the name used in writing the file
        public virtual bool WriteToFile(NSString path, bool atomicFlag, bool updateFilenamesFlag)
        {
            NSFileManager fm = NSFileManager.DefaultManager;
            bool success = false;

            //NSDebugLLog(@"NSFileWrapper",
            //            @"writeToFile: %@ atomically: updateFilenames: ", path);

            switch (_wrapperType)
            {
                case GSFileWrapperType.GSFileWrapperDirectoryType:
                    {
                        // FIXME - more robust save proceedure when atomicFlag set
                        NSEnumerator enumerator = ((NSDictionary)_wrapperData).KeyEnumerator();
                        NSString key;
                        NSError err = null;

                        fm.CreateDirectoryAtPath(path, true, _fileAttributes, ref err);
                        while ((key = (NSString)enumerator.NextObject()) != null)
                        {
                            NSString newPath = path.StringByAppendingPathComponent(key);
                            NSFileWrapper fw = (NSFileWrapper)((NSDictionary)_wrapperData).ObjectForKey(key);
                            fw.WriteToFile(newPath, atomicFlag, updateFilenamesFlag);
                        }
                        success = true;
                        break;
                    }
                case GSFileWrapperType.GSFileWrapperRegularFileType:
                    {
                        if (((NSDictionary)_wrapperData).WriteToFile(path, atomicFlag))
                            success = fm.ChangeFileAttributes(_fileAttributes, path);
                        break;
                    }
                //case GSFileWrapperType.GSFileWrapperSymbolicLinkType:
                //    {
                //        success = fm.CreateSymbolicLinkAtPath(path, _wrapperData);
                //        break;
                //    }
            }
            if (success && updateFilenamesFlag)
            {
                SetFilename(path.LastPathComponent());
            }

            return success;
        }
Example #3
0
        public virtual id InitWithName(NSString name, NSString path)
        {
            id self = this;
            NSColorList cl = null;
            bool could_load = false;

            _name = name;
            if (path != null)
            {
                bool isDir = false;
                // previously impl wrongly expected directory containing color file
                // rather than color file; we support this for apps that rely on it
                if ((NSFileManager.DefaultManager.FileExistsAtPath(path, ref isDir) == false) || isDir == true)
                {
                    //NSLog(@"NSColorList -initWithName:fromFile: warning: excluding " @"filename from path (%@) is deprecated.", path);
                    _fullFileName = path.StringByAppendingPathComponent(name).StringByAppendingPathExtension(@"clr");
                }
                else
                {
                    _fullFileName = path;
                }

                // Unarchive the color list

                // TODO [Optm]: Rewrite to initialize directly without unarchiving
                // in another object
                try
                {
                    //FIXME
                    //cl = (NSColorList) NSUnarchiver.UnarchiveObjectWithFile(_fullFileName);
                }
                catch (Exception ex)
                {
                    cl = null;
                }

                if ((cl != null) && (cl.IsKindOfClass(NSColorList.Class)))
                {
                    could_load = true;

                    _is_editable = NSFileManager.DefaultManager.IsWritableFileAtPath(_fullFileName);

                    _colorDictionary = NSMutableDictionary.DictionaryWithDictionary(cl._colorDictionary);

                    _orderedColorKeys = NSMutableArray.ArrayWithArray(cl._orderedColorKeys);
                }
                else if (NSFileManager.DefaultManager.FileExistsAtPath(path))
                {
                    _colorDictionary = (NSMutableDictionary)NSMutableDictionary.Alloc().Init();
                    _orderedColorKeys = (NSMutableArray)NSMutableArray.Alloc().Init();
                    _is_editable = true;

                    if (_ReadTextColorFile(_fullFileName))
                    {
                        could_load = true;
                        _is_editable = NSFileManager.DefaultManager.IsWritableFileAtPath(_fullFileName);
                    }
                    else
                    {
                        _colorDictionary = null;
                        _orderedColorKeys = null;
                    }
                }
            }

            if (could_load == false)
            {
                _fullFileName = null;
                _colorDictionary = (NSMutableDictionary)NSMutableDictionary.Alloc().Init();
                _orderedColorKeys = (NSMutableArray)NSMutableArray.Alloc().Init();
                _is_editable = true;
            }

            return self;
        }
        /**
         * Init an instance from the file, directory, or symbolic link at path.<br />
         * This can create a tree of instances with a directory instance at the top
         */
        public virtual id InitWithPath(NSString path)
        {
            NSFileManager fm = NSFileManager.DefaultManager;
            NSString fileType;

            //NS.DebugLLog(@"NSFileWrapper", @"initWithPath: %@", path);

            // Store the full path in filename, the specification is unclear in this point
            SetFilename(path);
            SetPreferredFilename(path.LastPathComponent());
            SetFileAttributes(fm.FileAttributesAtPath(path, false));

            fileType = FileAttributes().FileType;
            if (fileType.IsEqualToString("NSFileTypeDirectory"))
            {
                NSString filename;
                NSMutableArray fileWrappers = NSMutableArray.Array();
                NSArray filenames = fm.DirectoryContentsAtPath(path);
                NSEnumerator enumerator = filenames.ObjectEnumerator();

                while ((filename = (NSString)enumerator.NextObject()) != null)
                {
                    NSFileWrapper w;

                    w = (NSFileWrapper)NSFileWrapper.Alloc().InitWithPath(path.StringByAppendingPathComponent(filename));
                    fileWrappers.AddObject(w);
                    //RELEASE(w);
                }
                this.InitDirectoryWithFileWrappers((NSDictionary)NSMutableDictionary.DictionaryWithObjectsForKeys(fileWrappers, filenames));
            }
            else if (fileType.IsEqualToString("NSFileTypeRegular"))
            {
                this.InitRegularFileWithContents(NSData.Alloc().InitWithContentsOfFile(path));
            }
            else if (fileType.IsEqualToString("NSFileTypeSymbolicLink"))
            {
                //this.InitSymbolicLinkWithDestination(fm.PathContentOfSymbolicLinkAtPath(path));
            }

            return this;
        }