Beispiel #1
0
        private void bRefresh_Click(object sender, EventArgs e)
        {
            _loadErrors.Clear();
            var   selected = tlwAlbums.SelectedObject as AlbumDto;
            Album start    = null;

            if (selected != null)
            {
                start = _ctx.Albums.Where(x => x.Id == selected.Id).FirstOrDefault();
            }
            var txt = "Refresh music" +
                      (start == null || start.Path == "" ? "" :
                       " from " + (start.Path + "\\" + start.Name).Substring(6)) + "?";
            var confirmResult = MessageBox.Show(txt, "Confirm", MessageBoxButtons.YesNo);

            if (confirmResult == DialogResult.Yes)
            {
                _stop            = false;
                bRefresh.Enabled = false;
                bStop.Enabled    = true;

                DirectoryInfo d;
                if (start == null)
                {
                    // если все ветки
                    start = _ctx.Albums.Include("Songs").Include("DVDs")
                            .FirstOrDefault(x => x.Path == "" && x.Name == "Music");
                    if (start == null)
                    {
                        start = new Album()
                        {
                            Path = "",
                            Name = "Music",
                        };
                        _ctx.Albums.Add(start);
                        _ctx.SaveChanges();
                    }
                    d = new DirectoryInfo(txtMusicPath.Text);
                }
                else
                {
                    // если выбрана ветка
                    _ctx.Entry(start).Collection("Songs").Load();

                    var dir = GetPathToAlbum(start);
                    d = new DirectoryInfo(dir);
                }

                var dat = LoadDirectory(d,
                                        (string.IsNullOrEmpty(start.Path) ? "" : start.Path + "\\") + start.Name, start);

                if (selected != null)
                {
                    selected.ChildIds.Clear();
                    selected.Assign(dat);
                    LoadChildrenTree(selected);
                    foreach (var chId in selected.ChildIds)
                    {
                        var chAlb = _treeList.Where(x => x.Id == chId).First();
                        LoadChildrenTree(chAlb);
                    }
                    tlwAlbums.RefreshObject(selected);

                    // изменения наверх до корня
                    while (selected.ParentId.HasValue)
                    {
                        selected = _treeList.Where(x => x.Id == selected.ParentId.Value).First();
                        RecalcDurations(selected);
                        tlwAlbums.RefreshObject(selected);
                    }
                }

                FillDvds();

                var msg = "OK!";
                foreach (var err in _loadErrors)
                {
                    msg += "\n" + err;
                }
                MessageBox.Show(msg);
            }
            bRefresh.Enabled = true;
            bStop.Enabled    = false;
        }
