Erik Grinaker is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

erikg / Revelation

Revelation is a password manager for the GNOME desktop, released under the GNU GPL license. It stores all your accounts and passwords in a single, secure place, and gives you access to it through a user-friendly graphical interface.

Clone this repository (size: 2.1 MB): HTTPS / SSH
hg clone https://bitbucket.org/erikg/revelation
hg clone ssh://hg@bitbucket.org/erikg/revelation

Revelation / src / lib / dialog.py

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  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
  53
  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
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
#
# Revelation 0.4.0 - a password manager for GNOME 2
# http://oss.codepoet.no/revelation/
# $Id$
#
# Module with dialogs
#
#
# Copyright (c) 2003-2005 Erik Grinaker
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

import config, datahandler, entry, io, ui, util

import gtk, gnome.ui



RESPONSE_NEXT           = 10
RESPONSE_PREVIOUS       = 11



##### EXCEPTIONS #####

class CancelError(Exception):
        "Exception for dialog cancellations"
        pass



##### BASE DIALOGS #####

class Dialog(gtk.Dialog):
        "Base class for dialogs"

        def __init__(self, parent, title, buttons = (), default = None):
                gtk.Dialog.__init__(
                        self, title, parent,
                        gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_MODAL | gtk.DIALOG_NO_SEPARATOR
                )

                self.set_border_width(6)
                self.vbox.set_spacing(12)
                self.set_resizable(False)

                self.connect("key_press_event", self.__cb_keypress)

                for stock, response in buttons:
                        self.add_button(stock, response)

                if default is not None:
                        self.set_default_response(default)

                elif len(buttons) > 0:
                        self.set_default_response(buttons[-1][1])


        def __cb_keypress(self, widget, data):
                "Callback for handling key presses"

                # close the dialog on Escape
                if data.keyval == 65307:
                        self.response(gtk.RESPONSE_CLOSE)


        def get_button(self, index):
                "Get one of the dialog buttons"

                buttons = self.action_area.get_children()

                if index < len(buttons):
                        return buttons[index]



class Utility(Dialog):
        "A utility dialog"

        def __init__(self, parent, title, buttons = ( ( gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE ), ), default = None):
                Dialog.__init__(self, parent, title, buttons, default)

                self.set_border_width(12)
                self.vbox.set_spacing(18)

                self.sizegroup  = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)
                self.tooltips   = gtk.Tooltips()


        def add_section(self, title, description = None):
                "Adds an input section to the dialog"

                section = ui.InputSection(title, description, self.sizegroup)
                self.vbox.pack_start(section)

                return section



class Message(Dialog):
        "A message dialog"

        def __init__(self, parent, title, text, stockimage, buttons = (), default = None):
                Dialog.__init__(self, parent, "", buttons, default)

                # hbox with image and contents
                hbox = ui.HBox()
                hbox.set_spacing(12)
                hbox.set_border_width(6)
                self.vbox.pack_start(hbox)

                # set up image
                if stockimage is not None:
                        image = ui.Image(stockimage, gtk.ICON_SIZE_DIALOG)
                        image.set_alignment(0.5, 0)
                        hbox.pack_start(image, False, False)

                # set up message
                self.contents = ui.VBox()
                self.contents.set_spacing(10)
                hbox.pack_start(self.contents)

                label = ui.Label(
                        "<span size=\"larger\" weight=\"bold\">%s</span>\n\n%s" %
                        ( util.escape_markup(title), text)
                )
                label.set_alignment(0, 0)
                label.set_selectable(True)
                self.contents.pack_start(label)


        def run(self):
                "Displays the dialog"

                self.show_all()
                response = Dialog.run(self)
                self.destroy()

                return response



class Error(Message):
        "Displays an error message"

        def __init__(self, parent, title, text, buttons = ( ( gtk.STOCK_OK, gtk.RESPONSE_OK), ), default = None):
                Message.__init__(self, parent, title, text, gtk.STOCK_DIALOG_ERROR, buttons, default)



class Info(Message):
        "Displays an info message"

        def __init__(self, parent, title, text, buttons = ( ( gtk.STOCK_OK, gtk.RESPONSE_OK ), ), default = None):
                Message.__init__(self, parent, title, text, gtk.STOCK_DIALOG_INFO, buttons, default)



