示例#1
0
        private void ProjectWatcherServiceFileChangedEvent(object sender, SledProjectWatcherServiceEventArgs e)
        {
            var curProject = m_projectService.ActiveProject;

            if (curProject == null)
            {
                return;
            }

            if (m_modifiedProjectForm == null)
            {
                m_modifiedProjectForm = new SledProjectModifiedForm();
                m_modifiedProjectForm.ChangesSubmitted += ModifiedProjectFormChangesSubmitted;
            }

            //
            // Figure out what changed between the in-memory
            // project (m_projectService.ActiveProject) and project
            // on disk at e.AbsolutePath
            //

            string        name;
            string        projectDir;
            string        assetDir;
            Guid          guid;
            List <string> lstFiles;

            // Read details of changed-on-disk project
            var bGotProjectDetails =
                SledProjectUtilities.TryGetProjectDetails(
                    e.AbsolutePath,
                    out name,
                    out projectDir,
                    out assetDir,
                    out guid,
                    out lstFiles);

            if (!bGotProjectDetails)
            {
                return;
            }

            // Gather up all changes to
            // then report them to the GUI
            var lstChanges =
                new List <SledModifiedProjectChange>();

            // Check if name changed
            if (string.Compare(curProject.Name, name) != 0)
            {
                lstChanges.Add(
                    new SledModifiedProjectNameChange(
                        curProject.Name, name));
            }

            // Check if asset directory changed
            if (string.Compare(curProject.AssetDirectory, assetDir, true) != 0)
            {
                lstChanges.Add(
                    new SledModifiedProjectAssetDirChange(
                        curProject.AssetDirectory,
                        assetDir));
            }

            // Check if guid changed
            if (curProject.Guid != guid)
            {
                lstChanges.Add(
                    new SledModifiedProjectGuidChange(
                        curProject.Guid,
                        guid));
            }

            // Check if files added or removed
            {
                var curFilesAbsPaths =
                    curProject.AllFiles.Select(
                        file => file.AbsolutePath).ToList();

                // Check for removals
                lstChanges.AddRange(
                    (from curAbsFile in curFilesAbsPaths
                     let bExists = lstFiles.Any(newAbsFile => string.Compare(curAbsFile, newAbsFile, true) == 0)
                                   where !bExists
                                   select new SledModifiedProjectFileRemovedChange(curAbsFile)).Cast <SledModifiedProjectChange>());

                // Check for additions
                lstChanges.AddRange(
                    (from newAbsFile in lstFiles
                     let bExists = curFilesAbsPaths.Any(curAbsFile => string.Compare(newAbsFile, curAbsFile, true) == 0)
                                   where !bExists
                                   select new SledModifiedProjectFileAddedChange(newAbsFile)).Cast <SledModifiedProjectChange>());
            }

            // Report changes to GUI
            m_modifiedProjectForm.ReportChanges(lstChanges);

            // Are there any changes?
            var formChanges =
                new List <SledModifiedProjectChange>(
                    m_modifiedProjectForm.Changes);

            // Do we need to show the GUI?
            var bShowingGui =
                formChanges.Count > 0;

            // Fire event
            ChangesDetected.Raise(this, new SledModifiedProjectChangesDetectedEventArgs(bShowingGui));

            if (bShowingGui)
            {
                // Show the form finally
                m_modifiedProjectForm.Show(m_mainForm);
            }
            else
            {
                // Do we need to hide the GUI?

                // If the form is already visible but there are
                // no changes then we want to hide the form as
                // all changes have been dealt with or reverted
                if (m_modifiedProjectForm.Visible)
                {
                    m_modifiedProjectForm.Hide();
                }
            }
        }
