-
Notifications
You must be signed in to change notification settings - Fork 15
/
login.js
156 lines (124 loc) · 4.12 KB
/
login.js
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
#!/usr/bin/gjs
imports.gi.versions.Gtk = '3.0';
const GLib = imports.gi.GLib;
const Gtk = imports.gi.Gtk;
const WebKit = imports.gi.WebKit2;
const Mainloop = imports.mainloop;
const Lang = imports.lang;
Gtk.init(null);
const WebBrowser = new Lang.Class({
Name: 'WebBrowser',
Extends: Gtk.Application,
// Create the application itself
_init: function () {
this.parent({
application_id: 'OneDrive.login.WebBrowser'
});
this.inFile = GLib.get_tmp_dir() + "/in";
this.outFile = GLib.get_tmp_dir() + "/out";
// Connect 'activate' and 'startup' signals to the callback functions
this.connect('activate', () => this._onActivate())
this.connect('startup', () => this._onStartup())
},
// Callback function for 'activate' signal
_onActivate: function () {
// Present window when active
this._window.present();
GLib.spawn_command_line_async("onedrive --auth-files " + this.inFile + ":" + this.outFile + " --logout");
this.numeroTentativi = 0;
this._attendiIn = Mainloop.timeout_add(500, this.startLogin.bind(this));
},
// Callback function for 'startup' signal
_onStartup: function () {
// Build the UI
this._buildUI();
// Connect the UI signals
this._connectSignals();
},
// Build the application's UI
_buildUI: function () {
// Create the application window
this._window = new Gtk.ApplicationWindow({
application: this,
window_position: Gtk.WindowPosition.CENTER,
default_height: 768,
default_width: 1024,
border_width: 0,
title: "OneDrive login"
});
// Create the WebKit WebView, our window to the web
this._webView = new WebKit.WebView();
// Create a scrolled window to embed the WebView
let scrolledWindow = new Gtk.ScrolledWindow({
hscrollbar_policy: Gtk.PolicyType.AUTOMATIC,
vscrollbar_policy: Gtk.PolicyType.AUTOMATIC
});
scrolledWindow.add(this._webView);
// Create a box to organize everything in
let box = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
homogeneous: false,
spacing: 0
});
// Pack toolbar and scrolled window to the box
//box.pack_start(toolbar, false, true, 0);
box.pack_start(scrolledWindow, true, true, 0);
// Add the box to the window
this._window.add(box);
// Show the window and all child widgets
this._window.show_all();
},
_connectSignals: function () {
// Change the Window title when a new page is loaded
this._webView.connect('notify::title', () => {
this._window.set_title(this._webView.title);
});
// Update the url bar and buttons when a new page is loaded
this._webView.connect('load_changed', (webView, loadEvent) => {
if (loadEvent !== WebKit.LoadEvent.COMMITTED) {
return
}
});
},
startLogin()
{
if(this.numeroTentativi > 10)
{
print("Error in open file with uri");
this.quit();
return false;
}
let url = "";
try
{
url = String(GLib.file_get_contents(this.inFile)[1]);
this._homeUrl = url;
this._webView.load_uri(this._homeUrl);
this._attendiOut = Mainloop.timeout_add(500, this.endLogin.bind(this));
return false;
}
catch(ex)
{
this.numeroTentativi++;
}
return true;
},
endLogin()
{
if(this._webView.get_uri().indexOf("code=") === -1) return true;
let url = "";
try
{
GLib.file_set_contents(this.outFile, this._webView.get_uri());
Mainloop.timeout_add(1000, function() { this.quit(); }.bind(this));
}
catch(ex)
{
print(ex.message);
}
return false;
}
});
// Run the application
let app = new WebBrowser();
app.run(ARGV);