aboutsummaryrefslogtreecommitdiff
path: root/launcher/minecraft/auth/flows/MSAHelper.txt
blob: dfaec37493f3ddbc4142b9a94950bf76f0dd5804 (plain)
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
class Helper : public QObject {
    Q_OBJECT

public:
    Helper(MSAFlows * context) : QObject(), context_(context), msg_(QString()) {
        QFile tokenCache("usercache.dat");
        if(tokenCache.open(QIODevice::ReadOnly)) {
            context_->resumeFromState(tokenCache.readAll());
        }
    }

public slots:
    void run() {
        connect(context_, &MSAFlows::activityChanged, this, &Helper::onActivityChanged);
        context_->silentSignIn();
    }

    void onFailed() {
        qDebug() << "Login failed";
    }

    void onActivityChanged(Katabasis::Activity activity) {
        if(activity == Katabasis::Activity::Idle) {
            switch(context_->validity()) {
                case Katabasis::Validity::None: {
                    // account is gone, remove it.
                    QFile::remove("usercache.dat");
                }
                break;
                case Katabasis::Validity::Assumed: {
                    // this is basically a soft-failed refresh. do nothing.
                }
                break;
                case Katabasis::Validity::Certain: {
                    // stuff got refreshed / signed in. Save.
                    auto data = context_->saveState();
                    QSaveFile tokenCache("usercache.dat");
                    if(tokenCache.open(QIODevice::WriteOnly)) {
                        tokenCache.write(context_->saveState());
                        tokenCache.commit();
                    }
                }
                break;
            }
        }
    }

private:
    MSAFlows *context_;
    QString msg_;
};