Skip to content

ui

InvalidCredentialsWindow

Bases: QDialog

Source code in common/ayon_common/connection/ui/invalid_window.py
 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
class InvalidCredentialsWindow(QtWidgets.QDialog):
    default_width = 420
    default_height = 170

    def __init__(self, message, *args, **kwargs):
        super().__init__(*args, **kwargs)

        icon_path = get_icon_path()
        icon = QtGui.QIcon(icon_path)
        self.setWindowIcon(icon)
        self.setWindowTitle("Invalid credentials")

        info_widget = QtWidgets.QWidget(self)

        message_label = url_cred_sep = None
        if message:
            # --- Custom message ---
            message_label = QtWidgets.QLabel(message, info_widget)
            message_label.setWordWrap(True)
            message_label.setTextInteractionFlags(
                QtCore.Qt.TextBrowserInteraction
            )

            # --- URL separator ---
            url_cred_sep = QtWidgets.QFrame(info_widget)
            url_cred_sep.setObjectName("Separator")
            url_cred_sep.setMinimumHeight(2)
            url_cred_sep.setMaximumHeight(2)

        common_label = QtWidgets.QLabel(
            (
                " You are running AYON launcher in <b>'bypass login'</b>"
                " mode. Meaning your AYON connection information is"
                " defined by environment variables."
                "<br/><br/>Please contact your administrator or use valid"
                " credentials."
            ),
            info_widget
        )
        common_label.setWordWrap(True)
        common_label.setTextInteractionFlags(QtCore.Qt.TextBrowserInteraction)

        info_layout = QtWidgets.QVBoxLayout(info_widget)
        info_layout.setContentsMargins(0, 0, 0, 0)
        if message_label is not None:
            info_layout.addWidget(message_label, 0)
            info_layout.addWidget(url_cred_sep, 0)
        info_layout.addWidget(common_label, 0)

        footer_widget = QtWidgets.QWidget(self)
        close_btn = QtWidgets.QPushButton("Close", footer_widget)

        footer_layout = QtWidgets.QHBoxLayout(footer_widget)
        footer_layout.setContentsMargins(0, 0, 0, 0)
        footer_layout.addStretch(1)
        footer_layout.addWidget(close_btn, 0)

        main_layout = QtWidgets.QVBoxLayout(self)
        main_layout.addWidget(info_widget, 0)
        main_layout.addStretch(1)
        main_layout.addWidget(footer_widget, 0)

        close_btn.clicked.connect(self._on_close_click)

        self._first_show = True
        self._message_label = message_label
        self._common_label = common_label

    def resizeEvent(self, event):
        super().resizeEvent(event)
        print(self.size())

    def showEvent(self, event):
        super().showEvent(event)
        if self._first_show:
            self._first_show = False
            self._on_first_show()

    def _on_first_show(self):
        self.setStyleSheet(load_stylesheet())
        self.resize(self.default_width, self.default_height)
        self._center_window()

    def _center_window(self):
        """Move window to center of screen."""

        if hasattr(QtWidgets.QApplication, "desktop"):
            desktop = QtWidgets.QApplication.desktop()
            screen_idx = desktop.screenNumber(self)
            screen_geo = desktop.screenGeometry(screen_idx)
        else:
            screen = self.screen()
            screen_geo = screen.geometry()

        geo = self.frameGeometry()
        geo.moveCenter(screen_geo.center())
        if geo.y() < screen_geo.y():
            geo.setY(screen_geo.y())
        self.move(geo.topLeft())

    def _on_close_click(self):
        self.close()

ServerLoginWindow

Bases: QDialog

Source code in common/ayon_common/connection/ui/login_window.py
 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