class Question(Message):
        "Displays a question"

        def __init__(self, parent, title, text, buttons = ( ( gtk.STOCK_OK, gtk.RESPONSE_OK ), ), default = None):
                Message.__init__(self, parent, title, text, gtk.STOCK_DIALOG_QUESTION, buttons, default)



class Warning(Message):
        "Displays a warning message"

        def __init__(self, parent, title, text, buttons = ( ( gtk.STOCK_OK, gtk.RESPONSE_OK ), ), default = None):
                Message.__init__(self, parent, title, text, gtk.STOCK_DIALOG_WARNING, buttons, default)



##### QUESTION DIALOGS #####

class FileChanged(Warning):
        "Asks to save changes before proceeding"

        def __init__(self, parent, title, text):
                Warning.__init__(
                        self, parent, title, text,
                        ( ( ui.STOCK_DISCARD, gtk.RESPONSE_ACCEPT ), ( gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL ), ( gtk.STOCK_SAVE, gtk.RESPONSE_OK ) )
                )


        def run(self):
                "Displays the dialog"

                response = Warning.run(self)

                if response == gtk.RESPONSE_OK: 
                        return True

                elif response == gtk.RESPONSE_ACCEPT:
                        return False

                elif response in ( gtk.RESPONSE_CANCEL, gtk.RESPONSE_CLOSE ):
                        raise CancelError



class FileChangedNew(FileChanged):
        "Asks the user to save changes when creating a new file"

        def __init__(self, parent):
                FileChanged.__init__(
                        self, parent, "Save changes to current file?",
                        "You have made changes which have not been saved. If you create a new file without saving then these changes will be lost."
                )



class FileChangedOpen(FileChanged):
        "Asks the user to save changes when opening a different file"

        def __init__(self, parent):
                FileChanged.__init__(
                        self, parent, "Save changes before opening?",
                        "You have made changes which have not been saved. If you open a different file without saving then these changes will be lost."
                )



class FileChangedQuit(FileChanged):
        "Asks the user to save changes when quitting"

        def __init__(self, parent):
                FileChanged.__init__(
                        self, parent, "Save changes before quitting?",
                        "You have made changes which have not been saved. If you quit without saving, then these changes will be lost."
                )



class FileOverwrite(Warning):
        "Asks for confirmation when overwriting a file"

        def __init__(self, parent, file):
                Warning.__init__(
                        self, parent, "Overwrite existing file?",
                        "The file '%s' already exists - do you wish to replace this file?" % file,
                        ( ( gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL ), ( gtk.STOCK_OK, gtk.RESPONSE_OK ) )
                )


        def run(self):
                "Displays the dialog"

                if Warning.run(self) == gtk.RESPONSE_OK:
                        return True

                else:
                        raise CancelError



class FileSaveInsecure(Warning):
        "Asks for confirmation when exporting to insecure file"

        def __init__(self, parent):
                Warning.__init__(
                        self, parent, "Save to insecure file?",
                        "You have chosen to save your passwords to an insecure (unencrypted) file format - if anyone has access to this file, they will be able to see your passwords.",
                        ( ( gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL ), ( gtk.STOCK_SAVE, gtk.RESPONSE_OK ) ), 0
                )


        def run(self):
                "Runs the dialog"

                if Warning.run(self) == gtk.RESPONSE_OK:
                        return True

                else:
                        raise CancelError



##### FILE SELECTION DIALOGS #####

class FileSelector(gtk.FileChooserDialog):
        "A normal file selector"

        def __init__(self, parent, title = None, action = gtk.FILE_CHOOSER_ACTION_OPEN, stockbutton = None):

                if stockbutton is None:
                        if action == gtk.FILE_CHOOSER_ACTION_OPEN:
                                stockbutton = gtk.STOCK_OPEN

                        elif action == gtk.FILE_CHOOSER_ACTION_SAVE:
                                stockbutton = gtk.STOCK_SAVE

                gtk.FileChooserDialog.__init__(
                        self, title, parent, action,
                        ( gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, stockbutton, gtk.RESPONSE_OK )
                )

                self.set_local_only(False)
                self.set_default_response(gtk.RESPONSE_OK)

                self.inputsection = None


        def add_widget(self, title, widget):
                "Adds a widget to the file selection"

                if self.inputsection == None:
                        self.inputsection = ui.InputSection()
                        self.set_extra_widget(self.inputsection)

                self.inputsection.append_widget(title, widget)


        def get_filename(self):
                "Returns the file URI"

                return io.file_normpath(self.get_uri())


        def run(self):
                "Displays and runs the file selector, returns the filename"

                self.show_all()
                response = gtk.FileChooserDialog.run(self)
                filename = self.get_filename()
                self.destroy()

                if response == gtk.RESPONSE_OK:
                        return filename

                else:
                        raise CancelError



