~l-admin-3/evnc/automatic-cleanup-branch

« back to all changes in this revision

Viewing changes to src/Widgets/VncConnection.vala

  • Committer: Marcus Wichelmann
  • Date: 2015-07-09 18:17:05 UTC
  • Revision ID: marcus.wichelmann@hotmail.de-20150709181705-aliu5vv3gvha1p0a
Automatic code cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
*/
18
18
 
19
19
namespace evnc.Widgets {
20
 
        public class VncConnection : Granite.Widgets.Tab {
21
 
                public signal void on_save_screenshot_exception (string message);
22
 
 
23
 
                public string host;
24
 
                public string port;
25
 
                public string user;
26
 
                public string desc;
27
 
 
28
 
                private SettingsManager options;
29
 
 
30
 
                private Gtk.Grid grid;
31
 
 
32
 
                private Gtk.ScrolledWindow scrolledwindow;
33
 
 
34
 
                private Gtk.Viewport viewport;
35
 
 
36
 
                public Vnc.Display display;
37
 
 
38
 
                public VncConnection (string host, string port, string user) {
39
 
                        desc = _("Connecting...");
40
 
                        this.label = desc;
41
 
 
42
 
                        options = new SettingsManager ();
43
 
                        options.on_display_mode_changed.connect ((val) => {
44
 
                                set_size_mode (val);
45
 
                        });
46
 
 
47
 
                        grid = new Gtk.Grid ();
48
 
                        scrolledwindow = new Gtk.ScrolledWindow (null, null);
49
 
                        viewport = new Gtk.Viewport (null, null);
50
 
                        display = new Vnc.Display ();
51
 
 
52
 
                        viewport.add (display);
53
 
                        scrolledwindow.add (viewport);
54
 
                        grid.attach (scrolledwindow, 0, 0, 1, 1);
55
 
 
56
 
                        scrolledwindow.set_hexpand (true);
57
 
                        scrolledwindow.set_vexpand (true);
58
 
                        scrolledwindow.draw.connect (() => {
59
 
                                if (options.display_mode == 0) {
60
 
                                        update_view_border ();
61
 
                                }
62
 
 
63
 
                                return false;
64
 
                        });
65
 
 
66
 
                        var bg_color = Gdk.RGBA();
67
 
                        bg_color.red = 0;
68
 
                        bg_color.green = 0;
69
 
                        bg_color.blue = 0;
70
 
                        bg_color.alpha = 1;
71
 
                        scrolledwindow.override_background_color (Gtk.StateFlags.NORMAL, bg_color);
72
 
 
73
 
                        this.page = grid;
74
 
 
75
 
                        display.set_credential (Vnc.DisplayCredential.USERNAME, user);
76
 
                        display.set_credential (Vnc.DisplayCredential.CLIENTNAME, "evnc");
77
 
                        display.set_pointer_grab (true);
78
 
                        display.set_keyboard_grab (true);
79
 
                        display.set_pointer_local (options.pointer_local);
80
 
                        display.set_lossy_encoding (options.lossy_encoding);
81
 
                        display.vnc_disconnected.connect (() => {
82
 
                                this.close ();
83
 
                        });
84
 
                        display.vnc_connected.connect (() => {
85
 
                                desc = _("Logging in...");
86
 
                                this.label = desc;
87
 
                        });
88
 
                        display.vnc_initialized.connect (() => {
89
 
                                desc = "";
90
 
                                if (user != "") {
91
 
                                        desc += user + "@";
92
 
                                }
93
 
                                desc += host + ":" + port;
94
 
                                this.label = desc;
95
 
                        });
96
 
                        display.vnc_auth_credential.connect ((cred) => {
97
 
                                var input_box = new Gtk.MessageDialog (null, Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
98
 
                                                Gtk.MessageType.WARNING, Gtk.ButtonsType.OK_CANCEL, _("Password required!"));
99
 
                                input_box.title = _("Connect to:") + " " + host;
100
 
 
101
 
                                var pass_entry = new Gtk.Entry ();
102
 
                                pass_entry.set_size_request (400, 0);
103
 
                                pass_entry.set_visibility (false);
104
 
                                pass_entry.caps_lock_warning = true;
105
 
                                pass_entry.set_placeholder_text (_("Password"));
106
 
                                pass_entry.margin_start = 12;
107
 
                                pass_entry.margin_end = 12;
108
 
 
109
 
                                input_box.get_content_area ().pack_end (pass_entry, true, false, 0);
110
 
                                input_box.show_all ();
111
 
 
112
 
                                if (input_box.run () == Gtk.ResponseType.OK) {
113
 
                                        display.set_credential (Vnc.DisplayCredential.PASSWORD, pass_entry.get_text ());
114
 
                                } else {
115
 
                                        this.close ();
116
 
                                }
117
 
 
118
 
                                input_box.destroy ();
119
 
                        });
120
 
 
121
 
                        set_color_depth (options.color_depth);
122
 
                        options.settings.bind("readonly", display, "read_only", SettingsBindFlags.DEFAULT);
123
 
 
124
 
                        if (display.open_host (host, port)) {
125
 
                                this.host = host;
126
 
                                this.port = port;
127
 
                                this.user = user;
128
 
                        }
129
 
 
130
 
                        set_size_mode  (options.display_mode);
131
 
                }
132
 
 
133
 