class ServerLoginWindow(QtWidgets.QDialog):
    default_width = 410
    default_height = 170

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        icon_path = get_icon_path()
        icon = QtGui.QIcon(icon_path)
        self.setWindowIcon(icon)
        self.setWindowTitle("Login to server")

        edit_icon_path = get_resource_path("edit.png")
        edit_icon = QtGui.QIcon(edit_icon_path)

        # --- URL page ---
        login_bg_widget = QtWidgets.QWidget(self)

        login_widget = QtWidgets.QWidget(login_bg_widget)

        user_cred_widget = QtWidgets.QWidget(login_widget)

        url_label = QtWidgets.QLabel("URL:", user_cred_widget)

        url_widget = QtWidgets.QWidget(user_cred_widget)

        url_input = PlaceholderLineEdit(url_widget)
        url_input.setPlaceholderText("< https://ayon.server.com >")

        url_preview = QtWidgets.QLineEdit(url_widget)
        url_preview.setReadOnly(True)
        url_preview.setObjectName("LikeDisabledInput")

        url_edit_btn = PressHoverButton(user_cred_widget)
        url_edit_btn.setIcon(edit_icon)
        url_edit_btn.setObjectName("PasswordBtn")

        url_layout = QtWidgets.QHBoxLayout(url_widget)
        url_layout.setContentsMargins(0, 0, 0, 0)
        url_layout.addWidget(url_input, 1)
        url_layout.addWidget(url_preview, 1)

        # --- URL separator ---
        url_cred_sep = QtWidgets.QFrame(login_bg_widget)
        url_cred_sep.setObjectName("Separator")
        url_cred_sep.setMinimumHeight(2)
        url_cred_sep.setMaximumHeight(2)

        # --- Login page ---
        login_ayon_btn = QtWidgets.QPushButton(
            "Login with AYON server", login_widget
        )
        login_ayon_btn.setIcon(QtGui.QIcon(get_ayon_default_icon_path()))
        login_ayon_btn.setObjectName("AYONLoginButton")

        login_or_sep = OrSeparator(login_bg_widget)

        username_label = QtWidgets.QLabel("Username:", user_cred_widget)

        username_widget = QtWidgets.QWidget(user_cred_widget)

        username_input = PlaceholderLineEdit(username_widget)
        username_input.setPlaceholderText("< Artist >")

        username_preview = QtWidgets.QLineEdit(username_widget)
        username_preview.setReadOnly(True)
        username_preview.setObjectName("LikeDisabledInput")

        username_edit_btn = PressHoverButton(user_cred_widget)
        username_edit_btn.setIcon(edit_icon)
        username_edit_btn.setObjectName("PasswordBtn")

        username_layout = QtWidgets.QHBoxLayout(username_widget)
        username_layout.setContentsMargins(0, 0, 0, 0)
        username_layout.addWidget(username_input, 1)
        username_layout.addWidget(username_preview, 1)

        password_label = QtWidgets.QLabel("Password:", user_cred_widget)
        password_input = PlaceholderLineEdit(user_cred_widget)
        password_input.setPlaceholderText("< *********** >")
        password_input.setEchoMode(PlaceholderLineEdit.Password)

        api_label = QtWidgets.QLabel("API key:", user_cred_widget)
        api_preview = QtWidgets.QLineEdit(user_cred_widget)
        api_preview.setReadOnly(True)
        api_preview.setObjectName("LikeDisabledInput")

        show_password_btn = ShowPasswordButton(user_cred_widget)
        # Cannot be focused, user has to click with mouse
        show_password_btn.setFocusPolicy(QtCore.Qt.NoFocus)

        cred_msg_sep = QtWidgets.QFrame(login_bg_widget)
        cred_msg_sep.setObjectName("Separator")
        cred_msg_sep.setMinimumHeight(2)
        cred_msg_sep.setMaximumHeight(2)

        # --- Credentials inputs ---
        user_cred_layout = QtWidgets.QGridLayout(user_cred_widget)
        user_cred_layout.setContentsMargins(0, 0, 0, 0)
        user_cred_layout.setSpacing(6)
        row = 0

        user_cred_layout.addWidget(url_label, row, 0, 1, 1)
        user_cred_layout.addWidget(url_widget, row, 1, 1, 1)
        user_cred_layout.addWidget(url_edit_btn, row, 2, 1, 1)
        row += 1

        user_cred_layout.addWidget(url_cred_sep, row, 0, 1, 3)
        row += 1

        user_cred_layout.addWidget(login_ayon_btn, row, 0, 1, 3)
        row += 1

        user_cred_layout.addWidget(login_or_sep, row, 0, 1, 3)
        row += 1

        user_cred_layout.addWidget(username_label, row, 0, 1, 1)
        user_cred_layout.addWidget(username_widget, row, 1, 1, 1)
        user_cred_layout.addWidget(username_edit_btn, row, 2, 2, 1)
        row += 1

        user_cred_layout.addWidget(api_label, row, 0, 1, 1)
        user_cred_layout.addWidget(api_preview, row, 1, 1, 1)
        row += 1

        user_cred_layout.addWidget(password_label, row, 0, 1, 1)
        user_cred_layout.addWidget(password_input, row, 1, 1, 1)
        user_cred_layout.addWidget(show_password_btn, row, 2, 1, 1)
        row += 1

        user_cred_layout.addWidget(cred_msg_sep, row, 0, 1, 3)
        row += 1

        user_cred_layout.setColumnStretch(0, 0)
        user_cred_layout.setColumnStretch(1, 1)
        user_cred_layout.setColumnStretch(2, 0)

        login_layout = QtWidgets.QVBoxLayout(login_widget)
        login_layout.setContentsMargins(0, 0, 0, 0)
        login_layout.addWidget(user_cred_widget, 1)

        # --- Messages ---
        # Messages for users (e.g. invalid url etc.)
        message_label = QtWidgets.QLabel(login_bg_widget)
        message_label.setWordWrap(True)
        message_label.setTextInteractionFlags(QtCore.Qt.TextBrowserInteraction)

        footer_widget = QtWidgets.QWidget(login_bg_widget)
        logout_btn = QtWidgets.QPushButton("Logout", footer_widget)
        retry_btn = QtWidgets.QPushButton("Try again", footer_widget)
        retry_btn.setObjectName("AYONRetryButton")
        retry_btn.setVisible(False)
        login_btn = QtWidgets.QPushButton("Login", footer_widget)
        confirm_btn = QtWidgets.QPushButton("Confirm", footer_widget)

        # Disable default button behavior
        # - it is handled based on current input state
        for btn in (
            show_password_btn,
            login_ayon_btn,
            logout_btn,
            retry_btn,
            login_btn,
            confirm_btn,
        ):
            btn.setDefault(False)
            btn.setAutoDefault(False)

        footer_layout = QtWidgets.QHBoxLayout(footer_widget)
        footer_layout.setContentsMargins(0, 0, 0, 0)
        footer_layout.setSpacing(6)
        footer_layout.addWidget(logout_btn, 0)
        footer_layout.addStretch(1)
        footer_layout.addWidget(retry_btn, 0)
        footer_layout.addStretch(1)
        footer_layout.addWidget(login_btn, 0)
        footer_layout.addWidget(confirm_btn, 0)

        login_bg_layout = QtWidgets.QVBoxLayout(login_bg_widget)
        login_bg_layout.setContentsMargins(0, 0, 0, 0)
        login_bg_layout.setSpacing(6)
        login_bg_layout.addWidget(login_widget, 0)
        login_bg_layout.addWidget(message_label, 0)
        login_bg_layout.addStretch(1)
        login_bg_layout.addWidget(footer_widget, 0)

        main_layout = QtWidgets.QVBoxLayout(self)
        main_layout.setContentsMargins(10, 10, 10, 10)
        main_layout.setSpacing(6)
        main_layout.addWidget(login_bg_widget, 1)

        # --- Overlay ---
        overlay_frame = OverlayWidget(self)
        overlay_frame.set_visible(False)

        server_timer = QtCore.QTimer()
        server_timer.setInterval(100)

        server_timer.timeout.connect(self._on_server_timer)
        overlay_frame.cancelled.connect(self._on_server_cancel)
        url_input.textChanged.connect(self._on_url_change)
        url_input.returnPressed.connect(self._on_url_enter_press)
        login_ayon_btn.clicked.connect(self._login_with_ayon_server)
        username_input.textChanged.connect(self._on_user_change)
        username_input.returnPressed.connect(self._on_username_enter_press)
        password_input.returnPressed.connect(self._on_password_enter_press)
        show_password_btn.state_changed.connect(self._on_password_state_change)
        url_edit_btn.clicked.connect(self._on_url_edit_click)
        username_edit_btn.clicked.connect(self._on_username_edit_click)
        logout_btn.clicked.connect(self._on_logout_click)
        retry_btn.clicked.connect(self._on_retry_click)
        login_btn.clicked.connect(self._on_login_click)
        confirm_btn.clicked.connect(self._on_login_click)

        self._overlay_visible = False
        self._overlay_frame = overlay_frame

        self._login_bg_widget = login_bg_widget

        self._login_widget = login_widget

        self._login_or_sep = login_or_sep
        self._login_ayon_btn = login_ayon_btn

        self._url_widget = url_widget
        self._url_input = url_input
        self._url_preview = url_preview
        self._url_edit_btn = url_edit_btn

        self._user_cred_widget = user_cred_widget
        self._username_input = username_input
        self._username_preview = username_preview
        self._username_edit_btn = username_edit_btn

        self._password_label = password_label
        self._password_input = password_input
        self._show_password_btn = show_password_btn
        self._api_label = api_label
        self._api_preview = api_preview

        self._message_label = message_label

        self._logout_btn = logout_btn
        self._retry_btn = retry_btn
        self._login_btn = login_btn
        self._confirm_btn = confirm_btn

        self._url_is_valid = None
        self._credentials_are_valid = None
        self._result = (None, None, None, False)
        self._api_key = None
        self._first_show = True
        self._force_username = False

        self._allow_logout = False
        self._logged_in = False
        self._url_edit_mode = False
        self._username_edit_mode = False

        self._server_timer_counter = 0
        self._server_timer = server_timer
        self._server_handler = None

    def set_allow_logout(self, allow_logout):
        if allow_logout is self._allow_logout:
            return
        self._allow_logout = allow_logout

        self._update_states_by_edit_mode()

    def set_url(self, url):
        self._url_preview.setText(url)
        self._url_input.setText(url)
        self._set_url_valid(self._validate_url())

    def set_username(self, username):
        self._username_preview.setText(username)
        self._username_input.setText(username)

    def set_force_username(self, force_username: bool):
        """Force filled username.

        User cannot change username if enabled.

        Args:
            force_username (bool): If True, username will be forced.

        """
        self._force_username = force_username
        self._username_input.setEnabled(not force_username)
        self._url_input.setEnabled(not force_username)

    def set_logged_in(
        self,
        logged_in: bool,
        url: str | None = None,
        username: str | None = None,
        api_key: str | None = None,
        allow_logout: bool | None = None
    ) -> None:
        if url is not None:
            self.set_url(url)

        if username is not None:
            self.set_username(username)

        if api_key:
            self._set_api_key(api_key)

        if logged_in and allow_logout is None:
            allow_logout = True

        self._set_logged_in(logged_in)

        if allow_logout:
            self.set_allow_logout(True)
        elif allow_logout is False:
            self.set_allow_logout(False)

        if self._url_is_valid and api_key and not logged_in:
            self._set_message("<b>Invalid API key</b>")
            self._on_username_edit_click()

    def showEvent(self, event):
        super().showEvent(event)
        if self._first_show:
            self._first_show = False
            self._on_first_show()

        self._update_overlay_position()

    def resizeEvent(self, event):
        super().resizeEvent(event)
        self._update_overlay_position()

    def closeEvent(self, event):
        self._on_server_cancel()
        super().closeEvent(event)

    def result(self):
        """Result url and token or login.

        Returns:
            Union[Tuple[str, str], Tuple[None, None]]: Url and token used for
                login if was successful otherwise are both set to None.
        """
        return self._result

    def _set_logged_in(self, logged_in: bool) -> None:
        if logged_in is self._logged_in:
            return
        self._logged_in = logged_in

        self._update_states_by_edit_mode()

    def _set_url_edit_mode(self, edit_mode):
        if self._url_edit_mode is not edit_mode:
            self._url_edit_mode = edit_mode
            self._update_states_by_edit_mode()

    def _set_username_edit_mode(self, edit_mode):
        if self._username_edit_mode is not edit_mode:
            self._username_edit_mode = edit_mode
            self._update_states_by_edit_mode()

    def _get_url_user_edit(self):
        url_edit = True
        if self._logged_in and not self._url_edit_mode:
            url_edit = False
        user_edit = url_edit
        if not user_edit and self._logged_in and self._username_edit_mode:
            user_edit = True
        return url_edit, user_edit

    def _update_states_by_edit_mode(self):
        url_edit, user_edit = self._get_url_user_edit()

        self._url_preview.setVisible(not url_edit)
        self._url_input.setVisible(url_edit)
        self._url_edit_btn.setVisible(self._allow_logout and not url_edit)

        self._login_ayon_btn.setVisible(user_edit)
        self._login_or_sep.setVisible(user_edit)

        if self._url_edit_mode or self._username_edit_mode:
            self._api_key = None
            self._retry_btn.setVisible(False)
        else:
            self._retry_btn.setVisible(
                not self._url_is_valid
                and self._api_key is not None
            )

        self._username_preview.setVisible(not user_edit)
        self._username_input.setVisible(user_edit)
        self._username_edit_btn.setVisible(
            self._allow_logout and not user_edit
        )

        self._api_preview.setVisible(not user_edit)
        self._api_label.setVisible(not user_edit)

        self._password_label.setVisible(user_edit)
        self._show_password_btn.setVisible(user_edit)
        self._password_input.setVisible(user_edit)

        self._logout_btn.setVisible(self._allow_logout and self._logged_in)
        self._login_btn.setVisible(not self._allow_logout)
        self._confirm_btn.setVisible(self._allow_logout)
        self._update_login_btn_state(url_edit, user_edit)

    def _update_login_btn_state(self, url_edit=None, user_edit=None, url=None):
        if url_edit is None:
            url_edit, user_edit = self._get_url_user_edit()

        if url is None:
            url = self._url_input.text()

        enabled = bool(url) and (url_edit or user_edit)

        self._login_btn.setEnabled(enabled)
        self._confirm_btn.setEnabled(enabled)

    def _update_overlay_position(self):
        if not self._overlay_visible:
            return
        self._overlay_frame.resize(self.size())

    def _set_overlay_visible(self, visible):
        if self._overlay_visible is visible:
            return
        self._overlay_visible = visible
        self._overlay_frame.set_visible(visible)
        self._login_bg_widget.setEnabled(not visible)
        self._update_overlay_position()

    def _on_first_show(self):
        self.setStyleSheet(load_stylesheet())
        msh = self.minimumSizeHint()
        self.setMinimumWidth(max(msh.width(), 320))

        self.resize(self.default_width, self.default_height)

        self._center_window()
        if self._allow_logout is None:
            self.set_allow_logout(False)

        self._update_states_by_edit_mode()
        if not self._url_input.text():
            widget = self._url_input
        elif not self._username_input.text():
            widget = self._username_input
        else:
            widget = self._password_input

        self._set_input_focus(widget)

    def _center_window(self):
        """Move window to center of screen."""

        if hasattr(QtWidgets.QApplication, "desktop"):
            desktop = QtWidgets.QApplication.desktop()
            screen_idx = desktop.screenNumber(self)
            screen_geo = desktop.screenGeometry(screen_idx)
        else:
            screen = self.screen()
            screen_geo = screen.geometry()

        geo = self.frameGeometry()
        geo.moveCenter(screen_geo.center())
        if geo.y() < screen_geo.y():
            geo.setY(screen_geo.y())
        self.move(geo.topLeft())

    def _on_url_change(self, text):
        self._update_login_btn_state(url=text)
        self._set_url_valid(None)
        self._set_credentials_valid(None)
        self._url_preview.setText(text)

    def _set_url_valid(self, valid):
        if valid is self._url_is_valid:
            return

        self._url_is_valid = valid
        self._set_input_valid_state(self._url_input, valid)

    def _set_credentials_valid(self, valid):
        if self._credentials_are_valid is valid:
            return

        self._credentials_are_valid = valid
        self._set_input_valid_state(self._username_input, valid)
        self._set_input_valid_state(self._password_input, valid)

    def _on_url_enter_press(self):
        if self._login_ayon_btn.isVisible():
            self._login_with_ayon_server()
        else:
            self._set_input_focus(self._username_input)

    def _on_user_change(self, username: str):
        self._username_preview.setText(username)

    def _on_username_enter_press(self):
        self._set_input_focus(self._password_input)

    def _on_password_enter_press(self):
        self._on_login_click()

    def _on_password_state_change(self, show_password: bool):
        if show_password:
            placeholder_text = "< MySecret124 >"
            echo_mode = QtWidgets.QLineEdit.Normal
        else:
            placeholder_text = "< *********** >"
            echo_mode = QtWidgets.QLineEdit.Password

        self._password_input.setEchoMode(echo_mode)
        self._password_input.setPlaceholderText(placeholder_text)

    def _on_username_edit_click(self):
        self._username_edit_mode = True
        self._update_states_by_edit_mode()

    def _on_url_edit_click(self):
        self._url_edit_mode = True
        self._update_states_by_edit_mode()

    def _on_logout_click(self):
        dialog = LogoutConfirmDialog(self)
        dialog.exec_()
        if dialog.get_result():
            self._result = (None, None, None, True)
            self.accept()

    def _on_retry_click(self):
        self._clear_message()

        url = self._url_input.text()
        api_key = self._api_key
        if not self._url_is_valid:
            self._set_url_valid(self._validate_url())

        if not self._url_is_valid:
            return

        user = get_user(url, api_key)
        if user is not None:
            self._result = (url, api_key, user["name"], False)
            self.accept()
            return

        self._set_credentials_valid(False)
        self._set_message("<b>Invalid API key</b>")
        self._on_username_edit_click()

    def _on_login_click(self):
        self._login()

    def _validate_url(self):
        """Use url from input to connect and change window state on success.

        Todos:
            Threaded check.
        """

        url = self._url_input.text()
        if not url.strip():
            return False

        valid_url = None
        try:
            valid_url = validate_url(url)

        except UrlError as exc:
            parts = [f"<b>{exc.title}</b>"]
            parts.extend(f"- {hint}" for hint in exc.hints)
            self._set_message("<br/>".join(parts))

        except KeyboardInterrupt:
            # Reraise KeyboardInterrupt error
            raise

        except BaseException:
            self._set_unexpected_error()
            return False

        if valid_url is None:
            return False

        self._url_input.setText(valid_url)
        return True

    def _login(self):
        if (
            not self._login_btn.isEnabled()
            and not self._confirm_btn.isEnabled()
        ):
            return

        if not self._url_is_valid:
            self._set_url_valid(self._validate_url())

        if not self._url_is_valid:
            self._set_input_focus(self._url_input)
            self._set_credentials_valid(None)
            return

        self._clear_message()

        url = self._url_input.text()
        username = self._username_input.text()
        password = self._password_input.text()
        try:
            token = login_to_server(url, username, password)
        except BaseException:
            self._set_unexpected_error()
            return

        if token is not None:
            self._result = (url, token, username, False)
            self.accept()
            return

        self._set_credentials_valid(False)
        message_lines = ["<b>Invalid credentials</b>"]
        if not username.strip():
            message_lines.append("- Username is not filled")

        if not password.strip():
            message_lines.append("- Password is not filled")

        if username and password:
            message_lines.append("- Check your credentials")

        self._set_message("<br/>".join(message_lines))
        self._set_input_focus(self._username_input)

    def _set_input_focus(self, widget):
        widget.setFocus(QtCore.Qt.MouseFocusReason)

    def _set_input_valid_state(self, widget, valid):
        state = ""
        if valid is True:
            state = "valid"
        elif valid is False:
            state = "invalid"
        set_style_property(widget, "state", state)

    def _set_message(self, message):
        self._message_label.setText(message)

    def _clear_message(self):
        self._message_label.setText("")

    def _set_unexpected_error(self):
        # TODO add traceback somewhere
        # - maybe a button to show or copy?
        traceback.print_exc()
        lines = [
            "<b>Unexpected error happened</b>",
            "- Can be caused by wrong url (leading elsewhere)"
        ]
        self._set_message("<br/>".join(lines))

    def _set_api_key(self, api_key: str | None) -> None:
        if not api_key or len(api_key) < 3:
            self._api_preview.setText(api_key or "")
            self._api_key = None
            self._retry_btn.setVisible(False)
            return

        self._api_key = api_key
        self._retry_btn.setVisible(not self._url_is_valid)

        api_key_len = len(api_key)
        offset = 6
        if api_key_len < offset:
            offset = api_key_len // 2
        api_key = api_key[:offset] + "." * (api_key_len - offset)

        self._api_preview.setText(api_key)

    def _login_with_ayon_server(self):
        url = self._url_input.text()
        if not url.strip():
            self._set_input_focus(self._url_input)
            self._set_url_valid(False)
            self._set_message("<b>AYON url is not filled</b>")
            return

        if (
            not self._login_btn.isEnabled()
            and not self._confirm_btn.isEnabled()
        ):
            return

        if not self._url_is_valid:
            self._set_url_valid(self._validate_url())

        if not self._url_is_valid:
            self._set_input_focus(self._url_input)
            self._set_credentials_valid(None)
            return

        self._clear_message()

        try:
            version = get_server_version(url)
        except Exception:
            self._set_message(
                "<b>Server is not responding</b>"
                "<br/>- Failed to get AYON server version."
            )
            return

        if version < (1, 3, 2):
            self._set_message(
                "<b>AYON server does not support easy login</b>"
                "<br/>- Server version must be at least 1.3.2"
            )
            return

        self._set_overlay_visible(True)

        if self._server_handler is not None:
            self._server_handler.stop()
            self._server_handler = None
        self._server_handler = LoginServerListener(url)
        redir_url = f"http://localhost:{self._server_handler.port}"
        webbrowser.open_new_tab(f"{url}/?auth_redirect={redir_url}")
        self._server_handler.start()
        self._server_timer.start()

    def _stop_server_handler(self):
        server_handler, self._server_handler = self._server_handler, None
        if server_handler is None:
            return

        server_handler.stop()
        self._set_overlay_visible(False)

    def _on_server_cancel(self):
        self._stop_server_handler()

    def _on_server_timer(self):
        if self._server_handler is None:
            self._server_timer.stop()
            self._set_overlay_visible(False)
            return

        token = self._server_handler.get_token()
        if not token:
            return

        # TODO better solution
        # This is hack to allow server serve the page resources for a little
        # bit longer
        if self._server_timer_counter < 3:
            self._server_timer_counter += 1
            return

        # Stop server and timer
        self._stop_server_handler()
        self._server_timer.stop()

        # Collect user data
        url = self._url_input.text()
        user = get_user(url, token)
        username = user["name"]
        # Validate username if is forced
        input_username = self._username_input.text()
        if self._force_username and username != input_username:
            # Different user was used
            self._set_message(
                "<b>Invalid user</b>"
                f"<br/>You're logged as '{username}' in your default"
                f" browser but user '{input_username}' should be used."
                "<br/>Please change user in your default browser, or login"
                " using credentials."
            )
            return

        self._result = (url, token, username, False)
        self.accept()