class ExportFileSelector(FileSelector):
        "A file selector for exporting files"

        def __init__(self, parent):
                FileSelector.__init__(
                        self, parent, "Select File to Export to",
                        gtk.FILE_CHOOSER_ACTION_SAVE, ui.STOCK_EXPORT
                )

                # set up filetype dropdown
                self.dropdown = ui.DropDown()
                self.add_widget("Filetype", self.dropdown)

                for handler in datahandler.get_export_handlers():
                        self.dropdown.append_item(handler.name, None, handler)


        def run(self):
                "Displays the dialog"

                self.show_all()
                self.inputsection.show_all()

                if gtk.FileSelection.run(self) == gtk.RESPONSE_OK:
                        filename = self.get_filename()
                        handler = self.dropdown.get_active_item()[2]
                        self.destroy()

                        return filename, handler

                else:
                        self.destroy()
                        raise CancelError



class ImportFileSelector(FileSelector):
        "A file selector for importing files"

        def __init__(self, parent):
                FileSelector.__init__(
                        self, parent, "Select File to Import",
                        gtk.FILE_CHOOSER_ACTION_OPEN, ui.STOCK_IMPORT
                )

                # set up filetype dropdown
                self.dropdown = ui.DropDown()
                self.add_widget("Filetype", self.dropdown)

                self.dropdown.append_item("Automatically detect")

                for handler in datahandler.get_import_handlers():
                        self.dropdown.append_item(handler.name, None, handler)


        def run(self):
                "Displays the dialog"

                self.show_all()
                self.inputsection.show_all()

                if gtk.FileSelection.run(self) == gtk.RESPONSE_OK:
                        filename = self.get_filename()
                        handler = self.dropdown.get_active_item()[2]
                        self.destroy()

                        return filename, handler

                else:
                        self.destroy()
                        raise CancelError



class OpenFileSelector(FileSelector):
        "A file selector for opening files"

        def __init__(self, parent):
                FileSelector.__init__(
                        self, parent, "Select File to Open",
                        gtk.FILE_CHOOSER_ACTION_OPEN, gtk.STOCK_OPEN
                )



class SaveFileSelector(FileSelector):
        "A file selector for saving files"

        def __init__(self, parent):
                FileSelector.__init__(
                        self, parent, "Select File to Save to",
                        gtk.FILE_CHOOSER_ACTION_SAVE, gtk.STOCK_SAVE
                )



##### PASSWORD DIALOGS #####

class Password(Message):
        "A base dialog for asking for passwords"

        def __init__(self, parent, title, text, stock = gtk.STOCK_OK):
                Message.__init__(
                        self, parent, title, text, gtk.STOCK_DIALOG_AUTHENTICATION,
                        ( ( gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL ), ( stock, gtk.RESPONSE_OK ))
                )

                self.entries = []

                self.sect_passwords = ui.InputSection()
                self.contents.pack_start(self.sect_passwords)


        def add_entry(self, name):
                "Adds a password entry to the dialog"

                entry = ui.Entry()
                entry.set_visibility(False)
                self.sect_passwords.append_widget(name, entry)

                self.entries.append(entry)

                return entry


        def run(self):
                "Displays the dialog"

                self.show_all()

                if len(self.entries) > 0:
                        self.entries[0].grab_focus()

                return gtk.Dialog.run(self)



class PasswordChange(Password):
        "A dialog for changing the password"

        def __init__(self, parent, password = None):
                Password.__init__(
                        self, parent, "Enter new password",
                        "Enter a new password for the current data file. The file must be saved before the new password is applied.",
                        ui.STOCK_PASSWORD_CHANGE
                )

                self.password = password

                if password is not None:
                        self.entry_current = self.add_entry("Current password")

                self.entry_new = self.add_entry("New password")
                self.entry_confirm = self.add_entry("Confirm password")


        def run(self):
                "Displays the dialog"

                while 1:
                        if Password.run(self) == gtk.RESPONSE_OK:

                                if self.password is not None and self.entry_current.get_text() != self.password:
                                        Error(self, "Incorrect password", "The password you entered as the current file password is incorrect.").run()

                                elif self.entry_new.get_text() != self.entry_confirm.get_text():
                                        Error(self, "Passwords don't match", "The password and password confirmation you entered does not match.").run()

                                else:
                                        password = self.entry_new.get_text()
                                        self.destroy()
                                        return password

                        else:
                                self.destroy()
                                raise CancelError



