Beispiel #1
0
        // initialize all values of the given object. essentially makes the object a new refernece
        // and a new row will be created if commited
        public void Clear()
        {
            id                 = null;
            commitNeeded       = true;
            RetrievalInProcess = true;

            ReadOnlyCollection <DBField> fieldList = DBField.GetFieldList(this.GetType());

            foreach (DBField currField in fieldList)
            {
                object defaultVal = currField.Default;
                currField.SetValue(this, defaultVal);

                // if this is a dynamic (internally changable) object, setup a listener
                if (defaultVal != null && defaultVal.GetType() == typeof(IDynamic))
                {
                    ((IDynamic)defaultVal).Changed += new ChangedEventHandler(commitNeededEventHandler);
                }
            }

            ReadOnlyCollection <DBRelation> relationList = DBRelation.GetRelations(this.GetType());

            foreach (DBRelation currRelation in relationList)
            {
                try {
                    currRelation.GetRelationList(this).Changed += new ChangedEventHandler(commitNeededEventHandler);
                }
                catch (NullReferenceException) {
                    throw new System.InvalidOperationException("RelationLists must be initialized in the get{} property method.");
                }
            }

            RetrievalInProcess = false;
        }
Beispiel #2
0
        // Loads data into this object based on the given database record.
        public void LoadByRow(SQLiteResultSet.Row row)
        {
            RetrievalInProcess = true;
            ReadOnlyCollection <DBField> fieldList = DBField.GetFieldList(this.GetType());

            // load each field one at a time. they should have been retrieved in the
            // ordering in FieldList
            int i;

            for (i = 0; i < fieldList.Count; i++)
            {
                if (row.fields[i] == "")
                {
                    fieldList[i].SetValue(this, null);
                }
                else
                {
                    fieldList[i].SetValue(this, row.fields[i]);
                }
            }

            // id is always at the end, assign that too
            id = int.Parse(row.fields[i]);

            // all values are in sync with DB so no commit needed.
            commitNeeded       = false;
            RetrievalInProcess = false;
        }