result()

Result url and token or login.

Returns:

Type Description

Union[Tuple[str, str], Tuple[None, None]]: Url and token used for login if was successful otherwise are both set to None.

Source code in common/ayon_common/connection/ui/login_window.py
597
598
599
600
601
602
603
604
def result(self):
    """Result url and token or login.

    Returns:
        Union[Tuple[str, str], Tuple[None, None]]: Url and token used for
            login if was successful otherwise are both set to None.
    """
    return self._result

set_force_username(force_username)

Force filled username.

User cannot change username if enabled.

Parameters:

Name Type Description Default
force_username bool

If True, username will be forced.

required
Source code in common/ayon_common/connection/ui/login_window.py
537
538
539
540
541
542
543
544
545
546
547
548
def set_force_username(self, force_username: bool):
    """Force filled username.

    User cannot change username if enabled.

    Args:
        force_username (bool): If True, username will be forced.

    """
    self._force_username = force_username
    self._username_input.setEnabled(not force_username)
    self._url_input.setEnabled(not force_username)

ask_to_login(url=None, username=None, api_key=None, force_username=None, always_on_top=False)

Ask user to login using Qt dialog.

Function creates new QApplication if is not created yet.

Parameters:

Name Type Description Default
url Optional[str]

Server url that will be prefilled in dialog.