Beispiel #2
0
        private FolderDataDto LoadDirectory(DirectoryInfo d, string path, Album parentAlbum)
        {
            var res = new FolderDataDto();

            if (_stop)
            {
                return(res);
            }

            label1.Text = path;
            Application.DoEvents();

            // помечаем для удаления все песни (пока)
            var songs      = parentAlbum.Songs.ToList();
            var songsToDel = songs.Select(x => x.Id).ToList();
            var dvds       = parentAlbum.DVDs.ToList();
            var dvdsToDel  = dvds.Select(x => x.Id).ToList();

            try
            {
                var files = d.GetFiles();
                foreach (var file in files)
                {
                    if (file.Name == "magdata")
                    {
                        var magFile = File.ReadAllLines(file.FullName);
                        var magdata = new List <string>(magFile);

                        foreach (var line in magdata)
                        {
                            if (line == "hidden")
                            {
                                parentAlbum.IsHidden = true;
                            }
                            else if (!string.IsNullOrEmpty(line))
                            {
                                var dvd = dvds.FirstOrDefault(x => x.Name == line);
                                if (dvd == null)
                                {
                                    dvd = new DVD()
                                    {
                                        AlbumId = parentAlbum.Id,
                                        Name    = line
                                    };
                                    _ctx.DVDs.Add(dvd);
                                }
                                else
                                {
                                    dvdsToDel.Remove(dvd.Id);
                                }
                                res.Dvds.Add(line);
                            }
                        }
                    }
                    else
                    {
                        var sng = songs.FirstOrDefault(x => x.FileName == file.Name);
                        if (sng == null)
                        {
                            sng = new Song()
                            {
                                AlbumId  = parentAlbum.Id,
                                FileName = file.Name
                            };
                            _ctx.Songs.Add(sng);
                            parentAlbum.Songs.Add(sng);
                        }
                        else
                        {
                            songsToDel.Remove(sng.Id);
                        }

                        if (sng.Size != file.Length || sng.Created.ToString("dd.MM.yyyy hh:mm") != file.CreationTime.ToString("dd.MM.yyyy hh:mm"))
                        {
                            sng.Size    = file.Length;
                            sng.Created = file.CreationTime;

                            if (file.Extension.ToLower() == ".mp3")
                            {
                                try
                                {
                                    var mp3 = new Track(file.FullName);
                                    sng.Name     = Util.toUtf8(mp3.Title);
                                    sng.Bitrate  = mp3.Bitrate;
                                    sng.Duration = mp3.Duration;
                                    sng.IsVbr    = mp3.IsVBR;
                                }
                                catch (Exception e) {
                                    //MessageBox.Show(e.Message);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e) {
                MessageBox.Show(e.Message);
            }

            // удаляем не найденные песни
            if (songsToDel.Any())
            {
                var songToDelete = songs.Where(x => songsToDel.Contains(x.Id)).ToList();
                _ctx.Songs.RemoveRange(songToDelete);
            }
            if (dvdsToDel.Any())
            {
                var dvdToDelete = dvds.Where(x => dvdsToDel.Contains(x.Id)).ToList();
                _ctx.DVDs.RemoveRange(dvdToDelete);
            }
            _ctx.SaveChanges();


            // обрабатываем вложенные папки
            var albsToDel = _ctx.Albums.Where(x => x.ParentId == parentAlbum.Id).Select(x => x.Id).ToList();

            try
            {
                var innerDvds = new List <string>();
                var folders   = d.GetDirectories();
                foreach (var folder in folders)
                {
                    var dir = _ctx.Albums.Include("Songs").Include("DVDs")
                              .FirstOrDefault(x => x.Path == path && x.Name == folder.Name);
                    if (dir == null)
                    {
                        dir = new Album()
                        {
                            Path     = path,
                            Name     = folder.Name,
                            ParentId = parentAlbum.Id
                        };
                        _ctx.Albums.Add(dir);
                    }
                    else
                    {
                        dir.ParentId = parentAlbum.Id;
                        albsToDel.Remove(dir.Id);
                    }
                    _ctx.SaveChanges();

                    var dat = LoadDirectory(folder,
                                            (string.IsNullOrEmpty(path) ? "" : path + "\\") + folder.Name, dir);
                    res.Size     = (res.Size ?? 0) + (dat.Size ?? 0);
                    res.Duration = (res.Duration ?? 0) + (dat.Duration ?? 0);
                    foreach (var key in dat.Bitrates.Keys)
                    {
                        if (res.Bitrates.ContainsKey(key))
                        {
                            res.Bitrates[key] += dat.Bitrates[key];
                        }
                        else
                        {
                            res.Bitrates[key] = dat.Bitrates[key];
                        }
                    }
                    foreach (var dvd in dat.Dvds)
                    {
                        if (!innerDvds.Contains(dvd))
                        {
                            innerDvds.Add(dvd);
                        }
                    }
                }

                if (innerDvds.Any())
                {
                    foreach (var dvd in res.Dvds)
                    {
                        if (!innerDvds.Contains(dvd) && !dvd.StartsWith("_"))
                        {
                            _loadErrors.Add($"- {dvd}: {parentAlbum.Path}\\{parentAlbum.Name}");
                        }
                    }
                    foreach (var dvd in innerDvds)
                    {
                        if (!res.Dvds.Contains(dvd))
                        {
                            _loadErrors.Add($"+ {dvd}: {parentAlbum.Path}\\{parentAlbum.Name}");
                        }
                    }
                }
            }
            catch { }


            if (!_stop)
            {
                var albumsToDel = _ctx.Albums.Where(x => albsToDel.Contains(x.Id)).ToList();
                foreach (var alb in albumsToDel)
                {
                    // ищем для удаления только во вложенных папках
                    var albumsQuery = _ctx.Albums.Where(x => x.Path.StartsWith(alb.Path + "\\" + alb.Name));

                    var toDeleteSongs = _ctx.Songs
                                        .Where(y => albumsQuery.Select(x => x.Id).ToList().Contains(y.AlbumId)
                                               ).ToList();
                    _ctx.Songs.RemoveRange(toDeleteSongs);

                    var toDeleteDvds = _ctx.DVDs
                                       .Where(y => albumsQuery.Select(x => x.Id).ToList().Contains(y.AlbumId)
                                              ).ToList();
                    _ctx.DVDs.RemoveRange(toDeleteDvds);

                    var toDelete = albumsQuery.ToList();
                    _ctx.Albums.RemoveRange(toDelete);

                    if (alb.Songs.Any())
                    {
                        _ctx.Songs.RemoveRange(alb.Songs);
                    }
                    if (alb.DVDs.Any())
                    {
                        _ctx.DVDs.RemoveRange(alb.DVDs);
                    }
                    _ctx.Albums.Remove(alb);

                    _ctx.SaveChanges();
                }
            }
            _ctx.SaveChanges();

            RecalcDurations(parentAlbum, res);

            label1.Text = "";
            Application.DoEvents();

            return(res);
        }