class PasswordLock(Password):
        "Asks for a password when the file is locked"

        def __init__(self, parent, password):
                Password.__init__(
                        self, parent, "Enter password to unlock file",
                        "The current file has been locked, please enter the file password to unlock it."
                )

                self.get_button(1).destroy()

                self.password = password
                self.entry_password = self.add_entry("Password")


        def run(self):
                "Displays the dialog"

                while 1:
                        try:
                                if Password.run(self) != gtk.RESPONSE_OK:
                                        continue

                                elif self.entry_password.get_text() == self.password:
                                        break

                                else:
                                        Error(self, "Incorrect password", "The password you entered was not correct, please try again.").run()

                        except CancelError:
                                continue

                self.destroy()



class PasswordOpen(Password):
        "Password dialog for opening files"

        def __init__(self, parent, filename):
                Password.__init__(
                        self, parent, "Enter file password",
                        "The file '%s' is encrypted. Please enter the file password to open it." % filename
                )

                self.entry_password = self.add_entry("Password")


        def run(self):
                "Displays the dialog"

                response = Password.run(self)
                password = self.entry_password.get_text()
                self.destroy()

                if response == gtk.RESPONSE_OK:
                        return password

                else:
                        raise CancelError



class PasswordSave(Password):
        "Password dialog for saving data"

        def __init__(self, parent, filename):
                Password.__init__(
                        self, parent, "Enter password for file",
                        "Please enter a password for the file '%s'. You will need this password to open the file at a later time." % filename
                )

                self.entry_new          = self.add_entry("New password")
                self.entry_confirm      = self.add_entry("Confirm password")


        def run(self):
                "Displays the dialog"

                while 1:
                        if Password.run(self) != gtk.RESPONSE_OK:
                                self.destroy()
                                raise CancelError

                        elif self.entry_new.get_text() != self.entry_confirm.get_text():
                                Error(self, "Passwords don't match", "The passwords you entered does not match.").run()

                        elif len(self.entry_new.get_text()) == 0:
                                Error(self, "No password entered", "You must enter a password for the new data file.").run()

                        else:
                                password = self.entry_new.get_text()
                                self.destroy()

                                return password



##### ENTRY DIALOGS #####

