private void ScanLocation(LocationBase location, Device device)
 {
     UpdateWaitStatus("Processing " + location.Name + " ...");
     try
     {
         if (location == null || device == null)
         {
             return;
         }
         m_rescanResults = RescanResults.Run(location.Id, device.Id);
         LVNew.AddObjects(m_rescanResults.NewFiles);
         LVMissing.AddObjects(m_rescanResults.MissingFiles);
         Application.DoEvents();
     }
     catch (Exception err)
     {
         MessageBox.Show(err.Message);
     }
 }
        private void BtnApply_Click(object sender, EventArgs e)
        {
            if (m_rescanResults == null)
            {
                return;
            }

            //Remove missing
            UpdateWaitStatus("Removing missing...");
            foreach (var mf in m_rescanResults.MissingFiles)
            {
                if (mf.ShouldDelete)
                {
                    mf.Delete();
                }
                else
                {
                    if (mf.NewLocationBaseId > 0 && !string.IsNullOrEmpty(mf.NewLocationData))
                    {
                        mf.SetNewLocation();
                    }
                }
            }
            LVMissing.ClearObjects();


            //Add new
            UpdateWaitStatus("Adding new...");
            foreach (var nf in m_rescanResults.NewFiles)
            {
                nf.Save();
            }
            LVNew.ClearObjects();

            m_rescanResults = null;

            UpdateWaitStatus(null);
        }
        public static RescanResults Run(long locationBaseId, long deviceId)
        {
            var res = new RescanResults();

            //DEVICE_KIND = 1 - PC
            const string SQL = @"SELECT dl.LOCATION_MAPPING
					FROM device_location_map dl
					JOIN device d
					  ON d.DEVICE_ID = dl.DEVICE_ID
					 AND d.DEVICE_KIND = 1
					WHERE dl.LOCATION_BASE_ID = @0
					  AND dl.DEVICE_ID = @1"                    ;

            string basePath;
            List <TitleLocation> databaseStorage;

            using (var db = DB.GetDatabase())
            {
                basePath        = db.Fetch <string>(SQL, locationBaseId, deviceId).FirstOrDefault();
                databaseStorage = LocationPersistence.GetLocationForBase(locationBaseId, db);
            }

            //Don't process images and unknown files
            var fileStorage = SearchFileStorage.Generate(basePath, locationBaseId).Where((f) => { return(f.DataType != MediaType.Picture && f.DataType != MediaType.Unknown && f.DataType != MediaType.PictureFolder); }).ToList();

            /*if (fileStorage.Count == 0)
             * {
             *      res.ErrorCode = "NO_MEDIA_FILES_FOUND";
             *      return res;
             * } */

            fileStorage.Sort();
            databaseStorage.Sort();             //Sorting here to have exactly the same order

            int basePathLen = basePath.Length;

            int idxFile = 0;
            int idxDb   = 0;

            while (idxFile < fileStorage.Count && idxDb < databaseStorage.Count)
            {
                //In real world likely there will be more files on disk than in DB, so there is no optimization for removed files
                string filePath = fileStorage[idxFile].RelativePath;
                //if (!filePath.StartsWith(basePath)) throw new ApplicationException(string.Format("Path {0} doesn't start with {1}", filePath, basePath));
                string relPath = filePath;                 //.Substring(basePath.Length);

                int cmp = relPath.CompareTo(databaseStorage[idxDb].LocationData);
                if (cmp == 0)
                {
                    //cool - match found
                    idxDb++;
                    idxFile++;
                }
                else if (cmp < 0)
                {
                    //fileStorage precedes -> not in the DB
                    res.NewFiles.Add(fileStorage[idxFile]);
                    idxFile++;
                }
                else
                {
                    //InFiles precedes -> not in the DB
                    res.MissingFiles.Add(databaseStorage[idxDb]);
                    idxDb++;
                }
            }

            //Leftovers after one of the lists is fully processed
            for (; idxFile < fileStorage.Count; idxFile++)
            {
                res.NewFiles.Add(fileStorage[idxFile]);
            }
            for (; idxDb < databaseStorage.Count; idxDb++)
            {
                res.MissingFiles.Add(databaseStorage[idxDb]);
            }

            res.RetrieveMissingTitleInfo();
            return(res);
        }