None
username Optional[str]

Username that will be prefilled in dialog.

None
api_key Optional[str]

API token that will be prefilled in dialog.

None
force_username Optional[bool]

If True, username passed to function will be forced.

None
always_on_top Optional[bool]

Window will be drawn on top of other windows.

False

Returns:

Type Description

tuple[str, str, str]: Returns Url, user's token and username. Url can be changed during dialog lifetime that's why the url is returned.

Source code in common/ayon_common/connection/ui/login_window.py
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
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
def ask_to_login(
    url: str | None = None,
    username: str | None = None,
    api_key: str | None = None,
    force_username: bool | None = None,
    always_on_top: bool = False,
):
    """Ask user to login using Qt dialog.

    Function creates new QApplication if is not created yet.

    Args:
        url (Optional[str]): Server url that will be prefilled in dialog.
        username (Optional[str]): Username that will be prefilled in dialog.
        api_key (Optional[str]): API token that will be prefilled in dialog.
        force_username (Optional[bool]): If True, username passed to function
            will be forced.
        always_on_top (Optional[bool]): Window will be drawn on top of
            other windows.

    Returns:
        tuple[str, str, str]: Returns Url, user's token and username. Url can
            be changed during dialog lifetime that's why the url is returned.
    """

    app_instance = get_qt_app()

    window = ServerLoginWindow()
    if always_on_top:
        window.setWindowFlags(
            window.windowFlags()
            | QtCore.Qt.WindowStaysOnTopHint
        )

    if api_key and url and username:
        window.set_logged_in(
            True,
            url=url,
            username=username,
            api_key=api_key,
        )

    else:
        if url:
            window.set_url(url)

        if username:
            window.set_username(username)

    if force_username is None:
        force_username = False
    window.set_force_username(force_username)

    if not app_instance.startingUp():
        window.show()
        window.raise_()
        window.activateWindow()
        window.showNormal()
        window.exec_()
    else:
        window.open()
        app_instance.exec_()
    result = window.result()
    out_url, out_token, out_username, _ = result
    return out_url, out_token, out_username

