private static void DoLogout(UserClient user)
        {
            log.Debug("Logging out current user...");
            // deletes the stored credentials
            BaseWS.Logout();
            if (user != null)
                user.SaveUserPreferences();

            // disconnect the user and stop the periodic login thread
            WebConnector.Current.Logout();
            if (LoginTimer != null)
                LoginTimer.Dispose();
            log.Debug("Logout completed!");
        }
        public static ErrorCodes TryLoginOneAll(UserClient user, IPluginManager pluginManager)
        {
            UserWS repoUser = new UserWS();
            string identToken = BaseWS.IdentToken;
            Snip2Code.Model.Entities.User loggedUser = repoUser.LoginUserOneAll(identToken);

            if ((loggedUser != null) && (loggedUser.ID > 0))
            {
                // Save credentials
                BaseWS.Username = string.Empty;
                BaseWS.Password = string.Empty;
                user.SaveUserPreferences();

                // change the logged user on the package
                LoginPoller poller = new LoginPoller(string.Empty, string.Empty, identToken, true, pluginManager);
                if (ClientUtils.LoginTimer != null)
                    ClientUtils.LoginTimer.Dispose();
                ClientUtils.LoginTimer = new System.Threading.Timer(poller.RecallLogin, null, 0, AppConfig.Current.LoginRefreshTimeSec * 1000);

                return ErrorCodes.OK;
            }
            else
            {
                // something went wrong on the login: reload the oneAllPage
                BaseWS.IdentToken = string.Empty;

                if (repoUser.LastErrorCode == ErrorCodes.OK)
                    return ErrorCodes.NOT_LOGGED_IN;
                else
                    return repoUser.LastErrorCode;
            }
        }
        /// <summary>
        /// Do all the actions needed when a wrong login is detected after changing the server URL
        /// from About form
        /// </summary>
        /// <param name="pluginManager"></param>
        /// <param name="user"></param>
        /// <param name="oldServer">previous URL of the server</param>
        /// <param name="newServer">new URL of the server</param>
        public static void ManageInvalidLoginOnNewServerFromAbout(IPluginManager pluginManager, UserClient user,
            string oldServer, string newServer)
        {
            // change the visibility of buttons of top menu and right-click
            BaseWS.Server = oldServer;    // this is needed to logout the old session on the old server
            LogoutUser(pluginManager, user);
            BaseWS.Server = newServer;    // from now on, go ahead with the new server
            // save new server path
            user.SaveUserPreferences();

            // update the login page to address the one-all form to the new server
            LoginPageCleanup(pluginManager);
        }
        /// <summary>
        /// Try to login into the remote erver with the given credentials
        /// </summary>
        /// <param name="user"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns>true if the login has been successful; false otherwise</returns>
        public static ErrorCodes TryLogin(UserClient user, string username, string password, IPluginManager pluginManager)
        {
            // Test login
            UserWS repoUser = new UserWS();
            Snip2Code.Model.Entities.User loggedUser = repoUser.LoginUser(username, password);

            if ((loggedUser != null) && (loggedUser.ID > 0))
            {
                // Save credentials
                BaseWS.Username = username;
                BaseWS.Password = password;
                BaseWS.IdentToken = string.Empty;
                user.SaveUserPreferences();

                // change the logged user on the package
                LoginPoller poller = new LoginPoller(username, password, string.Empty, false, pluginManager);
                if (ClientUtils.LoginTimer != null)
                    ClientUtils.LoginTimer.Dispose();
                ClientUtils.LoginTimer = new System.Threading.Timer(poller.RecallLogin, null, 0, AppConfig.Current.LoginRefreshTimeSec * 1000);
                return ErrorCodes.OK;
            }

            if (repoUser.LastErrorCode == ErrorCodes.OK)
                return ErrorCodes.NOT_LOGGED_IN;
            else
                return repoUser.LastErrorCode;
        }