Exemplo n.º 1
0
 public override bool Load()
 {
     Log.WriteLine($"Loading database...");
     if (Directory.Exists(Path))
     {
         Mut.WaitOne();
         Collections.Clear();
         string[] files = Directory.GetFiles(Path);
         foreach (string file in files)
         {
             // file = Full Path
             string     name = System.IO.Path.GetFileNameWithoutExtension(file);
             Collection c    = JsonConvert.DeserializeObject <Collection>(
                 Encoding.UTF8.GetString(File.Sync.ReadFile(file)),
                 SerializerSettings);
             Collections.Add(name, c);
         }
         Mut.ReleaseMutex();
         Log.WriteLine($"Loaded {files.Length} collections.");
         return(true);
     }
     else
     {
         Mut.WaitOne();
         Directory.Create(Path);
         Collections.Clear();
         Mut.ReleaseMutex();
         Log.WriteLine("No database found. Created directory.");
         return(false);
     }
 }
        private void ShowCollections()
        {
            if (Database == string.Empty)
            {
                _userMessageService.ShowMessage("You must specify a non-empty database name");
                return;
            }

            IMongoQuery query = _mongoQueryFactory.BuildQuery();

            try
            {
                IList <string> collections = query.GetCollections(Server, Database, Port);

                Collections.Clear();

                foreach (string collection in collections)
                {
                    Collections.Add(collection);
                }
            }
            catch (UnknownServerException ex)
            {
                _userMessageService.ShowMessage(ex.Message);
            }
        }
        public void ReloadCollections()
        {
            try
            {
                var sw = new Stopwatch();
                sw.Start();

                List <Collection> collections = ImoutoCollectionService.Use(imoutoService =>
                {
                    var res = imoutoService.GetCollections();
                    return(res);
                });

                sw.Stop();
                Debug.WriteLine($"Collections reloaded in {sw.ElapsedMilliseconds}ms.");

                Collections.Clear();
                collections.ForEach(x => Collections.Add(new CollectionVM(x.Id, x.Name)));

                Collections.ForEach(x => x.LoadFolders());

                SelectedCollection = Collections.FirstOrDefault();
            }
            catch (Exception ex)
            {
                App.MainWindowVM?.SetStatusError("Can't reload collections", ex.Message);
                Debug.WriteLine("Collections reload error: " + ex.Message);
            }
        }
Exemplo n.º 4
0
    public async Task ReloadCollectionsAsync()
    {
        try
        {
            var sw = new Stopwatch();
            sw.Start();

            var collections = await _collectionService.GetAllCollectionsAsync();

            sw.Stop();
            Debug.WriteLine($"Collections reloaded in {sw.ElapsedMilliseconds}ms.");

            Collections.Clear();

            foreach (var collection in collections)
            {
                Collections.Add(new CollectionVM(collection.Id, collection.Name));
            }

            foreach (var collectionVm in Collections)
            {
                await collectionVm.LoadFolders();
            }

            SelectedCollection = Collections.FirstOrDefault();
        }
        catch (Exception ex)
        {
            App.MainWindowVM?.SetStatusError("Can't reload collections", ex.Message);
            Debug.WriteLine("Collections reload error: " + ex.Message);
        }
    }
 ///--------------------------------------------------------------------------------
 /// <summary>This method disposes of resources in the view model.</summary>
 ///--------------------------------------------------------------------------------
 protected override void OnDispose()
 {
     if (Collections != null)
     {
         foreach (CollectionViewModel itemView in Collections)
         {
             itemView.Updated -= Children_Updated;
             itemView.Dispose();
         }
         Collections.Clear();
         Collections = null;
     }
     Entity = null;
     base.OnDispose();
 }
