public static SmileyPack CopyPack(SmileyPack pack, string newName)
        {
            var newPack = AddPack(newName);

            foreach (var smiley in pack.Smileys)
            {
                string oldPath = Path.Combine(GetPath(pack), smiley.FileName);
                string newPath = Path.Combine(GetPath(newPack), smiley.FileName);

                File.Copy(oldPath, newPath);

                var newSmiley = new Smiley();
                newSmiley.FileName = smiley.FileName;
                newSmiley.Image    = GetImage(newPath);

                foreach (string s in smiley.Shortcuts)
                {
                    newSmiley.Shortcuts.Add(s);
                }

                newPack.Smileys.Add(smiley);
            }

            return(newPack);
        }
        public static SmileyPack AddPack(string name)
        {
            var pack = new SmileyPack(name, false);

            Directory.CreateDirectory(GetPath(pack));
            Packs.Add(pack);
            return(pack);
        }
        public static void SetCurrentPack(string packName)
        {
            CurrentPack = Packs.FirstOrDefault(p => p.Name == packName);

            if (CurrentPack == null)
            {
                CurrentPack = Packs.First();
            }
        }
        private static string GetUniquePath(SmileyPack pack, string fileName)
        {
            string pathPrefix = string.Empty;

            while (File.Exists(Path.Combine(GetPath(pack), pathPrefix + fileName)))
            {
                pathPrefix += SmileyNamePrefix;
            }
            return(Path.Combine(GetPath(pack), pathPrefix + fileName));
        }
        public static void SetSmileyImage(SmileyPack containingPack, Smiley smiley, string imagePath)
        {
            string oldFile = Path.Combine(GetPath(containingPack), smiley.FileName);
            string newFile = GetUniquePath(containingPack, Path.GetFileName(imagePath));

            File.Delete(oldFile);
            File.Copy(imagePath, newFile);
            smiley.FileName = Path.GetFileName(newFile);
            smiley.Image    = GetImage(newFile);
        }
        public static void RemoveSmiley(Smiley smiley, SmileyPack pack)
        {
            if (pack.IsDefault)
            {
                throw new ArgumentException("Cannot change a default pack.");
            }

            pack.Smileys.Remove(smiley);
            File.Delete(Path.Combine(CustomSmileysFolder, pack.Name, smiley.FileName));
        }
        public static void DeletePack(SmileyPack pack)
        {
            if (pack.IsDefault)
            {
                throw new ArgumentException("Cannot delete a default pack.");
            }

            Packs.Remove(pack);
            Directory.Delete(GetPath(pack), true);

            CurrentPack = Packs.First();
        }
        public static void Load()
        {
            string currentPackName = CurrentPack == null ? null : CurrentPack.Name;

            Packs.Clear();

            var defaultDirs = new DirectoryInfo(DefaultSmileysFolder).EnumerateDirectories();
            var customDirs  = new DirectoryInfo(CustomSmileysFolder).EnumerateDirectories();

            foreach (var dir in defaultDirs.Concat(customDirs))
            {
                var  doc       = XDocument.Load(Path.Combine(dir.FullName, ManifestFileName));
                bool isDefault = bool.Parse(doc.Root.Attribute(IsDefaultAttributeName).Value);
                var  pack      = new SmileyPack(dir.Name, isDefault);

                foreach (var elem in doc.Root.Elements(SmileyElementName))
                {
                    string   fileName  = elem.Attribute(FileNameAttributeName).Value;
                    string[] shortcuts = elem.Attribute(ShortcutsAttributeName).Value
                                         .Split(SmileyShortcutsSeparator);

                    var smiley = new Smiley();
                    smiley.FileName = fileName;
                    smiley.Image    = GetImage(Path.Combine(GetPath(pack), fileName));

                    foreach (string s in shortcuts)
                    {
                        smiley.Shortcuts.Add(s);
                    }

                    pack.Smileys.Add(smiley);
                }

                Packs.Add(pack);
            }

            if (currentPackName == null)
            {
                CurrentPack = Packs.First();
            }
            else
            {
                SetCurrentPack(currentPackName);
            }
        }
 private static string GetPath(SmileyPack pack)
 {
     return(Path.Combine(pack.IsDefault ? DefaultSmileysFolder : CustomSmileysFolder, pack.Name));
 }
Пример #10
0
        public static Smiley AddSmiley(string imagePath, IEnumerable <string> shortcuts, SmileyPack pack)
        {
            if (pack.IsDefault)
            {
                throw new ArgumentException("Cannot change a default pack.");
            }

            string fileName = Path.GetFileName(imagePath);
            string newPath  = GetUniquePath(pack, fileName);

            File.Copy(imagePath, newPath);

            var smiley = new Smiley();

            smiley.FileName = Path.GetFileName(newPath);
            smiley.Image    = GetImage(newPath);

            foreach (string s in shortcuts)
            {
                smiley.Shortcuts.Add(s);
            }

            pack.Smileys.Add(smiley);

            return(smiley);
        }