/// <summary>
 ///     Initializes a new instance of the <see cref="MainFormLogicManager" /> class.
 /// </summary>
 /// <param name="memoStorageService">The memo storage service.</param>
 /// <param name="fileStorageService">The file storage service.</param>
 /// <param name="passwordStorage">The password storage.</param>
 /// <param name="scope">The scope.</param>
 /// <param name="appSettingsService">The application settings service.</param>
 public MainFormLogicManager(MemoStorageService memoStorageService, FileStorageService fileStorageService, PasswordStorage passwordStorage, ILifetimeScope scope, AppSettingsService appSettingsService)
 {
     _memoStorageService = memoStorageService;
     _fileStorageService = fileStorageService;
     _scope = scope;
     _appSettingsService    = appSettingsService;
     _passwordStorage       = passwordStorage;
     _tabPageDataCollection = TabPageDataCollection.CreateNewPageDataCollection(_appSettingsService.Settings.DefaultEmptyTabPages);
 }
        private void openDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var formGetPassword = new FormGetPassword
            {
                PasswordSalt          = _appSettingsService.Settings.ApplicationSaltValue,
                PasswordDerivedString = _appSettingsService.Settings.PasswordDerivedString
            };

            if (formGetPassword.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }
            string password = formGetPassword.PasswordString;

            _passwordStorage.Set(PwdKey, password);

            TabPageDataCollection tabPageCollection;

            try
            {
                tabPageCollection = _memoStorageService.LoadTabPageCollection(password);

                // Make sure that every tabPageData has a unique Id
                bool uniqueIdCreated = tabPageCollection.TabPageDictionary.Values.Aggregate(false, (current, tabPageData) => current | tabPageData.GenerateUniqueIdIfNoneExists());

                if (uniqueIdCreated)
                {
                    _applicationState.UniqueIdMissingFromExistingTabPage = true;
                }

                if (tabPageCollection.TabPageDictionary.Count == 0)
                {
                    tabPageCollection = TabPageDataCollection.CreateNewPageDataCollection(_appSettingsService.Settings.DefaultEmptyTabPages);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Open database Error.");
                MessageBox.Show(this, "Unable to load database, please verify that you entered the correct password. " + ex.Message, "Failed to load database", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }

            _tabPageDataCollection = tabPageCollection;
            InitializeTabControls();
            _applicationState.DatabaseLoaded     = true;
            _applicationState.TabIndexChanged    = false;
            _applicationState.TabPageAddOrRemove = false;
            _applicationState.TabTextDataChanged = false;
            UpdateApplicationState();
        }
        public FormMain(AppSettingsService appSettingsService, MemoStorageService memoStorageService, PasswordStorage passwordStorage, ILifetimeScope scope)
        {
            if (DesignMode)
            {
                return;
            }

            _appSettingsService = appSettingsService;
            _memoStorageService = memoStorageService;
            _passwordStorage    = passwordStorage;
            _scope = scope;

            _applicationState      = new ApplicationState();
            _tabPageDataCollection = TabPageDataCollection.CreateNewPageDataCollection(_appSettingsService.Settings.DefaultEmptyTabPages);
            _licenseService        = LicenceService.Instance;
            InitializeComponent();
        }
        private void createNewDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (
                MessageBox.Show(this, "Do you want to create a new Memo database? Doing so will overwrite any existing stored database under this account.", "Create new database?",
                                MessageBoxButtons.OKCancel, MessageBoxIcon.Question) !=
                DialogResult.OK)
            {
                return;
            }

            var frmsetPassword = new FormSetPassword {
                Text = "Choose password"
            };

            if (frmsetPassword.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            string password = frmsetPassword.VerifiedPassword;

            if (string.IsNullOrEmpty(password))
            {
                MessageBox.Show(this, "Password can not be empty!", Resources.FormMain__ErrorText, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            _passwordStorage.Set(PwdKey, password);
            _tabPageDataCollection = TabPageDataCollection.CreateNewPageDataCollection(_appSettingsService.Settings.DefaultEmptyTabPages);
            _memoStorageService.SaveTabPageCollection(_tabPageDataCollection, password);

            _appSettingsService.Settings.PasswordDerivedString =
                GeneralConverters.GeneratePasswordDerivedString(_appSettingsService.Settings.ApplicationSaltValue + password + _appSettingsService.Settings.ApplicationSaltValue);

            _appSettingsService.SaveSettings();
            InitializeTabControls();
            _applicationState.DatabaseLoaded = true;
            _applicationState.DatabaseExists = true;
            UpdateApplicationState();
        }