private void taskListTimerCallback(object state)
 {
     try {
         FollwitBackgroundProcess bgProc = new FollwitBackgroundProcess();
         bgProc.Action = FitActions.ProcessTaskList;
         MovingPicturesCore.ProcessManager.StartProcess(bgProc);
     }
     catch (Exception ex) {
         logger.ErrorException("", ex);
         _follwitAPI = null;
         MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
         return;
     }
 }
        private void movieDeletedListener(DatabaseTable obj)
        {
            if (!MovingPicturesCore.Settings.FollwitEnabled) {
                logger.Warn("Attempt to call follw.it made when service is disabled.");
                return;
            }

            try {
                // if this is not a movie object, break
                if (obj.GetType() != typeof(DBMovieInfo))
                    return;

                if (!IsOnline) {
                    logger.Warn("Can not remove movie from follw.it collection because service is offline");
                    return;
                }

                DBMovieInfo movie = (DBMovieInfo)obj;

                List<DBMovieInfo> allMovies = DBMovieInfo.GetAll();

                int fitIdMovieCount = (from m in allMovies
                                       where m.FitId == movie.FitId
                                       select m).Count();

                if (fitIdMovieCount == 0)
                {
                    FollwitBackgroundProcess bgProc = new FollwitBackgroundProcess();
                    bgProc.Action = FitActions.RemoveMovieFromCollection;
                    bgProc.Movies.Add(movie);
                    MovingPicturesCore.ProcessManager.StartProcess(bgProc);
                }

            }
            catch (Exception ex) {
                logger.ErrorException("Unexpected error removing an object from your follw.it collection!", ex);
                _follwitAPI = null;
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
            }
        }
        private void DatabaseManager_ObjectUpdated(DatabaseTable obj, TableUpdateInfo updateInfo)
        {
            if (!MovingPicturesCore.Settings.FollwitEnabled) {
                logger.Warn("Attempt to call follw.it made when service is disabled.");
                return;
            }

            try {
                // we're looking for user rating changes
                if (obj.GetType() != typeof(DBUserMovieSettings))
                    return;

                DBUserMovieSettings settings = (DBUserMovieSettings)obj;
                if (updateInfo.RatingChanged()) {
                    if (!IsOnline) {
                        logger.Warn("Can not send rating info to follw.it because service is offline");
                        return;
                    }

                    DBMovieInfo movie = settings.AttachedMovies[0];

                    if (currentlySyncingMovies.Contains(movie))
                        return;

                    FollwitBackgroundProcess bgProc = new FollwitBackgroundProcess();
                    bgProc.Action = FitActions.UpdateUserRating;
                    bgProc.Movies.Add(movie);
                    MovingPicturesCore.ProcessManager.StartProcess(bgProc);
                }

            }
            catch (Exception ex) {
                logger.ErrorException("Unexpected error sending rating information to follw.it!", ex);
                _follwitAPI = null;
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
            }
        }
        private void DatabaseManager_ObjectInserted(DatabaseTable obj)
        {
            if (!MovingPicturesCore.Settings.FollwitEnabled) {
                logger.Warn("Attempt to call follw.it made when service is disabled.");
                return;
            }

            try {
                if (obj.GetType() == typeof(DBWatchedHistory)) {
                    DBWatchedHistory wh = (DBWatchedHistory)obj;
                    DBMovieInfo movie = wh.Movie;

                    WatchMovie(movie, true);
                }
                else if (obj.GetType() == typeof(DBMovieInfo)) {
                    DBMovieInfo movie = (DBMovieInfo)obj;
                    FollwitBackgroundProcess bgProc = new FollwitBackgroundProcess();
                    bgProc.Action = FitActions.AddMoviesToCollection;
                    bgProc.Movies.Add(movie);
                    MovingPicturesCore.ProcessManager.StartProcess(bgProc);
                }
            }
            catch (Exception ex) {
                logger.ErrorException("Unexpected error connecting to follw.it!", ex);
                _follwitAPI = null;
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
            }
        }
        public bool WatchMovie(DBMovieInfo movie, bool includeInStream)
        {
            if (currentlySyncingMovies.Contains(movie))
                return true;

            if (!MovingPicturesCore.Settings.FollwitEnabled) {
                logger.Warn("Attempt to call follw.it made when service is disabled.");
                return false;
            }

            if (!IsOnline) {
                logger.Warn("Can not send movie watched count to follw.it because service is offline");
                return false;
            }

            try {
                FollwitBackgroundProcess bgProc = new FollwitBackgroundProcess();
                bgProc.Action = includeInStream ? FitActions.WatchMovie : FitActions.WatchMovieIgnoreStream;
                bgProc.Movies.Add(movie);
                MovingPicturesCore.ProcessManager.StartProcess(bgProc);
            }
            catch (Exception ex) {
                logger.ErrorException("Unexpected error sending 'movie watched' information to follw.it!", ex);
                _follwitAPI = null;
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
                return false;
            }

            return true;
        }
        public bool CurrentlyWatching(DBMovieInfo movie, bool isWatching)
        {
            if (!MovingPicturesCore.Settings.FollwitEnabled) {
                logger.Warn("Attempt to call follw.it made when service is disabled.");
                return false;
            }

            if (!IsOnline) {
                logger.Warn("Can not send movie watched count to follw.it because service is offline");
                return false;
            }

            try {
                if (MovingPicturesCore.Settings.RestrictSynchronizedMovies) {
                    var filtered = MovingPicturesCore.Settings.FollwitSyncFilter.Filter(new List<DBMovieInfo>() {movie});
                    if (filtered.Count == 0)
                        return false;
                }

                FollwitBackgroundProcess bgProc = new FollwitBackgroundProcess();
                bgProc.Action = isWatching ? FitActions.BeginWatching : FitActions.EndWatching;
                bgProc.Movies.Add(movie);
                MovingPicturesCore.ProcessManager.StartProcess(bgProc);
            }
            catch (Exception ex) {
                logger.ErrorException("Unexpected error sending 'now watching' information to follw.it!", ex);
                _follwitAPI = null;
                MovingPicturesCore.Follwit.Status = FollwitConnector.StatusEnum.INTERNAL_ERROR;
                return false;
            }

            return true;
        }