/// <summary>
 /// Initializes a new instance of the FilesQueue class
 /// </summary>
 /// <param name="import">The import using this FilesQueue</param>
 public FilesQueue(TheImport import)
 {
     _import = import;
     _files = new Queue<EnhancedFileInfo>();
     _waitHandle = new AutoResetEvent(false);
     _worker = new Thread(() => SingleFileImportWorker.WorkerPerformSingleFileAndFileInfoImport(this)) { Name = "File Queue Thread" };
     _worker.Start();
 }
        internal static void DeleteNonExistentItems(TheImport theImport)
        {
            theImport.RelocatedFiles = new List<string>();
            theImport.DeletedFiles = 0;

            int oldItemCount
                = theImport.Importer
                .Section.ItemCount;

            for (int itemPos = oldItemCount - 1; itemPos >= 0; itemPos--)
            {

                IMLItem item
                    = theImport.Importer
                    .Section[itemPos];

                bool itemChanged = false;

                var locationTag
                    = new MLTag
                        ("Location",
                        item.Location);

                int percentage = (itemPos * 10) / oldItemCount;

                string logMessage1 =
                    "      Checking item: ID="
                    + item.ID
                    + " Location="
                    + locationTag.Value;

                if (!theImport.CheckProgress
                    (percentage, logMessage1))
                {
                    return;
                }

                theImport.Importer.LogMessages
                    .Enqueue(new LogMessage
                        ("Info", logMessage1));

                for (int filePos = locationTag.Values.Count - 1; filePos >= 0; filePos--)
                {

                    if (!theImport.CanDelete(locationTag.Values[filePos]))
                        continue;

                    if (!theImport.Relocate(locationTag, filePos))
                    {
                        string logMessage2 = "Removing " + locationTag.Values[filePos] + " from item's location";
                        theImport.Importer.LogMessages.Enqueue(new LogMessage("Info", logMessage2));
                        locationTag.Values.RemoveAt(filePos);
                    }

                    itemChanged = true;
                }

                if (locationTag.Values.Count == 0)
                {
                    theImport.Importer.Section.DeleteItem(item);
                    theImport.Importer.LogMessages.Enqueue(new LogMessage("Info", "Item deleted"));
                    theImport.DeletedFiles++;
                }
                else
                {
                    if (itemChanged)
                    {
                        item.Location = locationTag.Value;
                        item.ExternalID = locationTag.Value.ToLower();
                        item.SaveTags();
                    }

                }

            }
        }
 public ItemsChainedByTagCache(TheImport import)
 {
     this.import = import;
     this.cache = new Dictionary<string, IMLItem>();
 }
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or
        /// resetting unmanaged resources.
        /// </summary>
        /// <param name="calledFromCode">false if called by GC true if called from code</param>
        protected virtual void Dispose(bool calledFromCode)
        {
            if (!calledFromCode)
                return;

            Enqueue(null as EnhancedFileInfo); // Signal the consumer to exit.
            _worker.Join(); // Wait for the consumer's thread to finish.
            _waitHandle.Close(); // Release any OS resources.
            _files = null;
            _import = null;
            _worker = null;
            _waitHandle = null;

            // Free other state (managed objects).

            // Free your own state (unmanaged objects).
            // Set large fields to null.
        }
        internal static void ScanExistingItems(TheImport theImport)
        {
            IMLItemList oldItems
                = theImport.Importer
                .Section.GetReadOnlyItems();

            int oldItemCount = oldItems.Count;

            const string logMessage1
                = "Prescanning existing items:";

            if (!theImport
                .CheckProgress
                (0, logMessage1))
            {
                return;
            }

            bool createItemsChainedByTagCache
                = (theImport.Importer
                .ChainingOption
                == ChainOption.ChainByTags)
                &&   (theImport.Importer
                .TagsToChainBy.Length > 0);

            for (int itemPos = 0; itemPos < oldItemCount; itemPos++)
            {
                IMLItem item = oldItems[itemPos];
                var locationTag = new MLTag("Location", item.Location);

                foreach (string location in locationTag.Values)
                {

                    if (theImport.Location2Id.Keys.Contains
                        (location.ToLowerInvariant()))
                        continue;

                    string logMessage2 = "Mapped the location " + location  + " to the itemid " + item.ID;
                    int percentage = (itemPos * 10) / oldItemCount;
                    if (!theImport.CheckProgress(percentage, logMessage2))
                    {
                        return;
                    }

                    theImport.Location2Id.Add(location.ToLowerInvariant(), item.ID);
                }

                if (!createItemsChainedByTagCache)
                    continue;

                IMLItem writableItem
                    = theImport.Importer.Section
                        .FindItemByID(item.ID);

                IMLItem cachedItem
                    = theImport.ItemsChainedByTagCache
                        .Query(writableItem);

                if (cachedItem == writableItem)
                    continue;

                string logMessage4
                    = "You are chaining by tags " +
                      "but the item (ID: " + item.ID
                      + " Location: "
                      + item.Location
                      + ") has the same values" +
                      " for the tags you're chaining" +
                      " by as the item (ID: "
                      +  cachedItem.ID
                      + " Location: "
                      + cachedItem.Location + ").";

                theImport.Importer.LogMessages.Enqueue
                    (new LogMessage
                         ("Warn", logMessage4));

            }
        }