class EntryEdit(Utility):
        "A dialog for editing entries"

        def __init__(self, parent, cfg, title, e = None):
                Utility.__init__(
                        self, parent, title,
                        ( ( gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL ), ( gtk.STOCK_OK, gtk.RESPONSE_OK ) )
                )

                self.config             = cfg
                self.entry_field        = {}
                self.fielddata          = {}
                self.widgetdata         = {}

                # set up the ui
                self.sect_meta          = self.add_section(title)
                self.sect_fields        = None

                self.entry_name = ui.Entry()
                self.entry_name.set_width_chars(50)
                self.tooltips.set_tip(self.entry_name, "The name of the entry")
                self.sect_meta.append_widget("Name", self.entry_name)

                self.entry_desc = ui.Entry()
                self.tooltips.set_tip(self.entry_desc, "A description of the entry")
                self.sect_meta.append_widget("Description", self.entry_desc)

                self.dropdown = ui.EntryDropDown()
                self.dropdown.connect("changed", lambda w: self.__setup_fieldsect(self.dropdown.get_active_type()().fields))
                eventbox = ui.EventBox(self.dropdown)
                self.tooltips.set_tip(eventbox, "The type of entry - folders can contain other entries")
                self.sect_meta.append_widget("Type", eventbox)

                # populate the dialog with data
                self.set_entry(e)


        def __setup_fieldsect(self, fields):
                "Generates a field section based on a field list"

                # store current field values
                for fieldtype, fieldentry in self.entry_field.items():
                        self.fielddata[fieldtype] = fieldentry.get_text()

                self.entry_field = {}

                # create or remove field section as needed
                if self.sect_fields is None and len(fields) > 0:
                        self.sect_fields = self.add_section("Account data")

                elif self.sect_fields is not None and len(fields) > 0:
                        self.sect_fields.clear()

                elif self.sect_fields is not None and len(fields) == 0:
                        self.sect_fields.destroy()
                        self.sect_fields = None

                # generate field entries
                for field in fields:

                        if self.widgetdata.has_key(type(field)):
                                userdata = self.widgetdata[type(field)]
                        else:
                                userdata = None

                        fieldentry = ui.generate_field_edit_widget(field, self.config, userdata)
                        self.entry_field[type(field)] = fieldentry

                        if self.fielddata.has_key(type(field)):
                                fieldentry.set_text(self.fielddata[type(field)])

                        if (fieldentry.flags() & gtk.NO_WINDOW) == gtk.NO_WINDOW:
                                eventbox = ui.EventBox(fieldentry)
                                self.tooltips.set_tip(eventbox, field.description)
                                self.sect_fields.append_widget(field.name, eventbox)

                        else:
                                self.tooltips.set_tip(fieldentry, field.description)
                                self.sect_fields.append_widget(field.name, fieldentry)


                # show widgets
                if self.sect_fields is not None:
                        self.sect_fields.show_all()


        def allow_typechange(self, allow):
                "Sets whether type changes are allowed"

                self.dropdown.set_sensitive(allow)


        def get_entry(self):
                "Generates an entry from the dialog contents"

                e = self.dropdown.get_active_type()()

                e.name = self.entry_name.get_text()
                e.description = self.entry_desc.get_text()

                for field in e.fields:
                        field.value = self.entry_field[type(field)].get_text()

                return e


        def run(self):
                "Displays the dialog"

                while 1:
                        self.show_all()

                        if Utility.run(self) == gtk.RESPONSE_OK:
                                e = self.get_entry()

                                if e.name == "":
                                        Error(self, "Name not entered", "You must enter a name for the account").run()
                                        continue

                                self.destroy()
                                return e

                        else:
                                self.destroy()
                                raise CancelError


        def set_entry(self, e):
                "Sets an entry for the dialog"

                e = e is not None and e.copy() or entry.GenericEntry()

                self.entry_name.set_text(e.name)
                self.entry_desc.set_text(e.description)
                self.dropdown.set_active_type(type(e))

                for field in e.fields:
                        self.entry_field[type(field)].set_text(field.value)


        def set_fieldwidget_data(self, fieldtype, userdata):
                "Sets user data for fieldwidget"

                self.widgetdata[fieldtype] = userdata

                if fieldtype == entry.UsernameField and self.entry_field.has_key(entry.UsernameField):
                        self.entry_field[entry.UsernameField].set_values(userdata)



class EntryRemove(Warning):
        "Asks for confirmation when removing entries"

        def __init__(self, parent, entries):

                if len(entries) > 1:
                        title   = "Really remove the %i selected entries?" % len(entries)
                        text    = "By removing these entries you will also remove any entries they may contain."

                elif type(entries[0]) == entry.FolderEntry:
                        title   = "Really remove folder '%s'?" % entries[0].name
                        text    = "By removing this folder you will also remove all accounts and folders it contains."

                else:
                        title   = "Really remove account '%s'?" % entries[0].name
                        text    = "Please confirm that you wish to remove this account."


                Warning.__init__(
                        self, parent, title, text,
                        ( ( gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL ), ( ui.STOCK_REMOVE, gtk.RESPONSE_OK ) ),
                        0
                )


        def run(self):
                "Displays the dialog"

                if Warning.run(self) == gtk.RESPONSE_OK:
                        return True

                else:
                        raise CancelError



##### MISCELLANEOUS DIALOGS #####

class About(gnome.ui.About):
        "About dialog"

        def __init__(self, parent):
                gnome.ui.About.__init__(
                        self, config.APPNAME, config.VERSION, config.COPYRIGHT,
                        """"%s"\n\nRevelation is a password manager for the GNOME 2 desktop""" % config.RELNAME,
                        ( config.AUTHOR, ), None, "",
                        gtk.icon_theme_get_default().load_icon("revelation", 48, 0)
                )

                if parent is not None:
                        self.set_transient_for(parent)


        def run(self):
                "Displays the dialog"

                self.show_all()