Exemplo n.º 6
0
        /*
         * add collection
         * remove collection
         * edit collection name
         * merge x collections
         * intersect x collections
         * inverse map sum of x collections
         * difference x collections
         * clear collections
         * reorder collections
         * add beatmaps to collection
         * remove beatmaps from collection
         */
        private void EditCollection(CollectionEditArgs args, bool suspendRefresh = false)
        {
            var action = args.Action;

            if ((int)action >= 100)
            {
                return;
            }

            if (action == CollectionEdit.Add)
            {
                List <string> collectionNames = new List <string>();
                foreach (var collection in args.Collections)
                {
                    var name = GetValidCollectionName(collection.Name, collectionNames);

                    collection.Name = name;
                    collectionNames.Add(name);
                }
                LoadedCollections.AddRange(args.Collections);
            }
            else if (action == CollectionEdit.AddOrMergeIfExists)
            {
                foreach (var collection in args.Collections)
                {
                    if (CollectionNameExists(collection.Name))
                    {
                        EditCollection(CollectionEditArgs.MergeCollections(
                                           new Collections()
                        {
                            GetCollectionByName(collection.Name), collection
                        }, collection.Name), true);
                    }
                    else
                    {
                        EditCollection(CollectionEditArgs.AddCollections(new Collections()
                        {
                            collection
                        }), true);
                    }
                }
            }
            else if (action == CollectionEdit.Remove)
            {
                foreach (var collectionName in args.CollectionNames)
                {
                    var collection = GetCollectionByName(collectionName);
                    if (collection != null)
                    {
                        LoadedCollections.SilentRemove(collection);
                    }
                }
            }
            else if (action == CollectionEdit.Merge)
            {
                var collections       = args.Collections;
                var newCollectionName = args.NewName;
                if (collections.Count > 0)
                {
                    var masterCollection = collections[0];
                    for (int i = 1; i < collections.Count; i++)
                    {
                        var collectionToMerge = collections[i];
                        foreach (var beatmap in collectionToMerge.AllBeatmaps())
                        {
                            masterCollection.AddBeatmap(beatmap);
                        }
                        LoadedCollections.SilentRemove(collectionToMerge);
                    }
                    LoadedCollections.SilentRemove(masterCollection);

                    masterCollection.Name = GetValidCollectionName(newCollectionName);
                    EditCollection(CollectionEditArgs.AddCollections(new Collections()
                    {
                        masterCollection
                    }), true);
                }
            }
            else if (action == CollectionEdit.Intersect)
            {
                var targetCollection = args.Collections.Last();
                args.Collections.RemoveAt(args.Collections.Count - 1);
                var mainCollection = args.Collections[0];
                args.Collections.RemoveAt(0);
                var beatmaps = mainCollection.AllBeatmaps();
                foreach (var collection in args.Collections)
                {
                    beatmaps = beatmaps.Intersect(collection.AllBeatmaps(), new CollectionBeatmapComparer()).ToList();
                }

                foreach (var beatmap in beatmaps)
                {
                    targetCollection.AddBeatmap(beatmap);
                }

                EditCollection(CollectionEditArgs.AddCollections(new Collections()
                {
                    targetCollection
                }), true);
            }
            else if (action == CollectionEdit.Inverse)
            {
                var targetCollection = args.Collections.Last();
                args.Collections.RemoveAt(args.Collections.Count - 1);
                var beatmaps = LoadedBeatmaps.AsEnumerable().Cast <BeatmapExtension>();
                foreach (var collection in args.Collections)
                {
                    beatmaps = beatmaps.Except(collection.AllBeatmaps(), new CollectionBeatmapComparer());
                }

                foreach (var beatmap in beatmaps)
                {
                    targetCollection.AddBeatmap(beatmap);
                }

                EditCollection(CollectionEditArgs.AddCollections(new Collections()
                {
                    targetCollection
                }), true);
            }
            else if (action == CollectionEdit.Difference)
            {
                var targetCollection = args.Collections.Last();
                args.Collections.RemoveAt(args.Collections.Count - 1);
                var mainCollection = args.Collections[0];
                args.Collections.RemoveAt(0);
                var beatmaps = mainCollection.AllBeatmaps();
                foreach (var collection in args.Collections)
                {
                    beatmaps = beatmaps.Concat(collection.AllBeatmaps());
                }

                var differenceMd5   = beatmaps.GroupBy(x => x.Md5).Where(group => group.Count() == 1).Select(group => group.Key).ToList();
                var differenceMapId = beatmaps.GroupBy(x => x.MapId).Where(group => group.Count() == 1).Select(group => group.Key).ToList();

                foreach (var beatmap in beatmaps)
                {
                    if (differenceMd5.Contains(beatmap.Md5) || differenceMapId.Contains(beatmap.MapId))
                    {
                        targetCollection.AddBeatmap(beatmap);
                    }
                }

                EditCollection(CollectionEditArgs.AddCollections(new Collections()
                {
                    targetCollection
                }), true);
            }
            else if (action == CollectionEdit.Clear)
            {
                LoadedCollections.Clear();
            }
            else if (action == CollectionEdit.Reorder)
            {
                var targetCollection         = args.TargetCollection;
                var collectionsToReorder     = args.Collections.OrderBy(x => x.Name).ToList();
                var orderedLoadedCollections = LoadedCollections.OrderBy(x => x.Name).ToList();
                foreach (var coll in collectionsToReorder)
                {
                    orderedLoadedCollections.Remove(coll);
                }

                var targetCollectionIndex = orderedLoadedCollections.IndexOf(targetCollection);
                orderedLoadedCollections.InsertRange(args.PlaceCollectionsBefore ? targetCollectionIndex : targetCollectionIndex + 1, collectionsToReorder);
                var amountOfCharactersRequired = 0;
                var variations = 0;
                while (orderedLoadedCollections.Count() > variations)
                {
                    variations = Enumerable.Range(1, ++amountOfCharactersRequired).Aggregate(0, (acc, i) => Convert.ToInt32(Math.Pow(ReorderChars.Length, i)) + acc);
                }

                List <string> reorderStrings = new List <string>(variations);
                for (int i = 1; i <= amountOfCharactersRequired; i++)
                {
                    reorderStrings.AddRange(CombinationsWithRepetition(ReorderCharsString, i));
                }

                reorderStrings.Sort();
                var collectionIndex = 0;
                foreach (var collection in orderedLoadedCollections)
                {
                    if (collection.Name.Contains(reorderSeparator))
                    {
                        collection.Name = collection.Name.TrimStart(ReorderCharsWithSeparator);
                    }

                    collection.Name = $"{reorderStrings[collectionIndex++]}{reorderSeparator} {collection.Name}";
                }
            }
            else
            {
                var collection = GetCollectionByName(args.OrginalName);

                if (action == CollectionEdit.Rename)
                {
                    collection.Name = GetValidCollectionName(args.NewName);
                }
                else if (action == CollectionEdit.AddBeatmaps)
                {
                    if (collection != null)
                    {
                        foreach (var beatmap in args.Beatmaps)
                        {
                            collection.AddBeatmap(beatmap);
                        }
                    }
                }
                else if (action == CollectionEdit.RemoveBeatmaps)
                {
                    if (collection != null)
                    {
                        foreach (var beatmap in args.Beatmaps)
                        {
                            collection.RemoveBeatmap(beatmap.Md5);
                        }
                    }
                }
                else if (action == CollectionEdit.Duplicate)
                {
                    throw new NotImplementedException("Call AddCollections followed with AddBeatmaps instead");
                }
            }
            if (!suspendRefresh)
            {
                AfterCollectionsEdit();
            }
        }
