Project

Profile

Help

Revision bdd3adcb

Added by Vincent Le Goff over 3 years ago

Add #77: the user can now set default characters

View differences:

src/character.py
59 59
        self.username = ""
60 60
        self.password = ""
61 61
        self.other_commands = ""
62
        self.default = False
62 63

  
63 64
        # Character's configuration
64 65
        self.aliases = []
......
106 107
        self.username = safe.retrieve("username", "")
107 108
        self.password = safe.retrieve("password", "")
108 109
        self.other_commands = safe.retrieve("other_commands", "")
110
        self.default = safe.retrieve("default", False)
109 111

  
110 112
    def save(self):
111 113
        """Save the character."""
......
114 116
        safe.store("username", self.username)
115 117
        safe.store("password", self.password)
116 118
        safe.store("other_commands", self.other_commands)
119
        safe.store("default", self.default)
117 120

  
118 121
    def open_notepad(self):
119 122
        """Open and return the notepad associated to this character."""
src/safe.py
50 50
login = safe.retrieve("login")
51 51
password = safe.retrieve("password")
52 52

  
53
Note that datas that is not a string (like a bool or float) will be
54
saved as unprotected data.  If you want to save it encrypted, you can
55
convert it to string.
56

  
53 57
"""
54 58

  
55 59
import base64
......
154 158
        If the key isn't present in the dictionary, either
155 159
        return default if specified, or raise a KeyError.
156 160

  
161
        If the value at this location isn't a string, return it as is.
162

  
157 163
        """
158 164
        if key not in self.data:
159 165
            if default:
......
162 168
            raise KeyError(key)
163 169

  
164 170
        value = self.data[key]
165
        salt = self.get_salt_from_key(key)
166
        return self.decrypt(value, salt)
171
        if isinstance(value, basestring):
172
            salt = self.get_salt_from_key(key)
173
            return self.decrypt(value, salt)
174

  
175
        return value
167 176

  
168 177
    def store(self, key, value):
169 178
        """Store the key in the file.
170 179

  
171 180
        If the key already exists, replaces it.
181
        If the value is not a string or unicode, it will be stored
182
        WITHOUT encryption.
172 183

  
173 184
        """
174
        salt = self.get_salt_from_key(key)
175
        crypted = self.encrypt(value, salt)
176
        self.data[key] = crypted
185
        if isinstance(value, basestring):
186
            salt = self.get_salt_from_key(key)
187
            crypted = self.encrypt(value, salt)
188
            self.data[key] = crypted
189
        else:
190
            self.data[key] = value
177 191

  
178 192
        # Write the new data in the file
179 193
        with open(self.secret, "wb") as file:
src/translations/en/ui/message/character.yml
1
missing_name: You must specify a name to set this character.
1
default: Set this character as the default choice
2
missing_name: You must specify a name to set this character.
2 3
password: Password, if any
3 4
post_login: Commands, if any, to send after the password
4 5
username: Username or other commands to send before the password
src/translations/fr/ui/message/character.yml
1
missing_name: Vous devez préciser un nom pour modifier ce personnage.
1
default: Positionner ce personnage comme choix par défaut
2
missing_name: Vous devez préciser un nom pour modifier ce personnage.
2 3
password: Mot de passe optionnel
3 4
post_login: Les commandes optionnelles à envoyer après le mot de passe
4 5
username: >
src/ui/dialogs/character.py
93 93
        s_post_login.Add(t_post)
94 94
        sizer.Add(s_post_login)
95 95

  
96
        # Create the 'default character' checkbox
97
        self.default = wx.CheckBox(self,
98
                label=t("ui.message.character.default"))
99
        self.default.SetValue(character and character.default or True)
100

  
96 101
        # Buttons
97 102
        sizer.Add(buttons)
98 103
        t_name.SetFocus()
......
111 116
        password = self.password.GetValue().encode("utf-8", errors="replace")
112 117
        post_login = self.post_login.GetValue().encode("utf-8",
113 118
                errors="replace")
119
        default = self.default.GetValue()
114 120
        if not name:
115 121
            wx.MessageBox(t("ui.message.character.missing_name"),
116 122
                    t("ui.alert.missing"), wx.OK | wx.ICON_ERROR)
......
123 129
            character.username = username
124 130
            character.password = password
125 131
            character.other_commands = post_login
132
            character.default = default
126 133
            character.save()
127 134
            self.session.character = character
135

  
136
            # If 'default' is set to True, put other character.default
137
            # to False (there can only be one default character)
138
            for other in world.characters.values():
139
                if other is character:
140
                    continue
141

  
142
                if default and other.default:
143
                    other.default = False
144
                    other.save()
145

  
128 146
            self.Destroy()
129 147

  
130 148
    def OnCancel(self, e):
src/ui/dialogs/connection.py
123 123
                # Change the world's characters
124 124
                self.characters.DeleteAllItems()
125 125
                self.characters.Append((t("ui.client.any_character"), ))
126
                self.characters.Select(0)
127
                self.characters.Focus(0)
126 128
                characters = sorted(world.characters.values(),
127 129
                        key=lambda c: c.location)
128
                for character in characters:
130
                for i, character in enumerate(characters):
129 131
                    self.characters.Append((character.name, ))
130
                self.characters.Select(0)
131
                self.characters.Focus(0)
132
                    if character.default:
133
                        self.characters.Select(i + 1)
134
                        self.characters.Focus(i + 1)
132 135

  
133 136
    def OnSelectWorld(self, e):
134 137
        """When the selection changes."""
......
143 146
                    key=lambda c: c.location)
144 147
            self.characters.DeleteAllItems()
145 148
            self.characters.Append((t("ui.client.any_character"), ))
146
            for character in characters:
147
                self.characters.Append((character.name, ))
148 149
            self.characters.Select(0)
149 150
            self.characters.Focus(0)
151
            for i, character in enumerate(characters):
152
                self.characters.Append((character.name, ))
153
                if character.default:
154
                    self.characters.Select(i + 1)
155
                    self.characters.Focus(i + 1)
150 156

  
151 157
    def OnKeyDown(self, e):
152 158
        """If Enter is pressed, connect to the selected world."""

Also available in: Unified diff