class Exception(Error):
        "Displays a traceback for an unhandled exception"

        def __init__(self, parent, traceback):
                Error.__init__(
                        self, parent, "Unknown error",
                        "An unknown error occured. Please report the text below to the Revelation developers, along with what you were doing that may have caused the error. You may attempt to continue running Revelation, but it may behave unexpectedly.",
                        ( ( gtk.STOCK_QUIT, gtk.RESPONSE_CANCEL ), ( "Continue", gtk.RESPONSE_OK ) )
                )

                textview = ui.TextView(None, traceback)
                scrolledwindow = ui.ScrolledWindow(textview)
                scrolledwindow.set_size_request(-1, 120)

                self.contents.pack_start(scrolledwindow)


        def run(self):
                "Runs the dialog"

                return Error.run(self) == gtk.RESPONSE_OK



class Find(Utility):
        "A find dialog"

        def __init__(self, parent, cfg):
                Utility.__init__(
                        self, parent, "Find an entry",
                        ( ( gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE ), ( ui.STOCK_PREVIOUS, RESPONSE_PREVIOUS ), ( ui.STOCK_NEXT, RESPONSE_NEXT ) )
                )

                self.config = cfg

                section = self.add_section("Find an entry")

                # search string entry
                self.entry_string = ui.Entry()
                self.entry_string.connect("changed", lambda w: self.__state_buttons(self.entry_string.get_text() != ""))

                section.append_widget("Search for", self.entry_string)
                self.tooltips.set_tip(self.entry_string, "The text to search for")

                # account type dropdown
                self.dropdown = ui.EntryDropDown()
                self.dropdown.delete_item(0)
                self.dropdown.insert_item(0, "Any", "gnome-stock-about")

                eventbox = ui.EventBox(self.dropdown)
                section.append_widget("Account type", eventbox)
                self.tooltips.set_tip(eventbox, "The account type to search for")

                # folders checkbutton
                self.check_folders = ui.CheckButton("Search for folders as well")
                ui.config_bind(self.config, "search/folders", self.check_folders)

                section.append_widget(None, self.check_folders)
                self.tooltips.set_tip(self.check_folders, "When enabled, folder names and descriptions will also be searched")

                # namedesc checkbutton
                self.check_namedesc = ui.CheckButton("Only search in name and description")
                ui.config_bind(self.config, "search/namedesc", self.check_namedesc)

                section.append_widget(None, self.check_namedesc)
                self.tooltips.set_tip(self.check_namedesc, "When enabled, only entry names and descriptions will be searched")

                # case sensitive checkbutton
                self.check_casesensitive = ui.CheckButton("Case sensitive")
                ui.config_bind(self.config, "search/casesens", self.check_casesensitive)

                section.append_widget(None, self.check_casesensitive)
                self.tooltips.set_tip(self.check_casesensitive, "When enabled, searches will be case sensitive")

                # set initial state
                self.__state_buttons(False)


        def __state_buttons(self, active):
                "Sets button sensitivity based on entry contents"

                self.get_button(0).set_sensitive(active)
                self.get_button(1).set_sensitive(active)


        def run(self):
                "Displays the dialog"

                self.show_all()
                self.entry_string.grab_focus()

                return Utility.run(self)



class PasswordGenerator(Utility):
        "A password generator dialog"

        def __init__(self, parent, cfg):
                Utility.__init__(
                        self, parent, "Password Generator",
                        ( ( gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE ), ( ui.STOCK_GENERATE, gtk.RESPONSE_OK ) )
                )

                self.config = cfg
                self.section = self.add_section("Password Generator")

                self.entry = ui.Entry()
                self.entry.set_editable(False)
                self.tooltips.set_tip(self.entry, "The generated password")
                self.section.append_widget("Password", self.entry)

                self.spin_pwlen = ui.SpinEntry()
                self.spin_pwlen.set_range(4, 256)
                self.spin_pwlen.set_value(self.config.get("passwordgen/length"))
                self.tooltips.set_tip(self.spin_pwlen, "The number of characters in generated passwords - 8 or more are recommended")
                self.section.append_widget("Length", self.spin_pwlen)

                self.check_ambiguous = ui.CheckButton("Avoid ambiguous characters")
                self.check_ambiguous.set_active(self.config.get("passwordgen/avoid_ambiguous"))
                self.tooltips.set_tip(self.check_ambiguous, "When enabled, generated passwords will not contain ambiguous characters - like 0 (zero) and O (capital o)")
                self.section.append_widget(None, self.check_ambiguous)


        def run(self):
                "Displays the dialog"

                self.show_all()
                self.get_button(0).grab_focus()

                while 1:
                        if Utility.run(self) == gtk.RESPONSE_OK:
                                self.entry.set_text(util.generate_password(
                                        self.spin_pwlen.get_value(),
                                        self.check_ambiguous.get_active()
                                ))

                        else:
                                self.destroy()
                                break