Exemplo n.º 7
0
 public virtual void Dispose()
 {
     Page = PageSize = TotalPage = 0;
     Collections.Clear();
 }
Exemplo n.º 8
0
 public void RestoreCollections(List <CollectionData> collections)
 {
     Collections.Clear();
     Collections.AddRange(collections);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Disposes the database.
 /// </summary>
 public void Dispose()
 {
     Collections.Clear();
     Database.Dispose();
 }
Exemplo n.º 10
0
 /// <summary>
 /// Delete entire database content (delete all collections)
 /// </summary>
 public void Clear()
 {
     Collections.Clear();
 }
Exemplo n.º 11
0
        /*
         * add collection
         * remove collection
         * edit collection name
         * merge x collections
         * clear collections
         * add beatmaps to collection
         * remove beatmaps from collection
         */

        private void EditCollection(CollectionEditArgs args, bool suspendRefresh = false)
        {
            var action = args.Action;

            if (action == CollectionEdit.Add)
            {
                foreach (var collection in args.Collections)
                {
                    collection.Name = GetValidCollectionName(collection.Name);
                }
                LoadedCollections.AddRange(args.Collections);
            }
            else if (action == CollectionEdit.AddOrMergeIfExists)
            {
                foreach (var collection in args.Collections)
                {
                    if (CollectionNameExists(collection.Name))
                    {
                        EditCollection(CollectionEditArgs.MergeCollections(
                                           new Collections()
                        {
                            GetCollectionByName(collection.Name), collection
                        }, collection.Name), true);
                    }
                    else
                    {
                        EditCollection(CollectionEditArgs.AddCollections(new Collections()
                        {
                            collection
                        }), true);
                    }
                }
            }
            else if (action == CollectionEdit.Remove)
            {
                foreach (var collectionName in args.CollectionNames)
                {
                    var collection = GetCollectionByName(collectionName);
                    if (collection != null)
                    {
                        LoadedCollections.SilentRemove(collection);
                    }
                }
            }
            else if (action == CollectionEdit.Merge)
            {
                var collections       = args.Collections;
                var newCollectionName = args.NewName;
                if (collections.Count > 0)
                {
                    var masterCollection = collections[0];
                    for (int i = 1; i < collections.Count; i++)
                    {
                        var collectionToMerge = collections[i];
                        foreach (var beatmap in collectionToMerge.AllBeatmaps())
                        {
                            masterCollection.AddBeatmap(beatmap);
                        }
                        LoadedCollections.SilentRemove(collectionToMerge);
                    }
                    LoadedCollections.SilentRemove(masterCollection);

                    masterCollection.Name = GetValidCollectionName(newCollectionName);
                    EditCollection(CollectionEditArgs.AddCollections(new Collections()
                    {
                        masterCollection
                    }), true);
                }
            }
            else if (action == CollectionEdit.Clear)
            {
                LoadedCollections.Clear();
            }
            else
            {
                var collection = GetCollectionByName(args.OrginalName);

                if (action == CollectionEdit.Rename)
                {
                    collection.Name = GetValidCollectionName(args.NewName);
                }
                else if (action == CollectionEdit.AddBeatmaps)
                {
                    if (collection != null)
                    {
                        foreach (var beatmap in args.Beatmaps)
                        {
                            collection.AddBeatmap(beatmap);
                        }
                    }
                }
                else if (action == CollectionEdit.RemoveBeatmaps)
                {
                    if (collection != null)
                    {
                        foreach (var beatmap in args.Beatmaps)
                        {
                            collection.RemoveBeatmap(beatmap.Md5);
                        }
                    }
                }
            }
            if (!suspendRefresh)
            {
                AfterCollectionsEdit();
            }
        }
        /*
         * add collection
         * remove collection
         * edit collection name
         * merge x collections
         * intersect x collections
         * clear collections
         * add beatmaps to collection
         * remove beatmaps from collection
         */

        private void EditCollection(CollectionEditArgs args, bool suspendRefresh = false)
        {
            var action = args.Action;

            if (action == CollectionEdit.Add)
            {
                List <string> collectionNames = new List <string>();
                foreach (var collection in args.Collections)
                {
                    var name = GetValidCollectionName(collection.Name, collectionNames);

                    collection.Name = name;
                    collectionNames.Add(name);
                }
                LoadedCollections.AddRange(args.Collections);
            }
            else if (action == CollectionEdit.AddOrMergeIfExists)
            {
                foreach (var collection in args.Collections)
                {
                    if (CollectionNameExists(collection.Name))
                    {
                        EditCollection(CollectionEditArgs.MergeCollections(
                                           new Collections()
                        {
                            GetCollectionByName(collection.Name), collection
                        }, collection.Name), true);
                    }
                    else
                    {
                        EditCollection(CollectionEditArgs.AddCollections(new Collections()
                        {
                            collection
                        }), true);
                    }
                }
            }
            else if (action == CollectionEdit.Remove)
            {
                foreach (var collectionName in args.CollectionNames)
                {
                    var collection = GetCollectionByName(collectionName);
                    if (collection != null)
                    {
                        LoadedCollections.SilentRemove(collection);
                    }
                }
            }
            else if (action == CollectionEdit.Merge)
            {
                var collections       = args.Collections;
                var newCollectionName = args.NewName;
                if (collections.Count > 0)
                {
                    var masterCollection = collections[0];
                    for (int i = 1; i < collections.Count; i++)
                    {
                        var collectionToMerge = collections[i];
                        foreach (var beatmap in collectionToMerge.AllBeatmaps())
                        {
                            masterCollection.AddBeatmap(beatmap);
                        }
                        LoadedCollections.SilentRemove(collectionToMerge);
                    }
                    LoadedCollections.SilentRemove(masterCollection);

                    masterCollection.Name = GetValidCollectionName(newCollectionName);
                    EditCollection(CollectionEditArgs.AddCollections(new Collections()
                    {
                        masterCollection
                    }), true);
                }
            }
            else if (action == CollectionEdit.Intersect)
            {
                var targetCollection = args.Collections.Last();
                args.Collections.RemoveAt(args.Collections.Count - 1);
                var mainCollection = args.Collections[0];
                args.Collections.RemoveAt(0);
                var beatmaps = mainCollection.AllBeatmaps().ToList();
                foreach (var collection in args.Collections)
                {
                    beatmaps = beatmaps.Intersect(collection.AllBeatmaps(), new CollectionBeatmapComparer()).ToList();
                }

                foreach (var beatmap in beatmaps)
                {
                    targetCollection.AddBeatmap(beatmap);
                }

                EditCollection(CollectionEditArgs.AddCollections(new Collections()
                {
                    targetCollection
                }), true);
            }
            else if (action == CollectionEdit.Clear)
            {
                LoadedCollections.Clear();
            }
            else
            {
                var collection = GetCollectionByName(args.OrginalName);

                if (action == CollectionEdit.Rename)
                {
                    collection.Name = GetValidCollectionName(args.NewName);
                }
                else if (action == CollectionEdit.AddBeatmaps)
                {
                    if (collection != null)
                    {
                        foreach (var beatmap in args.Beatmaps)
                        {
                            collection.AddBeatmap(beatmap);
                        }
                    }
                }
                else if (action == CollectionEdit.RemoveBeatmaps)
                {
                    if (collection != null)
                    {
                        foreach (var beatmap in args.Beatmaps)
                        {
                            collection.RemoveBeatmap(beatmap.Md5);
                        }
                    }
                }
                else if (action == CollectionEdit.Duplicate)
                {
                    throw new NotImplementedException("Call AddCollections followed with AddBeatmaps instead");
                }
            }
            if (!suspendRefresh)
            {
                AfterCollectionsEdit();
            }
        }