create_key_pair.py 3.92 KB
Newer Older
1 2 3 4 5 6
'''
This modules helps display and manage key pairs
'''
import logging

from kivy.uix.boxlayout import BoxLayout
Shashank Kumar's avatar
Shashank Kumar committed
7
from kivy.uix.popup import Popup
8 9 10 11
from kivy.lang import Builder
from kivy.clock import Clock

from modules.encryption.tools.all_tools.exceptions import GPGError
Shashank Kumar's avatar
Shashank Kumar committed
12
from modules.encryption.tools.all_tools.services import create_gpg_key_pair
13
from modules.encryption.tools.all_tools.validations import (
Shashank Kumar's avatar
Shashank Kumar committed
14
    validate_email, validate_name, validate_comment, validate_expire_date
15 16 17 18 19 20
)


Builder.load_file('./ui/encryption/all_tools/create_key_pair.kv')


Shashank Kumar's avatar
Shashank Kumar committed
21 22 23 24 25 26 27
class CreateKeyPairPopup(Popup):
    '''
    CreateKeyPairPopup displays popup to inform about key creation progress
    '''
    pass


28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
class CreateKeyPair(BoxLayout):
    '''
    CreateKeyPair helps integrate UI with helper functions
    '''

    def prompt_error_message(self, label, error_text):
        '''
        Displays error message on the UI on the respective label widget
        '''
        original_text = self.ids[label].text
        self.ids[label].text = error_text
        self.ids[label].color = [1, 0, 0, 1]

        def replace_label(*args):
            '''
            Replacing original text in label
            delay time is defined by args[0]
            '''
            logging.info(
                '\'%s\' changed to \'%s\' after %s seconds',
                error_text,
                original_text,
                args[0]
            )
            self.ids[label].text = original_text
Shashank Kumar's avatar
Shashank Kumar committed
53
            self.ids[label].color = [0, 0, 0, 1]
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        Clock.schedule_once(replace_label, 2)

    def validate(self):
        '''
        Validating user input
        '''
        name = self.ids['input_name'].text
        email = self.ids['input_email'].text
        comment = self.ids['input_comment'].text
        expire_date = self.ids['input_expire_date'].text

        validation_successful = True
        try:
            validate_name(name)
        except GPGError as error:
            validation_successful = False
            self.prompt_error_message(
                'label_name',
                error.message
            )

        try:
            validate_email(email)
        except GPGError as error:
            validation_successful = False
            self.prompt_error_message(
                'label_email',
                error.message
            )

        try:
            validate_comment(comment)
        except GPGError as error:
            validation_successful = False
            self.prompt_error_message(
                'label_comment',
                error.message
            )

        try:
            validate_expire_date(expire_date)
        except GPGError as error:
            validation_successful = False
            self.prompt_error_message(
                'label_expire_date',
                error.message
            )

        return validation_successful

Shashank Kumar's avatar
Shashank Kumar committed
104
    def creating_key_pair(self):
105 106 107
        '''
        Processing user input to create key pair
        '''
Shashank Kumar's avatar
Shashank Kumar committed
108
        creating_key_pair_popup = CreateKeyPairPopup()
109 110 111

        def add_button(*args):
            '''
Shashank Kumar's avatar
Shashank Kumar committed
112
            Add Key fingerprint to label and button
113
            '''
Shashank Kumar's avatar
Shashank Kumar committed
114
            creating_key_pair_popup.ids['create_gpg_key_pair_popup_label'].text = args[0]
115 116 117 118 119 120 121 122

        if self.validate():
            name = self.ids['input_name'].text
            email = self.ids['input_email'].text
            comment = self.ids['input_comment'].text
            expire_date = self.ids['input_expire_date'].text

            try:
Shashank Kumar's avatar
Shashank Kumar committed
123 124
                creating_key_pair_popup.open()
                key_fingerprint = create_gpg_key_pair(
125 126 127 128 129 130 131 132 133 134
                    name=name,
                    email=email,
                    comment=comment,
                    expire_date=expire_date
                )
                message = 'Key has been created with\n{}\nfingerprint'.format(
                    key_fingerprint)
                add_button(message)
            except GPGError as error:
                add_button(error.message)