/// <summary>
        /// Scans the list of directories and adds the new titles to the db
        /// </summary>
        /// <param name="directories"></param>
        public void Scan(IList<OMLSettings.WatchedFolder> directories)
        {
            // only one thread should be doing a scan at a time
            // this shouldln't happen but I'm putting a lock here for safety
            lock (staticLockObject)
            {
                //try
                {
                    int addedCount = 0;
                    bool updated = false;

                    StSanaServices StSana = new StSanaServices();
                    StSana.Log += new StSanaServices.SSEventHandler(stsana_Log);

                    DateTime start = DateTime.Now;

                    foreach (OMLSettings.WatchedFolder watchedfolder in directories)
                    {
                        DebugLine("Performing a media scan on - " + watchedfolder.Folder);
                        List<Title> titles = StSana.CreateTitlesFromPathArray(watchedfolder.ParentID,
                            new string[] { watchedfolder.Folder },
                            OMLSettings.ScannerSettingsTagTitlesWith);

                        addedCount += titles.Count();

                        if (titles.Count() > 0) updated = true;

                        foreach (Title title in titles)
                        {
                            if (Added != null)
                                Added(this, title);

                            if ((title.TitleType & TitleTypes.AllMedia) != 0)
                            {
                                try
                                {
                                    DebugLine("Looking up metadata for " + title.Name);
                                    LookupPreferredMetaData(title);
                                    TitleCollectionManager.SaveTitleUpdates();
                                }
                                catch (Exception ex)
                                {
                                    DebugLineError(ex, "Metadata lookup exception ");
                                }
                            }
                        }
                        DebugLine("Media scan on - " + watchedfolder.Folder + " finished");
                    }

                    // save all the image updates
                    if (updated)
                    {
                        TitleCollectionManager.SaveTitleUpdates();
                        DebugLine("Folder scanning completed.  Took {0} seconds and added {1} title(s).", (DateTime.Now - start).TotalSeconds, addedCount);
                        OMLSettings.ScannerSettingsNewTitles = OMLSettings.ScannerSettingsNewTitles + addedCount;
                    }
                    else
                        DebugLine("Folder scan resulted in no updates. Took {0} seconds", (DateTime.Now - start).TotalSeconds);
                }
                //catch (Exception err)
                {
                    //DebugLineError(err, "Error scanning folders");
                }
                //finally
                {
                    // null out the meta data plugin
                    //metaDataPlugin = null;
                }
            }
        }
        /*private void CreateTitlesFromPathArray(int? parentid, string[] path)
        {
            StSanaEvents eventsForm = new StSanaEvents();
            eventsForm.Show();
            eventsForm.Activate();

            // TODO - Need to check for images in folder
            // TODO - Wrap this up in another thread
            foreach (string file in path)
            {
                try
                {
                    if (Directory.Exists(file))
                    {
                        // Folder passed in. This is where St Sana kicks in
                        Servant stsana = new Servant();
                        stsana.Log += new Servant.SSEventHandler(stsana_Log);
                        stsana.BasePaths.Add(file);
                        stsana.Scan();

                        int? a_parent;

                        if (OMLEngine.Settings.OMLSettings.StSanaCreateTLFolder)
                        {
                            a_parent = CreateFolderNonDuplicate(parentid, Path.GetFileName(file), TitleTypes.Collection, null, false);
                        }
                        else
                        {
                            a_parent = parentid;
                        }

                        if (stsana.Entities != null)
                        {
                            foreach (Entity e in stsana.Entities)
                            {
                                int? e_parent = a_parent;
                                if (e.Name != file)
                                {
                                    switch (e.EntityType)
                                    {
                                        case Serf.EntityType.COLLECTION:
                                        case Serf.EntityType.MOVIE:
                                            if ((e.Series.Count() > 1) || (OMLEngine.Settings.OMLSettings.StSanaAlwaysCreateMovieFolder))
                                            {
                                                e_parent = CreateFolderNonDuplicate(a_parent, e.Name, TitleTypes.Collection, null, false);
                                            }
                                            else
                                            {
                                                e_parent = a_parent;
                                            }
                                            break;
                                        case Serf.EntityType.TV_SHOW:
                                            e_parent = CreateFolderNonDuplicate(a_parent, e.Name, TitleTypes.TVShow, null, false);
                                            break;
                                    }
                                }

                                foreach (Series s in e.Series)
                                {
                                    int? s_parent = e_parent;
                                    // if the s.name and e.name are the same, its a movie, to be sure though lets check s.number, it should be
                                    // -1 for non tv shows.
                                    if (s.Name.ToUpperInvariant().CompareTo(e.Name.ToUpperInvariant()) != 0 || s.Number > -1)
                                    {
                                        //s_parent = CreateFolderNonDuplicate(e_parent, s.Name, TitleTypes.Collection, false);
                                        if (s.Name != e.Name)
                                        {
                                            switch (e.EntityType)
                                            {
                                                case Serf.EntityType.COLLECTION:
                                                case Serf.EntityType.MOVIE:
                                                    if ((e_parent == a_parent) || (OMLEngine.Settings.OMLSettings.StSanaAlwaysCreateMovieFolder))
                                                    {
                                                        s_parent = CreateFolderNonDuplicate(e_parent, s.Name, TitleTypes.Collection, null, false);
                                                    }
                                                    else
                                                    {
                                                        s_parent = e_parent;
                                                    }
                                                    break;
                                                case Serf.EntityType.TV_SHOW:
                                                    s_parent = CreateFolderNonDuplicate(e_parent, s.Name, TitleTypes.Season, (short)s.Number, false);
                                                    break;
                                            }
                                        }
                                        else
                                        {
                                            s_parent = e_parent;
                                        }
                                    }

                                    foreach (Video v in s.Videos)
                                    {
                                        stsana_Log("Processing " + v.Name);
                                        //int v_parent = CreateFolder(s_parent, Path.GetFileNameWithoutExtension(v.Name), TitleTypes.Collection, false);

                                        List<Disk> disks = new List<Disk>();

                                        if ((e.EntityType == Serf.EntityType.COLLECTION) ||
                                            (e.EntityType == Serf.EntityType.MOVIE))
                                        {
                                            // Collection or movie mode. Create one title per folder with multiple disks
                                            foreach (string f in v.Files)
                                            {
                                                if (!TitleCollectionManager.ContainsDisks(OMLEngine.FileSystem.NetworkScanner.FixPath(f)))
                                                {
                                                    Disk disk = new Disk();
                                                    disk.Path = f;
                                                    disk.Format = disk.GetFormatFromPath(f); // (VideoFormat)Enum.Parse(typeof(VideoFormat), fileExtension.ToUpperInvariant());
                                                    disk.Name = string.Format("Disk {0}", 0);
                                                    if (disk.Format != VideoFormat.UNKNOWN)
                                                    {
                                                        disks.Add(disk);
                                                    }
                                                }
                                            }
                                            if (disks.Count != 0)
                                            {
                                                int newTitleID = CreateTitle(s_parent,
                                                    Path.GetFileNameWithoutExtension(v.Name),
                                                    TitleTypes.Unknown,
                                                    disks.ToArray(),
                                                    false);

                                                // Reload title from db, lookup metadata and find images
                                                Title newTitle = TitleCollectionManager.GetTitle(newTitleID);
                                                LookupPreferredMetaData(newTitle);
                                                CheckDiskPathForImages(newTitle, disks[0]);
                                                TitleCollectionManager.SaveTitleUpdates();
                                            }
                                        }
                                        else
                                        {
                                            // TV mode. Create one title per file, each with single disks
                                            foreach (string f in v.Files)
                                            {
                                                if (!TitleCollectionManager.ContainsDisks(OMLEngine.FileSystem.NetworkScanner.FixPath(f)))
                                                {
                                                    Disk disk = new Disk();
                                                    disk.Path = f;
                                                    disk.Format = disk.GetFormatFromPath(f); //(VideoFormat)Enum.Parse(typeof(VideoFormat), fileExtension.ToUpperInvariant());
                                                    disk.Name = string.Format("Disk {0}", 0);
                                                    if (disk.Format != VideoFormat.UNKNOWN)
                                                    {
                                                        disks.Add(disk);
                                                    }
                                                }
                                                if (disks.Count != 0)
                                                {
                                                    short episodeno = 0;
                                                    if (v.EpisodeNumbers.Count > 0) episodeno = (short)v.EpisodeNumbers[0];

                                                    int newTitleID = CreateTitle(s_parent,
                                                        Path.GetFileNameWithoutExtension(f),
                                                        TitleTypes.Episode,
                                                        (short)s.Number,
                                                        episodeno,
                                                        disks.ToArray(),
                                                        false);

                                                    // Reload title from db, lookup metadata and find images
                                                    Title newTitle = TitleCollectionManager.GetTitle(newTitleID);
                                                    LookupPreferredMetaData(newTitle);
                                                    CheckDiskPathForImages(newTitle, disks[0]);
                                                    TitleCollectionManager.SaveTitleUpdates();

                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (File.Exists(file))
                    {
                        stsana_Log("Processing " + file);
                        string extension = Path.GetExtension(file).ToUpper().Replace(".", "");
                        extension = extension.Replace("-", "");

                        Disk disk = new Disk();
                        disk.Path = file;
                        disk.Format = disk.GetFormatFromPath(file); // (VideoFormat)Enum.Parse(typeof(VideoFormat), extension.ToUpperInvariant());
                        disk.Name = string.Format("Disk {0}", 0);

                        if (disk.Format != VideoFormat.UNKNOWN)
                        {
                            int newTitleID = CreateTitle(parentid,
                                Path.GetFileNameWithoutExtension(file),
                                TitleTypes.Unknown,
                                new Disk[1] { disk },
                                false);

                            // Reload title from db, lookup metadata and find images
                            Title newTitle = TitleCollectionManager.GetTitle(newTitleID);
                            LookupPreferredMetaData(newTitle);
                            CheckDiskPathForImages(newTitle, disk);
                            TitleCollectionManager.SaveTitleUpdates();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Utilities.DebugLine("[OMLDatabaseEditor] CreateTitlesFromPathArray exception" + ex.Message);
                }
            }

            eventsForm.Hide();
            eventsForm.Dispose();
            eventsForm = null;
            PopulateMovieListV2(SelectedTreeRoot);
            PopulateMediaTree();
        }*/
        private void CreateTitlesFromPathArray(int? parentid, string[] path)
        {
            StSanaEvents eventsForm = new StSanaEvents();
            eventsForm.Show();
            eventsForm.Activate();

            // Create the St Sana class
            StSanaServices StSana = new StSanaServices();

            StSana.Log += new StSanaServices.SSEventHandler(stsana_Log);

            List<Title> titles= StSana.CreateTitlesFromPathArray(parentid, path, null);

            foreach (Title t in titles)
            {
                try
                {
                    AddCreatedTitle(t, false);

                    if ((t.TitleType & TitleTypes.AllMedia) != 0)
                    {
                        stsana_Log("Looking up metadata for " + t.Name);
                        LookupPreferredMetaData(t);
                    }
                    TitleCollectionManager.SaveTitleUpdates();
                }
                catch (Exception ex)
                {
                    Utilities.DebugLine("[OMLDatabaseEditor] CreateTitlesFromPathArray exception  on title : " + t.Name + " : " + ex.Message);
                }
            }

            eventsForm.Hide();
            eventsForm.Dispose();
            eventsForm = null;
            PopulateMovieListV2(SelectedTreeRoot);
            PopulateMediaTree();
        }