                public void take_screenshot () {
134
 
                        var img = display.get_pixbuf ();
135
 
 
136
 
                        var save_dialog = new Gtk.FileChooserDialog (_("Save the screenshot"), null, Gtk.FileChooserAction.SAVE,
137
 
                                        _("Cancel"), Gtk.ResponseType.CANCEL, _("Save"), Gtk.ResponseType.ACCEPT);
138
 
 
139
 
                        var file_filter = new Gtk.FileFilter ();
140
 
                        file_filter.add_mime_type ("image/jpeg");
141
 
 
142
 
                        save_dialog.set_filter (file_filter);
143
 
 
144
 
                        var preview_image = new Gtk.Image ();
145
 
                        preview_image.set_from_pixbuf (img.scale_simple (150, (int)(((float)150 / (float)img.width) * img.height), Gdk.InterpType.BILINEAR));
146
 
 
147
 
                        save_dialog.set_preview_widget (preview_image);
148
 
 
149
 
                        preview_image.show ();
150
 
 
151
 
                        if (save_dialog.run () == Gtk.ResponseType.ACCEPT) {
152
 
                                try {
153
 
                                        img.save (save_dialog.get_filename (), "jpeg");
154
 
                                } catch (Error ex) {
155
 
                                        on_save_screenshot_exception (ex.message);
156
 
                                }
157
 
                        }
158
 
 
159
 
                        save_dialog.close ();
160
 
                        save_dialog.destroy ();
161
 
                }
162
 
 
163
 
                private void update_view_border () {
164
 
                        if (scrolledwindow.get_allocated_width () > 0 && scrolledwindow.get_allocated_height () > 0 && display.width > 0 && display.height > 0) {
165
 
                                if (((float)scrolledwindow.get_allocated_width () / (float)display.width) * (float)display.height > scrolledwindow.get_allocated_height ()) {
166
 
                                        int border_size = (int)(((float)scrolledwindow.get_allocated_width () - (((float)scrolledwindow.get_allocated_height () / (float)display.height) * (float)display.width)) / 2);
167
 
                                        viewport.margin_start = border_size;
168
 
                                        viewport.margin_end = border_size;
169
 
                                        viewport.margin_top = 0;
170
 
                                        viewport.margin_bottom = 0;
171
 
                                } else {
172
 
                                        int border_size = (int)(((float)scrolledwindow.get_allocated_height () - (((float)scrolledwindow.get_allocated_width () / (float)display.width) * (float)display.height)) / 2);
173
 
                                        viewport.margin_start = 0;
174
 
                                        viewport.margin_end = 0;
175
 
                                        viewport.margin_top = border_size;
176
 
                                        viewport.margin_bottom = border_size;
177
 
                                }
178
 
                        }
179
 
                }
180
 
 
181
 
                private void set_size_mode (int val) {
182
 
                        if (val == 0) {
183
 
                                        display.force_size = false;
184
 
                                        display.scaling = true;
185
 
                                        display.set_size_request (0, 0);
186
 
 
187
 
                                        update_view_border ();
188
 
                                } else {
189
 
                                        viewport.margin = 0;
190
 
 
191
 
                                        display.force_size = true;
192
 
                                        display.scaling = false;
193
 
                                        display.set_size_request (display.width, display.height);
194
 
                                }
195
 
                }
196
 
 
197
 
