Exemplo n.º 1
0
        public void ManageDatabase()
        {
            using (var db = new SAForumDB())
            {
                if (!db.DatabaseExists())
                {
                    db.CreateDatabase();
                    this.SetDefaults(db);
                }

                // manage versions
                var schemaUpdater = db.CreateDatabaseSchemaUpdater();
                int version = schemaUpdater.DatabaseSchemaVersion;
                try
                {
                    switch (version)
                    {
                        case 1:
                            this.UpdateToVersionTwo(schemaUpdater);
                            break;
                    }
                }
                catch (Exception ex) { Awful.Core.Event.Logger.AddEntry("An error occurred while updating schema:", ex); }
            }
        }
Exemplo n.º 2
0
        static void Authenticator_LoginSuccessful(object sender, Awful.Core.Event.ProfileChangedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(state =>
                {
                    try
                    {
                        OnProfileChanged(sender, e);
                        using (var db = new SAForumDB())
                        {
                            var count = db.Smilies.Count();
                            if (count == 0)
                            {
                                var signal = new AutoResetEvent(false);
                                Services.AwfulSmileyService.FetchSmiliesFromWebAsync((result, list) =>
                                    {
                                        if (result == Awful.Core.Models.ActionResult.Success)
                                        {
                                            db.Smilies.InsertAllOnSubmit(list);
                                        }
                                        signal.Set();
                                    });

                                signal.WaitOne();
                                db.SubmitChanges(ConflictMode.FailOnFirstConflict);
                            }
                        }
                    }

                    catch (Exception ex)
                    {
                        string error = string.Format("An error occured while updating the smiley database: [{0}] {1}", ex.Message, ex.StackTrace);
                        Awful.Core.Event.Logger.AddEntry(error);
                    }

                }, null);
        }
Exemplo n.º 3
0
        protected override void OnNavigatedTo(
            System.Windows.Navigation.NavigationEventArgs e)
        {
            SystemTray.IsVisible = !App.Settings.HideSystemTray;

            base.OnNavigatedTo(e);

            ForumData forum = null;
            try
            {
                int id = Convert.ToInt32(NavigationContext.QueryString["ID"]);
                using (var db = new Data.SAForumDB())
                {
                    forum = db.Forums.Where(f => f.ID == id).Single();
                }
            }
            catch (Exception)
            {
                forum = App.Settings.LastViewedForum;
            }

            UpdateFavoritesMenu(forum);

            context.SetForum(forum);

            if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
                reload = false;
        }
Exemplo n.º 4
0
 public override void SaveSettings()
 {
     base.SaveSettings();
     using (var db = new Data.SAForumDB()) { db.SubmitChanges(); }
 }
Exemplo n.º 5
0
        public override void LoadSettings()
        {
            ApplicationUsageHelper.Init(AwfulSettings.VERSION_INFO);

            // check database for updates
            DBUpdater.ManageDatabase();

            if (this.SetToDefault)
            {
                LoadDefaults();
                this.SetToDefault = false;
            }

            // set application detection mode to disabled: phone will not kill Awful if running in the foreground.
            PhoneApplicationService.Current.ApplicationIdleDetectionMode = this.RunUnderLockScreen ?
                IdleDetectionMode.Disabled :
                IdleDetectionMode.Enabled;

            if (!Awful.Core.Web.AwfulWebRequest.CanAuthenticate)
            {
                using (var db = new Data.SAForumDB())
                {
                    var query = db.Tokens.Where(t => t.ProfileID == CurrentProfileID);
                    var token = query.FirstOrDefault();
                    if (token != null)
                    {
                        var profile = token.Profile;
                        var cookies = profile.GetTokens();
                        Awful.Core.Web.AwfulWebRequest.SetCookieJar(cookies);
                        App.CurrentUser = profile.Username;
                    }
                }
            }

            this.SetPostForeground(this.PostTextColorSliderValue);
            Globals.Resources.HasSeen = App.Layout.CurrentTheme.PostHasSeen;
            Globals.Resources.PostForeground = App.Layout.CurrentTheme.PostForeground;
            Globals.Resources.Foreground = App.Layout.CurrentTheme.Foreground;
            Globals.Resources.Background = App.Layout.CurrentTheme.PostBackground;
        }
Exemplo n.º 6
0
 private void SetDefaults(SAForumDB db)
 {
     db.Profiles.InsertOnSubmit(SAForumDB.DefaultProfile);
     db.Subforums.InsertAllOnSubmit(SAForumDB.DefaultSubforums);
     db.SubmitChanges(ConflictMode.FailOnFirstConflict);
 }