Beispiel #3
0
        /// <summary>
        /// Returns true if all auto-updatable fields for this object are populated.
        /// </summary>
        public bool IsFullyPopulated()
        {
            ReadOnlyCollection <DBField> fieldList = DBField.GetFieldList(this.GetType());

            foreach (DBField currField in fieldList)
            {
                object oldValue = currField.GetValue(this);
                if (currField.Filterable && currField.AutoUpdate)
                {
                    // check if the old value is the default value
                    if (oldValue is string && currField.Default is string)
                    {
                        if (((string)oldValue ?? string.Empty).Trim().Equals(((string)currField.Default).Trim()))
                        {
                            return(false);
                        }
                    }
                    else if (oldValue == null || oldValue.Equals(currField.Default))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Beispiel #4
0
        private void generateFieldList()
        {
            // add fields from primary type
            fieldComboBox.Items.Clear();
            foreach (DBField currField in DBField.GetFieldList(Table))
            {
                if (currField.Filterable && currField.AllowDynamicFiltering)
                {
                    ComboFieldWrapper wrapper = new ComboFieldWrapper(currField);
                    wrapperLookup[currField] = wrapper;
                    fieldComboBox.Items.Add(wrapper);
                }
            }

            // add fields from secondary types
            foreach (DBRelation currRelation in DBRelation.GetRelations(Table))
            {
                if (!currRelation.Filterable)
                {
                    continue;
                }

                foreach (DBField currField in DBField.GetFieldList(currRelation.SecondaryType))
                {
                    if (currField.Filterable && currField.AllowDynamicFiltering)
                    {
                        ComboFieldWrapper wrapper = new ComboFieldWrapper(currField, currRelation);
                        wrapperLookup[currField] = wrapper;
                        fieldComboBox.Items.Add(wrapper);
                    }
                }
            }

            fieldComboBox.SelectedIndex = 0;
        }
Beispiel #5
0
        private List <string> getTypeList(IDBBackedControl control)
        {
            List <string> fieldNameList = new List <string>();

            foreach (DBField currField in DBField.GetFieldList(control.Table))
            {
                fieldNameList.Add(currField.Name);
            }

            return(fieldNameList);
        }
Beispiel #6
0
        // Updates the current object with all fields in the newData object
        public void Copy(DatabaseTable newData)
        {
            if (newData == null)
            {
                return;
            }
            ReadOnlyCollection <DBField> fieldList = DBField.GetFieldList(newData.GetType());

            foreach (DBField currField in fieldList)
            {
                currField.SetValue(this, currField.GetValue(newData));
            }
        }
Beispiel #7
0
        private void populateFieldCombo()
        {
            fieldChanging = true;

            // add fields from primary type
            fieldComboBox.Items.Clear();
            foreach (DBField currField in DBField.GetFieldList(typeof(T)))
            {
                if (currField.Filterable)
                {
                    ComboFieldWrapper wrapper = new ComboFieldWrapper(currField);
                    wrapperLookup[currField] = wrapper;
                    fieldComboBox.Items.Add(wrapper);
                }
            }

            // add fields from secondary types
            foreach (DBRelation currRelation in DBRelation.GetRelations(typeof(T)))
            {
                if (!currRelation.Filterable)
                {
                    continue;
                }

                foreach (DBField currField in DBField.GetFieldList(currRelation.SecondaryType))
                {
                    if (currField.Filterable)
                    {
                        ComboFieldWrapper wrapper = new ComboFieldWrapper(currField, currRelation);
                        wrapperLookup[currField] = wrapper;
                        fieldComboBox.Items.Add(wrapper);
                    }
                }
            }

            if (Criteria != null && Criteria.Field != null && wrapperLookup.ContainsKey(Criteria.Field))
            {
                fieldComboBox.SelectedItem = wrapperLookup[Criteria.Field];
            }
            else
            {
                fieldComboBox.SelectedIndex = 0;
            }

            fieldChanging = false;
        }
Beispiel #8
0
        // create the dictionary to map variables to the correct values for this movie
        private Dictionary <string, string> getVariableMapping(DBMovieInfo movie)
        {
            // load replacement strings
            Dictionary <string, string> replacementStrings = new Dictionary <string, string>();

            foreach (DBField currField in DBField.GetFieldList(typeof(DBMovieInfo)))
            {
                if (currField.AutoUpdate && currField.GetValue(movie) != null)
                {
                    replacementStrings["movie." + currField.FieldName] = currField.GetValue(movie).ToString().Trim();
                }
            }

            // add the replacement string for multipart movies
            if (MovingPicturesCore.Settings.FileMultipartString != null)
            {
                replacementStrings["moviepart"] = MovingPicturesCore.Settings.FileMultipartString;
            }

            return(replacementStrings);
        }
Beispiel #9
0
        // create the dictionary to map variables to the correct values for this movie
        public static Dictionary <string, string> getVariableMapping(DatabaseTable obj)
        {
            // add fields from primary object
            Dictionary <string, string> replacementStrings = new Dictionary <string, string>();

            foreach (DBField currField in DBField.GetFieldList(obj.GetType()))
            {
                if (currField.Filterable && currField.GetValue(obj) != null)
                {
                    replacementStrings[currField.FieldName] = currField.GetValue(obj).ToString().Trim();
                }
            }

            // add fields from secondary types
            foreach (DBRelation currRelation in DBRelation.GetRelations(obj.GetType()))
            {
                if (!currRelation.Filterable)
                {
                    continue;
                }

                if (currRelation.GetRelationList(obj).Count > 0)
                {
                    DatabaseTable subObj = (DatabaseTable)currRelation.GetRelationList(obj)[0];

                    foreach (DBField currField in DBField.GetFieldList(currRelation.SecondaryType))
                    {
                        if (currField.Filterable && currField.GetValue(subObj) != null)
                        {
                            replacementStrings[currField.FieldName] = currField.GetValue(subObj).ToString().Trim();
                        }
                    }
                }
            }

            return(replacementStrings);
        }
Beispiel #10
0
        // Updates the current object with all fields in the newData object that are
        // not set to default.
        public void CopyUpdatableValues(DatabaseTable newData)
        {
            if (newData == null)
            {
                return;
            }
            ReadOnlyCollection <DBField> fieldList = DBField.GetFieldList(newData.GetType());

            foreach (DBField currField in fieldList)
            {
                object newValue = currField.GetValue(newData);
                object oldValue = currField.GetValue(this);
                if (currField.AutoUpdate)
                {
                    if (newValue == null)
                    {
                        currField.SetValue(this, newValue);
                        continue;
                    }

                    // if the updated value is just the default, don't update.
                    // something is better than nothing
                    if (newValue.Equals(currField.Default))
                    {
                        continue;
                    }

                    // if we have a string try to compare trimmed strings
                    if (newValue.GetType() == typeof(string) && ((string)newValue ?? string.Empty).Trim() == ((string)oldValue ?? string.Empty).Trim())
                    {
                        continue;
                    }

                    // if the value just hasn't changed, dont update
                    if (newValue.Equals(oldValue))
                    {
                        continue;
                    }

                    // check if the old value is the default value
                    bool oldValueIsDefault = false;
                    if (oldValue is string && currField.Default is string)
                    {
                        if (((string)oldValue ?? string.Empty).Trim().Equals(((string)currField.Default).Trim()))
                        {
                            oldValueIsDefault = true;
                        }
                    }
                    else if (oldValue == null || oldValue.Equals(currField.Default))
                    {
                        oldValueIsDefault = true;
                    }

                    // if we are protecting non-default values continue
                    if (protectExistingValuesFromCopy && !oldValueIsDefault)
                    {
                        continue;
                    }

                    currField.SetValue(this, newValue);
                }
            }
            // reset the value protection to true again
            protectExistingValuesFromCopy = true;
        }
Beispiel #11
0
        public bool GetBackdrop(DBMovieInfo movie)
        {
            if (scraper == null)
            {
                return(false);
            }

            Dictionary <string, string> paramList = new Dictionary <string, string>();
            Dictionary <string, string> results;

            // if we already have a backdrop move on for now
            if (movie.BackdropFullPath.Trim().Length > 0)
            {
                return(true);
            }

            // try to load the id for the movie for this script
            DBSourceMovieInfo idObj = movie.GetSourceMovieInfo(ScriptID);

            if (idObj != null && idObj.Identifier != null)
            {
                paramList["movie.site_id"] = idObj.Identifier;
            }

            // load params for scraper
            foreach (DBField currField in DBField.GetFieldList(typeof(DBMovieInfo)))
            {
                if (currField.GetValue(movie) != null)
                {
                    paramList["movie." + currField.FieldName] = currField.GetValue(movie).ToString().Trim();
                }
            }

            //set higher level settings for script to use
            paramList["settings.defaultuseragent"] = MovingPicturesCore.Settings.UserAgent;
            paramList["settings.mepo_data"]        = Config.GetFolder(Config.Dir.Config);

            // run the scraper
            results = scraper.Execute("get_backdrop", paramList);
            if (results == null)
            {
                logger.Error(Name + " scraper script failed to execute \"get_backdrop\" node.");
                return(false);
            }


            // Loop through all the results until a valid backdrop is found
            int count = 0;

            while (results.ContainsKey("backdrop[" + count + "].url") || results.ContainsKey("backdrop[" + count + "].file"))
            {
                // attempt to load via a URL
                if (results.ContainsKey("backdrop[" + count + "].url"))
                {
                    string backdropURL = results["backdrop[" + count + "].url"];
                    if (backdropURL.Trim().Length > 0)
                    {
                        if (movie.AddBackdropFromURL(backdropURL) == ImageLoadResults.SUCCESS)
                        {
                            return(true);
                        }
                    }
                }

                // attempt to load via a file
                if (results.ContainsKey("backdrop[" + count + "].file"))
                {
                    string backdropFile = results["backdrop[" + count + "].file"];
                    if (backdropFile.Trim().Length > 0)
                    {
                        if (movie.AddBackdropFromFile(backdropFile))
                        {
                            return(true);
                        }
                    }
                }

                count++;
            }

            // no valid backdrop found
            return(false);
        }
Beispiel #12
0
        public bool GetArtwork(DBMovieInfo movie)
        {
            if (scraper == null)
            {
                return(false);
            }

            Dictionary <string, string> paramList = new Dictionary <string, string>();
            Dictionary <string, string> results;

            // grab coverart loading settings
            int maxCovers          = MovingPicturesCore.Settings.MaxCoversPerMovie;
            int maxCoversInSession = MovingPicturesCore.Settings.MaxCoversPerSession;

            // if we have already hit our limit for the number of covers to load, quit
            if (movie.AlternateCovers.Count >= maxCovers)
            {
                return(true);
            }

            // try to load the id for the movie for this script
            DBSourceMovieInfo idObj = movie.GetSourceMovieInfo(ScriptID);

            if (idObj != null && idObj.Identifier != null)
            {
                paramList["movie.site_id"] = idObj.Identifier;
            }

            // load params for scraper
            foreach (DBField currField in DBField.GetFieldList(typeof(DBMovieInfo)))
            {
                if (currField.GetValue(movie) != null)
                {
                    paramList["movie." + currField.FieldName] = currField.GetValue(movie).ToString().Trim();
                }
            }

            //set higher level settings for script to use
            paramList["settings.defaultuseragent"] = MovingPicturesCore.Settings.UserAgent;
            paramList["settings.mepo_data"]        = Config.GetFolder(Config.Dir.Config);

            // run the scraper
            results = scraper.Execute("get_cover_art", paramList);
            if (results == null)
            {
                logger.Error(Name + " scraper script failed to execute \"get_cover_art\" node.");
                return(false);
            }

            int coversAdded = 0;
            int count       = 0;

            while (results.ContainsKey("cover_art[" + count + "].url") || results.ContainsKey("cover_art[" + count + "].file"))
            {
                // if we have hit our limit quit
                if (movie.AlternateCovers.Count >= maxCovers || coversAdded >= maxCoversInSession)
                {
                    return(true);
                }

                // get url for cover and load it via the movie object
                if (results.ContainsKey("cover_art[" + count + "].url"))
                {
                    string coverPath = results["cover_art[" + count + "].url"];
                    if (coverPath.Trim() != string.Empty)
                    {
                        if (movie.AddCoverFromURL(coverPath) == ImageLoadResults.SUCCESS)
                        {
                            coversAdded++;
                        }
                    }
                }

                // get file for cover and load it via the movie object
                if (results.ContainsKey("cover_art[" + count + "].file"))
                {
                    string coverPath = results["cover_art[" + count + "].file"];
                    if (coverPath.Trim() != string.Empty)
                    {
                        if (movie.AddCoverFromFile(coverPath))
                        {
                            coversAdded++;
                        }
                    }
                }

                count++;
            }

            if (coversAdded > 0)
            {
                return(true);
            }

            return(false);
        }
Beispiel #13
0
        public UpdateResults Update(DBMovieInfo movie)
        {
            if (scraper == null)
            {
                return(UpdateResults.FAILED);
            }

            Dictionary <string, string> paramList = new Dictionary <string, string>();
            Dictionary <string, string> results;
            bool hasSiteId = false;

            // try to load the id for the movie for this script
            // if we have no site id still continue as we might still
            // be able to grab details using another identifier such as imdb_id
            DBSourceMovieInfo idObj = movie.GetSourceMovieInfo(ScriptID);

            if (idObj != null && idObj.Identifier != null)
            {
                paramList["movie.site_id"] = idObj.Identifier;
                hasSiteId = true;
            }

            // load params
            foreach (DBField currField in DBField.GetFieldList(typeof(DBMovieInfo)))
            {
                if (currField.AutoUpdate && currField.GetValue(movie) != null)
                {
                    paramList["movie." + currField.FieldName] = currField.GetValue(movie).ToString().Trim();
                }
            }

            //set higher level settings for script to use
            paramList["settings.defaultuseragent"] = MovingPicturesCore.Settings.UserAgent;
            paramList["settings.mepo_data"]        = Config.GetFolder(Config.Dir.Config);

            // try to retrieve results
            results = scraper.Execute("get_details", paramList);
            if (results == null)
            {
                logger.Error(Name + " scraper script failed to execute \"get_details\" node.");
                return(UpdateResults.FAILED);
            }

            if (!hasSiteId)
            {
                // if we started out without a site id
                // try to get it from the details response
                string siteId;
                if (results.TryGetValue("movie.site_id", out siteId))
                {
                    movie.GetSourceMovieInfo(ScriptID).Identifier = siteId;
                }
                else
                {
                    // still no site id, so we are returning
                    return(UpdateResults.FAILED_NEED_ID);
                }
            }

            // get our new movie details
            DBMovieInfo newMovie = new DBMovieInfo();

            foreach (DBField currField in DBField.GetFieldList(typeof(DBMovieInfo)))
            {
                string value;
                bool   success = results.TryGetValue("movie." + currField.FieldName, out value);

                if (success && value.Trim().Length > 0)
                {
                    currField.SetValue(newMovie, value.Trim());
                }
            }

            // and update as necessary
            movie.CopyUpdatableValues(newMovie);

            return(UpdateResults.SUCCESS);
        }
Beispiel #14
0
        public List <DBMovieInfo> Get(MovieSignature movieSignature)
        {
            if (scraper == null)
            {
                return(null);
            }

            List <DBMovieInfo>          rtn       = new List <DBMovieInfo>();
            Dictionary <string, string> paramList = new Dictionary <string, string>();
            Dictionary <string, string> results;

            if (movieSignature.Title != null)
            {
                paramList["search.title"] = movieSignature.Title;
            }
            if (movieSignature.Keywords != null)
            {
                paramList["search.keywords"] = movieSignature.Keywords;
            }
            if (movieSignature.Year != null)
            {
                paramList["search.year"] = movieSignature.Year.ToString();
            }
            if (movieSignature.ImdbId != null)
            {
                paramList["search.imdb_id"] = movieSignature.ImdbId;
            }
            if (movieSignature.DiscId != null)
            {
                paramList["search.disc_id"] = movieSignature.DiscId;
            }
            if (movieSignature.MovieHash != null)
            {
                paramList["search.moviehash"] = movieSignature.MovieHash;
            }
            if (movieSignature.Path != null)
            {
                paramList["search.basepath"] = movieSignature.Path;
            }
            if (movieSignature.Folder != null)
            {
                paramList["search.foldername"] = movieSignature.Folder;
            }
            if (movieSignature.File != null)
            {
                paramList["search.filename"] = movieSignature.File;
            }

            //set higher level settings for script to use
            paramList["settings.defaultuseragent"] = MovingPicturesCore.Settings.UserAgent;
            paramList["settings.mepo_data"]        = Config.GetFolder(Config.Dir.Config);

            // this variable is the filename without extension (and a second one without stackmarkers)
            if (!String.IsNullOrEmpty(movieSignature.File))
            {
                paramList["search.filename_noext"] = Path.GetFileNameWithoutExtension(movieSignature.File);
                paramList["search.clean_filename"] = Utility.GetFileNameWithoutExtensionAndStackMarkers(movieSignature.File);
            }

            results = scraper.Execute("search", paramList);
            if (results == null)
            {
                logger.Error(Name + " scraper script failed to execute \"search\" node.");
                return(rtn);
            }

            int count = 0;

            // The movie result is only valid if the script supplies a unique site
            while (results.ContainsKey("movie[" + count + "].site_id"))
            {
                string siteId;
                string prefix = "movie[" + count + "].";
                count++;

                // if the result does not yield a site id it's not valid so skip it
                if (!results.TryGetValue(prefix + "site_id", out siteId))
                {
                    continue;
                }

                // if this movie was already added skip it
                if (rtn.Exists(delegate(DBMovieInfo item) { return(item.GetSourceMovieInfo(ScriptID).Identifier == siteId); }))
                {
                    continue;
                }

                // if this movie does not have a valid title, don't bother
                if (!results.ContainsKey(prefix + "title"))
                {
                    continue;
                }

                // We passed all checks so create a new movie object
                DBMovieInfo newMovie = new DBMovieInfo();

                // store the site id in the new movie object
                newMovie.GetSourceMovieInfo(ScriptID).Identifier = siteId;

                // Try to store all other fields in the new movie object
                foreach (DBField currField in DBField.GetFieldList(typeof(DBMovieInfo)))
                {
                    string value;
                    if (results.TryGetValue(prefix + currField.FieldName, out value))
                    {
                        currField.SetValue(newMovie, value.Trim());
                    }
                }

                // add the movie to our movie results list
                rtn.Add(newMovie);
            }

            return(rtn);
        }