                private void set_color_depth (string val) {
198
 
                        switch (val) {
199
 
                                case "default":
200
 
                                        display.set_depth (Vnc.DisplayDepthColor.DEFAULT);
201
 
                                        break;
202
 
                                case "full":
203
 
                                        display.set_depth (Vnc.DisplayDepthColor.FULL);
204
 
                                        break;
205
 
                                case "medium":
206
 
                                        display.set_depth (Vnc.DisplayDepthColor.MEDIUM);
207
 
                                        break;
208
 
                                case "low":
209
 
                                        display.set_depth (Vnc.DisplayDepthColor.LOW);
210
 
                                        break;
211
 
                                case "ultra low":
212
 
                                        display.set_depth (Vnc.DisplayDepthColor.ULTRA_LOW);
213
 
                                        break;
214
 
                        }
215
 
                }
216
 
        }
 
20
    public class VncConnection : Granite.Widgets.Tab {
 
21
        public signal void on_save_screenshot_exception (string message);
 
22
 
 
23
        public string host;
 
24
        public string port;
 
25
        public string user;
 
26
        public string desc;
 
27
 
 
28
        private SettingsManager options;
 
29
 
 
30
        private Gtk.Grid grid;
 
31
 
 
32
        private Gtk.ScrolledWindow scrolledwindow;
 
33
 
 
34
        private Gtk.Viewport viewport;
 
35
 
 
36
        public Vnc.Display display;
 
37
 
 
38
        public VncConnection (string host, string port, string user) {
 
39
            desc = _ ("Connecting...");
 
40
            this.label = desc;
 
41
 
 
42
            options = new SettingsManager ();
 
43
            options.on_display_mode_changed.connect ((val) => {
 
44
                set_size_mode (val);
 
45
            });
 
46
 
 
47
            grid = new Gtk.Grid ();
 
48
            scrolledwindow = new Gtk.ScrolledWindow (null, null);
 
49
            viewport = new Gtk.Viewport (null, null);
 
50
            display = new Vnc.Display ();
 
51
 
 
52
            viewport.add (display);
 
53
            scrolledwindow.add (viewport);
 
54
            grid.attach (scrolledwindow, 0, 0, 1, 1);
 
55
 
 
56
            scrolledwindow.set_hexpand (true);
 
57
            scrolledwindow.set_vexpand (true);
 
58
            scrolledwindow.draw.connect (() => {
 
59
                if (options.display_mode == 0) {
 
60
                    update_view_border ();
 
61
                }
 
62
 
 
63
                return false;
 
64
            });
 
65
 
 
66
            var bg_color = Gdk.RGBA ();
 
67
            bg_color.red = 0;
 
68
            bg_color.green = 0;
 
69
            bg_color.blue = 0;
 
70
            bg_color.alpha = 1;
 
71
            scrolledwindow.override_background_color (Gtk.StateFlags.NORMAL, bg_color);
 
72
 
 
73
            this.page = grid;
 
74
 
 
75
            display.set_credential (Vnc.DisplayCredential.USERNAME, user);
 
76
            display.set_credential (Vnc.DisplayCredential.CLIENTNAME, "evnc");
 
77
            display.set_pointer_grab (true);
 
78
            display.set_keyboard_grab (true);
 
79
            display.set_pointer_local (options.pointer_local);
 
80
            display.set_lossy_encoding (options.lossy_encoding);
 
81
            display.vnc_disconnected.connect (() => {
 
82
                this.close ();
 
83
            });
 
84
            display.vnc_connected.connect (() => {
 
85
                desc = _ ("Logging in...");
 
86
                this.label = desc;
 
87
            });
 
88
            display.vnc_initialized.connect (() => {
 
89
                desc = "";
 
90
 
 
91
                if (user != "") {
 
92
                    desc += user + "@";
 
93
                }
 
94
 
 
95
                desc += host + ":" + port;
 
96
                this.label = desc;
 
97
            });
 
98
            display.vnc_auth_credential.connect ((cred) => {
 
99
                var input_box = new Gtk.MessageDialog (null, Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
 
100
                                                       Gtk.MessageType.WARNING, Gtk.ButtonsType.OK_CANCEL, _ ("Password required!"));
 
101
                input_box.title = _ ("Connect to:") + " " + host;
 
102
 
 
103
                var pass_entry = new Gtk.Entry ();
 
104
                pass_entry.set_size_request (400, 0);
 
105
                pass_entry.set_visibility (false);
 
106
                pass_entry.caps_lock_warning = true;
 
107
                pass_entry.set_placeholder_text (_ ("Password"));
 
108
                pass_entry.margin_start = 12;
 
109
                pass_entry.margin_end = 12;
 
110
 
 
111
                input_box.get_content_area ().pack_end (pass_entry, true, false, 0);
 
112
                input_box.show_all ();
 
113
 
 
114
                if (input_box.run () == Gtk.ResponseType.OK) {
 
115
                    display.set_credential (Vnc.DisplayCredential.PASSWORD, pass_entry.get_text ());
 
116
                } else {
 
117
                    this.close ();
 
118
                }
 
119
 
 
120
                input_box.destroy ();
 
121
            });
 
122
 
 
123
            set_color_depth (options.color_depth);
 
124
            options.settings.bind ("readonly", display, "read_only", SettingsBindFlags.DEFAULT);
 
125
 
 
126
            if (display.open_host (host, port)) {
 
127
                this.host = host;
 
128
                this.port = port;
 
129
                this.user = user;
 
130
            }
 
131
 
 
132
            set_size_mode  (options.display_mode);
 
133
        }
 
134
 
 
135
        public void take_screenshot () {
 
136
            var img = display.get_pixbuf ();
 
137
 
 
138
            var save_dialog = new Gtk.FileChooserDialog (_ ("Save the screenshot"), null, Gtk.FileChooserAction.SAVE,
 
139
                    _ ("Cancel"), Gtk.ResponseType.CANCEL, _ ("Save"), Gtk.ResponseType.ACCEPT);
 
140
 
 
141
            var file_filter = new Gtk.FileFilter ();
 
142
            file_filter.add_mime_type ("image/jpeg");
 
143
 
 
144
            save_dialog.set_filter (file_filter);
 
145
 
 
146
            var preview_image = new Gtk.Image ();
 
147
            preview_image.set_from_pixbuf (img.scale_simple (150, (int) (((float)150 / (float)img.width) * img.height),
 
148
                                           Gdk.InterpType.BILINEAR));
 
149
 
 
150
            save_dialog.set_preview_widget (preview_image);
 
151
 
 
152
            preview_image.show ();
 
153
 
 
154
            if (save_dialog.run () == Gtk.ResponseType.ACCEPT) {
 
155
                try {
 
156
                    img.save (save_dialog.get_filename (), "jpeg");
 
157
                } catch (Error ex) {
 
158
                    on_save_screenshot_exception (ex.message);
 
159
                }
 
160
            }
 
161
 
 
162
            save_dialog.close ();
 
163
            save_dialog.destroy ();
 
164
        }
 
165
 
 
166
        private void update_view_border () {
 
167
            if (scrolledwindow.get_allocated_width () > 0 && scrolledwindow.get_allocated_height () > 0 && display.width > 0 &&
 
168
                    display.height > 0) {
 
169
                if (((float)scrolledwindow.get_allocated_width () / (float)display.width) * (float)display.height >
 
170
                        scrolledwindow.get_allocated_height ()) {
 
171
                    int border_size = (int) (((float)scrolledwindow.get_allocated_width () - (((float)
 
172
                                              scrolledwindow.get_allocated_height () / (float)display.height) * (float)display.width)) / 2);
 
173
                    viewport.margin_start = border_size;
 
174
                    viewport.margin_end = border_size;
 
175
                    viewport.margin_top = 0;
 
176
                    viewport.margin_bottom = 0;
 
177
                } else {
 
178
                    int border_size = (int) (((float)scrolledwindow.get_allocated_height () - (((float)
 
179
                                              scrolledwindow.get_allocated_width () / (float)display.width) * (float)display.height)) / 2);
 
180
                    viewport.margin_start = 0;
 
181
                    viewport.margin_end = 0;
 
182
                    viewport.margin_top = border_size;
 
183
                    viewport.margin_bottom = border_size;
 
184
                }
 
185
            }
 
186
        }
 
187
 
 
188
        private void set_size_mode (int val) {
 
189
            if (val == 0) {
 
190
                display.force_size = false;
 
191
                display.scaling = true;
 
192
                display.set_size_request (0, 0);
 
193
 
 
194
                update_view_border ();
 
195
            } else {
 
196
                viewport.margin = 0;
 
197
 
 
198
                display.force_size = true;
 
199
                display.scaling = false;
 
200
                display.set_size_request (display.width, display.height);
 
201
            }
 
202
        }
 
203
 
 
204
        private void set_color_depth (string val) {
 
205
            switch (val) {
 
206
                case "default":
 
207
                    display.set_depth (Vnc.DisplayDepthColor.DEFAULT);
 
208
                    break;
 
209
 
 
210
                case "full":
 
211
                    display.set_depth (Vnc.DisplayDepthColor.FULL);
 
212
                    break;
 
213
 
 
214
                case "medium":
 
215
                    display.set_depth (Vnc.DisplayDepthColor.MEDIUM);
 
216
                    break;
 
217
 
 
218
                case "low":
 
219
                    display.set_depth (Vnc.DisplayDepthColor.LOW);
 
220
                    break;
 
221
 
 
222
                case "ultra low":
 
223
                    display.set_depth (Vnc.DisplayDepthColor.ULTRA_LOW);
 
224
                    break;
 
225
            }
 
226
        }
 
227
    }
217
228
}
 
 
b'\\ No newline at end of file'