Exemplo n.º 7
0
        static void OnThreadUpdated(object sender, EventArgs e)
        {
            SAThread thread = sender as SAThread;
            if (thread == null) return;

            ThreadPool.QueueUserWorkItem(state =>
                {
                    try
                    {
                        using (var db = new SAForumDB())
                        {
                            var query = from t in db.Threads
                                        where t.ID == thread.ID
                                        select t;

                            var selected = query.SingleOrDefault();

                            if (selected == null)
                            {
                                db.Threads.InsertOnSubmit(thread);
                            }

                            db.SubmitChanges(ConflictMode.FailOnFirstConflict);
                        }
                    }
                    catch (Exception ex)
                    {
                        string error = string.Format("An error occured while trying to modify the Thread DB: [{0}] - {1}", ex.Message, ex.StackTrace);
                        Awful.Core.Event.Logger.AddEntry(error);
                    }
                },
                null);
        }
Exemplo n.º 8
0
        static void OnThreadPageGenerated(object sender, Helpers.HtmlGeneratedEventArgs e)
        {
            SAThreadPage page = sender as SAThreadPage;
            if (page == null) return;

            ThreadPool.QueueUserWorkItem(state =>
            {
                try
                {
                    using (var db = new SAForumDB())
                    {
                        db.SubmitChanges(ConflictMode.FailOnFirstConflict);
                    }
                }

                catch (Exception ex)
                {
                    string error = string.Format("An error occured while modifying the Thread Page DB: [{0}] - {1}",
                        ex.Message, ex.StackTrace);

                    Awful.Core.Event.Logger.AddEntry(error);
                }

            }, null);
        }
Exemplo n.º 9
0
        private static void OnProfileChanged(object sender, Awful.Core.Event.ProfileChangedEventArgs e)
        {
            try
            {
                using (var db = new SAForumDB())
                {
                    var profile = db.Profiles.Where(p => p.Username.Equals(e.Value.Username)).SingleOrDefault();
                    if (profile == null)
                    {
                        profile = new Profile() { Username = e.Value.Username, Password = e.Value.Password };
                        profile.AssignTokens(e.Cookies);
                        db.Profiles.InsertOnSubmit(profile);
                        db.SubmitChanges(ConflictMode.FailOnFirstConflict);
                        App.Settings.CurrentProfileID = profile.ID;
                    }
                    else
                    {
                        App.Settings.CurrentProfileID = profile.ID;
                        App.CurrentUser = profile.Username;
                        foreach (var cookie in e.Cookies)
                        {
                            var token = new SAAuthToken(cookie) { Profile = profile };
                            db.Tokens.InsertOnSubmit(token);
                        }

                        db.SubmitChanges(ConflictMode.FailOnFirstConflict);

                        var profile2 = db.Profiles.Where(p => p.ID == profile.ID).SingleOrDefault();
                    }
                }
            }

            catch (Exception ex)
            {
                string error = string.Format(
                    "There was an error while trying to save profile to DB. [{0}] {1}",
                    ex.Message,
                    ex.StackTrace);

                Awful.Core.Event.Logger.AddEntry(error);
            }
        }
Exemplo n.º 10
0
        static void OnThreadPageCreated(object sender, EventArgs e)
        {
            SAThreadPage page = sender as SAThreadPage;
            if (page == null) return;

            ThreadPool.QueueUserWorkItem(state =>
                {
                    try
                    {
                        using (var db = new SAForumDB())
                        {
                            var query = from p in db.ThreadPages
                                        where (p.ThreadID == page.ThreadID) && (p.PageNumber == page.PageNumber)
                                        select p;

                            var single = query.SingleOrDefault();

                            if (single == null)
                            {
                                db.ThreadPages.InsertOnSubmit(page);
                            }

                            db.SubmitChanges(ConflictMode.FailOnFirstConflict);
                        }
                    }

                    catch (Exception ex)
                    {
                        string error = string.Format("An error occurred while updating the ThreadPage DB: [{0}] {1}", ex.Message, ex.StackTrace);
                        Awful.Core.Event.Logger.AddEntry(error);
                    }

                }, null);
        }