change_user(url, username, api_key, always_on_top=False)

Ask user to login using Qt dialog.

Function creates new QApplication if is not created yet.

Parameters:

Name Type Description Default
url str

Server url that will be prefilled in dialog.

required
username str

Username that will be prefilled in dialog.

required
api_key str

API key that will be prefilled in dialog.

required
always_on_top Optional[bool]

Window will be drawn on top of other windows.

False

Returns:

Type Description

Tuple[str, str]: Returns Url and user's token. Url can be changed during dialog lifetime that's why the url is returned.

Source code in common/ayon_common/connection/ui/login_window.py
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
def change_user(url, username, api_key, always_on_top=False):
    """Ask user to login using Qt dialog.

    Function creates new QApplication if is not created yet.

    Args:
        url (str): Server url that will be prefilled in dialog.
        username (str): Username that will be prefilled in dialog.
        api_key (str): API key that will be prefilled in dialog.
        always_on_top (Optional[bool]): Window will be drawn on top of
            other windows.

    Returns:
        Tuple[str, str]: Returns Url and user's token. Url can be changed
            during dialog lifetime that's why the url is returned.
    """

    app_instance = get_qt_app()
    window = ServerLoginWindow()
    if always_on_top:
        window.setWindowFlags(
            window.windowFlags()
            | QtCore.Qt.WindowStaysOnTopHint
        )
    window.set_logged_in(True, url, username, api_key)

    if not app_instance.startingUp():
        window.exec_()
    else:
        window.open()
        # This can become main Qt loop. Maybe should live elsewhere
        app_instance.exec_()
    return window.result()

invalid_credentials(message, always_on_top=False)

Tell user that his credentials are invalid.

This functionality is used when credentials that user did use were not received from keyring or login window.

Parameters:

Name Type Description Default
message str

Some information that can caller define.

required
always_on_top Optional[bool]

Window will be drawn on top of other windows.

False
Source code in common/ayon_common/connection/ui/invalid_window.py
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
def invalid_credentials(message, always_on_top=False):
    """Tell user that his credentials are invalid.

    This functionality is used when credentials that user did use were
        not received from keyring or login window.

    Args:
        message (str): Some information that can caller define.
        always_on_top (Optional[bool]): Window will be drawn on top of
            other windows.
    """

    app_instance = get_qt_app()
    window = InvalidCredentialsWindow(message)
    if always_on_top:
        window.setWindowFlags(
            window.windowFlags()
            | QtCore.Qt.WindowStaysOnTopHint
        )

    if not app_instance.startingUp():
        window.exec_()
    else:
        window.open()
        # This can become main Qt loop. Maybe should live elsewhere
        app_instance.exec_()