示例#2
0
        public (bool result, ChangesDetected changes) CheckForChanges(MusicDb db)
        {
            ChangesDetected changesDetected = ChangesDetected.None;
            bool            result          = false;
            var             st = new StepTimer();

            st.Start();
            var currentMusicFiles = GetMusicFilesFromDb(db);//.ToArray();

            st.Time();
            var filesOnDisk = GetFilesOnDisk();

            st.Time();
            bool anyFilesNotCatalogued()
            {
                bool r    = false;
                var  list = currentMusicFiles.Where(x => x.Track == null);

                r = list.Count() > 0;
                st.Time();
                if (r)
                {
                    changesDetected = ChangesDetected.AtLeastOneFileNotCatalogued;
                    //log.Trace($"anyFilesNotCatalogued() returns true");
                }
                return(r);
            }

            bool anyFilesRewritten()
            {
                bool r  = false;
                var  l1 = currentMusicFiles.Select(x => x.FileLastWriteTimeUtc);
                var  l2 = filesOnDisk.Select(x => new DateTimeOffset(x.fi.LastWriteTimeUtc, TimeSpan.Zero));

                r = !l1.SequenceEqual(l2);
                st.Time();
                if (r)
                {
                    changesDetected = ChangesDetected.AtLeastOneFileModifiedOnDisk;
                    log.Trace($"anyFilesRewritten() returns true");
                }
                return(r);
            }

            bool musicTagsAreNew()
            {
                bool r = false;

                if (HasMusicTags())
                {
                    var tagFile = Path.Combine(Folderpath, ITEOBase.TagFile);
                    var tagTime = new DateTimeOffset(new FileInfo(tagFile).LastWriteTimeUtc, TimeSpan.Zero);
                    r = currentMusicFiles.Any(mf => mf.LastCataloguedAt < tagTime);
                }
                st.Time();
                if (r)
                {
                    changesDetected = ChangesDetected.MusicTagsAreNewer;
                    //log.Trace($"customTagsAreNew() returns true");
                }
                return(r);
            }

            bool additionsOrDeletionsExist()
            {
                var differences = filesOnDisk.Select(f => f.fi.FullName).Except(currentMusicFiles.Select(mf => mf.File), StringComparer.CurrentCultureIgnoreCase);

                st.Time();
                var r = differences.Count() != 0;

                if (r)
                {
                    changesDetected = ChangesDetected.MusicFileCountHasChanged;
                    log.Debug($"music file difference count is {differences.Count()}");
                }
                return(r);
            }

            bool anyImageChanged()
            {
                bool r     = false;
                var  works = currentMusicFiles.Select(mf => mf.Track).Select(x => x.Work).Distinct();
                //currentMusicFiles.Select(mf => mf.Track).Where(x => x.Performance != null)
                //    .Select(x => x.Performance.Composition.Artist).Distinct();
                var artists = works.Select(x => x.Artist)
                              .Union(currentMusicFiles.Select(mf => mf.Track).Where(x => x.Performance != null)
                                     .Select(x => x.Performance.Composition.Artist))
                              .Distinct();

                foreach (var artist in artists.Where(a => a.Type != ArtistType.Various))
                {
                    var f = artist.GetPortraitFile(MusicOptions);
                    if (artist.Portrait.HasChanged(f))
                    {
                        log.Debug($"artist {artist.Name}, portrait file {f} found");
                        r = true;
                        break;
                    }
                }
                if (!r)
                {
                    foreach (var work in works)
                    {
                        //var coverFile = MusicStyle.GetMostRecentOpusCoverFile(MusicOptions, IsCollection ? "Collections" : work.Artist.Name, currentPathData.OpusPath);
                        //var coverFile = MusicStyle.GetMostRecentOpusCoverFile(MusicOptions, work, currentPathData.OpusPath);
                        var coverFile = work.GetMostRecentOpusCoverFile(MusicOptions);
                        if (work.Cover.HasChanged(coverFile))
                        {
                            log.Debug($"artist {work.Artist.Name}, work {work.Name}, cover art file {coverFile}");
                            r = true;
                            break;
                        }
                    }
                }
                if (r)
                {
                    changesDetected = ChangesDetected.CoverArtHasChanged;
                    //log.Trace($"anyImageChanged() returns true");
                }
                return(r);
            }

            if (additionsOrDeletionsExist() || musicTagsAreNew() || anyFilesRewritten() || anyFilesNotCatalogued() || anyImageChanged())
            {
                result = true;
            }
            return(result, changesDetected);
        }