Exemplo n.º 11
0
        static void OnForumPageUpdated(object sender, CollectionUpdatedEventArgs<Awful.Models.ThreadData> e)
        {
            SAForumPage page = sender as SAForumPage;
            if (page == null) return;

            ThreadPool.QueueUserWorkItem(state =>
                {
                    try
                    {
                        using (var db = new SAForumDB())
                        {
                            foreach (var item in e.Collection)
                            {
                                var thread = item as SAThread;
                                if (thread != null)
                                {
                                    var query = from t in db.Threads
                                                where t.ID == thread.ID
                                                select t;

                                    var single = query.SingleOrDefault();

                                    if (single == null) { db.Threads.InsertOnSubmit(thread); }
                                }
                            }

                            db.SubmitChanges(ConflictMode.FailOnFirstConflict);
                        }
                    }

                    catch (Exception ex)
                    {
                        string error = string.Format("An error occurred while updating the Thread DB: [{0}] {1}", ex.Message, ex.StackTrace);
                        Awful.Core.Event.Logger.AddEntry(error);
                    }

                }, null);
        }
Exemplo n.º 12
0
        private static void OnForumListRefreshed(object sender, Helpers.ForumsRefreshedEventArgs<SAForum> e)
        {
            ThreadPool.QueueUserWorkItem(state =>
                {
                    try
                    {
                        using (var db = new SAForumDB())
                        {
                            foreach (var item in e.Forums)
                            {
                                var query = from forum in db.Forums
                                            where forum.ID == item.ID
                                            select forum;

                                var selected = query.SingleOrDefault();
                                if (selected == null) { db.Forums.InsertOnSubmit(item); }
                                else
                                {
                                    selected.ForumName = item.ForumName;
                                }
                            }

                            db.SubmitChanges(ConflictMode.FailOnFirstConflict);
                        }
                    }

                    catch (Exception ex)
                    {
                        string error = string.Format("An error occured while trying to modify the Forums DB: [{0}] {1}", ex.Message, ex.StackTrace);
                        Awful.Core.Event.Logger.AddEntry(error);
                    }

                }, null);
        }
Exemplo n.º 13
0
        static void OnFavoriteRemoved(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(state =>
                {
                    try
                    {
                        SAForum forum = sender as SAForum;
                        using (var db = new SAForumDB())
                        {
                            Profile current = db.Profiles.SingleOrDefault(profile => profile.ID == App.Settings.CurrentProfileID);
                            if (current == null) throw new ArgumentException("Current profile can not be null.");

                            var query = from f in db.ForumFavorites
                                        where f.ProfileID == current.ID && f.ForumID == forum.ID
                                        select f;

                            ForumFavorite toRemove = query.SingleOrDefault();
                            if (toRemove != null)
                            {
                                toRemove.IsFavorite = false;
                                db.SubmitChanges(ConflictMode.FailOnFirstConflict);
                            }

                        }
                    }

                    catch (Exception ex)
                    {
                        string error = string.Format("An error occurred while updating the favorites DB: [{0}] {1}", ex.Message, ex.StackTrace);
                        Awful.Core.Event.Logger.AddEntry(error);
                    }

                }, null);
        }
Exemplo n.º 14
0
        static void OnFavoriteAdded(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(state =>
            {
                try
                {
                    SAForum forum = sender as SAForum;
                    using (var db = new SAForumDB())
                    {
                        Profile current = db.Profiles.SingleOrDefault(profile => profile.ID == App.Settings.CurrentProfileID);
                        if (current == null) throw new ArgumentException("Current profile can not be null.");

                        var query = from fav in db.ForumFavorites
                                    where fav.ProfileID == current.ID && fav.ForumID == forum.ID
                                    select fav;

                        if (query.Count() == 0)
                        {
                            ForumFavorite favorite = new ForumFavorite() { ForumID = forum.ID, ProfileID = current.ID, IsFavorite = true };
                            db.ForumFavorites.InsertOnSubmit(favorite);
                            db.SubmitChanges(ConflictMode.FailOnFirstConflict);
                        }
                    }
                }
                catch (Exception ex)
                {
                    string error = string.Format("An error occured while trying to modify the Favorites DB: [{0}] {1}", ex.Message, ex.StackTrace);
                    Awful.Core.Event.Logger.AddEntry(error);
                }

            }, null);
        }
Exemplo n.º 15
0
        public static IEnumerable<SAThreadPage> Pages(this SAThread thread)
        {
            IQueryable<SAThreadPage> query = null;
            IEnumerable<SAThreadPage> result = null;
            using (var db = new SAForumDB())
            {
                query = from page in db.ThreadPages
                        where page.ThreadID == thread.ID
                        select page;

                result = query.ToArray();
            }

            return result;
        }