class Preferences(Utility):
        "A preference dialog"

        def __init__(self, parent, cfg):
                Utility.__init__(self, parent, "Preferences")
                self.config = cfg

                self.notebook = ui.Notebook()
                self.vbox.pack_start(self.notebook)

                self.page_general = self.notebook.create_page("General")
                self.__init_section_file(self.page_general)
                self.__init_section_pwgen(self.page_general)

                self.page_gotocmd = self.notebook.create_page("Goto commands")
                self.__init_section_gotocmd(self.page_gotocmd)


        def __init_section_file(self, page):
                "Sets up the file section"

                self.section_file = page.add_section("File handling")

                # check-button for autosave
                self.check_autosave = ui.CheckButton("Autosave data when changed")
                ui.config_bind(self.config, "file/autosave", self.check_autosave)

                self.tooltips.set_tip(self.check_autosave, "Automatically saves the data file when an entry is added, modified or removed")
                self.section_file.append_widget(None, self.check_autosave)

                # check-button for autoloading a file
                self.check_autoload = ui.CheckButton("Open file on startup")
                ui.config_bind(self.config, "file/autoload", self.check_autoload)
                self.check_autoload.connect("toggled", lambda w: self.entry_autoload_file.set_sensitive(w.get_active()))

                self.tooltips.set_tip(self.check_autoload, "When enabled, a file will be opened when the program is started")
                self.section_file.append_widget(None, self.check_autoload)

                # entry for file to autoload
                self.entry_autoload_file = ui.FileEntry("Select File to Automatically Open")
                ui.config_bind(self.config, "file/autoload_file", self.entry_autoload_file)
                self.entry_autoload_file.set_sensitive(self.check_autoload.get_active())

                eventbox = ui.EventBox(self.entry_autoload_file)
                self.tooltips.set_tip(eventbox, "A file to be opened when the program is started")
                self.section_file.append_widget("File to open", eventbox)


        def __init_section_gotocmd(self, page):
                "Sets up the goto command section"

                self.section_gotocmd = page.add_section("Goto commands")

                for entrytype in entry.ENTRYLIST:
                        if entrytype == entry.FolderEntry:
                                continue

                        e = entrytype()

                        widget = ui.Entry()
                        ui.config_bind(self.config, "launcher/%s" % e.id, widget)

                        tooltip = "Goto command for %s accounts. The following expansion variables can be used:\n\n" % e.typename

                        for field in e.fields:
                                tooltip += "%%%s: %s\n" % ( field.symbol, field.name )

                        tooltip += "\n"
                        tooltip += "%%: a % sign\n"
                        tooltip += "%?x: optional expansion variable\n"
                        tooltip += "%(...%): optional substring expansion"

                        self.tooltips.set_tip(widget, tooltip)
                        self.section_gotocmd.append_widget(e.typename, widget)


        def __init_section_pwgen(self, page):
                "Sets up the password generator section"

                self.section_pwgen = page.add_section("Password Generator")

                # password length spinbutton
                self.spin_pwlen = ui.SpinEntry()
                self.spin_pwlen.set_range(4, 32)
                ui.config_bind(self.config, "passwordgen/length", self.spin_pwlen)

                self.tooltips.set_tip(self.spin_pwlen, "The number of characters in generated passwords - 8 or more are recommended")
                self.section_pwgen.append_widget("Password length", self.spin_pwlen)

                # checkbox for avoiding ambiguous characters
                self.check_ambiguous = ui.CheckButton("Avoid ambiguous characters")
                ui.config_bind(self.config, "passwordgen/avoid_ambiguous", self.check_ambiguous)

                self.tooltips.set_tip(self.check_ambiguous, "When enabled, generated passwords will not contain ambiguous characters - like 0 (zero) and O (capital o)")
                self.section_pwgen.append_widget(None, self.check_ambiguous)


        def run(self):
                "Runs the preference dialog"

                self.show_all()
                Utility.run(self)
                self.destroy()