summaryrefslogtreecommitdiffstats
path: root/extensions/44/hibernate-status/confirmDialog.js
blob: 8d56ed9708754a40e9135a35f2b867b38c760791 (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
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
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Mainloop = imports.mainloop;

const LoginManager = imports.misc.loginManager;
const Main = imports.ui.main;
const StatusSystem = imports.ui.status.system;
const PopupMenu = imports.ui.popupMenu;
const ModalDialog = imports.ui.modalDialog;
const CheckBox = imports.ui.checkBox.CheckBox;
const St = imports.gi.St;
const Clutter = imports.gi.Clutter;
const ExtensionUtils = imports.misc.extensionUtils;

// Use __ () and N__() for the extension gettext domain, and reuse
// the shell domain with the default _() and N_()
const Gettext = imports.gettext.domain('hibernate-status-button');
const __ = Gettext.gettext;
const N__ = function(e) { return e };

ExtensionUtils.initTranslations('hibernate-status-button');

var HibernateDialogContent = {
    subject: C_("title", __("Hibernate")),
    description: __("Do you really want to hibernate the system?"),
    confirmButtons: [{
        signal: 'Cancel',
        label: C_("button", __("Cancel")),
        key: Clutter.Escape
    },
    {
        signal: 'ConfirmedHibernate',
        label: C_("button", __("Hibernate")),
        default: true
    }],
    iconName: 'document-save-symbolic',
    iconStyleClass: 'end-session-dialog-shutdown-icon',
};

var SystemdMissingDialogContent = {
    subject: C_("title", __("Hibernate button: Systemd Missing")),
    description: __("Systemd seems to be missing and is required."),
    confirmButtons: [{
        signal: 'Cancel',
        label: C_("button", __("Cancel")),
        key: Clutter.Escape
    },
    {
        signal: 'DisableExtension',
        label: C_("button", __("Disable Extension")),
        default: true
    }],
    iconName: 'document-save-symbolic',
    iconStyleClass: 'end-session-dialog-shutdown-icon',
};


var HibernateFailedDialogContent = {
    subject: C_("title", __("Hibernate button: Hibernate failed")),
    description: __("Looks like hibernation failed.\n" +
        "On some linux distributions hibernation is disabled\n" +
        "because not all hardware supports it well;\n" +
        "please check your distribution documentation\n" +
        "on how to enable it."),
    checkBox: __("You are wrong, don't check this anymore!"),
    confirmButtons: [{
        signal: 'Cancel',
        label: C_("button", __("Cancel")),
        key: Clutter.Escape
    },
    {
        signal: 'DisableExtension',
        label: C_("button", __("Disable Extension")),
        default: true
    }],
    iconName: 'document-save-symbolic',
    iconStyleClass: 'end-session-dialog-shutdown-icon',
};

const _DIALOG_ICON_SIZE = 32;

function _setLabelText(label, text) {
    if (text) {
        label.set_text(text);
        label.show();
    } else {
        label.set_text('');
        label.hide();
    }
}

var ConfirmDialog = GObject.registerClass({
    Signals: { 'ConfirmedHibernate': { param_types: [ GObject.TYPE_BOOLEAN ] },
               'DisableExtension': { param_types: [ GObject.TYPE_BOOLEAN ] },
               'Cancel': { param_types: [ GObject.TYPE_BOOLEAN ] } }
},
class ConfirmDialog extends ModalDialog.ModalDialog {
    _init(dialog) {
        super._init({
            styleClass: 'end-session-dialog',
            destroyOnClose: true
        });

        let mainContentLayout = new St.BoxLayout({
            vertical: false,
            x_expand: true ,
            y_expand: false
        });
        this.contentLayout.add(mainContentLayout);

        this._iconBin = new St.Bin({
            x_expand: true,
            y_expand: false,
            x_align: St.Align.END,
            y_align: St.Align.START
        });
        mainContentLayout.add(this._iconBin);

        let messageLayout = new St.BoxLayout({
            vertical: true,
            y_align: St.Align.START
        });
        mainContentLayout.add(messageLayout);

        this._subjectLabel = new St.Label({
            style_class: 'end-session-dialog-subject',
            y_expand: false,
            y_align: St.Align.START
        });

        messageLayout.add(this._subjectLabel);

        this._descriptionLabel = new St.Label({
            style_class: 'end-session-dialog-description',
            y_expand: true,
            y_align: St.Align.START
        });

        messageLayout.add(this._descriptionLabel);

        // fill dialog

        _setLabelText(this._descriptionLabel, dialog.description);
        _setLabelText(this._subjectLabel, dialog.subject);

        if (dialog.iconName) {
            this._iconBin.child = new St.Icon({
                icon_name: dialog.iconName,
                icon_size: _DIALOG_ICON_SIZE,
                style_class: dialog.iconStyleClass
            });
        }

        if (dialog.checkBox) {
            this._checkBox = new CheckBox(dialog.checkBox);
            mainContentLayout.add(this._checkBox.actor);
        }

        let buttons = [];
        for (let i = 0; i < dialog.confirmButtons.length; i++) {
            let signal = dialog.confirmButtons[i].signal;
            let label = dialog.confirmButtons[i].label;
            let keys = dialog.confirmButtons[i].key;
            buttons.push({
                action: () => {
                    let signalId = this.connect('closed',
                        () => {
                            this.disconnect(signalId);
                            this._confirm(signal);
                        });
                    this.close();
                },
                label: label,
                key: keys
            });
        };

        this.setButtons(buttons);

    }

    _confirm(signal) {
        var checked;
        if (this._checkBox)
            checked = this._checkBox.actor.get_checked()
        this.emit(signal, checked);
    }

    cancel() {
        this.close();
    }
});