File : dialogs/apiKeysDialog/DataEncryptorHandlers.js

1
/*
2
Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/
3
4
This  program is free software;
5
you can redistribute it and/or modify it under the terms of the
6
GNU General Public License as published by the Free Software Foundation;
7
either version 3 of the License, or any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
*/
18
/*
19
Changes:
20
    - v4.0.0:
21
        - created from v3.6.0
22
Doc reviewed 202208
23
 */
24
25
import theUtilities from '../../core/uiLib/Utilities.js';
26
import theTranslator from '../../core/uiLib/Translator.js';
27
28
/* ------------------------------------------------------------------------------------------------------------------------- */
29
/**
30
handlers for DataEncryptor
31
*/
32
/* ------------------------------------------------------------------------------------------------------------------------- */
33
34
class DataEncryptorHandlers {
35
36
    /**
37
    A reference to the ApiKeys dialog
38
    @type {ApiKeysDialog}
39
    */
40
41
    #ApiKeysDialog;
42
43
    /**
44
    The constructor
45
    @param {ApiKeysDialog} ApiKeysDialog A reference to the ApiKeys dialog
46
    */
47
48
    constructor ( ApiKeysDialog ) {
49
        this.#ApiKeysDialog = ApiKeysDialog;
50
        Object.freeze ( this );
51
    }
52
53
    /**
54
    The destructor
55
    */
56
57
    destructor ( ) {
58
        this.#ApiKeysDialog = null;
59
    }
60
61
    /**
62
    onErrorDecrypt handler for the DataEncryptor
63
    @param {Error|String} err The error to handle
64
    */
65
66
    onErrorDecrypt ( err ) {
67
        this.#ApiKeysDialog.hideWait ( );
68
        this.#ApiKeysDialog.keyboardELEnabled = true;
69
        if ( err && 'Canceled by user' !== err ) {
70
            this.#ApiKeysDialog.showError (
71
                theTranslator.getText ( 'DataEncryptorHandlers - An error occurs when reading the file' )
72
            );
73
        }
74
    }
75
76
    /**
77
    onOkDecrypt handler for the DataEncryptor
78
    @param {Uint8Array} data The decrypted data to handle
79
    */
80
81
    onOkDecrypt ( data ) {
82
        try {
83
            this.#ApiKeysDialog.addApiKeys (
84
                JSON.parse ( new TextDecoder ( ).decode ( data ) )
85
            );
86
        }
87
        catch ( err ) {
88
            this.onErrorDecrypt ( err );
89
            return;
90
        }
91
        this.#ApiKeysDialog.hideWait ( );
92
        this.#ApiKeysDialog.hideError ( );
93
        this.#ApiKeysDialog.keyboardELEnabled = true;
94
    }
95
96
    /**
97
    onOkEncrypt handler for the DataEncryptor
98
    @param {Uint8Array} data The encrypted data to handle
99
    */
100
101
    onOkEncrypt ( data ) {
102
        this.#ApiKeysDialog.hideError ( );
103
        this.#ApiKeysDialog.hideWait ( );
104
        theUtilities.saveFile ( 'ApiKeys', data );
105
        this.#ApiKeysDialog.keyboardELEnabled = true;
106
    }
107
108
    /**
109
    onErrorEncrypt handler for the DataEncryptor
110
    */
111
112
    onErrorEncrypt ( ) {
113
        this.#ApiKeysDialog.showError (
114
            theTranslator.getText ( 'DataEncryptorHandlers - An error occurs when saving the keys' )
115
        );
116
        this.#ApiKeysDialog.hideWait ( );
117
        this.#ApiKeysDialog.keyboardELEnabled = true;
118
    }
119
120
}
121
122
export default DataEncryptorHandlers;
123
124
/* --- End of file --------------------------------------------------------------------------------------------------------- */
125